root / tools / munin-plugins-busybox / swap.c @ cbfa6b45
Historique | Voir | Annoter | Télécharger (1,84 ko)
| 1 |
#include <string.h> |
|---|---|
| 2 |
#include <stdio.h> |
| 3 |
#include <unistd.h> |
| 4 |
#include "common.h" |
| 5 |
|
| 6 |
int swap(int argc, char **argv) { |
| 7 |
FILE *f; |
| 8 |
char buff[256]; |
| 9 |
int in, out;
|
| 10 |
if(argc > 1) { |
| 11 |
if(!strcmp(argv[1], "config")) { |
| 12 |
puts("graph_title Swap in/out\n"
|
| 13 |
"graph_args -l 0 --base 1000\n"
|
| 14 |
"graph_vlabel pages per ${graph_period} in (-) / out (+)\n"
|
| 15 |
"graph_category system\n"
|
| 16 |
"swap_in.label swap\n"
|
| 17 |
"swap_in.type DERIVE\n"
|
| 18 |
"swap_in.max 100000\n"
|
| 19 |
"swap_in.min 0\n"
|
| 20 |
"swap_in.graph no\n"
|
| 21 |
"swap_out.label swap\n"
|
| 22 |
"swap_out.type DERIVE\n"
|
| 23 |
"swap_out.max 100000\n"
|
| 24 |
"swap_out.min 0\n"
|
| 25 |
"swap_out.negative swap_in");
|
| 26 |
print_warncrit("swap_in");
|
| 27 |
print_warncrit("swap_out");
|
| 28 |
return 0; |
| 29 |
} |
| 30 |
if(!strcmp(argv[1], "autoconf")) { |
| 31 |
if(0 == access(PROC_STAT, R_OK)) |
| 32 |
return writeyes();
|
| 33 |
else
|
| 34 |
return writeno(PROC_STAT " not readable"); |
| 35 |
} |
| 36 |
} |
| 37 |
if(!access("/proc/vmstat", F_OK)) { |
| 38 |
in=out=0;
|
| 39 |
if(!(f=fopen("/proc/vmstat", "r"))) { |
| 40 |
fputs("cannot open /proc/vmstat\n", stderr);
|
| 41 |
return 1; |
| 42 |
} |
| 43 |
while(fgets(buff, 256, f)) { |
| 44 |
if(!in && !strncmp(buff, "pswpin ", 7)) { |
| 45 |
++in; |
| 46 |
printf("swap_in.value %s", buff+7); |
| 47 |
} |
| 48 |
else if(!out && !strncmp(buff, "pswpout ", 8)) { |
| 49 |
++out; |
| 50 |
printf("swap_out.value %s", buff+8); |
| 51 |
} |
| 52 |
} |
| 53 |
fclose(f); |
| 54 |
if(!(in*out)) {
|
| 55 |
fputs("no usable data on /proc/vmstat\n", stderr);
|
| 56 |
return 1; |
| 57 |
} |
| 58 |
return 0; |
| 59 |
} else {
|
| 60 |
if(!(f=fopen(PROC_STAT, "r"))) { |
| 61 |
fputs("cannot open " PROC_STAT "\n", stderr); |
| 62 |
return 1; |
| 63 |
} |
| 64 |
while(fgets(buff, 256, f)) { |
| 65 |
if(!strncmp(buff, "swap ", 5)) { |
| 66 |
fclose(f); |
| 67 |
if(2 != sscanf(buff+5, "%d %d", &in, &out)) { |
| 68 |
fputs("bad data on " PROC_STAT "\n", |
| 69 |
stderr); |
| 70 |
return 1; |
| 71 |
} |
| 72 |
printf("swap_in.value %d\nswap_out.value %d\n", in, out);
|
| 73 |
return 0; |
| 74 |
} |
| 75 |
} |
| 76 |
fclose(f); |
| 77 |
fputs("no swap line found in " PROC_STAT "\n", stderr); |
| 78 |
return 1; |
| 79 |
} |
| 80 |
} |
