int main(int argc, char **argv){ sg_network_iface_stats *network_iface_stats; size_t iface_count, i; /* 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() != SG_ERROR_NONE) sg_die("Error. Failed to drop privileges", 1); network_iface_stats = sg_get_network_iface_stats(&iface_count); if(network_iface_stats == NULL) sg_die("Failed to get network interface stats", 1); if (argc != 1) { /* If an argument is given, use bsearch to find just that * interface. */ sg_network_iface_stats key; key.interface_name = argv[1]; network_iface_stats = bsearch(&key, network_iface_stats, iface_count, sizeof *network_iface_stats, sg_network_iface_compare_name); if (network_iface_stats == NULL) { fprintf(stderr, "Interface %s not found\n", argv[1]); exit(1); } iface_count = 1; } printf("Name\tSpeed\tDuplex\n"); for(i = 0; i < iface_count; i++) { printf("%s\t%llu\t", network_iface_stats->interface_name, network_iface_stats->speed); switch (network_iface_stats->duplex) { case SG_IFACE_DUPLEX_FULL: printf("full\n"); break; case SG_IFACE_DUPLEX_HALF: printf("half\n"); break; default: printf("unknown\n"); break; } network_iface_stats++; } exit(0); }
int main(int argc, char **argv) { sg_log_init("libstatgrab-test", "SGTEST_LOG_PROPERTIES", argc ? argv[0] : NULL); sg_init(1); if( 0 != get_params( opt_def, argc, argv ) ) { help(argv[0]); return 1; } if( opt_def[OPT_HLP].optarg.b ) { help(argv[0]); return 0; } else if( opt_def[OPT_LIST].optarg.b ) { print_testable_functions(0); return 0; } else if( opt_def[OPT_RUN].optarg.str ) { size_t *test_routines = NULL; size_t entries = funcnames_to_indices(opt_def[OPT_RUN].optarg.str, &test_routines, 0); int errors = 0; if( 0 == entries ) { die( ESRCH, "no functions to test" ); return 255; } while( opt_def[OPT_NLOOPS].optarg.u-- > 0 ) { size_t func_rel_idx; for( func_rel_idx = 0; func_rel_idx < entries; ++func_rel_idx ) { mark_func(test_routines[func_rel_idx]); if( !run_func( test_routines[func_rel_idx], 0 ) ) { done_func(test_routines[func_rel_idx], 0); ++errors; } else { done_func(test_routines[func_rel_idx], 1); } } } (void)report_testable_functions(0); free(test_routines); return errors; } help(argv[0]); return 1; }
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){ sg_mem_stats *mem_stats; sg_swap_stats *swap_stats; long long total, free; /* 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() != SG_ERROR_NONE) sg_die("Error. Failed to drop privileges", 1); if( ((mem_stats = sg_get_mem_stats(NULL)) != NULL) && ((swap_stats = sg_get_swap_stats(NULL)) != NULL) ) { printf("Total memory in bytes : %llu\n", mem_stats->total); printf("Used memory in bytes : %llu\n", mem_stats->used); printf("Cache memory in bytes : %llu\n", mem_stats->cache); printf("Free memory in bytes : %llu\n", mem_stats->free); printf("Swap total in bytes : %llu\n", swap_stats->total); printf("Swap used in bytes : %llu\n", swap_stats->used); printf("Swap free in bytes : %llu\n", swap_stats->free); total = mem_stats->total + swap_stats->total; free = mem_stats->free + swap_stats->free; printf("Total VM usage : %5.2f%%\n", 100 - (((float)total/(float)free))); } else { sg_die("Unable to get VM stats", 1); } exit(0); }
int main(int argc, char **argv) { sg_fs_stats *fs_stats; size_t fs_size; /* 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; } fs_stats = sg_get_fs_stats(&fs_size); if(fs_stats == NULL) sg_die("Failed to get file systems snapshot", 1); printf( "%-16s %-24s %-8s %16s %16s %16s %7s %7s %7s %7s %9s %9s %7s %7s %7s %7s\n", "device", "mountpt", "fstype", "size", "used", "avail", "i-total", "i-used", "i-free", "i-avail", "io size", "block size", "b-total", "b-used", "b-free", "b-avail"); for( ; fs_size > 0 ; --fs_size, ++fs_stats ) { printf( "%-16s %-24s %-8s %16llu %16llu %16llu %7llu %7llu %7llu %7llu %9llu %9llu %7llu %7llu %7llu %7llu\n", fs_stats->device_name, fs_stats->mnt_point, fs_stats->fs_type, fs_stats->size, fs_stats->used, fs_stats->avail, fs_stats->total_inodes, fs_stats->used_inodes, fs_stats->free_inodes, fs_stats->avail_inodes, fs_stats->io_size, fs_stats->block_size, fs_stats->total_blocks, fs_stats->used_blocks, fs_stats->free_blocks, fs_stats->avail_blocks); } return 0; }
int main(int argc, char **argv) { sg_log_init("libstatgrab-test", "SGTEST_LOG_PROPERTIES", argc ? argv[0] : NULL); sg_init(1); if( 0 != get_params( opt_def, argc, argv ) ) { help(argv[0]); return 1; } if( opt_def[OPT_HLP].optarg.b ) { help(argv[0]); return 0; } else if( opt_def[OPT_LIST].optarg.b ) { print_testable_functions(0); return 0; } else if( opt_def[OPT_RUN].optarg.str ) { unsigned long numthreads, i; size_t *test_routines = NULL, nfuncs, ok; struct statgrab_testfuncs *sg_testfuncs = get_testable_functions(&nfuncs); size_t entries = funcnames_to_indices(opt_def[OPT_RUN].optarg.str, &test_routines, 0); pthread_t *threadid = NULL; int rc, errors = 0; if( 0 == entries ) { die( ESRCH, "no functions to test" ); return 255; } if( -1 != opt_def[OPT_NTHREADS].optarg.s ) { numthreads = opt_def[OPT_NTHREADS].optarg.u; if( numthreads < entries ) { die( ERANGE, "%s %s - to small number for thread count", argv[0], argv[2] ); } } else if( opt_def[OPT_MTHREADS].optarg.u > 1 ) { numthreads = entries * opt_def[OPT_MTHREADS].optarg.u; } else { numthreads = entries; } if( NULL == ( threadid = calloc( sizeof(threadid[0]), numthreads ) ) ) die( ENOMEM, "%s", argv[0] ); rc = pthread_mutex_lock(&mutex); prove_libcall("pthread_mutex_lock", rc); TRACE_LOG_FMT( "multi_threaded", "create %lu threads", numthreads ); for( i = 0; i < numthreads; ++i ) { mark_func(test_routines[i % entries]); rc = pthread_create( &threadid[i], NULL, threadfunc, &test_routines[i % entries] ); prove_libcall("pthread_create", rc); } rc = pthread_mutex_unlock(&mutex); prove_libcall("pthread_mutex_unlock", rc); while( test_counter < numthreads ) sched_yield(); rc = pthread_mutex_lock(&mutex); prove_libcall("pthread_mutex_lock", rc); /* The condition has occured. Set the flag and wake up any waiting threads */ conditionMet = 1; TRACE_LOG( "multi_threaded", "Wake up all waiting threads..." ); rc = pthread_cond_broadcast(&cond); prove_libcall("pthread_cond_broadcast", rc); rc = pthread_mutex_unlock(&mutex); prove_libcall("pthread_mutex_unlock", rc); TRACE_LOG( "multi_threaded", "Wait for threads and cleanup" ); do { struct timespec ts = { 1, 0 }; struct timeval tv; gettimeofday(&tv, NULL); ts.tv_sec += tv.tv_sec; rc = pthread_mutex_lock(&mutex); prove_libcall("pthread_mutex_lock", rc); pthread_cond_timedwait(&cond, &mutex, &ts); prove_libcall("pthread_cond_timedwait", rc); ok = report_testable_functions(0); rc = pthread_mutex_unlock(&mutex); prove_libcall("pthread_mutex_unlock", rc); if( ok != nfuncs ) printf( "---------------\n" ); fflush(stdout); } while( ok != nfuncs ); for (i=0; i<numthreads; ++i) { rc = pthread_join(threadid[i], NULL); prove_libcall("pthread_join", rc); } pthread_cond_destroy(&cond); pthread_mutex_destroy(&mutex); for( i = 0; i < nfuncs; ++i ) errors += sg_testfuncs[i].needed - sg_testfuncs[i].succeeded; TRACE_LOG_FMT( "multi_threaded", "Main completed with test_counter = %lu", test_counter ); return errors; } help(argv[0]); return 1; }
int main(int argc, char **argv){ int c; int colouron = 0; char *fslist = NULL; time_t last_update = 0; extern int errno; int delay=2; sg_log_init("saidar", "SAIDAR_LOG_PROPERTIES", argc ? argv[0] : NULL); sg_init(1); if(sg_drop_privileges() != 0){ fprintf(stderr, "Failed to drop setuid/setgid privileges\n"); return 1; } #ifdef COLOR_SUPPORT while ((c = getopt(argc, argv, "d:F:cvh")) != -1){ #else while ((c = getopt(argc, argv, "d:F:vh")) != -1){ #endif switch (c){ case 'd': delay = atoi(optarg); if (delay < 1){ fprintf(stderr, "Time must be 1 second or greater\n"); exit(1); } break; #ifdef COLOR_SUPPORT case 'c': colouron = 1; break; #endif case 'v': version_num(argv[0]); break; case 'h': default: usage(argv[0]); return 1; } } if (fslist) { sg_error rc = set_valid_filesystems(fslist); if(rc != SG_ERROR_NONE) die(sg_str_error(rc)); free(fslist); } else { sg_error rc = set_valid_filesystems("!nfs, nfs3, nfs4, cifs, smbfs, samba, autofs"); if(rc != SG_ERROR_NONE) die(sg_str_error(rc)); } signal(SIGWINCH, sig_winch_handler); initscr(); #ifdef COLOR_SUPPORT /* turn on colour */ if (colouron) { if (has_colors()) { start_color(); use_default_colors(); init_pair(1,COLOR_RED,-1); init_pair(2,COLOR_GREEN,-1); init_pair(3,COLOR_YELLOW,-1); init_pair(4,COLOR_BLUE,-1); init_pair(5,COLOR_MAGENTA,-1); init_pair(6,COLOR_CYAN,-1); } else { fprintf(stderr, "Colour support disabled: your terminal does not support colour."); colouron = 0; } } #endif nonl(); curs_set(0); cbreak(); noecho(); timeout(delay * 1000); newwin(0, 0, 0, 0); clear(); if(!get_stats()){ fprintf(stderr, "Failed to get all the stats. Please check correct permissions\n"); endwin(); return 1; } display_headings(); for(;;){ time_t now; int ch = getch(); if (ch == 'q'){ break; } /* To keep the numbers slightly accurate we do not want them * updating more frequently than once a second. */ now = time(NULL); if ((now - last_update) >= 1) { get_stats(); } last_update = now; if(sig_winch_flag) { clear(); display_headings(); sig_winch_flag = 0; } display_data(colouron); } endwin(); sg_shutdown(); return 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; }