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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
|
/* $Id: main.c,v 1.51 2008/11/08 06:38:27 canacar Exp $ */
/*
* Copyright (c) 2001, 2007 Can Erkin Acar
* Copyright (c) 2001 Daniel Hartmeier
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
#include <sys/types.h>
#include <sys/param.h>
#include <sys/sysctl.h>
#include <ctype.h>
#include <curses.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <netdb.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <unistd.h>
#include <utmp.h>
#include "engine.h"
#include "systat.h"
double dellave;
kvm_t *kd;
char *nlistf = NULL;
char *memf = NULL;
double avenrun[3];
double naptime = 5.0;
int verbose = 1; /* to report kvm read errs */
int nflag = 1;
int ut, hz, stathz;
char hostname[MAXHOSTNAMELEN];
WINDOW *wnd;
int CMDLINE;
#define TIMEPOS 55
int ucount(void);
void usage(void);
/* command prompt */
void cmd_delay(const char *);
void cmd_count(const char *);
void cmd_compat(const char *);
struct command cm_compat = {"Command", cmd_compat};
struct command cm_delay = {"Seconds to delay", cmd_delay};
struct command cm_count = {"Number of lines to display", cmd_count};
/* display functions */
int
print_header(void)
{
time_t now;
int start = dispstart + 1, end = dispstart + maxprint;
char tbuf[26];
if (end > num_disp)
end = num_disp;
tb_start();
getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0]));
time(&now);
strlcpy(tbuf, ctime(&now), sizeof tbuf);
tbprintf(" %d users", ucount());
tbprintf(" Load %.2f %.2f %.2f", avenrun[0], avenrun[1], avenrun[2]);
if (num_disp && (start > 1 || end != num_disp))
tbprintf(" (%u-%u of %u)", start, end, num_disp);
if (paused)
tbprintf(" PAUSED");
if (rawmode)
printf("\n\n%s\n", tmp_buf);
else
mvprintw(0, 0, "%s", tmp_buf);
mvprintw(0, TIMEPOS, "%s", tbuf);
return (1);
}
/* compatibility functions, rearrange later */
void
error(const char *fmt, ...)
{
va_list ap;
char buf[MAX_LINE_BUF];
va_start(ap, fmt);
vsnprintf(buf, sizeof buf, fmt, ap);
va_end(ap);
message_set(buf);
}
void
nlisterr(struct nlist namelist[])
{
int i, n;
n = 0;
clear();
mvprintw(2, 10, "systat: nlist: can't find following symbols:");
for (i = 0;
namelist[i].n_name != NULL && *namelist[i].n_name != '\0'; i++)
if (namelist[i].n_value == 0)
mvprintw(2 + ++n, 10, "%s", namelist[i].n_name);
move(CMDLINE, 0);
clrtoeol();
refresh();
endwin();
exit(1);
}
void
die(void)
{
if (!rawmode)
endwin();
exit(0);
}
int
prefix(char *s1, char *s2)
{
while (*s1 == *s2) {
if (*s1 == '\0')
return (1);
s1++, s2++;
}
return (*s1 == '\0');
}
/* calculate number of users on the system */
int
ucount(void)
{
int nusers = 0;
struct utmp utmp;
if (ut < 0)
return (0);
lseek(ut, (off_t)0, SEEK_SET);
while (read(ut, &utmp, sizeof(utmp)))
if (utmp.ut_name[0] != '\0')
nusers++;
return (nusers);
}
/* main program functions */
void
usage(void)
{
extern char *__progname;
fprintf(stderr, "usage: %s [-abin] [-d count] "
"[-s delay] [-w width] [view] [delay]\n", __progname);
exit(1);
}
void
show_view(void)
{
if (rawmode)
return;
tb_start();
tbprintf("%s %g", curr_view->name, naptime);
tb_end();
message_set(tmp_buf);
}
void
add_view_tb(field_view *v)
{
if (curr_view == v)
tbprintf("[%s] ", v->name);
else
tbprintf("%s ", v->name);
}
void
show_help(void)
{
if (rawmode)
return;
tb_start();
foreach_view(add_view_tb);
tb_end();
message_set(tmp_buf);
}
void
cmd_compat(const char *buf)
{
const char *s;
if (strcasecmp(buf, "help") == 0) {
show_help();
need_update = 1;
return;
}
if (strcasecmp(buf, "quit") == 0 || strcasecmp(buf, "q") == 0) {
gotsig_close = 1;
return;
}
if (strcasecmp(buf, "stop") == 0) {
paused = 1;
gotsig_alarm = 1;
return;
}
if (strncasecmp(buf, "start", 5) == 0) {
paused = 0;
gotsig_alarm = 1;
cmd_delay(buf + 5);
return;
}
for (s = buf; *s && strchr("0123456789+-.eE", *s) != NULL; s++)
;
if (*s) {
if (set_view(buf))
error("Invalid/ambiguous view: %s", buf);
} else
cmd_delay(buf);
}
void
cmd_delay(const char *buf)
{
double del;
del = atof(buf);
if (del > 0) {
udelay = (useconds_t)(del * 1000000);
gotsig_alarm = 1;
naptime = del;
}
}
void
cmd_count(const char *buf)
{
int ms;
ms = atoi(buf);
if (ms <= 0 || ms > lines - HEADER_LINES)
maxprint = lines - HEADER_LINES;
else
maxprint = ms;
}
int
keyboard_callback(int ch)
{
switch (ch) {
case '?':
/* FALLTHROUGH */
case 'h':
show_help();
need_update = 1;
break;
case CTRL_G:
show_view();
need_update = 1;
break;
case 'l':
command_set(&cm_count, NULL);
break;
case 's':
command_set(&cm_delay, NULL);
break;
case ':':
command_set(&cm_compat, NULL);
break;
default:
return 0;
};
return 1;
}
void
initialize(void)
{
engine_initialize();
initvmstat();
initpigs();
initifstat();
initiostat();
initsensors();
initmembufs();
initnetstat();
initswap();
initpftop();
initpf();
initpool();
initmalloc();
}
void
gethz(void)
{
struct clockinfo cinf;
size_t size = sizeof(cinf);
int mib[2];
mib[0] = CTL_KERN;
mib[1] = KERN_CLOCKRATE;
if (sysctl(mib, 2, &cinf, &size, NULL, 0) == -1)
return;
stathz = cinf.stathz;
hz = cinf.hz;
}
int
main(int argc, char *argv[])
{
char errbuf[_POSIX2_LINE_MAX];
extern char *optarg;
extern int optind;
double delay = 5;
char *viewstr = NULL;
gid_t gid;
int countmax = 0;
int maxlines = 0;
int ch;
ut = open(_PATH_UTMP, O_RDONLY);
if (ut < 0) {
warn("No utmp");
}
kd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf);
gid = getgid();
if (setresgid(gid, gid, gid) == -1)
err(1, "setresgid");
while ((ch = getopt(argc, argv, "abd:ins:w:")) != -1) {
switch (ch) {
case 'a':
maxlines = -1;
break;
case 'b':
rawmode = 1;
interactive = 0;
break;
case 'd':
countmax = atoi(optarg);
if (countmax < 0)
countmax = 0;
break;
case 'i':
interactive = 1;
break;
case 'n':
nflag = 1;
break;
case 's':
delay = atof(optarg);
if (delay <= 0)
delay = 5;
break;
case 'w':
rawwidth = atoi(optarg);
if (rawwidth < 1)
rawwidth = DEFAULT_WIDTH;
if (rawwidth >= MAX_LINE_BUF)
rawwidth = MAX_LINE_BUF - 1;
break;
default:
usage();
/* NOTREACHED */
}
}
if (kd == NULL)
warnx("kvm_openfiles: %s", errbuf);
argc -= optind;
argv += optind;
if (argc == 1) {
double del = atof(argv[0]);
if (del == 0)
viewstr = argv[0];
else
delay = del;
} else if (argc == 2) {
viewstr = argv[0];
delay = atof(argv[1]);
if (delay <= 0)
delay = 5;
}
udelay = (useconds_t)(delay * 1000000.0);
if (udelay < 1)
udelay = 1;
naptime = (double)udelay / 1000000.0;
gethostname(hostname, sizeof (hostname));
gethz();
initialize();
set_order(NULL);
if (viewstr && set_view(viewstr)) {
fprintf(stderr, "Unknown/ambiguous view name: %s\n", viewstr);
return 1;
}
if (!isatty(STDOUT_FILENO)) {
rawmode = 1;
interactive = 0;
}
setup_term(maxlines);
if (rawmode && countmax == 0)
countmax = 1;
gotsig_alarm = 1;
engine_loop(countmax);
return 0;
}
|