Code

internationalization fixes and help fixes
[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.
16  
17  $Id$
19 ******************************************************************************/
21 #include "common.h"
22 #include "utils.h"
24 const char *progname = "check_mrtgtraf";
25 const char *revision = "$Revision$";
26 const char *copyright = "1999-2004";
27 const char *email = "nagiosplug-devel@lists.sourceforge.net";
29 int process_arguments (int, char **);
30 int validate_arguments (void);
31 void print_help(void);
32 void print_usage(void);
34 char *log_file = NULL;
35 int expire_minutes = -1;
36 int use_average = TRUE;
37 unsigned long incoming_warning_threshold = 0L;
38 unsigned long incoming_critical_threshold = 0L;
39 unsigned long outgoing_warning_threshold = 0L;
40 unsigned long outgoing_critical_threshold = 0L;
43 int
44 main (int argc, char **argv)
45 {
46         int result = STATE_UNKNOWN;
47         FILE *fp;
48         int line;
49         char input_buffer[MAX_INPUT_BUFFER];
50         char *temp_buffer;
51         time_t current_time;
52         char *error_message;
53         time_t timestamp = 0L;
54         unsigned long average_incoming_rate = 0L;
55         unsigned long average_outgoing_rate = 0L;
56         unsigned long maximum_incoming_rate = 0L;
57         unsigned long maximum_outgoing_rate = 0L;
58         unsigned long incoming_rate = 0L;
59         unsigned long outgoing_rate = 0L;
60         double adjusted_incoming_rate = 0.0;
61         double adjusted_outgoing_rate = 0.0;
62         char incoming_speed_rating[8];
63         char outgoing_speed_rating[8];
65         if (process_arguments (argc, argv) != TRUE)
66                 usage4 (_("Could not parse arguments"));
68         /* open the MRTG log file for reading */
69         fp = fopen (log_file, "r");
70         if (fp == NULL)
71                 usage4 (_("Unable to open MRTG log file"));
73         line = 0;
74         while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, fp)) {
76                 line++;
78                 /* skip the first line of the log file */
79                 if (line == 1)
80                         continue;
82                 /* break out of read loop */
83                 /* if we've passed the number of entries we want to read */
84                 if (line > 2)
85                         break;
87                 /* grab the timestamp */
88                 temp_buffer = strtok (input_buffer, " ");
89                 timestamp = strtoul (temp_buffer, NULL, 10);
91                 /* grab the average incoming transfer rate */
92                 temp_buffer = strtok (NULL, " ");
93                 average_incoming_rate = strtoul (temp_buffer, NULL, 10);
95                 /* grab the average outgoing transfer rate */
96                 temp_buffer = strtok (NULL, " ");
97                 average_outgoing_rate = strtoul (temp_buffer, NULL, 10);
99                 /* grab the maximum incoming transfer rate */
100                 temp_buffer = strtok (NULL, " ");
101                 maximum_incoming_rate = strtoul (temp_buffer, NULL, 10);
103                 /* grab the maximum outgoing transfer rate */
104                 temp_buffer = strtok (NULL, " ");
105                 maximum_outgoing_rate = strtoul (temp_buffer, NULL, 10);
106         }
108         /* close the log file */
109         fclose (fp);
111         /* if we couldn't read enough data, return an unknown error */
112         if (line <= 2)
113                 usage4 (_("Unable to process MRTG log file"));
115         /* make sure the MRTG data isn't too old */
116         time (&current_time);
117         if ((expire_minutes > 0) &&
118             (current_time - timestamp) > (expire_minutes * 60))
119                 die (STATE_WARNING,     _("MRTG data has expired (%d minutes old)\n"),
120                      (int) ((current_time - timestamp) / 60));
122         /* else check the incoming/outgoing rates */
123         if (use_average == TRUE) {
124                 incoming_rate = average_incoming_rate;
125                 outgoing_rate = average_outgoing_rate;
126         }
127         else {
128                 incoming_rate = maximum_incoming_rate;
129                 outgoing_rate = maximum_outgoing_rate;
130         }
132         /* report incoming traffic in Bytes/sec */
133         if (incoming_rate < 1024) {
134                 strcpy (incoming_speed_rating, "B/s");
135                 adjusted_incoming_rate = (double) incoming_rate;
136         }
138         /* report incoming traffic in KBytes/sec */
139         else if (incoming_rate < (1024 * 1024)) {
140                 strcpy (incoming_speed_rating, "KB/s");
141                 adjusted_incoming_rate = (double) (incoming_rate / 1024.0);
142         }
144         /* report incoming traffic in MBytes/sec */
145         else {
146                 strcpy (incoming_speed_rating, "MB/s");
147                 adjusted_incoming_rate = (double) (incoming_rate / 1024.0 / 1024.0);
148         }
150         /* report outgoing traffic in Bytes/sec */
151         if (outgoing_rate < 1024) {
152                 strcpy (outgoing_speed_rating, "B/s");
153                 adjusted_outgoing_rate = (double) outgoing_rate;
154         }
156         /* report outgoing traffic in KBytes/sec */
157         else if (outgoing_rate < (1024 * 1024)) {
158                 strcpy (outgoing_speed_rating, "KB/s");
159                 adjusted_outgoing_rate = (double) (outgoing_rate / 1024.0);
160         }
162         /* report outgoing traffic in MBytes/sec */
163         else {
164                 strcpy (outgoing_speed_rating, "MB/s");
165                 adjusted_outgoing_rate = (double) (outgoing_rate / 1024.0 / 1024.0);
166         }
168         if (incoming_rate > incoming_critical_threshold
169                         || outgoing_rate > outgoing_critical_threshold) {
170                 result = STATE_CRITICAL;
171         }
172         else if (incoming_rate > incoming_warning_threshold
173                                          || outgoing_rate > outgoing_warning_threshold) {
174                 result = STATE_WARNING;
175         }
177         asprintf (&error_message, _("%s. In = %0.1f %s, %s. Out = %0.1f %s|%s %s\n"),
178                   (use_average == TRUE) ? _("Avg") : _("Max"), adjusted_incoming_rate,
179                   incoming_speed_rating, (use_average == TRUE) ? _("Avg") : _("Max"),
180                   adjusted_outgoing_rate, outgoing_speed_rating,
181                   fperfdata("in", adjusted_incoming_rate, incoming_speed_rating,
182                            (int)incoming_warning_threshold, incoming_warning_threshold,
183                            (int)incoming_critical_threshold, incoming_critical_threshold,
184                            TRUE, 0, FALSE, 0),
185                   fperfdata("in", adjusted_outgoing_rate, outgoing_speed_rating,
186                            (int)outgoing_warning_threshold, outgoing_warning_threshold,
187                            (int)outgoing_critical_threshold, outgoing_critical_threshold,
188                            TRUE, 0, FALSE, 0));
190         printf (_("Traffic %s - %s\n"), state_text(result), error_message);
192         return result;
197 /* process command-line arguments */
198 int
199 process_arguments (int argc, char **argv)
201         int c;
203         int option = 0;
204         static struct option longopts[] = {
205                 {"logfile", required_argument, 0, 'F'},
206                 {"expires", required_argument, 0, 'e'},
207                 {"aggregation", required_argument, 0, 'a'},
208                 {"variable", required_argument, 0, 'v'},
209                 {"critical", required_argument, 0, 'c'},
210                 {"warning", required_argument, 0, 'w'},
211                 {"verbose", no_argument, 0, 'v'},
212                 {"version", no_argument, 0, 'V'},
213                 {"help", no_argument, 0, 'h'},
214                 {0, 0, 0, 0}
215         };
217         if (argc < 2)
218                 return ERROR;
220         for (c = 1; c < argc; c++) {
221                 if (strcmp ("-to", argv[c]) == 0)
222                         strcpy (argv[c], "-t");
223                 else if (strcmp ("-wt", argv[c]) == 0)
224                         strcpy (argv[c], "-w");
225                 else if (strcmp ("-ct", argv[c]) == 0)
226                         strcpy (argv[c], "-c");
227         }
229         while (1) {
230                 c = getopt_long (argc, argv, "hVF:e:a:c:w:", longopts, &option);
232                 if (c == -1 || c == EOF)
233                         break;
235                 switch (c) {
236                 case 'F':                                                                       /* input file */
237                         log_file = optarg;
238                         break;
239                 case 'e':                                                                       /* expiration time */
240                         expire_minutes = atoi (optarg);
241                         break;
242                 case 'a':                                                                       /* aggregation (AVE or MAX) */
243                         if (!strcmp (optarg, "MAX"))
244                                 use_average = FALSE;
245                         else
246                                 use_average = TRUE;
247                         break;
248                 case 'c':                                                                       /* warning threshold */
249                         sscanf (optarg, "%lu,%lu", &incoming_critical_threshold,
250                                                         &outgoing_critical_threshold);
251                         break;
252                 case 'w':                                                                       /* critical threshold */
253                         sscanf (optarg, "%lu,%lu", &incoming_warning_threshold,
254                                                         &outgoing_warning_threshold);
255                         break;
256                 case 'V':                                                                       /* version */
257                         print_revision (progname, revision);
258                         exit (STATE_OK);
259                 case 'h':                                                                       /* help */
260                         print_help ();
261                         exit (STATE_OK);
262                 case '?':                                                                       /* help */
263                         printf (_("%s: Unknown argument: %s\n\n"), progname, optarg);
264                         print_usage ();
265                         exit (STATE_UNKNOWN);
266                 }
267         }
269         c = optind;
270         if (argc > c && log_file == NULL) {
271                 log_file = argv[c++];
272         }
274         if (argc > c && expire_minutes == -1) {
275                 expire_minutes = atoi (argv[c++]);
276         }
278         if (argc > c && strcmp (argv[c], "MAX") == 0) {
279                 use_average = FALSE;
280                 c++;
281         }
282         else if (argc > c && strcmp (argv[c], "AVG") == 0) {
283                 use_average = TRUE;
284                 c++;
285         }
287         if (argc > c && incoming_warning_threshold == 0) {
288                 incoming_warning_threshold = strtoul (argv[c++], NULL, 10);
289         }
291         if (argc > c && incoming_critical_threshold == 0) {
292                 incoming_critical_threshold = strtoul (argv[c++], NULL, 10);
293         }
295         if (argc > c && outgoing_warning_threshold == 0) {
296                 outgoing_warning_threshold = strtoul (argv[c++], NULL, 10);
297         }
298         
299         if (argc > c && outgoing_critical_threshold == 0) {
300                 outgoing_critical_threshold = strtoul (argv[c++], NULL, 10);
301         }
303         return validate_arguments ();
308 int
309 validate_arguments (void)
311         return OK;
316 void
317 print_help (void)
319         print_revision (progname, revision);
321         printf ("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n");
322         printf (COPYRIGHT, copyright, email);
324         print_usage ();
326         printf (_(UT_HELP_VRSN));
328         printf (_("\
329  -F, --filename=STRING\n\
330    File to read log from\n\
331  -e, --expires=INTEGER\n\
332    Minutes after which log expires\n\
333  -a, --aggregation=(AVG|MAX)\n\
334    Test average or maximum\n\
335  -w, --warning\n\
336    Warning threshold pair \"<incoming>,<outgoing>\"\n\
337  -c, --critical\n\
338    Critical threshold pair \"<incoming>,<outgoing>\"\n"));
340         printf (_("\n\
341 This plugin will check the incoming/outgoing transfer rates of a router,\n\
342 switch, etc recorded in an MRTG log.  If the newest log entry is older\n\
343 than <expire_minutes>, a WARNING status is returned. If either the\n\
344 incoming or outgoing rates exceed the <icl> or <ocl> thresholds (in\n\
345 Bytes/sec), a CRITICAL status results.  If either of the rates exceed\n\
346 the <iwl> or <owl> thresholds (in Bytes/sec), a WARNING status results.\n\n"));
348         printf (_("Notes:\n\
349 - MRTG stands for Multi Router Traffic Grapher. It can be downloaded from\n\
350   http://ee-staff.ethz.ch/~oetiker/webtools/mrtg/mrtg.html\n\
351 - While MRTG can monitor things other than traffic rates, this\n\
352   plugin probably won't work with much else without modification.\n\
353 - The calculated i/o rates are a little off from what MRTG actually\n\
354   reports.  I'm not sure why this is right now, but will look into it\n\
355   for future enhancements of this plugin.\n"));
357         printf (_(UT_SUPPORT));
363 void
364 print_usage (void)
366         printf ("\
367 Usage: %s -F <log_file> -a <AVG | MAX> -v <variable> -w <warning_pair> -c <critical_pair>\n\
368   [-e expire_minutes] [-t timeout] [-v]\n", progname);
369         
370         printf (UT_HLP_VRS, progname, progname);