1 |
|
/* |
2 |
< |
* i-scream central monitoring system |
2 |
> |
* i-scream libstatgrab |
3 |
|
* http://www.i-scream.org |
4 |
< |
* Copyright (C) 2000-2002 i-scream |
4 |
> |
* Copyright (C) 2000-2004 i-scream |
5 |
|
* |
6 |
|
* This program is free software; you can redistribute it and/or |
7 |
|
* modify it under the terms of the GNU General Public License |
16 |
|
* You should have received a copy of the GNU General Public License |
17 |
|
* along with this program; if not, write to the Free Software |
18 |
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
19 |
+ |
* |
20 |
+ |
* $Id$ |
21 |
|
*/ |
22 |
|
|
23 |
|
#ifdef HAVE_CONFIG_H |
36 |
|
#include <sys/times.h> |
37 |
|
#include <limits.h> |
38 |
|
#include <time.h> |
39 |
+ |
#include <math.h> |
40 |
|
|
41 |
|
#ifdef HAVE_NCURSES_H |
42 |
|
#include <ncurses.h> |
44 |
|
#include <curses.h> |
45 |
|
#endif |
46 |
|
|
47 |
+ |
#define THRESHOLD_LOAD 1.0 |
48 |
+ |
|
49 |
+ |
#define THRESHOLD_WARN_ZMB 0 |
50 |
+ |
|
51 |
+ |
#define THRESHOLD_WARN_CPU 60.0 |
52 |
+ |
#define THRESHOLD_ALERT_CPU 90.0 |
53 |
+ |
|
54 |
+ |
#define THRESHOLD_WARN_SWAP 75.0 |
55 |
+ |
#define THRESHOLD_ALERT_SWAP 90.0 |
56 |
+ |
|
57 |
+ |
#define THRESHOLD_WARN_MEM 75.0 |
58 |
+ |
#define THRESHOLD_ALERT_MEM 90.0 |
59 |
+ |
|
60 |
+ |
#define THRESHOLD_WARN_DISK 75.0 |
61 |
+ |
#define THRESHOLD_ALERT_DISK 90.0 |
62 |
+ |
|
63 |
|
typedef struct{ |
64 |
< |
cpu_percent_t *cpu_percents; |
65 |
< |
mem_stat_t *mem_stats; |
66 |
< |
swap_stat_t *swap_stats; |
67 |
< |
load_stat_t *load_stats; |
68 |
< |
process_stat_t *process_stats; |
69 |
< |
page_stat_t *page_stats; |
64 |
> |
sg_cpu_percents *cpu_percents; |
65 |
> |
sg_mem_stats *mem_stats; |
66 |
> |
sg_swap_stats *swap_stats; |
67 |
> |
sg_load_stats *load_stats; |
68 |
> |
sg_process_count *process_count; |
69 |
> |
sg_page_stats *page_stats; |
70 |
|
|
71 |
< |
network_stat_t *network_stats; |
72 |
< |
int network_entries; |
71 |
> |
sg_network_io_stats *network_io_stats; |
72 |
> |
int network_io_entries; |
73 |
|
|
74 |
< |
diskio_stat_t *diskio_stats; |
75 |
< |
int diskio_entries; |
74 |
> |
sg_disk_io_stats *disk_io_stats; |
75 |
> |
int disk_io_entries; |
76 |
|
|
77 |
< |
disk_stat_t *disk_stats; |
78 |
< |
int disk_entries; |
77 |
> |
sg_fs_stats *fs_stats; |
78 |
> |
int fs_entries; |
79 |
|
|
80 |
< |
general_stat_t *general_stats; |
81 |
< |
user_stat_t *user_stats; |
80 |
> |
sg_host_info *host_info; |
81 |
> |
sg_user_stats *user_stats; |
82 |
|
}stats_t; |
83 |
< |
|
83 |
> |
|
84 |
|
stats_t stats; |
85 |
|
|
86 |
|
char *size_conv(long long number){ |
87 |
|
char type[] = {'B', 'K', 'M', 'G', 'T'}; |
88 |
|
int x=0; |
89 |
+ |
int sign=1; |
90 |
|
static char string[10]; |
91 |
|
|
92 |
+ |
if(number < 0){ |
93 |
+ |
sign=-1; |
94 |
+ |
number=-number; |
95 |
+ |
} |
96 |
+ |
|
97 |
|
for(;x<5;x++){ |
98 |
|
if( (number/1024) < (100)) { |
99 |
|
break; |
101 |
|
number = (number/1024); |
102 |
|
} |
103 |
|
|
104 |
< |
snprintf(string, 10, "%lld%c", number, type[x]); |
104 |
> |
number = number*sign; |
105 |
> |
|
106 |
> |
snprintf(string, 10, "%lld%c", number, type[x]); |
107 |
|
return string; |
108 |
< |
|
108 |
> |
|
109 |
|
} |
110 |
|
|
111 |
|
char *hr_uptime(time_t time){ |
112 |
|
int day = 0, hour = 0, min = 0; |
113 |
|
static char uptime_str[25]; |
114 |
|
int sec = (int) time; |
115 |
< |
|
115 |
> |
|
116 |
|
day = sec / (24*60*60); |
117 |
|
sec = sec % (24*60*60); |
118 |
|
hour = sec / (60*60); |
177 |
|
printw("Mem Free :"); |
178 |
|
|
179 |
|
/* Swap */ |
180 |
< |
move(6, 21); |
181 |
< |
printw("Swap Total:"); |
182 |
< |
move(7, 21); |
183 |
< |
printw("Swap Used :"); |
184 |
< |
move(8, 21); |
185 |
< |
printw("Swap Free :"); |
180 |
> |
move(6, 21); |
181 |
> |
printw("Swap Total:"); |
182 |
> |
move(7, 21); |
183 |
> |
printw("Swap Used :"); |
184 |
> |
move(8, 21); |
185 |
> |
printw("Swap Free :"); |
186 |
|
|
187 |
|
/* VM */ |
188 |
|
move(6, 42); |
204 |
|
move(10,15); |
205 |
|
printw("Read"); |
206 |
|
move(10,28); |
207 |
< |
printw("Write"); |
207 |
> |
printw("Write"); |
208 |
|
|
209 |
|
line = 10; |
210 |
< |
if (stats.network_stats != NULL) { |
210 |
> |
if (stats.network_io_stats != NULL) { |
211 |
|
/* Network IO */ |
212 |
|
move(line, 42); |
213 |
|
printw("Network Interface"); |
215 |
|
printw("rx"); |
216 |
|
move(line, 77); |
217 |
|
printw("tx"); |
218 |
< |
line += 2 + stats.network_entries; |
218 |
> |
line += 2 + stats.network_io_entries; |
219 |
|
} |
220 |
|
|
221 |
|
move(line, 42); |
228 |
|
refresh(); |
229 |
|
} |
230 |
|
|
231 |
< |
void display_data(){ |
231 |
> |
void display_data(int colors){ |
232 |
|
char cur_time[20]; |
233 |
|
struct tm *tm_time; |
234 |
|
time_t epoc_time; |
235 |
|
int counter, line; |
236 |
|
long long r,w; |
237 |
|
long long rt, wt; |
238 |
< |
diskio_stat_t *diskio_stat_ptr; |
239 |
< |
network_stat_t *network_stat_ptr; |
240 |
< |
disk_stat_t *disk_stat_ptr; |
238 |
> |
sg_disk_io_stats *disk_io_stat_ptr; |
239 |
> |
sg_network_io_stats *network_stat_ptr; |
240 |
> |
sg_fs_stats *disk_stat_ptr; |
241 |
|
/* Size before it will start overwriting "uptime" */ |
242 |
|
char hostname[15]; |
243 |
|
char *ptr; |
244 |
|
|
245 |
< |
if (stats.general_stats != NULL) { |
245 |
> |
if (stats.host_info != NULL) { |
246 |
|
move(0,12); |
247 |
< |
strncpy(hostname, stats.general_stats->hostname, (sizeof(hostname) - 1)); |
247 |
> |
strncpy(hostname, stats.host_info->hostname, (sizeof(hostname) - 1)); |
248 |
|
/* strncpy does not NULL terminate.. If only strlcpy was on all platforms :) */ |
249 |
|
hostname[14] = '\0'; |
250 |
|
ptr=strchr(hostname, '.'); |
253 |
|
*/ |
254 |
|
if (ptr != NULL){ |
255 |
|
*ptr = '\0'; |
256 |
< |
} |
256 |
> |
} |
257 |
> |
if (colors) { |
258 |
> |
attron(COLOR_PAIR(1)); |
259 |
> |
} |
260 |
|
printw("%s", hostname); |
261 |
|
move(0,36); |
262 |
< |
printw("%s", hr_uptime(stats.general_stats->uptime)); |
262 |
> |
printw("%s", hr_uptime(stats.host_info->uptime)); |
263 |
|
epoc_time=time(NULL); |
264 |
|
tm_time = localtime(&epoc_time); |
265 |
|
strftime(cur_time, 20, "%Y-%m-%d %T", tm_time); |
266 |
|
move(0,61); |
267 |
|
printw("%s", cur_time); |
268 |
+ |
if (colors) { |
269 |
+ |
attroff(COLOR_PAIR(1)); |
270 |
+ |
} |
271 |
|
} |
272 |
|
|
273 |
|
if (stats.load_stats != NULL) { |
274 |
|
/* Load */ |
275 |
+ |
if (colors) { |
276 |
+ |
attron(COLOR_PAIR(6)); |
277 |
+ |
} |
278 |
|
move(2,12); |
279 |
+ |
if (colors && fabs(stats.load_stats->min1 - stats.load_stats->min5) > THRESHOLD_LOAD) { |
280 |
+ |
attron(A_BOLD); |
281 |
+ |
} |
282 |
|
printw("%6.2f", stats.load_stats->min1); |
283 |
+ |
if (colors) { |
284 |
+ |
attroff(A_BOLD); |
285 |
+ |
} |
286 |
|
move(3,12); |
287 |
+ |
if (colors && fabs(stats.load_stats->min5 - stats.load_stats->min15) > THRESHOLD_LOAD) { |
288 |
+ |
attron(A_BOLD); |
289 |
+ |
} |
290 |
|
printw("%6.2f", stats.load_stats->min5); |
291 |
+ |
if (colors) { |
292 |
+ |
attroff(A_BOLD); |
293 |
+ |
} |
294 |
|
move(4,12); |
295 |
+ |
if (colors && fabs(stats.load_stats->min1 - stats.load_stats->min15) > THRESHOLD_LOAD) { |
296 |
+ |
attron(A_BOLD); |
297 |
+ |
} |
298 |
|
printw("%6.2f", stats.load_stats->min15); |
299 |
+ |
if (colors) { |
300 |
+ |
attroff(A_BOLD); |
301 |
+ |
} |
302 |
|
} |
303 |
|
|
304 |
|
if (stats.cpu_percents != NULL) { |
308 |
|
move(3,33); |
309 |
|
printw("%6.2f%%", (stats.cpu_percents->kernel + stats.cpu_percents->iowait + stats.cpu_percents->swap)); |
310 |
|
move(4,33); |
311 |
+ |
if (colors && stats.cpu_percents->user + stats.cpu_percents->nice > THRESHOLD_ALERT_CPU) { |
312 |
+ |
attron(A_STANDOUT); |
313 |
+ |
} |
314 |
+ |
else if (colors && stats.cpu_percents->user + stats.cpu_percents->nice > THRESHOLD_WARN_CPU) { |
315 |
+ |
attron(A_BOLD); |
316 |
+ |
} |
317 |
|
printw("%6.2f%%", (stats.cpu_percents->user + stats.cpu_percents->nice)); |
318 |
+ |
if(colors) { |
319 |
+ |
attroff(A_BOLD); |
320 |
+ |
attroff(A_STANDOUT); |
321 |
+ |
attron(COLOR_PAIR(6)); |
322 |
+ |
} |
323 |
|
} |
324 |
|
|
325 |
< |
if (stats.process_stats != NULL) { |
325 |
> |
if (stats.process_count != NULL) { |
326 |
|
/* Process */ |
327 |
|
move(2, 54); |
328 |
< |
printw("%5d", stats.process_stats->running); |
328 |
> |
printw("%5d", stats.process_count->running); |
329 |
|
move(2,74); |
330 |
< |
printw("%5d", stats.process_stats->zombie); |
330 |
> |
if (colors && stats.process_count->zombie > THRESHOLD_WARN_ZMB) { |
331 |
> |
attron(A_STANDOUT); |
332 |
> |
} |
333 |
> |
printw("%5d", stats.process_count->zombie); |
334 |
> |
if(colors) { |
335 |
> |
attroff(A_STANDOUT); |
336 |
> |
attron(COLOR_PAIR(6)); |
337 |
> |
} |
338 |
|
move(3, 54); |
339 |
< |
printw("%5d", stats.process_stats->sleeping); |
339 |
> |
printw("%5d", stats.process_count->sleeping); |
340 |
|
move(3, 74); |
341 |
< |
printw("%5d", stats.process_stats->total); |
341 |
> |
printw("%5d", stats.process_count->total); |
342 |
|
move(4, 54); |
343 |
< |
printw("%5d", stats.process_stats->stopped); |
343 |
> |
printw("%5d", stats.process_count->stopped); |
344 |
|
} |
345 |
|
if (stats.user_stats != NULL) { |
346 |
|
move(4,74); |
347 |
|
printw("%5d", stats.user_stats->num_entries); |
348 |
|
} |
349 |
|
|
350 |
+ |
if(colors) { |
351 |
+ |
attroff(COLOR_PAIR(6)); |
352 |
+ |
attron(COLOR_PAIR(5)); |
353 |
+ |
} |
354 |
|
if (stats.mem_stats != NULL) { |
355 |
|
/* Mem */ |
356 |
|
move(6, 12); |
357 |
< |
printw("%7s", size_conv(stats.mem_stats->total)); |
357 |
> |
printw("%7s", size_conv(stats.mem_stats->total)); |
358 |
|
move(7, 12); |
359 |
< |
printw("%7s", size_conv(stats.mem_stats->used)); |
359 |
> |
printw("%7s", size_conv(stats.mem_stats->used)); |
360 |
|
move(8, 12); |
361 |
|
printw("%7s", size_conv(stats.mem_stats->free)); |
362 |
|
} |
363 |
< |
|
364 |
< |
if (stats.swap_stats != NULL) { |
363 |
> |
|
364 |
> |
if (stats.swap_stats != NULL) { |
365 |
|
/* Swap */ |
366 |
|
move(6, 32); |
367 |
|
printw("%8s", size_conv(stats.swap_stats->total)); |
372 |
|
} |
373 |
|
|
374 |
|
/* VM */ |
375 |
< |
if (stats.mem_stats != NULL && stats.mem_stats->total != 0) { |
375 |
> |
if (stats.mem_stats != NULL && stats.mem_stats->total != 0) { |
376 |
> |
float f = 100.00 * (float)(stats.mem_stats->used)/stats.mem_stats->total; |
377 |
> |
if (colors && f > THRESHOLD_ALERT_MEM) { |
378 |
> |
attron(A_STANDOUT); |
379 |
> |
} |
380 |
> |
else if (colors && f > THRESHOLD_WARN_MEM) { |
381 |
> |
attron(A_BOLD); |
382 |
> |
} |
383 |
|
move(6, 54); |
384 |
< |
printw("%5.2f%%", (100.00 * (float)(stats.mem_stats->used)/stats.mem_stats->total)); |
384 |
> |
printw("%5.2f%%", f); |
385 |
> |
if (colors) { |
386 |
> |
attroff(A_STANDOUT); |
387 |
> |
attroff(A_BOLD); |
388 |
> |
attron(COLOR_PAIR(5)); |
389 |
> |
} |
390 |
|
} |
391 |
< |
if (stats.swap_stats != NULL && stats.swap_stats->total != 0) { |
391 |
> |
if (stats.swap_stats != NULL && stats.swap_stats->total != 0) { |
392 |
> |
float f = 100.00 * (float)(stats.swap_stats->used)/stats.swap_stats->total; |
393 |
> |
if (colors && f > THRESHOLD_ALERT_SWAP) { |
394 |
> |
attron(A_STANDOUT); |
395 |
> |
} |
396 |
> |
else if (colors && f > THRESHOLD_WARN_SWAP) { |
397 |
> |
attron(A_BOLD); |
398 |
> |
} |
399 |
|
move(7, 54); |
400 |
< |
printw("%5.2f%%", (100.00 * (float)(stats.swap_stats->used)/stats.swap_stats->total)); |
400 |
> |
printw("%5.2f%%", f); |
401 |
> |
if (colors) { |
402 |
> |
attroff(A_STANDOUT); |
403 |
> |
attroff(A_BOLD); |
404 |
> |
attron(COLOR_PAIR(5)); |
405 |
> |
} |
406 |
|
} |
407 |
|
if (stats.mem_stats != NULL && stats.swap_stats != NULL && |
408 |
< |
stats.mem_stats->total != 0 && stats.swap_stats->total != 0) { |
408 |
> |
stats.mem_stats->total != 0 && stats.swap_stats->total != 0) { |
409 |
|
move(8, 54); |
410 |
|
printw("%5.2f%%", (100.00 * (float)(stats.mem_stats->used+stats.swap_stats->used)/(stats.mem_stats->total+stats.swap_stats->total))); |
411 |
|
} |
412 |
|
|
413 |
< |
if (stats.page_stats != NULL) { |
413 |
> |
if (stats.page_stats != NULL) { |
414 |
|
/* Paging */ |
415 |
|
move(6, 74); |
416 |
|
printw("%5d", (stats.page_stats->systime)? (stats.page_stats->pages_pagein / stats.page_stats->systime): stats.page_stats->pages_pagein); |
417 |
|
move(7, 74); |
418 |
|
printw("%5d", (stats.page_stats->systime)? (stats.page_stats->pages_pageout / stats.page_stats->systime) : stats.page_stats->pages_pageout); |
419 |
|
} |
420 |
+ |
if (colors) { |
421 |
+ |
attroff(COLOR_PAIR(5)); |
422 |
+ |
} |
423 |
|
|
424 |
|
line = 11; |
425 |
< |
if (stats.diskio_stats != NULL) { |
425 |
> |
if (stats.disk_io_stats != NULL) { |
426 |
|
/* Disk IO */ |
427 |
< |
diskio_stat_ptr = stats.diskio_stats; |
427 |
> |
disk_io_stat_ptr = stats.disk_io_stats; |
428 |
|
r=0; |
429 |
|
w=0; |
430 |
< |
for(counter=0;counter<stats.diskio_entries;counter++){ |
430 |
> |
for(counter=0;counter<stats.disk_io_entries;counter++){ |
431 |
> |
char name[12]; |
432 |
> |
strncpy(name, disk_io_stat_ptr->disk_name, sizeof(name)); |
433 |
> |
name[sizeof(name)-1] = '\0'; /* strncpy doesn't terminate longer strings */ |
434 |
|
move(line, 0); |
435 |
< |
printw("%s", diskio_stat_ptr->disk_name); |
435 |
> |
printw("%s", name); |
436 |
|
move(line, 12); |
437 |
< |
rt = (diskio_stat_ptr->systime)? (diskio_stat_ptr->read_bytes/diskio_stat_ptr->systime): diskio_stat_ptr->read_bytes; |
437 |
> |
rt = (disk_io_stat_ptr->systime)? (disk_io_stat_ptr->read_bytes/disk_io_stat_ptr->systime): disk_io_stat_ptr->read_bytes; |
438 |
> |
if(colors) { |
439 |
> |
attron(COLOR_PAIR(4)); |
440 |
> |
} |
441 |
|
printw("%7s", size_conv(rt)); |
442 |
|
r+=rt; |
443 |
|
move(line, 26); |
444 |
< |
wt = (diskio_stat_ptr->systime)? (diskio_stat_ptr->write_bytes/diskio_stat_ptr->systime): diskio_stat_ptr->write_bytes; |
444 |
> |
wt = (disk_io_stat_ptr->systime)? (disk_io_stat_ptr->write_bytes/disk_io_stat_ptr->systime): disk_io_stat_ptr->write_bytes; |
445 |
|
printw("%7s", size_conv(wt)); |
446 |
|
w+=wt; |
447 |
< |
diskio_stat_ptr++; |
447 |
> |
disk_io_stat_ptr++; |
448 |
|
line++; |
449 |
+ |
if(colors) { |
450 |
+ |
attroff(COLOR_PAIR(4)); |
451 |
+ |
} |
452 |
|
} |
453 |
|
line++; |
454 |
|
move(line, 0); |
455 |
|
printw("Total"); |
456 |
|
move(line, 12); |
457 |
+ |
if(colors) { |
458 |
+ |
attron(COLOR_PAIR(4)); |
459 |
+ |
} |
460 |
|
printw("%7s", size_conv(r)); |
461 |
|
move(line, 26); |
462 |
|
printw("%7s", size_conv(w)); |
463 |
+ |
if(colors) { |
464 |
+ |
attroff(COLOR_PAIR(4)); |
465 |
+ |
} |
466 |
|
} |
467 |
|
|
468 |
|
line = 11; |
469 |
< |
if (stats.network_stats != NULL) { |
469 |
> |
if (stats.network_io_stats != NULL) { |
470 |
|
/* Network */ |
471 |
< |
network_stat_ptr = stats.network_stats; |
472 |
< |
for(counter=0;counter<stats.network_entries;counter++){ |
473 |
< |
move(line, 42); |
474 |
< |
printw("%s", network_stat_ptr->interface_name); |
471 |
> |
network_stat_ptr = stats.network_io_stats; |
472 |
> |
for(counter=0;counter<stats.network_io_entries;counter++){ |
473 |
> |
char name[20]; |
474 |
> |
strncpy(name, network_stat_ptr->interface_name, sizeof(name)); |
475 |
> |
name[sizeof(name)-1] = '\0'; /* strncpy doesn't terminate longer strings */ |
476 |
> |
move(line, 42); |
477 |
> |
printw("%s", name); |
478 |
|
move(line, 62); |
479 |
|
rt = (network_stat_ptr->systime)? (network_stat_ptr->rx / network_stat_ptr->systime): network_stat_ptr->rx; |
480 |
+ |
if(colors) { |
481 |
+ |
attron(COLOR_PAIR(4)); |
482 |
+ |
} |
483 |
|
printw("%7s", size_conv(rt)); |
484 |
|
move(line, 72); |
485 |
< |
wt = (network_stat_ptr->systime)? (network_stat_ptr->tx / network_stat_ptr->systime): network_stat_ptr->tx; |
485 |
> |
wt = (network_stat_ptr->systime)? (network_stat_ptr->tx / network_stat_ptr->systime): network_stat_ptr->tx; |
486 |
|
printw("%7s", size_conv(wt)); |
487 |
|
network_stat_ptr++; |
488 |
|
line++; |
489 |
+ |
if(colors) { |
490 |
+ |
attroff(COLOR_PAIR(4)); |
491 |
+ |
} |
492 |
|
} |
493 |
|
line += 2; |
494 |
|
} |
495 |
|
|
496 |
< |
if (stats.disk_stats != NULL) { |
496 |
> |
if (stats.fs_stats != NULL) { |
497 |
|
/* Disk */ |
498 |
< |
disk_stat_ptr = stats.disk_stats; |
499 |
< |
for(counter=0;counter<stats.disk_entries;counter++){ |
498 |
> |
disk_stat_ptr = stats.fs_stats; |
499 |
> |
for(counter=0;counter<stats.fs_entries;counter++){ |
500 |
> |
char name[20]; |
501 |
> |
strncpy(name, disk_stat_ptr->mnt_point, sizeof(name)); |
502 |
> |
name[sizeof(name)-1] = '\0'; /* strncpy doesn't terminate longer strings */ |
503 |
|
move(line, 42); |
504 |
< |
printw("%s", disk_stat_ptr->mnt_point); |
504 |
> |
printw("%s", name); |
505 |
|
move(line, 62); |
506 |
+ |
if(colors) { |
507 |
+ |
attron(COLOR_PAIR(2)); |
508 |
+ |
} |
509 |
|
printw("%7s", size_conv(disk_stat_ptr->avail)); |
510 |
|
move(line, 73); |
511 |
< |
printw("%5.2f%%", 100.00 * ((float) disk_stat_ptr->used / (float) (disk_stat_ptr->used + disk_stat_ptr->avail))); |
511 |
> |
if(colors && 100.00 * ((float) disk_stat_ptr->used / (float) (disk_stat_ptr->used + disk_stat_ptr->avail)) > THRESHOLD_ALERT_DISK) { |
512 |
> |
attron(A_STANDOUT); |
513 |
> |
} else if (colors && 100.00 * ((float) disk_stat_ptr->used / (float) (disk_stat_ptr->used + disk_stat_ptr->avail)) > THRESHOLD_WARN_DISK) { |
514 |
> |
attron(A_BOLD); |
515 |
> |
} |
516 |
> |
printw("%6.2f%%", 100.00 * ((float) disk_stat_ptr->used / (float) (disk_stat_ptr->used + disk_stat_ptr->avail))); |
517 |
|
disk_stat_ptr++; |
518 |
|
line++; |
519 |
+ |
if(colors) { |
520 |
+ |
attroff(COLOR_PAIR(2)); |
521 |
+ |
attroff(A_STANDOUT); |
522 |
+ |
attroff(A_BOLD); |
523 |
+ |
} |
524 |
|
} |
525 |
|
} |
526 |
|
|
527 |
|
refresh(); |
528 |
|
} |
529 |
|
|
530 |
< |
void sig_winch_handler(int sig){ |
530 |
> |
void sig_winch_handler(int dummy){ |
531 |
|
clear(); |
532 |
|
display_headings(); |
533 |
< |
display_data(); |
534 |
< |
signal(SIGWINCH, sig_winch_handler); |
533 |
> |
display_data(0); |
534 |
> |
signal(SIGWINCH, sig_winch_handler); |
535 |
|
} |
536 |
|
|
537 |
|
int get_stats(){ |
538 |
< |
stats.cpu_percents = cpu_percent_usage(); |
539 |
< |
stats.mem_stats = get_memory_stats(); |
540 |
< |
stats.swap_stats = get_swap_stats(); |
541 |
< |
stats.load_stats = get_load_stats(); |
542 |
< |
stats.process_stats = get_process_stats(); |
543 |
< |
stats.page_stats = get_page_stats_diff(); |
544 |
< |
stats.network_stats = get_network_stats_diff(&(stats.network_entries)); |
545 |
< |
stats.diskio_stats = get_diskio_stats_diff(&(stats.diskio_entries)); |
546 |
< |
stats.disk_stats = get_disk_stats(&(stats.disk_entries)); |
547 |
< |
stats.general_stats = get_general_stats(); |
548 |
< |
stats.user_stats = get_user_stats(); |
538 |
> |
stats.cpu_percents = sg_get_cpu_percents(); |
539 |
> |
stats.mem_stats = sg_get_mem_stats(); |
540 |
> |
stats.swap_stats = sg_get_swap_stats(); |
541 |
> |
stats.load_stats = sg_get_load_stats(); |
542 |
> |
stats.process_count = sg_get_process_count(); |
543 |
> |
stats.page_stats = sg_get_page_stats_diff(); |
544 |
> |
stats.network_io_stats = sg_get_network_io_stats_diff(&(stats.network_io_entries)); |
545 |
> |
stats.disk_io_stats = sg_get_disk_io_stats_diff(&(stats.disk_io_entries)); |
546 |
> |
stats.fs_stats = sg_get_fs_stats(&(stats.fs_entries)); |
547 |
> |
stats.host_info = sg_get_host_info(); |
548 |
> |
stats.user_stats = sg_get_user_stats(); |
549 |
|
|
550 |
|
return 1; |
551 |
|
} |
557 |
|
} |
558 |
|
|
559 |
|
void usage(char *progname){ |
560 |
< |
fprintf(stderr, "Usage: %s [-d delay] [-v] [-h]\n\n", progname); |
561 |
< |
fprintf(stderr, " -d Sets the update time in seconds\n"); |
560 |
> |
fprintf(stderr, "Usage: %s [-d delay] [-c] [-v] [-h]\n\n", progname); |
561 |
> |
fprintf(stderr, " -d Sets the update time in seconds\n"); |
562 |
> |
fprintf(stderr, " -c Enables coloured output\n"); |
563 |
|
fprintf(stderr, " -v Prints version number\n"); |
564 |
< |
fprintf(stderr, " -h Displays this help information.\n"); |
565 |
< |
fprintf(stderr, "\nReport bugs to <%s>.\n", PACKAGE_BUGREPORT); |
566 |
< |
exit(1); |
564 |
> |
fprintf(stderr, " -h Displays this help information.\n"); |
565 |
> |
fprintf(stderr, "\nReport bugs to <%s>.\n", PACKAGE_BUGREPORT); |
566 |
> |
exit(1); |
567 |
|
} |
568 |
|
|
569 |
|
int main(int argc, char **argv){ |
570 |
+ |
extern char *optarg; |
571 |
+ |
int c; |
572 |
+ |
int colouron = 0; |
573 |
|
|
574 |
< |
extern char *optarg; |
428 |
< |
extern int optind; |
429 |
< |
int c; |
574 |
> |
time_t last_update = 0; |
575 |
|
|
576 |
|
WINDOW *window; |
577 |
|
|
433 |
– |
int stdin_fileno; |
434 |
– |
fd_set infds; |
435 |
– |
struct timeval timeout; |
436 |
– |
|
578 |
|
extern int errno; |
438 |
– |
char ch; |
579 |
|
|
580 |
|
int delay=2; |
581 |
< |
#ifdef ALLBSD |
582 |
< |
gid_t gid; |
583 |
< |
#endif |
584 |
< |
statgrab_init(); |
445 |
< |
#ifdef ALLBSD |
446 |
< |
if((setegid(getgid())) != 0){ |
447 |
< |
fprintf(stderr, "Failed to lose setgid'ness\n"); |
581 |
> |
|
582 |
> |
sg_init(); |
583 |
> |
if(sg_drop_privileges() != 0){ |
584 |
> |
fprintf(stderr, "Failed to drop setuid/setgid privileges\n"); |
585 |
|
return 1; |
586 |
|
} |
587 |
< |
#endif |
588 |
< |
|
589 |
< |
while ((c = getopt(argc, argv, "vhd:")) != EOF){ |
590 |
< |
switch (c){ |
591 |
< |
case 'd': |
592 |
< |
delay = atoi(optarg); |
456 |
< |
if (delay == 0){ |
587 |
> |
|
588 |
> |
while ((c = getopt(argc, argv, "d:cvh")) != -1){ |
589 |
> |
switch (c){ |
590 |
> |
case 'd': |
591 |
> |
delay = atoi(optarg); |
592 |
> |
if (delay < 1){ |
593 |
|
fprintf(stderr, "Time must be 1 second or greater\n"); |
594 |
|
exit(1); |
595 |
|
} |
596 |
< |
delay--; |
597 |
< |
break; |
596 |
> |
break; |
597 |
> |
case 'c': |
598 |
> |
colouron = 1; |
599 |
> |
break; |
600 |
|
case 'v': |
601 |
< |
version_num(argv[0]); |
601 |
> |
version_num(argv[0]); |
602 |
|
break; |
603 |
|
case 'h': |
604 |
|
default: |
605 |
|
usage(argv[0]); |
606 |
|
return 1; |
607 |
|
break; |
608 |
< |
|
609 |
< |
} |
472 |
< |
} |
608 |
> |
} |
609 |
> |
} |
610 |
|
|
611 |
|
signal(SIGWINCH, sig_winch_handler); |
612 |
< |
initscr(); |
613 |
< |
nonl(); |
614 |
< |
cbreak(); |
615 |
< |
noecho(); |
616 |
< |
window=newwin(0, 0, 0, 0); |
612 |
> |
initscr(); |
613 |
> |
/* turn on colour */ |
614 |
> |
if (colouron) { |
615 |
> |
if (has_colors()) { |
616 |
> |
start_color(); |
617 |
> |
use_default_colors(); |
618 |
> |
init_pair(1,COLOR_RED,-1); |
619 |
> |
init_pair(2,COLOR_GREEN,-1); |
620 |
> |
init_pair(3,COLOR_YELLOW,-1); |
621 |
> |
init_pair(4,COLOR_BLUE,-1); |
622 |
> |
init_pair(5,COLOR_MAGENTA,-1); |
623 |
> |
init_pair(6,COLOR_CYAN,-1); |
624 |
> |
} else { |
625 |
> |
fprintf(stderr, "Colour support disabled: your terminal does not support colour."); |
626 |
> |
colouron = 0; |
627 |
> |
} |
628 |
> |
} |
629 |
> |
nonl(); |
630 |
> |
cbreak(); |
631 |
> |
noecho(); |
632 |
> |
timeout(delay * 1000); |
633 |
> |
window=newwin(0, 0, 0, 0); |
634 |
|
clear(); |
635 |
|
|
636 |
|
if(!get_stats()){ |
640 |
|
} |
641 |
|
|
642 |
|
display_headings(); |
489 |
– |
stdin_fileno=fileno(stdin); |
643 |
|
|
644 |
|
for(;;){ |
645 |
+ |
time_t now; |
646 |
+ |
int ch = getch(); |
647 |
|
|
648 |
< |
FD_ZERO(&infds); |
649 |
< |
FD_SET(stdin_fileno, &infds); |
495 |
< |
timeout.tv_sec = delay; |
496 |
< |
timeout.tv_usec = 0; |
497 |
< |
|
498 |
< |
if((select((stdin_fileno+1), &infds, NULL, NULL, &timeout)) == -1){ |
499 |
< |
if(errno!=EINTR){ |
500 |
< |
perror("select failed"); |
501 |
< |
exit(1); |
502 |
< |
} |
648 |
> |
if (ch == 'q'){ |
649 |
> |
break; |
650 |
|
} |
651 |
|
|
652 |
< |
if(FD_ISSET(stdin_fileno, &infds)){ |
653 |
< |
ch=getch(); |
654 |
< |
if (ch == 'q'){ |
655 |
< |
endwin(); |
656 |
< |
return 0; |
657 |
< |
} |
652 |
> |
/* To keep the numbers slightly accurate we do not want them |
653 |
> |
* updating more frequently than once a second. |
654 |
> |
*/ |
655 |
> |
now = time(NULL); |
656 |
> |
if ((now - last_update) >= 1) { |
657 |
> |
get_stats(); |
658 |
|
} |
659 |
+ |
last_update = now; |
660 |
|
|
661 |
< |
get_stats(); |
661 |
> |
display_data(colouron); |
662 |
> |
} |
663 |
|
|
515 |
– |
display_data(); |
516 |
– |
|
517 |
– |
/* To keep the numbers slightly accurate we do not want them updating more |
518 |
– |
* frequently than once a second. |
519 |
– |
*/ |
520 |
– |
sleep(1); |
521 |
– |
} |
522 |
– |
|
664 |
|
endwin(); |
665 |
|
return 0; |
666 |
< |
} |
666 |
> |
} |