diff options
Diffstat (limited to 'web/attachments/136226-nagiosplug-check_load.diff')
-rw-r--r-- | web/attachments/136226-nagiosplug-check_load.diff | 311 |
1 files changed, 311 insertions, 0 deletions
diff --git a/web/attachments/136226-nagiosplug-check_load.diff b/web/attachments/136226-nagiosplug-check_load.diff new file mode 100644 index 0000000..2330ab4 --- /dev/null +++ b/web/attachments/136226-nagiosplug-check_load.diff | |||
@@ -0,0 +1,311 @@ | |||
1 | diff -urN ../orig.nplg/plugins/check_load.c ./plugins/check_load.c | ||
2 | --- ../orig.nplg/plugins/check_load.c 2005-05-27 15:56:34.000000000 +0200 | ||
3 | +++ ./plugins/check_load.c 2005-05-27 18:04:50.000000000 +0200 | ||
4 | @@ -39,37 +39,64 @@ | ||
5 | #endif /* !defined LOADAVG_1MIN */ | ||
6 | |||
7 | |||
8 | -int process_arguments (int argc, char **argv); | ||
9 | -int validate_arguments (void); | ||
10 | +static int process_arguments (int argc, char **argv); | ||
11 | +static int validate_arguments (void); | ||
12 | void print_help (void); | ||
13 | void print_usage (void); | ||
14 | |||
15 | -float wload1 = -1, wload5 = -1, wload15 = -1; | ||
16 | -float cload1 = -1, cload5 = -1, cload15 = -1; | ||
17 | +/* strictly for pretty-print usage in loops */ | ||
18 | +static const int nums[3] = { 1, 5, 15 }; | ||
19 | + | ||
20 | +/* provide some fairly sane defaults */ | ||
21 | +double wload[3] = { 0.0, 0.0, 0.0 }; | ||
22 | +double cload[3] = { 0.0, 0.0, 0.0 }; | ||
23 | +#define la1 la[0] | ||
24 | +#define la5 la[1] | ||
25 | +#define la15 la[2] | ||
26 | |||
27 | char *status_line; | ||
28 | |||
29 | +static void | ||
30 | +get_threshold(char *arg, double *th) | ||
31 | +{ | ||
32 | + size_t i, n; | ||
33 | + char *str = arg, *p; | ||
34 | + | ||
35 | + n = strlen(arg); | ||
36 | + for(i = 0; i < 3; i++) { | ||
37 | + th[i] = strtod(str, &p); | ||
38 | + if(p == str) break; | ||
39 | + | ||
40 | + str = p + 1; | ||
41 | + if(n <= (size_t)(str - arg)) break; | ||
42 | + } | ||
43 | + | ||
44 | + /* empty argument or non-floatish, so warn about it and die */ | ||
45 | + if(!i) usage (_("Warning threshold must be float or float triplet!\n")); | ||
46 | + | ||
47 | + if(i != 2) { | ||
48 | + /* one or more numbers were given, so fill array with last | ||
49 | + * we got (most likely to NOT produce the least expected result) */ | ||
50 | + for(n = i; n < 3; n++) th[n] = th[i]; | ||
51 | + } | ||
52 | +} | ||
53 | |||
54 | |||
55 | int | ||
56 | main (int argc, char **argv) | ||
57 | { | ||
58 | - int result = STATE_UNKNOWN; | ||
59 | - | ||
60 | -#if HAVE_GETLOADAVG==1 | ||
61 | + int result; | ||
62 | + int i; | ||
63 | + | ||
64 | double la[3] = { 0.0, 0.0, 0.0 }; /* NetBSD complains about unitialized arrays */ | ||
65 | -#else | ||
66 | -# if HAVE_PROC_LOADAVG==1 | ||
67 | - FILE *fp; | ||
68 | - char input_buffer[MAX_INPUT_BUFFER]; | ||
69 | - char *tmp_ptr; | ||
70 | -# else | ||
71 | +#ifndef HAVE_GETLOADAVG | ||
72 | char input_buffer[MAX_INPUT_BUFFER]; | ||
73 | +# ifdef HAVE_PROC_LOADAVG | ||
74 | + FILE *fp; | ||
75 | + char *str, *next; | ||
76 | # endif | ||
77 | #endif | ||
78 | |||
79 | - float la1, la5, la15; | ||
80 | - | ||
81 | setlocale (LC_ALL, ""); | ||
82 | bindtextdomain (PACKAGE, LOCALEDIR); | ||
83 | textdomain (PACKAGE); | ||
84 | @@ -77,30 +104,24 @@ | ||
85 | if (process_arguments (argc, argv) == ERROR) | ||
86 | usage4 (_("Could not parse arguments")); | ||
87 | |||
88 | -#if HAVE_GETLOADAVG==1 | ||
89 | +#ifdef HAVE_GETLOADAVG | ||
90 | result = getloadavg (la, 3); | ||
91 | - if (result == -1) | ||
92 | + if (result != 3) | ||
93 | return STATE_UNKNOWN; | ||
94 | - la1 = la[LOADAVG_1MIN]; | ||
95 | - la5 = la[LOADAVG_5MIN]; | ||
96 | - la15 = la[LOADAVG_15MIN]; | ||
97 | #else | ||
98 | -# if HAVE_PROC_LOADAVG==1 | ||
99 | +# ifdef HAVE_PROC_LOADAVG | ||
100 | fp = fopen (PROC_LOADAVG, "r"); | ||
101 | if (fp == NULL) { | ||
102 | printf (_("Error opening %s\n"), PROC_LOADAVG); | ||
103 | return STATE_UNKNOWN; | ||
104 | } | ||
105 | |||
106 | - la1 = la5 = la15 = -1; | ||
107 | - | ||
108 | while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, fp)) { | ||
109 | - tmp_ptr = strtok (input_buffer, " "); | ||
110 | - la1 = atof (tmp_ptr); | ||
111 | - tmp_ptr = strtok (NULL, " "); | ||
112 | - la5 = atof (tmp_ptr); | ||
113 | - tmp_ptr = strtok (NULL, " "); | ||
114 | - la15 = atof (tmp_ptr); | ||
115 | + str = (char *)input_buffer; | ||
116 | + for(i = 0; i < 3; i++) { | ||
117 | + la[i] = strtod(str, &next); | ||
118 | + str = next; | ||
119 | + } | ||
120 | } | ||
121 | |||
122 | fclose (fp); | ||
123 | @@ -125,12 +146,11 @@ | ||
124 | # endif | ||
125 | #endif | ||
126 | |||
127 | - | ||
128 | - if ((la1 < 0.0) || (la5 < 0.0) || (la15 < 0.0)) { | ||
129 | -#if HAVE_GETLOADAVG==1 | ||
130 | + if ((la[0] < 0.0) || (la[1] < 0.0) || (la[2] < 0.0)) { | ||
131 | +#ifdef HAVE_GETLOADAVG | ||
132 | printf (_("Error in getloadavg()\n")); | ||
133 | #else | ||
134 | -# if HAVE_PROC_LOADAVG==1 | ||
135 | +# ifdef HAVE_PROC_LOADAVG | ||
136 | printf (_("Error processing %s\n"), PROC_LOADAVG); | ||
137 | # else | ||
138 | printf (_("Error processing %s\n"), PATH_TO_UPTIME); | ||
139 | @@ -139,29 +159,30 @@ | ||
140 | return STATE_UNKNOWN; | ||
141 | } | ||
142 | |||
143 | + /* we got this far, so assume OK until we've measured */ | ||
144 | + result = STATE_OK; | ||
145 | + | ||
146 | asprintf(&status_line, _("load average: %.2f, %.2f, %.2f"), la1, la5, la15); | ||
147 | |||
148 | - if ((la1 >= cload1) || (la5 >= cload5) || (la15 >= cload15)) | ||
149 | - result = STATE_CRITICAL; | ||
150 | - else if ((la1 >= wload1) || (la5 >= wload5) || (la15 >= wload15)) | ||
151 | - result = STATE_WARNING; | ||
152 | - else | ||
153 | - result = STATE_OK; | ||
154 | - | ||
155 | - die (result, | ||
156 | - "%s - %s|%s %s %s\n", | ||
157 | - state_text (result), | ||
158 | - status_line, | ||
159 | - fperfdata ("load1", la1, "", (int)wload1, wload1, (int)cload1, cload1, TRUE, 0, FALSE, 0), | ||
160 | - fperfdata ("load5", la5, "", (int)wload5, wload5, (int)cload5, cload5, TRUE, 0, FALSE, 0), | ||
161 | - fperfdata ("load15", la15, "", (int)wload15, wload15, (int)cload15, cload15, TRUE, 0, FALSE, 0)); | ||
162 | - return STATE_OK; | ||
163 | -} | ||
164 | + for(i = 0; i < 3; i++) { | ||
165 | + if(la[i] > cload[i]) { | ||
166 | + result = STATE_CRITICAL; | ||
167 | + break; | ||
168 | + } | ||
169 | + else if(la[i] > wload[i]) result = STATE_WARNING; | ||
170 | + } | ||
171 | |||
172 | + printf("%s - %s|", state_text(result), status_line); | ||
173 | + for(i = 0; i < 3; i++) | ||
174 | + printf("load%d=%.3f;%.3f;%.3f;0; ", nums[i], la[i], wload[i], cload[i]); | ||
175 | + | ||
176 | + putchar('\n'); | ||
177 | + return result; | ||
178 | +} | ||
179 | |||
180 | |||
181 | /* process command-line arguments */ | ||
182 | -int | ||
183 | +static int | ||
184 | process_arguments (int argc, char **argv) | ||
185 | { | ||
186 | int c = 0; | ||
187 | @@ -185,37 +206,11 @@ | ||
188 | break; | ||
189 | |||
190 | switch (c) { | ||
191 | - case 'w': /* warning time threshold */ | ||
192 | - if (is_intnonneg (optarg)) { | ||
193 | - wload1 = atof (optarg); | ||
194 | - wload5 = atof (optarg); | ||
195 | - wload15 = atof (optarg); | ||
196 | - break; | ||
197 | - } | ||
198 | - else if (strstr (optarg, ",") && | ||
199 | - sscanf (optarg, "%f,%f,%f", &wload1, &wload5, &wload15) == 3) | ||
200 | - break; | ||
201 | - else if (strstr (optarg, ":") && | ||
202 | - sscanf (optarg, "%f:%f:%f", &wload1, &wload5, &wload15) == 3) | ||
203 | - break; | ||
204 | - else | ||
205 | - usage (_("Warning threshold must be float or float triplet!\n")); | ||
206 | + case 'w': /* warning time threshold */ | ||
207 | + get_threshold(optarg, wload); | ||
208 | break; | ||
209 | - case 'c': /* critical time threshold */ | ||
210 | - if (is_intnonneg (optarg)) { | ||
211 | - cload1 = atof (optarg); | ||
212 | - cload5 = atof (optarg); | ||
213 | - cload15 = atof (optarg); | ||
214 | - break; | ||
215 | - } | ||
216 | - else if (strstr (optarg, ",") && | ||
217 | - sscanf (optarg, "%f,%f,%f", &cload1, &cload5, &cload15) == 3) | ||
218 | - break; | ||
219 | - else if (strstr (optarg, ":") && | ||
220 | - sscanf (optarg, "%f:%f:%f", &cload1, &cload5, &cload15) == 3) | ||
221 | - break; | ||
222 | - else | ||
223 | - usage (_("Critical threshold must be float or float triplet!\n")); | ||
224 | + case 'c': /* critical time threshold */ | ||
225 | + get_threshold(optarg, cload); | ||
226 | break; | ||
227 | case 'V': /* version */ | ||
228 | print_revision (progname, revision); | ||
229 | @@ -231,60 +226,38 @@ | ||
230 | c = optind; | ||
231 | if (c == argc) | ||
232 | return validate_arguments (); | ||
233 | - if (wload1 < 0 && is_nonnegative (argv[c])) | ||
234 | - wload1 = atof (argv[c++]); | ||
235 | - | ||
236 | - if (c == argc) | ||
237 | - return validate_arguments (); | ||
238 | - if (cload1 < 0 && is_nonnegative (argv[c])) | ||
239 | - cload1 = atof (argv[c++]); | ||
240 | - | ||
241 | - if (c == argc) | ||
242 | - return validate_arguments (); | ||
243 | - if (wload5 < 0 && is_nonnegative (argv[c])) | ||
244 | - wload5 = atof (argv[c++]); | ||
245 | - | ||
246 | - if (c == argc) | ||
247 | - return validate_arguments (); | ||
248 | - if (cload5 < 0 && is_nonnegative (argv[c])) | ||
249 | - cload5 = atof (argv[c++]); | ||
250 | |||
251 | - if (c == argc) | ||
252 | - return validate_arguments (); | ||
253 | - if (wload15 < 0 && is_nonnegative (argv[c])) | ||
254 | - wload15 = atof (argv[c++]); | ||
255 | - | ||
256 | - if (c == argc) | ||
257 | - return validate_arguments (); | ||
258 | - if (cload15 < 0 && is_nonnegative (argv[c])) | ||
259 | - cload15 = atof (argv[c++]); | ||
260 | + /* handle the case if both arguments are missing, | ||
261 | + * but not if only one is given without -c or -w flag */ | ||
262 | + if(c - argc == 2) { | ||
263 | + get_threshold(argv[c++], wload); | ||
264 | + get_threshold(argv[c++], cload); | ||
265 | + } | ||
266 | + else if(c - argc == 1) { | ||
267 | + get_threshold(argv[c++], cload); | ||
268 | + } | ||
269 | |||
270 | return validate_arguments (); | ||
271 | } | ||
272 | |||
273 | |||
274 | |||
275 | -int | ||
276 | +static int | ||
277 | validate_arguments (void) | ||
278 | { | ||
279 | - if (wload1 < 0) | ||
280 | - usage (_("Warning threshold for 1-minute load average is not specified\n")); | ||
281 | - if (wload5 < 0) | ||
282 | - usage (_("Warning threshold for 5-minute load average is not specified\n")); | ||
283 | - if (wload15 < 0) | ||
284 | - usage (_("Warning threshold for 15-minute load average is not specified\n")); | ||
285 | - if (cload1 < 0) | ||
286 | - usage (_("Critical threshold for 1-minute load average is not specified\n")); | ||
287 | - if (cload5 < 0) | ||
288 | - usage (_("Critical threshold for 5-minute load average is not specified\n")); | ||
289 | - if (cload15 < 0) | ||
290 | - usage (_("Critical threshold for 15-minute load average is not specified\n")); | ||
291 | - if (wload1 > cload1) | ||
292 | - usage (_("Parameter inconsistency: 1-minute \"warning load\" greater than \"critical load\".\n")); | ||
293 | - if (wload5 > cload5) | ||
294 | - usage (_("Parameter inconsistency: 5-minute \"warning load\" greater than \"critical load\".\n")); | ||
295 | - if (wload15 > cload15) | ||
296 | - usage (_("Parameter inconsistency: 15-minute \"warning load\" greater than \"critical load\".\n")); | ||
297 | + int i = 0; | ||
298 | + | ||
299 | + /* match cload first, as it will give the most friendly error message | ||
300 | + * if user hasn't given the -c switch properly */ | ||
301 | + for(i = 0; i < 3; i++) { | ||
302 | + if(cload[i] < 0) | ||
303 | + die (STATE_UNKNOWN, _("Critical threshold for %d-minute load average is not specified\n"), nums[i]); | ||
304 | + if(wload[i] < 0) | ||
305 | + die (STATE_UNKNOWN, _("Warning threshold for %d-minute load average is not specified\n"), nums[i]); | ||
306 | + if(wload[i] > cload[i]) | ||
307 | + die (STATE_UNKNOWN, _("Parameter inconsistency: %d-minute \"warning load\" is greater than \"critical load\"\n"), nums[i]); | ||
308 | + } | ||
309 | + | ||
310 | return OK; | ||
311 | } | ||