diff options
| author | Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> | 2024-10-31 15:27:01 +0100 |
|---|---|---|
| committer | Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> | 2024-10-31 15:27:01 +0100 |
| commit | fa15fdcf5dc2d40aba2f8520108e552b73b1df2b (patch) | |
| tree | d51d62db024b317091f42d9ee540da7371e9d170 /plugins/check_ups.c | |
| parent | 0fd0421052fed1972ecbdfdabecba5a616eaa109 (diff) | |
| parent | 87eb2bef1ee2a6a42793437b2f5d63f41b1e1806 (diff) | |
| download | monitoring-plugins-fa15fdcf5dc2d40aba2f8520108e552b73b1df2b.tar.gz | |
Merge branch 'master' into fix/check_ssh-variable-stuff
Diffstat (limited to 'plugins/check_ups.c')
| -rw-r--r-- | plugins/check_ups.c | 114 |
1 files changed, 39 insertions, 75 deletions
diff --git a/plugins/check_ups.c b/plugins/check_ups.c index 380ff3bc..adb7ab81 100644 --- a/plugins/check_ups.c +++ b/plugins/check_ups.c | |||
| @@ -6,7 +6,7 @@ | |||
| 6 | * Copyright (c) 2000 Tom Shields | 6 | * Copyright (c) 2000 Tom Shields |
| 7 | * 2004 Alain Richard <alain.richard@equation.fr> | 7 | * 2004 Alain Richard <alain.richard@equation.fr> |
| 8 | * 2004 Arnaud Quette <arnaud.quette@mgeups.com> | 8 | * 2004 Arnaud Quette <arnaud.quette@mgeups.com> |
| 9 | * Copyright (c) 2002-2023 Monitoring Plugins Development Team | 9 | * Copyright (c) 2002-2024 Monitoring Plugins Development Team |
| 10 | * | 10 | * |
| 11 | * Description: | 11 | * Description: |
| 12 | * | 12 | * |
| @@ -33,14 +33,16 @@ | |||
| 33 | *****************************************************************************/ | 33 | *****************************************************************************/ |
| 34 | 34 | ||
| 35 | const char *progname = "check_ups"; | 35 | const char *progname = "check_ups"; |
| 36 | const char *copyright = "2000-2023"; | 36 | const char *copyright = "2000-2024"; |
| 37 | const char *email = "devel@monitoring-plugins.org"; | 37 | const char *email = "devel@monitoring-plugins.org"; |
| 38 | 38 | ||
| 39 | #include "common.h" | 39 | #include "common.h" |
| 40 | #include "netutils.h" | 40 | #include "netutils.h" |
| 41 | #include "utils.h" | 41 | #include "utils.h" |
| 42 | 42 | ||
| 43 | enum { PORT = 3493 }; | 43 | enum { |
| 44 | PORT = 3493 | ||
| 45 | }; | ||
| 44 | 46 | ||
| 45 | #define UPS_NONE 0 /* no supported options */ | 47 | #define UPS_NONE 0 /* no supported options */ |
| 46 | #define UPS_UTILITY 1 /* supports utility line */ | 48 | #define UPS_UTILITY 1 /* supports utility line */ |
| @@ -66,7 +68,9 @@ enum { PORT = 3493 }; | |||
| 66 | #define UPSSTATUS_UNKNOWN 4096 | 68 | #define UPSSTATUS_UNKNOWN 4096 |
| 67 | #define UPSSTATUS_ALARM 8192 | 69 | #define UPSSTATUS_ALARM 8192 |
| 68 | 70 | ||
| 69 | enum { NOSUCHVAR = ERROR - 1 }; | 71 | enum { |
| 72 | NOSUCHVAR = ERROR - 1 | ||
| 73 | }; | ||
| 70 | 74 | ||
| 71 | typedef struct ups_config { | 75 | typedef struct ups_config { |
| 72 | unsigned int server_port; | 76 | unsigned int server_port; |
| @@ -141,7 +145,6 @@ int main(int argc, char **argv) { | |||
| 141 | return STATE_CRITICAL; | 145 | return STATE_CRITICAL; |
| 142 | } | 146 | } |
| 143 | 147 | ||
| 144 | |||
| 145 | if (supported_options & UPS_STATUS) { | 148 | if (supported_options & UPS_STATUS) { |
| 146 | 149 | ||
| 147 | ups_status = strdup(""); | 150 | ups_status = strdup(""); |
| @@ -151,8 +154,7 @@ int main(int argc, char **argv) { | |||
| 151 | if (config.status & UPSSTATUS_OFF) { | 154 | if (config.status & UPSSTATUS_OFF) { |
| 152 | xasprintf(&ups_status, "Off"); | 155 | xasprintf(&ups_status, "Off"); |
| 153 | result = STATE_CRITICAL; | 156 | result = STATE_CRITICAL; |
| 154 | } else if ((config.status & (UPSSTATUS_OB | UPSSTATUS_LB)) == | 157 | } else if ((config.status & (UPSSTATUS_OB | UPSSTATUS_LB)) == (UPSSTATUS_OB | UPSSTATUS_LB)) { |
| 155 | (UPSSTATUS_OB | UPSSTATUS_LB)) { | ||
| 156 | xasprintf(&ups_status, _("On Battery, Low Battery")); | 158 | xasprintf(&ups_status, _("On Battery, Low Battery")); |
| 157 | result = STATE_CRITICAL; | 159 | result = STATE_CRITICAL; |
| 158 | } else { | 160 | } else { |
| @@ -171,8 +173,7 @@ int main(int argc, char **argv) { | |||
| 171 | xasprintf(&ups_status, "%s%s", ups_status, _(", Calibrating")); | 173 | xasprintf(&ups_status, "%s%s", ups_status, _(", Calibrating")); |
| 172 | } | 174 | } |
| 173 | if (config.status & UPSSTATUS_RB) { | 175 | if (config.status & UPSSTATUS_RB) { |
| 174 | xasprintf(&ups_status, "%s%s", ups_status, | 176 | xasprintf(&ups_status, "%s%s", ups_status, _(", Replace Battery")); |
| 175 | _(", Replace Battery")); | ||
| 176 | result = max_state(result, STATE_WARNING); | 177 | result = max_state(result, STATE_WARNING); |
| 177 | } | 178 | } |
| 178 | if (config.status & UPSSTATUS_BYPASS) { | 179 | if (config.status & UPSSTATUS_BYPASS) { |
| @@ -233,24 +234,16 @@ int main(int argc, char **argv) { | |||
| 233 | } | 234 | } |
| 234 | 235 | ||
| 235 | if (config.check_variable == UPS_UTILITY) { | 236 | if (config.check_variable == UPS_UTILITY) { |
| 236 | if (config.check_crit && | 237 | if (config.check_crit && ups_utility_deviation >= config.critical_value) { |
| 237 | ups_utility_deviation >= config.critical_value) { | ||
| 238 | result = STATE_CRITICAL; | 238 | result = STATE_CRITICAL; |
| 239 | } else if (config.check_warn && | 239 | } else if (config.check_warn && ups_utility_deviation >= config.warning_value) { |
| 240 | ups_utility_deviation >= config.warning_value) { | ||
| 241 | result = max_state(result, STATE_WARNING); | 240 | result = max_state(result, STATE_WARNING); |
| 242 | } | 241 | } |
| 243 | xasprintf(&data, "%s", | 242 | xasprintf(&data, "%s", |
| 244 | perfdata("voltage", (long)(1000 * ups_utility_voltage), | 243 | perfdata("voltage", (long)(1000 * ups_utility_voltage), "mV", config.check_warn, (long)(1000 * config.warning_value), |
| 245 | "mV", config.check_warn, | 244 | config.check_crit, (long)(1000 * config.critical_value), true, 0, false, 0)); |
| 246 | (long)(1000 * config.warning_value), | ||
| 247 | config.check_crit, | ||
| 248 | (long)(1000 * config.critical_value), true, 0, | ||
| 249 | false, 0)); | ||
| 250 | } else { | 245 | } else { |
| 251 | xasprintf(&data, "%s", | 246 | xasprintf(&data, "%s", perfdata("voltage", (long)(1000 * ups_utility_voltage), "mV", false, 0, false, 0, true, 0, false, 0)); |
| 252 | perfdata("voltage", (long)(1000 * ups_utility_voltage), | ||
| 253 | "mV", false, 0, false, 0, true, 0, false, 0)); | ||
| 254 | } | 247 | } |
| 255 | } | 248 | } |
| 256 | 249 | ||
| @@ -268,22 +261,16 @@ int main(int argc, char **argv) { | |||
| 268 | xasprintf(&message, "%sBatt=%3.1f%% ", message, ups_battery_percent); | 261 | xasprintf(&message, "%sBatt=%3.1f%% ", message, ups_battery_percent); |
| 269 | 262 | ||
| 270 | if (config.check_variable == UPS_BATTPCT) { | 263 | if (config.check_variable == UPS_BATTPCT) { |
| 271 | if (config.check_crit && | 264 | if (config.check_crit && ups_battery_percent <= config.critical_value) { |
| 272 | ups_battery_percent <= config.critical_value) { | ||
| 273 | result = STATE_CRITICAL; | 265 | result = STATE_CRITICAL; |
| 274 | } else if (config.check_warn && | 266 | } else if (config.check_warn && ups_battery_percent <= config.warning_value) { |
| 275 | ups_battery_percent <= config.warning_value) { | ||
| 276 | result = max_state(result, STATE_WARNING); | 267 | result = max_state(result, STATE_WARNING); |
| 277 | } | 268 | } |
| 278 | xasprintf(&data, "%s %s", data, | 269 | xasprintf(&data, "%s %s", data, |
| 279 | perfdata("battery", (long)ups_battery_percent, "%", | 270 | perfdata("battery", (long)ups_battery_percent, "%", config.check_warn, (long)(config.warning_value), |
| 280 | config.check_warn, (long)(config.warning_value), | 271 | config.check_crit, (long)(config.critical_value), true, 0, true, 100)); |
| 281 | config.check_crit, (long)(config.critical_value), | ||
| 282 | true, 0, true, 100)); | ||
| 283 | } else { | 272 | } else { |
| 284 | xasprintf(&data, "%s %s", data, | 273 | xasprintf(&data, "%s %s", data, perfdata("battery", (long)ups_battery_percent, "%", false, 0, false, 0, true, 0, true, 100)); |
| 285 | perfdata("battery", (long)ups_battery_percent, "%", false, | ||
| 286 | 0, false, 0, true, 0, true, 100)); | ||
| 287 | } | 274 | } |
| 288 | } | 275 | } |
| 289 | 276 | ||
| @@ -301,22 +288,16 @@ int main(int argc, char **argv) { | |||
| 301 | xasprintf(&message, "%sLoad=%3.1f%% ", message, ups_load_percent); | 288 | xasprintf(&message, "%sLoad=%3.1f%% ", message, ups_load_percent); |
| 302 | 289 | ||
| 303 | if (config.check_variable == UPS_LOADPCT) { | 290 | if (config.check_variable == UPS_LOADPCT) { |
| 304 | if (config.check_crit && | 291 | if (config.check_crit && ups_load_percent >= config.critical_value) { |
| 305 | ups_load_percent >= config.critical_value) { | ||
| 306 | result = STATE_CRITICAL; | 292 | result = STATE_CRITICAL; |
| 307 | } else if (config.check_warn && | 293 | } else if (config.check_warn && ups_load_percent >= config.warning_value) { |
| 308 | ups_load_percent >= config.warning_value) { | ||
| 309 | result = max_state(result, STATE_WARNING); | 294 | result = max_state(result, STATE_WARNING); |
| 310 | } | 295 | } |
| 311 | xasprintf(&data, "%s %s", data, | 296 | xasprintf(&data, "%s %s", data, |
| 312 | perfdata("load", (long)ups_load_percent, "%", | 297 | perfdata("load", (long)ups_load_percent, "%", config.check_warn, (long)(config.warning_value), config.check_crit, |
| 313 | config.check_warn, (long)(config.warning_value), | 298 | (long)(config.critical_value), true, 0, true, 100)); |
| 314 | config.check_crit, (long)(config.critical_value), | ||
| 315 | true, 0, true, 100)); | ||
| 316 | } else { | 299 | } else { |
| 317 | xasprintf(&data, "%s %s", data, | 300 | xasprintf(&data, "%s %s", data, perfdata("load", (long)ups_load_percent, "%", false, 0, false, 0, true, 0, true, 100)); |
| 318 | perfdata("load", (long)ups_load_percent, "%", false, 0, | ||
| 319 | false, 0, true, 0, true, 100)); | ||
| 320 | } | 301 | } |
| 321 | } | 302 | } |
| 322 | 303 | ||
| @@ -345,19 +326,14 @@ int main(int argc, char **argv) { | |||
| 345 | if (config.check_variable == UPS_TEMP) { | 326 | if (config.check_variable == UPS_TEMP) { |
| 346 | if (config.check_crit && ups_temperature >= config.critical_value) { | 327 | if (config.check_crit && ups_temperature >= config.critical_value) { |
| 347 | result = STATE_CRITICAL; | 328 | result = STATE_CRITICAL; |
| 348 | } else if (config.check_warn && | 329 | } else if (config.check_warn && ups_temperature >= config.warning_value) { |
| 349 | ups_temperature >= config.warning_value) { | ||
| 350 | result = max_state(result, STATE_WARNING); | 330 | result = max_state(result, STATE_WARNING); |
| 351 | } | 331 | } |
| 352 | xasprintf(&data, "%s %s", data, | 332 | xasprintf(&data, "%s %s", data, |
| 353 | perfdata("temp", (long)ups_temperature, tunits, | 333 | perfdata("temp", (long)ups_temperature, tunits, config.check_warn, (long)(config.warning_value), config.check_crit, |
| 354 | config.check_warn, (long)(config.warning_value), | 334 | (long)(config.critical_value), true, 0, false, 0)); |
| 355 | config.check_crit, (long)(config.critical_value), | ||
| 356 | true, 0, false, 0)); | ||
| 357 | } else { | 335 | } else { |
| 358 | xasprintf(&data, "%s %s", data, | 336 | xasprintf(&data, "%s %s", data, perfdata("temp", (long)ups_temperature, tunits, false, 0, false, 0, true, 0, false, 0)); |
| 359 | perfdata("temp", (long)ups_temperature, tunits, false, 0, | ||
| 360 | false, 0, true, 0, false, 0)); | ||
| 361 | } | 337 | } |
| 362 | } | 338 | } |
| 363 | 339 | ||
| @@ -376,19 +352,14 @@ int main(int argc, char **argv) { | |||
| 376 | if (config.check_variable == UPS_REALPOWER) { | 352 | if (config.check_variable == UPS_REALPOWER) { |
| 377 | if (config.check_crit && ups_realpower >= config.critical_value) { | 353 | if (config.check_crit && ups_realpower >= config.critical_value) { |
| 378 | result = STATE_CRITICAL; | 354 | result = STATE_CRITICAL; |
| 379 | } else if (config.check_warn && | 355 | } else if (config.check_warn && ups_realpower >= config.warning_value) { |
| 380 | ups_realpower >= config.warning_value) { | ||
| 381 | result = max_state(result, STATE_WARNING); | 356 | result = max_state(result, STATE_WARNING); |
| 382 | } | 357 | } |
| 383 | xasprintf(&data, "%s %s", data, | 358 | xasprintf(&data, "%s %s", data, |
| 384 | perfdata("realpower", (long)ups_realpower, "W", | 359 | perfdata("realpower", (long)ups_realpower, "W", config.check_warn, (long)(config.warning_value), config.check_crit, |
| 385 | config.check_warn, (long)(config.warning_value), | 360 | (long)(config.critical_value), true, 0, false, 0)); |
| 386 | config.check_crit, (long)(config.critical_value), | ||
| 387 | true, 0, false, 0)); | ||
| 388 | } else { | 361 | } else { |
| 389 | xasprintf(&data, "%s %s", data, | 362 | xasprintf(&data, "%s %s", data, perfdata("realpower", (long)ups_realpower, "W", false, 0, false, 0, true, 0, false, 0)); |
| 390 | perfdata("realpower", (long)ups_realpower, "W", false, 0, | ||
| 391 | false, 0, true, 0, false, 0)); | ||
| 392 | } | 363 | } |
| 393 | } | 364 | } |
| 394 | 365 | ||
| @@ -425,8 +396,7 @@ int determine_status(ups_config *config, int *supported_options) { | |||
| 425 | char temp_buffer[MAX_INPUT_BUFFER]; | 396 | char temp_buffer[MAX_INPUT_BUFFER]; |
| 426 | 397 | ||
| 427 | strcpy(temp_buffer, recv_buffer); | 398 | strcpy(temp_buffer, recv_buffer); |
| 428 | for (char *ptr = (char *)strtok(temp_buffer, " "); ptr != NULL; | 399 | for (char *ptr = (char *)strtok(temp_buffer, " "); ptr != NULL; ptr = (char *)strtok(NULL, " ")) { |
| 429 | ptr = (char *)strtok(NULL, " ")) { | ||
| 430 | if (!strcmp(ptr, "OFF")) { | 400 | if (!strcmp(ptr, "OFF")) { |
| 431 | config->status |= UPSSTATUS_OFF; | 401 | config->status |= UPSSTATUS_OFF; |
| 432 | } else if (!strcmp(ptr, "OL")) { | 402 | } else if (!strcmp(ptr, "OL")) { |
| @@ -467,8 +437,7 @@ int get_ups_variable(const char *varname, char *buf, const ups_config config) { | |||
| 467 | 437 | ||
| 468 | /* create the command string to send to the UPS daemon */ | 438 | /* create the command string to send to the UPS daemon */ |
| 469 | /* Add LOGOUT to avoid read failure logs */ | 439 | /* Add LOGOUT to avoid read failure logs */ |
| 470 | int res = snprintf(send_buffer, sizeof(send_buffer), | 440 | int res = snprintf(send_buffer, sizeof(send_buffer), "GET VAR %s %s\nLOGOUT\n", config.ups_name, varname); |
| 471 | "GET VAR %s %s\nLOGOUT\n", config.ups_name, varname); | ||
| 472 | if ((res > 0) && ((size_t)res >= sizeof(send_buffer))) { | 441 | if ((res > 0) && ((size_t)res >= sizeof(send_buffer))) { |
| 473 | printf("%s\n", _("UPS name to long for buffer")); | 442 | printf("%s\n", _("UPS name to long for buffer")); |
| 474 | return ERROR; | 443 | return ERROR; |
| @@ -477,9 +446,7 @@ int get_ups_variable(const char *varname, char *buf, const ups_config config) { | |||
| 477 | char temp_buffer[MAX_INPUT_BUFFER]; | 446 | char temp_buffer[MAX_INPUT_BUFFER]; |
| 478 | 447 | ||
| 479 | /* send the command to the daemon and get a response back */ | 448 | /* send the command to the daemon and get a response back */ |
| 480 | if (process_tcp_request(config.server_address, config.server_port, | 449 | if (process_tcp_request(config.server_address, config.server_port, send_buffer, temp_buffer, sizeof(temp_buffer)) != STATE_OK) { |
| 481 | send_buffer, temp_buffer, | ||
| 482 | sizeof(temp_buffer)) != STATE_OK) { | ||
| 483 | printf("%s\n", _("Invalid response received from host")); | 450 | printf("%s\n", _("Invalid response received from host")); |
| 484 | return ERROR; | 451 | return ERROR; |
| 485 | } | 452 | } |
| @@ -496,8 +463,7 @@ int get_ups_variable(const char *varname, char *buf, const ups_config config) { | |||
| 496 | ptr[len - 1] = 0; | 463 | ptr[len - 1] = 0; |
| 497 | } | 464 | } |
| 498 | if (strcmp(ptr, "ERR UNKNOWN-UPS") == 0) { | 465 | if (strcmp(ptr, "ERR UNKNOWN-UPS") == 0) { |
| 499 | printf(_("CRITICAL - no such UPS '%s' on that host\n"), | 466 | printf(_("CRITICAL - no such UPS '%s' on that host\n"), config.ups_name); |
| 500 | config.ups_name); | ||
| 501 | return ERROR; | 467 | return ERROR; |
| 502 | } | 468 | } |
| 503 | 469 | ||
| @@ -694,8 +660,7 @@ void print_help(void) { | |||
| 694 | printf(" %s\n", "-T, --temperature"); | 660 | printf(" %s\n", "-T, --temperature"); |
| 695 | printf(" %s\n", _("Output of temperatures in Celsius")); | 661 | printf(" %s\n", _("Output of temperatures in Celsius")); |
| 696 | printf(" %s\n", "-v, --variable=STRING"); | 662 | printf(" %s\n", "-v, --variable=STRING"); |
| 697 | printf(" %s %s\n", _("Valid values for STRING are"), | 663 | printf(" %s %s\n", _("Valid values for STRING are"), "LINE, TEMP, BATTPCT, LOADPCT or REALPOWER"); |
| 698 | "LINE, TEMP, BATTPCT, LOADPCT or REALPOWER"); | ||
| 699 | 664 | ||
| 700 | printf(UT_WARN_CRIT); | 665 | printf(UT_WARN_CRIT); |
| 701 | 666 | ||
| @@ -731,8 +696,7 @@ void print_help(void) { | |||
| 731 | "with Russell Kroll's")); | 696 | "with Russell Kroll's")); |
| 732 | printf(" %s\n", _("Network UPS Tools be installed on the remote host. If " | 697 | printf(" %s\n", _("Network UPS Tools be installed on the remote host. If " |
| 733 | "you do not have the")); | 698 | "you do not have the")); |
| 734 | printf(" %s\n", | 699 | printf(" %s\n", _("package installed on your system, you can download it from")); |
| 735 | _("package installed on your system, you can download it from")); | ||
| 736 | printf(" %s\n", _("http://www.networkupstools.org")); | 700 | printf(" %s\n", _("http://www.networkupstools.org")); |
| 737 | 701 | ||
| 738 | printf(UT_SUPPORT); | 702 | printf(UT_SUPPORT); |
