int main(int argc, char **argv){ extern char *optarg; int c; int delay = 1; sg_page_stats *page_stats; while ((c = getopt(argc, argv, "d:")) != -1){ switch (c){ case 'd': delay = atoi(optarg); break; } } /* Initialise helper - e.g. logging, if any */ sg_log_init("libstatgrab-examples", "SGEXAMPLES_LOG_PROPERTIES", argc ? argv[0] : NULL); /* Initialise statgrab */ sg_init(1); register_sig_flagger( SIGINT, &quit ); /* Drop setuid/setgid privileges. */ if (sg_drop_privileges() != SG_ERROR_NONE) sg_die("Error. Failed to drop privileges", 1); page_stats = sg_get_page_stats_diff(NULL); if(page_stats == NULL) sg_die("Failed to get page stats", 1); while( (page_stats = sg_get_page_stats_diff(NULL)) != NULL){ int ch; printf("Pages in : %llu\n", page_stats->pages_pagein); printf("Pages out : %llu\n", page_stats->pages_pageout); ch = inp_wait(delay); if( quit || (ch == 'q') ) break; } exit(0); }
int main(int argc, char **argv){ extern char *optarg; int c; int delay = 1; sg_cpu_percents *cpu_percent; sg_cpu_stats *cpu_diff_stats; while ((c = getopt(argc, argv, "d:")) != -1){ switch (c){ case 'd': delay = atoi(optarg); break; } } #ifdef WIN32 delay = delay * 1000; #endif /* Initialise helper - e.g. logging, if any */ sg_log_init("libstatgrab-examples", "SGEXAMPLES_LOG_PROPERTIES", argc ? argv[0] : NULL); /* Initialise statgrab */ sg_init(1); /* XXX must be replaced by termios/(n)curses function .... if( 0 != setvbuf(stdin, NULL, _IONBF, 0) ) { perror("setvbuf"); exit(1); } */ /* Drop setuid/setgid privileges. */ if (sg_drop_privileges() != SG_ERROR_NONE) { sg_die("Error. Failed to drop privileges", 1); } register_sig_flagger( SIGINT, &quit ); /* Throw away the first reading as thats averaged over the machines uptime */ sg_snapshot(); cpu_percent = sg_get_cpu_percents(NULL); if( NULL == cpu_percent ) sg_die("Failed to get cpu stats", 1); /* Clear the screen ready for display the cpu usage */ printf("\033[2J"); while( ( ( cpu_diff_stats = sg_get_cpu_stats_diff(NULL) ) != NULL ) && ( ( cpu_percent = sg_get_cpu_percents_of(sg_last_diff_cpu_percent, NULL) ) != NULL ) ) { int ch; sg_snapshot(); printf("\033[2;2H%-14s : %lld (%6.2f)", "User CPU", cpu_diff_stats->user, cpu_percent->user); printf("\033[3;2H%-14s : %lld (%6.2f)", "Kernel CPU", cpu_diff_stats->kernel, cpu_percent->kernel); printf("\033[4;2H%-14s : %lld (%6.2f)", "IOWait CPU", cpu_diff_stats->iowait, cpu_percent->iowait); printf("\033[5;2H%-14s : %lld (%6.2f)", "Swap CPU", cpu_diff_stats->swap, cpu_percent->swap); printf("\033[6;2H%-14s : %lld (%6.2f)", "Nice CPU", cpu_diff_stats->nice, cpu_percent->nice); printf("\033[7;2H%-14s : %lld (%6.2f)", "Idle CPU", cpu_diff_stats->idle, cpu_percent->idle); printf("\033[8;2H%-14s : %llu", "Ctxts", cpu_diff_stats->context_switches); printf("\033[9;2H%-14s : %llu", " Voluntary", cpu_diff_stats->voluntary_context_switches); printf("\033[10;2H%-14s : %llu", " Involuntary", cpu_diff_stats->involuntary_context_switches); printf("\033[11;2H%-14s : %llu", "Syscalls", cpu_diff_stats->syscalls); printf("\033[12;2H%-14s : %llu", "Intrs", cpu_diff_stats->interrupts); printf("\033[13;2H%-14s : %llu", "SoftIntrs", cpu_diff_stats->soft_interrupts); fflush(stdout); ch = inp_wait(delay); if( quit || (ch == 'q') ) break; } sg_shutdown(); exit(0); }
int main(int argc, char **argv){ extern char *optarg; int c; /* We default to 1 second updates and displaying in bytes*/ int delay = 1; char units = 'b'; unsigned long long divider = 1; sg_disk_io_stats *diskio_stats; size_t num_diskio_stats; /* Parse command line options */ while ((c = getopt(argc, argv, "d:bkm")) != -1){ switch (c){ case 'd': delay = atoi(optarg); break; case 'b': units = 'b'; break; case 'k': units = 'k'; divider = 1024; break; case 'm': units = 'm'; divider = 1024 * 1024; break; } } /* Initialise helper - e.g. logging, if any */ sg_log_init("libstatgrab-examples", "SGEXAMPLES_LOG_PROPERTIES", argc ? argv[0] : NULL); /* Initialise statgrab */ sg_init(1); /* Drop setuid/setgid privileges. */ if (sg_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. */ register_sig_flagger( SIGINT, &quit ); diskio_stats = sg_get_disk_io_stats_diff(&num_diskio_stats); if (diskio_stats == NULL) sg_die("Failed to get disk stats", 1); /* Clear the screen ready for display the disk stats */ printf("\033[2J"); /* Keep getting the disk stats */ while ( (diskio_stats = sg_get_disk_io_stats_diff(&num_diskio_stats)) != NULL) { size_t x; int line_number = 2; int ch; long long total_write=0; long long total_read=0; qsort(diskio_stats , num_diskio_stats, sizeof(diskio_stats[0]), sg_disk_io_compare_traffic); for(x = 0; x < num_diskio_stats; x++){ /* Print at location 2, linenumber the interface name */ printf("\033[%d;2H%-25s : %-10s", line_number++, "Disk Name", diskio_stats->disk_name); /* Print out at the correct location the traffic in the requsted units passed at command time */ printf("\033[%d;2H%-25s : %8llu %c", line_number++, "Disk read", diskio_stats->read_bytes / divider, units); printf("\033[%d;2H%-25s : %8llu %c", line_number++, "Disk write", diskio_stats->write_bytes / divider, units); printf("\033[%d;2H%-25s : %ld ", line_number++, "Disk systime", (long) diskio_stats->systime); /* Add a blank line between interfaces */ line_number++; /* Add up this interface to the total so we can display a "total" disk io" */ total_write+=diskio_stats->write_bytes; total_read+=diskio_stats->read_bytes; /* Move the pointer onto the next interface. Since this returns a static buffer, we dont need * 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"); printf("\033[%d;2H%-25s : %8llu %c", line_number++, "Disk Total read", total_read / divider, units); printf("\033[%d;2H%-25s : %8llu %c", line_number++, "Disk Total write", total_write / divider, units); fflush(stdout); ch = inp_wait(delay); if( quit || (ch == 'q') ) break; } return 0; }
int main(int argc, char **argv){ extern char *optarg; int c; /* We default to 1 second updates and displaying in bytes*/ int delay = 1; char units = 'b'; unsigned long long divider = 1; sg_network_io_stats *network_stats; size_t num_network_stats; /* Parse command line options */ while ((c = getopt(argc, argv, "d:bkm")) != -1){ switch (c){ case 'd': delay = atoi(optarg); break; case 'b': units = 'b'; break; case 'k': units = 'k'; divider = 1024; break; case 'm': units = 'm'; divider = 1024 * 1024; break; } } /* Initialise helper - e.g. logging, if any */ sg_log_init("libstatgrab-examples", "SGEXAMPLES_LOG_PROPERTIES", argc ? argv[0] : NULL); /* Initialise statgrab */ sg_init(1); register_sig_flagger( SIGINT, &quit ); /* Drop setuid/setgid privileges. */ if (sg_drop_privileges() != SG_ERROR_NONE) sg_die("Error. Failed to drop privileges", 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. */ network_stats = sg_get_network_io_stats_diff(&num_network_stats); if (network_stats == NULL) sg_die("Error. Failed to get network stats", 1); /* Clear the screen ready for display the network stats */ printf("\033[2J"); /* Keep getting the network stats */ while ( (network_stats = sg_get_network_io_stats_diff(&num_network_stats)) != NULL){ size_t x; int line_number = 2; int ch; unsigned long long total_tx=0; unsigned long long total_rx=0; unsigned long long total_ipackets=0; unsigned long long total_opackets=0; unsigned long long total_ierrors=0; unsigned long long total_oerrors=0; unsigned long long total_collisions=0; for(x = 0; x < num_network_stats; x++){ /* Print at location 2, linenumber the interface name */ printf("\033[%d;2H%-30s : %-10s", line_number++, "Network Interface Name", network_stats->interface_name); /* Print out at the correct location the traffic in the requsted units passed at command time */ printf("\033[%d;2H%-30s : %8llu %c", line_number++, "Network Interface Rx", network_stats->rx / divider, units); printf("\033[%d;2H%-30s : %8llu %c", line_number++, "Network Interface Tx", network_stats->tx / divider, units); printf("\033[%d;2H%-30s : %llu ", line_number++, "Network Interface packets in", network_stats->ipackets); printf("\033[%d;2H%-30s : %llu ", line_number++, "Network Interface packets out", network_stats->opackets); printf("\033[%d;2H%-30s : %llu ", line_number++, "Network Interface errors in", network_stats->ierrors); printf("\033[%d;2H%-30s : %llu ", line_number++, "Network Interface errors out", network_stats->oerrors); printf("\033[%d;2H%-30s : %llu ", line_number++, "Network Interface collisions", network_stats->collisions); printf("\033[%d;2H%-30s : %ld ", line_number++, "Network Interface systime", (long) network_stats->systime); /* Add a blank line between interfaces */ line_number++; /* Add up this interface to the total so we can display a "total" network io" */ total_tx+=network_stats->tx; total_rx+=network_stats->rx; total_ipackets+=network_stats->ipackets; total_opackets+=network_stats->opackets; total_ierrors+=network_stats->ierrors; total_oerrors+=network_stats->oerrors; total_collisions+=network_stats->collisions; /* Move the pointer onto the next interface. Since this returns a static buffer, we dont need * to keep track of the orginal pointer to free later */ network_stats++; } printf("\033[%d;2H%-30s : %-10s", line_number++, "Network Interface Name", "Total Network IO"); printf("\033[%d;2H%-30s : %8llu %c", line_number++, "Network Total Rx", total_rx / divider, units); printf("\033[%d;2H%-30s : %8llu %c", line_number++, "Network Total Tx", total_tx / divider, units); printf("\033[%d;2H%-30s : %llu ", line_number++, "Network Total packets in", total_ipackets); printf("\033[%d;2H%-30s : %llu ", line_number++, "Network Total packets out", total_opackets); printf("\033[%d;2H%-30s : %llu ", line_number++, "Network Total errors in", total_ierrors); printf("\033[%d;2H%-30s : %llu ", line_number++, "Network Total errors out", total_oerrors); printf("\033[%d;2H%-30s : %llu ", line_number++, "Network Total collisions", total_collisions); fflush(stdout); ch = inp_wait(delay); if( quit || (ch == 'q') ) break; } return 0; }