~ubuntu-branches/ubuntu/saucy/nut/saucy

« back to all changes in this revision

Viewing changes to clients/upsstats.c

  • Committer: Bazaar Package Importer
  • Author(s): Reinhard Tartler
  • Date: 2005-07-20 19:48:50 UTC
  • mto: (16.1.1 squeeze)
  • mto: This revision was merged to the branch mainline in revision 4.
  • Revision ID: james.westby@ubuntu.com-20050720194850-oo61wjr33rrx2mre
Tags: upstream-2.0.2
ImportĀ upstreamĀ versionĀ 2.0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
94
94
        return 1;
95
95
}       
96
96
 
97
 
static void parse_var(const char *buf)
 
97
static int get_var(const char *var, char *buf, size_t buflen, int verbose)
98
98
{
99
 
        char    ans[SMALLBUF];
 
99
        int     ret;
 
100
        unsigned int    numq, numa;
 
101
        const   char    *query[4];
 
102
        char    **answer;
100
103
 
101
104
        if (!check_ups_fd(1))
102
 
                return;
103
 
 
104
 
        if (upscli_getvar(&ups, upsname, buf, ans, sizeof(ans)) < 0) {
105
 
                report_error();
106
 
                return;
107
 
        }
108
 
 
109
 
        printf("%s\n", ans);
 
105
                return 0;
 
106
 
 
107
        if (!upsname) {
 
108
                if (verbose)
 
109
                        printf("[No UPS name specified]\n");
 
110
 
 
111
                return 0;
 
112
        }
 
113
 
 
114
        query[0] = "VAR";
 
115
        query[1] = upsname;
 
116
        query[2] = var;
 
117
 
 
118
        numq = 3;
 
119
 
 
120
        ret = upscli_get(&ups, numq, query, &numa, &answer);
 
121
 
 
122
        if (ret < 0) {
 
123
                if (verbose)
 
124
                        report_error();
 
125
                return 0;
 
126
        }
 
127
 
 
128
        if (numa < numq) {
 
129
                if (verbose)
 
130
                        printf("[Invalid response]\n");
 
131
                
 
132
                return 0;
 
133
        }
 
134
 
 
135
        snprintf(buf, buflen, "%s", answer[3]);
 
136
        return 1;
 
137
}
 
138
 
 
139
static void parse_var(const char *var)
 
140
{
 
141
        char    answer[SMALLBUF];
 
142
 
 
143
        if (!get_var(var, answer, sizeof(answer), 1))
 
144
                return;
 
145
 
 
146
        printf("%s\n", answer);
110
147
}
111
148
 
112
149
static void do_status(void)
113
150
{
114
151
        int     i;
115
 
        char    stat[SMALLBUF], *sp, *ptr;
116
 
 
117
 
        if (!check_ups_fd(1))
118
 
                return;
119
 
 
120
 
        if (upscli_getvar(&ups, upsname, "status", stat, sizeof(stat)) < 0) {
121
 
                report_error();
122
 
                return;
123
 
        }
124
 
 
125
 
        sp = stat;
 
152
        char    status[SMALLBUF], *sp, *ptr;
 
153
 
 
154
        if (!get_var("ups.status", status, sizeof(status), 1))
 
155
                return;
 
156
 
 
157
        sp = status;
126
158
 
127
159
        while (sp) {
128
160
                ptr = strchr(sp, ' ');
154
186
 
155
187
static int get_img_val(const char *var, const char *desc, const char *imgargs)
156
188
{
157
 
        char    temp[SMALLBUF];
158
 
 
159
 
        if (!check_ups_fd(1))
160
 
                return 1;
161
 
 
162
 
        if (upscli_getvar(&ups, upsname, var, temp, sizeof(temp)) < 0) {
163
 
                report_error();
164
 
                return 1;
165
 
        }
 
189
        char    answer[SMALLBUF];
 
190
 
 
191
        if (!get_var(var, answer, sizeof(answer), 1))
 
192
                return 1;
166
193
 
167
194
        printf("<IMG SRC=\"upsimage.cgi?host=%s&amp;display=%s",
168
195
                currups->sys, var);
171
198
                printf("&amp;%s", imgargs);
172
199
 
173
200
        printf("\" WIDTH=\"100\" HEIGHT=\"350\" ALT=\"%s: %s\">\n",
174
 
                desc, temp);
 
201
                desc, answer);
175
202
 
176
203
        return 1;
177
204
}
242
269
                split_imgarg(ptr, imgargs, sizeof(imgargs));
243
270
        }
