Code

The "-e" option now accepts a comma-delimited list of expected status
[nagiosplug.git] / plugins / check_mrtgtraf.c
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         /* Parse extra opts if any */
84         argv=np_extra_opts (&argc, argv, progname);
86         if (process_arguments (argc, argv) == ERROR)
87                 usage4 (_("Could not parse arguments"));
89         /* open the MRTG log file for reading */
90         fp = fopen (log_file, "r");
91         if (fp == NULL)
92                 usage4 (_("Unable to open MRTG log file"));
94         line = 0;
95         while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, fp)) {
97                 line++;
99                 /* skip the first line of the log file */
100                 if (line == 1)
101                         continue;
103                 /* break out of read loop */
104                 /* if we've passed the number of entries we want to read */
105                 if (line > 2)
106                         break;
108                 /* grab the timestamp */
109                 temp_buffer = strtok (input_buffer, " ");
110                 timestamp = strtoul (temp_buffer, NULL, 10);
112                 /* grab the average incoming transfer rate */
113                 temp_buffer = strtok (NULL, " ");
114                 average_incoming_rate = strtoul (temp_buffer, NULL, 10);
116                 /* grab the average outgoing transfer rate */
117                 temp_buffer = strtok (NULL, " ");
118                 average_outgoing_rate = strtoul (temp_buffer, NULL, 10);
120                 /* grab the maximum incoming transfer rate */
121                 temp_buffer = strtok (NULL, " ");
122                 maximum_incoming_rate = strtoul (temp_buffer, NULL, 10);
124                 /* grab the maximum outgoing transfer rate */
125                 temp_buffer = strtok (NULL, " ");
126                 maximum_outgoing_rate = strtoul (temp_buffer, NULL, 10);
127         }
129         /* close the log file */
130         fclose (fp);
132         /* if we couldn't read enough data, return an unknown error */
133         if (line <= 2)
134                 usage4 (_("Unable to process MRTG log file"));
136         /* make sure the MRTG data isn't too old */
137         time (&current_time);
138         if ((expire_minutes > 0) &&
139             (current_time - timestamp) > (expire_minutes * 60))
140                 die (STATE_WARNING,     _("MRTG data has expired (%d minutes old)\n"),
141                      (int) ((current_time - timestamp) / 60));
143         /* else check the incoming/outgoing rates */
144         if (use_average == TRUE) {
145                 incoming_rate = average_incoming_rate;
146                 outgoing_rate = average_outgoing_rate;
147         }
148         else {
149                 incoming_rate = maximum_incoming_rate;
150                 outgoing_rate = maximum_outgoing_rate;
151         }
153         /* report incoming traffic in Bytes/sec */
154         if (incoming_rate < 1024) {
155                 strcpy (incoming_speed_rating, "B/s");
156                 adjusted_incoming_rate = (double) incoming_rate;
157         }
159         /* report incoming traffic in KBytes/sec */
160         else if (incoming_rate < (1024 * 1024)) {
161                 strcpy (incoming_speed_rating, "KB/s");
162                 adjusted_incoming_rate = (double) (incoming_rate / 1024.0);
163         }
165         /* report incoming traffic in MBytes/sec */
166         else {
167                 strcpy (incoming_speed_rating, "MB/s");
168                 adjusted_incoming_rate = (double) (incoming_rate / 1024.0 / 1024.0);
169         }
171         /* report outgoing traffic in Bytes/sec */
172         if (outgoing_rate < 1024) {
173                 strcpy (outgoing_speed_rating, "B/s");
174                 adjusted_outgoing_rate = (double) outgoing_rate;
175         }
177         /* report outgoing traffic in KBytes/sec */
178         else if (outgoing_rate < (1024 * 1024)) {
179                 strcpy (outgoing_speed_rating, "KB/s");
180                 adjusted_outgoing_rate = (double) (outgoing_rate / 1024.0);
181         }
183         /* report outgoing traffic in MBytes/sec */
184         else {
185                 strcpy (outgoing_speed_rating, "MB/s");
186                 adjusted_outgoing_rate = (double) (outgoing_rate / 1024.0 / 1024.0);
187         }
189         if (incoming_rate > incoming_critical_threshold
190                         || outgoing_rate > outgoing_critical_threshold) {
191                 result = STATE_CRITICAL;
192         }
193         else if (incoming_rate > incoming_warning_threshold
194                                          || outgoing_rate > outgoing_warning_threshold) {
195                 result = STATE_WARNING;
196         }
198         asprintf (&error_message, _("%s. In = %0.1f %s, %s. Out = %0.1f %s|%s %s\n"),
199                   (use_average == TRUE) ? _("Avg") : _("Max"), adjusted_incoming_rate,
200                   incoming_speed_rating, (use_average == TRUE) ? _("Avg") : _("Max"),
201                   adjusted_outgoing_rate, outgoing_speed_rating,
202                   fperfdata("in", adjusted_incoming_rate, incoming_speed_rating,
203                            (int)incoming_warning_threshold, incoming_warning_threshold,
204                            (int)incoming_critical_threshold, incoming_critical_threshold,
205                            TRUE, 0, FALSE, 0),
206                   fperfdata("in", adjusted_outgoing_rate, outgoing_speed_rating,
207                            (int)outgoing_warning_threshold, outgoing_warning_threshold,
208                            (int)outgoing_critical_threshold, outgoing_critical_threshold,
209                            TRUE, 0, FALSE, 0));
211         printf (_("Traffic %s - %s\n"), state_text(result), error_message);
213         return result;
218 /* process command-line arguments */
219 int
220 process_arguments (int argc, char **argv)
222         int c;
224         int option = 0;
225         static struct option longopts[] = {
226                 {"logfile", required_argument, 0, 'F'},
227                 {"expires", required_argument, 0, 'e'},
228                 {"aggregation", required_argument, 0, 'a'},
229                 {"variable", required_argument, 0, 'v'},
230                 {"critical", required_argument, 0, 'c'},
231                 {"warning", required_argument, 0, 'w'},
232                 {"verbose", no_argument, 0, 'v'},
233                 {"version", no_argument, 0, 'V'},
234                 {"help", no_argument, 0, 'h'},
235                 {0, 0, 0, 0}
236         };
238         if (argc < 2)
239                 return ERROR;
241         for (c = 1; c < argc; c++) {
242                 if (strcmp ("-to", argv[c]) == 0)
243                         strcpy (argv[c], "-t");
244                 else if (strcmp ("-wt", argv[c]) == 0)
245                         strcpy (argv[c], "-w");
246                 else if (strcmp ("-ct", argv[c]) == 0)
247                         strcpy (argv[c], "-c");
248         }
250         while (1) {
251                 c = getopt_long (argc, argv, "hVF:e:a:c:w:", longopts, &option);
253                 if (c == -1 || c == EOF)
254                         break;
256                 switch (c) {
257                 case 'F':                                                                       /* input file */
258                         log_file = optarg;
259                         break;
260                 case 'e':                                                                       /* expiration time */
261                         expire_minutes = atoi (optarg);
262                         break;
263                 case 'a':                                                                       /* aggregation (AVE or MAX) */
264                         if (!strcmp (optarg, "MAX"))
265                                 use_average = FALSE;
266                         else
267                                 use_average = TRUE;
268                         break;
269                 case 'c':                                                                       /* warning threshold */
270                         sscanf (optarg, "%lu,%lu", &incoming_critical_threshold,
271                                                         &outgoing_critical_threshold);
272                         break;
273                 case 'w':                                                                       /* critical threshold */
274                         sscanf (optarg, "%lu,%lu", &incoming_warning_threshold,
275                                                         &outgoing_warning_threshold);
276                         break;
277                 case 'V':                                                                       /* version */
278                         print_revision (progname, revision);
279                         exit (STATE_OK);
280                 case 'h':                                                                       /* help */
281                         print_help ();
282                         exit (STATE_OK);
283                 case '?':                                                                       /* help */
284                         usage5 ();
285                 }
286         }
288         c = optind;
289         if (argc > c && log_file == NULL) {
290                 log_file = argv[c++];
291         }
293         if (argc > c && expire_minutes == -1) {
294                 expire_minutes = atoi (argv[c++]);
295         }
297         if (argc > c && strcmp (argv[c], "MAX") == 0) {
298                 use_average = FALSE;
299                 c++;
300         }
301         else if (argc > c && strcmp (argv[c], "AVG") == 0) {
302                 use_average = TRUE;
303                 c++;
304         }
306         if (argc > c && incoming_warning_threshold == 0) {
307                 incoming_warning_threshold = strtoul (argv[c++], NULL, 10);
308         }
310         if (argc > c && incoming_critical_threshold == 0) {
311                 incoming_critical_threshold = strtoul (argv[c++], NULL, 10);
312         }
314         if (argc > c && outgoing_warning_threshold == 0) {
315                 outgoing_warning_threshold = strtoul (argv[c++], NULL, 10);
316         }
317         
318         if (argc > c && outgoing_critical_threshold == 0) {
319                 outgoing_critical_threshold = strtoul (argv[c++], NULL, 10);
320         }
322         return validate_arguments ();
326 int
327 validate_arguments (void)
329         return OK;
333 void
334 print_help (void)
336         print_revision (progname, revision);
338         printf ("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n");
339         printf (COPYRIGHT, copyright, email);
341         printf ("%s\n", _("This plugin will check the incoming/outgoing transfer rates of a router,"));
342   printf ("%s\n", _("switch, etc recorded in an MRTG log.  If the newest log entry is older"));
343   printf ("%s\n", _("than <expire_minutes>, a WARNING status is returned. If either the"));
344   printf ("%s\n", _("incoming or outgoing rates exceed the <icl> or <ocl> thresholds (in"));
345   printf ("%s\n", _("Bytes/sec), a CRITICAL status results.  If either of the rates exceed"));
346   printf ("%s\n", _("the <iwl> or <owl> thresholds (in Bytes/sec), a WARNING status results."));
348   printf ("\n\n");
350         print_usage ();
352         printf (_(UT_HELP_VRSN));
353         printf (_(UT_EXTRA_OPTS));
355         printf (" %s\n", "-F, --filename=STRING");
356   printf ("    %s\n", _("File to read log from"));
357   printf (" %s\n", "-e, --expires=INTEGER");
358   printf ("    %s\n", _("Minutes after which log expires"));
359   printf (" %s\n", "-a, --aggregation=(AVG|MAX)");
360   printf ("    %s\n", _("Test average or maximum"));
361   printf (" %s\n", "-w, --warning");
362   printf ("    %s\n", _("Warning threshold pair <incoming>,<outgoing>"));
363   printf (" %s\n", "-c, --critical");
364   printf ("    %s\n", _("Critical threshold pair <incoming>,<outgoing>"));
366   printf ("\n");
367         printf ("%s\n", _("Notes:"));
368   printf (" %s\n", _("- MRTG stands for Multi Router Traffic Grapher. It can be downloaded from"));
369   printf (" %s\n", "  http://ee-staff.ethz.ch/~oetiker/webtools/mrtg/mrtg.html");
370   printf (" %s\n", _("- While MRTG can monitor things other than traffic rates, this"));
371   printf (" %s\n", _("  plugin probably won't work with much else without modification."));
372   printf (" %s\n", _("- The calculated i/o rates are a little off from what MRTG actually"));
373   printf (" %s\n", _("  reports.  I'm not sure why this is right now, but will look into it"));
374   printf (" %s\n", _("  for future enhancements of this plugin."));
375 #ifdef NP_EXTRA_OPTS
376         printf (" -%s", _(UT_EXTRA_OPTS_NOTES));
377 #endif
379         printf (_(UT_SUPPORT));
384 void
385 print_usage (void)
387         printf (_("Usage"));
388   printf (" %s -F <log_file> -a <AVG | MAX> -v <variable> -w <warning_pair>",progname);
389   printf ("-c <critical_pair> [-e expire_minutes] [-t timeout] [-v]\n");