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