summaryrefslogtreecommitdiffstats
path: root/plugins/check_mrtgtraf.c
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/check_mrtgtraf.c')
-rw-r--r--plugins/check_mrtgtraf.c419
1 files changed, 419 insertions, 0 deletions
diff --git a/plugins/check_mrtgtraf.c b/plugins/check_mrtgtraf.c
new file mode 100644
index 0000000..11f5146
--- /dev/null
+++ b/plugins/check_mrtgtraf.c
@@ -0,0 +1,419 @@
1/******************************************************************************
2 *
3 * CHECK_MRTGTRAF.C
4 *
5 * Program: MRTG (Multi-Router Traffic Grapher) traffic plugin for Nagios
6 * License: GPL
7 * Copyright (c) 1999 Ethan Galstad (nagios@nagios.org)
8 *
9 * Last Modified: $Date$
10 *
11 * Command line: CHECK_MRTGTRAF <log_file> <expire_minutes> <AVG|MAX> <iwl> <icl> <owl> <ocl>
12 *
13 * Description:
14 *
15 * This plugin will check the incoming/outgoing transfer rates of a
16 * router, switch, etc recorded in an MRTG log. If the newest log
17 * entry is older than <expire_minutes>, a WARNING status is returned.
18 * If either the incoming or outgoing rates exceed the <icl> or <ocl>
19 * thresholds (in Bytes/sec), a CRITICAL status results. If either of
20 * the rates exceed the <iwl> or <owl> thresholds (in Bytes/sec), a
21 * WARNING status results.
22 *
23 * Notes:
24 * - MRTG stands for the Multi Router Traffic Grapher. It can be
25 * downloaded from
26 * http://ee-staff.ethz.ch/~oetiker/webtools/mrtg/mrtg.html
27 * - While MRTG can monitor things other than traffic rates, this
28 * plugin probably won't work with much else without modification.
29 * - The calculated i/o rates are a little off from what MRTG actually
30 * reports. I'm not sure why this is right now, but will look into it
31 * for future enhancements of this plugin.
32 *
33 * License Information:
34 *
35 * This program is free software; you can redistribute it and/or modify
36 * it under the terms of the GNU General Public License as published by
37 * the Free Software Foundation; either version 2 of the License, or
38 * (at your option) any later version.
39 *
40 * This program is distributed in the hope that it will be useful,
41 * but WITHOUT ANY WARRANTY; without even the implied warranty of
42 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
43 * GNU General Public License for more details.
44 *
45 * You should have received a copy of the GNU General Public License
46 * along with this program; if not, write to the Free Software
47 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
48 *
49 *****************************************************************************/
50
51#include "config.h"
52#include "common.h"
53#include "utils.h"
54
55#define PROGNAME "check_mrtgtraf"
56
57int process_arguments (int, char **);
58int call_getopt (int, char **);
59int validate_arguments (void);
60void print_help (void);
61void print_usage (void);
62
63char *log_file = NULL;
64int expire_minutes = -1;
65int use_average = TRUE;
66unsigned long incoming_warning_threshold = 0L;
67unsigned long incoming_critical_threshold = 0L;
68unsigned long outgoing_warning_threshold = 0L;
69unsigned long outgoing_critical_threshold = 0L;
70
71int
72main (int argc, char **argv)
73{
74 int result = STATE_OK;
75 FILE *fp;
76 int line;
77 char input_buffer[MAX_INPUT_BUFFER];
78 char *temp_buffer;
79 time_t current_time;
80 char error_message[MAX_INPUT_BUFFER];
81 time_t timestamp = 0L;
82 unsigned long average_incoming_rate = 0L;
83 unsigned long average_outgoing_rate = 0L;
84 unsigned long maximum_incoming_rate = 0L;
85 unsigned long maximum_outgoing_rate = 0L;
86 unsigned long incoming_rate = 0L;
87 unsigned long outgoing_rate = 0L;
88 double adjusted_incoming_rate = 0.0;
89 double adjusted_outgoing_rate = 0.0;
90 char incoming_speed_rating[8];
91 char outgoing_speed_rating[8];
92
93 if (process_arguments (argc, argv) != OK)
94 usage ("Invalid command arguments supplied\n");
95
96 /* open the MRTG log file for reading */
97 fp = fopen (log_file, "r");
98 if (fp == NULL)
99 usage ("Unable to open MRTG log file\n");
100
101 line = 0;
102 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, fp)) {
103
104 line++;
105
106 /* skip the first line of the log file */
107 if (line == 1)
108 continue;
109
110 /* break out of read loop */
111 /* if we've passed the number of entries we want to read */
112 if (line > 2)
113 break;
114
115 /* grab the timestamp */
116 temp_buffer = strtok (input_buffer, " ");
117 timestamp = strtoul (temp_buffer, NULL, 10);
118
119 /* grab the average incoming transfer rate */
120 temp_buffer = strtok (NULL, " ");
121 average_incoming_rate = strtoul (temp_buffer, NULL, 10);
122
123 /* grab the average outgoing transfer rate */
124 temp_buffer = strtok (NULL, " ");
125 average_outgoing_rate = strtoul (temp_buffer, NULL, 10);
126
127 /* grab the maximum incoming transfer rate */
128 temp_buffer = strtok (NULL, " ");
129 maximum_incoming_rate = strtoul (temp_buffer, NULL, 10);
130
131 /* grab the maximum outgoing transfer rate */
132 temp_buffer = strtok (NULL, " ");
133 maximum_outgoing_rate = strtoul (temp_buffer, NULL, 10);
134 }
135
136 /* close the log file */
137 fclose (fp);
138
139 /* if we couldn't read enough data, return an unknown error */
140 if (line <= 2)
141 usage ("Unable to process MRTG log file\n");
142
143 /* make sure the MRTG data isn't too old */
144 time (&current_time);
145 if (expire_minutes > 0
146 && (current_time - timestamp) >
147 (expire_minutes * 60)) terminate (STATE_WARNING,
148 "MRTG data has expired (%d minutes old)\n",
149 (int) ((current_time - timestamp) /
150 60));
151
152 /* else check the incoming/outgoing rates */
153 if (use_average == TRUE) {
154 incoming_rate = average_incoming_rate;
155 outgoing_rate = average_outgoing_rate;
156 }
157 else {
158 incoming_rate = maximum_incoming_rate;
159 outgoing_rate = maximum_outgoing_rate;
160 }
161
162 /* report incoming traffic in Bytes/sec */
163 if (incoming_rate < 1024) {
164 strcpy (incoming_speed_rating, "B/s");
165 adjusted_incoming_rate = (double) incoming_rate;
166 }
167
168 /* report incoming traffic in KBytes/sec */
169 else if (incoming_rate < (1024 * 1024)) {
170 strcpy (incoming_speed_rating, "KB/s");
171 adjusted_incoming_rate = (double) (incoming_rate / 1024.0);
172 }
173
174 /* report incoming traffic in MBytes/sec */
175 else {
176 strcpy (incoming_speed_rating, "MB/s");
177 adjusted_incoming_rate = (double) (incoming_rate / 1024.0 / 1024.0);
178 }
179
180 /* report outgoing traffic in Bytes/sec */
181 if (outgoing_rate < 1024) {
182 strcpy (outgoing_speed_rating, "B/s");
183 adjusted_outgoing_rate = (double) outgoing_rate;
184 }
185
186 /* report outgoing traffic in KBytes/sec */
187 else if (outgoing_rate < (1024 * 1024)) {
188 strcpy (outgoing_speed_rating, "KB/s");
189 adjusted_outgoing_rate = (double) (outgoing_rate / 1024.0);
190 }
191
192 /* report outgoing traffic in MBytes/sec */
193 else {
194 strcpy (outgoing_speed_rating, "MB/s");
195 adjusted_outgoing_rate = (double) (outgoing_rate / 1024.0 / 1024.0);
196 }
197
198 if (incoming_rate > incoming_critical_threshold
199 || outgoing_rate > outgoing_critical_threshold) {
200 result = STATE_CRITICAL;
201 sprintf (error_message, "%s. In = %0.1f %s, %s. Out = %0.1f %s",
202 (use_average == TRUE) ? "Ave" : "Max", adjusted_incoming_rate,
203 incoming_speed_rating, (use_average == TRUE) ? "Ave" : "Max",
204 adjusted_outgoing_rate, outgoing_speed_rating);
205 }
206 else if (incoming_rate > incoming_warning_threshold
207 || outgoing_rate > outgoing_warning_threshold) {
208 result = STATE_WARNING;
209 sprintf (error_message, "%s. In = %0.1f %s, %s. Out = %0.1f %s",
210 (use_average == TRUE) ? "Ave" : "Max", adjusted_incoming_rate,
211 incoming_speed_rating, (use_average == TRUE) ? "Ave" : "Max",
212 adjusted_outgoing_rate, outgoing_speed_rating);
213 }
214
215 if (result == STATE_OK)
216 printf ("Traffic ok - %s. In = %0.1f %s, %s. Out = %0.1f %s\n",
217 (use_average == TRUE) ? "Ave" : "Max", adjusted_incoming_rate,
218 incoming_speed_rating, (use_average == TRUE) ? "Ave" : "Max",
219 adjusted_outgoing_rate, outgoing_speed_rating);
220 else
221 printf ("%s\n", error_message);
222
223 return result;
224}
225
226
227
228
229
230/* process command-line arguments */
231int
232process_arguments (int argc, char **argv)
233{
234 int c;
235
236 if (argc < 2)
237 return ERROR;
238
239 for (c = 1; c < argc; c++) {
240 if (strcmp ("-to", argv[c]) == 0)
241 strcpy (argv[c], "-t");
242 else if (strcmp ("-wt", argv[c]) == 0)
243 strcpy (argv[c], "-w");
244 else if (strcmp ("-ct", argv[c]) == 0)
245 strcpy (argv[c], "-c");
246 }
247
248
249
250 c = 0;
251 while ((c += (call_getopt (argc - c, &argv[c]))) < argc) {
252
253 if (is_option (argv[c]))
254 continue;
255
256 if (log_file == NULL) {
257 log_file = argv[c];
258 }
259 else if (expire_minutes == -1) {
260 expire_minutes = atoi (optarg);
261 }
262 else if (strcmp (argv[c], "MAX") == 0) {
263 use_average = FALSE;
264 }
265 else if (strcmp (argv[c], "AVG") == 0) {
266 use_average = TRUE;
267 }
268 else if (incoming_warning_threshold == 0) {
269 incoming_warning_threshold = strtoul (argv[c], NULL, 10);
270 }
271 else if (incoming_critical_threshold == 0) {
272 incoming_critical_threshold = strtoul (argv[c], NULL, 10);
273 }
274 else if (outgoing_warning_threshold == 0) {
275 outgoing_warning_threshold = strtoul (argv[c], NULL, 10);
276 }
277 else if (outgoing_critical_threshold == 0) {
278 outgoing_critical_threshold = strtoul (argv[c], NULL, 10);
279 }
280 }
281
282 return validate_arguments ();
283}
284
285
286
287
288
289
290int
291call_getopt (int argc, char **argv)
292{
293 int c, i = 0;
294
295#ifdef HAVE_GETOPT_H
296 int option_index = 0;
297 static struct option long_options[] = {
298 {"logfile", required_argument, 0, 'F'},
299 {"expires", required_argument, 0, 'e'},
300 {"aggregation", required_argument, 0, 'a'},
301 {"variable", required_argument, 0, 'v'},
302 {"critical", required_argument, 0, 'c'},
303 {"warning", required_argument, 0, 'w'},
304 {"verbose", no_argument, 0, 'v'},
305 {"version", no_argument, 0, 'V'},
306 {"help", no_argument, 0, 'h'},
307 {0, 0, 0, 0}
308 };
309#endif
310
311 while (1) {
312#ifdef HAVE_GETOPT_H
313 c =
314 getopt_long (argc, argv, "+hVF:e:a:c:w:", long_options, &option_index);
315#else
316 c = getopt (argc, argv, "+hVF:e:a:c:w:");
317#endif
318
319 i++;
320
321 if (c == -1 || c == EOF || c == 1)
322 break;
323
324 switch (c) {
325 case 'F':
326 case 'e':
327 case 'a':
328 case 'c':
329 case 'w':
330 i++;
331 }
332
333 switch (c) {
334 case 'F': /* input file */
335 log_file = optarg;
336 break;
337 case 'e': /* expiration time */
338 expire_minutes = atoi (optarg);
339 break;
340 case 'a': /* aggregation (AVE or MAX) */
341 if (!strcmp (optarg, "MAX"))
342 use_average = FALSE;
343 else
344 use_average = TRUE;
345 break;
346 case 'c': /* warning threshold */
347 sscanf (optarg, "%lu,%lu", &incoming_critical_threshold,
348 &outgoing_critical_threshold);
349 break;
350 case 'w': /* critical threshold */
351 sscanf (optarg, "%lu,%lu", &incoming_warning_threshold,
352 &outgoing_warning_threshold);
353 break;
354 case 'V': /* version */
355 print_revision (PROGNAME, "$Revision$");
356 exit (STATE_OK);
357 case 'h': /* help */
358 print_help ();
359 exit (STATE_OK);
360 case '?': /* help */
361 usage ("Invalid argument\n");
362 }
363 }
364 return i;
365}
366
367
368
369
370
371int
372validate_arguments (void)
373{
374 return OK;
375}
376
377
378
379
380
381void
382print_help (void)
383{
384 print_revision (PROGNAME, "$Revision$");
385 printf
386 ("Copyright (c) 2000 Tom Shields/Karl DeBisschop\n\n"
387 "This plugin tests the UPS service on the specified host.\n\n");
388 print_usage ();
389 printf
390 ("\nOptions:\n"
391 " -F, --filename=STRING\n"
392 " File to read log from\n"
393 " -e, --expires=INTEGER\n"
394 " Minutes after which log expires\n"
395 " -a, --aggregation=(AVG|MAX)\n"
396 " Test average or maximum"
397 " -w, --warning\n"
398 " Warning threshold pair \"<incoming>,<outgoing>\"\n"
399 " -c, --critical\n"
400 " Critical threshold pair \"<incoming>,<outgoing>\"\n"
401 " -h, --help\n"
402 " Print detailed help screen\n"
403 " -V, --version\n" " Print version information\n\n");
404 support ();
405}
406
407
408
409
410
411void
412print_usage (void)
413{
414 printf
415 ("Usage: %s -F <log_file> -a <AVG | MAX> -v <variable> -w <warning_pair> -c <critical_pair>\n"
416 " [-e expire_minutes] [-t timeout] [-v]\n"
417 " %s --help\n"
418 " %s --version\n", PROGNAME, PROGNAME, PROGNAME);
419}