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