diff options
Diffstat (limited to 'plugins/check_ups.c')
| -rw-r--r-- | plugins/check_ups.c | 377 |
1 files changed, 163 insertions, 214 deletions
diff --git a/plugins/check_ups.c b/plugins/check_ups.c index 380ff3bc..54decce3 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,100 +33,52 @@ | |||
| 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 | #include "check_ups.d/config.h" | ||
| 43 | #include "states.h" | ||
| 42 | 44 | ||
| 43 | enum { PORT = 3493 }; | 45 | enum { |
| 44 | 46 | NOSUCHVAR = ERROR - 1 | |
| 45 | #define UPS_NONE 0 /* no supported options */ | 47 | }; |
| 46 | #define UPS_UTILITY 1 /* supports utility line */ | ||
| 47 | #define UPS_BATTPCT 2 /* supports percent battery remaining */ | ||
| 48 | #define UPS_STATUS 4 /* supports UPS status */ | ||
| 49 | #define UPS_TEMP 8 /* supports UPS temperature */ | ||
| 50 | #define UPS_LOADPCT 16 /* supports load percent */ | ||
| 51 | #define UPS_REALPOWER 32 /* supports real power */ | ||
| 52 | |||
| 53 | #define UPSSTATUS_NONE 0 | ||
| 54 | #define UPSSTATUS_OFF 1 | ||
| 55 | #define UPSSTATUS_OL 2 | ||
| 56 | #define UPSSTATUS_OB 4 | ||
| 57 | #define UPSSTATUS_LB 8 | ||
| 58 | #define UPSSTATUS_CAL 16 | ||
| 59 | #define UPSSTATUS_RB 32 /*Replace Battery */ | ||
| 60 | #define UPSSTATUS_BYPASS 64 | ||
| 61 | #define UPSSTATUS_OVER 128 | ||
| 62 | #define UPSSTATUS_TRIM 256 | ||
| 63 | #define UPSSTATUS_BOOST 512 | ||
| 64 | #define UPSSTATUS_CHRG 1024 | ||
| 65 | #define UPSSTATUS_DISCHRG 2048 | ||
| 66 | #define UPSSTATUS_UNKNOWN 4096 | ||
| 67 | #define UPSSTATUS_ALARM 8192 | ||
| 68 | |||
| 69 | enum { NOSUCHVAR = ERROR - 1 }; | ||
| 70 | |||
| 71 | typedef struct ups_config { | ||
| 72 | unsigned int server_port; | ||
| 73 | char *server_address; | ||
| 74 | char *ups_name; | ||
| 75 | double warning_value; | ||
| 76 | double critical_value; | ||
| 77 | bool check_warn; | ||
| 78 | bool check_crit; | ||
| 79 | int check_variable; | ||
| 80 | int status; | ||
| 81 | bool temp_output_c; | ||
| 82 | } ups_config; | ||
| 83 | |||
| 84 | ups_config ups_config_init(void) { | ||
| 85 | ups_config tmp = {0}; | ||
| 86 | tmp.server_port = PORT; | ||
| 87 | tmp.server_address = NULL; | ||
| 88 | tmp.ups_name = NULL; | ||
| 89 | tmp.check_variable = UPS_NONE; | ||
| 90 | tmp.status = UPSSTATUS_NONE; | ||
| 91 | |||
| 92 | return tmp; | ||
| 93 | } | ||
| 94 | 48 | ||
| 95 | // Forward declarations | 49 | // Forward declarations |
| 96 | int determine_status(ups_config *, int *supported_options); | 50 | typedef struct { |
| 97 | int get_ups_variable(const char *, char *, const ups_config config); | 51 | int errorcode; |
| 98 | 52 | int ups_status; | |
| 99 | int process_arguments(int, char **, ups_config *); | 53 | int supported_options; |
| 100 | int validate_arguments(ups_config); | 54 | } determine_status_result; |
| 101 | void print_help(void); | 55 | static determine_status_result determine_status(check_ups_config /*config*/); |
| 56 | static int get_ups_variable(const char * /*varname*/, char * /*buf*/, check_ups_config config); | ||
| 57 | |||
| 58 | typedef struct { | ||
| 59 | int errorcode; | ||
| 60 | check_ups_config config; | ||
| 61 | } check_ups_config_wrapper; | ||
| 62 | static check_ups_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/); | ||
| 63 | static check_ups_config_wrapper validate_arguments(check_ups_config_wrapper /*config_wrapper*/); | ||
| 64 | |||
| 65 | static void print_help(void); | ||
| 102 | void print_usage(void); | 66 | void print_usage(void); |
| 103 | 67 | ||
| 104 | int main(int argc, char **argv) { | 68 | int main(int argc, char **argv) { |
| 105 | setlocale(LC_ALL, ""); | 69 | setlocale(LC_ALL, ""); |
| 106 | bindtextdomain(PACKAGE, LOCALEDIR); | 70 | bindtextdomain(PACKAGE, LOCALEDIR); |
| 107 | textdomain(PACKAGE); | 71 | textdomain(PACKAGE); |
| 108 | |||
| 109 | char *ups_status; | ||
| 110 | ups_status = strdup("N/A"); | ||
| 111 | |||
| 112 | char *data; | ||
| 113 | data = strdup(""); | ||
| 114 | |||
| 115 | char *message; | ||
| 116 | message = strdup(""); | ||
| 117 | |||
| 118 | // Exit result | ||
| 119 | int result = STATE_UNKNOWN; | ||
| 120 | |||
| 121 | /* Parse extra opts if any */ | 72 | /* Parse extra opts if any */ |
| 122 | argv = np_extra_opts(&argc, argv, progname); | 73 | argv = np_extra_opts(&argc, argv, progname); |
| 123 | 74 | ||
| 124 | // Config from commandline | 75 | check_ups_config_wrapper tmp_config = process_arguments(argc, argv); |
| 125 | ups_config config = ups_config_init(); | ||
| 126 | 76 | ||
| 127 | if (process_arguments(argc, argv, &config) == ERROR) { | 77 | if (tmp_config.errorcode == ERROR) { |
| 128 | usage4(_("Could not parse arguments")); | 78 | usage4(_("Could not parse arguments")); |
| 129 | } | 79 | } |
| 80 | // Config from commandline | ||
| 81 | check_ups_config config = tmp_config.config; | ||
| 130 | 82 | ||
| 131 | /* initialize alarm signal handling */ | 83 | /* initialize alarm signal handling */ |
| 132 | signal(SIGALRM, socket_timeout_alarm_handler); | 84 | signal(SIGALRM, socket_timeout_alarm_handler); |
| @@ -134,74 +86,76 @@ int main(int argc, char **argv) { | |||
| 134 | /* set socket timeout */ | 86 | /* set socket timeout */ |
| 135 | alarm(socket_timeout); | 87 | alarm(socket_timeout); |
| 136 | 88 | ||
| 137 | int supported_options = UPS_NONE; | ||
| 138 | |||
| 139 | /* get the ups status if possible */ | 89 | /* get the ups status if possible */ |
| 140 | if (determine_status(&config, &supported_options) != OK) { | 90 | determine_status_result query_result = determine_status(config); |
| 91 | if (query_result.errorcode != OK) { | ||
| 141 | return STATE_CRITICAL; | 92 | return STATE_CRITICAL; |
| 142 | } | 93 | } |
| 143 | 94 | ||
| 95 | int ups_status_flags = query_result.ups_status; | ||
| 96 | int supported_options = query_result.supported_options; | ||
| 144 | 97 | ||
| 145 | if (supported_options & UPS_STATUS) { | 98 | // Exit result |
| 146 | 99 | mp_state_enum result = STATE_UNKNOWN; | |
| 147 | ups_status = strdup(""); | 100 | char *message = NULL; |
| 148 | 101 | ||
| 102 | if (supported_options & UPS_STATUS) { | ||
| 103 | char *ups_status = strdup(""); | ||
| 149 | result = STATE_OK; | 104 | result = STATE_OK; |
| 150 | 105 | ||
| 151 | if (config.status & UPSSTATUS_OFF) { | 106 | if (ups_status_flags & UPSSTATUS_OFF) { |
| 152 | xasprintf(&ups_status, "Off"); | 107 | xasprintf(&ups_status, "Off"); |
| 153 | result = STATE_CRITICAL; | 108 | result = STATE_CRITICAL; |
| 154 | } else if ((config.status & (UPSSTATUS_OB | UPSSTATUS_LB)) == | 109 | } else if ((ups_status_flags & (UPSSTATUS_OB | UPSSTATUS_LB)) == |
| 155 | (UPSSTATUS_OB | UPSSTATUS_LB)) { | 110 | (UPSSTATUS_OB | UPSSTATUS_LB)) { |
| 156 | xasprintf(&ups_status, _("On Battery, Low Battery")); | 111 | xasprintf(&ups_status, _("On Battery, Low Battery")); |
| 157 | result = STATE_CRITICAL; | 112 | result = STATE_CRITICAL; |
| 158 | } else { | 113 | } else { |
| 159 | if (config.status & UPSSTATUS_OL) { | 114 | if (ups_status_flags & UPSSTATUS_OL) { |
| 160 | xasprintf(&ups_status, "%s%s", ups_status, _("Online")); | 115 | xasprintf(&ups_status, "%s%s", ups_status, _("Online")); |
| 161 | } | 116 | } |
| 162 | if (config.status & UPSSTATUS_OB) { | 117 | if (ups_status_flags & UPSSTATUS_OB) { |
| 163 | xasprintf(&ups_status, "%s%s", ups_status, _("On Battery")); | 118 | xasprintf(&ups_status, "%s%s", ups_status, _("On Battery")); |
| 164 | result = max_state(result, STATE_WARNING); | 119 | result = max_state(result, STATE_WARNING); |
| 165 | } | 120 | } |
| 166 | if (config.status & UPSSTATUS_LB) { | 121 | if (ups_status_flags & UPSSTATUS_LB) { |
| 167 | xasprintf(&ups_status, "%s%s", ups_status, _(", Low Battery")); | 122 | xasprintf(&ups_status, "%s%s", ups_status, _(", Low Battery")); |
| 168 | result = max_state(result, STATE_WARNING); | 123 | result = max_state(result, STATE_WARNING); |
| 169 | } | 124 | } |
| 170 | if (config.status & UPSSTATUS_CAL) { | 125 | if (ups_status_flags & UPSSTATUS_CAL) { |
| 171 | xasprintf(&ups_status, "%s%s", ups_status, _(", Calibrating")); | 126 | xasprintf(&ups_status, "%s%s", ups_status, _(", Calibrating")); |
| 172 | } | 127 | } |
| 173 | if (config.status & UPSSTATUS_RB) { | 128 | if (ups_status_flags & UPSSTATUS_RB) { |
| 174 | xasprintf(&ups_status, "%s%s", ups_status, | 129 | xasprintf(&ups_status, "%s%s", ups_status, _(", Replace Battery")); |
| 175 | _(", Replace Battery")); | ||
| 176 | result = max_state(result, STATE_WARNING); | 130 | result = max_state(result, STATE_WARNING); |
| 177 | } | 131 | } |
| 178 | if (config.status & UPSSTATUS_BYPASS) { | 132 | if (ups_status_flags & UPSSTATUS_BYPASS) { |
| 179 | xasprintf(&ups_status, "%s%s", ups_status, _(", On Bypass")); | 133 | xasprintf(&ups_status, "%s%s", ups_status, _(", On Bypass")); |
| 180 | // Bypassing the battery is likely a bad thing | 134 | // Bypassing the battery is likely a bad thing |
| 181 | result = STATE_CRITICAL; | 135 | result = STATE_CRITICAL; |
| 182 | } | 136 | } |
| 183 | if (config.status & UPSSTATUS_OVER) { | 137 | if (ups_status_flags & UPSSTATUS_OVER) { |
| 184 | xasprintf(&ups_status, "%s%s", ups_status, _(", Overload")); | 138 | xasprintf(&ups_status, "%s%s", ups_status, _(", Overload")); |
| 185 | result = max_state(result, STATE_WARNING); | 139 | result = max_state(result, STATE_WARNING); |
| 186 | } | 140 | } |
| 187 | if (config.status & UPSSTATUS_TRIM) { | 141 | if (ups_status_flags & UPSSTATUS_TRIM) { |
| 188 | xasprintf(&ups_status, "%s%s", ups_status, _(", Trimming")); | 142 | xasprintf(&ups_status, "%s%s", ups_status, _(", Trimming")); |
| 189 | } | 143 | } |
| 190 | if (config.status & UPSSTATUS_BOOST) { | 144 | if (ups_status_flags & UPSSTATUS_BOOST) { |
| 191 | xasprintf(&ups_status, "%s%s", ups_status, _(", Boosting")); | 145 | xasprintf(&ups_status, "%s%s", ups_status, _(", Boosting")); |
| 192 | } | 146 | } |
| 193 | if (config.status & UPSSTATUS_CHRG) { | 147 | if (ups_status_flags & UPSSTATUS_CHRG) { |
| 194 | xasprintf(&ups_status, "%s%s", ups_status, _(", Charging")); | 148 | xasprintf(&ups_status, "%s%s", ups_status, _(", Charging")); |
| 195 | } | 149 | } |
| 196 | if (config.status & UPSSTATUS_DISCHRG) { | 150 | if (ups_status_flags & UPSSTATUS_DISCHRG) { |
| 197 | xasprintf(&ups_status, "%s%s", ups_status, _(", Discharging")); | 151 | xasprintf(&ups_status, "%s%s", ups_status, _(", Discharging")); |
| 198 | result = max_state(result, STATE_WARNING); | 152 | result = max_state(result, STATE_WARNING); |
| 199 | } | 153 | } |
| 200 | if (config.status & UPSSTATUS_ALARM) { | 154 | if (ups_status_flags & UPSSTATUS_ALARM) { |
| 201 | xasprintf(&ups_status, "%s%s", ups_status, _(", ALARM")); | 155 | xasprintf(&ups_status, "%s%s", ups_status, _(", ALARM")); |
| 202 | result = STATE_CRITICAL; | 156 | result = STATE_CRITICAL; |
| 203 | } | 157 | } |
| 204 | if (config.status & UPSSTATUS_UNKNOWN) { | 158 | if (ups_status_flags & UPSSTATUS_UNKNOWN) { |
| 205 | xasprintf(&ups_status, "%s%s", ups_status, _(", Unknown")); | 159 | xasprintf(&ups_status, "%s%s", ups_status, _(", Unknown")); |
| 206 | } | 160 | } |
| 207 | } | 161 | } |
| @@ -210,7 +164,7 @@ int main(int argc, char **argv) { | |||
| 210 | 164 | ||
| 211 | int res; | 165 | int res; |
| 212 | char temp_buffer[MAX_INPUT_BUFFER]; | 166 | char temp_buffer[MAX_INPUT_BUFFER]; |
| 213 | 167 | char *performance_data = strdup(""); | |
| 214 | /* get the ups utility voltage if possible */ | 168 | /* get the ups utility voltage if possible */ |
| 215 | res = get_ups_variable("input.voltage", temp_buffer, config); | 169 | res = get_ups_variable("input.voltage", temp_buffer, config); |
| 216 | if (res == NOSUCHVAR) { | 170 | if (res == NOSUCHVAR) { |
| @@ -233,24 +187,20 @@ int main(int argc, char **argv) { | |||
| 233 | } | 187 | } |
| 234 | 188 | ||
| 235 | if (config.check_variable == UPS_UTILITY) { | 189 | if (config.check_variable == UPS_UTILITY) { |
| 236 | if (config.check_crit && | 190 | if (config.check_crit && ups_utility_deviation >= config.critical_value) { |
| 237 | ups_utility_deviation >= config.critical_value) { | ||
| 238 | result = STATE_CRITICAL; | 191 | result = STATE_CRITICAL; |
| 239 | } else if (config.check_warn && | 192 | } 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); | 193 | result = max_state(result, STATE_WARNING); |
| 242 | } | 194 | } |
| 243 | xasprintf(&data, "%s", | 195 | xasprintf(&performance_data, "%s", |
| 244 | perfdata("voltage", (long)(1000 * ups_utility_voltage), | 196 | perfdata("voltage", (long)(1000 * ups_utility_voltage), "mV", |
| 245 | "mV", config.check_warn, | 197 | config.check_warn, (long)(1000 * config.warning_value), |
| 246 | (long)(1000 * config.warning_value), | 198 | config.check_crit, (long)(1000 * config.critical_value), true, 0, |
| 247 | config.check_crit, | ||
| 248 | (long)(1000 * config.critical_value), true, 0, | ||
| 249 | false, 0)); | 199 | false, 0)); |
| 250 | } else { | 200 | } else { |
| 251 | xasprintf(&data, "%s", | 201 | xasprintf(&performance_data, "%s", |
| 252 | perfdata("voltage", (long)(1000 * ups_utility_voltage), | 202 | perfdata("voltage", (long)(1000 * ups_utility_voltage), "mV", false, 0, false, |
| 253 | "mV", false, 0, false, 0, true, 0, false, 0)); | 203 | 0, true, 0, false, 0)); |
| 254 | } | 204 | } |
| 255 | } | 205 | } |
| 256 | 206 | ||
| @@ -268,22 +218,19 @@ int main(int argc, char **argv) { | |||
| 268 | xasprintf(&message, "%sBatt=%3.1f%% ", message, ups_battery_percent); | 218 | xasprintf(&message, "%sBatt=%3.1f%% ", message, ups_battery_percent); |
| 269 | 219 | ||
| 270 | if (config.check_variable == UPS_BATTPCT) { | 220 | if (config.check_variable == UPS_BATTPCT) { |
| 271 | if (config.check_crit && | 221 | if (config.check_crit && ups_battery_percent <= config.critical_value) { |
| 272 | ups_battery_percent <= config.critical_value) { | ||
| 273 | result = STATE_CRITICAL; | 222 | result = STATE_CRITICAL; |
| 274 | } else if (config.check_warn && | 223 | } 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); | 224 | result = max_state(result, STATE_WARNING); |
| 277 | } | 225 | } |
| 278 | xasprintf(&data, "%s %s", data, | 226 | xasprintf(&performance_data, "%s %s", performance_data, |
| 279 | perfdata("battery", (long)ups_battery_percent, "%", | 227 | perfdata("battery", (long)ups_battery_percent, "%", config.check_warn, |
| 280 | config.check_warn, (long)(config.warning_value), | 228 | (long)(config.warning_value), config.check_crit, |
| 281 | config.check_crit, (long)(config.critical_value), | 229 | (long)(config.critical_value), true, 0, true, 100)); |
| 282 | true, 0, true, 100)); | ||
| 283 | } else { | 230 | } else { |
| 284 | xasprintf(&data, "%s %s", data, | 231 | xasprintf(&performance_data, "%s %s", performance_data, |
| 285 | perfdata("battery", (long)ups_battery_percent, "%", false, | 232 | perfdata("battery", (long)ups_battery_percent, "%", false, 0, false, 0, true, |
| 286 | 0, false, 0, true, 0, true, 100)); | 233 | 0, true, 100)); |
| 287 | } | 234 | } |
| 288 | } | 235 | } |
| 289 | 236 | ||
| @@ -301,22 +248,19 @@ int main(int argc, char **argv) { | |||
| 301 | xasprintf(&message, "%sLoad=%3.1f%% ", message, ups_load_percent); | 248 | xasprintf(&message, "%sLoad=%3.1f%% ", message, ups_load_percent); |
| 302 | 249 | ||
| 303 | if (config.check_variable == UPS_LOADPCT) { | 250 | if (config.check_variable == UPS_LOADPCT) { |
| 304 | if (config.check_crit && | 251 | if (config.check_crit && ups_load_percent >= config.critical_value) { |
| 305 | ups_load_percent >= config.critical_value) { | ||
| 306 | result = STATE_CRITICAL; | 252 | result = STATE_CRITICAL; |
| 307 | } else if (config.check_warn && | 253 | } 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); | 254 | result = max_state(result, STATE_WARNING); |
| 310 | } | 255 | } |
| 311 | xasprintf(&data, "%s %s", data, | 256 | xasprintf(&performance_data, "%s %s", performance_data, |
| 312 | perfdata("load", (long)ups_load_percent, "%", | 257 | perfdata("load", (long)ups_load_percent, "%", config.check_warn, |
| 313 | config.check_warn, (long)(config.warning_value), | 258 | (long)(config.warning_value), config.check_crit, |
| 314 | config.check_crit, (long)(config.critical_value), | 259 | (long)(config.critical_value), true, 0, true, 100)); |
| 315 | true, 0, true, 100)); | ||
| 316 | } else { | 260 | } else { |
| 317 | xasprintf(&data, "%s %s", data, | 261 | xasprintf(&performance_data, "%s %s", performance_data, |
| 318 | perfdata("load", (long)ups_load_percent, "%", false, 0, | 262 | perfdata("load", (long)ups_load_percent, "%", false, 0, false, 0, true, 0, |
| 319 | false, 0, true, 0, true, 100)); | 263 | true, 100)); |
| 320 | } | 264 | } |
| 321 | } | 265 | } |
| 322 | 266 | ||
| @@ -345,19 +289,17 @@ int main(int argc, char **argv) { | |||
| 345 | if (config.check_variable == UPS_TEMP) { | 289 | if (config.check_variable == UPS_TEMP) { |
| 346 | if (config.check_crit && ups_temperature >= config.critical_value) { | 290 | if (config.check_crit && ups_temperature >= config.critical_value) { |
| 347 | result = STATE_CRITICAL; | 291 | result = STATE_CRITICAL; |
| 348 | } else if (config.check_warn && | 292 | } else if (config.check_warn && ups_temperature >= config.warning_value) { |
| 349 | ups_temperature >= config.warning_value) { | ||
| 350 | result = max_state(result, STATE_WARNING); | 293 | result = max_state(result, STATE_WARNING); |
| 351 | } | 294 | } |
| 352 | xasprintf(&data, "%s %s", data, | 295 | xasprintf(&performance_data, "%s %s", performance_data, |
| 353 | perfdata("temp", (long)ups_temperature, tunits, | 296 | perfdata("temp", (long)ups_temperature, tunits, config.check_warn, |
| 354 | config.check_warn, (long)(config.warning_value), | 297 | (long)(config.warning_value), config.check_crit, |
| 355 | config.check_crit, (long)(config.critical_value), | 298 | (long)(config.critical_value), true, 0, false, 0)); |
| 356 | true, 0, false, 0)); | ||
| 357 | } else { | 299 | } else { |
| 358 | xasprintf(&data, "%s %s", data, | 300 | xasprintf(&performance_data, "%s %s", performance_data, |
| 359 | perfdata("temp", (long)ups_temperature, tunits, false, 0, | 301 | perfdata("temp", (long)ups_temperature, tunits, false, 0, false, 0, true, 0, |
| 360 | false, 0, true, 0, false, 0)); | 302 | false, 0)); |
| 361 | } | 303 | } |
| 362 | } | 304 | } |
| 363 | 305 | ||
| @@ -376,19 +318,17 @@ int main(int argc, char **argv) { | |||
| 376 | if (config.check_variable == UPS_REALPOWER) { | 318 | if (config.check_variable == UPS_REALPOWER) { |
| 377 | if (config.check_crit && ups_realpower >= config.critical_value) { | 319 | if (config.check_crit && ups_realpower >= config.critical_value) { |
| 378 | result = STATE_CRITICAL; | 320 | result = STATE_CRITICAL; |
| 379 | } else if (config.check_warn && | 321 | } else if (config.check_warn && ups_realpower >= config.warning_value) { |
| 380 | ups_realpower >= config.warning_value) { | ||
| 381 | result = max_state(result, STATE_WARNING); | 322 | result = max_state(result, STATE_WARNING); |
| 382 | } | 323 | } |
| 383 | xasprintf(&data, "%s %s", data, | 324 | xasprintf(&performance_data, "%s %s", performance_data, |
| 384 | perfdata("realpower", (long)ups_realpower, "W", | 325 | perfdata("realpower", (long)ups_realpower, "W", config.check_warn, |
| 385 | config.check_warn, (long)(config.warning_value), | 326 | (long)(config.warning_value), config.check_crit, |
| 386 | config.check_crit, (long)(config.critical_value), | 327 | (long)(config.critical_value), true, 0, false, 0)); |
| 387 | true, 0, false, 0)); | ||
| 388 | } else { | 328 | } else { |
| 389 | xasprintf(&data, "%s %s", data, | 329 | xasprintf(&performance_data, "%s %s", performance_data, |
| 390 | perfdata("realpower", (long)ups_realpower, "W", false, 0, | 330 | perfdata("realpower", (long)ups_realpower, "W", false, 0, false, 0, true, 0, |
| 391 | false, 0, true, 0, false, 0)); | 331 | false, 0)); |
| 392 | } | 332 | } |
| 393 | } | 333 | } |
| 394 | 334 | ||
| @@ -402,73 +342,79 @@ int main(int argc, char **argv) { | |||
| 402 | /* reset timeout */ | 342 | /* reset timeout */ |
| 403 | alarm(0); | 343 | alarm(0); |
| 404 | 344 | ||
| 405 | printf("UPS %s - %s|%s\n", state_text(result), message, data); | 345 | printf("UPS %s - %s|%s\n", state_text(result), message, performance_data); |
| 406 | return result; | 346 | exit(result); |
| 407 | } | 347 | } |
| 408 | 348 | ||
| 409 | /* determines what options are supported by the UPS */ | 349 | /* determines what options are supported by the UPS */ |
| 410 | int determine_status(ups_config *config, int *supported_options) { | 350 | determine_status_result determine_status(const check_ups_config config) { |
| 411 | char recv_buffer[MAX_INPUT_BUFFER]; | 351 | |
| 352 | determine_status_result result = { | ||
| 353 | .errorcode = OK, | ||
| 354 | .ups_status = UPSSTATUS_NONE, | ||
| 355 | .supported_options = 0, | ||
| 356 | }; | ||
| 412 | 357 | ||
| 413 | int res = get_ups_variable("ups.status", recv_buffer, *config); | 358 | char recv_buffer[MAX_INPUT_BUFFER]; |
| 359 | int res = get_ups_variable("ups.status", recv_buffer, config); | ||
| 414 | if (res == NOSUCHVAR) { | 360 | if (res == NOSUCHVAR) { |
| 415 | return OK; | 361 | return result; |
| 416 | } | 362 | } |
| 417 | 363 | ||
| 418 | if (res != STATE_OK) { | 364 | if (res != STATE_OK) { |
| 419 | printf("%s\n", _("Invalid response received from host")); | 365 | printf("%s\n", _("Invalid response received from host")); |
| 420 | return ERROR; | 366 | result.errorcode = ERROR; |
| 367 | return result; | ||
| 421 | } | 368 | } |
| 422 | 369 | ||
| 423 | *supported_options |= UPS_STATUS; | 370 | result.supported_options |= UPS_STATUS; |
| 424 | 371 | ||
| 425 | char temp_buffer[MAX_INPUT_BUFFER]; | 372 | char temp_buffer[MAX_INPUT_BUFFER]; |
| 426 | 373 | ||
| 427 | strcpy(temp_buffer, recv_buffer); | 374 | strcpy(temp_buffer, recv_buffer); |
| 428 | for (char *ptr = (char *)strtok(temp_buffer, " "); ptr != NULL; | 375 | for (char *ptr = strtok(temp_buffer, " "); ptr != NULL; ptr = strtok(NULL, " ")) { |
| 429 | ptr = (char *)strtok(NULL, " ")) { | ||
| 430 | if (!strcmp(ptr, "OFF")) { | 376 | if (!strcmp(ptr, "OFF")) { |
| 431 | config->status |= UPSSTATUS_OFF; | 377 | result.ups_status |= UPSSTATUS_OFF; |
| 432 | } else if (!strcmp(ptr, "OL")) { | 378 | } else if (!strcmp(ptr, "OL")) { |
| 433 | config->status |= UPSSTATUS_OL; | 379 | result.ups_status |= UPSSTATUS_OL; |
| 434 | } else if (!strcmp(ptr, "OB")) { | 380 | } else if (!strcmp(ptr, "OB")) { |
| 435 | config->status |= UPSSTATUS_OB; | 381 | result.ups_status |= UPSSTATUS_OB; |
| 436 | } else if (!strcmp(ptr, "LB")) { | 382 | } else if (!strcmp(ptr, "LB")) { |
| 437 | config->status |= UPSSTATUS_LB; | 383 | result.ups_status |= UPSSTATUS_LB; |
| 438 | } else if (!strcmp(ptr, "CAL")) { | 384 | } else if (!strcmp(ptr, "CAL")) { |
| 439 | config->status |= UPSSTATUS_CAL; | 385 | result.ups_status |= UPSSTATUS_CAL; |
| 440 | } else if (!strcmp(ptr, "RB")) { | 386 | } else if (!strcmp(ptr, "RB")) { |
| 441 | config->status |= UPSSTATUS_RB; | 387 | result.ups_status |= UPSSTATUS_RB; |
| 442 | } else if (!strcmp(ptr, "BYPASS")) { | 388 | } else if (!strcmp(ptr, "BYPASS")) { |
| 443 | config->status |= UPSSTATUS_BYPASS; | 389 | result.ups_status |= UPSSTATUS_BYPASS; |
| 444 | } else if (!strcmp(ptr, "OVER")) { | 390 | } else if (!strcmp(ptr, "OVER")) { |
| 445 | config->status |= UPSSTATUS_OVER; | 391 | result.ups_status |= UPSSTATUS_OVER; |
| 446 | } else if (!strcmp(ptr, "TRIM")) { | 392 | } else if (!strcmp(ptr, "TRIM")) { |
| 447 | config->status |= UPSSTATUS_TRIM; | 393 | result.ups_status |= UPSSTATUS_TRIM; |
| 448 | } else if (!strcmp(ptr, "BOOST")) { | 394 | } else if (!strcmp(ptr, "BOOST")) { |
| 449 | config->status |= UPSSTATUS_BOOST; | 395 | result.ups_status |= UPSSTATUS_BOOST; |
| 450 | } else if (!strcmp(ptr, "CHRG")) { | 396 | } else if (!strcmp(ptr, "CHRG")) { |
| 451 | config->status |= UPSSTATUS_CHRG; | 397 | result.ups_status |= UPSSTATUS_CHRG; |
| 452 | } else if (!strcmp(ptr, "DISCHRG")) { | 398 | } else if (!strcmp(ptr, "DISCHRG")) { |
| 453 | config->status |= UPSSTATUS_DISCHRG; | 399 | result.ups_status |= UPSSTATUS_DISCHRG; |
| 454 | } else if (!strcmp(ptr, "ALARM")) { | 400 | } else if (!strcmp(ptr, "ALARM")) { |
| 455 | config->status |= UPSSTATUS_ALARM; | 401 | result.ups_status |= UPSSTATUS_ALARM; |
| 456 | } else { | 402 | } else { |
| 457 | config->status |= UPSSTATUS_UNKNOWN; | 403 | result.ups_status |= UPSSTATUS_UNKNOWN; |
| 458 | } | 404 | } |
| 459 | } | 405 | } |
| 460 | 406 | ||
| 461 | return OK; | 407 | return result; |
| 462 | } | 408 | } |
| 463 | 409 | ||
| 464 | /* gets a variable value for a specific UPS */ | 410 | /* gets a variable value for a specific UPS */ |
| 465 | int get_ups_variable(const char *varname, char *buf, const ups_config config) { | 411 | int get_ups_variable(const char *varname, char *buf, const check_ups_config config) { |
| 466 | char send_buffer[MAX_INPUT_BUFFER]; | 412 | char send_buffer[MAX_INPUT_BUFFER]; |
| 467 | 413 | ||
| 468 | /* create the command string to send to the UPS daemon */ | 414 | /* create the command string to send to the UPS daemon */ |
| 469 | /* Add LOGOUT to avoid read failure logs */ | 415 | /* Add LOGOUT to avoid read failure logs */ |
| 470 | int res = snprintf(send_buffer, sizeof(send_buffer), | 416 | int res = snprintf(send_buffer, sizeof(send_buffer), "GET VAR %s %s\nLOGOUT\n", config.ups_name, |
| 471 | "GET VAR %s %s\nLOGOUT\n", config.ups_name, varname); | 417 | varname); |
| 472 | if ((res > 0) && ((size_t)res >= sizeof(send_buffer))) { | 418 | if ((res > 0) && ((size_t)res >= sizeof(send_buffer))) { |
| 473 | printf("%s\n", _("UPS name to long for buffer")); | 419 | printf("%s\n", _("UPS name to long for buffer")); |
| 474 | return ERROR; | 420 | return ERROR; |
| @@ -477,8 +423,7 @@ int get_ups_variable(const char *varname, char *buf, const ups_config config) { | |||
| 477 | char temp_buffer[MAX_INPUT_BUFFER]; | 423 | char temp_buffer[MAX_INPUT_BUFFER]; |
| 478 | 424 | ||
| 479 | /* send the command to the daemon and get a response back */ | 425 | /* send the command to the daemon and get a response back */ |
| 480 | if (process_tcp_request(config.server_address, config.server_port, | 426 | if (process_tcp_request(config.server_address, config.server_port, send_buffer, temp_buffer, |
| 481 | send_buffer, temp_buffer, | ||
| 482 | sizeof(temp_buffer)) != STATE_OK) { | 427 | sizeof(temp_buffer)) != STATE_OK) { |
| 483 | printf("%s\n", _("Invalid response received from host")); | 428 | printf("%s\n", _("Invalid response received from host")); |
| 484 | return ERROR; | 429 | return ERROR; |
| @@ -496,8 +441,7 @@ int get_ups_variable(const char *varname, char *buf, const ups_config config) { | |||
| 496 | ptr[len - 1] = 0; | 441 | ptr[len - 1] = 0; |
| 497 | } | 442 | } |
| 498 | if (strcmp(ptr, "ERR UNKNOWN-UPS") == 0) { | 443 | if (strcmp(ptr, "ERR UNKNOWN-UPS") == 0) { |
| 499 | printf(_("CRITICAL - no such UPS '%s' on that host\n"), | 444 | printf(_("CRITICAL - no such UPS '%s' on that host\n"), config.ups_name); |
| 500 | config.ups_name); | ||
| 501 | return ERROR; | 445 | return ERROR; |
| 502 | } | 446 | } |
| 503 | 447 | ||
| @@ -534,7 +478,7 @@ int get_ups_variable(const char *varname, char *buf, const ups_config config) { | |||
| 534 | [-wv warn_value] [-cv crit_value] [-to to_sec] */ | 478 | [-wv warn_value] [-cv crit_value] [-to to_sec] */ |
| 535 | 479 | ||
| 536 | /* process command-line arguments */ | 480 | /* process command-line arguments */ |
| 537 | int process_arguments(int argc, char **argv, ups_config *config) { | 481 | check_ups_config_wrapper process_arguments(int argc, char **argv) { |
| 538 | 482 | ||
| 539 | static struct option longopts[] = {{"hostname", required_argument, 0, 'H'}, | 483 | static struct option longopts[] = {{"hostname", required_argument, 0, 'H'}, |
| 540 | {"ups", required_argument, 0, 'u'}, | 484 | {"ups", required_argument, 0, 'u'}, |
| @@ -548,8 +492,14 @@ int process_arguments(int argc, char **argv, ups_config *config) { | |||
| 548 | {"help", no_argument, 0, 'h'}, | 492 | {"help", no_argument, 0, 'h'}, |
| 549 | {0, 0, 0, 0}}; | 493 | {0, 0, 0, 0}}; |
| 550 | 494 | ||
| 495 | check_ups_config_wrapper result = { | ||
| 496 | .errorcode = OK, | ||
| 497 | .config = check_ups_config_init(), | ||
| 498 | }; | ||
| 499 | |||
| 551 | if (argc < 2) { | 500 | if (argc < 2) { |
| 552 | return ERROR; | 501 | result.errorcode = ERROR; |
| 502 | return result; | ||
| 553 | } | 503 | } |
| 554 | 504 | ||
| 555 | int c; | 505 | int c; |
| @@ -576,52 +526,52 @@ int process_arguments(int argc, char **argv, ups_config *config) { | |||
| 576 | usage5(); | 526 | usage5(); |
| 577 | case 'H': /* hostname */ | 527 | case 'H': /* hostname */ |
| 578 | if (is_host(optarg)) { | 528 | if (is_host(optarg)) { |
| 579 | config->server_address = optarg; | 529 | result.config.server_address = optarg; |
| 580 | } else { | 530 | } else { |
| 581 | usage2(_("Invalid hostname/address"), optarg); | 531 | usage2(_("Invalid hostname/address"), optarg); |
| 582 | } | 532 | } |
| 583 | break; | 533 | break; |
| 584 | case 'T': /* FIXME: to be improved (ie "-T C" for Celsius or "-T F" for | 534 | case 'T': /* FIXME: to be improved (ie "-T C" for Celsius or "-T F" for |
| 585 | Fahrenheit) */ | 535 | Fahrenheit) */ |
| 586 | config->temp_output_c = true; | 536 | result.config.temp_output_c = true; |
| 587 | break; | 537 | break; |
| 588 | case 'u': /* ups name */ | 538 | case 'u': /* ups name */ |
| 589 | config->ups_name = optarg; | 539 | result.config.ups_name = optarg; |
| 590 | break; | 540 | break; |
| 591 | case 'p': /* port */ | 541 | case 'p': /* port */ |
| 592 | if (is_intpos(optarg)) { | 542 | if (is_intpos(optarg)) { |
| 593 | config->server_port = atoi(optarg); | 543 | result.config.server_port = atoi(optarg); |
| 594 | } else { | 544 | } else { |
| 595 | usage2(_("Port must be a positive integer"), optarg); | 545 | usage2(_("Port must be a positive integer"), optarg); |
| 596 | } | 546 | } |
| 597 | break; | 547 | break; |
| 598 | case 'c': /* critical time threshold */ | 548 | case 'c': /* critical time threshold */ |
| 599 | if (is_intnonneg(optarg)) { | 549 | if (is_intnonneg(optarg)) { |
| 600 | config->critical_value = atoi(optarg); | 550 | result.config.critical_value = atoi(optarg); |
| 601 | config->check_crit = true; | 551 | result.config.check_crit = true; |
| 602 | } else { | 552 | } else { |
| 603 | usage2(_("Critical time must be a positive integer"), optarg); | 553 | usage2(_("Critical time must be a positive integer"), optarg); |
| 604 | } | 554 | } |
| 605 | break; | 555 | break; |
| 606 | case 'w': /* warning time threshold */ | 556 | case 'w': /* warning time threshold */ |
| 607 | if (is_intnonneg(optarg)) { | 557 | if (is_intnonneg(optarg)) { |
| 608 | config->warning_value = atoi(optarg); | 558 | result.config.warning_value = atoi(optarg); |
| 609 | config->check_warn = true; | 559 | result.config.check_warn = true; |
| 610 | } else { | 560 | } else { |
| 611 | usage2(_("Warning time must be a positive integer"), optarg); | 561 | usage2(_("Warning time must be a positive integer"), optarg); |
| 612 | } | 562 | } |
| 613 | break; | 563 | break; |
| 614 | case 'v': /* variable */ | 564 | case 'v': /* variable */ |
| 615 | if (!strcmp(optarg, "LINE")) { | 565 | if (!strcmp(optarg, "LINE")) { |
| 616 | config->check_variable = UPS_UTILITY; | 566 | result.config.check_variable = UPS_UTILITY; |
| 617 | } else if (!strcmp(optarg, "TEMP")) { | 567 | } else if (!strcmp(optarg, "TEMP")) { |
| 618 | config->check_variable = UPS_TEMP; | 568 | result.config.check_variable = UPS_TEMP; |
| 619 | } else if (!strcmp(optarg, "BATTPCT")) { | 569 | } else if (!strcmp(optarg, "BATTPCT")) { |
| 620 | config->check_variable = UPS_BATTPCT; | 570 | result.config.check_variable = UPS_BATTPCT; |
| 621 | } else if (!strcmp(optarg, "LOADPCT")) { | 571 | } else if (!strcmp(optarg, "LOADPCT")) { |
| 622 | config->check_variable = UPS_LOADPCT; | 572 | result.config.check_variable = UPS_LOADPCT; |
| 623 | } else if (!strcmp(optarg, "REALPOWER")) { | 573 | } else if (!strcmp(optarg, "REALPOWER")) { |
| 624 | config->check_variable = UPS_REALPOWER; | 574 | result.config.check_variable = UPS_REALPOWER; |
| 625 | } else { | 575 | } else { |
| 626 | usage2(_("Unrecognized UPS variable"), optarg); | 576 | usage2(_("Unrecognized UPS variable"), optarg); |
| 627 | } | 577 | } |
| @@ -642,27 +592,27 @@ int process_arguments(int argc, char **argv, ups_config *config) { | |||
| 642 | } | 592 | } |
| 643 | } | 593 | } |
| 644 | 594 | ||
| 645 | if (config->server_address == NULL && argc > optind) { | 595 | if (result.config.server_address == NULL && argc > optind) { |
| 646 | if (is_host(argv[optind])) { | 596 | if (is_host(argv[optind])) { |
| 647 | config->server_address = argv[optind++]; | 597 | result.config.server_address = argv[optind++]; |
| 648 | } else { | 598 | } else { |
| 649 | usage2(_("Invalid hostname/address"), optarg); | 599 | usage2(_("Invalid hostname/address"), optarg); |
| 650 | } | 600 | } |
| 651 | } | 601 | } |
| 652 | 602 | ||
| 653 | if (config->server_address == NULL) { | 603 | if (result.config.server_address == NULL) { |
| 654 | config->server_address = strdup("127.0.0.1"); | 604 | result.config.server_address = strdup("127.0.0.1"); |
| 655 | } | 605 | } |
| 656 | 606 | ||
| 657 | return validate_arguments(*config); | 607 | return validate_arguments(result); |
| 658 | } | 608 | } |
| 659 | 609 | ||
| 660 | int validate_arguments(ups_config config) { | 610 | check_ups_config_wrapper validate_arguments(check_ups_config_wrapper config_wrapper) { |
| 661 | if (!config.ups_name) { | 611 | if (config_wrapper.config.ups_name) { |
| 662 | printf("%s\n", _("Error : no UPS indicated")); | 612 | printf("%s\n", _("Error : no UPS indicated")); |
| 663 | return ERROR; | 613 | config_wrapper.errorcode = ERROR; |
| 664 | } | 614 | } |
| 665 | return OK; | 615 | return config_wrapper; |
| 666 | } | 616 | } |
| 667 | 617 | ||
| 668 | void print_help(void) { | 618 | void print_help(void) { |
| @@ -731,8 +681,7 @@ void print_help(void) { | |||
| 731 | "with Russell Kroll's")); | 681 | "with Russell Kroll's")); |
| 732 | printf(" %s\n", _("Network UPS Tools be installed on the remote host. If " | 682 | printf(" %s\n", _("Network UPS Tools be installed on the remote host. If " |
| 733 | "you do not have the")); | 683 | "you do not have the")); |
| 734 | printf(" %s\n", | 684 | 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")); | 685 | printf(" %s\n", _("http://www.networkupstools.org")); |
| 737 | 686 | ||
| 738 | printf(UT_SUPPORT); | 687 | printf(UT_SUPPORT); |
