-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjruls.c
334 lines (294 loc) · 7.06 KB
/
jruls.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#include <sys/types.h>
#include <curses.h>
#include <err.h>
#include <errno.h>
#include <inttypes.h>
#include <locale.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/param.h>
#include <sys/jail.h>
#include <sys/rctl.h>
#include <sys/sysctl.h>
#include <sysexits.h>
#include <jail.h>
#include <unistd.h>
/* column widths */
static int cw_jid = 3, cw_name = 20, cw_pct = 4, cw_mem = 6;
static int cw_iops = 5, cw_iovol = 6, cw_cnt = 5;
static int smart_terminal = 1;
static volatile int done = 0;
static void print_n(uint64_t v, int colwidth);
static void print_nmp(uint64_t v, int colwidth);
struct metric
{
const char *label, *name;
const int *colwidth;
void (*print)(uint64_t v, int colwidth);
};
static const struct metric all_metrics[] = {
{ "cpu%", "pcpu", &cw_pct, &print_n },
{ "mem", "memoryuse", &cw_mem, &print_nmp },
{ "memlck", "memorylocked", &cw_mem, &print_nmp },
{ "proc", "maxproc", &cw_cnt, &print_n },
{ "fds", "openfiles", &cw_cnt, &print_nmp },
{ "vmem", "vmemoryuse", &cw_mem, &print_nmp },
{ "ptys", "pseudoterminals", &cw_cnt, &print_n },
{ "swap", "swapuse", &cw_mem, &print_nmp },
{ "thread", "nthr", &cw_cnt, &print_n },
{ "r/s", "readiops", &cw_iops, &print_nmp },
{ "read", "readbps", &cw_iovol, &print_nmp },
{ "w/s", "writeiops", &cw_iops, &print_nmp },
{ "writtn", "writebps", &cw_iovol, &print_nmp },
};
static const size_t tot_metrics =
sizeof (all_metrics) / sizeof (all_metrics[0]);
static const struct metric *metrics[30];
/* Rudimentary I/O abstraction to support smart and dump terminals */
struct iofuncs {
/* x-prefix is a work-around as clear, attron... are macros */
int (*xclear)(void);
int (*xattron)(int attrs);
int (*xattroff)(int attrs);
int (*xprint)(const char *fmt, ...);
int (*xrefresh)(void);
} io = { 0 };
static const struct metric*
find_metric(const char *name)
{
const struct metric *it = all_metrics, *endp = it + tot_metrics;
for (; it != endp; ++it)
if (!strcmp(it->label, name) ||
!strcmp(it->name, name))
return it;
return 0;
}
static uint64_t
findval(const char *list,
const char *name)
{
char *p = strstr(list, name);
if (p) {
while (*p && *p != '=')
++p;
if (*p == '=') {
++p;
return (uint64_t)strtoull(p, 0, 10);
}
}
return 0;
}
static void
print_n(uint64_t v, int colwidth)
{
io.xprint("%*"PRIu64, colwidth, v);
}
static void
print_nmp(uint64_t v, int colwidth)
{
char buf[20];
if (v < 9999)
snprintf(buf, sizeof buf, "%"PRIu64, v);
else {
if (v > 9999 * 1024 * 1024ll)
snprintf(buf, sizeof buf, "%.2fG",
v / (1024 * 1024 * 1024.0));
else if (v > 9999 * 1024)
snprintf(buf, sizeof buf, "%.1fM",
v / (1024 * 1024.0));
else /* if (v > 9999) */
snprintf(buf, sizeof buf, "%.0fK",
v / 1024.0);
}
io.xprint("%*s", colwidth, buf);
}
static void
print_headers(void)
{
io.xclear();
io.xattron(A_BOLD);
io.xprint("%*s %-*s",
cw_jid, "jid",
cw_name, "name");
const struct metric **it = metrics;
for (; *it; ++it) {
io.xprint(" %*s", *(*it)->colwidth, (*it)->label);
}
io.xprint("\n");
io.xattroff(A_BOLD);
}
static void
print_jail(int jid,
const char *name)
{
char q[MAXHOSTNAMELEN + 20], buf[4096];
io.xprint("%*d %-*s",
cw_jid, jid,
cw_name, name);
snprintf(q, sizeof q, "jail:%s:cputime", name);
int rv = rctl_get_racct(q, strlen(q) + 1,
buf, sizeof buf);
if (rv != -1) {
/* buf is comma-delimited sequence of key=value
* pairs: cputime=##,datasize=##,... */
const struct metric **it = metrics;
for (; *it; ++it) {
io.xprint(" ");
(*it)->print(findval(buf, (*it)->name),
*(*it)->colwidth);
}
} else {
if (errno == ENOSYS)
err(EX_OSERR, "rctl_get_racct");
io.xprint(": %s", strerror(errno));
}
io.xprint("\n");
}
static int
no_clear(void)
{
return 0;
}
static int
no_refresh(void)
{
printf("\n");
fflush(stdout);
return 0;
}
static int
no_attrtoggle(int attrs)
{
(void)attrs;
return 0;
}
static void
init_io(void)
{
smart_terminal = getenv("TERM") != 0;
if (smart_terminal) {
smart_terminal = isatty(1);
}
if (smart_terminal) {
/* Initialize curses library */
setlocale(LC_ALL, "");
initscr();
io.xclear = &clear;
io.xattron = &attron;
io.xattroff = &attroff;
io.xprint = &printw;
io.xrefresh = &refresh;
} else { /* dump terminal */
io.xclear = &no_clear;
io.xattron = &no_attrtoggle;
io.xattroff = &no_attrtoggle;
io.xprint = &printf;
io.xrefresh = &no_refresh;
}
}
static void
restore_io(void)
{
if (smart_terminal) {
endwin();
}
}
static void
signal_done(int signo)
{
(void)signo;
done = 1;
}
static int
usage(void)
{
printf("Usage: jruls [-d count] [-s time]\n");
return 0;
}
int
main(int argc, char *argv[])
{
int ena;
size_t ena_len = sizeof ena;
int rv = sysctlbyname("kern.racct.enable", &ena, &ena_len, 0, 0);
if (rv == -1 && errno == ENOENT)
errx(EX_UNAVAILABLE,
"RACCT/RCTL support not compiled; see rctl(8)");
if (!ena)
errx(EX_UNAVAILABLE,
"RACCT/RCTL support not enabled; enable using kern.racct.enable=1 tunable");
int count = INT_MAX, sleep_itv = 2;
int opt;
while ((opt = getopt(argc, argv, "s:d:h")) != -1) {
char *endp = 0;
switch (opt) {
case 's':
sleep_itv = strtol(optarg, &endp, 10);
if (!endp || *endp || sleep_itv <= 0)
errx(EX_USAGE, "illegal time -- '%s'", optarg);
break;
case 'd':
count = strtol(optarg, &endp, 10);
if (!endp || *endp || count < 0)
errx(EX_USAGE, "illegal count -- '%s'", optarg);
break;
default:
usage();
return EX_USAGE;
}
}
if (!metrics[0]) {
/* include default metrics, unless customized */
metrics[0] = find_metric("cpu%");
metrics[1] = find_metric("mem");
metrics[2] = find_metric("r/s");
metrics[3] = find_metric("read");
metrics[4] = find_metric("w/s");
metrics[5] = find_metric("writtn");
}
int jid, lastjid;
char name[MAXHOSTNAMELEN];
struct jailparam param[3]; /* lastjid jid name */
if (jailparam_init(¶m[0], "lastjid") == -1 ||
jailparam_init(¶m[1], "jid") == -1 ||
jailparam_init(¶m[2], "name") == -1)
errx(EX_OSERR, "jailparam_init: %s", jail_errmsg);
if (jailparam_import_raw(¶m[0], &lastjid, sizeof lastjid) == -1 ||
jailparam_import_raw(¶m[1], &jid, sizeof jid) == -1 ||
jailparam_import_raw(¶m[2], name, sizeof name) == -1)
errx(EX_OSERR, "jailparam_import_raw: %s", jail_errmsg);
if (signal(SIGINT, &signal_done) == SIG_ERR ||
signal(SIGTERM, &signal_done) == SIG_ERR)
warn("signal");
init_io();
atexit(&restore_io);
if (count == INT_MAX && !smart_terminal) {
count = 1;
}
do {
int jflags = 0, rv, n;
lastjid = n = 0;
do {
rv = jailparam_get(param, sizeof (param) / sizeof (param[0]), jflags);
if (rv != -1) {
if (++n == 1)
print_headers();
print_jail(*(int*)param[1].jp_value,
(char*)param[2].jp_value);
}
lastjid = rv;
} while (rv >= 0);
if (rv == -1 && errno != ENOENT)
errx(EX_OSERR, "jailparam_get: %s", jail_errmsg);
if (!n)
errx(EX_UNAVAILABLE, "no jails found");
io.xrefresh();
if (count > 1) {
sleep(sleep_itv);
puts("");
}
} while (--count > 0 && !done);
return 0;
}