--- projects/libstatgrab/examples/disk_traffic.c 2003/08/24 20:24:09 1.2 +++ projects/libstatgrab/examples/disk_traffic.c 2004/04/04 22:48:49 1.8 @@ -1,7 +1,7 @@ /* * i-scream central monitoring system * http://www.i-scream.org - * Copyright (C) 2000-2003 i-scream + * Copyright (C) 2000-2004 i-scream * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -16,20 +16,22 @@ * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * $Id: disk_traffic.c,v 1.8 2004/04/04 22:48:49 tdb Exp $ */ /* A very basic example of how to get the disk statistics from the system * and diaply them. Also it adds up all the traffic to create and print out * a total * Takes several arguments : - * -d Takes the number of seconds to wait to get traffic sent since last call - * Note, this is not disk traffic per second. Its the traffic since last - * call. If you want it traffic per second averaged over time since last call + * -d Takes the number of seconds to wait to get traffic sent since last call + * Note, this is not disk traffic per second. Its the traffic since last + * call. If you want it traffic per second averaged over time since last call * You need to divide against the time since last time this was called. The time * between 2 calls is stored in systime in the disk_stat_t structure. * -b Display in bytes * -k Display in kilobytes - * -m Display in megabytes + * -m Display in megabytes */ #include @@ -40,7 +42,6 @@ int main(int argc, char **argv){ extern char *optarg; - extern int optind; int c; /* We default to 1 second updates and displaying in bytes*/ @@ -51,7 +52,7 @@ int main(int argc, char **argv){ int num_diskio_stats; /* Parse command line options */ - while ((c = getopt(argc, argv, "d:bkm")) != EOF){ + while ((c = getopt(argc, argv, "d:bkm")) != -1){ switch (c){ case 'd': delay = atoi(optarg); @@ -68,10 +69,19 @@ int main(int argc, char **argv){ } } - /* We are not intrested in the amount of traffic ever transmitted, just differences. + /* Initialise statgrab */ + statgrab_init(); + + /* Drop setuid/setgid privileges. */ + if (statgrab_drop_privileges() != 0) { + perror("Error. Failed to drop privileges"); + return 1; + } + + /* We are not interested in the amount of traffic ever transmitted, just differences. * Because of this, we do nothing for the very first call. */ - + diskio_stats = get_diskio_stats_diff(&num_diskio_stats); if (diskio_stats == NULL){ perror("Error. Failed to get disk stats"); @@ -80,7 +90,7 @@ int main(int argc, char **argv){ /* Clear the screen ready for display the disk stats */ printf("\033[2J"); - + /* Keep getting the disk stats */ while ( (diskio_stats = get_diskio_stats_diff(&num_diskio_stats)) != NULL){ int x; @@ -100,14 +110,14 @@ int main(int argc, char **argv){ break; case 'k': printf("\033[%d;2H%-25s : %5lld k", line_number++, "Disk read", (diskio_stats->read_bytes / 1024)); - printf("\033[%d;2H%-25s : %5lld", line_number++, "Disk write", (diskio_stats->write_bytes / 1024)); - break; + printf("\033[%d;2H%-25s : %5lld", line_number++, "Disk write", (diskio_stats->write_bytes / 1024)); + break; case 'm': printf("\033[%d;2H%-25s : %5.2f m", line_number++, "Disk read", diskio_stats->read_bytes / (1024.00*1024.00)); printf("\033[%d;2H%-25s : %5.2f m", line_number++, "Disk write", diskio_stats->write_bytes / (1024.00*1024.00)); } printf("\033[%d;2H%-25s : %ld ", line_number++, "Disk systime", (long) diskio_stats->systime); - + /* Add a blank line between interfaces */ line_number++; @@ -119,7 +129,7 @@ int main(int argc, char **argv){ * to keep track of the orginal pointer to free later */ diskio_stats++; } - + printf("\033[%d;2H%-25s : %-10s", line_number++, "Disk Name", "Total Disk IO"); switch(units){ case 'b': @@ -135,7 +145,7 @@ int main(int argc, char **argv){ printf("\033[%d;2H%-25s : %5.2f m", line_number++, "Disk Total write", (total_write / (1024.00*1024.00))); break; } - + fflush(stdout); sleep(delay);