/* * Disk I/O statistics difference since last call, * see <tt>sg_get_disk_io_stats_diff(3)</tt> manpage. */ static VALUE statgrab_disk_io_stats_diff(VALUE self) { int entries, i; sg_disk_io_stats *stats; VALUE arr, info, time_now; if ((stats = sg_get_disk_io_stats_diff(&entries)) == NULL) statgrab_handle_error(); arr = rb_ary_new(); for(i = 0; i < entries; i++) { info = rb_hash_new(); rb_hash_aset(info, ID2SYM(rb_intern("disk_name")), rb_str_new2(stats[i].disk_name)); rb_hash_aset(info, ID2SYM(rb_intern("read_bytes")), INT2NUM(stats[i].read_bytes/1024)); rb_hash_aset(info, ID2SYM(rb_intern("write_bytes")), INT2NUM(stats[i].write_bytes/1024)); rb_hash_aset(info, ID2SYM(rb_intern("systime")), INT2NUM(stats[i].systime)); time_now = rb_funcall(rb_cTime, rb_intern("now"), 0); rb_hash_aset(info, ID2SYM(rb_intern("last_call")), rb_funcall(time_now, rb_intern("-"), 1, INT2NUM(stats[i].systime))); rb_ary_push(arr, info); } return arr; }
int get_stats(void) { st.cpu = sg_get_cpu_percents(); if (!st.cpu) { LOG(LOG_INFO, "could not sg_get_cpu_stats"); } st.mem = sg_get_mem_stats(); if (!st.mem) { LOG(LOG_INFO, "could not sg_get_mem_stats"); } st.swap = sg_get_swap_stats(); if (!st.swap) { LOG(LOG_INFO, "could not get_swap_stats"); } st.load = sg_get_load_stats(); if (!st.load) { LOG(LOG_INFO, "could not get_load_stats"); } st.process = sg_get_process_stats(&st.process_entries); if (!st.process) { LOG(LOG_INFO, "could not get_process_stats"); } st.paging = sg_get_page_stats_diff(); if (!st.paging) { LOG(LOG_INFO, "could not get_page_stats_diff"); } st.network = sg_get_network_io_stats_diff(&(st.network_entries)); if (!st.network) { LOG(LOG_INFO, "could not get_network_stats_diff"); } st.diskio = sg_get_disk_io_stats_diff(&(st.diskio_entries)); if (!st.diskio) { LOG(LOG_INFO, "could not get_diskio_stats_diff"); } st.disk = sg_get_fs_stats(&(st.disk_entries)); if (!st.disk) { LOG(LOG_INFO, "could not get_disk_stats"); } st.hostinfo = sg_get_host_info(); if (!st.hostinfo) { LOG(LOG_INFO, "could not get_host_info"); } st.user = sg_get_user_stats(); if (!st.user) { LOG(LOG_INFO, "could not get get_user_stats"); } return 1; }
int get_stats() { stats.cpu_percents = sg_get_cpu_percents(); stats.mem_stats = sg_get_mem_stats(); stats.swap_stats = sg_get_swap_stats(); stats.load_stats = sg_get_load_stats(); stats.process_count = sg_get_process_count(); stats.page_stats = sg_get_page_stats_diff(); stats.network_io_stats = sg_get_network_io_stats_diff(&(stats.network_io_entries)); stats.disk_io_stats = sg_get_disk_io_stats_diff(&(stats.disk_io_entries)); stats.fs_stats = sg_get_fs_stats(&(stats.fs_entries)); stats.host_info = sg_get_host_info(); stats.user_stats = sg_get_user_stats(); return 1; }
void populate_disk() { int n, i; sg_disk_io_stats *diskio; diskio = use_diffs ? sg_get_disk_io_stats_diff(&n) : sg_get_disk_io_stats(&n); if (diskio != NULL) { for (i = 0; i < n; i++) { const char *name = diskio[i].disk_name; add_stat(STRING, &diskio[i].disk_name, "disk", name, "disk_name", NULL); add_stat(BYTES, &diskio[i].read_bytes, "disk", name, "read_bytes", NULL); add_stat(BYTES, &diskio[i].write_bytes, "disk", name, "write_bytes", NULL); add_stat(TIME_T, &diskio[i].systime, "disk", name, "systime", NULL); } } }
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'; sg_disk_io_stats *diskio_stats; int 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'; break; case 'm': units = 'm'; break; } } /* Initialise statgrab */ sg_init(); /* 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. */ diskio_stats = sg_get_disk_io_stats_diff(&num_diskio_stats); if (diskio_stats == NULL) { perror("Error. Failed to get disk stats"); return 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) { int x; int line_number = 2; long long total_write=0; long long total_read=0; 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 */ switch(units) { case 'b': printf("\033[%d;2H%-25s : %8lld b", line_number++, "Disk read", diskio_stats->read_bytes); printf("\033[%d;2H%-25s : %8lld b", line_number++, "Disk write", diskio_stats->write_bytes); 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; 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++; /* 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"); switch(units) { case 'b': printf("\033[%d;2H%-25s : %8lld b", line_number++, "Disk Total read", total_read); printf("\033[%d;2H%-25s : %8lld b", line_number++, "Disk Total write", total_write); break; case 'k': printf("\033[%d;2H%-25s : %5lld k", line_number++, "Disk Total read", (total_read / 1024)); printf("\033[%d;2H%-25s : %5lld k", line_number++, "Disk Total write", (total_write / 1024)); break; case 'm': printf("\033[%d;2H%-25s : %5.2f m", line_number++, "Disk Total read", (total_read / (1024.00*1024.00))); printf("\033[%d;2H%-25s : %5.2f m", line_number++, "Disk Total write", (total_write / (1024.00*1024.00))); break; } fflush(stdout); sleep(delay); } 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_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; }