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) > (expire_minutes * 60))
120 die (STATE_WARNING, _("MRTG data has expired (%d minutes old)\n"),
121 (int) ((current_time - timestamp) / 60));
123 /* else check the incoming/outgoing rates */
124 if (use_average == TRUE) {
125 incoming_rate = average_incoming_rate;
126 outgoing_rate = average_outgoing_rate;
127 }
128 else {
129 incoming_rate = maximum_incoming_rate;
130 outgoing_rate = maximum_outgoing_rate;
131 }
133 /* report incoming traffic in Bytes/sec */
134 if (incoming_rate < 1024) {
135 strcpy (incoming_speed_rating, "B/s");
136 adjusted_incoming_rate = (double) incoming_rate;
137 }
139 /* report incoming traffic in KBytes/sec */
140 else if (incoming_rate < (1024 * 1024)) {
141 strcpy (incoming_speed_rating, "KB/s");
142 adjusted_incoming_rate = (double) (incoming_rate / 1024.0);
143 }
145 /* report incoming traffic in MBytes/sec */
146 else {
147 strcpy (incoming_speed_rating, "MB/s");
148 adjusted_incoming_rate = (double) (incoming_rate / 1024.0 / 1024.0);
149 }
151 /* report outgoing traffic in Bytes/sec */
152 if (outgoing_rate < 1024) {
153 strcpy (outgoing_speed_rating, "B/s");
154 adjusted_outgoing_rate = (double) outgoing_rate;
155 }
157 /* report outgoing traffic in KBytes/sec */
158 else if (outgoing_rate < (1024 * 1024)) {
159 strcpy (outgoing_speed_rating, "KB/s");
160 adjusted_outgoing_rate = (double) (outgoing_rate / 1024.0);
161 }
163 /* report outgoing traffic in MBytes/sec */
164 else {
165 strcpy (outgoing_speed_rating, "MB/s");
166 adjusted_outgoing_rate = (double) (outgoing_rate / 1024.0 / 1024.0);
167 }
169 if (incoming_rate > incoming_critical_threshold
170 || outgoing_rate > outgoing_critical_threshold) {
171 result = STATE_CRITICAL;
172 }
173 else if (incoming_rate > incoming_warning_threshold
174 || outgoing_rate > outgoing_warning_threshold) {
175 result = STATE_WARNING;
176 }
178 asprintf (&error_message, _("%s. In = %0.1f %s, %s. Out = %0.1f %s|%s %s\n"),
179 (use_average == TRUE) ? _("Avg") : _("Max"), adjusted_incoming_rate,
180 incoming_speed_rating, (use_average == TRUE) ? _("Avg") : _("Max"),
181 adjusted_outgoing_rate, outgoing_speed_rating,
182 fperfdata("in", adjusted_incoming_rate, incoming_speed_rating,
183 (int)incoming_warning_threshold, incoming_warning_threshold,
184 (int)incoming_critical_threshold, incoming_critical_threshold,
185 TRUE, 0, FALSE, 0),
186 fperfdata("in", adjusted_outgoing_rate, outgoing_speed_rating,
187 (int)outgoing_warning_threshold, outgoing_warning_threshold,
188 (int)outgoing_critical_threshold, outgoing_critical_threshold,
189 TRUE, 0, FALSE, 0));
191 printf (_("Traffic %s - %s\n"), state_text(result), error_message);
193 return result;
194 }
195 \f
196 /* process command-line arguments */
197 int
198 process_arguments (int argc, char **argv)
199 {
200 int c;
202 int option = 0;
203 static struct option longopts[] = {
204 {"logfile", required_argument, 0, 'F'},
205 {"expires", required_argument, 0, 'e'},
206 {"aggregation", required_argument, 0, 'a'},
207 {"variable", required_argument, 0, 'v'},
208 {"critical", required_argument, 0, 'c'},
209 {"warning", required_argument, 0, 'w'},
210 {"verbose", no_argument, 0, 'v'},
211 {"version", no_argument, 0, 'V'},
212 {"help", no_argument, 0, 'h'},
213 {0, 0, 0, 0}
214 };
216 if (argc < 2)
217 return ERROR;
219 for (c = 1; c < argc; c++) {
220 if (strcmp ("-to", argv[c]) == 0)
221 strcpy (argv[c], "-t");
222 else if (strcmp ("-wt", argv[c]) == 0)
223 strcpy (argv[c], "-w");
224 else if (strcmp ("-ct", argv[c]) == 0)
225 strcpy (argv[c], "-c");
226 }
228 while (1) {
229 c = getopt_long (argc, argv, "hVF:e:a:c:w:", longopts, &option);
231 if (c == -1 || c == EOF)
232 break;
234 switch (c) {
235 case 'F': /* input file */
236 log_file = optarg;
237 break;
238 case 'e': /* expiration time */
239 expire_minutes = atoi (optarg);
240 break;
241 case 'a': /* aggregation (AVE or MAX) */
242 if (!strcmp (optarg, "MAX"))
243 use_average = FALSE;
244 else
245 use_average = TRUE;
246 break;
247 case 'c': /* warning threshold */
248 sscanf (optarg, "%lu,%lu", &incoming_critical_threshold,
249 &outgoing_critical_threshold);
250 break;
251 case 'w': /* critical threshold */
252 sscanf (optarg, "%lu,%lu", &incoming_warning_threshold,
253 &outgoing_warning_threshold);
254 break;
255 case 'V': /* version */
256 print_revision (progname, revision);
257 exit (STATE_OK);
258 case 'h': /* help */
259 print_help ();
260 exit (STATE_OK);
261 case '?': /* help */
262 usage (_("Invalid argument\n"));
263 }
264 }
266 c = optind;
267 if (argc > c && log_file == NULL) {
268 log_file = argv[c++];
269 }
271 if (argc > c && expire_minutes == -1) {
272 expire_minutes = atoi (argv[c++]);
273 }
275 if (argc > c && strcmp (argv[c], "MAX") == 0) {
276 use_average = FALSE;
277 c++;
278 }
279 else if (argc > c && strcmp (argv[c], "AVG") == 0) {
280 use_average = TRUE;
281 c++;
282 }
284 if (argc > c && incoming_warning_threshold == 0) {
285 incoming_warning_threshold = strtoul (argv[c++], NULL, 10);
286 }
288 if (argc > c && incoming_critical_threshold == 0) {
289 incoming_critical_threshold = strtoul (argv[c++], NULL, 10);
290 }
292 if (argc > c && outgoing_warning_threshold == 0) {
293 outgoing_warning_threshold = strtoul (argv[c++], NULL, 10);
294 }
296 if (argc > c && outgoing_critical_threshold == 0) {
297 outgoing_critical_threshold = strtoul (argv[c++], NULL, 10);
298 }
300 return validate_arguments ();
301 }
307 int
308 validate_arguments (void)
309 {
310 return OK;
311 }
317 \f
318 void
319 print_help (void)
320 {
321 print_revision (progname, revision);
323 printf (_("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n"));
324 printf (_(COPYRIGHT), copyright, email);
326 print_usage ();
328 printf (_(UT_HELP_VRSN));
330 printf (_("\
331 -F, --filename=STRING\n\
332 File to read log from\n\
333 -e, --expires=INTEGER\n\
334 Minutes after which log expires\n\
335 -a, --aggregation=(AVG|MAX)\n\
336 Test average or maximum\n\
337 -w, --warning\n\
338 Warning threshold pair \"<incoming>,<outgoing>\"\n\
339 -c, --critical\n\
340 Critical threshold pair \"<incoming>,<outgoing>\"\n"));
342 printf (_("\n\
343 This plugin will check the incoming/outgoing transfer rates of a router,\n\
344 switch, etc recorded in an MRTG log. If the newest log entry is older\n\
345 than <expire_minutes>, a WARNING status is returned. If either the\n\
346 incoming or outgoing rates exceed the <icl> or <ocl> thresholds (in\n\
347 Bytes/sec), a CRITICAL status results. If either of the rates exceed\n\
348 the <iwl> or <owl> thresholds (in Bytes/sec), a WARNING status results.\n\n"));
350 printf (_("Notes:\n\
351 - MRTG stands for Multi Router Traffic Grapher. It can be downloaded from\n\
352 http://ee-staff.ethz.ch/~oetiker/webtools/mrtg/mrtg.html\n\
353 - While MRTG can monitor things other than traffic rates, this\n\
354 plugin probably won't work with much else without modification.\n\
355 - The calculated i/o rates are a little off from what MRTG actually\n\
356 reports. I'm not sure why this is right now, but will look into it\n\
357 for future enhancements of this plugin.\n"));
359 printf (_(UT_SUPPORT));
360 }
365 void
366 print_usage (void)
367 {
368 printf (_("\
369 Usage: %s -F <log_file> -a <AVG | MAX> -v <variable> -w <warning_pair> -c <critical_pair>\n\
370 [-e expire_minutes] [-t timeout] [-v]\n"), progname);
371 printf (_(UT_HLP_VRS), progname, progname);
372 }