int open_inodes(int argc, char **argv) { FILE *f; int nr, freen; if (argc > 1) { if (!strcmp(argv[1], "config")) { puts("graph_title Inode table usage\n" "graph_args --base 1000 -l 0\n" "graph_vlabel number of open inodes\n" "graph_category system\n" "graph_info This graph monitors the Linux open inode table.\n" "used.label open inodes\n" "used.info The number of currently open inodes.\n" "max.label inode table size\n" "max.info The size of the system inode table. This is dynamically adjusted by the kernel."); print_warncrit("used"); print_warncrit("max"); return 0; } if (!strcmp(argv[1], "autoconf")) return autoconf_check_readable(FS_INODE_NR); } if (!(f = fopen(FS_INODE_NR, "r"))) return fail("cannot open " FS_INODE_NR); if (2 != fscanf(f, "%d %d", &nr, &freen)) { fclose(f); return fail("cannot read from " FS_INODE_NR); } fclose(f); printf("used.value %d\nmax.value %d\n", nr - freen, nr); return 0; }
int uptime(int argc, char **argv) { FILE *f; float uptime; if(argc > 1) { if(!strcmp(argv[1], "config")) { puts("graph_title Uptime\n" "graph_args --base 1000 -l 0 \n" "graph_vlabel uptime in days\n" "uptime.label uptime\n" "uptime.draw AREA"); print_warncrit("uptime"); return 0; } if(!strcmp(argv[1], "autoconf")) return writeyes(); } if(!(f=fopen("/proc/uptime", "r"))) return fail("cannot open /proc/uptime"); if(1 != fscanf(f, "%f", &uptime)) { fclose(f); return fail("cannot read from /proc/uptime"); } fclose(f); printf("uptime.value %.2f\n", uptime/86400); return 0; }
int load(int argc, char **argv) { FILE *f; float val; if(argc > 1) { if(!strcmp(argv[1], "config")) { puts("graph_title Load average\n" "graph_args --base 1000 -l 0\n" "graph_vlabel load\n" "graph_scale no\n" "graph_category system\n" "load.label load"); print_warncrit("load"); puts("graph_info The load average of the machine describes how many processes are in the run-queue (scheduled to run \"immediately\").\n" "load.info 5 minute load average"); return 0; } if(!strcmp(argv[1], "autoconf")) return writeyes(); } if(!(f=fopen(PROC_LOADAVG, "r"))) return fail("cannot open " PROC_LOADAVG); if(1 != fscanf(f, "%*f %f", &val)) { fclose(f); return fail("cannot read from " PROC_LOADAVG); } fclose(f); printf("load.value %.2f\n", val); return 0; }
int forks(int argc, char **argv) { FILE *f; char buff[256]; if(argc > 1) { if(!strcmp(argv[1], "config")) { puts("graph_title Fork rate\n" "graph_args --base 1000 -l 0 \n" "graph_vlabel forks / ${graph_period}\n" "graph_category processes\n" "graph_info This graph shows the forking rate (new processes started).\n" "forks.label forks\n" "forks.type DERIVE\n" "forks.min 0\n" "forks.max 100000\n" "forks.info The number of forks per second."); print_warncrit("forks"); return 0; } if(!strcmp(argv[1], "autoconf")) return autoconf_check_readable(PROC_STAT); } if(!(f=fopen(PROC_STAT, "r"))) return fail("cannot open " PROC_STAT); while(fgets(buff, 256, f)) { if(!strncmp(buff, "processes ", 10)) { fclose(f); printf("forks.value %s", buff+10); return 0; } } fclose(f); return fail("no processes line found in " PROC_STAT); }
int entropy(int argc, char **argv) { FILE *f; int entropy; if(argc > 1) { if(!strcmp(argv[1], "config")) { puts("graph_title Available entropy\n" "graph_args --base 1000 -l 0\n" "graph_vlabel entropy (bytes)\n" "graph_scale no\n" "graph_category system\n" "graph_info This graph shows the amount of entropy available in the system.\n" "entropy.label entropy\n" "entropy.info The number of random bytes available. This is typically used by cryptographic applications."); print_warncrit("entropy"); return 0; } if(!strcmp(argv[1], "autoconf")) return autoconf_check_readable(ENTROPY_AVAIL); } if(!(f=fopen(ENTROPY_AVAIL, "r"))) return fail("cannot open " ENTROPY_AVAIL); if(1 != fscanf(f, "%d", &entropy)) { fclose(f); return fail("cannot read from " ENTROPY_AVAIL); } fclose(f); printf("entropy.value %d\n", entropy); return 0; }
int interrupts(int argc, char **argv) { FILE *f; char buff[256]; if(argc > 1) { if(!strcmp(argv[1], "config")) { puts("graph_title Interrupts and context switches\n" "graph_args --base 1000 -l 0\n" "graph_vlabel interrupts & ctx switches / ${graph_period}\n" "graph_category system\n" "graph_info This graph shows the number of interrupts and context switches on the system. These are typically high on a busy system.\n" "intr.info Interrupts are events that alter sequence of instructions executed by a processor. They can come from either hardware (exceptions, NMI, IRQ) or software."); puts("ctx.info A context switch occurs when a multitasking operatings system suspends the currently running process, and starts executing another.\n" "intr.label interrupts\n" "ctx.label context switches\n" "intr.type DERIVE\n" "ctx.type DERIVE\n" "intr.max 100000\n" "ctx.max 100000\n" "intr.min 0\n" "ctx.min 0"); print_warncrit("intr"); print_warncrit("ctx"); return 0; } if(!strcmp(argv[1], "autoconf")) return autoconf_check_readable(PROC_STAT); } if(!(f=fopen(PROC_STAT, "r"))) return fail("cannot open " PROC_STAT); while(fgets(buff, 256, f)) { if(!strncmp(buff, "intr ", 5)) { buff[5 + strcspn(buff + 5, " \t\n")] = '\0'; printf("intr.value %s\n", buff+5); } else if(!strncmp(buff, "ctxt ", 5)) { buff[5 + strcspn(buff + 5, " \t\n")] = '\0'; printf("ctx.value %s\n", buff+5); } } fclose(f); return 0; }
int if_err_(int argc, char **argv) { char *interface; size_t interface_len; FILE *f; char buff[256], *s; int i; interface = basename(argv[0]); if(strncmp(interface, "if_err_", 7) != 0) return fail("if_err_ invoked with invalid basename"); interface += 7; interface_len = strlen(interface); if(argc > 1) { if(!strcmp(argv[1], "autoconf")) return autoconf_check_readable(PROC_NET_DEV); if(!strcmp(argv[1], "suggest")) { if(NULL == (f = fopen(PROC_NET_DEV, "r"))) return 1; while(fgets(buff, 256, f)) { for(s=buff;*s == ' ';++s) ; i = 0; if(!strncmp(s, "lo:", 3)) continue; if(!strncmp(s, "sit", 3)) { for(i=3; xisdigit(s[i]); ++i) ; if(s[i] == ':') continue; } while(s[i] != ':' && s[i] != '\0') ++i; if(s[i] != ':') continue; /* a header line */ s[i] = '\0'; puts(s); } fclose(f); return 0; } if(!strcmp(argv[1], "config")) { puts("graph_order rcvd trans"); printf("graph_title %s errors\n", interface); puts("graph_args --base 1000\n" "graph_vlabel packets in (-) / out (+) per " "${graph_period}\n" "graph_category network"); printf("graph_info This graph shows the amount of " "errors on the %s network interface.\n", interface); puts("rcvd.label packets\n" "rcvd.type COUNTER\n" "rcvd.graph no\n" "rcvd.warning 1\n" "trans.label packets\n" "trans.type COUNTER\n" "trans.negative rcvd\n" "trans.warning 1"); print_warncrit("rcvd"); print_warncrit("trans"); return 0; } } if(NULL == (f = fopen(PROC_NET_DEV, "r"))) return 1; while(fgets(buff, 256, f)) { for(s=buff;*s == ' ';++s) ; if(0 != strncmp(s, interface, interface_len)) continue; s += interface_len; if(*s != ':') continue; ++s; while(*s == ' ') ++s; for(i=1;i<3;++i) { while(xisdigit(*s)) ++s; while(xisspace(*s)) ++s; } for(i=0;xisdigit(s[i]);++i) ; printf("rcvd.value "); fwrite(s, 1, i, stdout); putchar('\n'); s += i; while(xisspace(*s)) ++s; for(i=4;i<11;++i) { while(xisdigit(*s)) ++s; while(xisspace(*s)) ++s; } for(i=0;xisdigit(s[i]);++i) ; printf("trans.value "); fwrite(s, 1, i, stdout); putchar('\n'); } fclose(f); return 0; }
int swap(int argc, char **argv) { FILE *f; char buff[256]; int in, out; if(argc > 1) { if(!strcmp(argv[1], "config")) { puts("graph_title Swap in/out\n" "graph_args -l 0 --base 1000\n" "graph_vlabel pages per ${graph_period} in (-) / out (+)\n" "graph_category system\n" "swap_in.label swap\n" "swap_in.type DERIVE\n" "swap_in.max 100000\n" "swap_in.min 0\n" "swap_in.graph no\n" "swap_out.label swap\n" "swap_out.type DERIVE\n" "swap_out.max 100000\n" "swap_out.min 0\n" "swap_out.negative swap_in"); print_warncrit("swap_in"); print_warncrit("swap_out"); return 0; } if(!strcmp(argv[1], "autoconf")) { if(0 == access(PROC_STAT, R_OK)) return writeyes(); else return writeno(PROC_STAT " not readable"); } } if(!access("/proc/vmstat", F_OK)) { in=out=0; if(!(f=fopen("/proc/vmstat", "r"))) { fputs("cannot open /proc/vmstat\n", stderr); return 1; } while(fgets(buff, 256, f)) { if(!in && !strncmp(buff, "pswpin ", 7)) { ++in; printf("swap_in.value %s", buff+7); } else if(!out && !strncmp(buff, "pswpout ", 8)) { ++out; printf("swap_out.value %s", buff+8); } } fclose(f); if(!(in*out)) { fputs("no usable data on /proc/vmstat\n", stderr); return 1; } return 0; } else { if(!(f=fopen(PROC_STAT, "r"))) { fputs("cannot open " PROC_STAT "\n", stderr); return 1; } while(fgets(buff, 256, f)) { if(!strncmp(buff, "swap ", 5)) { fclose(f); if(2 != sscanf(buff+5, "%d %d", &in, &out)) { fputs("bad data on " PROC_STAT "\n", stderr); return 1; } printf("swap_in.value %d\nswap_out.value %d\n", in, out); return 0; } } fclose(f); fputs("no swap line found in " PROC_STAT "\n", stderr); return 1; } }