--- projects/libstatgrab/examples/cpu_usage.c 2003/11/23 13:51:10 1.4 +++ projects/libstatgrab/examples/cpu_usage.c 2005/09/24 13:29:22 1.11 @@ -1,7 +1,7 @@ /* - * i-scream central monitoring system + * i-scream libstatgrab * 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,6 +16,8 @@ * 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: cpu_usage.c,v 1.11 2005/09/24 13:29:22 tdb Exp $ */ #include @@ -26,11 +28,10 @@ int main(int argc, char **argv){ extern char *optarg; - extern int optind; int c; int delay = 1; - cpu_percent_t *cpu_percent; + sg_cpu_percents *cpu_percent; while ((c = getopt(argc, argv, "d:")) != -1){ switch (c){ @@ -39,17 +40,28 @@ int main(int argc, char **argv){ break; } } +#ifdef WIN32 + delay = delay * 1000; +#endif /* Initialise statgrab */ - statgrab_init(); + sg_init(); + /* Drop setuid/setgid privileges. */ + if (sg_drop_privileges() != 0) { + perror("Error. Failed to drop privileges"); + return 1; + } + /* Throw away the first reading as thats averaged over the machines uptime */ - cpu_percent = cpu_percent_usage(); + sg_snapshot(); + cpu_percent = sg_get_cpu_percents(); /* Clear the screen ready for display the cpu usage */ printf("\033[2J"); - while( (cpu_percent = cpu_percent_usage()) != NULL){ + while( (cpu_percent = sg_get_cpu_percents()) != NULL){ + sg_snapshot(); printf("\033[2;2H%-12s : %6.2f", "User CPU", cpu_percent->user); printf("\033[3;2H%-12s : %6.2f", "Kernel CPU", cpu_percent->kernel); printf("\033[4;2H%-12s : %6.2f", "IOWait CPU", cpu_percent->iowait); @@ -59,6 +71,7 @@ int main(int argc, char **argv){ fflush(stdout); sleep(delay); } + sg_shutdown(); exit(0); }