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; }
/* * Filesystem statistics, see <tt>sg_get_fs_stats(3)</tt> manpage. */ static VALUE statgrab_fs_stats(VALUE self) { int entries, i; sg_fs_stats *stats; VALUE arr, info; if ((stats = sg_get_fs_stats(&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("device_name")), rb_str_new2(stats[i].device_name)); rb_hash_aset(info, ID2SYM(rb_intern("fs_type")), rb_str_new2(stats[i].fs_type)); rb_hash_aset(info, ID2SYM(rb_intern("mnt_point")), rb_str_new2(stats[i].mnt_point)); rb_hash_aset(info, ID2SYM(rb_intern("size")), INT2NUM(stats[i].size/1024)); rb_hash_aset(info, ID2SYM(rb_intern("used")), INT2NUM(stats[i].used/1024)); rb_hash_aset(info, ID2SYM(rb_intern("avail")), INT2NUM(stats[i].avail/1024)); rb_hash_aset(info, ID2SYM(rb_intern("total_inodes")), INT2NUM(stats[i].total_inodes)); rb_hash_aset(info, ID2SYM(rb_intern("used_inodes")), INT2NUM(stats[i].used_inodes)); rb_hash_aset(info, ID2SYM(rb_intern("free_inodes")), INT2NUM(stats[i].free_inodes)); rb_hash_aset(info, ID2SYM(rb_intern("avail_blocks")), INT2NUM(stats[i].avail_blocks)); rb_hash_aset(info, ID2SYM(rb_intern("io_size")), INT2NUM(stats[i].io_size)); rb_hash_aset(info, ID2SYM(rb_intern("block_size")), INT2NUM(stats[i].block_size)); rb_hash_aset(info, ID2SYM(rb_intern("total_blocks")), INT2NUM(stats[i].total_blocks)); rb_hash_aset(info, ID2SYM(rb_intern("free_blocks")), INT2NUM(stats[i].free_blocks)); rb_hash_aset(info, ID2SYM(rb_intern("used_blocks")), INT2NUM(stats[i].used_blocks)); rb_hash_aset(info, ID2SYM(rb_intern("avail_blocks")), INT2NUM(stats[i].avail_blocks)); rb_ary_push(arr, info); } return arr; }
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_fs() { int n, i; sg_fs_stats *disk = sg_get_fs_stats(&n); if (disk != NULL) { for (i = 0; i < n; i++) { /* FIXME it'd be nicer if libstatgrab did this */ char *buf, *name, *p; const char *device = disk[i].device_name; if (strcmp(device, "/") == 0) device = "root"; buf = strdup(device); if (buf == NULL) die("out of memory"); name = buf; if (strlen(name) == 2 && name[1] == ':') name[1] = '\0'; if (strncmp(name, "/dev/", 5) == 0) name += 5; while ((p = strchr(name, '/')) != NULL) *p = '_'; add_stat(STRING, &disk[i].device_name, "fs", name, "device_name", NULL); add_stat(STRING, &disk[i].fs_type, "fs", name, "fs_type", NULL); add_stat(STRING, &disk[i].mnt_point, "fs", name, "mnt_point", NULL); add_stat(BYTES, &disk[i].size, "fs", name, "size", NULL); add_stat(BYTES, &disk[i].used, "fs", name, "used", NULL); add_stat(BYTES, &disk[i].avail, "fs", name, "avail", NULL); add_stat(LONG_LONG, &disk[i].total_inodes, "fs", name, "total_inodes", NULL); add_stat(LONG_LONG, &disk[i].used_inodes, "fs", name, "used_inodes", NULL); add_stat(LONG_LONG, &disk[i].free_inodes, "fs", name, "free_inodes", NULL); free(buf); } } }
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; }