ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/saidar/saidar.c
(Generate patch)

Comparing projects/libstatgrab/src/saidar/saidar.c (file contents):
Revision 1.7 by pajs, Sat Oct 18 14:00:21 2003 UTC vs.
Revision 1.24 by tdb, Mon Jan 19 16:49:23 2004 UTC

# Line 1 | Line 1
1   /*
2   * i-scream central monitoring system
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
# Line 16 | Line 16
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
# Line 102 | Line 104 | char *hr_uptime(time_t time){
104   }
105  
106   void display_headings(){
107 +        int line;
108 +
109          move(0,0);
110          printw("Hostname  :");
111          move(0,27);
# Line 177 | Line 181 | void display_headings(){
181          move(10,28);
182          printw("Write");        
183  
184 <        /* Network IO */
185 <        move(10, 42);
186 <        printw("Network Interface");
187 <        move(10, 67);
188 <        printw("rx");
189 <        move(10,77);
190 <        printw("tx");
184 >        line = 10;
185 >        if (stats.network_stats != NULL) {
186 >                /* Network IO */
187 >                move(line, 42);
188 >                printw("Network Interface");
189 >                move(line, 67);
190 >                printw("rx");
191 >                move(line, 77);
192 >                printw("tx");
193 >                line += 2 + stats.network_entries;
194 >        }
195  
196 <        move(12+stats.network_entries, 42);
196 >        move(line, 42);
197          printw("Mount Point");
198 <        move(12+stats.network_entries, 65);
198 >        move(line, 65);
199          printw("Free");
200 <        move(12+stats.network_entries, 75);
200 >        move(line, 75);
201          printw("Used");
202  
203          refresh();
# Line 199 | Line 207 | void display_data(){
207          char cur_time[20];
208          struct tm *tm_time;
209          time_t epoc_time;
210 <        int counter;
210 >        int counter, line;
211          long long r,w;
212          long long rt, wt;
213          diskio_stat_t *diskio_stat_ptr;
# Line 209 | Line 217 | void display_data(){
217          char hostname[15];
218          char *ptr;
219  
220 <        move(0,12);
221 <        strncpy(hostname, stats.general_stats->hostname, sizeof(hostname));
222 <        ptr=strchr(hostname, '.');
223 <        /* Some hosts give back a FQDN for hostname. To avoid this, we'll
224 <         * just blank out everything after the first "."
225 <         */
226 <        if (ptr != NULL){
227 <                *ptr = '\0';
228 <        }      
229 <        printw("%s", 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);
220 >        if (stats.general_stats != NULL) {
221 >                move(0,12);
222 >                strncpy(hostname, stats.general_stats->hostname, (sizeof(hostname) - 1));
223 >                /* strncpy does not NULL terminate.. If only strlcpy was on all platforms :) */
224 >                hostname[14] = '\0';
225 >                ptr=strchr(hostname, '.');
226 >                /* Some hosts give back a FQDN for hostname. To avoid this, we'll
227 >                 * just blank out everything after the first "."
228 >                 */
229 >                if (ptr != NULL){
230 >                        *ptr = '\0';
231 >                }      
232 >                printw("%s", hostname);
233 >                move(0,36);
234 >                printw("%s", hr_uptime(stats.general_stats->uptime));
235 >                epoc_time=time(NULL);
236 >                tm_time = localtime(&epoc_time);
237 >                strftime(cur_time, 20, "%Y-%m-%d %T", tm_time);
238 >                move(0,61);
239 >                printw("%s", cur_time);
240 >        }
241  
242 <        /* Load */
243 <        move(2,12);
244 <        printw("%6.2f", stats.load_stats->min1);
245 <        move(3,12);
246 <        printw("%6.2f", stats.load_stats->min5);
247 <        move(4,12);
248 <        printw("%6.2f", stats.load_stats->min15);
242 >        if (stats.load_stats != NULL) {
243 >                /* Load */
244 >                move(2,12);
245 >                printw("%6.2f", stats.load_stats->min1);
246 >                move(3,12);
247 >                printw("%6.2f", stats.load_stats->min5);
248 >                move(4,12);
249 >                printw("%6.2f", stats.load_stats->min15);
250 >        }
251  
252 <        /* CPU */
253 <        move(2,33);
254 <        printw("%6.2f%%", stats.cpu_percents->idle);
255 <        move(3,33);
256 <        printw("%6.2f%%", (stats.cpu_percents->kernel + stats.cpu_percents->iowait + stats.cpu_percents->swap));
257 <        move(4,33);
258 <        printw("%6.2f%%", (stats.cpu_percents->user + stats.cpu_percents->nice));
252 >        if (stats.cpu_percents != NULL) {
253 >                /* CPU */
254 >                move(2,33);
255 >                printw("%6.2f%%", stats.cpu_percents->idle);
256 >                move(3,33);
257 >                printw("%6.2f%%", (stats.cpu_percents->kernel + stats.cpu_percents->iowait + stats.cpu_percents->swap));
258 >                move(4,33);
259 >                printw("%6.2f%%", (stats.cpu_percents->user + stats.cpu_percents->nice));
260 >        }
261  
262 <        /* Process */
263 <        move(2, 54);
264 <        printw("%5d", stats.process_stats->running);
265 <        move(2,74);
266 <        printw("%5d", stats.process_stats->zombie);
267 <        move(3, 54);
268 <        printw("%5d", stats.process_stats->sleeping);
269 <        move(3, 74);
270 <        printw("%5d", stats.process_stats->total);
271 <        move(4, 54);
272 <        printw("%5d", stats.process_stats->stopped);
273 <        move(4,74);
274 <        printw("%5d", stats.user_stats->num_entries);
262 >        if (stats.process_stats != NULL) {
263 >                /* Process */
264 >                move(2, 54);
265 >                printw("%5d", stats.process_stats->running);
266 >                move(2,74);
267 >                printw("%5d", stats.process_stats->zombie);
268 >                move(3, 54);
269 >                printw("%5d", stats.process_stats->sleeping);
270 >                move(3, 74);
271 >                printw("%5d", stats.process_stats->total);
272 >                move(4, 54);
273 >                printw("%5d", stats.process_stats->stopped);
274 >        }
275 >        if (stats.user_stats != NULL) {
276 >                move(4,74);
277 >                printw("%5d", stats.user_stats->num_entries);
278 >        }
279  
280 <        /* Mem */
281 <        move(6, 12);
282 <        printw("%7s", size_conv(stats.mem_stats->total));      
283 <        move(7, 12);
284 <        printw("%7s", size_conv(stats.mem_stats->used));        
285 <        move(8, 12);
286 <        printw("%7s", size_conv(stats.mem_stats->free));
280 >        if (stats.mem_stats != NULL) {
281 >                /* Mem */
282 >                move(6, 12);
283 >                printw("%7s", size_conv(stats.mem_stats->total));      
284 >                move(7, 12);
285 >                printw("%7s", size_conv(stats.mem_stats->used));        
286 >                move(8, 12);
287 >                printw("%7s", size_conv(stats.mem_stats->free));
288 >        }
289          
290 <        /* Swap */
291 <        move(6, 32);
292 <        printw("%8s", size_conv(stats.swap_stats->total));
293 <        move(7, 32);
294 <        printw("%8s", size_conv(stats.swap_stats->used));
295 <        move(8, 32);
296 <        printw("%8s", size_conv(stats.swap_stats->free));
290 >        if (stats.swap_stats != NULL) {
291 >                /* Swap */
292 >                move(6, 32);
293 >                printw("%8s", size_conv(stats.swap_stats->total));
294 >                move(7, 32);
295 >                printw("%8s", size_conv(stats.swap_stats->used));
296 >                move(8, 32);
297 >                printw("%8s", size_conv(stats.swap_stats->free));
298 >        }
299  
300          /* VM */
301 <        move(6, 54);
302 <        printw("%5.2f%%", (100.00 * (float)(stats.mem_stats->used)/stats.mem_stats->total));
303 <        move(7, 54);
304 <        printw("%5.2f%%", (100.00 * (float)(stats.swap_stats->used)/stats.swap_stats->total));
305 <        move(8, 54);
306 <        printw("%5.2f%%", (100.00 * (float)(stats.mem_stats->used+stats.swap_stats->used)/(stats.mem_stats->total+stats.swap_stats->total)));
301 >        if (stats.mem_stats != NULL && stats.mem_stats->total != 0) {  
302 >                move(6, 54);
303 >                printw("%5.2f%%", (100.00 * (float)(stats.mem_stats->used)/stats.mem_stats->total));
304 >        }
305 >        if (stats.swap_stats != NULL && stats.swap_stats->total != 0) {
306 >                move(7, 54);
307 >                printw("%5.2f%%", (100.00 * (float)(stats.swap_stats->used)/stats.swap_stats->total));
308 >        }
309 >        if (stats.mem_stats != NULL && stats.swap_stats != NULL &&
310 >            stats.mem_stats->total != 0 && stats.swap_stats->total != 0) {      
311 >                move(8, 54);
312 >                printw("%5.2f%%", (100.00 * (float)(stats.mem_stats->used+stats.swap_stats->used)/(stats.mem_stats->total+stats.swap_stats->total)));
313 >        }
314  
315 <        /* Paging */
316 <        move(6, 74);
317 <        printw("%5d", (stats.page_stats->systime)? (stats.page_stats->pages_pagein / stats.page_stats->systime): stats.page_stats->pages_pagein);
318 <        move(7, 74);
319 <        printw("%5d", (stats.page_stats->systime)? (stats.page_stats->pages_pageout / stats.page_stats->systime) : stats.page_stats->pages_pageout);
315 >        if (stats.page_stats != NULL) {
316 >                /* Paging */
317 >                move(6, 74);
318 >                printw("%5d", (stats.page_stats->systime)? (stats.page_stats->pages_pagein / stats.page_stats->systime): stats.page_stats->pages_pagein);
319 >                move(7, 74);
320 >                printw("%5d", (stats.page_stats->systime)? (stats.page_stats->pages_pageout / stats.page_stats->systime) : stats.page_stats->pages_pageout);
321 >        }
322  
323 <        /* Disk IO */
324 <        diskio_stat_ptr = stats.diskio_stats;
325 <        r=0;
326 <        w=0;
327 <        for(counter=0;counter<stats.diskio_entries;counter++){
328 <                move(11+counter, 0);
329 <                printw("%s", diskio_stat_ptr->disk_name);
330 <                move(11+counter, 12);
331 <                rt = (diskio_stat_ptr->systime)? (diskio_stat_ptr->read_bytes/diskio_stat_ptr->systime): diskio_stat_ptr->read_bytes;
332 <                printw("%7s", size_conv(rt));
333 <                r+=rt;
334 <                move(11+counter, 26);
335 <                wt = (diskio_stat_ptr->systime)? (diskio_stat_ptr->write_bytes/diskio_stat_ptr->systime): diskio_stat_ptr->write_bytes;
336 <                printw("%7s", size_conv(wt));
337 <                w+=wt;
338 <                diskio_stat_ptr++;
323 >        line = 11;
324 >        if (stats.diskio_stats != NULL) {      
325 >                /* Disk IO */
326 >                diskio_stat_ptr = stats.diskio_stats;
327 >                r=0;
328 >                w=0;
329 >                for(counter=0;counter<stats.diskio_entries;counter++){
330 >                        move(line, 0);
331 >                        printw("%s", diskio_stat_ptr->disk_name);
332 >                        move(line, 12);
333 >                        rt = (diskio_stat_ptr->systime)? (diskio_stat_ptr->read_bytes/diskio_stat_ptr->systime): diskio_stat_ptr->read_bytes;
334 >                        printw("%7s", size_conv(rt));
335 >                        r+=rt;
336 >                        move(line, 26);
337 >                        wt = (diskio_stat_ptr->systime)? (diskio_stat_ptr->write_bytes/diskio_stat_ptr->systime): diskio_stat_ptr->write_bytes;
338 >                        printw("%7s", size_conv(wt));
339 >                        w+=wt;
340 >                        diskio_stat_ptr++;
341 >                        line++;
342 >                }
343 >                line++;
344 >                move(line, 0);
345 >                printw("Total");
346 >                move(line, 12);
347 >                printw("%7s", size_conv(r));
348 >                move(line, 26);
349 >                printw("%7s", size_conv(w));
350          }
307        move(12+counter, 0);
308        printw("Total");
309        move(12+counter, 12);
310        printw("%7s", size_conv(r));
311        move(12+counter, 26);
312        printw("%7s", size_conv(w));
351  
352 <        /* Network */
353 <        network_stat_ptr = stats.network_stats;
354 <        for(counter=0;counter<stats.network_entries;counter++){
355 <                move(11+counter, 42);
356 <                printw("%s", network_stat_ptr->interface_name);
357 <                move(11+counter, 62);
358 <                rt = (network_stat_ptr->systime)? (network_stat_ptr->rx / network_stat_ptr->systime): network_stat_ptr->rx;
359 <                printw("%7s", size_conv(rt));
360 <                move(11+counter, 72);
361 <                wt = (network_stat_ptr->systime)? (network_stat_ptr->tx / network_stat_ptr->systime): network_stat_ptr->tx;
362 <                printw("%7s", size_conv(wt));
363 <                network_stat_ptr++;
352 >        line = 11;
353 >        if (stats.network_stats != NULL) {      
354 >                /* Network */
355 >                network_stat_ptr = stats.network_stats;
356 >                for(counter=0;counter<stats.network_entries;counter++){
357 >                        move(line, 42);
358 >                        printw("%s", network_stat_ptr->interface_name);
359 >                        move(line, 62);
360 >                        rt = (network_stat_ptr->systime)? (network_stat_ptr->rx / network_stat_ptr->systime): network_stat_ptr->rx;
361 >                        printw("%7s", size_conv(rt));
362 >                        move(line, 72);
363 >                        wt = (network_stat_ptr->systime)? (network_stat_ptr->tx / network_stat_ptr->systime): network_stat_ptr->tx;
364 >                        printw("%7s", size_conv(wt));
365 >                        network_stat_ptr++;
366 >                        line++;
367 >                }
368 >                line += 2;
369          }
370  
371 <        /* Disk */
372 <        disk_stat_ptr = stats.disk_stats;
373 <        for(counter=0;counter<stats.disk_entries;counter++){
374 <                move(13+stats.network_entries+counter, 42);
375 <                printw("%s", disk_stat_ptr->mnt_point);
376 <                move(13+stats.network_entries+counter, 62);
377 <                printw("%7s", size_conv(disk_stat_ptr->avail));
378 <                move(13+stats.network_entries+counter, 73);
379 <                printw("%5.2f%%", 100.00 * ((float) (disk_stat_ptr->size - disk_stat_ptr->avail) / (float) disk_stat_ptr->size));
380 <                disk_stat_ptr++;
371 >        if (stats.disk_stats != NULL) {
372 >                /* Disk */
373 >                disk_stat_ptr = stats.disk_stats;
374 >                for(counter=0;counter<stats.disk_entries;counter++){
375 >                        move(line, 42);
376 >                        printw("%s", disk_stat_ptr->mnt_point);
377 >                        move(line, 62);
378 >                        printw("%7s", size_conv(disk_stat_ptr->avail));
379 >                        move(line, 73);
380 >                        printw("%5.2f%%", 100.00 * ((float) disk_stat_ptr->used / (float) (disk_stat_ptr->used + disk_stat_ptr->avail)));
381 >                        disk_stat_ptr++;
382 >                        line++;
383 >                }
384          }
385  
386          refresh();
# Line 348 | Line 394 | void sig_winch_handler(int sig){
394   }
395  
396   int get_stats(){
397 <                if((stats.cpu_percents = cpu_percent_usage()) == NULL) return 0;
398 <                if((stats.mem_stats = get_memory_stats()) == NULL) return 0;
399 <                if((stats.swap_stats = get_swap_stats()) == NULL) return 0;
400 <                if((stats.load_stats = get_load_stats()) == NULL) return 0;
401 <                if((stats.process_stats = get_process_stats()) == NULL) return 0;
402 <                if((stats.page_stats = get_page_stats_diff()) == NULL) return 0;
403 <                if((stats.network_stats = get_network_stats_diff(&(stats.network_entries))) == NULL) return 0;
404 <                if((stats.diskio_stats = get_diskio_stats_diff(&(stats.diskio_entries))) == NULL) return 0;
405 <                if((stats.disk_stats = get_disk_stats(&(stats.disk_entries))) == NULL) return 0;
406 <                if((stats.general_stats = get_general_stats()) == NULL) return 0;
407 <                if((stats.user_stats = get_user_stats()) == NULL) return 0;
397 >        stats.cpu_percents = cpu_percent_usage();
398 >        stats.mem_stats = get_memory_stats();
399 >        stats.swap_stats = get_swap_stats();
400 >        stats.load_stats = get_load_stats();
401 >        stats.process_stats = get_process_stats();
402 >        stats.page_stats = get_page_stats_diff();
403 >        stats.network_stats = get_network_stats_diff(&(stats.network_entries));
404 >        stats.diskio_stats = get_diskio_stats_diff(&(stats.diskio_entries));
405 >        stats.disk_stats = get_disk_stats(&(stats.disk_entries));
406 >        stats.general_stats = get_general_stats();
407 >        stats.user_stats = get_user_stats();
408  
409 <                return 1;
409 >        return 1;
410   }
411  
412   void version_num(char *progname){
# Line 376 | Line 422 | void usage(char *progname){
422          fprintf(stderr, "  -h    Displays this help information.\n");
423          fprintf(stderr, "\nReport bugs to <%s>.\n", PACKAGE_BUGREPORT);
424          exit(1);
379
425   }
426  
427   int main(int argc, char **argv){
# Line 385 | Line 430 | int main(int argc, char **argv){
430          extern int optind;
431          int c;
432  
433 +        time_t last_update = 0;
434 +
435          WINDOW *window;
436  
390        int stdin_fileno;
391        fd_set infds;
392        struct timeval timeout;
393
437          extern int errno;
395        char ch;
438  
439          int delay=2;
440  
441 <        while ((c = getopt(argc, argv, "vhd:")) != EOF){
441 >        statgrab_init();
442 >        if(statgrab_drop_privileges() != 0){
443 >                fprintf(stderr, "Failed to drop setuid/setgid privileges\n");
444 >                return 1;
445 >        }
446 >                
447 >        while ((c = getopt(argc, argv, "vhd:")) != -1){
448                  switch (c){
449                          case 'd':
450                                  delay = atoi(optarg);
# Line 404 | Line 452 | int main(int argc, char **argv){
452                                          fprintf(stderr, "Time must be 1 second or greater\n");
453                                          exit(1);
454                                  }
407                                delay--;
455                                  break;
456                          case 'v':
457                                  version_num(argv[0]);  
# Line 414 | Line 461 | int main(int argc, char **argv){
461                                  usage(argv[0]);
462                                  return 1;
463                                  break;
417                                
464                  }
465          }
466  
# Line 423 | Line 469 | int main(int argc, char **argv){
469          nonl();
470          cbreak();
471          noecho();
472 +        timeout(delay * 1000);
473          window=newwin(0, 0, 0, 0);
474          clear();
475  
# Line 433 | Line 480 | int main(int argc, char **argv){
480          }
481  
482          display_headings();
436        stdin_fileno=fileno(stdin);
483  
484          for(;;){
485 +                time_t now;
486 +                int ch = getch();
487  
488 <                FD_ZERO(&infds);
489 <                FD_SET(stdin_fileno, &infds);
442 <                timeout.tv_sec = delay;
443 <                timeout.tv_usec = 0;
444 <                
445 <                if((select((stdin_fileno+1), &infds, NULL, NULL, &timeout)) == -1){
446 <                        if(errno!=EINTR){
447 <                                perror("select failed");
448 <                                exit(1);
449 <                        }
488 >                if (ch == 'q'){
489 >                        break;
490                  }
491  
492 <                if(FD_ISSET(stdin_fileno, &infds)){
493 <                        ch=getch();
494 <                        if (ch == 'q'){
495 <                                endwin();
496 <                                return 0;
497 <                        }
492 >                /* To keep the numbers slightly accurate we do not want them
493 >                 * updating more frequently than once a second.
494 >                 */
495 >                now = time(NULL);
496 >                if ((now - last_update) >= 1) {
497 >                        get_stats();
498                  }
499 +                last_update = now;
500  
460                get_stats();
461
501                  display_data();
463
464                /* To keep the numbers slightly accurate we do not want them updating more
465                 * frequently than once a second.
466                 */
467                sleep(1);
468
502          }      
470
471
503  
504          endwin();
505          return 0;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines