Code

remove call_getopt and ssprintf
[nagiosplug.git] / plugins / check_mrtgtraf.c
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 #define 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 (&current_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, "%s. In = %0.1f %s, %s. Out = %0.1f %s",
201                                                  (use_average == TRUE) ? "Ave" : "Max", adjusted_incoming_rate,
202                                                  incoming_speed_rating, (use_average == TRUE) ? "Ave" : "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, "%s. In = %0.1f %s, %s. Out = %0.1f %s",
209                                                  (use_average == TRUE) ? "Ave" : "Max", adjusted_incoming_rate,
210                                                  incoming_speed_rating, (use_average == TRUE) ? "Ave" : "Max",
211                                                  adjusted_outgoing_rate, outgoing_speed_rating);
212         }
214         if (result == STATE_OK)
215                 printf ("Traffic ok - %s. In = %0.1f %s, %s. Out = %0.1f %s\n",
216                                                 (use_average == TRUE) ? "Ave" : "Max", adjusted_incoming_rate,
217                                                 incoming_speed_rating, (use_average == TRUE) ? "Ave" : "Max",
218                                                 adjusted_outgoing_rate, outgoing_speed_rating);
219         else
220                 printf ("%s\n", error_message);
222         return result;
229 /* process command-line arguments */
230 int
231 process_arguments (int argc, char **argv)
233         int c;
235 #ifdef HAVE_GETOPT_H
236         int option_index = 0;
237         static struct option longopts[] = {
238                 {"logfile", required_argument, 0, 'F'},
239                 {"expires", required_argument, 0, 'e'},
240                 {"aggregation", required_argument, 0, 'a'},
241                 {"variable", required_argument, 0, 'v'},
242                 {"critical", required_argument, 0, 'c'},
243                 {"warning", required_argument, 0, 'w'},
244                 {"verbose", no_argument, 0, 'v'},
245                 {"version", no_argument, 0, 'V'},
246                 {"help", no_argument, 0, 'h'},
247                 {0, 0, 0, 0}
248         };
249 #endif
251         if (argc < 2)
252                 return ERROR;
254         for (c = 1; c < argc; c++) {
255                 if (strcmp ("-to", argv[c]) == 0)
256                         strcpy (argv[c], "-t");
257                 else if (strcmp ("-wt", argv[c]) == 0)
258                         strcpy (argv[c], "-w");
259                 else if (strcmp ("-ct", argv[c]) == 0)
260                         strcpy (argv[c], "-c");
261         }
263         while (1) {
264 #ifdef HAVE_GETOPT_H
265                 c =     getopt_long (argc, argv, "hVF:e:a:c:w:", longopts, &option_index);
266 #else
267                 c = getopt (argc, argv, "hVF:e:a:c:w:");
268 #endif
270                 if (c == -1 || c == EOF)
271                         break;
273                 switch (c) {
274                 case 'F':                                                                       /* input file */
275                         log_file = optarg;
276                         break;
277                 case 'e':                                                                       /* expiration time */
278                         expire_minutes = atoi (optarg);
279                         break;
280                 case 'a':                                                                       /* aggregation (AVE or MAX) */
281                         if (!strcmp (optarg, "MAX"))
282                                 use_average = FALSE;
283                         else
284                                 use_average = TRUE;
285                         break;
286                 case 'c':                                                                       /* warning threshold */
287                         sscanf (optarg, "%lu,%lu", &incoming_critical_threshold,
288                                                         &outgoing_critical_threshold);
289                         break;
290                 case 'w':                                                                       /* critical threshold */
291                         sscanf (optarg, "%lu,%lu", &incoming_warning_threshold,
292                                                         &outgoing_warning_threshold);
293                         break;
294                 case 'V':                                                                       /* version */
295                         print_revision (PROGNAME, "$Revision$");
296                         exit (STATE_OK);
297                 case 'h':                                                                       /* help */
298                         print_help ();
299                         exit (STATE_OK);
300                 case '?':                                                                       /* help */
301                         usage ("Invalid argument\n");
302                 }
303         }
305         c = optind;
306         if (argc > c && log_file == NULL) {
307                 log_file = argv[c++];
308         }
310         if (argc > c && expire_minutes == -1) {
311                 expire_minutes = atoi (argv[c++]);
312         }
314         if (argc > c && strcmp (argv[c], "MAX") == 0) {
315                 use_average = FALSE;
316                 c++;
317         }
318         else if (argc > c && strcmp (argv[c], "AVG") == 0) {
319                 use_average = TRUE;
320                 c++;
321         }
323         if (argc > c && incoming_warning_threshold == 0) {
324                 incoming_warning_threshold = strtoul (argv[c++], NULL, 10);
325         }
327         if (argc > c && incoming_critical_threshold == 0) {
328                 incoming_critical_threshold = strtoul (argv[c++], NULL, 10);
329         }
331         if (argc > c && outgoing_warning_threshold == 0) {
332                 outgoing_warning_threshold = strtoul (argv[c++], NULL, 10);
333         }
334         
335         if (argc > c && outgoing_critical_threshold == 0) {
336                 outgoing_critical_threshold = strtoul (argv[c++], NULL, 10);
337         }
339         return validate_arguments ();
346 int
347 validate_arguments (void)
349         return OK;
356 void
357 print_help (void)
359         print_revision (PROGNAME, "$Revision$");
360         printf
361                 ("Copyright (c) 2000 Tom Shields/Karl DeBisschop\n\n"
362                  "This plugin tests the UPS service on the specified host.\n\n");
363         print_usage ();
364         printf
365                 ("\nOptions:\n"
366                  " -F, --filename=STRING\n"
367                  "   File to read log from\n"
368                  " -e, --expires=INTEGER\n"
369                  "   Minutes after which log expires\n"
370                  " -a, --aggregation=(AVG|MAX)\n"
371                  "   Test average or maximum"
372                  " -w, --warning\n"
373                  "   Warning threshold pair \"<incoming>,<outgoing>\"\n"
374                  " -c, --critical\n"
375                  "   Critical threshold pair \"<incoming>,<outgoing>\"\n"
376                  " -h, --help\n"
377                  "   Print detailed help screen\n"
378                  " -V, --version\n" "   Print version information\n\n");
379         support ();
386 void
387 print_usage (void)
389         printf
390                 ("Usage: %s  -F <log_file> -a <AVG | MAX> -v <variable> -w <warning_pair> -c <critical_pair>\n"
391                  "            [-e expire_minutes] [-t timeout] [-v]\n"
392                  "       %s --help\n"
393                  "       %s --version\n", PROGNAME, PROGNAME, PROGNAME);