244
271
 
245
 
        if (!strcmp(type, "BATTVOLT"))
246
 
                return get_img_val("battvolt", "Battery Voltage", imgargs);
247
 
 
248
 
        if (!strcmp(type, "BATTPCT"))
249
 
                return get_img_val("battpct", "Battery Charge", imgargs);
250
 
 
251
 
        if (!strcmp(type, "UTILITY"))
252
 
                return get_img_val("utility", "Utility", imgargs);
253
 
 
254
 
        if (!strcmp(type, "OUTVOLT"))
255
 
                return get_img_val("outvolt", "Output Voltage", imgargs);
256
 
 
257
 
        if (!strcmp(type, "LOADPCT"))
258
 
                return get_img_val("loadpct", "UPS Load", imgargs);
259
 
 
260
 
        if (!strcmp(type, "ACFREQ"))
261
 
                return get_img_val("acfreq", "AC Freq", imgargs);
262
 
 
263
 
        if (!strcmp(type, "OUT_FREQ"))
264
 
                return get_img_val("out_freq", "Out Freq", imgargs);
 
272
        /* only allow known types through */
 
273
 
 
274
        if (!strcmp(type, "input.voltage"))
 
275
                return get_img_val("input.voltage", "Input voltage", imgargs);
 
276
 
 
277
        if (!strcmp(type, "battery.voltage"))
 
278
                return get_img_val("battery.voltage", "Battery voltage", imgargs);
 
279
 
 
280
        if (!strcmp(type, "battery.charge"))
 
281
                return get_img_val("battery.charge", "Battery charge", imgargs);
 
282
 
 
283
        if (!strcmp(type, "output.voltage"))
 
284
                return get_img_val("output.voltage", "Output voltage", imgargs);
 
285
 
 
286
        if (!strcmp(type, "ups.load"))
 
287
                return get_img_val("ups.load", "UPS load", imgargs);
 
288
 
 
289
        if (!strcmp(type, "input.frequency"))
 
290
                return get_img_val("input.frequency", "Input frequency", imgargs);
 
291
 
 
292
        if (!strcmp(type, "output.frequency"))
 
293
                return get_img_val("output.frequency", "Output frequency", imgargs);
265
294
 
266
295
        return 0;
267
296
}
282
311
                }
283
312
 
284
313
                /* see if it's just on the same host */
285
 
                upscli_splitname(currups->sys, &newups, &newhost, &newport);
 
314
                newups = newhost = NULL;
 
315
 
 
316
                if (upscli_splitname(currups->sys, &newups, &newhost, 
 
317
                        &newport) != 0) {
 
318
                        printf("Unusable UPS definition [%s]\n", currups->sys);
 
319
                        fprintf(stderr, "Unusable UPS definition [%s]\n", 
 
320
                                currups->sys);
 
321
                        exit(EXIT_FAILURE);
 
322
                }               
286
323
 
287
324
                if ((!strcmp(newhost, hostname)) && (port == newport)) {
288
325
                        free(upsname);
305
342
        if (hostname)
306
343
                free(hostname);
307
344
 
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", 
 
348
                        currups->sys);
 
349
                exit(EXIT_FAILURE);
 
350
        }
309
351
 
310
352
        if (upscli_connect(&ups, hostname, port, 0) < 0)
311
353
                fprintf(stderr, "UPS [%s]: can't connect to server: %s\n",
324
366
}
325
367
 
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)
328
370
{
329
 
        char    ans[SMALLBUF];
 
371
        char    dummy[SMALLBUF];
330
372
 
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;
334
376
                return;
335
 
        }               
 
377
        }
336
378
 
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;
 
381
                return;
 
382
        }
339
383
}
340
384
 
341
 
static void do_temp(char *var)
 
