summaryrefslogtreecommitdiffstats
path: root/plugins/check_ups.c
diff options
context:
space:
mode:
authorKarl DeBisschop <kdebisschop@users.sourceforge.net>2002-11-22 10:52:18 (GMT)
committerKarl DeBisschop <kdebisschop@users.sourceforge.net>2002-11-22 10:52:18 (GMT)
commit63ac65ccb855786a489f40e72dd1a7f678f7e03e (patch)
treee81e4f06fe38e70a6d0197fac984add211a21873 /plugins/check_ups.c
parent2898a0ebb67c42c719b5389f07158e47a163d2a8 (diff)
downloadmonitoring-plugins-63ac65ccb855786a489f40e72dd1a7f678f7e03e.tar.gz
add replace battery condition, replace unchecked strcat calls with asprintf (I do not think buffer overflow was possible here, but lets be consistent)
git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/nagiosplug/trunk@225 f882894a-f735-0410-b71e-b25c423dba1c
Diffstat (limited to 'plugins/check_ups.c')
-rw-r--r--plugins/check_ups.c104
1 files changed, 51 insertions, 53 deletions
diff --git a/plugins/check_ups.c b/plugins/check_ups.c
index e250ef9..470e3be 100644
--- a/plugins/check_ups.c
+++ b/plugins/check_ups.c
@@ -31,7 +31,7 @@
31* This plugin requires that the UPSD daemon distributed with Russel 31* This plugin requires that the UPSD daemon distributed with Russel
32* Kroll's "Smart UPS Tools" be installed on the remote host. If you 32* Kroll's "Smart UPS Tools" be installed on the remote host. If you
33* don't have the package installed on your system, you can download 33* don't have the package installed on your system, you can download
34* it from http://www.exploits.org/~rkroll/smartupstools 34* it from http://www.exploits.org/nut
35* 35*
36* License Information: 36* License Information:
37* 37*
@@ -66,20 +66,21 @@
66 66
67#define PORT 3305 67#define PORT 3305
68 68
69#define UPS_NONE 0 /* no supported options */ 69#define UPS_NONE 0 /* no supported options */
70#define UPS_UTILITY 1 /* supports utility line voltage */ 70#define UPS_UTILITY 1 /* supports utility line voltage */
71#define UPS_BATTPCT 2 /* supports percent battery remaining */ 71#define UPS_BATTPCT 2 /* supports percent battery remaining */
72#define UPS_STATUS 4 /* supports UPS status */ 72#define UPS_STATUS 4 /* supports UPS status */
73#define UPS_TEMP 8 /* supports UPS temperature */ 73#define UPS_TEMP 8 /* supports UPS temperature */
74#define UPS_LOADPCT 16 /* supports load percent */ 74#define UPS_LOADPCT 16 /* supports load percent */
75 75
76#define UPSSTATUS_NONE 0 76#define UPSSTATUS_NONE 0
77#define UPSSTATUS_OFF 1 77#define UPSSTATUS_OFF 1
78#define UPSSTATUS_OL 2 78#define UPSSTATUS_OL 2
79#define UPSSTATUS_OB 4 79#define UPSSTATUS_OB 4
80#define UPSSTATUS_LB 8 80#define UPSSTATUS_LB 8
81#define UPSSTATUS_CAL 16 81#define UPSSTATUS_CAL 16
82#define UPSSTATUS_UNKOWN 32 82#define UPSSTATUS_RB 32 /*Replace Battery */
83#define UPSSTATUS_UNKOWN 64
83 84
84int server_port = PORT; 85int server_port = PORT;
85char *server_address = "127.0.0.1"; 86char *server_address = "127.0.0.1";
@@ -96,7 +97,7 @@ double ups_utility_voltage = 0.0L;
96double ups_battery_percent = 0.0L; 97double ups_battery_percent = 0.0L;
97double ups_load_percent = 0.0L; 98double ups_load_percent = 0.0L;
98double ups_temperature = 0.0L; 99double ups_temperature = 0.0L;
99char ups_status[MAX_INPUT_BUFFER] = "N/A"; 100char *ups_status = "N/A";
100 101
101int determine_status (void); 102int determine_status (void);
102int determine_supported_vars (void); 103int determine_supported_vars (void);
@@ -111,7 +112,7 @@ int
111main (int argc, char **argv) 112main (int argc, char **argv)
112{ 113{
113 int result = STATE_OK; 114 int result = STATE_OK;
114 char output_message[MAX_INPUT_BUFFER]; 115 char *message;
115 char temp_buffer[MAX_INPUT_BUFFER]; 116 char temp_buffer[MAX_INPUT_BUFFER];
116 117
117 double ups_utility_deviation = 0.0L; 118 double ups_utility_deviation = 0.0L;
@@ -134,35 +135,39 @@ main (int argc, char **argv)
134 135
135 if (determine_status () != OK) 136 if (determine_status () != OK)
136 return STATE_CRITICAL; 137 return STATE_CRITICAL;
137 ups_status[0] = 0; 138 asprintf (&ups_status, "");
138 result = STATE_OK; 139 result = STATE_OK;
139 140
140 if (status & UPSSTATUS_OFF) { 141 if (status & UPSSTATUS_OFF) {
141 strcpy (ups_status, "Off"); 142 asprintf (&ups_status, "Off");
142 result = STATE_CRITICAL; 143 result = STATE_CRITICAL;
143 } 144 }
144 else if ((status & (UPSSTATUS_OB | UPSSTATUS_LB)) == 145 else if ((status & (UPSSTATUS_OB | UPSSTATUS_LB)) ==
145 (UPSSTATUS_OB | UPSSTATUS_LB)) { 146 (UPSSTATUS_OB | UPSSTATUS_LB)) {
146 strcpy (ups_status, "On Battery, Low Battery"); 147 asprintf (&ups_status, "On Battery, Low Battery");
147 result = STATE_CRITICAL; 148 result = STATE_CRITICAL;
148 } 149 }
149 else { 150 else {
150 if (status & UPSSTATUS_OL) { 151 if (status & UPSSTATUS_OL) {
151 strcat (ups_status, "Online"); 152 asprintf (&ups_status, "%s%s", ups_status, "Online");
152 } 153 }
153 if (status & UPSSTATUS_OB) { 154 if (status & UPSSTATUS_OB) {
154 strcat (ups_status, "On Battery"); 155 asprintf (&ups_status, "%s%s", ups_status, "On Battery");
155 result = STATE_WARNING; 156 result = STATE_WARNING;
156 } 157 }
157 if (status & UPSSTATUS_LB) { 158 if (status & UPSSTATUS_LB) {
158 strcat (ups_status, ", Low Battery"); 159 asprintf (&ups_status, "%s%s", ups_status, ", Low Battery");
159 result = STATE_WARNING; 160 result = STATE_WARNING;
160 } 161 }
161 if (status & UPSSTATUS_CAL) { 162 if (status & UPSSTATUS_CAL) {
162 strcat (ups_status, ", Calibrating"); 163 asprintf (&ups_status, "%s%s", ups_status, ", Calibrating");
164 }
165 if (status & UPSSTATUS_RB) {
166 asprintf (&ups_status, "%s%s", ups_status, ", Replace Battery");
167 result = STATE_WARNING;
163 } 168 }
164 if (status & UPSSTATUS_UNKOWN) { 169 if (status & UPSSTATUS_UNKOWN) {
165 strcat (ups_status, ", Unknown"); 170 asprintf (&ups_status, "%s%s", ups_status, ", Unknown");
166 } 171 }
167 } 172 }
168 } 173 }
@@ -248,36 +253,27 @@ main (int argc, char **argv)
248 alarm (0); 253 alarm (0);
249 254
250 255
251 sprintf (output_message, "UPS %s - ", 256 asprintf (&message, "UPS %s - ", (result == STATE_OK) ? "ok" : "problem");
252 (result == STATE_OK) ? "ok" : "problem");
253 257
254 if (supported_options & UPS_STATUS) { 258 if (supported_options & UPS_STATUS)
255 sprintf (temp_buffer, "Status=%s ", ups_status); 259 asprintf (&message, "%sStatus=%s ", message, ups_status);
256 strcat (output_message, temp_buffer); 260
257 } 261 if (supported_options & UPS_UTILITY)
258 if (supported_options & UPS_UTILITY) { 262 asprintf (&message, "%sUtility=%3.1fV ", message, ups_utility_voltage);
259 sprintf (temp_buffer, "Utility=%3.1fV ", ups_utility_voltage); 263
260 strcat (output_message, temp_buffer); 264 if (supported_options & UPS_BATTPCT)
261 } 265 asprintf (&message, "%sBatt=%3.1f%% ", message, ups_battery_percent);
262 if (supported_options & UPS_BATTPCT) { 266
263 sprintf (temp_buffer, "Batt=%3.1f%% ", ups_battery_percent); 267 if (supported_options & UPS_LOADPCT)
264 strcat (output_message, temp_buffer); 268 asprintf (&message, "%sLoad=%3.1f%% ", message, ups_load_percent);
265 } 269
266 if (supported_options & UPS_LOADPCT) { 270 if (supported_options & UPS_TEMP)
267 sprintf (temp_buffer, "Load=%3.1f%% ", ups_load_percent); 271 asprintf (&message, "%sTemp=%3.1fF", message, ups_temperature);
268 strcat (output_message, temp_buffer); 272
269 } 273 if (supported_options == UPS_NONE)
270 if (supported_options & UPS_TEMP) { 274 asprintf (&message, "UPS does not support any available options\n");
271 sprintf (temp_buffer, "Temp=%3.1fF", ups_temperature);
272 strcat (output_message, temp_buffer);
273 }
274 if (supported_options == UPS_NONE) {
275 sprintf (temp_buffer,
276 "UPS does not appear to support any available options\n");
277 strcat (output_message, temp_buffer);
278 }
279 275
280 printf ("%s\n", output_message); 276 printf ("%s\n", message);
281 277
282 return result; 278 return result;
283} 279}
@@ -313,6 +309,8 @@ determine_status (void)
313 status |= UPSSTATUS_LB; 309 status |= UPSSTATUS_LB;
314 else if (!strcmp (ptr, "CAL")) 310 else if (!strcmp (ptr, "CAL"))
315 status |= UPSSTATUS_CAL; 311 status |= UPSSTATUS_CAL;
312 else if (!strcmp (ptr, "RB"))
313 status |= UPSSTATUS_RB;
316 else 314 else
317 status |= UPSSTATUS_UNKOWN; 315 status |= UPSSTATUS_UNKOWN;
318 } 316 }