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 *****************************************************************************/
51 #include "config.h"
52 #include "common.h"
53 #include "utils.h"
55 const char *progname = "check_mrtgtraf";
57 int process_arguments (int, char **);
58 int validate_arguments (void);
59 void print_help (void);
60 void print_usage (void);
62 char *log_file = NULL;
63 int expire_minutes = -1;
64 int use_average = TRUE;
65 unsigned long incoming_warning_threshold = 0L;
66 unsigned long incoming_critical_threshold = 0L;
67 unsigned long outgoing_warning_threshold = 0L;
68 unsigned long outgoing_critical_threshold = 0L;
70 int
71 main (int argc, char **argv)
72 {
73 int result = STATE_OK;
74 FILE *fp;
75 int line;
76 char input_buffer[MAX_INPUT_BUFFER];
77 char *temp_buffer;
78 time_t current_time;
79 char *error_message;
80 time_t timestamp = 0L;
81 unsigned long average_incoming_rate = 0L;
82 unsigned long average_outgoing_rate = 0L;
83 unsigned long maximum_incoming_rate = 0L;
84 unsigned long maximum_outgoing_rate = 0L;
85 unsigned long incoming_rate = 0L;
86 unsigned long outgoing_rate = 0L;
87 double adjusted_incoming_rate = 0.0;
88 double adjusted_outgoing_rate = 0.0;
89 char incoming_speed_rating[8];
90 char outgoing_speed_rating[8];
92 if (process_arguments (argc, argv) != OK)
93 usage ("Invalid command arguments supplied\n");
95 /* open the MRTG log file for reading */
96 fp = fopen (log_file, "r");
97 if (fp == NULL)
98 usage ("Unable to open MRTG log file\n");
100 line = 0;
101 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, fp)) {
103 line++;
105 /* skip the first line of the log file */
106 if (line == 1)
107 continue;
109 /* break out of read loop */
110 /* if we've passed the number of entries we want to read */
111 if (line > 2)
112 break;
114 /* grab the timestamp */
115 temp_buffer = strtok (input_buffer, " ");
116 timestamp = strtoul (temp_buffer, NULL, 10);
118 /* grab the average incoming transfer rate */
119 temp_buffer = strtok (NULL, " ");
120 average_incoming_rate = strtoul (temp_buffer, NULL, 10);
122 /* grab the average outgoing transfer rate */
123 temp_buffer = strtok (NULL, " ");
124 average_outgoing_rate = strtoul (temp_buffer, NULL, 10);
126 /* grab the maximum incoming transfer rate */
127 temp_buffer = strtok (NULL, " ");
128 maximum_incoming_rate = strtoul (temp_buffer, NULL, 10);
130 /* grab the maximum outgoing transfer rate */
131 temp_buffer = strtok (NULL, " ");
132 maximum_outgoing_rate = strtoul (temp_buffer, NULL, 10);
133 }
135 /* close the log file */
136 fclose (fp);
138 /* if we couldn't read enough data, return an unknown error */
139 if (line <= 2)
140 usage ("Unable to process MRTG log file\n");
142 /* make sure the MRTG data isn't too old */
143 time (¤t_time);
144 if (expire_minutes > 0
145 && (current_time - timestamp) >
146 (expire_minutes * 60)) terminate (STATE_WARNING,
147 "MRTG data has expired (%d minutes old)\n",
148 (int) ((current_time - timestamp) /
149 60));
151 /* else check the incoming/outgoing rates */
152 if (use_average == TRUE) {
153 incoming_rate = average_incoming_rate;
154 outgoing_rate = average_outgoing_rate;
155 }
156 else {
157 incoming_rate = maximum_incoming_rate;
158 outgoing_rate = maximum_outgoing_rate;
159 }
161 /* report incoming traffic in Bytes/sec */
162 if (incoming_rate < 1024) {
163 strcpy (incoming_speed_rating, "B/s");
164 adjusted_incoming_rate = (double) incoming_rate;
165 }
167 /* report incoming traffic in KBytes/sec */
168 else if (incoming_rate < (1024 * 1024)) {
169 strcpy (incoming_speed_rating, "KB/s");
170 adjusted_incoming_rate = (double) (incoming_rate / 1024.0);
171 }
173 /* report incoming traffic in MBytes/sec */
174 else {
175 strcpy (incoming_speed_rating, "MB/s");
176 adjusted_incoming_rate = (double) (incoming_rate / 1024.0 / 1024.0);
177 }
179 /* report outgoing traffic in Bytes/sec */
180 if (outgoing_rate < 1024) {
181 strcpy (outgoing_speed_rating, "B/s");
182 adjusted_outgoing_rate = (double) outgoing_rate;
183 }
185 /* report outgoing traffic in KBytes/sec */
186 else if (outgoing_rate < (1024 * 1024)) {
187 strcpy (outgoing_speed_rating, "KB/s");
188 adjusted_outgoing_rate = (double) (outgoing_rate / 1024.0);
189 }
191 /* report outgoing traffic in MBytes/sec */
192 else {
193 strcpy (outgoing_speed_rating, "MB/s");
194 adjusted_outgoing_rate = (double) (outgoing_rate / 1024.0 / 1024.0);
195 }
197 if (incoming_rate > incoming_critical_threshold
198 || outgoing_rate > outgoing_critical_threshold) {
199 result = STATE_CRITICAL;
200 asprintf (&error_message, "Traffic CRITICAL %s. In = %0.1f %s, %s. Out = %0.1f %s",
201 (use_average == TRUE) ? "Avg" : "Max", adjusted_incoming_rate,
202 incoming_speed_rating, (use_average == TRUE) ? "Avg" : "Max",
203 adjusted_outgoing_rate, outgoing_speed_rating);
204 }
205 else if (incoming_rate > incoming_warning_threshold
206 || outgoing_rate > outgoing_warning_threshold) {
207 result = STATE_WARNING;
208 asprintf (&error_message, "Traffic WARNING %s. In = %0.1f %s, %s. Out = %0.1f %s",
209 (use_average == TRUE) ? "Avg" : "Max", adjusted_incoming_rate,
210 incoming_speed_rating, (use_average == TRUE) ? "Avg" : "Max",
211 adjusted_outgoing_rate, outgoing_speed_rating);
212 }
213 else if (result == STATE_OK)
214 printf ("Traffic OK - %s. In = %0.1f %s, %s. Out = %0.1f %s\n",
215 (use_average == TRUE) ? "Avg" : "Max", adjusted_incoming_rate,
216 incoming_speed_rating, (use_average == TRUE) ? "Avg" : "Max",
217 adjusted_outgoing_rate, outgoing_speed_rating);
218 else
219 printf ("UNKNOWN %s\n", error_message);
221 return result;
222 }
228 /* process command-line arguments */
229 int
230 process_arguments (int argc, char **argv)
231 {
232 int c;
234 #ifdef HAVE_GETOPT_H
235 int option_index = 0;
236 static struct option longopts[] = {
237 {"logfile", required_argument, 0, 'F'},
238 {"expires", required_argument, 0, 'e'},
239 {"aggregation", required_argument, 0, 'a'},
240 {"variable", required_argument, 0, 'v'},
241 {"critical", required_argument, 0, 'c'},
242 {"warning", required_argument, 0, 'w'},
243 {"verbose", no_argument, 0, 'v'},
244 {"version", no_argument, 0, 'V'},
245 {"help", no_argument, 0, 'h'},
246 {0, 0, 0, 0}
247 };
248 #endif
250 if (argc < 2)
251 return ERROR;
253 for (c = 1; c < argc; c++) {
254 if (strcmp ("-to", argv[c]) == 0)
255 strcpy (argv[c], "-t");
256 else if (strcmp ("-wt", argv[c]) == 0)
257 strcpy (argv[c], "-w");
258 else if (strcmp ("-ct", argv[c]) == 0)
259 strcpy (argv[c], "-c");
260 }
262 while (1) {
263 #ifdef HAVE_GETOPT_H
264 c = getopt_long (argc, argv, "hVF:e:a:c:w:", longopts, &option_index);
265 #else
266 c = getopt (argc, argv, "hVF:e:a:c:w:");
267 #endif
269 if (c == -1 || c == EOF)
270 break;
272 switch (c) {
273 case 'F': /* input file */
274 log_file = optarg;
275 break;
276 case 'e': /* expiration time */
277 expire_minutes = atoi (optarg);
278 break;
279 case 'a': /* aggregation (AVE or MAX) */
280 if (!strcmp (optarg, "MAX"))
281 use_average = FALSE;
282 else
283 use_average = TRUE;
284 break;
285 case 'c': /* warning threshold */
286 sscanf (optarg, "%lu,%lu", &incoming_critical_threshold,
287 &outgoing_critical_threshold);
288 break;
289 case 'w': /* critical threshold */
290 sscanf (optarg, "%lu,%lu", &incoming_warning_threshold,
291 &outgoing_warning_threshold);
292 break;
293 case 'V': /* version */
294 print_revision (progname, "$Revision$");
295 exit (STATE_OK);
296 case 'h': /* help */
297 print_help ();
298 exit (STATE_OK);
299 case '?': /* help */
300 usage ("Invalid argument\n");
301 }
302 }
304 c = optind;
305 if (argc > c && log_file == NULL) {
306 log_file = argv[c++];
307 }
309 if (argc > c && expire_minutes == -1) {
310 expire_minutes = atoi (argv[c++]);
311 }
313 if (argc > c && strcmp (argv[c], "MAX") == 0) {
314 use_average = FALSE;
315 c++;
316 }
317 else if (argc > c && strcmp (argv[c], "AVG") == 0) {
318 use_average = TRUE;
319 c++;
320 }
322 if (argc > c && incoming_warning_threshold == 0) {
323 incoming_warning_threshold = strtoul (argv[c++], NULL, 10);
324 }
326 if (argc > c && incoming_critical_threshold == 0) {
327 incoming_critical_threshold = strtoul (argv[c++], NULL, 10);
328 }
330 if (argc > c && outgoing_warning_threshold == 0) {
331 outgoing_warning_threshold = strtoul (argv[c++], NULL, 10);
332 }
334 if (argc > c && outgoing_critical_threshold == 0) {
335 outgoing_critical_threshold = strtoul (argv[c++], NULL, 10);
336 }
338 return validate_arguments ();
339 }
345 int
346 validate_arguments (void)
347 {
348 return OK;
349 }
355 void
356 print_help (void)
357 {
358 print_revision (progname, "$Revision$");
359 printf
360 ("Copyright (c) 2000 Tom Shields/Karl DeBisschop\n\n"
361 "This plugin tests the UPS service on the specified host.\n\n");
362 print_usage ();
363 printf
364 ("\nOptions:\n"
365 " -F, --filename=STRING\n"
366 " File to read log from\n"
367 " -e, --expires=INTEGER\n"
368 " Minutes after which log expires\n"
369 " -a, --aggregation=(AVG|MAX)\n"
370 " Test average or maximum"
371 " -w, --warning\n"
372 " Warning threshold pair \"<incoming>,<outgoing>\"\n"
373 " -c, --critical\n"
374 " Critical threshold pair \"<incoming>,<outgoing>\"\n"
375 " -h, --help\n"
376 " Print detailed help screen\n"
377 " -V, --version\n" " Print version information\n\n");
378 support ();
379 }
385 void
386 print_usage (void)
387 {
388 printf
389 ("Usage: %s -F <log_file> -a <AVG | MAX> -v <variable> -w <warning_pair> -c <critical_pair>\n"
390 " [-e expire_minutes] [-t timeout] [-v]\n"
391 " %s --help\n"
392 " %s --version\n", progname, progname, progname);
393 }