diff options
Diffstat (limited to 'plugins/check_ups.c')
| -rw-r--r-- | plugins/check_ups.c | 1004 |
1 files changed, 500 insertions, 504 deletions
diff --git a/plugins/check_ups.c b/plugins/check_ups.c index 2fb04eef..54decce3 100644 --- a/plugins/check_ups.c +++ b/plugins/check_ups.c | |||
| @@ -1,699 +1,695 @@ | |||
| 1 | /***************************************************************************** | 1 | /***************************************************************************** |
| 2 | * | 2 | * |
| 3 | * Monitoring check_ups plugin | 3 | * Monitoring check_ups plugin |
| 4 | * | 4 | * |
| 5 | * License: GPL | 5 | * License: GPL |
| 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-2007 Monitoring Plugins Development Team | 9 | * Copyright (c) 2002-2024 Monitoring Plugins Development Team |
| 10 | * | 10 | * |
| 11 | * Description: | 11 | * Description: |
| 12 | * | 12 | * |
| 13 | * This file contains Network UPS Tools plugin for Monitoring | 13 | * This file contains Network UPS Tools plugin for Monitoring |
| 14 | * | 14 | * |
| 15 | * This plugin tests the UPS service on the specified host. Network UPS Tools | 15 | * This plugin tests the UPS service on the specified host. Network UPS Tools |
| 16 | * from www.networkupstools.org must be running for this plugin to work. | 16 | * from www.networkupstools.org must be running for this plugin to work. |
| 17 | * | 17 | * |
| 18 | * | 18 | * |
| 19 | * This program is free software: you can redistribute it and/or modify | 19 | * This program is free software: you can redistribute it and/or modify |
| 20 | * it under the terms of the GNU General Public License as published by | 20 | * it under the terms of the GNU General Public License as published by |
| 21 | * the Free Software Foundation, either version 3 of the License, or | 21 | * the Free Software Foundation, either version 3 of the License, or |
| 22 | * (at your option) any later version. | 22 | * (at your option) any later version. |
| 23 | * | 23 | * |
| 24 | * This program is distributed in the hope that it will be useful, | 24 | * This program is distributed in the hope that it will be useful, |
| 25 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 25 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 26 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 26 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 27 | * GNU General Public License for more details. | 27 | * GNU General Public License for more details. |
| 28 | * | 28 | * |
| 29 | * You should have received a copy of the GNU General Public License | 29 | * You should have received a copy of the GNU General Public License |
| 30 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | 30 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 31 | * | 31 | * |
| 32 | * | 32 | * |
| 33 | *****************************************************************************/ | 33 | *****************************************************************************/ |
| 34 | 34 | ||
| 35 | const char *progname = "check_ups"; | 35 | const char *progname = "check_ups"; |
| 36 | const char *copyright = "2000-2007"; | 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 { | 45 | enum { |
| 44 | PORT = 3493 | 46 | NOSUCHVAR = ERROR - 1 |
| 45 | }; | 47 | }; |
| 46 | 48 | ||
| 47 | #define CHECK_NONE 0 | 49 | // Forward declarations |
| 48 | 50 | typedef struct { | |
| 49 | #define UPS_NONE 0 /* no supported options */ | 51 | int errorcode; |
| 50 | #define UPS_UTILITY 1 /* supports utility line */ | 52 | int ups_status; |
| 51 | #define UPS_BATTPCT 2 /* supports percent battery remaining */ | 53 | int supported_options; |
| 52 | #define UPS_STATUS 4 /* supports UPS status */ | 54 | } determine_status_result; |
| 53 | #define UPS_TEMP 8 /* supports UPS temperature */ | 55 | static determine_status_result determine_status(check_ups_config /*config*/); |
| 54 | #define UPS_LOADPCT 16 /* supports load percent */ | 56 | static int get_ups_variable(const char * /*varname*/, char * /*buf*/, check_ups_config config); |
| 55 | #define UPS_REALPOWER 32 /* supports real power */ | 57 | |
| 56 | 58 | typedef struct { | |
| 57 | #define UPSSTATUS_NONE 0 | 59 | int errorcode; |
| 58 | #define UPSSTATUS_OFF 1 | 60 | check_ups_config config; |
| 59 | #define UPSSTATUS_OL 2 | 61 | } check_ups_config_wrapper; |
| 60 | #define UPSSTATUS_OB 4 | 62 | static check_ups_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/); |
| 61 | #define UPSSTATUS_LB 8 | 63 | static check_ups_config_wrapper validate_arguments(check_ups_config_wrapper /*config_wrapper*/); |
| 62 | #define UPSSTATUS_CAL 16 | 64 | |
| 63 | #define UPSSTATUS_RB 32 /*Replace Battery */ | 65 | static void print_help(void); |
| 64 | #define UPSSTATUS_BYPASS 64 | 66 | void print_usage(void); |
| 65 | #define UPSSTATUS_OVER 128 | 67 | |
| 66 | #define UPSSTATUS_TRIM 256 | 68 | int main(int argc, char **argv) { |
| 67 | #define UPSSTATUS_BOOST 512 | 69 | setlocale(LC_ALL, ""); |
| 68 | #define UPSSTATUS_CHRG 1024 | 70 | bindtextdomain(PACKAGE, LOCALEDIR); |
| 69 | #define UPSSTATUS_DISCHRG 2048 | 71 | textdomain(PACKAGE); |
| 70 | #define UPSSTATUS_UNKNOWN 4096 | ||
| 71 | |||
| 72 | enum { NOSUCHVAR = ERROR-1 }; | ||
| 73 | |||
| 74 | int server_port = PORT; | ||
| 75 | char *server_address; | ||
| 76 | char *ups_name = NULL; | ||
| 77 | double warning_value = 0.0; | ||
| 78 | double critical_value = 0.0; | ||
| 79 | bool check_warn = false; | ||
| 80 | bool check_crit = false; | ||
| 81 | int check_variable = UPS_NONE; | ||
| 82 | int supported_options = UPS_NONE; | ||
| 83 | int status = UPSSTATUS_NONE; | ||
| 84 | |||
| 85 | double ups_utility_voltage = 0.0; | ||
| 86 | double ups_battery_percent = 0.0; | ||
| 87 | double ups_load_percent = 0.0; | ||
| 88 | double ups_temperature = 0.0; | ||
| 89 | double ups_realpower = 0.0; | ||
| 90 | char *ups_status; | ||
| 91 | bool temp_output_c = false; | ||
| 92 | |||
| 93 | int determine_status (void); | ||
| 94 | int get_ups_variable (const char *, char *); | ||
| 95 | |||
| 96 | int process_arguments (int, char **); | ||
| 97 | int validate_arguments (void); | ||
| 98 | void print_help (void); | ||
| 99 | void print_usage (void); | ||
| 100 | |||
| 101 | int | ||
| 102 | main (int argc, char **argv) | ||
| 103 | { | ||
| 104 | int result = STATE_UNKNOWN; | ||
| 105 | char *message; | ||
| 106 | char *data; | ||
| 107 | char *tunits; | ||
| 108 | char temp_buffer[MAX_INPUT_BUFFER]; | ||
| 109 | double ups_utility_deviation = 0.0; | ||
| 110 | int res; | ||
| 111 | |||
| 112 | setlocale (LC_ALL, ""); | ||
| 113 | bindtextdomain (PACKAGE, LOCALEDIR); | ||
| 114 | textdomain (PACKAGE); | ||
| 115 | |||
| 116 | ups_status = strdup ("N/A"); | ||
| 117 | data = strdup (""); | ||
| 118 | message = strdup (""); | ||
| 119 | |||
| 120 | /* Parse extra opts if any */ | 72 | /* Parse extra opts if any */ |
| 121 | argv=np_extra_opts (&argc, argv, progname); | 73 | argv = np_extra_opts(&argc, argv, progname); |
| 74 | |||
| 75 | check_ups_config_wrapper tmp_config = process_arguments(argc, argv); | ||
| 122 | 76 | ||
| 123 | if (process_arguments (argc, argv) == ERROR) | 77 | if (tmp_config.errorcode == ERROR) { |
| 124 | usage4 (_("Could not parse arguments")); | 78 | usage4(_("Could not parse arguments")); |
| 79 | } | ||
| 80 | // Config from commandline | ||
| 81 | check_ups_config config = tmp_config.config; | ||
| 125 | 82 | ||
| 126 | /* initialize alarm signal handling */ | 83 | /* initialize alarm signal handling */ |
| 127 | signal (SIGALRM, socket_timeout_alarm_handler); | 84 | signal(SIGALRM, socket_timeout_alarm_handler); |
| 128 | 85 | ||
| 129 | /* set socket timeout */ | 86 | /* set socket timeout */ |
| 130 | alarm (socket_timeout); | 87 | alarm(socket_timeout); |
| 131 | 88 | ||
| 132 | /* get the ups status if possible */ | 89 | /* get the ups status if possible */ |
| 133 | if (determine_status () != OK) | 90 | determine_status_result query_result = determine_status(config); |
| 91 | if (query_result.errorcode != OK) { | ||
| 134 | return STATE_CRITICAL; | 92 | return STATE_CRITICAL; |
| 135 | if (supported_options & UPS_STATUS) { | 93 | } |
| 136 | 94 | ||
| 137 | ups_status = strdup (""); | 95 | int ups_status_flags = query_result.ups_status; |
| 96 | int supported_options = query_result.supported_options; | ||
| 97 | |||
| 98 | // Exit result | ||
| 99 | mp_state_enum result = STATE_UNKNOWN; | ||
| 100 | char *message = NULL; | ||
| 101 | |||
| 102 | if (supported_options & UPS_STATUS) { | ||
| 103 | char *ups_status = strdup(""); | ||
| 138 | result = STATE_OK; | 104 | result = STATE_OK; |
| 139 | 105 | ||
| 140 | if (status & UPSSTATUS_OFF) { | 106 | if (ups_status_flags & UPSSTATUS_OFF) { |
| 141 | xasprintf (&ups_status, "Off"); | 107 | xasprintf(&ups_status, "Off"); |
| 142 | result = STATE_CRITICAL; | 108 | result = STATE_CRITICAL; |
| 143 | } | 109 | } else if ((ups_status_flags & (UPSSTATUS_OB | UPSSTATUS_LB)) == |
| 144 | else if ((status & (UPSSTATUS_OB | UPSSTATUS_LB)) == | 110 | (UPSSTATUS_OB | UPSSTATUS_LB)) { |
| 145 | (UPSSTATUS_OB | UPSSTATUS_LB)) { | 111 | xasprintf(&ups_status, _("On Battery, Low Battery")); |
| 146 | xasprintf (&ups_status, _("On Battery, Low Battery")); | ||
| 147 | result = STATE_CRITICAL; | 112 | result = STATE_CRITICAL; |
| 148 | } | 113 | } else { |
| 149 | else { | 114 | if (ups_status_flags & UPSSTATUS_OL) { |
| 150 | if (status & UPSSTATUS_OL) { | 115 | xasprintf(&ups_status, "%s%s", ups_status, _("Online")); |
| 151 | xasprintf (&ups_status, "%s%s", ups_status, _("Online")); | ||
| 152 | } | 116 | } |
| 153 | if (status & UPSSTATUS_OB) { | 117 | if (ups_status_flags & UPSSTATUS_OB) { |
| 154 | xasprintf (&ups_status, "%s%s", ups_status, _("On Battery")); | 118 | xasprintf(&ups_status, "%s%s", ups_status, _("On Battery")); |
| 155 | result = STATE_WARNING; | 119 | result = max_state(result, STATE_WARNING); |
| 156 | } | 120 | } |
| 157 | if (status & UPSSTATUS_LB) { | 121 | if (ups_status_flags & UPSSTATUS_LB) { |
| 158 | xasprintf (&ups_status, "%s%s", ups_status, _(", Low Battery")); | 122 | xasprintf(&ups_status, "%s%s", ups_status, _(", Low Battery")); |
| 159 | result = STATE_WARNING; | 123 | result = max_state(result, STATE_WARNING); |
| 160 | } | 124 | } |
| 161 | if (status & UPSSTATUS_CAL) { | 125 | if (ups_status_flags & UPSSTATUS_CAL) { |
| 162 | xasprintf (&ups_status, "%s%s", ups_status, _(", Calibrating")); | 126 | xasprintf(&ups_status, "%s%s", ups_status, _(", Calibrating")); |
| 163 | } | 127 | } |
| 164 | if (status & UPSSTATUS_RB) { | 128 | if (ups_status_flags & UPSSTATUS_RB) { |
| 165 | xasprintf (&ups_status, "%s%s", ups_status, _(", Replace Battery")); | 129 | xasprintf(&ups_status, "%s%s", ups_status, _(", Replace Battery")); |
| 166 | result = STATE_WARNING; | 130 | result = max_state(result, STATE_WARNING); |
| 167 | } | 131 | } |
| 168 | if (status & UPSSTATUS_BYPASS) { | 132 | if (ups_status_flags & UPSSTATUS_BYPASS) { |
| 169 | xasprintf (&ups_status, "%s%s", ups_status, _(", On Bypass")); | 133 | xasprintf(&ups_status, "%s%s", ups_status, _(", On Bypass")); |
| 134 | // Bypassing the battery is likely a bad thing | ||
| 135 | result = STATE_CRITICAL; | ||
| 136 | } | ||
| 137 | if (ups_status_flags & UPSSTATUS_OVER) { | ||
| 138 | xasprintf(&ups_status, "%s%s", ups_status, _(", Overload")); | ||
| 139 | result = max_state(result, STATE_WARNING); | ||
| 170 | } | 140 | } |
| 171 | if (status & UPSSTATUS_OVER) { | 141 | if (ups_status_flags & UPSSTATUS_TRIM) { |
| 172 | xasprintf (&ups_status, "%s%s", ups_status, _(", Overload")); | 142 | xasprintf(&ups_status, "%s%s", ups_status, _(", Trimming")); |
| 173 | } | 143 | } |
| 174 | if (status & UPSSTATUS_TRIM) { | 144 | if (ups_status_flags & UPSSTATUS_BOOST) { |
| 175 | xasprintf (&ups_status, "%s%s", ups_status, _(", Trimming")); | 145 | xasprintf(&ups_status, "%s%s", ups_status, _(", Boosting")); |
| 176 | } | 146 | } |
| 177 | if (status & UPSSTATUS_BOOST) { | 147 | if (ups_status_flags & UPSSTATUS_CHRG) { |
| 178 | xasprintf (&ups_status, "%s%s", ups_status, _(", Boosting")); | 148 | xasprintf(&ups_status, "%s%s", ups_status, _(", Charging")); |
| 179 | } | 149 | } |
| 180 | if (status & UPSSTATUS_CHRG) { | 150 | if (ups_status_flags & UPSSTATUS_DISCHRG) { |
| 181 | xasprintf (&ups_status, "%s%s", ups_status, _(", Charging")); | 151 | xasprintf(&ups_status, "%s%s", ups_status, _(", Discharging")); |
| 152 | result = max_state(result, STATE_WARNING); | ||
| 182 | } | 153 | } |
| 183 | if (status & UPSSTATUS_DISCHRG) { | 154 | if (ups_status_flags & UPSSTATUS_ALARM) { |
| 184 | xasprintf (&ups_status, "%s%s", ups_status, _(", Discharging")); | 155 | xasprintf(&ups_status, "%s%s", ups_status, _(", ALARM")); |
| 156 | result = STATE_CRITICAL; | ||
| 185 | } | 157 | } |
| 186 | if (status & UPSSTATUS_UNKNOWN) { | 158 | if (ups_status_flags & UPSSTATUS_UNKNOWN) { |
| 187 | xasprintf (&ups_status, "%s%s", ups_status, _(", Unknown")); | 159 | xasprintf(&ups_status, "%s%s", ups_status, _(", Unknown")); |
| 188 | } | 160 | } |
| 189 | } | 161 | } |
| 190 | xasprintf (&message, "%sStatus=%s ", message, ups_status); | 162 | xasprintf(&message, "%sStatus=%s ", message, ups_status); |
| 191 | } | 163 | } |
| 192 | 164 | ||
| 165 | int res; | ||
| 166 | char temp_buffer[MAX_INPUT_BUFFER]; | ||
| 167 | char *performance_data = strdup(""); | ||
| 193 | /* get the ups utility voltage if possible */ | 168 | /* get the ups utility voltage if possible */ |
| 194 | res=get_ups_variable ("input.voltage", temp_buffer); | 169 | res = get_ups_variable("input.voltage", temp_buffer, config); |
| 195 | if (res == NOSUCHVAR) supported_options &= ~UPS_UTILITY; | 170 | if (res == NOSUCHVAR) { |
| 196 | else if (res != OK) | 171 | supported_options &= ~UPS_UTILITY; |
| 172 | } else if (res != OK) { | ||
| 197 | return STATE_CRITICAL; | 173 | return STATE_CRITICAL; |
| 198 | else { | 174 | } else { |
| 199 | supported_options |= UPS_UTILITY; | 175 | supported_options |= UPS_UTILITY; |
| 200 | 176 | ||
| 201 | ups_utility_voltage = atof (temp_buffer); | 177 | double ups_utility_voltage = 0.0; |
| 202 | xasprintf (&message, "%sUtility=%3.1fV ", message, ups_utility_voltage); | 178 | ups_utility_voltage = atof(temp_buffer); |
| 179 | xasprintf(&message, "%sUtility=%3.1fV ", message, ups_utility_voltage); | ||
| 180 | |||
| 181 | double ups_utility_deviation = 0.0; | ||
| 203 | 182 | ||
| 204 | if (ups_utility_voltage > 120.0) | 183 | if (ups_utility_voltage > 120.0) { |
| 205 | ups_utility_deviation = 120.0 - ups_utility_voltage; | 184 | ups_utility_deviation = 120.0 - ups_utility_voltage; |
| 206 | else | 185 | } else { |
| 207 | ups_utility_deviation = ups_utility_voltage - 120.0; | 186 | ups_utility_deviation = ups_utility_voltage - 120.0; |
| 187 | } | ||
| 208 | 188 | ||
| 209 | if (check_variable == UPS_UTILITY) { | 189 | if (config.check_variable == UPS_UTILITY) { |
| 210 | if (check_crit && ups_utility_deviation>=critical_value) { | 190 | if (config.check_crit && ups_utility_deviation >= config.critical_value) { |
| 211 | result = STATE_CRITICAL; | 191 | result = STATE_CRITICAL; |
| 212 | } | 192 | } else if (config.check_warn && ups_utility_deviation >= config.warning_value) { |
| 213 | else if (check_warn && ups_utility_deviation>=warning_value) { | 193 | result = max_state(result, STATE_WARNING); |
| 214 | result = max_state (result, STATE_WARNING); | 194 | } |
| 215 | } | 195 | xasprintf(&performance_data, "%s", |
| 216 | xasprintf (&data, "%s", | 196 | perfdata("voltage", (long)(1000 * ups_utility_voltage), "mV", |
| 217 | perfdata ("voltage", (long)(1000*ups_utility_voltage), "mV", | 197 | config.check_warn, (long)(1000 * config.warning_value), |
| 218 | check_warn, (long)(1000*warning_value), | 198 | config.check_crit, (long)(1000 * config.critical_value), true, 0, |
| 219 | check_crit, (long)(1000*critical_value), | 199 | false, 0)); |
| 220 | true, 0, false, 0)); | ||
| 221 | } else { | 200 | } else { |
| 222 | xasprintf (&data, "%s", | 201 | xasprintf(&performance_data, "%s", |
| 223 | perfdata ("voltage", (long)(1000*ups_utility_voltage), "mV", | 202 | perfdata("voltage", (long)(1000 * ups_utility_voltage), "mV", false, 0, false, |
| 224 | false, 0, false, 0, true, 0, false, 0)); | 203 | 0, true, 0, false, 0)); |
| 225 | } | 204 | } |
| 226 | } | 205 | } |
| 227 | 206 | ||
| 228 | /* get the ups battery percent if possible */ | 207 | /* get the ups battery percent if possible */ |
| 229 | res=get_ups_variable ("battery.charge", temp_buffer); | 208 | res = get_ups_variable("battery.charge", temp_buffer, config); |
| 230 | if (res == NOSUCHVAR) supported_options &= ~UPS_BATTPCT; | 209 | if (res == NOSUCHVAR) { |
| 231 | else if ( res != OK) | 210 | supported_options &= ~UPS_BATTPCT; |
| 211 | } else if (res != OK) { | ||
| 232 | return STATE_CRITICAL; | 212 | return STATE_CRITICAL; |
| 233 | else { | 213 | } else { |
| 234 | supported_options |= UPS_BATTPCT; | 214 | supported_options |= UPS_BATTPCT; |
| 235 | ups_battery_percent = atof (temp_buffer); | ||
| 236 | xasprintf (&message, "%sBatt=%3.1f%% ", message, ups_battery_percent); | ||
| 237 | 215 | ||
| 238 | if (check_variable == UPS_BATTPCT) { | 216 | double ups_battery_percent = 0.0; |
| 239 | if (check_crit && ups_battery_percent <= critical_value) { | 217 | ups_battery_percent = atof(temp_buffer); |
| 218 | xasprintf(&message, "%sBatt=%3.1f%% ", message, ups_battery_percent); | ||
| 219 | |||
| 220 | if (config.check_variable == UPS_BATTPCT) { | ||
| 221 | if (config.check_crit && ups_battery_percent <= config.critical_value) { | ||
| 240 | result = STATE_CRITICAL; | 222 | result = STATE_CRITICAL; |
| 223 | } else if (config.check_warn && ups_battery_percent <= config.warning_value) { | ||
| 224 | result = max_state(result, STATE_WARNING); | ||
| 241 | } | 225 | } |
| 242 | else if (check_warn && ups_battery_percent<=warning_value) { | 226 | xasprintf(&performance_data, "%s %s", performance_data, |
| 243 | result = max_state (result, STATE_WARNING); | 227 | perfdata("battery", (long)ups_battery_percent, "%", config.check_warn, |
| 244 | } | 228 | (long)(config.warning_value), config.check_crit, |
| 245 | xasprintf (&data, "%s %s", data, | 229 | (long)(config.critical_value), true, 0, true, 100)); |
| 246 | perfdata ("battery", (long)ups_battery_percent, "%", | ||
| 247 | check_warn, (long)(warning_value), | ||
| 248 | check_crit, (long)(critical_value), | ||
| 249 | true, 0, true, 100)); | ||
| 250 | } else { | 230 | } else { |
| 251 | xasprintf (&data, "%s %s", data, | 231 | xasprintf(&performance_data, "%s %s", performance_data, |
| 252 | perfdata ("battery", (long)ups_battery_percent, "%", | 232 | perfdata("battery", (long)ups_battery_percent, "%", false, 0, false, 0, true, |
| 253 | false, 0, false, 0, true, 0, true, 100)); | 233 | 0, true, 100)); |
| 254 | } | 234 | } |
| 255 | } | 235 | } |
| 256 | 236 | ||
| 257 | /* get the ups load percent if possible */ | 237 | /* get the ups load percent if possible */ |
| 258 | res=get_ups_variable ("ups.load", temp_buffer); | 238 | res = get_ups_variable("ups.load", temp_buffer, config); |
| 259 | if ( res == NOSUCHVAR ) supported_options &= ~UPS_LOADPCT; | 239 | if (res == NOSUCHVAR) { |
| 260 | else if ( res != OK) | 240 | supported_options &= ~UPS_LOADPCT; |
| 241 | } else if (res != OK) { | ||
| 261 | return STATE_CRITICAL; | 242 | return STATE_CRITICAL; |
| 262 | else { | 243 | } else { |
| 263 | supported_options |= UPS_LOADPCT; | 244 | supported_options |= UPS_LOADPCT; |
| 264 | ups_load_percent = atof (temp_buffer); | ||
| 265 | xasprintf (&message, "%sLoad=%3.1f%% ", message, ups_load_percent); | ||
| 266 | 245 | ||
| 267 | if (check_variable == UPS_LOADPCT) { | 246 | double ups_load_percent = 0.0; |
| 268 | if (check_crit && ups_load_percent>=critical_value) { | 247 | ups_load_percent = atof(temp_buffer); |
| 248 | xasprintf(&message, "%sLoad=%3.1f%% ", message, ups_load_percent); | ||
| 249 | |||
| 250 | if (config.check_variable == UPS_LOADPCT) { | ||
| 251 | if (config.check_crit && ups_load_percent >= config.critical_value) { | ||
| 269 | result = STATE_CRITICAL; | 252 | result = STATE_CRITICAL; |
| 253 | } else if (config.check_warn && ups_load_percent >= config.warning_value) { | ||
| 254 | result = max_state(result, STATE_WARNING); | ||
| 270 | } | 255 | } |
| 271 | else if (check_warn && ups_load_percent>=warning_value) { | 256 | xasprintf(&performance_data, "%s %s", performance_data, |
| 272 | result = max_state (result, STATE_WARNING); | 257 | perfdata("load", (long)ups_load_percent, "%", config.check_warn, |
| 273 | } | 258 | (long)(config.warning_value), config.check_crit, |
| 274 | xasprintf (&data, "%s %s", data, | 259 | (long)(config.critical_value), true, 0, true, 100)); |
| 275 | perfdata ("load", (long)ups_load_percent, "%", | ||
| 276 | check_warn, (long)(warning_value), | ||
| 277 | check_crit, (long)(critical_value), | ||
| 278 | true, 0, true, 100)); | ||
| 279 | } else { | 260 | } else { |
| 280 | xasprintf (&data, "%s %s", data, | 261 | xasprintf(&performance_data, "%s %s", performance_data, |
| 281 | perfdata ("load", (long)ups_load_percent, "%", | 262 | perfdata("load", (long)ups_load_percent, "%", false, 0, false, 0, true, 0, |
| 282 | false, 0, false, 0, true, 0, true, 100)); | 263 | true, 100)); |
| 283 | } | 264 | } |
| 284 | } | 265 | } |
| 285 | 266 | ||
| 286 | /* get the ups temperature if possible */ | 267 | /* get the ups temperature if possible */ |
| 287 | res=get_ups_variable ("ups.temperature", temp_buffer); | 268 | res = get_ups_variable("ups.temperature", temp_buffer, config); |
| 288 | if ( res == NOSUCHVAR ) supported_options &= ~UPS_TEMP; | 269 | if (res == NOSUCHVAR) { |
| 289 | else if ( res != OK) | 270 | supported_options &= ~UPS_TEMP; |
| 271 | } else if (res != OK) { | ||
| 290 | return STATE_CRITICAL; | 272 | return STATE_CRITICAL; |
| 291 | else { | 273 | } else { |
| 292 | supported_options |= UPS_TEMP; | 274 | supported_options |= UPS_TEMP; |
| 293 | if (temp_output_c) { | 275 | |
| 294 | tunits="degC"; | 276 | double ups_temperature = 0.0; |
| 295 | ups_temperature = atof (temp_buffer); | 277 | char *tunits; |
| 296 | xasprintf (&message, "%sTemp=%3.1fC", message, ups_temperature); | 278 | |
| 297 | } | 279 | if (config.temp_output_c) { |
| 298 | else { | 280 | tunits = "degC"; |
| 299 | tunits="degF"; | 281 | ups_temperature = atof(temp_buffer); |
| 300 | ups_temperature = (atof (temp_buffer) * 1.8) + 32; | 282 | xasprintf(&message, "%sTemp=%3.1fC", message, ups_temperature); |
| 301 | xasprintf (&message, "%sTemp=%3.1fF", message, ups_temperature); | 283 | } else { |
| 284 | tunits = "degF"; | ||
| 285 | ups_temperature = (atof(temp_buffer) * 1.8) + 32; | ||
| 286 | xasprintf(&message, "%sTemp=%3.1fF", message, ups_temperature); | ||
| 302 | } | 287 | } |
| 303 | 288 | ||
| 304 | if (check_variable == UPS_TEMP) { | 289 | if (config.check_variable == UPS_TEMP) { |
| 305 | if (check_crit && ups_temperature>=critical_value) { | 290 | if (config.check_crit && ups_temperature >= config.critical_value) { |
| 306 | result = STATE_CRITICAL; | 291 | result = STATE_CRITICAL; |
| 292 | } else if (config.check_warn && ups_temperature >= config.warning_value) { | ||
| 293 | result = max_state(result, STATE_WARNING); | ||
| 307 | } | 294 | } |
| 308 | else if (check_warn && ups_temperature>=warning_value) { | 295 | xasprintf(&performance_data, "%s %s", performance_data, |
| 309 | result = max_state (result, STATE_WARNING); | 296 | perfdata("temp", (long)ups_temperature, tunits, config.check_warn, |
| 310 | } | 297 | (long)(config.warning_value), config.check_crit, |
| 311 | xasprintf (&data, "%s %s", data, | 298 | (long)(config.critical_value), true, 0, false, 0)); |
| 312 | perfdata ("temp", (long)ups_temperature, tunits, | ||
| 313 | check_warn, (long)(warning_value), | ||
| 314 | check_crit, (long)(critical_value), | ||
| 315 | true, 0, false, 0)); | ||
| 316 | } else { | 299 | } else { |
| 317 | xasprintf (&data, "%s %s", data, | 300 | xasprintf(&performance_data, "%s %s", performance_data, |
| 318 | perfdata ("temp", (long)ups_temperature, tunits, | 301 | perfdata("temp", (long)ups_temperature, tunits, false, 0, false, 0, true, 0, |
| 319 | false, 0, false, 0, true, 0, false, 0)); | 302 | false, 0)); |
| 320 | } | 303 | } |
| 321 | } | 304 | } |
| 322 | 305 | ||
| 323 | /* get the ups real power if possible */ | 306 | /* get the ups real power if possible */ |
| 324 | res=get_ups_variable ("ups.realpower", temp_buffer); | 307 | res = get_ups_variable("ups.realpower", temp_buffer, config); |
| 325 | if ( res == NOSUCHVAR ) supported_options &= ~UPS_REALPOWER; | 308 | if (res == NOSUCHVAR) { |
| 326 | else if ( res != OK) | 309 | supported_options &= ~UPS_REALPOWER; |
| 310 | } else if (res != OK) { | ||
| 327 | return STATE_CRITICAL; | 311 | return STATE_CRITICAL; |
| 328 | else { | 312 | } else { |
| 329 | supported_options |= UPS_REALPOWER; | 313 | supported_options |= UPS_REALPOWER; |
| 330 | ups_realpower = atof (temp_buffer); | 314 | double ups_realpower = 0.0; |
| 331 | xasprintf (&message, "%sReal power=%3.1fW ", message, ups_realpower); | 315 | ups_realpower = atof(temp_buffer); |
| 316 | xasprintf(&message, "%sReal power=%3.1fW ", message, ups_realpower); | ||
| 332 | 317 | ||
| 333 | if (check_variable == UPS_REALPOWER) { | 318 | if (config.check_variable == UPS_REALPOWER) { |
| 334 | if (check_crit && ups_realpower>=critical_value) { | 319 | if (config.check_crit && ups_realpower >= config.critical_value) { |
| 335 | result = STATE_CRITICAL; | 320 | result = STATE_CRITICAL; |
| 321 | } else if (config.check_warn && ups_realpower >= config.warning_value) { | ||
| 322 | result = max_state(result, STATE_WARNING); | ||
| 336 | } | 323 | } |
| 337 | else if (check_warn && ups_realpower>=warning_value) { | 324 | xasprintf(&performance_data, "%s %s", performance_data, |
| 338 | result = max_state (result, STATE_WARNING); | 325 | perfdata("realpower", (long)ups_realpower, "W", config.check_warn, |
| 339 | } | 326 | (long)(config.warning_value), config.check_crit, |
| 340 | xasprintf (&data, "%s %s", data, | 327 | (long)(config.critical_value), true, 0, false, 0)); |
| 341 | perfdata ("realpower", (long)ups_realpower, "W", | ||
| 342 | check_warn, (long)(warning_value), | ||
| 343 | check_crit, (long)(critical_value), | ||
| 344 | true, 0, false, 0)); | ||
| 345 | } else { | 328 | } else { |
| 346 | xasprintf (&data, "%s %s", data, | 329 | xasprintf(&performance_data, "%s %s", performance_data, |
| 347 | perfdata ("realpower", (long)ups_realpower, "W", | 330 | perfdata("realpower", (long)ups_realpower, "W", false, 0, false, 0, true, 0, |
| 348 | false, 0, false, 0, true, 0, false, 0)); | 331 | false, 0)); |
| 349 | } | 332 | } |
| 350 | } | 333 | } |
| 351 | 334 | ||
| 352 | /* if the UPS does not support any options we are looking for, report an error */ | 335 | /* if the UPS does not support any options we are looking for, report an |
| 336 | * error */ | ||
| 353 | if (supported_options == UPS_NONE) { | 337 | if (supported_options == UPS_NONE) { |
| 354 | result = STATE_CRITICAL; | 338 | result = STATE_CRITICAL; |
| 355 | xasprintf (&message, _("UPS does not support any available options\n")); | 339 | xasprintf(&message, _("UPS does not support any available options\n")); |
| 356 | } | 340 | } |
| 357 | 341 | ||
| 358 | /* reset timeout */ | 342 | /* reset timeout */ |
| 359 | alarm (0); | 343 | alarm(0); |
| 360 | 344 | ||
| 361 | printf ("UPS %s - %s|%s\n", state_text(result), message, data); | 345 | printf("UPS %s - %s|%s\n", state_text(result), message, performance_data); |
| 362 | return result; | 346 | exit(result); |
| 363 | } | 347 | } |
| 364 | 348 | ||
| 349 | /* determines what options are supported by the UPS */ | ||
| 350 | determine_status_result determine_status(const check_ups_config config) { | ||
| 365 | 351 | ||
| 352 | determine_status_result result = { | ||
| 353 | .errorcode = OK, | ||
| 354 | .ups_status = UPSSTATUS_NONE, | ||
| 355 | .supported_options = 0, | ||
| 356 | }; | ||
| 366 | 357 | ||
| 367 | /* determines what options are supported by the UPS */ | ||
| 368 | int | ||
| 369 | determine_status (void) | ||
| 370 | { | ||
| 371 | char recv_buffer[MAX_INPUT_BUFFER]; | 358 | char recv_buffer[MAX_INPUT_BUFFER]; |
| 372 | char temp_buffer[MAX_INPUT_BUFFER]; | 359 | int res = get_ups_variable("ups.status", recv_buffer, config); |
| 373 | char *ptr; | 360 | if (res == NOSUCHVAR) { |
| 374 | int res; | 361 | return result; |
| 362 | } | ||
| 375 | 363 | ||
| 376 | res=get_ups_variable ("ups.status", recv_buffer); | ||
| 377 | if (res == NOSUCHVAR) return OK; | ||
| 378 | if (res != STATE_OK) { | 364 | if (res != STATE_OK) { |
| 379 | printf ("%s\n", _("Invalid response received from host")); | 365 | printf("%s\n", _("Invalid response received from host")); |
| 380 | return ERROR; | 366 | result.errorcode = ERROR; |
| 367 | return result; | ||
| 381 | } | 368 | } |
| 382 | 369 | ||
| 383 | supported_options |= UPS_STATUS; | 370 | result.supported_options |= UPS_STATUS; |
| 384 | 371 | ||
| 385 | strcpy (temp_buffer, recv_buffer); | 372 | char temp_buffer[MAX_INPUT_BUFFER]; |
| 386 | for (ptr = (char *) strtok (temp_buffer, " "); ptr != NULL; | 373 | |
| 387 | ptr = (char *) strtok (NULL, " ")) { | 374 | strcpy(temp_buffer, recv_buffer); |
| 388 | if (!strcmp (ptr, "OFF")) | 375 | for (char *ptr = strtok(temp_buffer, " "); ptr != NULL; ptr = strtok(NULL, " ")) { |
| 389 | status |= UPSSTATUS_OFF; | 376 | if (!strcmp(ptr, "OFF")) { |
| 390 | else if (!strcmp (ptr, "OL")) | 377 | result.ups_status |= UPSSTATUS_OFF; |
| 391 | status |= UPSSTATUS_OL; | 378 | } else if (!strcmp(ptr, "OL")) { |
| 392 | else if (!strcmp (ptr, "OB")) | 379 | result.ups_status |= UPSSTATUS_OL; |
| 393 | status |= UPSSTATUS_OB; | 380 | } else if (!strcmp(ptr, "OB")) { |
| 394 | else if (!strcmp (ptr, "LB")) | 381 | result.ups_status |= UPSSTATUS_OB; |
| 395 | status |= UPSSTATUS_LB; | 382 | } else if (!strcmp(ptr, "LB")) { |
| 396 | else if (!strcmp (ptr, "CAL")) | 383 | result.ups_status |= UPSSTATUS_LB; |
| 397 | status |= UPSSTATUS_CAL; | 384 | } else if (!strcmp(ptr, "CAL")) { |
| 398 | else if (!strcmp (ptr, "RB")) | 385 | result.ups_status |= UPSSTATUS_CAL; |
| 399 | status |= UPSSTATUS_RB; | 386 | } else if (!strcmp(ptr, "RB")) { |
| 400 | else if (!strcmp (ptr, "BYPASS")) | 387 | result.ups_status |= UPSSTATUS_RB; |
| 401 | status |= UPSSTATUS_BYPASS; | 388 | } else if (!strcmp(ptr, "BYPASS")) { |
| 402 | else if (!strcmp (ptr, "OVER")) | 389 | result.ups_status |= UPSSTATUS_BYPASS; |
| 403 | status |= UPSSTATUS_OVER; | 390 | } else if (!strcmp(ptr, "OVER")) { |
| 404 | else if (!strcmp (ptr, "TRIM")) | 391 | result.ups_status |= UPSSTATUS_OVER; |
| 405 | status |= UPSSTATUS_TRIM; | 392 | } else if (!strcmp(ptr, "TRIM")) { |
| 406 | else if (!strcmp (ptr, "BOOST")) | 393 | result.ups_status |= UPSSTATUS_TRIM; |
| 407 | status |= UPSSTATUS_BOOST; | 394 | } else if (!strcmp(ptr, "BOOST")) { |
| 408 | else if (!strcmp (ptr, "CHRG")) | 395 | result.ups_status |= UPSSTATUS_BOOST; |
| 409 | status |= UPSSTATUS_CHRG; | 396 | } else if (!strcmp(ptr, "CHRG")) { |
| 410 | else if (!strcmp (ptr, "DISCHRG")) | 397 | result.ups_status |= UPSSTATUS_CHRG; |
| 411 | status |= UPSSTATUS_DISCHRG; | 398 | } else if (!strcmp(ptr, "DISCHRG")) { |
| 412 | else | 399 | result.ups_status |= UPSSTATUS_DISCHRG; |
| 413 | status |= UPSSTATUS_UNKNOWN; | 400 | } else if (!strcmp(ptr, "ALARM")) { |
| 401 | result.ups_status |= UPSSTATUS_ALARM; | ||
| 402 | } else { | ||
| 403 | result.ups_status |= UPSSTATUS_UNKNOWN; | ||
| 404 | } | ||
| 414 | } | 405 | } |
| 415 | 406 | ||
| 416 | return OK; | 407 | return result; |
| 417 | } | 408 | } |
| 418 | 409 | ||
| 419 | |||
| 420 | /* gets a variable value for a specific UPS */ | 410 | /* gets a variable value for a specific UPS */ |
| 421 | int | 411 | int get_ups_variable(const char *varname, char *buf, const check_ups_config config) { |
| 422 | get_ups_variable (const char *varname, char *buf) | ||
| 423 | { | ||
| 424 | /* char command[MAX_INPUT_BUFFER]; */ | ||
| 425 | char temp_buffer[MAX_INPUT_BUFFER]; | ||
| 426 | char send_buffer[MAX_INPUT_BUFFER]; | 412 | char send_buffer[MAX_INPUT_BUFFER]; |
| 427 | char *ptr; | ||
| 428 | char *logout = "OK Goodbye\n"; | ||
| 429 | int logout_len = strlen(logout); | ||
| 430 | int len; | ||
| 431 | |||
| 432 | *buf=0; | ||
| 433 | 413 | ||
| 434 | /* create the command string to send to the UPS daemon */ | 414 | /* create the command string to send to the UPS daemon */ |
| 435 | /* Add LOGOUT to avoid read failure logs */ | 415 | /* Add LOGOUT to avoid read failure logs */ |
| 436 | int res = snprintf (send_buffer, sizeof(send_buffer), "GET VAR %s %s\nLOGOUT\n", ups_name, varname); | 416 | int res = snprintf(send_buffer, sizeof(send_buffer), "GET VAR %s %s\nLOGOUT\n", config.ups_name, |
| 437 | if ( (res > 0) && ((size_t)res >= sizeof(send_buffer))) { | 417 | varname); |
| 418 | if ((res > 0) && ((size_t)res >= sizeof(send_buffer))) { | ||
| 438 | printf("%s\n", _("UPS name to long for buffer")); | 419 | printf("%s\n", _("UPS name to long for buffer")); |
| 439 | return ERROR; | 420 | return ERROR; |
| 440 | } | 421 | } |
| 441 | 422 | ||
| 423 | char temp_buffer[MAX_INPUT_BUFFER]; | ||
| 424 | |||
| 442 | /* send the command to the daemon and get a response back */ | 425 | /* send the command to the daemon and get a response back */ |
| 443 | if (process_tcp_request | 426 | if (process_tcp_request(config.server_address, config.server_port, send_buffer, temp_buffer, |
| 444 | (server_address, server_port, send_buffer, temp_buffer, | 427 | sizeof(temp_buffer)) != STATE_OK) { |
| 445 | sizeof (temp_buffer)) != STATE_OK) { | 428 | printf("%s\n", _("Invalid response received from host")); |
| 446 | printf ("%s\n", _("Invalid response received from host")); | ||
| 447 | return ERROR; | 429 | return ERROR; |
| 448 | } | 430 | } |
| 449 | 431 | ||
| 450 | ptr = temp_buffer; | 432 | char *ptr = temp_buffer; |
| 451 | len = strlen(ptr); | 433 | int len = strlen(ptr); |
| 452 | if (len > logout_len && strcmp (ptr + len - logout_len, logout) == 0) len -= logout_len; | 434 | const char *logout = "OK Goodbye\n"; |
| 453 | if (len > 0 && ptr[len-1] == '\n') ptr[len-1]=0; | 435 | const int logout_len = strlen(logout); |
| 454 | if (strcmp (ptr, "ERR UNKNOWN-UPS") == 0) { | 436 | |
| 455 | printf (_("CRITICAL - no such UPS '%s' on that host\n"), ups_name); | 437 | if (len > logout_len && strcmp(ptr + len - logout_len, logout) == 0) { |
| 438 | len -= logout_len; | ||
| 439 | } | ||
| 440 | if (len > 0 && ptr[len - 1] == '\n') { | ||
| 441 | ptr[len - 1] = 0; | ||
| 442 | } | ||
| 443 | if (strcmp(ptr, "ERR UNKNOWN-UPS") == 0) { | ||
| 444 | printf(_("CRITICAL - no such UPS '%s' on that host\n"), config.ups_name); | ||
| 456 | return ERROR; | 445 | return ERROR; |
| 457 | } | 446 | } |
| 458 | 447 | ||
| 459 | if (strcmp (ptr, "ERR VAR-NOT-SUPPORTED") == 0) { | 448 | if (strcmp(ptr, "ERR VAR-NOT-SUPPORTED") == 0) { |
| 460 | /*printf ("Error: Variable '%s' is not supported\n", varname);*/ | 449 | /*printf ("Error: Variable '%s' is not supported\n", varname);*/ |
| 461 | return NOSUCHVAR; | 450 | return NOSUCHVAR; |
| 462 | } | 451 | } |
| 463 | 452 | ||
| 464 | if (strcmp (ptr, "ERR DATA-STALE") == 0) { | 453 | if (strcmp(ptr, "ERR DATA-STALE") == 0) { |
| 465 | printf ("%s\n", _("CRITICAL - UPS data is stale")); | 454 | printf("%s\n", _("CRITICAL - UPS data is stale")); |
| 466 | return ERROR; | 455 | return ERROR; |
| 467 | } | 456 | } |
| 468 | 457 | ||
| 469 | if (strncmp (ptr, "ERR", 3) == 0) { | 458 | if (strncmp(ptr, "ERR", 3) == 0) { |
| 470 | printf (_("Unknown error: %s\n"), ptr); | 459 | printf(_("Unknown error: %s\n"), ptr); |
| 471 | return ERROR; | 460 | return ERROR; |
| 472 | } | 461 | } |
| 473 | 462 | ||
| 474 | ptr = temp_buffer + strlen (varname) + strlen (ups_name) + 6; | 463 | ptr = temp_buffer + strlen(varname) + strlen(config.ups_name) + 6; |
| 475 | len = strlen(ptr); | 464 | len = strlen(ptr); |
| 476 | if (len < 2 || ptr[0] != '"' || ptr[len-1] != '"') { | 465 | if (len < 2 || ptr[0] != '"' || ptr[len - 1] != '"') { |
| 477 | printf ("%s\n", _("Error: unable to parse variable")); | 466 | printf("%s\n", _("Error: unable to parse variable")); |
| 478 | return ERROR; | 467 | return ERROR; |
| 479 | } | 468 | } |
| 480 | strncpy (buf, ptr+1, len - 2); | 469 | |
| 470 | *buf = 0; | ||
| 471 | strncpy(buf, ptr + 1, len - 2); | ||
| 481 | buf[len - 2] = 0; | 472 | buf[len - 2] = 0; |
| 482 | 473 | ||
| 483 | return OK; | 474 | return OK; |
| 484 | } | 475 | } |
| 485 | 476 | ||
| 486 | |||
| 487 | /* Command line: CHECK_UPS -H <host_address> -u ups [-p port] [-v variable] | 477 | /* Command line: CHECK_UPS -H <host_address> -u ups [-p port] [-v variable] |
| 488 | [-wv warn_value] [-cv crit_value] [-to to_sec] */ | 478 | [-wv warn_value] [-cv crit_value] [-to to_sec] */ |
| 489 | 479 | ||
| 490 | |||
| 491 | /* process command-line arguments */ | 480 | /* process command-line arguments */ |
| 492 | int | 481 | check_ups_config_wrapper process_arguments(int argc, char **argv) { |
| 493 | process_arguments (int argc, char **argv) | 482 | |
| 494 | { | 483 | static struct option longopts[] = {{"hostname", required_argument, 0, 'H'}, |
| 495 | int c; | 484 | {"ups", required_argument, 0, 'u'}, |
| 496 | 485 | {"port", required_argument, 0, 'p'}, | |
| 497 | int option = 0; | 486 | {"critical", required_argument, 0, 'c'}, |
| 498 | static struct option longopts[] = { | 487 | {"warning", required_argument, 0, 'w'}, |
| 499 | {"hostname", required_argument, 0, 'H'}, | 488 | {"timeout", required_argument, 0, 't'}, |
| 500 | {"ups", required_argument, 0, 'u'}, | 489 | {"temperature", no_argument, 0, 'T'}, |
| 501 | {"port", required_argument, 0, 'p'}, | 490 | {"variable", required_argument, 0, 'v'}, |
| 502 | {"critical", required_argument, 0, 'c'}, | 491 | {"version", no_argument, 0, 'V'}, |
| 503 | {"warning", required_argument, 0, 'w'}, | 492 | {"help", no_argument, 0, 'h'}, |
| 504 | {"timeout", required_argument, 0, 't'}, | 493 | {0, 0, 0, 0}}; |
| 505 | {"temperature", no_argument, 0, 'T'}, | 494 | |
| 506 | {"variable", required_argument, 0, 'v'}, | 495 | check_ups_config_wrapper result = { |
| 507 | {"version", no_argument, 0, 'V'}, | 496 | .errorcode = OK, |
| 508 | {"help", no_argument, 0, 'h'}, | 497 | .config = check_ups_config_init(), |
| 509 | {0, 0, 0, 0} | ||
| 510 | }; | 498 | }; |
| 511 | 499 | ||
| 512 | if (argc < 2) | 500 | if (argc < 2) { |
| 513 | return ERROR; | 501 | result.errorcode = ERROR; |
| 502 | return result; | ||
| 503 | } | ||
| 514 | 504 | ||
| 505 | int c; | ||
| 515 | for (c = 1; c < argc; c++) { | 506 | for (c = 1; c < argc; c++) { |
| 516 | if (strcmp ("-to", argv[c]) == 0) | 507 | if (strcmp("-to", argv[c]) == 0) { |
| 517 | strcpy (argv[c], "-t"); | 508 | strcpy(argv[c], "-t"); |
| 518 | else if (strcmp ("-wt", argv[c]) == 0) | 509 | } else if (strcmp("-wt", argv[c]) == 0) { |
| 519 | strcpy (argv[c], "-w"); | 510 | strcpy(argv[c], "-w"); |
| 520 | else if (strcmp ("-ct", argv[c]) == 0) | 511 | } else if (strcmp("-ct", argv[c]) == 0) { |
| 521 | strcpy (argv[c], "-c"); | 512 | strcpy(argv[c], "-c"); |
| 513 | } | ||
| 522 | } | 514 | } |
| 523 | 515 | ||
| 516 | int option = 0; | ||
| 524 | while (1) { | 517 | while (1) { |
| 525 | c = getopt_long (argc, argv, "hVTH:u:p:v:c:w:t:", longopts, | 518 | c = getopt_long(argc, argv, "hVTH:u:p:v:c:w:t:", longopts, &option); |
| 526 | &option); | ||
| 527 | 519 | ||
| 528 | if (c == -1 || c == EOF) | 520 | if (c == -1 || c == EOF) { |
| 529 | break; | 521 | break; |
| 522 | } | ||
| 530 | 523 | ||
| 531 | switch (c) { | 524 | switch (c) { |
| 532 | case '?': /* help */ | 525 | case '?': /* help */ |
| 533 | usage5 (); | 526 | usage5(); |
| 534 | case 'H': /* hostname */ | 527 | case 'H': /* hostname */ |
| 535 | if (is_host (optarg)) { | 528 | if (is_host(optarg)) { |
| 536 | server_address = optarg; | 529 | result.config.server_address = optarg; |
| 537 | } | 530 | } else { |
| 538 | else { | 531 | usage2(_("Invalid hostname/address"), optarg); |
| 539 | usage2 (_("Invalid hostname/address"), optarg); | ||
| 540 | } | 532 | } |
| 541 | break; | 533 | break; |
| 542 | case 'T': /* FIXME: to be improved (ie "-T C" for Celsius or "-T F" for Fahrenheit) */ | 534 | case 'T': /* FIXME: to be improved (ie "-T C" for Celsius or "-T F" for |
| 543 | temp_output_c = true; | 535 | Fahrenheit) */ |
| 536 | result.config.temp_output_c = true; | ||
| 544 | break; | 537 | break; |
| 545 | case 'u': /* ups name */ | 538 | case 'u': /* ups name */ |
| 546 | ups_name = optarg; | 539 | result.config.ups_name = optarg; |
| 547 | break; | 540 | break; |
| 548 | case 'p': /* port */ | 541 | case 'p': /* port */ |
| 549 | if (is_intpos (optarg)) { | 542 | if (is_intpos(optarg)) { |
| 550 | server_port = atoi (optarg); | 543 | result.config.server_port = atoi(optarg); |
| 551 | } | 544 | } else { |
| 552 | else { | 545 | usage2(_("Port must be a positive integer"), optarg); |
| 553 | usage2 (_("Port must be a positive integer"), optarg); | ||
| 554 | } | 546 | } |
| 555 | break; | 547 | break; |
| 556 | case 'c': /* critical time threshold */ | 548 | case 'c': /* critical time threshold */ |
| 557 | if (is_intnonneg (optarg)) { | 549 | if (is_intnonneg(optarg)) { |
| 558 | critical_value = atoi (optarg); | 550 | result.config.critical_value = atoi(optarg); |
| 559 | check_crit = true; | 551 | result.config.check_crit = true; |
| 560 | } | 552 | } else { |
| 561 | else { | 553 | usage2(_("Critical time must be a positive integer"), optarg); |
| 562 | usage2 (_("Critical time must be a positive integer"), optarg); | ||
| 563 | } | 554 | } |
| 564 | break; | 555 | break; |
| 565 | case 'w': /* warning time threshold */ | 556 | case 'w': /* warning time threshold */ |
| 566 | if (is_intnonneg (optarg)) { | 557 | if (is_intnonneg(optarg)) { |
| 567 | warning_value = atoi (optarg); | 558 | result.config.warning_value = atoi(optarg); |
| 568 | check_warn = true; | 559 | result.config.check_warn = true; |
| 569 | } | 560 | } else { |
| 570 | else { | 561 | usage2(_("Warning time must be a positive integer"), optarg); |
| 571 | usage2 (_("Warning time must be a positive integer"), optarg); | ||
| 572 | } | 562 | } |
| 573 | break; | 563 | break; |
| 574 | case 'v': /* variable */ | 564 | case 'v': /* variable */ |
| 575 | if (!strcmp (optarg, "LINE")) | 565 | if (!strcmp(optarg, "LINE")) { |
| 576 | check_variable = UPS_UTILITY; | 566 | result.config.check_variable = UPS_UTILITY; |
| 577 | else if (!strcmp (optarg, "TEMP")) | 567 | } else if (!strcmp(optarg, "TEMP")) { |
| 578 | check_variable = UPS_TEMP; | 568 | result.config.check_variable = UPS_TEMP; |
| 579 | else if (!strcmp (optarg, "BATTPCT")) | 569 | } else if (!strcmp(optarg, "BATTPCT")) { |
| 580 | check_variable = UPS_BATTPCT; | 570 | result.config.check_variable = UPS_BATTPCT; |
| 581 | else if (!strcmp (optarg, "LOADPCT")) | 571 | } else if (!strcmp(optarg, "LOADPCT")) { |
| 582 | check_variable = UPS_LOADPCT; | 572 | result.config.check_variable = UPS_LOADPCT; |
| 583 | else if (!strcmp (optarg, "REALPOWER")) | 573 | } else if (!strcmp(optarg, "REALPOWER")) { |
| 584 | check_variable = UPS_REALPOWER; | 574 | result.config.check_variable = UPS_REALPOWER; |
| 585 | else | 575 | } else { |
| 586 | usage2 (_("Unrecognized UPS variable"), optarg); | 576 | usage2(_("Unrecognized UPS variable"), optarg); |
| 587 | break; | ||
| 588 | case 't': /* timeout */ | ||
| 589 | if (is_intnonneg (optarg)) { | ||
| 590 | socket_timeout = atoi (optarg); | ||
| 591 | } | 577 | } |
| 592 | else { | 578 | break; |
| 593 | usage4 (_("Timeout interval must be a positive integer")); | 579 | case 't': /* timeout */ |
| 580 | if (is_intnonneg(optarg)) { | ||
| 581 | socket_timeout = atoi(optarg); | ||
| 582 | } else { | ||
| 583 | usage4(_("Timeout interval must be a positive integer")); | ||
| 594 | } | 584 | } |
| 595 | break; | 585 | break; |
| 596 | case 'V': /* version */ | 586 | case 'V': /* version */ |
| 597 | print_revision (progname, NP_VERSION); | 587 | print_revision(progname, NP_VERSION); |
| 598 | exit (STATE_UNKNOWN); | 588 | exit(STATE_UNKNOWN); |
| 599 | case 'h': /* help */ | 589 | case 'h': /* help */ |
| 600 | print_help (); | 590 | print_help(); |
| 601 | exit (STATE_UNKNOWN); | 591 | exit(STATE_UNKNOWN); |
| 602 | } | 592 | } |
| 603 | } | 593 | } |
| 604 | 594 | ||
| 605 | 595 | if (result.config.server_address == NULL && argc > optind) { | |
| 606 | if (server_address == NULL && argc > optind) { | 596 | if (is_host(argv[optind])) { |
| 607 | if (is_host (argv[optind])) | 597 | result.config.server_address = argv[optind++]; |
| 608 | server_address = argv[optind++]; | 598 | } else { |
| 609 | else | 599 | usage2(_("Invalid hostname/address"), optarg); |
| 610 | usage2 (_("Invalid hostname/address"), optarg); | 600 | } |
| 611 | } | 601 | } |
| 612 | 602 | ||
| 613 | if (server_address == NULL) | 603 | if (result.config.server_address == NULL) { |
| 614 | server_address = strdup("127.0.0.1"); | 604 | result.config.server_address = strdup("127.0.0.1"); |
| 605 | } | ||
| 615 | 606 | ||
| 616 | return validate_arguments(); | 607 | return validate_arguments(result); |
| 617 | } | 608 | } |
| 618 | 609 | ||
| 619 | 610 | check_ups_config_wrapper validate_arguments(check_ups_config_wrapper config_wrapper) { | |
| 620 | int | 611 | if (config_wrapper.config.ups_name) { |
| 621 | validate_arguments (void) | 612 | printf("%s\n", _("Error : no UPS indicated")); |
| 622 | { | 613 | config_wrapper.errorcode = ERROR; |
| 623 | if (! ups_name) { | ||
| 624 | printf ("%s\n", _("Error : no UPS indicated")); | ||
| 625 | return ERROR; | ||
| 626 | } | 614 | } |
| 627 | return OK; | 615 | return config_wrapper; |
| 628 | } | 616 | } |
| 629 | 617 | ||
| 618 | void print_help(void) { | ||
| 619 | print_revision(progname, NP_VERSION); | ||
| 630 | 620 | ||
| 631 | void | 621 | printf("Copyright (c) 2000 Tom Shields\n"); |
| 632 | print_help (void) | 622 | printf("Copyright (c) 2004 Alain Richard <alain.richard@equation.fr>\n"); |
| 633 | { | 623 | printf("Copyright (c) 2004 Arnaud Quette <arnaud.quette@mgeups.com>\n"); |
| 634 | char *myport; | 624 | printf(COPYRIGHT, copyright, email); |
| 635 | xasprintf (&myport, "%d", PORT); | ||
| 636 | |||
| 637 | print_revision (progname, NP_VERSION); | ||
| 638 | |||
| 639 | printf ("Copyright (c) 2000 Tom Shields\n"); | ||
| 640 | printf ("Copyright (c) 2004 Alain Richard <alain.richard@equation.fr>\n"); | ||
| 641 | printf ("Copyright (c) 2004 Arnaud Quette <arnaud.quette@mgeups.com>\n"); | ||
| 642 | printf (COPYRIGHT, copyright, email); | ||
| 643 | |||
| 644 | printf ("%s\n", _("This plugin tests the UPS service on the specified host. Network UPS Tools")); | ||
| 645 | printf ("%s\n", _("from www.networkupstools.org must be running for this plugin to work.")); | ||
| 646 | |||
| 647 | printf ("\n\n"); | ||
| 648 | |||
| 649 | print_usage (); | ||
| 650 | 625 | ||
| 651 | printf (UT_HELP_VRSN); | 626 | printf("%s\n", _("This plugin tests the UPS service on the specified host. " |
| 652 | printf (UT_EXTRA_OPTS); | 627 | "Network UPS Tools")); |
| 628 | printf("%s\n", _("from www.networkupstools.org must be running for this " | ||
| 629 | "plugin to work.")); | ||
| 653 | 630 | ||
| 654 | printf (UT_HOST_PORT, 'p', myport); | 631 | printf("\n\n"); |
| 655 | 632 | ||
| 656 | printf (" %s\n", "-u, --ups=STRING"); | 633 | print_usage(); |
| 657 | printf (" %s\n", _("Name of UPS")); | ||
| 658 | printf (" %s\n", "-T, --temperature"); | ||
| 659 | printf (" %s\n", _("Output of temperatures in Celsius")); | ||
| 660 | printf (" %s\n", "-v, --variable=STRING"); | ||
| 661 | printf (" %s %s\n", _("Valid values for STRING are"), "LINE, TEMP, BATTPCT, LOADPCT or REALPOWER"); | ||
| 662 | 634 | ||
| 663 | printf (UT_WARN_CRIT); | 635 | printf(UT_HELP_VRSN); |
| 636 | printf(UT_EXTRA_OPTS); | ||
| 664 | 637 | ||
| 665 | printf (UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT); | 638 | char *myport; |
| 666 | 639 | xasprintf(&myport, "%d", PORT); | |
| 667 | /* TODO: -v clashing with -v/-variable. Commenting out help text since verbose | 640 | printf(UT_HOST_PORT, 'p', myport); |
| 668 | is unused up to now */ | 641 | |
| 669 | /* printf (UT_VERBOSE); */ | 642 | printf(" %s\n", "-u, --ups=STRING"); |
| 670 | 643 | printf(" %s\n", _("Name of UPS")); | |
| 671 | printf ("\n"); | 644 | printf(" %s\n", "-T, --temperature"); |
| 672 | printf ("%s\n", _("This plugin attempts to determine the status of a UPS (Uninterruptible Power")); | 645 | printf(" %s\n", _("Output of temperatures in Celsius")); |
| 673 | printf ("%s\n", _("Supply) on a local or remote host. If the UPS is online or calibrating, the")); | 646 | printf(" %s\n", "-v, --variable=STRING"); |
| 674 | printf ("%s\n", _("plugin will return an OK state. If the battery is on it will return a WARNING")); | 647 | printf(" %s %s\n", _("Valid values for STRING are"), |
| 675 | printf ("%s\n", _("state. If the UPS is off or has a low battery the plugin will return a CRITICAL")); | 648 | "LINE, TEMP, BATTPCT, LOADPCT or REALPOWER"); |
| 676 | printf ("%s\n", _("state.")); | 649 | |
| 677 | 650 | printf(UT_WARN_CRIT); | |
| 678 | printf ("\n"); | 651 | |
| 679 | printf ("%s\n", _("Notes:")); | 652 | printf(UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT); |
| 680 | printf (" %s\n", _("You may also specify a variable to check (such as temperature, utility voltage,")); | 653 | |
| 681 | printf (" %s\n", _("battery load, etc.) as well as warning and critical thresholds for the value")); | 654 | /* TODO: -v clashing with -v/-variable. Commenting out help text since |
| 682 | printf (" %s\n", _("of that variable. If the remote host has multiple UPS that are being monitored")); | 655 | verbose is unused up to now */ |
| 683 | printf (" %s\n", _("you will have to use the --ups option to specify which UPS to check.")); | 656 | /* printf (UT_VERBOSE); */ |
| 684 | printf ("\n"); | 657 | |
| 685 | printf (" %s\n", _("This plugin requires that the UPSD daemon distributed with Russell Kroll's")); | 658 | printf("\n"); |
| 686 | printf (" %s\n", _("Network UPS Tools be installed on the remote host. If you do not have the")); | 659 | printf("%s\n", _("This plugin attempts to determine the status of a UPS " |
| 687 | printf (" %s\n", _("package installed on your system, you can download it from")); | 660 | "(Uninterruptible Power")); |
| 688 | printf (" %s\n", _("http://www.networkupstools.org")); | 661 | printf("%s\n", _("Supply) on a local or remote host. If the UPS is online " |
| 689 | 662 | "or calibrating, the")); | |
| 690 | printf (UT_SUPPORT); | 663 | printf("%s\n", _("plugin will return an OK state. If the battery is on it " |
| 664 | "will return a WARNING")); | ||
| 665 | printf("%s\n", _("state. If the UPS is off or has a low battery the plugin " | ||
| 666 | "will return a CRITICAL")); | ||
| 667 | printf("%s\n", _("state.")); | ||
| 668 | |||
| 669 | printf("\n"); | ||
| 670 | printf("%s\n", _("Notes:")); | ||
| 671 | printf(" %s\n", _("You may also specify a variable to check (such as " | ||
| 672 | "temperature, utility voltage,")); | ||
| 673 | printf(" %s\n", _("battery load, etc.) as well as warning and critical " | ||
| 674 | "thresholds for the value")); | ||
| 675 | printf(" %s\n", _("of that variable. If the remote host has multiple UPS " | ||
| 676 | "that are being monitored")); | ||
| 677 | printf(" %s\n", _("you will have to use the --ups option to specify which " | ||
| 678 | "UPS to check.")); | ||
| 679 | printf("\n"); | ||
| 680 | printf(" %s\n", _("This plugin requires that the UPSD daemon distributed " | ||
| 681 | "with Russell Kroll's")); | ||
| 682 | printf(" %s\n", _("Network UPS Tools be installed on the remote host. If " | ||
| 683 | "you do not have the")); | ||
| 684 | printf(" %s\n", _("package installed on your system, you can download it from")); | ||
| 685 | printf(" %s\n", _("http://www.networkupstools.org")); | ||
| 686 | |||
| 687 | printf(UT_SUPPORT); | ||
| 691 | } | 688 | } |
| 692 | 689 | ||
| 693 | 690 | void print_usage(void) { | |
| 694 | void | 691 | printf("%s\n", _("Usage:")); |
| 695 | print_usage (void) | 692 | printf("%s -H host -u ups [-p port] [-v variable] [-w warn_value] [-c " |
| 696 | { | 693 | "crit_value] [-to to_sec] [-T]\n", |
| 697 | printf ("%s\n", _("Usage:")); | 694 | progname); |
| 698 | printf ("%s -H host -u ups [-p port] [-v variable] [-w warn_value] [-c crit_value] [-to to_sec] [-T]\n", progname); | ||
| 699 | } | 695 | } |
