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 autoconf_check_readable(const char *path) { if(0 == access(path, R_OK)) return writeyes(); else { printf("no (%s is not readable, errno=%d)\n", path, errno); return 0; } }
int open_files(int argc, char **argv) { FILE *f; int alloc, freeh, avail; if(argc > 1) { if(!strcmp(argv[1], "config")) { if(!(f=fopen(FS_FILE_NR, "r"))) { fprintf(stderr, "cannot open " FS_FILE_NR "\n"); return 1; } if(1 != fscanf(f, "%*d %*d %d", &avail)) { fclose(f); fprintf(stderr, "cannot read from " FS_FILE_NR "\n"); return 1; } fclose(f); puts("graph_title File table usage\n" "graph_args --base 1000 -l 0\n" "graph_vlabel number of open files\n" "graph_category system\n" "graph_info This graph monitors the Linux open files table.\n" "used.label open files\n" "used.info The number of currently open files.\n" "max.label max open files\n" "max.info The maximum supported number of open " "files. Tune by modifying " FS_FILE_NR "."); printf("used.warning %d\nused.critical %d\n", (int)(avail*0.92), (int)(avail*0.98)); return 0; } if(!strcmp(argv[1], "autoconf")) { if(0 == access(FS_FILE_NR, R_OK)) return writeyes(); else return writeno(FS_FILE_NR " not readable"); } } if(!(f=fopen(FS_FILE_NR, "r"))) { fputs("cannot open " FS_FILE_NR "\n", stderr); return 1; } if(3 != fscanf(f, "%d %d %d", &alloc, &freeh, &avail)) { fclose(f); fputs("cannot read from " FS_FILE_NR "\n", stderr); return 1; } fclose(f); printf("used.value %d\nmax.value %d\n", alloc-freeh, avail); return 0; }
int threads(int argc, char **argv) { FILE *f; char buff[256]; const char *s; int i, sum; DIR *d; struct dirent *e; if(argc > 1) { if(!strcmp(argv[1], "autoconf")) { i = getpid(); sprintf(buff, "/proc/%d/status", i); if(NULL == (f = fopen(buff, "r"))) return fail("failed to open /proc/$$/status"); while(fgets(buff, 256, f)) if(!strncmp(buff, "Threads:", 8)) { fclose(f); return writeyes(); } fclose(f); puts("no"); return 0; } if(!strcmp(argv[1], "config")) { puts("graph_title Number of threads\n" "graph_vlabel number of threads\n" "graph_category processes\n" "graph_info This graph shows the number of threads.\n" "threads.label threads\n" "threads.info The current number of threads."); return 0; } } if(NULL == (d = opendir("/proc"))) return fail("cannot open /proc"); sum = 0; while((e = readdir(d))) { for(s=e->d_name;*s;++s) if(!isdigit(*s)) break; if(*s) /* non-digit found */ continue; snprintf(buff, 256, "/proc/%s/status", e->d_name); if(!(f = fopen(buff, "r"))) continue; /* process has vanished */ while(fgets(buff, 256, f)) { if(strncmp(buff, "Threads:", 8)) continue; if(1 != sscanf(buff+8, "%d", &i)) { fclose(f); closedir(d); return fail("failed to parse " "/proc/somepid/status"); } sum += i; } fclose(f); } closedir(d); printf("threads.value %d\n", sum); 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; } }