1 /*****************************************************************************
2 *
3 * Nagios check_mrtg 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_mrtg plugin
13 *
14 * This plugin will check either the average or maximum value of one of the
15 * two variables recorded in an MRTG log file.
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 const char *progname = "check_mrtg";
36 const char *revision = "$Revision$";
37 const char *copyright = "1999-2007";
38 const char *email = "nagiosplug-devel@lists.sourceforge.net";
40 #include "common.h"
41 #include "utils.h"
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 = 0;
50 int use_average = TRUE;
51 int variable_number = -1;
52 unsigned long value_warning_threshold = 0L;
53 unsigned long value_critical_threshold = 0L;
54 char *label;
55 char *units;
57 int
58 main (int argc, char **argv)
59 {
60 int result = STATE_UNKNOWN;
61 FILE *fp;
62 int line;
63 char input_buffer[MAX_INPUT_BUFFER];
64 char *temp_buffer;
65 time_t current_time;
66 time_t timestamp = 0L;
67 unsigned long average_value_rate = 0L;
68 unsigned long maximum_value_rate = 0L;
69 unsigned long rate = 0L;
71 setlocale (LC_ALL, "");
72 bindtextdomain (PACKAGE, LOCALEDIR);
73 textdomain (PACKAGE);
75 if (process_arguments (argc, argv) == ERROR)
76 usage4 (_("Could not parse arguments\n"));
78 /* open the MRTG log file for reading */
79 fp = fopen (log_file, "r");
80 if (fp == NULL) {
81 printf (_("Unable to open MRTG log file\n"));
82 return STATE_UNKNOWN;
83 }
85 line = 0;
86 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, fp)) {
88 line++;
90 /* skip the first line of the log file */
91 if (line == 1)
92 continue;
94 /* break out of read loop if we've passed the number of entries we want to read */
95 if (line > 2)
96 break;
98 /* grab the timestamp */
99 temp_buffer = strtok (input_buffer, " ");
100 timestamp = strtoul (temp_buffer, NULL, 10);
102 /* grab the average value 1 rate */
103 temp_buffer = strtok (NULL, " ");
104 if (variable_number == 1)
105 average_value_rate = strtoul (temp_buffer, NULL, 10);
107 /* grab the average value 2 rate */
108 temp_buffer = strtok (NULL, " ");
109 if (variable_number == 2)
110 average_value_rate = strtoul (temp_buffer, NULL, 10);
112 /* grab the maximum value 1 rate */
113 temp_buffer = strtok (NULL, " ");
114 if (variable_number == 1)
115 maximum_value_rate = strtoul (temp_buffer, NULL, 10);
117 /* grab the maximum value 2 rate */
118 temp_buffer = strtok (NULL, " ");
119 if (variable_number == 2)
120 maximum_value_rate = strtoul (temp_buffer, NULL, 10);
121 }
123 /* close the log file */
124 fclose (fp);
126 /* if we couldn't read enough data, return an unknown error */
127 if (line <= 2) {
128 printf (_("Unable to process MRTG log file\n"));
129 return STATE_UNKNOWN;
130 }
132 /* make sure the MRTG data isn't too old */
133 time (¤t_time);
134 if (expire_minutes > 0
135 && (current_time - timestamp) > (expire_minutes * 60)) {
136 printf (_("MRTG data has expired (%d minutes old)\n"),
137 (int) ((current_time - timestamp) / 60));
138 return STATE_WARNING;
139 }
141 /* else check the incoming/outgoing rates */
142 if (use_average == TRUE)
143 rate = average_value_rate;
144 else
145 rate = maximum_value_rate;
147 if (rate > value_critical_threshold)
148 result = STATE_CRITICAL;
149 else if (rate > value_warning_threshold)
150 result = STATE_WARNING;
152 printf("%s. %s = %lu %s|%s\n",
153 (use_average == TRUE) ? _("Avg") : _("Max"),
154 label, rate, units,
155 perfdata(label, (long) rate, units,
156 (int) value_warning_threshold, (long) value_warning_threshold,
157 (int) value_critical_threshold, (long) value_critical_threshold,
158 0, 0, 0, 0));
160 return result;
161 }
165 /* process command-line arguments */
166 int
167 process_arguments (int argc, char **argv)
168 {
169 int c;
171 int option = 0;
172 static struct option longopts[] = {
173 {"logfile", required_argument, 0, 'F'},
174 {"expires", required_argument, 0, 'e'},
175 {"aggregation", required_argument, 0, 'a'},
176 {"variable", required_argument, 0, 'v'},
177 {"critical", required_argument, 0, 'c'},
178 {"warning", required_argument, 0, 'w'},
179 {"label", required_argument, 0, 'l'},
180 {"units", required_argument, 0, 'u'},
181 {"verbose", no_argument, 0, 'v'},
182 {"version", no_argument, 0, 'V'},
183 {"help", no_argument, 0, 'h'},
184 {0, 0, 0, 0}
185 };
187 if (argc < 2)
188 return ERROR;
190 for (c = 1; c < argc; c++) {
191 if (strcmp ("-to", argv[c]) == 0)
192 strcpy (argv[c], "-t");
193 else if (strcmp ("-wt", argv[c]) == 0)
194 strcpy (argv[c], "-w");
195 else if (strcmp ("-ct", argv[c]) == 0)
196 strcpy (argv[c], "-c");
197 }
199 while (1) {
200 c = getopt_long (argc, argv, "hVF:e:a:v:c:w:l:u:", longopts,
201 &option);
203 if (c == -1 || c == EOF)
204 break;
206 switch (c) {
207 case 'F': /* input file */
208 log_file = optarg;
209 break;
210 case 'e': /* ups name */
211 expire_minutes = atoi (optarg);
212 break;
213 case 'a': /* port */
214 if (!strcmp (optarg, "MAX"))
215 use_average = FALSE;
216 else
217 use_average = TRUE;
218 break;
219 case 'v':
220 variable_number = atoi (optarg);
221 if (variable_number < 1 || variable_number > 2)
222 usage4 (_("Invalid variable number"));
223 break;
224 case 'w': /* critical time threshold */
225 value_warning_threshold = strtoul (optarg, NULL, 10);
226 break;
227 case 'c': /* warning time threshold */
228 value_critical_threshold = strtoul (optarg, NULL, 10);
229 break;
230 case 'l': /* label */
231 label = optarg;
232 break;
233 case 'u': /* timeout */
234 units = optarg;
235 break;
236 case 'V': /* version */
237 print_revision (progname, revision);
238 exit (STATE_OK);
239 case 'h': /* help */
240 print_help ();
241 exit (STATE_OK);
242 case '?': /* help */
243 usage5 ();
244 }
245 }
247 c = optind;
248 if (log_file == NULL && argc > c) {
249 log_file = argv[c++];
250 }
252 if (expire_minutes <= 0 && argc > c) {
253 if (is_intpos (argv[c]))
254 expire_minutes = atoi (argv[c++]);
255 else
256 die (STATE_UNKNOWN,
257 _("%s is not a valid expiration time\nUse '%s -h' for additional help\n"),
258 argv[c], progname);
259 }
261 if (argc > c && strcmp (argv[c], "MAX") == 0) {
262 use_average = FALSE;
263 c++;
264 }
265 else if (argc > c && strcmp (argv[c], "AVG") == 0) {
266 use_average = TRUE;
267 c++;
268 }
270 if (argc > c && variable_number == -1) {
271 variable_number = atoi (argv[c++]);
272 if (variable_number < 1 || variable_number > 2) {
273 printf ("%s :", argv[c]);
274 usage (_("Invalid variable number\n"));
275 }
276 }
278 if (argc > c && value_warning_threshold == 0) {
279 value_warning_threshold = strtoul (argv[c++], NULL, 10);
280 }
282 if (argc > c && value_critical_threshold == 0) {
283 value_critical_threshold = strtoul (argv[c++], NULL, 10);
284 }
286 if (argc > c && strlen (label) == 0) {
287 label = argv[c++];
288 }
290 if (argc > c && strlen (units) == 0) {
291 units = argv[c++];
292 }
294 return validate_arguments ();
295 }
297 int
298 validate_arguments (void)
299 {
300 if (variable_number == -1)
301 usage4 (_("You must supply the variable number"));
303 if (label == NULL)
304 label = strdup ("value");
306 if (units == NULL)
307 units = strdup ("");
309 return OK;
310 }
314 void
315 print_help (void)
316 {
317 print_revision (progname, revision);
319 printf ("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n");
320 printf (COPYRIGHT, copyright, email);
322 printf ("%s\n", _("This plugin will check either the average or maximum value of one of the"));
323 printf ("%s\n", _("two variables recorded in an MRTG log file."));
325 printf ("\n\n");
327 print_usage ();
329 printf (_(UT_HELP_VRSN));
331 printf (" %s\n", "-F, --logfile=FILE");
332 printf (" %s\n", _("The MRTG log file containing the data you want to monitor"));
333 printf ("-e, --expires=MINUTES");
334 printf (" %s\n", _("Minutes before MRTG data is considered to be too old"));
335 printf (" %s\n", "-a, --aggregation=AVG|MAX");
336 printf (" %s\n", _("Should we check average or maximum values?"));
337 printf (" %s\n", "-v, --variable=INTEGER");
338 printf (" %s\n", _("Which variable set should we inspect? (1 or 2)"));
339 printf (" %s\n", "-w, --warning=INTEGER");
340 printf (" %s\n", _("Threshold value for data to result in WARNING status"));
341 printf (" %s\n", "-c, --critical=INTEGER");
342 printf (" %s\n", _("Threshold value for data to result in CRITICAL status"));
343 printf (" %s\n", "-l, --label=STRING");
344 printf (" %s\n", _("Type label for data (Examples: Conns, \"Processor Load\", In, Out)"));
345 printf (" %s\n", "-u, --units=STRING");
346 printf (" %s\n", _("Option units label for data (Example: Packets/Sec, Errors/Sec,"));
347 printf (" %s\n", _("\"Bytes Per Second\", \"%% Utilization\")"));
349 printf ("\n");
350 printf (" %s\n", _("If the value exceeds the <vwl> threshold, a WARNING status is returned. If"));
351 printf (" %s\n", _("the value exceeds the <vcl> threshold, a CRITICAL status is returned. If"));
352 printf (" %s\n", _("the data in the log file is older than <expire_minutes> old, a WARNING"));
353 printf (" %s\n", _("status is returned and a warning message is printed."));
355 printf ("\n");
356 printf (" %s\n", _("This plugin is useful for monitoring MRTG data that does not correspond to"));
357 printf (" %s\n", _("bandwidth usage. (Use the check_mrtgtraf plugin for monitoring bandwidth)."));
358 printf (" %s\n", _("It can be used to monitor any kind of data that MRTG is monitoring - errors,"));
359 printf (" %s\n", _("packets/sec, etc. I use MRTG in conjuction with the Novell NLM that allows"));
360 printf (" %s\n", _("me to track processor utilization, user connections, drive space, etc and"));
361 printf (" %s\n\n", _("this plugin works well for monitoring that kind of data as well."));
363 printf ("%s\n", _("Notes:"));
364 printf (" %s\n", _("- This plugin only monitors one of the two variables stored in the MRTG log"));
365 printf (" %s\n", _("file. If you want to monitor both values you will have to define two"));
366 printf (" %s\n", _("commands with different values for the <variable> argument. Of course,"));
367 printf (" %s\n", _("you can always hack the code to make this plugin work for you..."));
368 printf (" %s\n", _("- MRTG stands for the Multi Router Traffic Grapher. It can be downloaded from"));
369 printf (" %s\n", "http://ee-staff.ethz.ch/~oetiker/webtools/mrtg/mrtg.html");
371 printf (_(UT_SUPPORT));
372 }
376 /* original command line:
377 <log_file> <expire_minutes> <AVG|MAX> <variable> <vwl> <vcl> <label> [units] */
379 void
380 print_usage (void)
381 {
382 printf (_("Usage:"));
383 printf ("%s -F log_file -a <AVG | MAX> -v variable -w warning -c critical\n",progname);
384 printf ("[-l label] [-u units] [-e expire_minutes] [-t timeout] [-v]\n");
385 }