summaryrefslogtreecommitdiffstats
path: root/plugins/check_mrtg.c
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/check_mrtg.c')
-rw-r--r--plugins/check_mrtg.c226
1 files changed, 134 insertions, 92 deletions
diff --git a/plugins/check_mrtg.c b/plugins/check_mrtg.c
index 632e66fb..4a17049a 100644
--- a/plugins/check_mrtg.c
+++ b/plugins/check_mrtg.c
@@ -35,21 +35,18 @@ const char *email = "devel@monitoring-plugins.org";
35 35
36#include "common.h" 36#include "common.h"
37#include "utils.h" 37#include "utils.h"
38#include "check_mrtg.d/config.h"
39
40typedef struct {
41 int errorcode;
42 check_mrtg_config config;
43} check_mrtg_config_wrapper;
44static check_mrtg_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/);
45static check_mrtg_config_wrapper validate_arguments(check_mrtg_config_wrapper /*config_wrapper*/);
38 46
39static int process_arguments(int /*argc*/, char ** /*argv*/);
40static int validate_arguments(void);
41static void print_help(void); 47static void print_help(void);
42void print_usage(void); 48void print_usage(void);
43 49
44static char *log_file = NULL;
45static int expire_minutes = 0;
46static bool use_average = true;
47static int variable_number = -1;
48static unsigned long value_warning_threshold = 0L;
49static unsigned long value_critical_threshold = 0L;
50static char *label;
51static char *units;
52
53int main(int argc, char **argv) { 50int main(int argc, char **argv) {
54 setlocale(LC_ALL, ""); 51 setlocale(LC_ALL, "");
55 bindtextdomain(PACKAGE, LOCALEDIR); 52 bindtextdomain(PACKAGE, LOCALEDIR);
@@ -58,32 +55,37 @@ int main(int argc, char **argv) {
58 /* Parse extra opts if any */ 55 /* Parse extra opts if any */
59 argv = np_extra_opts(&argc, argv, progname); 56 argv = np_extra_opts(&argc, argv, progname);
60 57
61 if (process_arguments(argc, argv) == ERROR) 58 check_mrtg_config_wrapper tmp_config = process_arguments(argc, argv);
59 if (tmp_config.errorcode == ERROR) {
62 usage4(_("Could not parse arguments\n")); 60 usage4(_("Could not parse arguments\n"));
61 }
62
63 const check_mrtg_config config = tmp_config.config;
63 64
64 /* open the MRTG log file for reading */ 65 /* open the MRTG log file for reading */
65 FILE *mtrg_log_file = fopen(log_file, "r"); 66 FILE *mtrg_log_file = fopen(config.log_file, "r");
66 if (mtrg_log_file == NULL) { 67 if (mtrg_log_file == NULL) {
67 printf(_("Unable to open MRTG log file\n")); 68 printf(_("Unable to open MRTG log file\n"));
68 return STATE_UNKNOWN; 69 return STATE_UNKNOWN;
69 } 70 }
70 71
71 time_t timestamp = 0L; 72 time_t timestamp = 0;
72 unsigned long average_value_rate = 0L; 73 unsigned long average_value_rate = 0;
73 unsigned long maximum_value_rate = 0L; 74 unsigned long maximum_value_rate = 0;
74 char input_buffer[MAX_INPUT_BUFFER]; 75 char input_buffer[MAX_INPUT_BUFFER];
75 int line = 0; 76 int line = 0;
76 while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, mtrg_log_file)) { 77 while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, mtrg_log_file)) {
77
78 line++; 78 line++;
79 79
80 /* skip the first line of the log file */ 80 /* skip the first line of the log file */
81 if (line == 1) 81 if (line == 1) {
82 continue; 82 continue;
83 }
83 84
84 /* break out of read loop if we've passed the number of entries we want to read */ 85 /* break out of read loop if we've passed the number of entries we want to read */
85 if (line > 2) 86 if (line > 2) {
86 break; 87 break;
88 }
87 89
88 /* grab the timestamp */ 90 /* grab the timestamp */
89 char *temp_buffer = strtok(input_buffer, " "); 91 char *temp_buffer = strtok(input_buffer, " ");
@@ -91,23 +93,27 @@ int main(int argc, char **argv) {
91 93
92 /* grab the average value 1 rate */ 94 /* grab the average value 1 rate */
93 temp_buffer = strtok(NULL, " "); 95 temp_buffer = strtok(NULL, " ");
94 if (variable_number == 1) 96 if (config.variable_number == 1) {
95 average_value_rate = strtoul(temp_buffer, NULL, 10); 97 average_value_rate = strtoul(temp_buffer, NULL, 10);
98 }
96 99
97 /* grab the average value 2 rate */ 100 /* grab the average value 2 rate */
98 temp_buffer = strtok(NULL, " "); 101 temp_buffer = strtok(NULL, " ");
99 if (variable_number == 2) 102 if (config.variable_number == 2) {
100 average_value_rate = strtoul(temp_buffer, NULL, 10); 103 average_value_rate = strtoul(temp_buffer, NULL, 10);
104 }
101 105
102 /* grab the maximum value 1 rate */ 106 /* grab the maximum value 1 rate */
103 temp_buffer = strtok(NULL, " "); 107 temp_buffer = strtok(NULL, " ");
104 if (variable_number == 1) 108 if (config.variable_number == 1) {
105 maximum_value_rate = strtoul(temp_buffer, NULL, 10); 109 maximum_value_rate = strtoul(temp_buffer, NULL, 10);
110 }
106 111
107 /* grab the maximum value 2 rate */ 112 /* grab the maximum value 2 rate */
108 temp_buffer = strtok(NULL, " "); 113 temp_buffer = strtok(NULL, " ");
109 if (variable_number == 2) 114 if (config.variable_number == 2) {
110 maximum_value_rate = strtoul(temp_buffer, NULL, 10); 115 maximum_value_rate = strtoul(temp_buffer, NULL, 10);
116 }
111 } 117 }
112 118
113 /* close the log file */ 119 /* close the log file */
@@ -122,49 +128,69 @@ int main(int argc, char **argv) {
122 /* make sure the MRTG data isn't too old */ 128 /* make sure the MRTG data isn't too old */
123 time_t current_time; 129 time_t current_time;
124 time(&current_time); 130 time(&current_time);
125 if (expire_minutes > 0 && (current_time - timestamp) > (expire_minutes * 60)) { 131 if (config.expire_minutes > 0 && (current_time - timestamp) > (config.expire_minutes * 60)) {
126 printf(_("MRTG data has expired (%d minutes old)\n"), (int)((current_time - timestamp) / 60)); 132 printf(_("MRTG data has expired (%d minutes old)\n"),
133 (int)((current_time - timestamp) / 60));
127 return STATE_WARNING; 134 return STATE_WARNING;
128 } 135 }
129 136
130 unsigned long rate = 0L; 137 unsigned long rate = 0L;
131 /* else check the incoming/outgoing rates */ 138 /* else check the incoming/outgoing rates */
132 if (use_average) 139 if (config.use_average) {
133 rate = average_value_rate; 140 rate = average_value_rate;
134 else 141 } else {
135 rate = maximum_value_rate; 142 rate = maximum_value_rate;
143 }
136 144
137 int result = STATE_OK; 145 int result = STATE_OK;
138 if (rate > value_critical_threshold) 146 if (config.value_critical_threshold_set && rate > config.value_critical_threshold) {
139 result = STATE_CRITICAL; 147 result = STATE_CRITICAL;
140 else if (rate > value_warning_threshold) 148 } else if (config.value_warning_threshold_set && rate > config.value_warning_threshold) {
141 result = STATE_WARNING; 149 result = STATE_WARNING;
150 }
142 151
143 printf("%s. %s = %lu %s|%s\n", (use_average) ? _("Avg") : _("Max"), label, rate, units, 152 printf("%s. %s = %lu %s|%s\n", (config.use_average) ? _("Avg") : _("Max"), config.label, rate,
144 perfdata(label, (long)rate, units, (int)value_warning_threshold, (long)value_warning_threshold, (int)value_critical_threshold, 153 config.units,
145 (long)value_critical_threshold, 0, 0, 0, 0)); 154 perfdata(config.label, (long)rate, config.units, config.value_warning_threshold_set,
155 (long)config.value_warning_threshold, config.value_critical_threshold_set,
156 (long)config.value_critical_threshold, 0, 0, 0, 0));
146 157
147 return result; 158 return result;
148} 159}
149 160
150/* process command-line arguments */ 161/* process command-line arguments */
151int process_arguments(int argc, char **argv) { 162check_mrtg_config_wrapper process_arguments(int argc, char **argv) {
152 static struct option longopts[] = { 163 static struct option longopts[] = {{"logfile", required_argument, 0, 'F'},
153 {"logfile", required_argument, 0, 'F'}, {"expires", required_argument, 0, 'e'}, {"aggregation", required_argument, 0, 'a'}, 164 {"expires", required_argument, 0, 'e'},
154 {"variable", required_argument, 0, 'v'}, {"critical", required_argument, 0, 'c'}, {"warning", required_argument, 0, 'w'}, 165 {"aggregation", required_argument, 0, 'a'},
155 {"label", required_argument, 0, 'l'}, {"units", required_argument, 0, 'u'}, {"variable", required_argument, 0, 'v'}, 166 {"variable", required_argument, 0, 'v'},
156 {"version", no_argument, 0, 'V'}, {"help", no_argument, 0, 'h'}, {0, 0, 0, 0}}; 167 {"critical", required_argument, 0, 'c'},
157 168 {"warning", required_argument, 0, 'w'},
158 if (argc < 2) 169 {"label", required_argument, 0, 'l'},
159 return ERROR; 170 {"units", required_argument, 0, 'u'},
171 {"variable", required_argument, 0, 'v'},
172 {"version", no_argument, 0, 'V'},
173 {"help", no_argument, 0, 'h'},
174 {0, 0, 0, 0}};
175
176 check_mrtg_config_wrapper result = {
177 .errorcode = OK,
178 .config = check_mrtg_config_init(),
179 };
180
181 if (argc < 2) {
182 result.errorcode = ERROR;
183 return result;
184 }
160 185
161 for (int i = 1; i < argc; i++) { 186 for (int i = 1; i < argc; i++) {
162 if (strcmp("-to", argv[i]) == 0) 187 if (strcmp("-to", argv[i]) == 0) {
163 strcpy(argv[i], "-t"); 188 strcpy(argv[i], "-t");
164 else if (strcmp("-wt", argv[i]) == 0) 189 } else if (strcmp("-wt", argv[i]) == 0) {
165 strcpy(argv[i], "-w"); 190 strcpy(argv[i], "-w");
166 else if (strcmp("-ct", argv[i]) == 0) 191 } else if (strcmp("-ct", argv[i]) == 0) {
167 strcpy(argv[i], "-c"); 192 strcpy(argv[i], "-c");
193 }
168 } 194 }
169 195
170 int option_char; 196 int option_char;
@@ -172,38 +198,39 @@ int process_arguments(int argc, char **argv) {
172 while (1) { 198 while (1) {
173 option_char = getopt_long(argc, argv, "hVF:e:a:v:c:w:l:u:", longopts, &option); 199 option_char = getopt_long(argc, argv, "hVF:e:a:v:c:w:l:u:", longopts, &option);
174 200
175 if (option_char == -1 || option_char == EOF) 201 if (option_char == -1 || option_char == EOF) {
176 break; 202 break;
203 }
177 204
178 switch (option_char) { 205 switch (option_char) {
179 case 'F': /* input file */ 206 case 'F': /* input file */
180 log_file = optarg; 207 result.config.log_file = optarg;
181 break; 208 break;
182 case 'e': /* ups name */ 209 case 'e': /* ups name */
183 expire_minutes = atoi(optarg); 210 result.config.expire_minutes = atoi(optarg);
184 break; 211 break;
185 case 'a': /* port */ 212 case 'a': /* port */
186 if (!strcmp(optarg, "MAX")) 213 result.config.use_average = (bool)(strcmp(optarg, "MAX"));
187 use_average = false;
188 else
189 use_average = true;
190 break; 214 break;
191 case 'v': 215 case 'v':
192 variable_number = atoi(optarg); 216 result.config.variable_number = atoi(optarg);
193 if (variable_number < 1 || variable_number > 2) 217 if (result.config.variable_number < 1 || result.config.variable_number > 2) {
194 usage4(_("Invalid variable number")); 218 usage4(_("Invalid variable number"));
219 }
195 break; 220 break;
196 case 'w': /* critical time threshold */ 221 case 'w': /* critical time threshold */
197 value_warning_threshold = strtoul(optarg, NULL, 10); 222 result.config.value_warning_threshold_set = true;
223 result.config.value_warning_threshold = strtoul(optarg, NULL, 10);
198 break; 224 break;
199 case 'c': /* warning time threshold */ 225 case 'c': /* warning time threshold */
200 value_critical_threshold = strtoul(optarg, NULL, 10); 226 result.config.value_critical_threshold_set = true;
227 result.config.value_critical_threshold = strtoul(optarg, NULL, 10);
201 break; 228 break;
202 case 'l': /* label */ 229 case 'l': /* label */
203 label = optarg; 230 result.config.label = optarg;
204 break; 231 break;
205 case 'u': /* timeout */ 232 case 'u': /* timeout */
206 units = optarg; 233 result.config.units = optarg;
207 break; 234 break;
208 case 'V': /* version */ 235 case 'V': /* version */
209 print_revision(progname, NP_VERSION); 236 print_revision(progname, NP_VERSION);
@@ -217,63 +244,71 @@ int process_arguments(int argc, char **argv) {
217 } 244 }
218 245
219 option_char = optind; 246 option_char = optind;
220 if (log_file == NULL && argc > option_char) { 247 if (result.config.log_file == NULL && argc > option_char) {
221 log_file = argv[option_char++]; 248 result.config.log_file = argv[option_char++];
222 } 249 }
223 250
224 if (expire_minutes <= 0 && argc > option_char) { 251 if (result.config.expire_minutes <= 0 && argc > option_char) {
225 if (is_intpos(argv[option_char])) 252 if (is_intpos(argv[option_char])) {
226 expire_minutes = atoi(argv[option_char++]); 253 result.config.expire_minutes = atoi(argv[option_char++]);
227 else 254 } else {
228 die(STATE_UNKNOWN, _("%s is not a valid expiration time\nUse '%s -h' for additional help\n"), argv[option_char], progname); 255 die(STATE_UNKNOWN,
256 _("%s is not a valid expiration time\nUse '%s -h' for additional help\n"),
257 argv[option_char], progname);
258 }
229 } 259 }
230 260
231 if (argc > option_char && strcmp(argv[option_char], "MAX") == 0) { 261 if (argc > option_char && strcmp(argv[option_char], "MAX") == 0) {
232 use_average = false; 262 result.config.use_average = false;
233 option_char++; 263 option_char++;
234 } else if (argc > option_char && strcmp(argv[option_char], "AVG") == 0) { 264 } else if (argc > option_char && strcmp(argv[option_char], "AVG") == 0) {
235 use_average = true; 265 result.config.use_average = true;
236 option_char++; 266 option_char++;
237 } 267 }
238 268
239 if (argc > option_char && variable_number == -1) { 269 if (argc > option_char && result.config.variable_number == -1) {
240 variable_number = atoi(argv[option_char++]); 270 result.config.variable_number = atoi(argv[option_char++]);
241 if (variable_number < 1 || variable_number > 2) { 271 if (result.config.variable_number < 1 || result.config.variable_number > 2) {
242 printf("%s :", argv[option_char]); 272 printf("%s :", argv[option_char]);
243 usage(_("Invalid variable number\n")); 273 usage(_("Invalid variable number\n"));
244 } 274 }
245 } 275 }
246 276
247 if (argc > option_char && value_warning_threshold == 0) { 277 if (argc > option_char && !result.config.value_warning_threshold_set) {
248 value_warning_threshold = strtoul(argv[option_char++], NULL, 10); 278 result.config.value_warning_threshold_set = true;
279 result.config.value_warning_threshold = strtoul(argv[option_char++], NULL, 10);
249 } 280 }
250 281
251 if (argc > option_char && value_critical_threshold == 0) { 282 if (argc > option_char && !result.config.value_critical_threshold_set) {
252 value_critical_threshold = strtoul(argv[option_char++], NULL, 10); 283 result.config.value_critical_threshold_set = true;
284 result.config.value_critical_threshold = strtoul(argv[option_char++], NULL, 10);
253 } 285 }
254 286
255 if (argc > option_char && strlen(label) == 0) { 287 if (argc > option_char && strlen(result.config.label) == 0) {
256 label = argv[option_char++]; 288 result.config.label = argv[option_char++];
257 } 289 }
258 290
259 if (argc > option_char && strlen(units) == 0) { 291 if (argc > option_char && strlen(result.config.units) == 0) {
260 units = argv[option_char++]; 292 result.config.units = argv[option_char++];
261 } 293 }
262 294
263 return validate_arguments(); 295 return validate_arguments(result);
264} 296}
265 297
266int validate_arguments(void) { 298check_mrtg_config_wrapper validate_arguments(check_mrtg_config_wrapper config_wrapper) {
267 if (variable_number == -1) 299 if (config_wrapper.config.variable_number == -1) {
268 usage4(_("You must supply the variable number")); 300 usage4(_("You must supply the variable number"));
301 }
269 302
270 if (label == NULL) 303 if (config_wrapper.config.label == NULL) {
271 label = strdup("value"); 304 config_wrapper.config.label = strdup("value");
305 }
272 306
273 if (units == NULL) 307 if (config_wrapper.config.units == NULL) {
274 units = strdup(""); 308 config_wrapper.config.units = strdup("");
309 }
275 310
276 return OK; 311 return config_wrapper;
277} 312}
278 313
279void print_help(void) { 314void print_help(void) {
@@ -311,25 +346,32 @@ void print_help(void) {
311 printf(" %s\n", _("\"Bytes Per Second\", \"%% Utilization\")")); 346 printf(" %s\n", _("\"Bytes Per Second\", \"%% Utilization\")"));
312 347
313 printf("\n"); 348 printf("\n");
314 printf(" %s\n", _("If the value exceeds the <vwl> threshold, a WARNING status is returned. If")); 349 printf(" %s\n",
350 _("If the value exceeds the <vwl> threshold, a WARNING status is returned. If"));
315 printf(" %s\n", _("the value exceeds the <vcl> threshold, a CRITICAL status is returned. If")); 351 printf(" %s\n", _("the value exceeds the <vcl> threshold, a CRITICAL status is returned. If"));
316 printf(" %s\n", _("the data in the log file is older than <expire_minutes> old, a WARNING")); 352 printf(" %s\n", _("the data in the log file is older than <expire_minutes> old, a WARNING"));
317 printf(" %s\n", _("status is returned and a warning message is printed.")); 353 printf(" %s\n", _("status is returned and a warning message is printed."));
318 354
319 printf("\n"); 355 printf("\n");
320 printf(" %s\n", _("This plugin is useful for monitoring MRTG data that does not correspond to")); 356 printf(" %s\n",
321 printf(" %s\n", _("bandwidth usage. (Use the check_mrtgtraf plugin for monitoring bandwidth).")); 357 _("This plugin is useful for monitoring MRTG data that does not correspond to"));
322 printf(" %s\n", _("It can be used to monitor any kind of data that MRTG is monitoring - errors,")); 358 printf(" %s\n",
323 printf(" %s\n", _("packets/sec, etc. I use MRTG in conjunction with the Novell NLM that allows")); 359 _("bandwidth usage. (Use the check_mrtgtraf plugin for monitoring bandwidth)."));
360 printf(" %s\n",
361 _("It can be used to monitor any kind of data that MRTG is monitoring - errors,"));
362 printf(" %s\n",
363 _("packets/sec, etc. I use MRTG in conjunction with the Novell NLM that allows"));
324 printf(" %s\n", _("me to track processor utilization, user connections, drive space, etc and")); 364 printf(" %s\n", _("me to track processor utilization, user connections, drive space, etc and"));
325 printf(" %s\n\n", _("this plugin works well for monitoring that kind of data as well.")); 365 printf(" %s\n\n", _("this plugin works well for monitoring that kind of data as well."));
326 366
327 printf("%s\n", _("Notes:")); 367 printf("%s\n", _("Notes:"));
328 printf(" %s\n", _("- This plugin only monitors one of the two variables stored in the MRTG log")); 368 printf(" %s\n",
369 _("- This plugin only monitors one of the two variables stored in the MRTG log"));
329 printf(" %s\n", _("file. If you want to monitor both values you will have to define two")); 370 printf(" %s\n", _("file. If you want to monitor both values you will have to define two"));
330 printf(" %s\n", _("commands with different values for the <variable> argument. Of course,")); 371 printf(" %s\n", _("commands with different values for the <variable> argument. Of course,"));
331 printf(" %s\n", _("you can always hack the code to make this plugin work for you...")); 372 printf(" %s\n", _("you can always hack the code to make this plugin work for you..."));
332 printf(" %s\n", _("- MRTG stands for the Multi Router Traffic Grapher. It can be downloaded from")); 373 printf(" %s\n",
374 _("- MRTG stands for the Multi Router Traffic Grapher. It can be downloaded from"));
333 printf(" %s\n", "http://ee-staff.ethz.ch/~oetiker/webtools/mrtg/mrtg.html"); 375 printf(" %s\n", "http://ee-staff.ethz.ch/~oetiker/webtools/mrtg/mrtg.html");
334 376
335 printf(UT_SUPPORT); 377 printf(UT_SUPPORT);