diff options
Diffstat (limited to 'plugins/check_mrtgtraf.c')
| -rw-r--r-- | plugins/check_mrtgtraf.c | 146 |
1 files changed, 84 insertions, 62 deletions
diff --git a/plugins/check_mrtgtraf.c b/plugins/check_mrtgtraf.c index e5a2e2ad..10ce936f 100644 --- a/plugins/check_mrtgtraf.c +++ b/plugins/check_mrtgtraf.c | |||
| @@ -29,25 +29,23 @@ | |||
| 29 | * | 29 | * |
| 30 | *****************************************************************************/ | 30 | *****************************************************************************/ |
| 31 | 31 | ||
| 32 | #include "common.h" | ||
| 33 | #include "utils.h" | ||
| 34 | |||
| 35 | const char *progname = "check_mrtgtraf"; | 32 | const char *progname = "check_mrtgtraf"; |
| 36 | const char *copyright = "1999-2024"; | 33 | const char *copyright = "1999-2024"; |
| 37 | const char *email = "devel@monitoring-plugins.org"; | 34 | const char *email = "devel@monitoring-plugins.org"; |
| 38 | 35 | ||
| 39 | static int process_arguments(int /*argc*/, char ** /*argv*/); | 36 | #include "check_mrtgtraf.d/config.h" |
| 37 | #include "common.h" | ||
| 38 | #include "utils.h" | ||
| 39 | |||
| 40 | typedef struct { | ||
| 41 | int errorcode; | ||
| 42 | check_mrtgtraf_config config; | ||
| 43 | } check_mrtgtraf_config_wrapper; | ||
| 44 | |||
| 45 | static check_mrtgtraf_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/); | ||
| 40 | static void print_help(void); | 46 | static void print_help(void); |
| 41 | void print_usage(void); | 47 | void print_usage(void); |
| 42 | 48 | ||
| 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; | ||
| 50 | |||
| 51 | int main(int argc, char **argv) { | 49 | int main(int argc, char **argv) { |
| 52 | setlocale(LC_ALL, ""); | 50 | setlocale(LC_ALL, ""); |
| 53 | bindtextdomain(PACKAGE, LOCALEDIR); | 51 | bindtextdomain(PACKAGE, LOCALEDIR); |
| @@ -56,13 +54,18 @@ int main(int argc, char **argv) { | |||
| 56 | /* Parse extra opts if any */ | 54 | /* Parse extra opts if any */ |
| 57 | argv = np_extra_opts(&argc, argv, progname); | 55 | argv = np_extra_opts(&argc, argv, progname); |
| 58 | 56 | ||
| 59 | if (process_arguments(argc, argv) == ERROR) | 57 | check_mrtgtraf_config_wrapper tmp_config = process_arguments(argc, argv); |
| 58 | if (tmp_config.errorcode == ERROR) { | ||
| 60 | usage4(_("Could not parse arguments")); | 59 | usage4(_("Could not parse arguments")); |
| 60 | } | ||
| 61 | |||
| 62 | const check_mrtgtraf_config config = tmp_config.config; | ||
| 61 | 63 | ||
| 62 | /* open the MRTG log file for reading */ | 64 | /* open the MRTG log file for reading */ |
| 63 | FILE *mrtg_log_file_ptr = fopen(log_file, "r"); | 65 | FILE *mrtg_log_file_ptr = fopen(config.log_file, "r"); |
| 64 | if (mrtg_log_file_ptr == NULL) | 66 | if (mrtg_log_file_ptr == NULL) { |
| 65 | usage4(_("Unable to open MRTG log file")); | 67 | usage4(_("Unable to open MRTG log file")); |
| 68 | } | ||
| 66 | 69 | ||
| 67 | time_t timestamp = 0L; | 70 | time_t timestamp = 0L; |
| 68 | char input_buffer[MAX_INPUT_BUFFER]; | 71 | char input_buffer[MAX_INPUT_BUFFER]; |
| @@ -76,13 +79,15 @@ int main(int argc, char **argv) { | |||
| 76 | line++; | 79 | line++; |
| 77 | 80 | ||
| 78 | /* skip the first line of the log file */ | 81 | /* skip the first line of the log file */ |
| 79 | if (line == 1) | 82 | if (line == 1) { |
| 80 | continue; | 83 | continue; |
| 84 | } | ||
| 81 | 85 | ||
| 82 | /* break out of read loop */ | 86 | /* break out of read loop */ |
| 83 | /* if we've passed the number of entries we want to read */ | 87 | /* if we've passed the number of entries we want to read */ |
| 84 | if (line > 2) | 88 | if (line > 2) { |
| 85 | break; | 89 | break; |
| 90 | } | ||
| 86 | 91 | ||
| 87 | /* grab the timestamp */ | 92 | /* grab the timestamp */ |
| 88 | char *temp_buffer = strtok(input_buffer, " "); | 93 | char *temp_buffer = strtok(input_buffer, " "); |
| @@ -109,19 +114,22 @@ int main(int argc, char **argv) { | |||
| 109 | fclose(mrtg_log_file_ptr); | 114 | fclose(mrtg_log_file_ptr); |
| 110 | 115 | ||
| 111 | /* if we couldn't read enough data, return an unknown error */ | 116 | /* if we couldn't read enough data, return an unknown error */ |
| 112 | if (line <= 2) | 117 | if (line <= 2) { |
| 113 | usage4(_("Unable to process MRTG log file")); | 118 | usage4(_("Unable to process MRTG log file")); |
| 119 | } | ||
| 114 | 120 | ||
| 115 | /* make sure the MRTG data isn't too old */ | 121 | /* make sure the MRTG data isn't too old */ |
| 116 | time_t current_time; | 122 | time_t current_time; |
| 117 | time(¤t_time); | 123 | time(¤t_time); |
| 118 | if ((expire_minutes > 0) && (current_time - timestamp) > (expire_minutes * 60)) | 124 | if ((config.expire_minutes > 0) && (current_time - timestamp) > (config.expire_minutes * 60)) { |
| 119 | die(STATE_WARNING, _("MRTG data has expired (%d minutes old)\n"), (int)((current_time - timestamp) / 60)); | 125 | die(STATE_WARNING, _("MRTG data has expired (%d minutes old)\n"), |
| 126 | (int)((current_time - timestamp) / 60)); | ||
| 127 | } | ||
| 120 | 128 | ||
| 121 | unsigned long incoming_rate = 0L; | 129 | unsigned long incoming_rate = 0L; |
| 122 | unsigned long outgoing_rate = 0L; | 130 | unsigned long outgoing_rate = 0L; |
| 123 | /* else check the incoming/outgoing rates */ | 131 | /* else check the incoming/outgoing rates */ |
| 124 | if (use_average) { | 132 | if (config.use_average) { |
| 125 | incoming_rate = average_incoming_rate; | 133 | incoming_rate = average_incoming_rate; |
| 126 | outgoing_rate = average_outgoing_rate; | 134 | outgoing_rate = average_outgoing_rate; |
| 127 | } else { | 135 | } else { |
| @@ -166,24 +174,31 @@ int main(int argc, char **argv) { | |||
| 166 | /* report outgoing traffic in MBytes/sec */ | 174 | /* report outgoing traffic in MBytes/sec */ |
| 167 | else { | 175 | else { |
| 168 | strcpy(outgoing_speed_rating, "MB"); | 176 | strcpy(outgoing_speed_rating, "MB"); |
| 169 | adjusted_outgoing_rate = (double)(outgoing_rate / 1024.0 / 1024.0); | 177 | adjusted_outgoing_rate = (outgoing_rate / 1024.0 / 1024.0); |
| 170 | } | 178 | } |
| 171 | 179 | ||
| 172 | int result = STATE_OK; | 180 | int result = STATE_OK; |
| 173 | if (incoming_rate > incoming_critical_threshold || outgoing_rate > outgoing_critical_threshold) { | 181 | if (incoming_rate > config.incoming_critical_threshold || |
| 182 | outgoing_rate > config.outgoing_critical_threshold) { | ||
| 174 | result = STATE_CRITICAL; | 183 | result = STATE_CRITICAL; |
| 175 | } else if (incoming_rate > incoming_warning_threshold || outgoing_rate > outgoing_warning_threshold) { | 184 | } else if (incoming_rate > config.incoming_warning_threshold || |
| 185 | outgoing_rate > config.outgoing_warning_threshold) { | ||
| 176 | result = STATE_WARNING; | 186 | result = STATE_WARNING; |
| 177 | } | 187 | } |
| 178 | 188 | ||
| 179 | char *error_message; | 189 | char *error_message; |
| 180 | xasprintf(&error_message, _("%s. In = %0.1f %s/s, %s. Out = %0.1f %s/s|%s %s\n"), (use_average) ? _("Avg") : _("Max"), | 190 | xasprintf(&error_message, _("%s. In = %0.1f %s/s, %s. Out = %0.1f %s/s|%s %s\n"), |
| 181 | adjusted_incoming_rate, incoming_speed_rating, (use_average) ? _("Avg") : _("Max"), adjusted_outgoing_rate, | 191 | (config.use_average) ? _("Avg") : _("Max"), adjusted_incoming_rate, |
| 182 | outgoing_speed_rating, | 192 | incoming_speed_rating, (config.use_average) ? _("Avg") : _("Max"), |
| 183 | fperfdata("in", adjusted_incoming_rate, incoming_speed_rating, (int)incoming_warning_threshold, incoming_warning_threshold, | 193 | adjusted_outgoing_rate, outgoing_speed_rating, |
| 184 | (int)incoming_critical_threshold, incoming_critical_threshold, true, 0, false, 0), | 194 | fperfdata("in", adjusted_incoming_rate, incoming_speed_rating, |
| 185 | fperfdata("out", adjusted_outgoing_rate, outgoing_speed_rating, (int)outgoing_warning_threshold, outgoing_warning_threshold, | 195 | (int)config.incoming_warning_threshold, config.incoming_warning_threshold, |
| 186 | (int)outgoing_critical_threshold, outgoing_critical_threshold, true, 0, false, 0)); | 196 | (int)config.incoming_critical_threshold, config.incoming_critical_threshold, |
| 197 | true, 0, false, 0), | ||
| 198 | fperfdata("out", adjusted_outgoing_rate, outgoing_speed_rating, | ||
| 199 | (int)config.outgoing_warning_threshold, config.outgoing_warning_threshold, | ||
| 200 | (int)config.outgoing_critical_threshold, config.outgoing_critical_threshold, | ||
| 201 | true, 0, false, 0)); | ||
| 187 | 202 | ||
| 188 | printf(_("Traffic %s - %s\n"), state_text(result), error_message); | 203 | printf(_("Traffic %s - %s\n"), state_text(result), error_message); |
| 189 | 204 | ||
| @@ -191,7 +206,7 @@ int main(int argc, char **argv) { | |||
| 191 | } | 206 | } |
| 192 | 207 | ||
| 193 | /* process command-line arguments */ | 208 | /* process command-line arguments */ |
| 194 | int process_arguments(int argc, char **argv) { | 209 | check_mrtgtraf_config_wrapper process_arguments(int argc, char **argv) { |
| 195 | static struct option longopts[] = {{"filename", required_argument, 0, 'F'}, | 210 | static struct option longopts[] = {{"filename", required_argument, 0, 'F'}, |
| 196 | {"expires", required_argument, 0, 'e'}, | 211 | {"expires", required_argument, 0, 'e'}, |
| 197 | {"aggregation", required_argument, 0, 'a'}, | 212 | {"aggregation", required_argument, 0, 'a'}, |
| @@ -201,44 +216,51 @@ int process_arguments(int argc, char **argv) { | |||
| 201 | {"help", no_argument, 0, 'h'}, | 216 | {"help", no_argument, 0, 'h'}, |
| 202 | {0, 0, 0, 0}}; | 217 | {0, 0, 0, 0}}; |
| 203 | 218 | ||
| 204 | if (argc < 2) | 219 | check_mrtgtraf_config_wrapper result = { |
| 205 | return ERROR; | 220 | .errorcode = OK, |
| 221 | .config = check_mrtgtraf_config_init(), | ||
| 222 | }; | ||
| 223 | if (argc < 2) { | ||
| 224 | result.errorcode = ERROR; | ||
| 225 | return result; | ||
| 226 | } | ||
| 206 | 227 | ||
| 207 | for (int i = 1; i < argc; i++) { | 228 | for (int i = 1; i < argc; i++) { |
| 208 | if (strcmp("-to", argv[i]) == 0) | 229 | if (strcmp("-to", argv[i]) == 0) { |
| 209 | strcpy(argv[i], "-t"); | 230 | strcpy(argv[i], "-t"); |
| 210 | else if (strcmp("-wt", argv[i]) == 0) | 231 | } else if (strcmp("-wt", argv[i]) == 0) { |
| 211 | strcpy(argv[i], "-w"); | 232 | strcpy(argv[i], "-w"); |
| 212 | else if (strcmp("-ct", argv[i]) == 0) | 233 | } else if (strcmp("-ct", argv[i]) == 0) { |
| 213 | strcpy(argv[i], "-c"); | 234 | strcpy(argv[i], "-c"); |
| 235 | } | ||
| 214 | } | 236 | } |
| 215 | 237 | ||
| 216 | int option_char; | 238 | int option_char; |
| 217 | int option = 0; | 239 | int option = 0; |
| 218 | while (1) { | 240 | while (true) { |
| 219 | option_char = getopt_long(argc, argv, "hVF:e:a:c:w:", longopts, &option); | 241 | option_char = getopt_long(argc, argv, "hVF:e:a:c:w:", longopts, &option); |
| 220 | 242 | ||
| 221 | if (option_char == -1 || option_char == EOF) | 243 | if (option_char == -1 || option_char == EOF) { |
| 222 | break; | 244 | break; |
| 245 | } | ||
| 223 | 246 | ||
| 224 | switch (option_char) { | 247 | switch (option_char) { |
| 225 | case 'F': /* input file */ | 248 | case 'F': /* input file */ |
| 226 | log_file = optarg; | 249 | result.config.log_file = optarg; |
| 227 | break; | 250 | break; |
| 228 | case 'e': /* expiration time */ | 251 | case 'e': /* expiration time */ |
| 229 | expire_minutes = atoi(optarg); | 252 | result.config.expire_minutes = atoi(optarg); |
| 230 | break; | 253 | break; |
| 231 | case 'a': /* aggregation (AVE or MAX) */ | 254 | case 'a': /* aggregation (AVE or MAX) */ |
| 232 | if (!strcmp(optarg, "MAX")) | 255 | result.config.use_average = (bool)(strcmp(optarg, "MAX")); |
| 233 | use_average = false; | ||
| 234 | else | ||
| 235 | use_average = true; | ||
| 236 | break; | 256 | break; |
| 237 | case 'c': /* warning threshold */ | 257 | case 'c': /* warning threshold */ |
| 238 | sscanf(optarg, "%lu,%lu", &incoming_critical_threshold, &outgoing_critical_threshold); | 258 | sscanf(optarg, "%lu,%lu", &result.config.incoming_critical_threshold, |
| 259 | &result.config.outgoing_critical_threshold); | ||
| 239 | break; | 260 | break; |
| 240 | case 'w': /* critical threshold */ | 261 | case 'w': /* critical threshold */ |
| 241 | sscanf(optarg, "%lu,%lu", &incoming_warning_threshold, &outgoing_warning_threshold); | 262 | sscanf(optarg, "%lu,%lu", &result.config.incoming_warning_threshold, |
| 263 | &result.config.outgoing_warning_threshold); | ||
| 242 | break; | 264 | break; |
| 243 | case 'V': /* version */ | 265 | case 'V': /* version */ |
| 244 | print_revision(progname, NP_VERSION); | 266 | print_revision(progname, NP_VERSION); |
| @@ -252,39 +274,39 @@ int process_arguments(int argc, char **argv) { | |||
| 252 | } | 274 | } |
| 253 | 275 | ||
| 254 | option_char = optind; | 276 | option_char = optind; |
| 255 | if (argc > option_char && log_file == NULL) { | 277 | if (argc > option_char && result.config.log_file == NULL) { |
| 256 | log_file = argv[option_char++]; | 278 | result.config.log_file = argv[option_char++]; |
| 257 | } | 279 | } |
| 258 | 280 | ||
| 259 | if (argc > option_char && expire_minutes == -1) { | 281 | if (argc > option_char && result.config.expire_minutes == -1) { |
| 260 | expire_minutes = atoi(argv[option_char++]); | 282 | result.config.expire_minutes = atoi(argv[option_char++]); |
| 261 | } | 283 | } |
| 262 | 284 | ||
| 263 | if (argc > option_char && strcmp(argv[option_char], "MAX") == 0) { | 285 | if (argc > option_char && strcmp(argv[option_char], "MAX") == 0) { |
| 264 | use_average = false; | 286 | result.config.use_average = false; |
| 265 | option_char++; | 287 | option_char++; |
| 266 | } else if (argc > option_char && strcmp(argv[option_char], "AVG") == 0) { | 288 | } else if (argc > option_char && strcmp(argv[option_char], "AVG") == 0) { |
| 267 | use_average = true; | 289 | result.config.use_average = true; |
| 268 | option_char++; | 290 | option_char++; |
| 269 | } | 291 | } |
| 270 | 292 | ||
| 271 | if (argc > option_char && incoming_warning_threshold == 0) { | 293 | if (argc > option_char && result.config.incoming_warning_threshold == 0) { |
| 272 | incoming_warning_threshold = strtoul(argv[option_char++], NULL, 10); | 294 | result.config.incoming_warning_threshold = strtoul(argv[option_char++], NULL, 10); |
| 273 | } | 295 | } |
| 274 | 296 | ||
| 275 | if (argc > option_char && incoming_critical_threshold == 0) { | 297 | if (argc > option_char && result.config.incoming_critical_threshold == 0) { |
| 276 | incoming_critical_threshold = strtoul(argv[option_char++], NULL, 10); | 298 | result.config.incoming_critical_threshold = strtoul(argv[option_char++], NULL, 10); |
| 277 | } | 299 | } |
| 278 | 300 | ||
| 279 | if (argc > option_char && outgoing_warning_threshold == 0) { | 301 | if (argc > option_char && result.config.outgoing_warning_threshold == 0) { |
| 280 | outgoing_warning_threshold = strtoul(argv[option_char++], NULL, 10); | 302 | result.config.outgoing_warning_threshold = strtoul(argv[option_char++], NULL, 10); |
| 281 | } | 303 | } |
| 282 | 304 | ||
| 283 | if (argc > option_char && outgoing_critical_threshold == 0) { | 305 | if (argc > option_char && result.config.outgoing_critical_threshold == 0) { |
| 284 | outgoing_critical_threshold = strtoul(argv[option_char++], NULL, 10); | 306 | result.config.outgoing_critical_threshold = strtoul(argv[option_char++], NULL, 10); |
| 285 | } | 307 | } |
| 286 | 308 | ||
| 287 | return OK; | 309 | return result; |
| 288 | } | 310 | } |
| 289 | 311 | ||
| 290 | void print_help(void) { | 312 | void print_help(void) { |
