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 /* Parse extra opts if any */
76 argv=np_extra_opts (&argc, argv, progname);
78 if (process_arguments (argc, argv) == ERROR)
79 usage4 (_("Could not parse arguments\n"));
81 /* open the MRTG log file for reading */
82 fp = fopen (log_file, "r");
83 if (fp == NULL) {
84 printf (_("Unable to open MRTG log file\n"));
85 return STATE_UNKNOWN;
86 }
88 line = 0;
89 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, fp)) {
91 line++;
93 /* skip the first line of the log file */
94 if (line == 1)
95 continue;
97 /* break out of read loop if we've passed the number of entries we want to read */
98 if (line > 2)
99 break;
101 /* grab the timestamp */
102 temp_buffer = strtok (input_buffer, " ");
103 timestamp = strtoul (temp_buffer, NULL, 10);
105 /* grab the average value 1 rate */
106 temp_buffer = strtok (NULL, " ");
107 if (variable_number == 1)
108 average_value_rate = strtoul (temp_buffer, NULL, 10);
110 /* grab the average value 2 rate */
111 temp_buffer = strtok (NULL, " ");
112 if (variable_number == 2)
113 average_value_rate = strtoul (temp_buffer, NULL, 10);
115 /* grab the maximum value 1 rate */
116 temp_buffer = strtok (NULL, " ");
117 if (variable_number == 1)
118 maximum_value_rate = strtoul (temp_buffer, NULL, 10);
120 /* grab the maximum value 2 rate */
121 temp_buffer = strtok (NULL, " ");
122 if (variable_number == 2)
123 maximum_value_rate = strtoul (temp_buffer, NULL, 10);
124 }
126 /* close the log file */
127 fclose (fp);
129 /* if we couldn't read enough data, return an unknown error */
130 if (line <= 2) {
131 printf (_("Unable to process MRTG log file\n"));
132 return STATE_UNKNOWN;
133 }
135 /* make sure the MRTG data isn't too old */
136 time (¤t_time);
137 if (expire_minutes > 0
138 && (current_time - timestamp) > (expire_minutes * 60)) {
139 printf (_("MRTG data has expired (%d minutes old)\n"),
140 (int) ((current_time - timestamp) / 60));
141 return STATE_WARNING;
142 }
144 /* else check the incoming/outgoing rates */
145 if (use_average == TRUE)
146 rate = average_value_rate;
147 else
148 rate = maximum_value_rate;
150 if (rate > value_critical_threshold)
151 result = STATE_CRITICAL;
152 else if (rate > value_warning_threshold)
153 result = STATE_WARNING;
155 printf("%s. %s = %lu %s|%s\n",
156 (use_average == TRUE) ? _("Avg") : _("Max"),
157 label, rate, units,
158 perfdata(label, (long) rate, units,
159 (int) value_warning_threshold, (long) value_warning_threshold,
160 (int) value_critical_threshold, (long) value_critical_threshold,
161 0, 0, 0, 0));
163 return result;
164 }
168 /* process command-line arguments */
169 int
170 process_arguments (int argc, char **argv)
171 {
172 int c;
174 int option = 0;
175 static struct option longopts[] = {
176 {"logfile", required_argument, 0, 'F'},
177 {"expires", required_argument, 0, 'e'},
178 {"aggregation", required_argument, 0, 'a'},
179 {"variable", required_argument, 0, 'v'},
180 {"critical", required_argument, 0, 'c'},
181 {"warning", required_argument, 0, 'w'},
182 {"label", required_argument, 0, 'l'},
183 {"units", required_argument, 0, 'u'},
184 {"verbose", no_argument, 0, 'v'},
185 {"version", no_argument, 0, 'V'},
186 {"help", no_argument, 0, 'h'},
187 {0, 0, 0, 0}
188 };
190 if (argc < 2)
191 return ERROR;
193 for (c = 1; c < argc; c++) {
194 if (strcmp ("-to", argv[c]) == 0)
195 strcpy (argv[c], "-t");
196 else if (strcmp ("-wt", argv[c]) == 0)
197 strcpy (argv[c], "-w");
198 else if (strcmp ("-ct", argv[c]) == 0)
199 strcpy (argv[c], "-c");
200 }
202 while (1) {
203 c = getopt_long (argc, argv, "hVF:e:a:v:c:w:l:u:", longopts,
204 &option);
206 if (c == -1 || c == EOF)
207 break;
209 switch (c) {
210 case 'F': /* input file */
211 log_file = optarg;
212 break;
213 case 'e': /* ups name */
214 expire_minutes = atoi (optarg);
215 break;
216 case 'a': /* port */
217 if (!strcmp (optarg, "MAX"))
218 use_average = FALSE;
219 else
220 use_average = TRUE;
221 break;
222 case 'v':
223 variable_number = atoi (optarg);
224 if (variable_number < 1 || variable_number > 2)
225 usage4 (_("Invalid variable number"));
226 break;
227 case 'w': /* critical time threshold */
228 value_warning_threshold = strtoul (optarg, NULL, 10);
229 break;
230 case 'c': /* warning time threshold */
231 value_critical_threshold = strtoul (optarg, NULL, 10);
232 break;
233 case 'l': /* label */
234 label = optarg;
235 break;
236 case 'u': /* timeout */
237 units = optarg;
238 break;
239 case 'V': /* version */
240 print_revision (progname, revision);
241 exit (STATE_OK);
242 case 'h': /* help */
243 print_help ();
244 exit (STATE_OK);
245 case '?': /* help */
246 usage5 ();
247 }
248 }
250 c = optind;
251 if (log_file == NULL && argc > c) {
252 log_file = argv[c++];
253 }
255 if (expire_minutes <= 0 && argc > c) {
256 if (is_intpos (argv[c]))
257 expire_minutes = atoi (argv[c++]);
258 else
259 die (STATE_UNKNOWN,
260 _("%s is not a valid expiration time\nUse '%s -h' for additional help\n"),
261 argv[c], progname);
262 }
264 if (argc > c && strcmp (argv[c], "MAX") == 0) {
265 use_average = FALSE;
266 c++;
267 }
268 else if (argc > c && strcmp (argv[c], "AVG") == 0) {
269 use_average = TRUE;
270 c++;
271 }
273 if (argc > c && variable_number == -1) {
274 variable_number = atoi (argv[c++]);
275 if (variable_number < 1 || variable_number > 2) {
276 printf ("%s :", argv[c]);
277 usage (_("Invalid variable number\n"));
278 }
279 }
281 if (argc > c && value_warning_threshold == 0) {
282 value_warning_threshold = strtoul (argv[c++], NULL, 10);
283 }
285 if (argc > c && value_critical_threshold == 0) {
286 value_critical_threshold = strtoul (argv[c++], NULL, 10);
287 }
289 if (argc > c && strlen (label) == 0) {
290 label = argv[c++];
291 }
293 if (argc > c && strlen (units) == 0) {
294 units = argv[c++];
295 }
297 return validate_arguments ();
298 }
300 int
301 validate_arguments (void)
302 {
303 if (variable_number == -1)
304 usage4 (_("You must supply the variable number"));
306 if (label == NULL)
307 label = strdup ("value");
309 if (units == NULL)
310 units = strdup ("");
312 return OK;
313 }
317 void
318 print_help (void)
319 {
320 print_revision (progname, revision);
322 printf ("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n");
323 printf (COPYRIGHT, copyright, email);
325 printf ("%s\n", _("This plugin will check either the average or maximum value of one of the"));
326 printf ("%s\n", _("two variables recorded in an MRTG log file."));
328 printf ("\n\n");
330 print_usage ();
332 printf (_(UT_HELP_VRSN));
333 printf (_(UT_EXTRA_OPTS));
335 printf (" %s\n", "-F, --logfile=FILE");
336 printf (" %s\n", _("The MRTG log file containing the data you want to monitor"));
337 printf ("-e, --expires=MINUTES");
338 printf (" %s\n", _("Minutes before MRTG data is considered to be too old"));
339 printf (" %s\n", "-a, --aggregation=AVG|MAX");
340 printf (" %s\n", _("Should we check average or maximum values?"));
341 printf (" %s\n", "-v, --variable=INTEGER");
342 printf (" %s\n", _("Which variable set should we inspect? (1 or 2)"));
343 printf (" %s\n", "-w, --warning=INTEGER");
344 printf (" %s\n", _("Threshold value for data to result in WARNING status"));
345 printf (" %s\n", "-c, --critical=INTEGER");
346 printf (" %s\n", _("Threshold value for data to result in CRITICAL status"));
347 printf (" %s\n", "-l, --label=STRING");
348 printf (" %s\n", _("Type label for data (Examples: Conns, \"Processor Load\", In, Out)"));
349 printf (" %s\n", "-u, --units=STRING");
350 printf (" %s\n", _("Option units label for data (Example: Packets/Sec, Errors/Sec,"));
351 printf (" %s\n", _("\"Bytes Per Second\", \"%% Utilization\")"));
353 printf ("\n");
354 printf (" %s\n", _("If the value exceeds the <vwl> threshold, a WARNING status is returned. If"));
355 printf (" %s\n", _("the value exceeds the <vcl> threshold, a CRITICAL status is returned. If"));
356 printf (" %s\n", _("the data in the log file is older than <expire_minutes> old, a WARNING"));
357 printf (" %s\n", _("status is returned and a warning message is printed."));
359 printf ("\n");
360 printf (" %s\n", _("This plugin is useful for monitoring MRTG data that does not correspond to"));
361 printf (" %s\n", _("bandwidth usage. (Use the check_mrtgtraf plugin for monitoring bandwidth)."));
362 printf (" %s\n", _("It can be used to monitor any kind of data that MRTG is monitoring - errors,"));
363 printf (" %s\n", _("packets/sec, etc. I use MRTG in conjuction with the Novell NLM that allows"));
364 printf (" %s\n", _("me to track processor utilization, user connections, drive space, etc and"));
365 printf (" %s\n\n", _("this plugin works well for monitoring that kind of data as well."));
367 printf ("%s\n", _("Notes:"));
368 printf (" %s\n", _("- This plugin only monitors one of the two variables stored in the MRTG log"));
369 printf (" %s\n", _("file. If you want to monitor both values you will have to define two"));
370 printf (" %s\n", _("commands with different values for the <variable> argument. Of course,"));
371 printf (" %s\n", _("you can always hack the code to make this plugin work for you..."));
372 printf (" %s\n", _("- MRTG stands for the Multi Router Traffic Grapher. It can be downloaded from"));
373 printf (" %s\n", "http://ee-staff.ethz.ch/~oetiker/webtools/mrtg/mrtg.html");
374 #ifdef NP_EXTRA_OPTS
375 printf (" -%s", _(UT_EXTRA_OPTS_NOTES));
376 #endif
378 printf (_(UT_SUPPORT));
379 }
383 /* original command line:
384 <log_file> <expire_minutes> <AVG|MAX> <variable> <vwl> <vcl> <label> [units] */
386 void
387 print_usage (void)
388 {
389 printf (_("Usage:"));
390 printf ("%s -F log_file -a <AVG | MAX> -v variable -w warning -c critical\n",progname);
391 printf ("[-l label] [-u units] [-e expire_minutes] [-t timeout] [-v]\n");
392 }