1 |
+ |
/* |
2 |
+ |
* i-scream libstatgrab |
3 |
+ |
* http://www.i-scream.org |
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 |
8 |
+ |
* as published by the Free Software Foundation; either version 2 |
9 |
+ |
* of the License, or (at your option) any later version. |
10 |
+ |
* |
11 |
+ |
* This program is distributed in the hope that it will be useful, |
12 |
+ |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
+ |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
+ |
* GNU General Public License for more details. |
15 |
+ |
* |
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 |
24 |
+ |
#include "config.h" |
25 |
+ |
#endif |
26 |
+ |
|
27 |
|
#include <stdio.h> |
28 |
|
#include <string.h> |
29 |
|
#include <sys/ioctl.h> |
36 |
|
#include <sys/times.h> |
37 |
|
#include <limits.h> |
38 |
|
#include <time.h> |
39 |
+ |
|
40 |
+ |
#ifdef HAVE_NCURSES_H |
41 |
+ |
#include <ncurses.h> |
42 |
+ |
#else |
43 |
|
#include <curses.h> |
44 |
+ |
#endif |
45 |
|
|
46 |
|
typedef struct{ |
47 |
< |
cpu_percent_t *cpu_percents; |
48 |
< |
mem_stat_t *mem_stats; |
49 |
< |
swap_stat_t *swap_stats; |
50 |
< |
load_stat_t *load_stats; |
51 |
< |
process_stat_t *process_stats; |
52 |
< |
page_stat_t *page_stats; |
47 |
> |
sg_cpu_percents *cpu_percents; |
48 |
> |
sg_mem_stats *mem_stats; |
49 |
> |
sg_swap_stats *swap_stats; |
50 |
> |
sg_load_stats *load_stats; |
51 |
> |
sg_process_count *process_count; |
52 |
> |
sg_page_stats *page_stats; |
53 |
|
|
54 |
< |
network_stat_t *network_stats; |
55 |
< |
int network_entries; |
54 |
> |
sg_network_io_stats *network_io_stats; |
55 |
> |
int network_io_entries; |
56 |
|
|
57 |
< |
diskio_stat_t *diskio_stats; |
58 |
< |
int diskio_entries; |
57 |
> |
sg_disk_io_stats *disk_io_stats; |
58 |
> |
int disk_io_entries; |
59 |
|
|
60 |
< |
disk_stat_t *disk_stats; |
61 |
< |
int disk_entries; |
60 |
> |
sg_fs_stats *fs_stats; |
61 |
> |
int fs_entries; |
62 |
|
|
63 |
< |
general_stat_t *general_stats; |
64 |
< |
user_stat_t *user_stats; |
63 |
> |
sg_host_info *host_info; |
64 |
> |
sg_user_stats *user_stats; |
65 |
|
}stats_t; |
66 |
|
|
67 |
|
stats_t stats; |
69 |
|
char *size_conv(long long number){ |
70 |
|
char type[] = {'B', 'K', 'M', 'G', 'T'}; |
71 |
|
int x=0; |
72 |
+ |
int sign=1; |
73 |
|
static char string[10]; |
74 |
|
|
75 |
+ |
if(number < 0){ |
76 |
+ |
sign=-1; |
77 |
+ |
number=-number; |
78 |
+ |
} |
79 |
+ |
|
80 |
|
for(;x<5;x++){ |
81 |
|
if( (number/1024) < (100)) { |
82 |
|
break; |
84 |
|
number = (number/1024); |
85 |
|
} |
86 |
|
|
87 |
< |
snprintf(string, 10, "%lld%c", number, type[x]); |
87 |
> |
number = number*sign; |
88 |
> |
|
89 |
> |
snprintf(string, 10, "%lld%c", number, type[x]); |
90 |
|
return string; |
91 |
|
|
92 |
|
} |
112 |
|
} |
113 |
|
|
114 |
|
void display_headings(){ |
115 |
+ |
int line; |
116 |
+ |
|
117 |
|
move(0,0); |
118 |
|
printw("Hostname :"); |
119 |
|
move(0,27); |
160 |
|
printw("Mem Free :"); |
161 |
|
|
162 |
|
/* Swap */ |
163 |
< |
move(6, 21); |
164 |
< |
printw("Swap Total:"); |
165 |
< |
move(7, 21); |
166 |
< |
printw("Swap Used :"); |
167 |
< |
move(8, 21); |
168 |
< |
printw("Swap Free :"); |
163 |
> |
move(6, 21); |
164 |
> |
printw("Swap Total:"); |
165 |
> |
move(7, 21); |
166 |
> |
printw("Swap Used :"); |
167 |
> |
move(8, 21); |
168 |
> |
printw("Swap Free :"); |
169 |
|
|
170 |
|
/* VM */ |
171 |
|
move(6, 42); |
189 |
|
move(10,28); |
190 |
|
printw("Write"); |
191 |
|
|
192 |
< |
/* Network IO */ |
193 |
< |
move(10, 42); |
194 |
< |
printw("Network Interface"); |
195 |
< |
move(10, 67); |
196 |
< |
printw("rx"); |
197 |
< |
move(10,77); |
198 |
< |
printw("tx"); |
192 |
> |
line = 10; |
193 |
> |
if (stats.network_io_stats != NULL) { |
194 |
> |
/* Network IO */ |
195 |
> |
move(line, 42); |
196 |
> |
printw("Network Interface"); |
197 |
> |
move(line, 67); |
198 |
> |
printw("rx"); |
199 |
> |
move(line, 77); |
200 |
> |
printw("tx"); |
201 |
> |
line += 2 + stats.network_io_entries; |
202 |
> |
} |
203 |
|
|
204 |
< |
move(12+stats.network_entries, 42); |
204 |
> |
move(line, 42); |
205 |
|
printw("Mount Point"); |
206 |
< |
move(12+stats.network_entries, 65); |
206 |
> |
move(line, 65); |
207 |
|
printw("Free"); |
208 |
< |
move(12+stats.network_entries, 75); |
208 |
> |
move(line, 75); |
209 |
|
printw("Used"); |
210 |
|
|
211 |
|
refresh(); |
215 |
|
char cur_time[20]; |
216 |
|
struct tm *tm_time; |
217 |
|
time_t epoc_time; |
218 |
< |
int counter; |
218 |
> |
int counter, line; |
219 |
|
long long r,w; |
220 |
|
long long rt, wt; |
221 |
< |
diskio_stat_t *diskio_stat_ptr; |
222 |
< |
network_stat_t *network_stat_ptr; |
223 |
< |
disk_stat_t *disk_stat_ptr; |
221 |
> |
sg_disk_io_stats *disk_io_stat_ptr; |
222 |
> |
sg_network_io_stats *network_stat_ptr; |
223 |
> |
sg_fs_stats *disk_stat_ptr; |
224 |
> |
/* Size before it will start overwriting "uptime" */ |
225 |
> |
char hostname[15]; |
226 |
> |
char *ptr; |
227 |
|
|
228 |
< |
move(0,12); |
229 |
< |
printw("%s", stats.general_stats->hostname); |
230 |
< |
move(0,36); |
231 |
< |
printw("%s", hr_uptime(stats.general_stats->uptime)); |
232 |
< |
epoc_time=time(NULL); |
233 |
< |
tm_time = localtime(&epoc_time); |
234 |
< |
strftime(cur_time, 20, "%Y-%m-%d %T", tm_time); |
235 |
< |
move(0,61); |
236 |
< |
printw("%s", cur_time); |
228 |
> |
if (stats.host_info != NULL) { |
229 |
> |
move(0,12); |
230 |
> |
strncpy(hostname, stats.host_info->hostname, (sizeof(hostname) - 1)); |
231 |
> |
/* strncpy does not NULL terminate.. If only strlcpy was on all platforms :) */ |
232 |
> |
hostname[14] = '\0'; |
233 |
> |
ptr=strchr(hostname, '.'); |
234 |
> |
/* Some hosts give back a FQDN for hostname. To avoid this, we'll |
235 |
> |
* just blank out everything after the first "." |
236 |
> |
*/ |
237 |
> |
if (ptr != NULL){ |
238 |
> |
*ptr = '\0'; |
239 |
> |
} |
240 |
> |
printw("%s", hostname); |
241 |
> |
move(0,36); |
242 |
> |
printw("%s", hr_uptime(stats.host_info->uptime)); |
243 |
> |
epoc_time=time(NULL); |
244 |
> |
tm_time = localtime(&epoc_time); |
245 |
> |
strftime(cur_time, 20, "%Y-%m-%d %T", tm_time); |
246 |
> |
move(0,61); |
247 |
> |
printw("%s", cur_time); |
248 |
> |
} |
249 |
|
|
250 |
< |
/* Load */ |
251 |
< |
move(2,12); |
252 |
< |
printw("%6.2f", stats.load_stats->min1); |
253 |
< |
move(3,12); |
254 |
< |
printw("%6.2f", stats.load_stats->min5); |
255 |
< |
move(4,12); |
256 |
< |
printw("%6.2f", stats.load_stats->min15); |
250 |
> |
if (stats.load_stats != NULL) { |
251 |
> |
/* Load */ |
252 |
> |
move(2,12); |
253 |
> |
printw("%6.2f", stats.load_stats->min1); |
254 |
> |
move(3,12); |
255 |
> |
printw("%6.2f", stats.load_stats->min5); |
256 |
> |
move(4,12); |
257 |
> |
printw("%6.2f", stats.load_stats->min15); |
258 |
> |
} |
259 |
|
|
260 |
< |
/* CPU */ |
261 |
< |
move(2,33); |
262 |
< |
printw("%6.2f%%", stats.cpu_percents->idle); |
263 |
< |
move(3,33); |
264 |
< |
printw("%6.2f%%", (stats.cpu_percents->kernel + stats.cpu_percents->iowait + stats.cpu_percents->swap)); |
265 |
< |
move(4,33); |
266 |
< |
printw("%6.2f%%", (stats.cpu_percents->user + stats.cpu_percents->nice)); |
260 |
> |
if (stats.cpu_percents != NULL) { |
261 |
> |
/* CPU */ |
262 |
> |
move(2,33); |
263 |
> |
printw("%6.2f%%", stats.cpu_percents->idle); |
264 |
> |
move(3,33); |
265 |
> |
printw("%6.2f%%", (stats.cpu_percents->kernel + stats.cpu_percents->iowait + stats.cpu_percents->swap)); |
266 |
> |
move(4,33); |
267 |
> |
printw("%6.2f%%", (stats.cpu_percents->user + stats.cpu_percents->nice)); |
268 |
> |
} |
269 |
|
|
270 |
< |
/* Process */ |
271 |
< |
move(2, 54); |
272 |
< |
printw("%5d", stats.process_stats->running); |
273 |
< |
move(2,74); |
274 |
< |
printw("%5d", stats.process_stats->zombie); |
275 |
< |
move(3, 54); |
276 |
< |
printw("%5d", stats.process_stats->sleeping); |
277 |
< |
move(3, 74); |
278 |
< |
printw("%5d", stats.process_stats->total); |
279 |
< |
move(4, 54); |
280 |
< |
printw("%5d", stats.process_stats->stopped); |
281 |
< |
move(4,74); |
282 |
< |
printw("%5d", stats.user_stats->num_entries); |
270 |
> |
if (stats.process_count != NULL) { |
271 |
> |
/* Process */ |
272 |
> |
move(2, 54); |
273 |
> |
printw("%5d", stats.process_count->running); |
274 |
> |
move(2,74); |
275 |
> |
printw("%5d", stats.process_count->zombie); |
276 |
> |
move(3, 54); |
277 |
> |
printw("%5d", stats.process_count->sleeping); |
278 |
> |
move(3, 74); |
279 |
> |
printw("%5d", stats.process_count->total); |
280 |
> |
move(4, 54); |
281 |
> |
printw("%5d", stats.process_count->stopped); |
282 |
> |
} |
283 |
> |
if (stats.user_stats != NULL) { |
284 |
> |
move(4,74); |
285 |
> |
printw("%5d", stats.user_stats->num_entries); |
286 |
> |
} |
287 |
|
|
288 |
< |
/* Mem */ |
289 |
< |
|
290 |
< |
move(6, 12); |
291 |
< |
printw("%7s", size_conv(stats.mem_stats->total)); |
292 |
< |
move(7, 12); |
293 |
< |
printw("%7s", size_conv(stats.mem_stats->used)); |
294 |
< |
move(8, 12); |
295 |
< |
printw("%7s", size_conv(stats.mem_stats->free)); |
288 |
> |
if (stats.mem_stats != NULL) { |
289 |
> |
/* Mem */ |
290 |
> |
move(6, 12); |
291 |
> |
printw("%7s", size_conv(stats.mem_stats->total)); |
292 |
> |
move(7, 12); |
293 |
> |
printw("%7s", size_conv(stats.mem_stats->used)); |
294 |
> |
move(8, 12); |
295 |
> |
printw("%7s", size_conv(stats.mem_stats->free)); |
296 |
> |
} |
297 |
|
|
298 |
< |
/* Swap */ |
299 |
< |
move(6, 32); |
300 |
< |
printw("%8s", size_conv(stats.swap_stats->total)); |
301 |
< |
move(7, 32); |
302 |
< |
printw("%8s", size_conv(stats.swap_stats->used)); |
303 |
< |
move(8, 32); |
304 |
< |
printw("%8s", size_conv(stats.swap_stats->free)); |
298 |
> |
if (stats.swap_stats != NULL) { |
299 |
> |
/* Swap */ |
300 |
> |
move(6, 32); |
301 |
> |
printw("%8s", size_conv(stats.swap_stats->total)); |
302 |
> |
move(7, 32); |
303 |
> |
printw("%8s", size_conv(stats.swap_stats->used)); |
304 |
> |
move(8, 32); |
305 |
> |
printw("%8s", size_conv(stats.swap_stats->free)); |
306 |
> |
} |
307 |
|
|
308 |
|
/* VM */ |
309 |
< |
move(6, 54); |
310 |
< |
printw("%5.2f%%", (100.00 * (float)(stats.mem_stats->used)/stats.mem_stats->total)); |
311 |
< |
move(7, 54); |
312 |
< |
printw("%5.2f%%", (100.00 * (float)(stats.swap_stats->used)/stats.swap_stats->total)); |
313 |
< |
move(8, 54); |
314 |
< |
printw("%5.2f%%", (100.00 * (float)(stats.mem_stats->used+stats.swap_stats->used)/(stats.mem_stats->total+stats.swap_stats->total))); |
309 |
> |
if (stats.mem_stats != NULL && stats.mem_stats->total != 0) { |
310 |
> |
move(6, 54); |
311 |
> |
printw("%5.2f%%", (100.00 * (float)(stats.mem_stats->used)/stats.mem_stats->total)); |
312 |
> |
} |
313 |
> |
if (stats.swap_stats != NULL && stats.swap_stats->total != 0) { |
314 |
> |
move(7, 54); |
315 |
> |
printw("%5.2f%%", (100.00 * (float)(stats.swap_stats->used)/stats.swap_stats->total)); |
316 |
> |
} |
317 |
> |
if (stats.mem_stats != NULL && stats.swap_stats != NULL && |
318 |
> |
stats.mem_stats->total != 0 && stats.swap_stats->total != 0) { |
319 |
> |
move(8, 54); |
320 |
> |
printw("%5.2f%%", (100.00 * (float)(stats.mem_stats->used+stats.swap_stats->used)/(stats.mem_stats->total+stats.swap_stats->total))); |
321 |
> |
} |
322 |
|
|
323 |
< |
/* Paging */ |
324 |
< |
move(6, 74); |
325 |
< |
printw("%5d", (stats.page_stats->systime)? (stats.page_stats->pages_pagein / stats.page_stats->systime): stats.page_stats->pages_pagein); |
326 |
< |
move(7, 74); |
327 |
< |
printw("%5d", (stats.page_stats->systime)? (stats.page_stats->pages_pageout / stats.page_stats->systime) : stats.page_stats->pages_pageout); |
323 |
> |
if (stats.page_stats != NULL) { |
324 |
> |
/* Paging */ |
325 |
> |
move(6, 74); |
326 |
> |
printw("%5d", (stats.page_stats->systime)? (stats.page_stats->pages_pagein / stats.page_stats->systime): stats.page_stats->pages_pagein); |
327 |
> |
move(7, 74); |
328 |
> |
printw("%5d", (stats.page_stats->systime)? (stats.page_stats->pages_pageout / stats.page_stats->systime) : stats.page_stats->pages_pageout); |
329 |
> |
} |
330 |
|
|
331 |
< |
/* Disk IO */ |
332 |
< |
diskio_stat_ptr = stats.diskio_stats; |
333 |
< |
r=0; |
334 |
< |
w=0; |
335 |
< |
for(counter=0;counter<stats.diskio_entries;counter++){ |
336 |
< |
move(11+counter, 0); |
337 |
< |
printw("%s", diskio_stat_ptr->disk_name); |
338 |
< |
move(11+counter, 12); |
339 |
< |
rt = (diskio_stat_ptr->systime)? (diskio_stat_ptr->read_bytes/diskio_stat_ptr->systime): diskio_stat_ptr->read_bytes; |
340 |
< |
printw("%7s", size_conv(rt)); |
341 |
< |
r+=rt; |
342 |
< |
move(11+counter, 26); |
343 |
< |
wt = (diskio_stat_ptr->systime)? (diskio_stat_ptr->write_bytes/diskio_stat_ptr->systime): diskio_stat_ptr->write_bytes; |
344 |
< |
printw("%7s", size_conv(wt)); |
345 |
< |
w+=wt; |
346 |
< |
diskio_stat_ptr++; |
331 |
> |
line = 11; |
332 |
> |
if (stats.disk_io_stats != NULL) { |
333 |
> |
/* Disk IO */ |
334 |
> |
disk_io_stat_ptr = stats.disk_io_stats; |
335 |
> |
r=0; |
336 |
> |
w=0; |
337 |
> |
for(counter=0;counter<stats.disk_io_entries;counter++){ |
338 |
> |
move(line, 0); |
339 |
> |
printw("%s", disk_io_stat_ptr->disk_name); |
340 |
> |
move(line, 12); |
341 |
> |
rt = (disk_io_stat_ptr->systime)? (disk_io_stat_ptr->read_bytes/disk_io_stat_ptr->systime): disk_io_stat_ptr->read_bytes; |
342 |
> |
printw("%7s", size_conv(rt)); |
343 |
> |
r+=rt; |
344 |
> |
move(line, 26); |
345 |
> |
wt = (disk_io_stat_ptr->systime)? (disk_io_stat_ptr->write_bytes/disk_io_stat_ptr->systime): disk_io_stat_ptr->write_bytes; |
346 |
> |
printw("%7s", size_conv(wt)); |
347 |
> |
w+=wt; |
348 |
> |
disk_io_stat_ptr++; |
349 |
> |
line++; |
350 |
> |
} |
351 |
> |
line++; |
352 |
> |
move(line, 0); |
353 |
> |
printw("Total"); |
354 |
> |
move(line, 12); |
355 |
> |
printw("%7s", size_conv(r)); |
356 |
> |
move(line, 26); |
357 |
> |
printw("%7s", size_conv(w)); |
358 |
|
} |
268 |
– |
move(12+counter, 0); |
269 |
– |
printw("Total"); |
270 |
– |
move(12+counter, 12); |
271 |
– |
printw("%7s", size_conv(r)); |
272 |
– |
move(12+counter, 26); |
273 |
– |
printw("%7s", size_conv(w)); |
359 |
|
|
360 |
< |
/* Network */ |
361 |
< |
network_stat_ptr = stats.network_stats; |
362 |
< |
for(counter=0;counter<stats.network_entries;counter++){ |
363 |
< |
move(11+counter, 42); |
364 |
< |
printw("%s", network_stat_ptr->interface_name); |
365 |
< |
move(11+counter, 62); |
366 |
< |
rt = (network_stat_ptr->systime)? (network_stat_ptr->rx / network_stat_ptr->systime): network_stat_ptr->rx; |
367 |
< |
printw("%7s", size_conv(rt)); |
368 |
< |
move(11+counter, 72); |
369 |
< |
wt = (network_stat_ptr->systime)? (network_stat_ptr->tx / network_stat_ptr->systime): network_stat_ptr->tx; |
370 |
< |
printw("%7s", size_conv(wt)); |
371 |
< |
network_stat_ptr++; |
360 |
> |
line = 11; |
361 |
> |
if (stats.network_io_stats != NULL) { |
362 |
> |
/* Network */ |
363 |
> |
network_stat_ptr = stats.network_io_stats; |
364 |
> |
for(counter=0;counter<stats.network_io_entries;counter++){ |
365 |
> |
move(line, 42); |
366 |
> |
printw("%s", network_stat_ptr->interface_name); |
367 |
> |
move(line, 62); |
368 |
> |
rt = (network_stat_ptr->systime)? (network_stat_ptr->rx / network_stat_ptr->systime): network_stat_ptr->rx; |
369 |
> |
printw("%7s", size_conv(rt)); |
370 |
> |
move(line, 72); |
371 |
> |
wt = (network_stat_ptr->systime)? (network_stat_ptr->tx / network_stat_ptr->systime): network_stat_ptr->tx; |
372 |
> |
printw("%7s", size_conv(wt)); |
373 |
> |
network_stat_ptr++; |
374 |
> |
line++; |
375 |
> |
} |
376 |
> |
line += 2; |
377 |
|
} |
378 |
|
|
379 |
< |
disk_stat_ptr = stats.disk_stats; |
380 |
< |
for(counter=0;counter<stats.disk_entries;counter++){ |
381 |
< |
move(13+stats.network_entries+counter, 42); |
382 |
< |
printw("%s", disk_stat_ptr->mnt_point); |
383 |
< |
move(13+stats.network_entries+counter, 62); |
384 |
< |
printw("%7s", size_conv(disk_stat_ptr->avail)); |
385 |
< |
move(13+stats.network_entries+counter, 73); |
386 |
< |
printw("%5.2f%%", 100.00 * ((float)disk_stat_ptr->used / (float)disk_stat_ptr->size)); |
387 |
< |
disk_stat_ptr++; |
379 |
> |
if (stats.fs_stats != NULL) { |
380 |
> |
/* Disk */ |
381 |
> |
disk_stat_ptr = stats.fs_stats; |
382 |
> |
for(counter=0;counter<stats.fs_entries;counter++){ |
383 |
> |
move(line, 42); |
384 |
> |
printw("%s", disk_stat_ptr->mnt_point); |
385 |
> |
move(line, 62); |
386 |
> |
printw("%7s", size_conv(disk_stat_ptr->avail)); |
387 |
> |
move(line, 73); |
388 |
> |
printw("%6.2f%%", 100.00 * ((float) disk_stat_ptr->used / (float) (disk_stat_ptr->used + disk_stat_ptr->avail))); |
389 |
> |
disk_stat_ptr++; |
390 |
> |
line++; |
391 |
> |
} |
392 |
|
} |
393 |
|
|
394 |
|
refresh(); |
395 |
|
} |
396 |
|
|
397 |
< |
void sig_winch_handler(int sig){ |
397 |
> |
void sig_winch_handler(int dummy){ |
398 |
|
clear(); |
399 |
|
display_headings(); |
400 |
|
display_data(); |
401 |
< |
signal(SIGWINCH, sig_winch_handler); |
401 |
> |
signal(SIGWINCH, sig_winch_handler); |
402 |
|
} |
403 |
|
|
404 |
|
int get_stats(){ |
405 |
< |
if((stats.cpu_percents = cpu_percent_usage()) == NULL) return 0; |
406 |
< |
if((stats.mem_stats = get_memory_stats()) == NULL) return 0; |
407 |
< |
if((stats.swap_stats = get_swap_stats()) == NULL) return 0; |
408 |
< |
if((stats.load_stats = get_load_stats()) == NULL) return 0; |
409 |
< |
if((stats.process_stats = get_process_stats()) == NULL) return 0; |
410 |
< |
if((stats.page_stats = get_page_stats_diff()) == NULL) return 0; |
411 |
< |
if((stats.network_stats = get_network_stats_diff(&(stats.network_entries))) == NULL) return 0; |
412 |
< |
if((stats.diskio_stats = get_diskio_stats_diff(&(stats.diskio_entries))) == NULL) return 0; |
413 |
< |
if((stats.disk_stats = get_disk_stats(&(stats.disk_entries))) == NULL) return 0; |
414 |
< |
if((stats.general_stats = get_general_stats()) == NULL) return 0; |
415 |
< |
if((stats.user_stats = get_user_stats()) == NULL) return 0; |
405 |
> |
stats.cpu_percents = sg_get_cpu_percents(); |
406 |
> |
stats.mem_stats = sg_get_mem_stats(); |
407 |
> |
stats.swap_stats = sg_get_swap_stats(); |
408 |
> |
stats.load_stats = sg_get_load_stats(); |
409 |
> |
stats.process_count = sg_get_process_count(); |
410 |
> |
stats.page_stats = sg_get_page_stats_diff(); |
411 |
> |
stats.network_io_stats = sg_get_network_io_stats_diff(&(stats.network_io_entries)); |
412 |
> |
stats.disk_io_stats = sg_get_disk_io_stats_diff(&(stats.disk_io_entries)); |
413 |
> |
stats.fs_stats = sg_get_fs_stats(&(stats.fs_entries)); |
414 |
> |
stats.host_info = sg_get_host_info(); |
415 |
> |
stats.user_stats = sg_get_user_stats(); |
416 |
|
|
417 |
< |
return 1; |
417 |
> |
return 1; |
418 |
|
} |
419 |
|
|
420 |
+ |
void version_num(char *progname){ |
421 |
+ |
fprintf(stderr, "%s version %s\n", progname, PACKAGE_VERSION); |
422 |
+ |
fprintf(stderr, "\nReport bugs to <%s>.\n", PACKAGE_BUGREPORT); |
423 |
+ |
exit(1); |
424 |
+ |
} |
425 |
+ |
|
426 |
+ |
void usage(char *progname){ |
427 |
+ |
fprintf(stderr, "Usage: %s [-d delay] [-v] [-h]\n\n", progname); |
428 |
+ |
fprintf(stderr, " -d Sets the update time in seconds\n"); |
429 |
+ |
fprintf(stderr, " -v Prints version number\n"); |
430 |
+ |
fprintf(stderr, " -h Displays this help information.\n"); |
431 |
+ |
fprintf(stderr, "\nReport bugs to <%s>.\n", PACKAGE_BUGREPORT); |
432 |
+ |
exit(1); |
433 |
+ |
} |
434 |
+ |
|
435 |
|
int main(int argc, char **argv){ |
436 |
|
|
437 |
< |
extern char *optarg; |
438 |
< |
extern int optind; |
330 |
< |
int c; |
437 |
> |
extern char *optarg; |
438 |
> |
int c; |
439 |
|
|
440 |
+ |
time_t last_update = 0; |
441 |
+ |
|
442 |
|
WINDOW *window; |
443 |
|
|
334 |
– |
int stdin_fileno; |
335 |
– |
fd_set infds; |
336 |
– |
struct timeval timeout; |
337 |
– |
|
444 |
|
extern int errno; |
339 |
– |
char ch; |
445 |
|
|
446 |
|
int delay=2; |
447 |
|
|
448 |
< |
while ((c = getopt(argc, argv, "d:")) != EOF){ |
449 |
< |
switch (c){ |
450 |
< |
case 'd': |
451 |
< |
delay = atoi(optarg); |
452 |
< |
if (delay == 0){ |
448 |
> |
sg_init(); |
449 |
> |
if(sg_drop_privileges() != 0){ |
450 |
> |
fprintf(stderr, "Failed to drop setuid/setgid privileges\n"); |
451 |
> |
return 1; |
452 |
> |
} |
453 |
> |
|
454 |
> |
while ((c = getopt(argc, argv, "vhd:")) != -1){ |
455 |
> |
switch (c){ |
456 |
> |
case 'd': |
457 |
> |
delay = atoi(optarg); |
458 |
> |
if (delay < 1){ |
459 |
|
fprintf(stderr, "Time must be 1 second or greater\n"); |
460 |
|
exit(1); |
461 |
|
} |
462 |
< |
delay--; |
463 |
< |
break; |
464 |
< |
} |
465 |
< |
} |
462 |
> |
break; |
463 |
> |
case 'v': |
464 |
> |
version_num(argv[0]); |
465 |
> |
break; |
466 |
> |
case 'h': |
467 |
> |
default: |
468 |
> |
usage(argv[0]); |
469 |
> |
return 1; |
470 |
> |
break; |
471 |
> |
} |
472 |
> |
} |
473 |
|
|
474 |
|
signal(SIGWINCH, sig_winch_handler); |
475 |
< |
initscr(); |
476 |
< |
nonl(); |
477 |
< |
cbreak(); |
478 |
< |
noecho(); |
479 |
< |
window=newwin(0, 0, 0, 0); |
475 |
> |
initscr(); |
476 |
> |
nonl(); |
477 |
> |
cbreak(); |
478 |
> |
noecho(); |
479 |
> |
timeout(delay * 1000); |
480 |
> |
window=newwin(0, 0, 0, 0); |
481 |
|
clear(); |
482 |
|
|
483 |
|
if(!get_stats()){ |
487 |
|
} |
488 |
|
|
489 |
|
display_headings(); |
371 |
– |
stdin_fileno=fileno(stdin); |
490 |
|
|
491 |
|
for(;;){ |
492 |
+ |
time_t now; |
493 |
+ |
int ch = getch(); |
494 |
|
|
495 |
< |
FD_ZERO(&infds); |
496 |
< |
FD_SET(stdin_fileno, &infds); |
377 |
< |
timeout.tv_sec = delay; |
378 |
< |
timeout.tv_usec = 0; |
379 |
< |
|
380 |
< |
if((select((stdin_fileno+1), &infds, NULL, NULL, &timeout)) == -1){ |
381 |
< |
if(errno!=EINTR){ |
382 |
< |
perror("select failed"); |
383 |
< |
exit(1); |
384 |
< |
} |
495 |
> |
if (ch == 'q'){ |
496 |
> |
break; |
497 |
|
} |
498 |
|
|
499 |
< |
if(FD_ISSET(stdin_fileno, &infds)){ |
500 |
< |
ch=getch(); |
501 |
< |
if (ch == 'q'){ |
502 |
< |
endwin(); |
503 |
< |
return 0; |
504 |
< |
} |
499 |
> |
/* To keep the numbers slightly accurate we do not want them |
500 |
> |
* updating more frequently than once a second. |
501 |
> |
*/ |
502 |
> |
now = time(NULL); |
503 |
> |
if ((now - last_update) >= 1) { |
504 |
> |
get_stats(); |
505 |
|
} |
506 |
+ |
last_update = now; |
507 |
|
|
395 |
– |
get_stats(); |
396 |
– |
|
508 |
|
display_data(); |
398 |
– |
|
399 |
– |
/* To keep the numbers slightly accurate we do not want them updating more |
400 |
– |
* frequently than once a second. |
401 |
– |
*/ |
402 |
– |
sleep(1); |
403 |
– |
|
509 |
|
} |
405 |
– |
|
406 |
– |
|
510 |
|
|
511 |
|
endwin(); |
512 |
|
return 0; |