--- projects/libstatgrab/src/statgrab/statgrab.c 2003/08/29 06:56:12 1.8 +++ projects/libstatgrab/src/statgrab/statgrab.c 2004/04/06 14:53:00 1.25 @@ -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: statgrab.c,v 1.25 2004/04/06 14:53:00 tdb Exp $ */ #ifdef HAVE_CONFIG_H @@ -35,7 +37,9 @@ typedef enum { FLOAT, DOUBLE, STRING, - INT + INT, + BOOL, + DUPLEX } stat_type; typedef enum { @@ -140,11 +144,19 @@ void add_stat(stat_type type, void *stat, ...) { ++num_stats; } -/* Compare two stats by name, for sorting purposes. */ +/* Compare two stats by name for qsort and bsearch. */ int stats_compare(const void *a, const void *b) { return strcmp(((stat *)a)->name, ((stat *)b)->name); } +/* Compare up to the length of the key for bsearch. */ +int stats_compare_prefix(const void *key, const void *item) { + const char *kn = ((stat *)key)->name; + const char *in = ((stat *)item)->name; + + return strncmp(kn, in, strlen(kn)); +} + void populate_const() { static int zero = 0; @@ -154,7 +166,7 @@ void populate_const() { void populate_cpu() { if (use_cpu_percent) { - cpu_percent_t *cpu_p = cpu_percent_usage(); + sg_cpu_percents *cpu_p = sg_get_cpu_percents(); if (cpu_p != NULL) { add_stat(FLOAT, &cpu_p->user, @@ -173,9 +185,10 @@ void populate_cpu() { "cpu", "time_taken", NULL); } } else { - cpu_states_t *cpu_s; + sg_cpu_stats *cpu_s; - cpu_s = use_diffs ? get_cpu_diff() : get_cpu_totals(); + cpu_s = use_diffs ? sg_get_cpu_stats_diff() + : sg_get_cpu_stats(); if (cpu_s != NULL) { add_stat(LONG_LONG, &cpu_s->user, "cpu", "user", NULL); @@ -198,7 +211,7 @@ void populate_cpu() { } void populate_mem() { - mem_stat_t *mem = get_memory_stats(); + sg_mem_stats *mem = sg_get_mem_stats(); if (mem != NULL) { add_stat(LONG_LONG, &mem->total, "mem", "total", NULL); @@ -209,7 +222,7 @@ void populate_mem() { } void populate_load() { - load_stat_t *load = get_load_stats(); + sg_load_stats *load = sg_get_load_stats(); if (load != NULL) { add_stat(DOUBLE, &load->min1, "load", "min1", NULL); @@ -219,7 +232,7 @@ void populate_load() { } void populate_user() { - user_stat_t *user = get_user_stats(); + sg_user_stats *user = sg_get_user_stats(); if (user != NULL) { add_stat(INT, &user->num_entries, "user", "num", NULL); @@ -228,7 +241,7 @@ void populate_user() { } void populate_swap() { - swap_stat_t *swap = get_swap_stats(); + sg_swap_stats *swap = sg_get_swap_stats(); if (swap != NULL) { add_stat(LONG_LONG, &swap->total, "swap", "total", NULL); @@ -238,35 +251,47 @@ void populate_swap() { } void populate_general() { - general_stat_t *gen = get_general_stats(); + /* FIXME this should be renamed to host. */ + sg_host_info *host = sg_get_host_info(); - if (gen != NULL) { - add_stat(STRING, &gen->os_name, + if (host != NULL) { + add_stat(STRING, &host->os_name, "general", "os_name", NULL); - add_stat(STRING, &gen->os_release, + add_stat(STRING, &host->os_release, "general", "os_release", NULL); - add_stat(STRING, &gen->os_version, + add_stat(STRING, &host->os_version, "general", "os_version", NULL); - add_stat(STRING, &gen->platform, "general", "platform", NULL); - add_stat(STRING, &gen->hostname, "general", "hostname", NULL); - add_stat(TIME_T, &gen->uptime, "general", "uptime", NULL); + add_stat(STRING, &host->platform, "general", "platform", NULL); + add_stat(STRING, &host->hostname, "general", "hostname", NULL); + add_stat(TIME_T, &host->uptime, "general", "uptime", NULL); } } void populate_fs() { int n, i; - disk_stat_t *disk = get_disk_stats(&n); + 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 */ - const char *name = disk[i].device_name, - *p = strrchr(name, '/'); - if (p != NULL) - name = p + 1; - if (*name == '\0') - name = "root"; - + 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, @@ -285,15 +310,18 @@ void populate_fs() { "fs", name, "used_inodes", NULL); add_stat(LONG_LONG, &disk[i].free_inodes, "fs", name, "free_inodes", NULL); + + free(buf); } } } void populate_disk() { int n, i; - diskio_stat_t *diskio; + sg_disk_io_stats *diskio; - diskio = use_diffs ? get_diskio_stats_diff(&n) : get_diskio_stats(&n); + 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; @@ -311,7 +339,8 @@ void populate_disk() { } void populate_proc() { - process_stat_t *proc = get_process_stats(); + /* FIXME expose individual process info too */ + sg_process_count *proc = sg_get_process_count(); if (proc != NULL) { add_stat(INT, &proc->total, "proc", "total", NULL); @@ -324,33 +353,59 @@ void populate_proc() { void populate_net() { int n, i; - network_stat_t *net; + sg_network_io_stats *io; + sg_network_iface_stats *iface; - net = use_diffs ? get_network_stats_diff(&n) : get_network_stats(&n); - if (net != NULL) { + io = use_diffs ? sg_get_network_io_stats_diff(&n) + : sg_get_network_io_stats(&n); + if (io != NULL) { for (i = 0; i < n; i++) { - const char *name = net[i].interface_name; + const char *name = io[i].interface_name; - add_stat(STRING, &net[i].interface_name, + add_stat(STRING, &io[i].interface_name, "net", name, "interface_name", NULL); - add_stat(LONG_LONG, &net[i].tx, + add_stat(LONG_LONG, &io[i].tx, "net", name, "tx", NULL); - add_stat(LONG_LONG, &net[i].rx, + add_stat(LONG_LONG, &io[i].rx, "net", name, "rx", NULL); - add_stat(TIME_T, &net[i].systime, + add_stat(LONG_LONG, &io[i].ipackets, + "net", name, "ipackets", NULL); + add_stat(LONG_LONG, &io[i].opackets, + "net", name, "opackets", NULL); + add_stat(LONG_LONG, &io[i].ierrors, + "net", name, "ierrors", NULL); + add_stat(LONG_LONG, &io[i].oerrors, + "net", name, "oerrors", NULL); + add_stat(LONG_LONG, &io[i].collisions, + "net", name, "collisions", NULL); + add_stat(TIME_T, &io[i].systime, "net", name, "systime", NULL); } } + + iface = sg_get_network_iface_stats(&n); + if (iface != NULL) { + for (i = 0; i < n; i++) { + const char *name = iface[i].interface_name; + + add_stat(INT, &iface[i].speed, + "net", name, "speed", NULL); + add_stat(BOOL, &iface[i].up, + "net", name, "up", NULL); + add_stat(DUPLEX, &iface[i].dup, + "net", name, "duplex", NULL); + } + } } void populate_page() { - page_stat_t *page; + sg_page_stats *page; - page = use_diffs ? get_page_stats_diff() : get_page_stats(); + page = use_diffs ? sg_get_page_stats_diff() : sg_get_page_stats(); if (page != NULL) { add_stat(LONG_LONG, &page->pages_pagein, "page", "in", NULL); add_stat(LONG_LONG, &page->pages_pageout, "page", "out", NULL); - add_stat(LONG_LONG, &page->systime, "page", "systime", NULL); + add_stat(TIME_T, &page->systime, "page", "systime", NULL); } } @@ -409,12 +464,14 @@ void get_stats() { t->populate(); } - qsort(stats, num_stats, sizeof *stats, stats_compare); + if (stats != NULL) + qsort(stats, num_stats, sizeof *stats, stats_compare); } /* Print the value of a stat. */ void print_stat_value(const stat *s) { void *v = s->stat; + long l; switch (s->type) { case LONG_LONG: @@ -422,7 +479,8 @@ void print_stat_value(const stat *s) { break; case TIME_T: /* FIXME option for formatted time? */ - printf("%ld", *(time_t *)v); + l = *(time_t *)v; + printf("%ld", l); break; case FLOAT: printf("%f", *(float *)v); @@ -437,6 +495,22 @@ void print_stat_value(const stat *s) { case INT: printf("%d", *(int *)v); break; + case BOOL: + printf("%s", *(int *)v ? "true" : "false"); + break; + case DUPLEX: + switch (*(sg_iface_duplex *) v) { + case SG_IFACE_DUPLEX_FULL: + printf("full"); + break; + case SG_IFACE_DUPLEX_HALF: + printf("half"); + break; + default: + printf("unknown"); + break; + } + break; } } @@ -468,14 +542,44 @@ void print_stats(int argc, char **argv) { } else { /* Print selected stats. */ for (i = optind; i < argc; i++) { + char *name = argv[i]; stat key; - const stat *s; + const stat *s, *end; + int (*compare)(const void *, const void *); - key.name = argv[i]; - s = (const stat *)bsearch(&key, stats, num_stats, - sizeof *stats, - stats_compare); - if (s != NULL) { + key.name = name; + if (name[strlen(name) - 1] == '.') + compare = stats_compare_prefix; + else + compare = stats_compare; + + if (stats == NULL) { + s = NULL; + } else { + s = (const stat *)bsearch(&key, stats, + num_stats, + sizeof *stats, + compare); + } + + if (s == NULL) { + printf("Unknown stat %s\n", name); + continue; + } + + /* Find the range of stats the user wanted. */ + for (; s >= stats; s--) { + if (compare(&key, s) != 0) + break; + } + s++; + for (end = s; end < &stats[num_stats]; end++) { + if (compare(&key, end) != 0) + break; + } + + /* And print them. */ + for (; s < end; s++) { print_stat(s); } } @@ -484,7 +588,10 @@ void print_stats(int argc, char **argv) { void usage() { printf("Usage: statgrab [OPTION]... [STAT]...\n" - "Display system statistics (all statistics by default).\n" + "Display system statistics.\n" + "\n" + "If no STATs are given, all will be displayed. Specify 'STAT.' to display all\n" + "statistics starting with that prefix.\n" "\n"); printf(" -l Linux sysctl-style output (default)\n" " -b BSD sysctl-style output\n" @@ -557,6 +664,12 @@ int main(int argc, char **argv) { use_diffs = 1; select_interesting(argc - optind, &argv[optind]); + + /* We don't care if statgrab_init fails, because we can just display + the statistics that can be read as non-root. */ + sg_init(); + if (sg_drop_privileges() != 0) + die("Failed to drop setuid/setgid privileges"); switch (repeat_mode) { case REPEAT_NONE: