66 |
|
repeat_mode_type repeat_mode = REPEAT_NONE; |
67 |
|
int repeat_time = 1; |
68 |
|
int use_cpu_percent = 0; |
69 |
+ |
int use_diffs = 0; |
70 |
|
|
71 |
|
/* Exit with an error message. */ |
72 |
|
void die(const char *s) { |
145 |
|
return strcmp(((stat *)a)->name, ((stat *)b)->name); |
146 |
|
} |
147 |
|
|
148 |
< |
/* Clear and rebuild the stats array. */ |
148 |
< |
void get_stats(int use_diffs) { |
149 |
< |
cpu_states_t *cpu_s; |
150 |
< |
cpu_percent_t *cpu_p; |
151 |
< |
mem_stat_t *mem; |
152 |
< |
load_stat_t *load; |
153 |
< |
user_stat_t *user; |
154 |
< |
swap_stat_t *swap; |
155 |
< |
general_stat_t *gen; |
156 |
< |
disk_stat_t *disk; |
157 |
< |
diskio_stat_t *diskio; |
158 |
< |
process_stat_t *proc; |
159 |
< |
network_stat_t *net; |
160 |
< |
page_stat_t *page; |
148 |
> |
void populate_const() { |
149 |
|
static int zero = 0; |
162 |
– |
int n, i; |
150 |
|
|
164 |
– |
clear_stats(); |
165 |
– |
|
151 |
|
/* Constants, for use with MRTG mode. */ |
152 |
|
add_stat(INT, &zero, "const", "0", NULL); |
153 |
+ |
} |
154 |
|
|
155 |
< |
/* FIXME when only fetching some stats, it'd be more efficient to only |
170 |
< |
do the libstatgrab calls needed, rather than fetching everything. */ |
171 |
< |
|
155 |
> |
void populate_cpu() { |
156 |
|
if (use_cpu_percent) { |
157 |
< |
cpu_p = cpu_percent_usage(); |
157 |
> |
cpu_percent_t *cpu_p = cpu_percent_usage(); |
158 |
> |
|
159 |
|
if (cpu_p != NULL) { |
160 |
|
add_stat(FLOAT, &cpu_p->user, |
161 |
|
"cpu", "user", NULL); |
173 |
|
"cpu", "time_taken", NULL); |
174 |
|
} |
175 |
|
} else { |
176 |
+ |
cpu_states_t *cpu_s; |
177 |
+ |
|
178 |
|
cpu_s = use_diffs ? get_cpu_diff() : get_cpu_totals(); |
179 |
|
if (cpu_s != NULL) { |
180 |
|
add_stat(LONG_LONG, &cpu_s->user, |
195 |
|
"cpu", "systime", NULL); |
196 |
|
} |
197 |
|
} |
198 |
+ |
} |
199 |
|
|
200 |
< |
mem = get_memory_stats(); |
200 |
> |
void populate_mem() { |
201 |
> |
mem_stat_t *mem = get_memory_stats(); |
202 |
> |
|
203 |
|
if (mem != NULL) { |
204 |
|
add_stat(LONG_LONG, &mem->total, "mem", "total", NULL); |
205 |
|
add_stat(LONG_LONG, &mem->free, "mem", "free", NULL); |
206 |
|
add_stat(LONG_LONG, &mem->used, "mem", "used", NULL); |
207 |
|
add_stat(LONG_LONG, &mem->cache, "mem", "cache", NULL); |
208 |
|
} |
209 |
+ |
} |
210 |
|
|
211 |
< |
load = get_load_stats(); |
211 |
> |
void populate_load() { |
212 |
> |
load_stat_t *load = get_load_stats(); |
213 |
> |
|
214 |
|
if (load != NULL) { |
215 |
|
add_stat(DOUBLE, &load->min1, "load", "min1", NULL); |
216 |
|
add_stat(DOUBLE, &load->min5, "load", "min5", NULL); |
217 |
|
add_stat(DOUBLE, &load->min15, "load", "min15", NULL); |
218 |
|
} |
219 |
+ |
} |
220 |
|
|
221 |
< |
user = get_user_stats(); |
221 |
> |
void populate_user() { |
222 |
> |
user_stat_t *user = get_user_stats(); |
223 |
> |
|
224 |
|
if (user != NULL) { |
225 |
|
add_stat(INT, &user->num_entries, "user", "num", NULL); |
226 |
|
add_stat(STRING, &user->name_list, "user", "names", NULL); |
227 |
|
} |
228 |
+ |
} |
229 |
|
|
230 |
< |
swap = get_swap_stats(); |
230 |
> |
void populate_swap() { |
231 |
> |
swap_stat_t *swap = get_swap_stats(); |
232 |
> |
|
233 |
|
if (swap != NULL) { |
234 |
|
add_stat(LONG_LONG, &swap->total, "swap", "total", NULL); |
235 |
|
add_stat(LONG_LONG, &swap->used, "swap", "used", NULL); |
236 |
|
add_stat(LONG_LONG, &swap->free, "swap", "free", NULL); |
237 |
|
} |
238 |
+ |
} |
239 |
|
|
240 |
< |
gen = get_general_stats(); |
240 |
> |
void populate_general() { |
241 |
> |
general_stat_t *gen = get_general_stats(); |
242 |
> |
|
243 |
|
if (gen != NULL) { |
244 |
|
add_stat(STRING, &gen->os_name, |
245 |
|
"general", "os_name", NULL); |
251 |
|
add_stat(STRING, &gen->hostname, "general", "hostname", NULL); |
252 |
|
add_stat(TIME_T, &gen->uptime, "general", "uptime", NULL); |
253 |
|
} |
254 |
+ |
} |
255 |
|
|
256 |
< |
disk = get_disk_stats(&n); |
256 |
> |
void populate_fs() { |
257 |
> |
int n, i; |
258 |
> |
disk_stat_t *disk = get_disk_stats(&n); |
259 |
> |
|
260 |
|
if (disk != NULL) { |
261 |
|
for (i = 0; i < n; i++) { |
262 |
|
/* FIXME it'd be nicer if libstatgrab did this */ |
287 |
|
"fs", name, "free_inodes", NULL); |
288 |
|
} |
289 |
|
} |
290 |
+ |
} |
291 |
|
|
292 |
+ |
void populate_disk() { |
293 |
+ |
int n, i; |
294 |
+ |
diskio_stat_t *diskio; |
295 |
+ |
|
296 |
|
diskio = use_diffs ? get_diskio_stats_diff(&n) : get_diskio_stats(&n); |
297 |
|
if (diskio != NULL) { |
298 |
|
for (i = 0; i < n; i++) { |
308 |
|
"disk", name, "systime", NULL); |
309 |
|
} |
310 |
|
} |
311 |
+ |
} |
312 |
|
|
313 |
< |
proc = get_process_stats(); |
313 |
> |
void populate_proc() { |
314 |
> |
process_stat_t *proc = get_process_stats(); |
315 |
> |
|
316 |
|
if (proc != NULL) { |
317 |
|
add_stat(INT, &proc->total, "proc", "total", NULL); |
318 |
|
add_stat(INT, &proc->running, "proc", "running", NULL); |
320 |
|
add_stat(INT, &proc->stopped, "proc", "stopped", NULL); |
321 |
|
add_stat(INT, &proc->zombie, "proc", "zombie", NULL); |
322 |
|
} |
323 |
+ |
} |
324 |
|
|
325 |
+ |
void populate_net() { |
326 |
+ |
int n, i; |
327 |
+ |
network_stat_t *net; |
328 |
+ |
|
329 |
|
net = use_diffs ? get_network_stats_diff(&n) : get_network_stats(&n); |
330 |
|
if (net != NULL) { |
331 |
|
for (i = 0; i < n; i++) { |
341 |
|
"net", name, "systime", NULL); |
342 |
|
} |
343 |
|
} |
344 |
+ |
} |
345 |
|
|
346 |
+ |
void populate_page() { |
347 |
+ |
page_stat_t *page; |
348 |
+ |
|
349 |
|
page = use_diffs ? get_page_stats_diff() : get_page_stats(); |
350 |
|
if (page != NULL) { |
351 |
|
add_stat(LONG_LONG, &page->pages_pagein, "page", "in", NULL); |
352 |
|
add_stat(LONG_LONG, &page->pages_pageout, "page", "out", NULL); |
353 |
|
add_stat(LONG_LONG, &page->systime, "page", "systime", NULL); |
354 |
|
} |
355 |
+ |
} |
356 |
|
|
357 |
+ |
typedef struct { |
358 |
+ |
const char *name; |
359 |
+ |
void (*populate)(); |
360 |
+ |
int interesting; |
361 |
+ |
} toplevel; |
362 |
+ |
toplevel toplevels[] = { |
363 |
+ |
{"const.", populate_const, 0}, |
364 |
+ |
{"cpu.", populate_cpu, 0}, |
365 |
+ |
{"mem.", populate_mem, 0}, |
366 |
+ |
{"load.", populate_load, 0}, |
367 |
+ |
{"user.", populate_user, 0}, |
368 |
+ |
{"swap.", populate_swap, 0}, |
369 |
+ |
{"general.", populate_general, 0}, |
370 |
+ |
{"fs.", populate_fs, 0}, |
371 |
+ |
{"disk.", populate_disk, 0}, |
372 |
+ |
{"proc.", populate_proc, 0}, |
373 |
+ |
{"net.", populate_net, 0}, |
374 |
+ |
{"page.", populate_page, 0}, |
375 |
+ |
{NULL, NULL, 0} |
376 |
+ |
}; |
377 |
+ |
|
378 |
+ |
/* Set the "interesting" flag on the sections that we actually need to |
379 |
+ |
fetch. */ |
380 |
+ |
void select_interesting(int argc, char **argv) { |
381 |
+ |
toplevel *t; |
382 |
+ |
|
383 |
+ |
if (argc == 0) { |
384 |
+ |
for (t = &toplevels[0]; t->name != NULL; t++) |
385 |
+ |
t->interesting = 1; |
386 |
+ |
} else { |
387 |
+ |
int i; |
388 |
+ |
|
389 |
+ |
for (i = 0; i < argc; i++) { |
390 |
+ |
for (t = &toplevels[0]; t->name != NULL; t++) { |
391 |
+ |
if (strncmp(argv[i], t->name, |
392 |
+ |
strlen(t->name)) == 0) { |
393 |
+ |
t->interesting = 1; |
394 |
+ |
break; |
395 |
+ |
} |
396 |
+ |
} |
397 |
+ |
} |
398 |
+ |
} |
399 |
+ |
} |
400 |
+ |
|
401 |
+ |
/* Clear and rebuild the stats array. */ |
402 |
+ |
void get_stats() { |
403 |
+ |
toplevel *t; |
404 |
+ |
|
405 |
+ |
clear_stats(); |
406 |
+ |
|
407 |
+ |
for (t = &toplevels[0]; t->name != NULL; t++) { |
408 |
+ |
if (t->interesting) |
409 |
+ |
t->populate(); |
410 |
+ |
} |
411 |
+ |
|
412 |
|
qsort(stats, num_stats, sizeof *stats, stats_compare); |
413 |
|
} |
414 |
|
|
551 |
|
if (use_cpu_percent && repeat_mode == REPEAT_NONE) |
552 |
|
die("CPU percentage usage display requires stat differences"); |
553 |
|
|
554 |
+ |
if (repeat_mode == REPEAT_NONE) |
555 |
+ |
use_diffs = 0; |
556 |
+ |
else |
557 |
+ |
use_diffs = 1; |
558 |
+ |
|
559 |
+ |
select_interesting(argc - optind, &argv[optind]); |
560 |
+ |
|
561 |
|
switch (repeat_mode) { |
562 |
|
case REPEAT_NONE: |
563 |
< |
get_stats(0); |
563 |
> |
get_stats(); |
564 |
|
print_stats(argc, argv); |
565 |
|
break; |
566 |
|
case REPEAT_ONCE: |
567 |
< |
get_stats(1); |
567 |
> |
get_stats(); |
568 |
|
sleep(repeat_time); |
569 |
< |
get_stats(1); |
569 |
> |
get_stats(); |
570 |
|
print_stats(argc, argv); |
571 |
|
break; |
572 |
|
case REPEAT_FOREVER: |
573 |
|
while (1) { |
574 |
< |
get_stats(1); |
574 |
> |
get_stats(); |
575 |
|
print_stats(argc, argv); |
576 |
|
printf("\n"); |
577 |
|
sleep(repeat_time); |