97
static void parse_var(const char *buf)
97
static int get_var(const char *var, char *buf, size_t buflen, int verbose)
100
unsigned int numq, numa;
101
const char *query[4];
101
104
if (!check_ups_fd(1))
104
if (upscli_getvar(&ups, upsname, buf, ans, sizeof(ans)) < 0) {
109
printf("[No UPS name specified]\n");
120
ret = upscli_get(&ups, numq, query, &numa, &answer);
130
printf("[Invalid response]\n");
135
snprintf(buf, buflen, "%s", answer[3]);
139
static void parse_var(const char *var)
141
char answer[SMALLBUF];
143
if (!get_var(var, answer, sizeof(answer), 1))
146
printf("%s\n", answer);
112
149
static void do_status(void)
115
char stat[SMALLBUF], *sp, *ptr;
117
if (!check_ups_fd(1))
120
if (upscli_getvar(&ups, upsname, "status", stat, sizeof(stat)) < 0) {
152
char status[SMALLBUF], *sp, *ptr;
154
if (!get_var("ups.status", status, sizeof(status), 1))
128
160
ptr = strchr(sp, ' ');
155
187
static int get_img_val(const char *var, const char *desc, const char *imgargs)
159
if (!check_ups_fd(1))
162
if (upscli_getvar(&ups, upsname, var, temp, sizeof(temp)) < 0) {
189
char answer[SMALLBUF];
191
if (!get_var(var, answer, sizeof(answer), 1))
167
194
printf("<IMG SRC=\"upsimage.cgi?host=%s&display=%s",
168
195
currups->sys, var);
242
269
split_imgarg(ptr, imgargs, sizeof(imgargs));
245
if (!strcmp(type, "BATTVOLT"))
246
return get_img_val("battvolt", "Battery Voltage", imgargs);
248
if (!strcmp(type, "BATTPCT"))
249
return get_img_val("battpct", "Battery Charge", imgargs);
251
if (!strcmp(type, "UTILITY"))
252
return get_img_val("utility", "Utility", imgargs);
254
if (!strcmp(type, "OUTVOLT"))
255
return get_img_val("outvolt", "Output Voltage", imgargs);
257
if (!strcmp(type, "LOADPCT"))
258
return get_img_val("loadpct", "UPS Load", imgargs);
260
if (!strcmp(type, "ACFREQ"))
261
return get_img_val("acfreq", "AC Freq", imgargs);
263
if (!strcmp(type, "OUT_FREQ"))
264
return get_img_val("out_freq", "Out Freq", imgargs);
272
/* only allow known types through */
274
if (!strcmp(type, "input.voltage"))
275
return get_img_val("input.voltage", "Input voltage", imgargs);
277
if (!strcmp(type, "battery.voltage"))
278
return get_img_val("battery.voltage", "Battery voltage", imgargs);
280
if (!strcmp(type, "battery.charge"))
281
return get_img_val("battery.charge", "Battery charge", imgargs);
283
if (!strcmp(type, "output.voltage"))
284
return get_img_val("output.voltage", "Output voltage", imgargs);
286
if (!strcmp(type, "ups.load"))
287
return get_img_val("ups.load", "UPS load", imgargs);
289
if (!strcmp(type, "input.frequency"))
290
return get_img_val("input.frequency", "Input frequency", imgargs);
292
if (!strcmp(type, "output.frequency"))
293
return get_img_val("output.frequency", "Output frequency", imgargs);
284
313
/* see if it's just on the same host */
285
upscli_splitname(currups->sys, &newups, &newhost, &newport);
314
newups = newhost = NULL;
316
if (upscli_splitname(currups->sys, &newups, &newhost,
318
printf("Unusable UPS definition [%s]\n", currups->sys);
319
fprintf(stderr, "Unusable UPS definition [%s]\n",
287
324
if ((!strcmp(newhost, hostname)) && (port == newport)) {
308
upscli_splitname(currups->sys, &upsname, &hostname, &port);
345
if (upscli_splitname(currups->sys, &upsname, &hostname, &port) != 0) {
346
printf("Unusable UPS definition [%s]\n", currups->sys);
347
fprintf(stderr, "Unusable UPS definition [%s]\n",
310
352
if (upscli_connect(&ups, hostname, port, 0) < 0)
311
353
fprintf(stderr, "UPS [%s]: can't connect to server: %s\n",
326
368
/* see if the UPS supports this variable - skip to the next ENDIF if not */
327
static void do_ifsupp(char *var)
369
static void do_ifsupp(const char *var)
371
char dummy[SMALLBUF];
331
/* if not connected, assume it's not supported to skip more */
373
/* if not connected, act like it's not supported and skip the rest */
332
374
if (!check_ups_fd(0)) {
333
375
skip_to_endif = 1;
337
if (upscli_getvar(&ups, upsname, var, ans, sizeof(ans)) < 0)
379
if (!get_var(var, dummy, sizeof(dummy), 0)) {
338
380
skip_to_endif = 1;
341
static void do_temp(char *var)
385
static void do_temp(const char *var)
343
387
char tempc[SMALLBUF];
346
if (!check_ups_fd(1))
349
if (upscli_getvar(&ups, upsname, var, tempc, sizeof(tempc)) < 0) {
390
if (!get_var(var, tempc, sizeof(tempc), 1))
354
393
if (use_celsius) {
355
394
printf("%s\n", tempc);
438
475
utility = strtol(tmp, (char **) NULL, 10);
440
if (upscli_getvar(&ups, upsname, "lowxfer", tmp, sizeof(tmp)) < 0) {
477
if (!get_var("input.transfer.low", tmp, sizeof(tmp), 0)) {
442
/* no lowxfer? default to green */
479
/* not available = default to green */
443
480
printf("#00FF00\n");
447
484
lowxfer = strtol(tmp, (char **) NULL, 10);
449
if (upscli_getvar(&ups, upsname, "highxfer", tmp, sizeof(tmp)) < 0) {
486
if (!get_var("input.transfer.high", tmp, sizeof(tmp), 0)) {
451
/* same idea for highxfer */
452
489
printf("#00FF00\n");
717
754
pconf_finish(&ctx);
757
printf("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\"\n");
758
printf(" \"http://www.w3.org/TR/REC-html40/loose.dtd\">\n");
759
printf("<HTML><HEAD>\n");
760
printf("<TITLE>Error: no hosts to monitor</TITLE>\n");
761
printf("</HEAD><BODY>\n");
762
printf("Error: no hosts to monitor (check <CODE>hosts.conf</CODE>)\n");
763
printf("</BODY></HTML>\n");
765
/* leave something for the admin */
766
fprintf(stderr, "upsstats: no hosts to monitor\n");
720
771
static void display_single(void)