Code

Add comment to recent check_disk patch
[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                 {"filename", required_argument, 0, 'F'},
227                 {"expires", required_argument, 0, 'e'},
228                 {"aggregation", required_argument, 0, 'a'},
229                 {"critical", required_argument, 0, 'c'},
230                 {"warning", required_argument, 0, 'w'},
231                 {"version", no_argument, 0, 'V'},
232                 {"help", no_argument, 0, 'h'},
233                 {0, 0, 0, 0}
234         };
236         if (argc < 2)
237                 return ERROR;
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         }
248         while (1) {
249                 c = getopt_long (argc, argv, "hVF:e:a:c:w:", longopts, &option);
251                 if (c == -1 || c == EOF)
252                         break;
254                 switch (c) {
255                 case 'F':                                                                       /* input file */
256                         log_file = optarg;
257                         break;
258                 case 'e':                                                                       /* expiration time */
259                         expire_minutes = atoi (optarg);
260                         break;
261                 case 'a':                                                                       /* aggregation (AVE or MAX) */
262                         if (!strcmp (optarg, "MAX"))
263                                 use_average = FALSE;
264                         else
265                                 use_average = TRUE;
266                         break;
267                 case 'c':                                                                       /* warning threshold */
268                         sscanf (optarg, "%lu,%lu", &incoming_critical_threshold,
269                                                         &outgoing_critical_threshold);
270                         break;
271                 case 'w':                                                                       /* critical threshold */
272                         sscanf (optarg, "%lu,%lu", &incoming_warning_threshold,
273                                                         &outgoing_warning_threshold);
274                         break;
275                 case 'V':                                                                       /* version */
276                         print_revision (progname, revision);
277                         exit (STATE_OK);
278                 case 'h':                                                                       /* help */
279                         print_help ();
280                         exit (STATE_OK);
281                 case '?':                                                                       /* help */
282                         usage5 ();
283                 }
284         }
286         c = optind;
287         if (argc > c && log_file == NULL) {
288                 log_file = argv[c++];
289         }
291         if (argc > c && expire_minutes == -1) {
292                 expire_minutes = atoi (argv[c++]);
293         }
295         if (argc > c && strcmp (argv[c], "MAX") == 0) {
296                 use_average = FALSE;
297                 c++;
298         }
299         else if (argc > c && strcmp (argv[c], "AVG") == 0) {
300                 use_average = TRUE;
301                 c++;
302         }
304         if (argc > c && incoming_warning_threshold == 0) {
305                 incoming_warning_threshold = strtoul (argv[c++], NULL, 10);
306         }
308         if (argc > c && incoming_critical_threshold == 0) {
309                 incoming_critical_threshold = strtoul (argv[c++], NULL, 10);
310         }
312         if (argc > c && outgoing_warning_threshold == 0) {
313                 outgoing_warning_threshold = strtoul (argv[c++], NULL, 10);
314         }
315         
316         if (argc > c && outgoing_critical_threshold == 0) {
317                 outgoing_critical_threshold = strtoul (argv[c++], NULL, 10);
318         }
320         return validate_arguments ();
324 int
325 validate_arguments (void)
327         return OK;
331 void
332 print_help (void)
334         print_revision (progname, revision);
336         printf ("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n");
337         printf (COPYRIGHT, copyright, email);
339         printf ("%s\n", _("This plugin will check the incoming/outgoing transfer rates of a router,"));
340   printf ("%s\n", _("switch, etc recorded in an MRTG log.  If the newest log entry is older"));
341   printf ("%s\n", _("than <expire_minutes>, a WARNING status is returned. If either the"));
342   printf ("%s\n", _("incoming or outgoing rates exceed the <icl> or <ocl> thresholds (in"));
343   printf ("%s\n", _("Bytes/sec), a CRITICAL status results.  If either of the rates exceed"));
344   printf ("%s\n", _("the <iwl> or <owl> thresholds (in Bytes/sec), a WARNING status results."));
346   printf ("\n\n");
348         print_usage ();
350         printf (_(UT_HELP_VRSN));
351         printf (_(UT_EXTRA_OPTS));
353         printf (" %s\n", "-F, --filename=STRING");
354   printf ("    %s\n", _("File to read log from"));
355   printf (" %s\n", "-e, --expires=INTEGER");
356   printf ("    %s\n", _("Minutes after which log expires"));
357   printf (" %s\n", "-a, --aggregation=(AVG|MAX)");
358   printf ("    %s\n", _("Test average or maximum"));
359   printf (" %s\n", "-w, --warning");
360   printf ("    %s\n", _("Warning threshold pair <incoming>,<outgoing>"));
361   printf (" %s\n", "-c, --critical");
362   printf ("    %s\n", _("Critical threshold pair <incoming>,<outgoing>"));
364   printf ("\n");
365         printf ("%s\n", _("Notes:"));
366   printf (" %s\n", _("- MRTG stands for Multi Router Traffic Grapher. It can be downloaded from"));
367   printf (" %s\n", "  http://ee-staff.ethz.ch/~oetiker/webtools/mrtg/mrtg.html");
368   printf (" %s\n", _("- While MRTG can monitor things other than traffic rates, this"));
369   printf (" %s\n", _("  plugin probably won't work with much else without modification."));
370   printf (" %s\n", _("- The calculated i/o rates are a little off from what MRTG actually"));
371   printf (" %s\n", _("  reports.  I'm not sure why this is right now, but will look into it"));
372   printf (" %s\n", _("  for future enhancements of this plugin."));
373 #ifdef NP_EXTRA_OPTS
374         printf (" -%s", _(UT_EXTRA_OPTS_NOTES));
375 #endif
377         printf (_(UT_SUPPORT));
382 void
383 print_usage (void)
385         printf (_("Usage"));
386   printf (" %s -F <log_file> -a <AVG | MAX> -w <warning_pair>\n",progname);
387   printf ("-c <critical_pair> [-e expire_minutes]\n");