-
Notifications
You must be signed in to change notification settings - Fork 9
/
check_loadtrend.c
100 lines (88 loc) · 2.01 KB
/
check_loadtrend.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
static void
usage(void)
{
fprintf(stderr,
"usage: check_loadtrend [-m minimum per-cpu load cutoff]\n"
" [-c critical ratio] [-w warning ratio]\n");
exit(1);
}
static double
getdouble(const char *str)
{
double number;
char *ep;
errno = 0;
number = strtod(str, &ep);
if (errno != 0 || *str == '\0' || *ep != '\0') {
fprintf(stderr, "%s: invalid number\n", str);
exit(1);
}
return (number);
}
static void
testratio(double load1, double load2, double ratio,
const char *severity, int code, int loadtime1, int loadtime2)
{
if (load1 >= ratio * load2) {
printf(
"%s - load%d / load%d = %.2lf / %.2lf = %.2lf, "
"which is greater than or equal to %.2lf\n",
severity, loadtime1, loadtime2, load1, load2,
load1 / load2, ratio);
exit(code);
}
}
int
main(int argc, char *argv[])
{
int ch;
double minload = 1.5, critratio = 1.5, warnratio = 1.1;
double ladv[3];
long ncpus;
while ((ch = getopt(argc, argv, "c:m:w:")) != -1) {
switch (ch) {
case 'c':
critratio = getdouble(optarg);
break;
case 'm':
minload = getdouble(optarg);
break;
case 'w':
warnratio = getdouble(optarg);
break;
default:
usage();
}
}
argv += optind;
argc -= optind;
if (argc != 0)
usage();
if (getloadavg(ladv, 3) == -1) {
printf("CRITICAL - Failed to obtain load averages: %s",
strerror(errno));
return (2);
}
ncpus = sysconf(_SC_NPROCESSORS_ONLN);
if (ncpus == -1) {
printf(
"CRITICAL - Failed to obtain the number of online CPUs: %s",
strerror(errno));
return (2);
}
if (ladv[0] < minload * ncpus) {
printf("OK - load1 = %.2lf, which is less than %.2lf\n",
ladv[0], minload * ncpus);
return (0);
}
testratio(ladv[1], ladv[2], critratio, "CRITICAL", 2, 5, 15);
testratio(ladv[1], ladv[2], warnratio, "WARNING", 1, 5, 15);
printf("OK - load5 / load15 = %.2lf / %.2lf = %.2lf\n",
ladv[1], ladv[2], ladv[1] / ladv[2]);
return (0);
}