1 /*****************************************************************************
2 *
3 * Nagios check_mrtgtraf plugin
4 *
5 * License: GPL
6 * Copyright (c) 1999-2007 Nagios Plugins Development 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 * This program is free software: you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation, either version 3 of the License, or
21 * (at your option) any later version.
22 *
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
27 *
28 * You should have received a copy of the GNU General Public License
29 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 *
31 * $Id$
32 *
33 *****************************************************************************/
35 #include "common.h"
36 #include "utils.h"
38 const char *progname = "check_mrtgtraf";
39 const char *revision = "$Revision$";
40 const char *copyright = "1999-2007";
41 const char *email = "nagiosplug-devel@lists.sourceforge.net";
43 int process_arguments (int, char **);
44 int validate_arguments (void);
45 void print_help(void);
46 void print_usage(void);
48 char *log_file = NULL;
49 int expire_minutes = -1;
50 int use_average = TRUE;
51 unsigned long incoming_warning_threshold = 0L;
52 unsigned long incoming_critical_threshold = 0L;
53 unsigned long outgoing_warning_threshold = 0L;
54 unsigned long outgoing_critical_threshold = 0L;
57 int
58 main (int argc, char **argv)
59 {
60 int result = STATE_OK;
61 FILE *fp;
62 int line;
63 char input_buffer[MAX_INPUT_BUFFER];
64 char *temp_buffer;
65 time_t current_time;
66 char *error_message;
67 time_t timestamp = 0L;
68 unsigned long average_incoming_rate = 0L;
69 unsigned long average_outgoing_rate = 0L;
70 unsigned long maximum_incoming_rate = 0L;
71 unsigned long maximum_outgoing_rate = 0L;
72 unsigned long incoming_rate = 0L;
73 unsigned long outgoing_rate = 0L;
74 double adjusted_incoming_rate = 0.0;
75 double adjusted_outgoing_rate = 0.0;
76 char incoming_speed_rating[8];
77 char outgoing_speed_rating[8];
79 setlocale (LC_ALL, "");
80 bindtextdomain (PACKAGE, LOCALEDIR);
81 textdomain (PACKAGE);
83 if (process_arguments (argc, argv) == ERROR)
84 usage4 (_("Could not parse arguments"));
86 /* open the MRTG log file for reading */
87 fp = fopen (log_file, "r");
88 if (fp == NULL)
89 usage4 (_("Unable to open MRTG log file"));
91 line = 0;
92 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, fp)) {
94 line++;
96 /* skip the first line of the log file */
97 if (line == 1)
98 continue;
100 /* break out of read loop */
101 /* if we've passed the number of entries we want to read */
102 if (line > 2)
103 break;
105 /* grab the timestamp */
106 temp_buffer = strtok (input_buffer, " ");
107 timestamp = strtoul (temp_buffer, NULL, 10);
109 /* grab the average incoming transfer rate */
110 temp_buffer = strtok (NULL, " ");
111 average_incoming_rate = strtoul (temp_buffer, NULL, 10);
113 /* grab the average outgoing transfer rate */
114 temp_buffer = strtok (NULL, " ");
115 average_outgoing_rate = strtoul (temp_buffer, NULL, 10);
117 /* grab the maximum incoming transfer rate */
118 temp_buffer = strtok (NULL, " ");
119 maximum_incoming_rate = strtoul (temp_buffer, NULL, 10);
121 /* grab the maximum outgoing transfer rate */
122 temp_buffer = strtok (NULL, " ");
123 maximum_outgoing_rate = strtoul (temp_buffer, NULL, 10);
124 }
126 /* close the log file */
127 fclose (fp);
129 /* if we couldn't read enough data, return an unknown error */
130 if (line <= 2)
131 usage4 (_("Unable to process MRTG log file"));
133 /* make sure the MRTG data isn't too old */
134 time (¤t_time);
135 if ((expire_minutes > 0) &&
136 (current_time - timestamp) > (expire_minutes * 60))
137 die (STATE_WARNING, _("MRTG data has expired (%d minutes old)\n"),
138 (int) ((current_time - timestamp) / 60));
140 /* else check the incoming/outgoing rates */
141 if (use_average == TRUE) {
142 incoming_rate = average_incoming_rate;
143 outgoing_rate = average_outgoing_rate;
144 }
145 else {
146 incoming_rate = maximum_incoming_rate;
147 outgoing_rate = maximum_outgoing_rate;
148 }
150 /* report incoming traffic in Bytes/sec */
151 if (incoming_rate < 1024) {
152 strcpy (incoming_speed_rating, "B/s");
153 adjusted_incoming_rate = (double) incoming_rate;
154 }
156 /* report incoming traffic in KBytes/sec */
157 else if (incoming_rate < (1024 * 1024)) {
158 strcpy (incoming_speed_rating, "KB/s");
159 adjusted_incoming_rate = (double) (incoming_rate / 1024.0);
160 }
162 /* report incoming traffic in MBytes/sec */
163 else {
164 strcpy (incoming_speed_rating, "MB/s");
165 adjusted_incoming_rate = (double) (incoming_rate / 1024.0 / 1024.0);
166 }
168 /* report outgoing traffic in Bytes/sec */
169 if (outgoing_rate < 1024) {
170 strcpy (outgoing_speed_rating, "B/s");
171 adjusted_outgoing_rate = (double) outgoing_rate;
172 }
174 /* report outgoing traffic in KBytes/sec */
175 else if (outgoing_rate < (1024 * 1024)) {
176 strcpy (outgoing_speed_rating, "KB/s");
177 adjusted_outgoing_rate = (double) (outgoing_rate / 1024.0);
178 }
180 /* report outgoing traffic in MBytes/sec */
181 else {
182 strcpy (outgoing_speed_rating, "MB/s");
183 adjusted_outgoing_rate = (double) (outgoing_rate / 1024.0 / 1024.0);
184 }
186 if (incoming_rate > incoming_critical_threshold
187 || outgoing_rate > outgoing_critical_threshold) {
188 result = STATE_CRITICAL;
189 }
190 else if (incoming_rate > incoming_warning_threshold
191 || outgoing_rate > outgoing_warning_threshold) {
192 result = STATE_WARNING;
193 }
195 asprintf (&error_message, _("%s. In = %0.1f %s, %s. Out = %0.1f %s|%s %s\n"),
196 (use_average == TRUE) ? _("Avg") : _("Max"), adjusted_incoming_rate,
197 incoming_speed_rating, (use_average == TRUE) ? _("Avg") : _("Max"),
198 adjusted_outgoing_rate, outgoing_speed_rating,
199 fperfdata("in", adjusted_incoming_rate, incoming_speed_rating,
200 (int)incoming_warning_threshold, incoming_warning_threshold,
201 (int)incoming_critical_threshold, incoming_critical_threshold,
202 TRUE, 0, FALSE, 0),
203 fperfdata("in", adjusted_outgoing_rate, outgoing_speed_rating,
204 (int)outgoing_warning_threshold, outgoing_warning_threshold,
205 (int)outgoing_critical_threshold, outgoing_critical_threshold,
206 TRUE, 0, FALSE, 0));
208 printf (_("Traffic %s - %s\n"), state_text(result), error_message);
210 return result;
211 }
215 /* process command-line arguments */
216 int
217 process_arguments (int argc, char **argv)
218 {
219 int c;
221 int option = 0;
222 static struct option longopts[] = {
223 {"logfile", required_argument, 0, 'F'},
224 {"expires", required_argument, 0, 'e'},
225 {"aggregation", required_argument, 0, 'a'},
226 {"variable", required_argument, 0, 'v'},
227 {"critical", required_argument, 0, 'c'},
228 {"warning", required_argument, 0, 'w'},
229 {"verbose", no_argument, 0, 'v'},
230 {"version", no_argument, 0, 'V'},
231 {"help", no_argument, 0, 'h'},
232 {0, 0, 0, 0}
233 };
235 if (argc < 2)
236 return ERROR;
238 for (c = 1; c < argc; c++) {
239 if (strcmp ("-to", argv[c]) == 0)
240 strcpy (argv[c], "-t");
241 else if (strcmp ("-wt", argv[c]) == 0)
242 strcpy (argv[c], "-w");
243 else if (strcmp ("-ct", argv[c]) == 0)
244 strcpy (argv[c], "-c");
245 }
247 while (1) {
248 c = getopt_long (argc, argv, "hVF:e:a:c:w:", longopts, &option);
250 if (c == -1 || c == EOF)
251 break;
253 switch (c) {
254 case 'F': /* input file */
255 log_file = optarg;
256 break;
257 case 'e': /* expiration time */
258 expire_minutes = atoi (optarg);
259 break;
260 case 'a': /* aggregation (AVE or MAX) */
261 if (!strcmp (optarg, "MAX"))
262 use_average = FALSE;
263 else
264 use_average = TRUE;
265 break;
266 case 'c': /* warning threshold */
267 sscanf (optarg, "%lu,%lu", &incoming_critical_threshold,
268 &outgoing_critical_threshold);
269 break;
270 case 'w': /* critical threshold */
271 sscanf (optarg, "%lu,%lu", &incoming_warning_threshold,
272 &outgoing_warning_threshold);
273 break;
274 case 'V': /* version */
275 print_revision (progname, revision);
276 exit (STATE_OK);
277 case 'h': /* help */
278 print_help ();
279 exit (STATE_OK);
280 case '?': /* help */
281 usage5 ();
282 }
283 }
285 c = optind;
286 if (argc > c && log_file == NULL) {
287 log_file = argv[c++];
288 }
290 if (argc > c && expire_minutes == -1) {
291 expire_minutes = atoi (argv[c++]);
292 }
294 if (argc > c && strcmp (argv[c], "MAX") == 0) {
295 use_average = FALSE;
296 c++;
297 }
298 else if (argc > c && strcmp (argv[c], "AVG") == 0) {
299 use_average = TRUE;
300 c++;
301 }
303 if (argc > c && incoming_warning_threshold == 0) {
304 incoming_warning_threshold = strtoul (argv[c++], NULL, 10);
305 }
307 if (argc > c && incoming_critical_threshold == 0) {
308 incoming_critical_threshold = strtoul (argv[c++], NULL, 10);
309 }
311 if (argc > c && outgoing_warning_threshold == 0) {
312 outgoing_warning_threshold = strtoul (argv[c++], NULL, 10);
313 }
315 if (argc > c && outgoing_critical_threshold == 0) {
316 outgoing_critical_threshold = strtoul (argv[c++], NULL, 10);
317 }
319 return validate_arguments ();
320 }
323 int
324 validate_arguments (void)
325 {
326 return OK;
327 }
330 void
331 print_help (void)
332 {
333 print_revision (progname, revision);
335 printf ("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n");
336 printf (COPYRIGHT, copyright, email);
338 printf ("%s\n", _("This plugin will check the incoming/outgoing transfer rates of a router,"));
339 printf ("%s\n", _("switch, etc recorded in an MRTG log. If the newest log entry is older"));
340 printf ("%s\n", _("than <expire_minutes>, a WARNING status is returned. If either the"));
341 printf ("%s\n", _("incoming or outgoing rates exceed the <icl> or <ocl> thresholds (in"));
342 printf ("%s\n", _("Bytes/sec), a CRITICAL status results. If either of the rates exceed"));
343 printf ("%s\n", _("the <iwl> or <owl> thresholds (in Bytes/sec), a WARNING status results."));
345 printf ("\n\n");
347 print_usage ();
349 printf (_(UT_HELP_VRSN));
351 printf (" %s\n", "-F, --filename=STRING");
352 printf (" %s\n", _("File to read log from"));
353 printf (" %s\n", "-e, --expires=INTEGER");
354 printf (" %s\n", _("Minutes after which log expires"));
355 printf (" %s\n", "-a, --aggregation=(AVG|MAX)");
356 printf (" %s\n", _("Test average or maximum"));
357 printf (" %s\n", "-w, --warning");
358 printf (" %s\n", _("Warning threshold pair <incoming>,<outgoing>"));
359 printf (" %s\n", "-c, --critical");
360 printf (" %s\n", _("Critical threshold pair <incoming>,<outgoing>"));
362 printf ("\n");
363 printf ("%s\n", _("Notes:"));
364 printf (" %s\n", _("- MRTG stands for Multi Router Traffic Grapher. It can be downloaded from"));
365 printf (" %s\n", " http://ee-staff.ethz.ch/~oetiker/webtools/mrtg/mrtg.html");
366 printf (" %s\n", _("- While MRTG can monitor things other than traffic rates, this"));
367 printf (" %s\n", _(" plugin probably won't work with much else without modification."));
368 printf (" %s\n", _("- The calculated i/o rates are a little off from what MRTG actually"));
369 printf (" %s\n", _(" reports. I'm not sure why this is right now, but will look into it"));
370 printf (" %s\n", _(" for future enhancements of this plugin."));
372 printf (_(UT_SUPPORT));
373 }
377 void
378 print_usage (void)
379 {
380 printf (_("Usage"));
381 printf (" %s -F <log_file> -a <AVG | MAX> -v <variable> -w <warning_pair>",progname);
382 printf ("-c <critical_pair> [-e expire_minutes] [-t timeout] [-v]\n");
383 }