1 /*****************************************************************************
3 * Monitoring check_mrtgtraf plugin
6 * Copyright (c) 1999-2024 Monitoring Plugins Development Team
10 * This file contains the check_mtrgtraf plugin
12 * This plugin will check the incoming/outgoing transfer rates of a router
13 * switch, etc recorded in an MRTG log.
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 *****************************************************************************/
35 const char *progname
= "check_mrtgtraf";
36 const char *copyright
= "1999-2024";
37 const char *email
= "devel@monitoring-plugins.org";
39 static int process_arguments(int /*argc*/, char ** /*argv*/);
40 static void print_help(void);
41 void print_usage(void);
43 static char *log_file
= NULL
;
44 static int expire_minutes
= -1;
45 static bool use_average
= true;
46 static unsigned long incoming_warning_threshold
= 0L;
47 static unsigned long incoming_critical_threshold
= 0L;
48 static unsigned long outgoing_warning_threshold
= 0L;
49 static unsigned long outgoing_critical_threshold
= 0L;
51 int main(int argc
, char **argv
) {
52 setlocale(LC_ALL
, "");
53 bindtextdomain(PACKAGE
, LOCALEDIR
);
56 /* Parse extra opts if any */
57 argv
= np_extra_opts(&argc
, argv
, progname
);
59 if (process_arguments(argc
, argv
) == ERROR
)
60 usage4(_("Could not parse arguments"));
62 /* open the MRTG log file for reading */
63 FILE *mrtg_log_file_ptr
= fopen(log_file
, "r");
64 if (mrtg_log_file_ptr
== NULL
)
65 usage4(_("Unable to open MRTG log file"));
67 time_t timestamp
= 0L;
68 char input_buffer
[MAX_INPUT_BUFFER
];
69 unsigned long average_incoming_rate
= 0L;
70 unsigned long average_outgoing_rate
= 0L;
71 unsigned long maximum_incoming_rate
= 0L;
72 unsigned long maximum_outgoing_rate
= 0L;
74 while (fgets(input_buffer
, MAX_INPUT_BUFFER
- 1, mrtg_log_file_ptr
)) {
78 /* skip the first line of the log file */
82 /* break out of read loop */
83 /* if we've passed the number of entries we want to read */
87 /* grab the timestamp */
88 char *temp_buffer
= strtok(input_buffer
, " ");
89 timestamp
= strtoul(temp_buffer
, NULL
, 10);
91 /* grab the average incoming transfer rate */
92 temp_buffer
= strtok(NULL
, " ");
93 average_incoming_rate
= strtoul(temp_buffer
, NULL
, 10);
95 /* grab the average outgoing transfer rate */
96 temp_buffer
= strtok(NULL
, " ");
97 average_outgoing_rate
= strtoul(temp_buffer
, NULL
, 10);
99 /* grab the maximum incoming transfer rate */
100 temp_buffer
= strtok(NULL
, " ");
101 maximum_incoming_rate
= strtoul(temp_buffer
, NULL
, 10);
103 /* grab the maximum outgoing transfer rate */
104 temp_buffer
= strtok(NULL
, " ");
105 maximum_outgoing_rate
= strtoul(temp_buffer
, NULL
, 10);
108 /* close the log file */
109 fclose(mrtg_log_file_ptr
);
111 /* if we couldn't read enough data, return an unknown error */
113 usage4(_("Unable to process MRTG log file"));
115 /* make sure the MRTG data isn't too old */
118 if ((expire_minutes
> 0) && (current_time
- timestamp
) > (expire_minutes
* 60))
119 die(STATE_WARNING
, _("MRTG data has expired (%d minutes old)\n"), (int)((current_time
- timestamp
) / 60));
121 unsigned long incoming_rate
= 0L;
122 unsigned long outgoing_rate
= 0L;
123 /* else check the incoming/outgoing rates */
125 incoming_rate
= average_incoming_rate
;
126 outgoing_rate
= average_outgoing_rate
;
128 incoming_rate
= maximum_incoming_rate
;
129 outgoing_rate
= maximum_outgoing_rate
;
132 double adjusted_incoming_rate
= 0.0;
133 char incoming_speed_rating
[8];
134 /* report incoming traffic in Bytes/sec */
135 if (incoming_rate
< 1024) {
136 strcpy(incoming_speed_rating
, "B");
137 adjusted_incoming_rate
= (double)incoming_rate
;
140 /* report incoming traffic in KBytes/sec */
141 else if (incoming_rate
< (1024 * 1024)) {
142 strcpy(incoming_speed_rating
, "KB");
143 adjusted_incoming_rate
= (double)(incoming_rate
/ 1024.0);
146 /* report incoming traffic in MBytes/sec */
148 strcpy(incoming_speed_rating
, "MB");
149 adjusted_incoming_rate
= (double)(incoming_rate
/ 1024.0 / 1024.0);
152 double adjusted_outgoing_rate
= 0.0;
153 char outgoing_speed_rating
[8];
154 /* report outgoing traffic in Bytes/sec */
155 if (outgoing_rate
< 1024) {
156 strcpy(outgoing_speed_rating
, "B");
157 adjusted_outgoing_rate
= (double)outgoing_rate
;
160 /* report outgoing traffic in KBytes/sec */
161 else if (outgoing_rate
< (1024 * 1024)) {
162 strcpy(outgoing_speed_rating
, "KB");
163 adjusted_outgoing_rate
= (double)(outgoing_rate
/ 1024.0);
166 /* report outgoing traffic in MBytes/sec */
168 strcpy(outgoing_speed_rating
, "MB");
169 adjusted_outgoing_rate
= (double)(outgoing_rate
/ 1024.0 / 1024.0);
172 int result
= STATE_OK
;
173 if (incoming_rate
> incoming_critical_threshold
|| outgoing_rate
> outgoing_critical_threshold
) {
174 result
= STATE_CRITICAL
;
175 } else if (incoming_rate
> incoming_warning_threshold
|| outgoing_rate
> outgoing_warning_threshold
) {
176 result
= STATE_WARNING
;
180 xasprintf(&error_message
, _("%s. In = %0.1f %s/s, %s. Out = %0.1f %s/s|%s %s\n"), (use_average
) ? _("Avg") : _("Max"),
181 adjusted_incoming_rate
, incoming_speed_rating
, (use_average
) ? _("Avg") : _("Max"), adjusted_outgoing_rate
,
182 outgoing_speed_rating
,
183 fperfdata("in", adjusted_incoming_rate
, incoming_speed_rating
, (int)incoming_warning_threshold
, incoming_warning_threshold
,
184 (int)incoming_critical_threshold
, incoming_critical_threshold
, true, 0, false, 0),
185 fperfdata("out", adjusted_outgoing_rate
, outgoing_speed_rating
, (int)outgoing_warning_threshold
, outgoing_warning_threshold
,
186 (int)outgoing_critical_threshold
, outgoing_critical_threshold
, true, 0, false, 0));
188 printf(_("Traffic %s - %s\n"), state_text(result
), error_message
);
193 /* process command-line arguments */
194 int process_arguments(int argc
, char **argv
) {
195 static struct option longopts
[] = {{"filename", required_argument
, 0, 'F'},
196 {"expires", required_argument
, 0, 'e'},
197 {"aggregation", required_argument
, 0, 'a'},
198 {"critical", required_argument
, 0, 'c'},
199 {"warning", required_argument
, 0, 'w'},
200 {"version", no_argument
, 0, 'V'},
201 {"help", no_argument
, 0, 'h'},
207 for (int i
= 1; i
< argc
; i
++) {
208 if (strcmp("-to", argv
[i
]) == 0)
209 strcpy(argv
[i
], "-t");
210 else if (strcmp("-wt", argv
[i
]) == 0)
211 strcpy(argv
[i
], "-w");
212 else if (strcmp("-ct", argv
[i
]) == 0)
213 strcpy(argv
[i
], "-c");
219 option_char
= getopt_long(argc
, argv
, "hVF:e:a:c:w:", longopts
, &option
);
221 if (option_char
== -1 || option_char
== EOF
)
224 switch (option_char
) {
225 case 'F': /* input file */
228 case 'e': /* expiration time */
229 expire_minutes
= atoi(optarg
);
231 case 'a': /* aggregation (AVE or MAX) */
232 if (!strcmp(optarg
, "MAX"))
237 case 'c': /* warning threshold */
238 sscanf(optarg
, "%lu,%lu", &incoming_critical_threshold
, &outgoing_critical_threshold
);
240 case 'w': /* critical threshold */
241 sscanf(optarg
, "%lu,%lu", &incoming_warning_threshold
, &outgoing_warning_threshold
);
243 case 'V': /* version */
244 print_revision(progname
, NP_VERSION
);
254 option_char
= optind
;
255 if (argc
> option_char
&& log_file
== NULL
) {
256 log_file
= argv
[option_char
++];
259 if (argc
> option_char
&& expire_minutes
== -1) {
260 expire_minutes
= atoi(argv
[option_char
++]);
263 if (argc
> option_char
&& strcmp(argv
[option_char
], "MAX") == 0) {
266 } else if (argc
> option_char
&& strcmp(argv
[option_char
], "AVG") == 0) {
271 if (argc
> option_char
&& incoming_warning_threshold
== 0) {
272 incoming_warning_threshold
= strtoul(argv
[option_char
++], NULL
, 10);
275 if (argc
> option_char
&& incoming_critical_threshold
== 0) {
276 incoming_critical_threshold
= strtoul(argv
[option_char
++], NULL
, 10);
279 if (argc
> option_char
&& outgoing_warning_threshold
== 0) {
280 outgoing_warning_threshold
= strtoul(argv
[option_char
++], NULL
, 10);
283 if (argc
> option_char
&& outgoing_critical_threshold
== 0) {
284 outgoing_critical_threshold
= strtoul(argv
[option_char
++], NULL
, 10);
290 void print_help(void) {
291 print_revision(progname
, NP_VERSION
);
293 printf("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n");
294 printf(COPYRIGHT
, copyright
, email
);
296 printf("%s\n", _("This plugin will check the incoming/outgoing transfer rates of a router,"));
297 printf("%s\n", _("switch, etc recorded in an MRTG log. If the newest log entry is older"));
298 printf("%s\n", _("than <expire_minutes>, a WARNING status is returned. If either the"));
299 printf("%s\n", _("incoming or outgoing rates exceed the <icl> or <ocl> thresholds (in"));
300 printf("%s\n", _("Bytes/sec), a CRITICAL status results. If either of the rates exceed"));
301 printf("%s\n", _("the <iwl> or <owl> thresholds (in Bytes/sec), a WARNING status results."));
307 printf(UT_HELP_VRSN
);
308 printf(UT_EXTRA_OPTS
);
310 printf(" %s\n", "-F, --filename=STRING");
311 printf(" %s\n", _("File to read log from"));
312 printf(" %s\n", "-e, --expires=INTEGER");
313 printf(" %s\n", _("Minutes after which log expires"));
314 printf(" %s\n", "-a, --aggregation=(AVG|MAX)");
315 printf(" %s\n", _("Test average or maximum"));
316 printf(" %s\n", "-w, --warning");
317 printf(" %s\n", _("Warning threshold pair <incoming>,<outgoing>"));
318 printf(" %s\n", "-c, --critical");
319 printf(" %s\n", _("Critical threshold pair <incoming>,<outgoing>"));
322 printf("%s\n", _("Notes:"));
323 printf(" %s\n", _("- MRTG stands for Multi Router Traffic Grapher. It can be downloaded from"));
324 printf(" %s\n", " http://ee-staff.ethz.ch/~oetiker/webtools/mrtg/mrtg.html");
325 printf(" %s\n", _("- While MRTG can monitor things other than traffic rates, this"));
326 printf(" %s\n", _(" plugin probably won't work with much else without modification."));
327 printf(" %s\n", _("- The calculated i/o rates are a little off from what MRTG actually"));
328 printf(" %s\n", _(" reports. I'm not sure why this is right now, but will look into it"));
329 printf(" %s\n", _(" for future enhancements of this plugin."));
334 void print_usage(void) {
336 printf(" %s -F <log_file> -a <AVG | MAX> -w <warning_pair>\n", progname
);
337 printf("-c <critical_pair> [-e expire_minutes]\n");