1 /*****************************************************************************
3 * Monitoring check_mrtg plugin
6 * Copyright (c) 1999-2007 Monitoring Plugins Development Team
10 * This file contains the check_mrtg plugin
12 * This plugin will check either the average or maximum value of one of the
13 * two variables recorded in an MRTG log file.
16 * This program is free software: you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation, either version 3 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 *****************************************************************************/
32 const char *progname
= "check_mrtg";
33 const char *copyright
= "1999-2007";
34 const char *email
= "devel@monitoring-plugins.org";
39 int process_arguments (int, char **);
40 int validate_arguments (void);
41 void print_help (void);
42 void print_usage (void);
44 char *log_file
= NULL
;
45 int expire_minutes
= 0;
46 int use_average
= TRUE
;
47 int variable_number
= -1;
48 unsigned long value_warning_threshold
= 0L;
49 unsigned long value_critical_threshold
= 0L;
54 main (int argc
, char **argv
)
56 int result
= STATE_OK
;
59 char input_buffer
[MAX_INPUT_BUFFER
];
62 time_t timestamp
= 0L;
63 unsigned long average_value_rate
= 0L;
64 unsigned long maximum_value_rate
= 0L;
65 unsigned long rate
= 0L;
67 setlocale (LC_ALL
, "");
68 bindtextdomain (PACKAGE
, LOCALEDIR
);
71 /* Parse extra opts if any */
72 argv
=np_extra_opts (&argc
, argv
, progname
);
74 if (process_arguments (argc
, argv
) == ERROR
)
75 usage4 (_("Could not parse arguments\n"));
77 /* open the MRTG log file for reading */
78 fp
= fopen (log_file
, "r");
80 printf (_("Unable to open MRTG log file\n"));
85 while (fgets (input_buffer
, MAX_INPUT_BUFFER
- 1, fp
)) {
89 /* skip the first line of the log file */
93 /* break out of read loop if we've passed the number of entries we want to read */
97 /* grab the timestamp */
98 temp_buffer
= strtok (input_buffer
, " ");
99 timestamp
= strtoul (temp_buffer
, NULL
, 10);
101 /* grab the average value 1 rate */
102 temp_buffer
= strtok (NULL
, " ");
103 if (variable_number
== 1)
104 average_value_rate
= strtoul (temp_buffer
, NULL
, 10);
106 /* grab the average value 2 rate */
107 temp_buffer
= strtok (NULL
, " ");
108 if (variable_number
== 2)
109 average_value_rate
= strtoul (temp_buffer
, NULL
, 10);
111 /* grab the maximum value 1 rate */
112 temp_buffer
= strtok (NULL
, " ");
113 if (variable_number
== 1)
114 maximum_value_rate
= strtoul (temp_buffer
, NULL
, 10);
116 /* grab the maximum value 2 rate */
117 temp_buffer
= strtok (NULL
, " ");
118 if (variable_number
== 2)
119 maximum_value_rate
= strtoul (temp_buffer
, NULL
, 10);
122 /* close the log file */
125 /* if we couldn't read enough data, return an unknown error */
127 printf (_("Unable to process MRTG log file\n"));
128 return STATE_UNKNOWN
;
131 /* make sure the MRTG data isn't too old */
132 time (¤t_time
);
133 if (expire_minutes
> 0
134 && (current_time
- timestamp
) > (expire_minutes
* 60)) {
135 printf (_("MRTG data has expired (%d minutes old)\n"),
136 (int) ((current_time
- timestamp
) / 60));
137 return STATE_WARNING
;
140 /* else check the incoming/outgoing rates */
141 if (use_average
== TRUE
)
142 rate
= average_value_rate
;
144 rate
= maximum_value_rate
;
146 if (rate
> value_critical_threshold
)
147 result
= STATE_CRITICAL
;
148 else if (rate
> value_warning_threshold
)
149 result
= STATE_WARNING
;
151 printf("%s. %s = %lu %s|%s\n",
152 (use_average
== TRUE
) ? _("Avg") : _("Max"),
154 perfdata(label
, (long) rate
, units
,
155 (int) value_warning_threshold
, (long) value_warning_threshold
,
156 (int) value_critical_threshold
, (long) value_critical_threshold
,
164 /* process command-line arguments */
166 process_arguments (int argc
, char **argv
)
171 static struct option longopts
[] = {
172 {"logfile", required_argument
, 0, 'F'},
173 {"expires", required_argument
, 0, 'e'},
174 {"aggregation", required_argument
, 0, 'a'},
175 {"variable", required_argument
, 0, 'v'},
176 {"critical", required_argument
, 0, 'c'},
177 {"warning", required_argument
, 0, 'w'},
178 {"label", required_argument
, 0, 'l'},
179 {"units", required_argument
, 0, 'u'},
180 {"variable", required_argument
, 0, 'v'},
181 {"version", no_argument
, 0, 'V'},
182 {"help", no_argument
, 0, 'h'},
189 for (c
= 1; c
< argc
; c
++) {
190 if (strcmp ("-to", argv
[c
]) == 0)
191 strcpy (argv
[c
], "-t");
192 else if (strcmp ("-wt", argv
[c
]) == 0)
193 strcpy (argv
[c
], "-w");
194 else if (strcmp ("-ct", argv
[c
]) == 0)
195 strcpy (argv
[c
], "-c");
199 c
= getopt_long (argc
, argv
, "hVF:e:a:v:c:w:l:u:", longopts
,
202 if (c
== -1 || c
== EOF
)
206 case 'F': /* input file */
209 case 'e': /* ups name */
210 expire_minutes
= atoi (optarg
);
213 if (!strcmp (optarg
, "MAX"))
219 variable_number
= atoi (optarg
);
220 if (variable_number
< 1 || variable_number
> 2)
221 usage4 (_("Invalid variable number"));
223 case 'w': /* critical time threshold */
224 value_warning_threshold
= strtoul (optarg
, NULL
, 10);
226 case 'c': /* warning time threshold */
227 value_critical_threshold
= strtoul (optarg
, NULL
, 10);
229 case 'l': /* label */
232 case 'u': /* timeout */
235 case 'V': /* version */
236 print_revision (progname
, NP_VERSION
);
237 exit (STATE_UNKNOWN
);
240 exit (STATE_UNKNOWN
);
247 if (log_file
== NULL
&& argc
> c
) {
248 log_file
= argv
[c
++];
251 if (expire_minutes
<= 0 && argc
> c
) {
252 if (is_intpos (argv
[c
]))
253 expire_minutes
= atoi (argv
[c
++]);
256 _("%s is not a valid expiration time\nUse '%s -h' for additional help\n"),
260 if (argc
> c
&& strcmp (argv
[c
], "MAX") == 0) {
264 else if (argc
> c
&& strcmp (argv
[c
], "AVG") == 0) {
269 if (argc
> c
&& variable_number
== -1) {
270 variable_number
= atoi (argv
[c
++]);
271 if (variable_number
< 1 || variable_number
> 2) {
272 printf ("%s :", argv
[c
]);
273 usage (_("Invalid variable number\n"));
277 if (argc
> c
&& value_warning_threshold
== 0) {
278 value_warning_threshold
= strtoul (argv
[c
++], NULL
, 10);
281 if (argc
> c
&& value_critical_threshold
== 0) {
282 value_critical_threshold
= strtoul (argv
[c
++], NULL
, 10);
285 if (argc
> c
&& strlen (label
) == 0) {
289 if (argc
> c
&& strlen (units
) == 0) {
293 return validate_arguments ();
297 validate_arguments (void)
299 if (variable_number
== -1)
300 usage4 (_("You must supply the variable number"));
303 label
= strdup ("value");
316 print_revision (progname
, NP_VERSION
);
318 printf ("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n");
319 printf (COPYRIGHT
, copyright
, email
);
321 printf ("%s\n", _("This plugin will check either the average or maximum value of one of the"));
322 printf ("%s\n", _("two variables recorded in an MRTG log file."));
328 printf (UT_HELP_VRSN
);
329 printf (UT_EXTRA_OPTS
);
331 printf (" %s\n", "-F, --logfile=FILE");
332 printf (" %s\n", _("The MRTG log file containing the data you want to monitor"));
333 printf (" %s\n", "-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\")"));
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."));
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 conjunction 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");
376 /* original command line:
377 <log_file> <expire_minutes> <AVG|MAX> <variable> <vwl> <vcl> <label> [units] */
382 printf ("%s\n", _("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");