1 /******************************************************************************
2 *
3 * Nagios check_mrtgtraf plugin
4 *
5 * License: GPL
6 * Copyright (c) 1999-2007 nagios-plugins team
7 *
8 * Last Modified: $Date$
9 *
10 * Description:
11 *
12 * This file contains the check_mtrgtraf plugin
13 *
14 * This plugin will check the incoming/outgoing transfer rates of a router
15 * switch, etc recorded in an MRTG log.
16 *
17 *
18 * License Information:
19 *
20 * This program is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation; either version 2 of the License, or
23 * (at your option) any later version.
24 *
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
29 *
30 * You should have received a copy of the GNU General Public License
31 * along with this program; if not, write to the Free Software
32 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
34 $Id$
36 ******************************************************************************/
38 #include "common.h"
39 #include "utils.h"
41 const char *progname = "check_mrtgtraf";
42 const char *revision = "$Revision$";
43 const char *copyright = "1999-2007";
44 const char *email = "nagiosplug-devel@lists.sourceforge.net";
46 int process_arguments (int, char **);
47 int validate_arguments (void);
48 void print_help(void);
49 void print_usage(void);
51 char *log_file = NULL;
52 int expire_minutes = -1;
53 int use_average = TRUE;
54 unsigned long incoming_warning_threshold = 0L;
55 unsigned long incoming_critical_threshold = 0L;
56 unsigned long outgoing_warning_threshold = 0L;
57 unsigned long outgoing_critical_threshold = 0L;
60 int
61 main (int argc, char **argv)
62 {
63 int result = STATE_OK;
64 FILE *fp;
65 int line;
66 char input_buffer[MAX_INPUT_BUFFER];
67 char *temp_buffer;
68 time_t current_time;
69 char *error_message;
70 time_t timestamp = 0L;
71 unsigned long average_incoming_rate = 0L;
72 unsigned long average_outgoing_rate = 0L;
73 unsigned long maximum_incoming_rate = 0L;
74 unsigned long maximum_outgoing_rate = 0L;
75 unsigned long incoming_rate = 0L;
76 unsigned long outgoing_rate = 0L;
77 double adjusted_incoming_rate = 0.0;
78 double adjusted_outgoing_rate = 0.0;
79 char incoming_speed_rating[8];
80 char outgoing_speed_rating[8];
82 if (process_arguments (argc, argv) == ERROR)
83 usage4 (_("Could not parse arguments"));
85 /* open the MRTG log file for reading */
86 fp = fopen (log_file, "r");
87 if (fp == NULL)
88 usage4 (_("Unable to open MRTG log file"));
90 line = 0;
91 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, fp)) {
93 line++;
95 /* skip the first line of the log file */
96 if (line == 1)
97 continue;
99 /* break out of read loop */
100 /* if we've passed the number of entries we want to read */
101 if (line > 2)
102 break;
104 /* grab the timestamp */
105 temp_buffer = strtok (input_buffer, " ");
106 timestamp = strtoul (temp_buffer, NULL, 10);
108 /* grab the average incoming transfer rate */
109 temp_buffer = strtok (NULL, " ");
110 average_incoming_rate = strtoul (temp_buffer, NULL, 10);
112 /* grab the average outgoing transfer rate */
113 temp_buffer = strtok (NULL, " ");
114 average_outgoing_rate = strtoul (temp_buffer, NULL, 10);
116 /* grab the maximum incoming transfer rate */
117 temp_buffer = strtok (NULL, " ");
118 maximum_incoming_rate = strtoul (temp_buffer, NULL, 10);
120 /* grab the maximum outgoing transfer rate */
121 temp_buffer = strtok (NULL, " ");
122 maximum_outgoing_rate = strtoul (temp_buffer, NULL, 10);
123 }
125 /* close the log file */
126 fclose (fp);
128 /* if we couldn't read enough data, return an unknown error */
129 if (line <= 2)
130 usage4 (_("Unable to process MRTG log file"));
132 /* make sure the MRTG data isn't too old */
133 time (¤t_time);
134 if ((expire_minutes > 0) &&
135 (current_time - timestamp) > (expire_minutes * 60))
136 die (STATE_WARNING, _("MRTG data has expired (%d minutes old)\n"),
137 (int) ((current_time - timestamp) / 60));
139 /* else check the incoming/outgoing rates */
140 if (use_average == TRUE) {
141 incoming_rate = average_incoming_rate;
142 outgoing_rate = average_outgoing_rate;
143 }
144 else {
145 incoming_rate = maximum_incoming_rate;
146 outgoing_rate = maximum_outgoing_rate;
147 }
149 /* report incoming traffic in Bytes/sec */
150 if (incoming_rate < 1024) {
151 strcpy (incoming_speed_rating, "B/s");
152 adjusted_incoming_rate = (double) incoming_rate;
153 }
155 /* report incoming traffic in KBytes/sec */
156 else if (incoming_rate < (1024 * 1024)) {
157 strcpy (incoming_speed_rating, "KB/s");
158 adjusted_incoming_rate = (double) (incoming_rate / 1024.0);
159 }
161 /* report incoming traffic in MBytes/sec */
162 else {
163 strcpy (incoming_speed_rating, "MB/s");
164 adjusted_incoming_rate = (double) (incoming_rate / 1024.0 / 1024.0);
165 }
167 /* report outgoing traffic in Bytes/sec */
168 if (outgoing_rate < 1024) {
169 strcpy (outgoing_speed_rating, "B/s");
170 adjusted_outgoing_rate = (double) outgoing_rate;
171 }
173 /* report outgoing traffic in KBytes/sec */
174 else if (outgoing_rate < (1024 * 1024)) {
175 strcpy (outgoing_speed_rating, "KB/s");
176 adjusted_outgoing_rate = (double) (outgoing_rate / 1024.0);
177 }
179 /* report outgoing traffic in MBytes/sec */
180 else {
181 strcpy (outgoing_speed_rating, "MB/s");
182 adjusted_outgoing_rate = (double) (outgoing_rate / 1024.0 / 1024.0);
183 }
185 if (incoming_rate > incoming_critical_threshold
186 || outgoing_rate > outgoing_critical_threshold) {
187 result = STATE_CRITICAL;
188 }
189 else if (incoming_rate > incoming_warning_threshold
190 || outgoing_rate > outgoing_warning_threshold) {
191 result = STATE_WARNING;
192 }
194 asprintf (&error_message, _("%s. In = %0.1f %s, %s. Out = %0.1f %s|%s %s\n"),
195 (use_average == TRUE) ? _("Avg") : _("Max"), adjusted_incoming_rate,
196 incoming_speed_rating, (use_average == TRUE) ? _("Avg") : _("Max"),
197 adjusted_outgoing_rate, outgoing_speed_rating,
198 fperfdata("in", adjusted_incoming_rate, incoming_speed_rating,
199 (int)incoming_warning_threshold, incoming_warning_threshold,
200 (int)incoming_critical_threshold, incoming_critical_threshold,
201 TRUE, 0, FALSE, 0),
202 fperfdata("in", adjusted_outgoing_rate, outgoing_speed_rating,
203 (int)outgoing_warning_threshold, outgoing_warning_threshold,
204 (int)outgoing_critical_threshold, outgoing_critical_threshold,
205 TRUE, 0, FALSE, 0));
207 printf (_("Traffic %s - %s\n"), state_text(result), error_message);
209 return result;
210 }
214 /* process command-line arguments */
215 int
216 process_arguments (int argc, char **argv)
217 {
218 int c;
220 int option = 0;
221 static struct option longopts[] = {
222 {"logfile", required_argument, 0, 'F'},
223 {"expires", required_argument, 0, 'e'},
224 {"aggregation", required_argument, 0, 'a'},
225 {"variable", required_argument, 0, 'v'},
226 {"critical", required_argument, 0, 'c'},
227 {"warning", required_argument, 0, 'w'},
228 {"verbose", no_argument, 0, 'v'},
229 {"version", no_argument, 0, 'V'},
230 {"help", no_argument, 0, 'h'},
231 {0, 0, 0, 0}
232 };
234 if (argc < 2)
235 return ERROR;
237 for (c = 1; c < argc; c++) {
238 if (strcmp ("-to", argv[c]) == 0)
239 strcpy (argv[c], "-t");
240 else if (strcmp ("-wt", argv[c]) == 0)
241 strcpy (argv[c], "-w");
242 else if (strcmp ("-ct", argv[c]) == 0)
243 strcpy (argv[c], "-c");
244 }
246 while (1) {
247 c = getopt_long (argc, argv, "hVF:e:a:c:w:", longopts, &option);
249 if (c == -1 || c == EOF)
250 break;
252 switch (c) {
253 case 'F': /* input file */
254 log_file = optarg;
255 break;
256 case 'e': /* expiration time */
257 expire_minutes = atoi (optarg);
258 break;
259 case 'a': /* aggregation (AVE or MAX) */
260 if (!strcmp (optarg, "MAX"))
261 use_average = FALSE;
262 else
263 use_average = TRUE;
264 break;
265 case 'c': /* warning threshold */
266 sscanf (optarg, "%lu,%lu", &incoming_critical_threshold,
267 &outgoing_critical_threshold);
268 break;
269 case 'w': /* critical threshold */
270 sscanf (optarg, "%lu,%lu", &incoming_warning_threshold,
271 &outgoing_warning_threshold);
272 break;
273 case 'V': /* version */
274 print_revision (progname, revision);
275 exit (STATE_OK);
276 case 'h': /* help */
277 print_help ();
278 exit (STATE_OK);
279 case '?': /* help */
280 usage5 ();
281 }
282 }
284 c = optind;
285 if (argc > c && log_file == NULL) {
286 log_file = argv[c++];
287 }
289 if (argc > c && expire_minutes == -1) {
290 expire_minutes = atoi (argv[c++]);
291 }
293 if (argc > c && strcmp (argv[c], "MAX") == 0) {
294 use_average = FALSE;
295 c++;
296 }
297 else if (argc > c && strcmp (argv[c], "AVG") == 0) {
298 use_average = TRUE;
299 c++;
300 }
302 if (argc > c && incoming_warning_threshold == 0) {
303 incoming_warning_threshold = strtoul (argv[c++], NULL, 10);
304 }
306 if (argc > c && incoming_critical_threshold == 0) {
307 incoming_critical_threshold = strtoul (argv[c++], NULL, 10);
308 }
310 if (argc > c && outgoing_warning_threshold == 0) {
311 outgoing_warning_threshold = strtoul (argv[c++], NULL, 10);
312 }
314 if (argc > c && outgoing_critical_threshold == 0) {
315 outgoing_critical_threshold = strtoul (argv[c++], NULL, 10);
316 }
318 return validate_arguments ();
319 }
322 int
323 validate_arguments (void)
324 {
325 return OK;
326 }
329 void
330 print_help (void)
331 {
332 print_revision (progname, revision);
334 printf ("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n");
335 printf (COPYRIGHT, copyright, email);
337 printf ("%s\n", _("This plugin will check the incoming/outgoing transfer rates of a router,"));
338 printf ("%s\n", _("switch, etc recorded in an MRTG log. If the newest log entry is older"));
339 printf ("%s\n", _("than <expire_minutes>, a WARNING status is returned. If either the"));
340 printf ("%s\n", _("incoming or outgoing rates exceed the <icl> or <ocl> thresholds (in"));
341 printf ("%s\n", _("Bytes/sec), a CRITICAL status results. If either of the rates exceed"));
342 printf ("%s\n", _("the <iwl> or <owl> thresholds (in Bytes/sec), a WARNING status results."));
344 printf ("\n\n");
346 print_usage ();
348 printf (_(UT_HELP_VRSN));
350 printf (" %s\n", "-F, --filename=STRING");
351 printf (" %s\n", _("File to read log from"));
352 printf (" %s\n", "-e, --expires=INTEGER");
353 printf (" %s\n", _("Minutes after which log expires"));
354 printf (" %s\n", "-a, --aggregation=(AVG|MAX)");
355 printf (" %s\n", _("Test average or maximum"));
356 printf (" %s\n", "-w, --warning");
357 printf (" %s\n", _("Warning threshold pair <incoming>,<outgoing>"));
358 printf (" %s\n", "-c, --critical");
359 printf (" %s\n", _("Critical threshold pair <incoming>,<outgoing>"));
361 printf ("%s\n", _("Notes:"));
362 printf (" %s\n", _("- MRTG stands for Multi Router Traffic Grapher. It can be downloaded from"));
363 printf (" %s\n", " http://ee-staff.ethz.ch/~oetiker/webtools/mrtg/mrtg.html");
364 printf (" %s\n", _("- While MRTG can monitor things other than traffic rates, this"));
365 printf (" %s\n", _(" plugin probably won't work with much else without modification."));
366 printf (" %s\n", _("- The calculated i/o rates are a little off from what MRTG actually"));
367 printf (" %s\n", _(" reports. I'm not sure why this is right now, but will look into it"));
368 printf (" %s\n", _(" for future enhancements of this plugin."));
370 printf (_(UT_SUPPORT));
371 }
375 void
376 print_usage (void)
377 {
378 printf (_("Usage"));
379 printf (" %s -F <log_file> -a <AVG | MAX> -v <variable> -w <warning_pair>",progname);
380 printf ("-c <critical_pair> [-e expire_minutes] [-t timeout] [-v]\n");
381 }