1 /*****************************************************************************
3 * Nagios check_mrtgtraf plugin
6 * Copyright (c) 1999-2007 Nagios Plugins Development Team
8 * Last Modified: $Date$
12 * This file contains the check_mtrgtraf plugin
14 * This plugin will check the incoming/outgoing transfer rates of a router
15 * switch, etc recorded in an MRTG log.
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.
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.
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/>.
33 *****************************************************************************/
38 const char *progname
= "check_mrtgtraf";
39 const char *revision
= "$Revision$";
40 const char *copyright
= "1999-2007";
41 const char *email
= "nagiosplug-devel@lists.sourceforge.net";
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
= -1;
50 int use_average
= TRUE
;
51 unsigned long incoming_warning_threshold
= 0L;
52 unsigned long incoming_critical_threshold
= 0L;
53 unsigned long outgoing_warning_threshold
= 0L;
54 unsigned long outgoing_critical_threshold
= 0L;
58 main (int argc
, char **argv
)
60 int result
= STATE_OK
;
63 char input_buffer
[MAX_INPUT_BUFFER
];
67 time_t timestamp
= 0L;
68 unsigned long average_incoming_rate
= 0L;
69 unsigned long average_outgoing_rate
= 0L;
70 unsigned long maximum_incoming_rate
= 0L;
71 unsigned long maximum_outgoing_rate
= 0L;
72 unsigned long incoming_rate
= 0L;
73 unsigned long outgoing_rate
= 0L;
74 double adjusted_incoming_rate
= 0.0;
75 double adjusted_outgoing_rate
= 0.0;
76 char incoming_speed_rating
[8];
77 char outgoing_speed_rating
[8];
79 setlocale (LC_ALL
, "");
80 bindtextdomain (PACKAGE
, LOCALEDIR
);
83 /* Parse extra opts if any */
84 argv
=np_extra_opts (&argc
, argv
, progname
);
86 if (process_arguments (argc
, argv
) == ERROR
)
87 usage4 (_("Could not parse arguments"));
89 /* open the MRTG log file for reading */
90 fp
= fopen (log_file
, "r");
92 usage4 (_("Unable to open MRTG log file"));
95 while (fgets (input_buffer
, MAX_INPUT_BUFFER
- 1, fp
)) {
99 /* skip the first line of the log file */
103 /* break out of read loop */
104 /* if we've passed the number of entries we want to read */
108 /* grab the timestamp */
109 temp_buffer
= strtok (input_buffer
, " ");
110 timestamp
= strtoul (temp_buffer
, NULL
, 10);
112 /* grab the average incoming transfer rate */
113 temp_buffer
= strtok (NULL
, " ");
114 average_incoming_rate
= strtoul (temp_buffer
, NULL
, 10);
116 /* grab the average outgoing transfer rate */
117 temp_buffer
= strtok (NULL
, " ");
118 average_outgoing_rate
= strtoul (temp_buffer
, NULL
, 10);
120 /* grab the maximum incoming transfer rate */
121 temp_buffer
= strtok (NULL
, " ");
122 maximum_incoming_rate
= strtoul (temp_buffer
, NULL
, 10);
124 /* grab the maximum outgoing transfer rate */
125 temp_buffer
= strtok (NULL
, " ");
126 maximum_outgoing_rate
= strtoul (temp_buffer
, NULL
, 10);
129 /* close the log file */
132 /* if we couldn't read enough data, return an unknown error */
134 usage4 (_("Unable to process MRTG log file"));
136 /* make sure the MRTG data isn't too old */
137 time (¤t_time
);
138 if ((expire_minutes
> 0) &&
139 (current_time
- timestamp
) > (expire_minutes
* 60))
140 die (STATE_WARNING
, _("MRTG data has expired (%d minutes old)\n"),
141 (int) ((current_time
- timestamp
) / 60));
143 /* else check the incoming/outgoing rates */
144 if (use_average
== TRUE
) {
145 incoming_rate
= average_incoming_rate
;
146 outgoing_rate
= average_outgoing_rate
;
149 incoming_rate
= maximum_incoming_rate
;
150 outgoing_rate
= maximum_outgoing_rate
;
153 /* report incoming traffic in Bytes/sec */
154 if (incoming_rate
< 1024) {
155 strcpy (incoming_speed_rating
, "B/s");
156 adjusted_incoming_rate
= (double) incoming_rate
;
159 /* report incoming traffic in KBytes/sec */
160 else if (incoming_rate
< (1024 * 1024)) {
161 strcpy (incoming_speed_rating
, "KB/s");
162 adjusted_incoming_rate
= (double) (incoming_rate
/ 1024.0);
165 /* report incoming traffic in MBytes/sec */
167 strcpy (incoming_speed_rating
, "MB/s");
168 adjusted_incoming_rate
= (double) (incoming_rate
/ 1024.0 / 1024.0);
171 /* report outgoing traffic in Bytes/sec */
172 if (outgoing_rate
< 1024) {
173 strcpy (outgoing_speed_rating
, "B/s");
174 adjusted_outgoing_rate
= (double) outgoing_rate
;
177 /* report outgoing traffic in KBytes/sec */
178 else if (outgoing_rate
< (1024 * 1024)) {
179 strcpy (outgoing_speed_rating
, "KB/s");
180 adjusted_outgoing_rate
= (double) (outgoing_rate
/ 1024.0);
183 /* report outgoing traffic in MBytes/sec */
185 strcpy (outgoing_speed_rating
, "MB/s");
186 adjusted_outgoing_rate
= (double) (outgoing_rate
/ 1024.0 / 1024.0);
189 if (incoming_rate
> incoming_critical_threshold
190 || outgoing_rate
> outgoing_critical_threshold
) {
191 result
= STATE_CRITICAL
;
193 else if (incoming_rate
> incoming_warning_threshold
194 || outgoing_rate
> outgoing_warning_threshold
) {
195 result
= STATE_WARNING
;
198 asprintf (&error_message
, _("%s. In = %0.1f %s, %s. Out = %0.1f %s|%s %s\n"),
199 (use_average
== TRUE
) ? _("Avg") : _("Max"), adjusted_incoming_rate
,
200 incoming_speed_rating
, (use_average
== TRUE
) ? _("Avg") : _("Max"),
201 adjusted_outgoing_rate
, outgoing_speed_rating
,
202 fperfdata("in", adjusted_incoming_rate
, incoming_speed_rating
,
203 (int)incoming_warning_threshold
, incoming_warning_threshold
,
204 (int)incoming_critical_threshold
, incoming_critical_threshold
,
206 fperfdata("in", adjusted_outgoing_rate
, outgoing_speed_rating
,
207 (int)outgoing_warning_threshold
, outgoing_warning_threshold
,
208 (int)outgoing_critical_threshold
, outgoing_critical_threshold
,
211 printf (_("Traffic %s - %s\n"), state_text(result
), error_message
);
218 /* process command-line arguments */
220 process_arguments (int argc
, char **argv
)
225 static struct option longopts
[] = {
226 {"logfile", required_argument
, 0, 'F'},
227 {"expires", required_argument
, 0, 'e'},
228 {"aggregation", required_argument
, 0, 'a'},
229 {"variable", required_argument
, 0, 'v'},
230 {"critical", required_argument
, 0, 'c'},
231 {"warning", required_argument
, 0, 'w'},
232 {"verbose", no_argument
, 0, 'v'},
233 {"version", no_argument
, 0, 'V'},
234 {"help", no_argument
, 0, 'h'},
241 for (c
= 1; c
< argc
; c
++) {
242 if (strcmp ("-to", argv
[c
]) == 0)
243 strcpy (argv
[c
], "-t");
244 else if (strcmp ("-wt", argv
[c
]) == 0)
245 strcpy (argv
[c
], "-w");
246 else if (strcmp ("-ct", argv
[c
]) == 0)
247 strcpy (argv
[c
], "-c");
251 c
= getopt_long (argc
, argv
, "hVF:e:a:c:w:", longopts
, &option
);
253 if (c
== -1 || c
== EOF
)
257 case 'F': /* input file */
260 case 'e': /* expiration time */
261 expire_minutes
= atoi (optarg
);
263 case 'a': /* aggregation (AVE or MAX) */
264 if (!strcmp (optarg
, "MAX"))
269 case 'c': /* warning threshold */
270 sscanf (optarg
, "%lu,%lu", &incoming_critical_threshold
,
271 &outgoing_critical_threshold
);
273 case 'w': /* critical threshold */
274 sscanf (optarg
, "%lu,%lu", &incoming_warning_threshold
,
275 &outgoing_warning_threshold
);
277 case 'V': /* version */
278 print_revision (progname
, revision
);
289 if (argc
> c
&& log_file
== NULL
) {
290 log_file
= argv
[c
++];
293 if (argc
> c
&& expire_minutes
== -1) {
294 expire_minutes
= atoi (argv
[c
++]);
297 if (argc
> c
&& strcmp (argv
[c
], "MAX") == 0) {
301 else if (argc
> c
&& strcmp (argv
[c
], "AVG") == 0) {
306 if (argc
> c
&& incoming_warning_threshold
== 0) {
307 incoming_warning_threshold
= strtoul (argv
[c
++], NULL
, 10);
310 if (argc
> c
&& incoming_critical_threshold
== 0) {
311 incoming_critical_threshold
= strtoul (argv
[c
++], NULL
, 10);
314 if (argc
> c
&& outgoing_warning_threshold
== 0) {
315 outgoing_warning_threshold
= strtoul (argv
[c
++], NULL
, 10);
318 if (argc
> c
&& outgoing_critical_threshold
== 0) {
319 outgoing_critical_threshold
= strtoul (argv
[c
++], NULL
, 10);
322 return validate_arguments ();
327 validate_arguments (void)
336 print_revision (progname
, revision
);
338 printf ("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n");
339 printf (COPYRIGHT
, copyright
, email
);
341 printf ("%s\n", _("This plugin will check the incoming/outgoing transfer rates of a router,"));
342 printf ("%s\n", _("switch, etc recorded in an MRTG log. If the newest log entry is older"));
343 printf ("%s\n", _("than <expire_minutes>, a WARNING status is returned. If either the"));
344 printf ("%s\n", _("incoming or outgoing rates exceed the <icl> or <ocl> thresholds (in"));
345 printf ("%s\n", _("Bytes/sec), a CRITICAL status results. If either of the rates exceed"));
346 printf ("%s\n", _("the <iwl> or <owl> thresholds (in Bytes/sec), a WARNING status results."));
352 printf (_(UT_HELP_VRSN
));
353 printf (_(UT_EXTRA_OPTS
));
355 printf (" %s\n", "-F, --filename=STRING");
356 printf (" %s\n", _("File to read log from"));
357 printf (" %s\n", "-e, --expires=INTEGER");
358 printf (" %s\n", _("Minutes after which log expires"));
359 printf (" %s\n", "-a, --aggregation=(AVG|MAX)");
360 printf (" %s\n", _("Test average or maximum"));
361 printf (" %s\n", "-w, --warning");
362 printf (" %s\n", _("Warning threshold pair <incoming>,<outgoing>"));
363 printf (" %s\n", "-c, --critical");
364 printf (" %s\n", _("Critical threshold pair <incoming>,<outgoing>"));
367 printf ("%s\n", _("Notes:"));
368 printf (" %s\n", _("- MRTG stands for Multi Router Traffic Grapher. It can be downloaded from"));
369 printf (" %s\n", " http://ee-staff.ethz.ch/~oetiker/webtools/mrtg/mrtg.html");
370 printf (" %s\n", _("- While MRTG can monitor things other than traffic rates, this"));
371 printf (" %s\n", _(" plugin probably won't work with much else without modification."));
372 printf (" %s\n", _("- The calculated i/o rates are a little off from what MRTG actually"));
373 printf (" %s\n", _(" reports. I'm not sure why this is right now, but will look into it"));
374 printf (" %s\n", _(" for future enhancements of this plugin."));
376 printf (" -%s", _(UT_EXTRA_OPTS_NOTES
));
379 printf (_(UT_SUPPORT
));
388 printf (" %s -F <log_file> -a <AVG | MAX> -v <variable> -w <warning_pair>",progname
);
389 printf ("-c <critical_pair> [-e expire_minutes] [-t timeout] [-v]\n");