385
static void do_temp(const char *var)
342
386
{
343
387
        char    tempc[SMALLBUF];
344
388
        float   tempf;
345
389
 
346
 
        if (!check_ups_fd(1))
347
 
                return;
348
 
 
349
 
        if (upscli_getvar(&ups, upsname, var, tempc, sizeof(tempc)) < 0) {
350
 
                report_error();
351
 
                return;
352
 
        }
 
390
        if (!get_var(var, tempc, sizeof(tempc), 1))
 
391
                return;
353
392
 
354
393
        if (use_celsius) {
355
394
                printf("%s\n", tempc);
383
422
                return;
384
423
        }
385
424
 
386
 
        if (upscli_getvar(&ups, upsname, "status", stat, sizeof(stat)) < 0) {
387
 
 
 
425
        if (!get_var("ups.status", stat, sizeof(stat), 0)) {
388
426
                /* status not available - give yellow as a warning */
389
427
                printf("#FFFF00\n");
390
428
                return;
428
466
                return;
429
467
        }
430
468
 
431
 
        if (upscli_getvar(&ups, upsname, "utility", tmp, sizeof(tmp)) < 0) {
432
 
 
 
469
        if (!get_var("input.voltage", tmp, sizeof(tmp), 0)) {
433
470
                /* nothing available - default is green */
434
471
                printf("#00FF00\n");
435
472
                return;
437
474
 
438
475
        utility = strtol(tmp, (char **) NULL, 10);
439
476
 
440
 
        if (upscli_getvar(&ups, upsname, "lowxfer", tmp, sizeof(tmp)) < 0) {
 
477
        if (!get_var("input.transfer.low", tmp, sizeof(tmp), 0)) {
441
478
 
442
 
                /* no lowxfer?  default to green */
 
479
                /* not available = default to green */
443
480
                printf("#00FF00\n");
444
481
                return;
445
482
        }
446
483
 
447
484
        lowxfer = strtol(tmp, (char **) NULL, 10);
448
485
 
449
 
        if (upscli_getvar(&ups, upsname, "highxfer", tmp, sizeof(tmp)) < 0) {
 
486
        if (!get_var("input.transfer.high", tmp, sizeof(tmp), 0)) {
450
487
 
451
 
                /* same idea for highxfer */
 
488
                /* same idea */
452
489
                printf("#00FF00\n");
453
490
                return;
454
491
        }
470
507
        if (strlen(buf) < 3) {
471
508
 
472
509
                if (skip_to_endif == 1)
473
 
                        return 1;
 
510
                        return 1;
474
511
 
475
512
                return 0;
476
513
        }
600
637
        }
601
638
 
602
639
        if (!strcmp(cmd, "UPSTEMP")) {
603
 
                do_temp("UPSTEMP");
 
640
                do_temp("ups.temperature");
604
641
                return 1;
605
642
        }
606
643
 
607
644
        if (!strcmp(cmd, "AMBTEMP")) {
608
 
                do_temp("AMBTEMP");
 
645
                do_temp("ambient.temperature");
609
646
                return 1;
610
647
        }
611
648
 
631
668
 
632
669
                printf("Error: can't open template file (%s)\n", tfn);
633
670
 
634
 
                exit(1);
 
671
                exit(EXIT_FAILURE);
635
672
        }
636
673
 
637
674
        while (fgets(buf, sizeof(buf), tf)) {
668
705
}
669
706
 
670
707
/* called for fatal errors in parseconf like malloc failures */
671
 
void upsstats_hosts_err(const char *errmsg)
 
708
static void upsstats_hosts_err(const char *errmsg)
672
709
{
673
710
        upslogx(LOG_ERR, "Fatal error in parseconf(hosts.conf): %s", errmsg);
674
711
}
695
732
 
696
733
                /* leave something for the admin */
697
734
                fprintf(stderr, "upsstats: %s\n", ctx.errmsg);
698
 
                exit(0);
 
735
                exit(EXIT_FAILURE);
699
736
        }
700
737
 
701
738
        while (pconf_file_next(&ctx)) {
715
752
        }
716
753
 
717
754
        pconf_finish(&ctx);
 
755
 
 
756
        if (!ulhead) {
 
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");
 
764
 
 
765
                /* leave something for the admin */
 
766
                fprintf(stderr, "upsstats: no hosts to monitor\n");
 
767
                exit(EXIT_FAILURE);
 
768
        }
718
769
}
719
770
 
720
771
static void display_single(void)
722
773
        if (!checkhost(monhost, &monhostdesc)) {
723
774
                printf("Access to that host [%s] is not authorized.\n",
724
775
                        monhost);
725
 
                exit(0);
 
776
                exit(EXIT_FAILURE);
726
777
        }
727
778
 
728
779
        add_ups(monhost, monhostdesc);
746
797
        /* if a host is specified, use upsstats-single.html instead */
747
798
        if (monhost) {
748
799
                display_single();
749
 
                exit(0);
 
800
                exit(EXIT_SUCCESS);
750
801
        }
751
802
 
752
803
        /* default: multimon replacement mode */