Code

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