~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): Arnaud Quette
  • Date: 2004-05-28 13:10:01 UTC
  • mto: (16.1.1 squeeze)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20040528131001-yj2m9qcez4ya2w14
Tags: upstream-1.4.2
ImportĀ upstreamĀ versionĀ 1.4.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
18
 */
19
19
 
20
 
/* default host - only used if you don't specify one in the URL */
21
 
#define MONHOST "127.0.0.1"
22
 
 
23
20
#include "common.h"
24
 
#include "upsfetch.h"
 
21
#include "upsclient.h"
25
22
#include "status.h"
26
23
#include "cgilib.h"
 
24
#include "parseconf.h"
27
25
#include "timehead.h"
28
 
 
29
 
#include <sys/time.h>
30
 
 
31
 
static  char    monhost[SMALLBUF];
 
26
#include "upsstats.h"
 
27
#include "upsimagearg.h"
 
28
 
 
29
#define MAX_CGI_STRLEN 64
 
30
 
 
31
static  char    *monhost = NULL;
32
32
static  int     use_celsius = 1, refreshdelay = -1;
33
33
 
 
34
        /* from cgilib's checkhost() */
 
35
static  char    *monhostdesc = NULL;
 
36
 
 
37
static  int     port;
 
38
static  char    *upsname, *hostname;
 
39
static  UPSCONN ups;
 
40
 
 
41
static  FILE    *tf;
 
42
static  long    forofs = 0;
 
43
 
 
44
static  ulist_t *ulhead = NULL, *currups = NULL;
 
45
 
 
46
static  int     skip_to_endif = 0;
 
47
 
34
48
void parsearg(char *var, char *value)
35
49
{
36
 
        if (!strcmp(var, "host"))
37
 
                strlcpy(monhost, value, sizeof(monhost));
38
 
 
39
 
        if (!strcmp(var, "use_celsius"))
40
 
                use_celsius = (int) strtol(value, (char **) NULL, 10);
41
 
        
 
50
        /* avoid bogus junk from evil people */
 
51
        if ((strlen(var) > MAX_CGI_STRLEN) || (strlen(value) > MAX_CGI_STRLEN))
 
52
                return;
 
53
 
 
54
        if (!strcmp(var, "host")) {
 
55
                if (monhost)
 
56
                        free(monhost);
 
57
 
 
58
                monhost = xstrdup(value);
 
59
                return;
 
60
        }
 
61
 
42
62
        if (!strcmp(var, "refresh"))
43
63
                refreshdelay = (int) strtol(value, (char **) NULL, 10);
44
64
}
45
65
 
46
 
const char *my_getdate()
47
 
{
 
66
static void report_error(void)
 
67
{
 
68
        if (upscli_upserror(&ups) == UPSCLI_ERR_VARNOTSUPP)
 
69
                printf("Not supported\n");
 
70
        else
 
71
                printf("[error: %s]\n", upscli_strerror(&ups));
 
72
}
 
73
 
 
74
/* make sure we're actually connected to upsd */
 
75
static int check_ups_fd(int do_report)
 
76
{
 
77
        if (upscli_fd(&ups) == -1) {
 
78
                if (do_report)
 
79
                        report_error();
 
80
 
 
81
                return 0;
 
82
        }
 
83
 
 
84
        /* also check for insanity in currups */
 
85
 
 
86
        if (!currups) {
 
87
                if (do_report)
 
88
                        printf("No UPS specified for monitoring\n");
 
89
 
 
90
                return 0;
 
91
        }
 
92
 
 
93
        /* must be OK */
 
94
        return 1;
 
95
}       
 
96
 
 
97
static void parse_var(const char *buf)
 
98
{
 
99
        char    ans[SMALLBUF];
 
100
 
 
101
        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);
 
110
}
 
111
 
 
112
static void do_status(void)
 
113
{
 
114
        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;
 
126
 
 
127
        while (sp) {
 
128
                ptr = strchr(sp, ' ');
 
129
                if (ptr)
 
130
                        *ptr++ = '\0';
 
131
 
 
132
                /* expand from table in status.h */
 
133
                for (i = 0; stattab[i].name != NULL; i++)
 
134
                        if (!strcmp(stattab[i].name, sp))
 
135
                                printf("%s<BR>", stattab[i].desc);
 
136
 
 
137
                sp = ptr;
 
138
        }
 
139
}
 
140
 
 
141
static int do_date(const char *buf)
 
142
{
 
143
        char    datebuf[SMALLBUF];
48
144
        time_t  tod;
49
 
        static char timestr[255];
50
 
 
51
 
        time (&tod);
52
 
        if (strftime (timestr, sizeof(timestr), "%a %b %d %X %Z %Y", localtime(&tod)) == 0)
53
 
                fatal("strftime");
54
 
 
55
 
        return (timestr);
56
 
}
57
 
 
58
 
void nocomm()
59
 
{
60
 
        printf ("Error: %s\n", upsstrerror(upserror));
61
 
        exit (1);
62
 
}
63
 
 
64
 
int main() 
65
 
{
66
 
        double  tempf;
67
 
        char    model[64], status[64], battpct[64], utility[64], loadpct[64],
68
 
                upstemp[64], acfreq[64], *stat, *ptr;
 
145
 
 
146
        time(&tod);
 
147
        if (strftime(datebuf, sizeof(datebuf), buf, localtime(&tod))) {
 
148
                printf("%s\n", datebuf);
 
149
                return 1;
 
150
        }
 
151
 
 
152
        return 0;
 
153
}
 
154
 
 
155
static int get_img_val(const char *var, const char *desc, const char *imgargs)
 
156
{
 
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
        }
 
166
 
 
167
        printf("<IMG SRC=\"upsimage.cgi?host=%s&amp;display=%s",
 
168
                currups->sys, var);
 
169
 
 
170
        if ((imgargs) && (strlen(imgargs) > 0))
 
171
                printf("&amp;%s", imgargs);
 
172
 
 
173
        printf("\" WIDTH=\"100\" HEIGHT=\"350\" ALT=\"%s: %s\">\n",
 
174
                desc, temp);
 
175
 
 
176
        return 1;
 
177
}
 
178
 
 
179
/* see if <arg> is valid - table from upsimagearg.h */
 
180
static void check_imgarg(char *arg, char *out, size_t outlen)
 
181
{
69
182
        int     i;
70
 
 
71
 
        strcpy (monhost, MONHOST);      /* default host */
72
 
 
 
183
        char    *ep;
 
184
 
 
185
        ep = strchr(arg, '=');
 
186
 
 
187
        if (!ep)
 
188
                return;
 
189
 
 
190
        *ep++= '\0';
 
191
 
 
192
        /* if it's allowed, append it so it can become part of the URL */
 
193
        for (i = 0; imgarg[i].name != NULL; i++) {
 
194
                if (!strcmp(imgarg[i].name, arg)) {
 
195
 
 
196
                        if (strlen(out) == 0)
 
197
                                snprintf(out, outlen, "%s=%s", arg, ep);
 
198
                        else
 
199
                                snprintfcat(out, outlen, "&amp;%s=%s", arg, ep);
 
200
                        return;
 
201
                }
 
202
        }
 
203
}
 
204
 
 
205
/* split out the var=val commands from the IMG line */
 
206
static void split_imgarg(char *in, char *out, size_t outlen)
 
207
{
 
208
        char    *ptr, *sp;
 
209
 
 
210
        if (strlen(in) < 3)
 
211
                return;
 
212
 
 
213
        ptr = in;
 
214
 
 
215
        sp = strchr(ptr, ' ');
 
216
 
 
217
        /* split by spaces, then check each one (can't use parseconf...) */
 
218
        while (sp) {
 
219
                *sp++ = '\0';
 
220
                check_imgarg(ptr, out, outlen);
 
221
 
 
222
                ptr = sp;
 
223
                sp = strchr(ptr, ' ');
 
224
        }
 
225
 
 
226
        check_imgarg(ptr, out, outlen);
 
227
}
 
228
 
 
229
/* IMG <type> [<var>=<val] [<var>=<val>] ... */
 
230
static int do_img(char *buf)
 
231
{
 
232
        char    *type, *ptr, imgargs[SMALLBUF];
 
233
 
 
234
        memset(imgargs, '\0', sizeof(imgargs));
 
235
 
 
236
        type = buf;
 
237
 
 
238
        ptr = strchr(buf, ' ');
 
239
 
 
240
        if (ptr) {
 
241
                *ptr++ = '\0';
 
242
                split_imgarg(ptr, imgargs, sizeof(imgargs));
 
243
        }
 
244
 
 
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);
 
265
 
 
266
        return 0;
 
267
}
 
268
 
 
269
static void ups_connect(void)
 
270
{
 
271
        static ulist_t  *lastups = NULL;
 
272
        char    *newups, *newhost;
 
273
        int     newport;
 
274
 
 
275
        /* try to minimize reconnects */
 
276
        if (lastups) {
 
277
 
 
278
                /* don't reconnect if these are both the same UPS */
 
279
                if (!strcmp(lastups->sys, currups->sys)) {
 
280
                        lastups = currups;
 
281
                        return;
 
282
                }
 
283
 
 
284
                /* see if it's just on the same host */
 
285
                upscli_splitname(currups->sys, &newups, &newhost, &newport);
 
286
 
 
287
                if ((!strcmp(newhost, hostname)) && (port == newport)) {
 
288
                        free(upsname);
 
289
                        upsname = newups;
 
290
 
 
291
                        free(newhost);
 
292
                        lastups = currups;
 
293
                        return;
 
294
                }
 
295
 
 
296
                /* not the same upsd, so disconnect */
 
297
                free(newups);
 
298
                free(newhost);
 
299
        }
 
300
 
 
301
        upscli_disconnect(&ups);
 
302
 
 
303
        if (upsname)
 
304
                free(upsname);
 
305
        if (hostname)
 
306
                free(hostname);
 
307
 
 
308
        upscli_splitname(currups->sys, &upsname, &hostname, &port);
 
309
 
 
310
        if (upscli_connect(&ups, hostname, port, 0) < 0)
 
311
                fprintf(stderr, "UPS [%s]: can't connect to server: %s\n",
 
312
                        currups->sys, upscli_strerror(&ups));
 
313
 
 
314
        lastups = currups;
 
315
}
 
316
 
 
317
static void do_hostlink(void)
 
318
{
 
319
        if (!currups)
 
320
                return;
 
321
 
 
322
        printf("<a href=\"upsstats.cgi?host=%s\">%s</a>\n",
 
323
                currups->sys, currups->desc);
 
324
}
 
325
 
 
326
/* see if the UPS supports this variable - skip to the next ENDIF if not */
 
327
static void do_ifsupp(char *var)
 
328
{
 
329
        char    ans[SMALLBUF];
 
330
 
 
331
        /* if not connected, assume it's not supported to skip more */
 
332
        if (!check_ups_fd(0)) {
 
333
                skip_to_endif = 1;
 
334
                return;
 
335
        }               
 
336
 
 
337
        if (upscli_getvar(&ups, upsname, var, ans, sizeof(ans)) < 0)
 
338
                skip_to_endif = 1;
 
339
}
 
340
 
 
341
static void do_temp(char *var)
 
342
{
 
343
        char    tempc[SMALLBUF];
 
344
        float   tempf;
 
345
 
 
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
        }
 
353
 
 
354
        if (use_celsius) {
 
355
                printf("%s\n", tempc);
 
356
                return;
 
357
        }
 
358
 
 
359
        tempf = (strtod(tempc, (char **) NULL) * 1.8) + 32;
 
360
        printf("%.1f\n", tempf);
 
361
}
 
362
 
 
363
static void do_degrees(void)
 
364
{
 
365
        printf("&deg;");
 
366
 
 
367
        if (use_celsius)
 
368
                printf("C\n");
 
369
        else
 
370
                printf("F\n");
 
371
}
 
372
 
 
373
/* plug in the right color string (like #FF0000) for the UPS status */
 
374
static void do_statuscolor(void)
 
375
{
 
376
        int     severity, i;
 
377
        char    stat[SMALLBUF], *sp, *ptr;
 
378
 
 
379
        if (!check_ups_fd(0)) {
 
380
 
 
381
                /* can't print the warning here - give a red error condition */
 
382
                printf("#FF0000\n");
 
383
                return;
 
384
        }
 
385
 
 
386
        if (upscli_getvar(&ups, upsname, "status", stat, sizeof(stat)) < 0) {
 
387
 
 
388
                /* status not available - give yellow as a warning */
 
389
                printf("#FFFF00\n");
 
390
                return;
 
391
        }
 
392
 
 
393
        severity = 0;
 
394
        sp = stat;
 
395
 
 
396
        while (sp) {
 
397
                ptr = strchr(sp, ' ');
 
398
                if (ptr)
 
399
                        *ptr++ = '\0';
 
400
 
 
401
                /* expand from table in status.h */
 
402
                for (i = 0; stattab[i].name != NULL; i++)
 
403
                        if (!strcmp(stattab[i].name, sp))
 
404
                                if (stattab[i].severity > severity)
 
405
                                        severity = stattab[i].severity;
 
406
 
 
407
                sp = ptr;
 
408
        }
 
409
 
 
410
        switch(severity) {
 
411
                case 0: printf("#00FF00\n"); break;     /* green  : OK      */
 
412
                case 1: printf("#FFFF00\n"); break;     /* yellow : warning */
 
413
 
 
414
                default: printf("#FF0000\n"); break;    /* red    : error   */
 
415
        }
 
416
}
 
417
 
 
418
/* use red if utility is outside lowxfer+highxfer bounds, else green */
 
419
static void do_utilitycolor(void)
 
420
{
 
421
        char    tmp[SMALLBUF];
 
422
        int     lowxfer, highxfer, utility;
 
423
 
 
424
        if (!check_ups_fd(0)) {
 
425
 
 
426
                /* can't print the warning here - give a red error condition */
 
427
                printf("#FF0000\n");
 
428
                return;
 
429
        }
 
430
 
 
431
        if (upscli_getvar(&ups, upsname, "utility", tmp, sizeof(tmp)) < 0) {
 
432
 
 
433
                /* nothing available - default is green */
 
434
                printf("#00FF00\n");
 
435
                return;
 
436
        }
 
437
 
 
438
        utility = strtol(tmp, (char **) NULL, 10);
 
439
 
 
440
        if (upscli_getvar(&ups, upsname, "lowxfer", tmp, sizeof(tmp)) < 0) {
 
441
 
 
442
                /* no lowxfer?  default to green */
 
443
                printf("#00FF00\n");
 
444
                return;
 
445
        }
 
446
 
 
447
        lowxfer = strtol(tmp, (char **) NULL, 10);
 
448
 
 
449
        if (upscli_getvar(&ups, upsname, "highxfer", tmp, sizeof(tmp)) < 0) {
 
450
 
 
451
                /* same idea for highxfer */
 
452
                printf("#00FF00\n");
 
453
                return;
 
454
        }
 
455
 
 
456
        highxfer = strtol(tmp, (char **) NULL, 10);
 
457
 
 
458
        if ((utility < lowxfer) || (utility > highxfer))
 
459
                printf("#FF0000\n");
 
460
        else
 
461
                printf("#00FF00\n");
 
462
}
 
463
 
 
464
/* look for lines starting and ending with @ containing valid commands */
 
465
static int parse_line(const char *buf)
 
466
{
 
467
        static  char    *cmd = NULL;
 
468
 
 
469
        /* deal with extremely short lines as a special case */
 
470
        if (strlen(buf) < 3) {
 
471
 
 
472
                if (skip_to_endif == 1)
 
473
                        return 1;
 
474
 
 
475
                return 0;
 
476
        }
 
477
 
 
478
        if (buf[0] != '@') {
 
479
 
 
480
                /* if skipping a section, act like we parsed the line */
 
481
                if (skip_to_endif == 1)
 
482
                        return 1;
 
483
                
 
484
                /* otherwise pass it through for normal printing */
 
485
                return 0;
 
486
        }
 
487
 
 
488
        if (buf[strlen(buf) - 1] != '@')
 
489
                return 0;
 
490
 
 
491
        if (cmd)
 
492
                free(cmd);
 
493
 
 
494
        cmd = xstrdup(&buf[1]);
 
495
 
 
496
        /* strip off final @ */
 
497
        cmd[strlen(cmd) - 1] = '\0';
 
498
 
 
499
        /* ending an if block? */
 
500
        if (!strcmp(cmd, "ENDIF")) {
 
501
                skip_to_endif = 0;
 
502
                return 1;
 
503
        }
 
504
 
 
505
        /* don't do any commands if skipping a section */
 
506
        if (skip_to_endif == 1)
 
507
                return 1;
 
508
 
 
509
        if (!strncmp(cmd, "VAR ", 4)) {
 
510
                parse_var(&cmd[4]);
 
511
                return 1;
 
512
        }
 
513
 
 
514
        if (!strcmp(cmd, "HOST")) {
 
515
                printf("%s\n", currups->sys);
 
516
                return 1;
 
517
        }
 
518
 
 
519
        if (!strcmp(cmd, "HOSTDESC")) {
 
520
                printf("%s\n", currups->desc);
 
521
                return 1;
 
522
        }
 
523
 
 
524
        if (!strcmp(cmd, "STATUS")) {
 
525
                do_status();
 
526
                return 1;
 
527
        }
 
528
 
 
529
        if (!strcmp(cmd, "STATUSCOLOR")) {
 
530
                do_statuscolor();
 
531
                return 1;
 
532
        }
 
533
 
 
534
        if (!strcmp(cmd, "UTILITYCOLOR")) {
 
535
                do_utilitycolor();
 
536
                return 1;
 
537
        }
 
538
 
 
539
        if (!strcmp(cmd, "TEMPF")) {
 
540
                use_celsius = 0;
 
541
                return 1;
 
542
        }
 
543
 
 
544
        if (!strcmp(cmd, "TEMPC")) {
 
545
                use_celsius = 1;
 
546
                return 1;
 
547
        }
 
548
 
 
549
        if (!strncmp(cmd, "DATE ", 5))
 
550
                return do_date(&cmd[5]);
 
551
 
 
552
        if (!strncmp(cmd, "IMG ", 4))
 
553
                return do_img(&cmd[4]);
 
554
 
 
555
        if (!strcmp(cmd, "VERSION")) {
 
556
                printf("%s\n", UPS_VERSION);
 
557
                return 1;
 
558
        }
 
559
 
 
560
        if (!strcmp(cmd, "REFRESH")) {
 
561
                if (refreshdelay > 0)
 
562
                        printf("<META HTTP-EQUIV=\"Refresh\" CONTENT=\"%d\">\n", 
 
563
                                refreshdelay);
 
564
 
 
565
                return 1;
 
566
        }
 
567
 
 
568
        if (!strcmp(cmd, "FOREACHUPS")) {
 
569
                forofs = ftell(tf);
 
570
 
 
571
                currups = ulhead;
 
572
                ups_connect();
 
573
                return 1;
 
574
        }
 
575
 
 
576
        if (!strcmp(cmd, "ENDFOR")) {
 
577
 
 
578
                /* if not in a for, ignore this */
 
579
                if (forofs == 0)
 
580
                        return 1;
 
581
                        
 
582
                currups = currups->next;
 
583
 
 
584
                if (currups) {
 
585
                        fseek(tf, forofs, SEEK_SET);
 
586
                        ups_connect();
 
587
                }
 
588
 
 
589
                return 1;
 
590
        }               
 
591
 
 
592
        if (!strcmp(cmd, "HOSTLINK")) {
 
593
                do_hostlink();
 
594
                return 1;
 
595
        }
 
596
 
 
597
        if (!strncmp(cmd, "IFSUPP ", 7)) {
 
598
                do_ifsupp(&cmd[7]);
 
599
                return 1;
 
600
        }
 
601
 
 
602
        if (!strcmp(cmd, "UPSTEMP")) {
 
603
                do_temp("UPSTEMP");
 
604
                return 1;
 
605
        }
 
606
 
 
607
        if (!strcmp(cmd, "AMBTEMP")) {
 
608
                do_temp("AMBTEMP");
 
609
                return 1;
 
610
        }
 
611
 
 
612
        if (!strcmp(cmd, "DEGREES")) {
 
613
                do_degrees();
 
614
                return 1;
 
615
        }
 
616
                
 
617
        return 0;
 
618
}
 
619
 
 
620
static void display_template(const char *tfn)
 
621
{
 
622
        char    fn[SMALLBUF], buf[LARGEBUF];    
 
623
 
 
624
        snprintf(fn, sizeof(fn), "%s/%s", confpath(), tfn);
 
625
 
 
626
        tf = fopen(fn, "r");
 
627
 
 
628
        if (!tf) {
 
629
                fprintf(stderr, "upsstats: Can't open %s: %s\n", 
 
630
                        fn, strerror(errno));
 
631
 
 
632
                printf("Error: can't open template file (%s)\n", tfn);
 
633
 
 
634
                exit(1);
 
635
        }
 
636
 
 
637
        while (fgets(buf, sizeof(buf), tf)) {
 
638
                buf[strlen(buf) - 1] = '\0';
 
639
 
 
640
                if (!parse_line(buf))
 
641
                        printf("%s\n", buf);
 
642
        }
 
643
 
 
644
        fclose(tf);
 
645
}
 
646
 
 
647
static void add_ups(char *sys, char *desc)
 
648
{
 
649
        ulist_t *tmp, *last;
 
650
 
 
651
        tmp = last = ulhead;
 
652
 
 
653
        while (tmp) { 
 
654
                last = tmp;
 
655
                tmp = tmp->next;
 
656
        }
 
657
 
 
658
        tmp = xmalloc(sizeof(ulist_t));
 
659
 
 
660
        tmp->sys = xstrdup(sys);
 
661
        tmp->desc = xstrdup(desc);
 
662
        tmp->next = NULL;
 
663
 
 
664
        if (last)
 
665
                last->next = tmp;
 
666
        else
 
667
                ulhead = tmp;
 
668
}
 
669
 
 
670
/* called for fatal errors in parseconf like malloc failures */
 
671
void upsstats_hosts_err(const char *errmsg)
 
672
{
 
673
        upslogx(LOG_ERR, "Fatal error in parseconf(hosts.conf): %s", errmsg);
 
674
}
 
675
 
 
676
static void load_hosts_conf(void)
 
677
{
 
678
        char    fn[SMALLBUF];
 
679
        PCONF_CTX       ctx;
 
680
 
 
681
        snprintf(fn, sizeof(fn), "%s/hosts.conf", CONFPATH);
 
682
 
 
683
        pconf_init(&ctx, upsstats_hosts_err);
 
684
 
 
685
        if (!pconf_file_begin(&ctx, fn)) {
 
686
                pconf_finish(&ctx);
 
687
 
 
688
                printf("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\"\n");
 
689
                printf("        \"http://www.w3.org/TR/REC-html40/loose.dtd\">\n");
 
690
                printf("<HTML><HEAD>\n");
 
691
                printf("<TITLE>Error: can't open hosts.conf</TITLE>\n");
 
692
                printf("</HEAD><BODY>\n");
 
693
                printf("Error: can't open hosts.conf\n");
 
694
                printf("</BODY></HTML>\n");
 
695
 
 
696
                /* leave something for the admin */
 
697
                fprintf(stderr, "upsstats: %s\n", ctx.errmsg);
 
698
                exit(0);
 
699
        }
 
700
 
 
701
        while (pconf_file_next(&ctx)) {
 
702
                if (pconf_parse_error(&ctx)) {
 
703
                        upslogx(LOG_ERR, "Parse error: %s:%d: %s",
 
704
                                fn, ctx.linenum, ctx.errmsg);
 
705
                        continue;
 
706
                }
 
707
 
 
708
                if (ctx.numargs < 3)
 
709
                        continue;
 
710
 
 
711
                /* MONITOR <host> <desc> */
 
712
                if (!strcmp(ctx.arglist[0], "MONITOR"))
 
713
                        add_ups(ctx.arglist[1], ctx.arglist[2]);
 
714
 
 
715
        }
 
716
 
 
717
        pconf_finish(&ctx);
 
718
}
 
719
 
 
720
static void display_single(void)
 
721
{
 
722
        if (!checkhost(monhost, &monhostdesc)) {
 
723
                printf("Access to that host [%s] is not authorized.\n",
 
724
                        monhost);
 
725
                exit(0);
 
726
        }
 
727
 
 
728
        add_ups(monhost, monhostdesc);
 
729
 
 
730
        currups = ulhead;
 
731
        ups_connect();
 
732
 
 
733
        display_template("upsstats-single.html");
 
734
 
 
735
        upscli_disconnect(&ups);
 
736
}
 
737
 
 
738
int main(int argc, char **argv)
 
739
{
73
740
        extractcgiargs();
74
741
 
75
 
        printf ("Content-type: text/html\n"); 
76
 
        printf ("Pragma: no-cache\n");
77
 
        printf ("\n");
78
 
 
79
 
        if (!checkhost (monhost, NULL)) {
80
 
                printf ("Access to that host is not authorized.\n");
81
 
                exit (0);
82
 
        }
83
 
 
84
 
        if (getupsvar(monhost, "model", model, sizeof(model)) < 0)
85
 
                snprintf (model, sizeof(model), "Unknown - %s", upsstrerror(upserror));
86
 
        
87
 
        printf ("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\"\n");
88
 
        printf ("       \"http://www.w3.org/TR/REC-html40/loose.dtd\">\n");
89
 
 
90
 
        printf ("<HTML><HEAD>\n");
91
 
        if (refreshdelay > 0)
92
 
                printf ("<META HTTP-EQUIV=\"Refresh\" CONTENT=\"%d\">\n", refreshdelay);
93
 
        
94
 
        printf ("<TITLE>%s on %s</TITLE></HEAD>\n", model, monhost);
95
 
        printf ("<BODY BGCOLOR=\"#C8C8C8\" TEXT=\"#000000\" LINK=\"#0000EE\" VLINK=\"#551A8B\">\n");
96
 
        printf ("<CENTER><TABLE BORDER CELLSPACING=10 CELLPADDING=5>\n");
97
 
        printf ("<TR><TH>%s</TH>\n", my_getdate());
98
 
        printf ("<TH>Batt Chg</TH><TH>Utility</TH><TH>UPS Load</TH></TR>\n");
99
 
        printf ("<TR><TD BGCOLOR=\"#FFFFFF\"><PRE>\n");
100
 
        printf ("Monitoring: %s\n", monhost);
101
 
        printf (" UPS Model: %s\n", model);
102
 
 
103
 
        getupsvar (monhost, "status", status, sizeof(status));
104
 
        printf ("    Status: ");
105
 
 
106
 
        stat = status;
107
 
        while (stat != NULL) {
108
 
                ptr = strchr (stat, ' ');
109
 
                if (ptr)
110
 
                        *ptr++ = '\0';  
111
 
 
112
 
                for (i = 0; stattab[i].name != NULL; i++) { 
113
 
                        if (!strcmp(stattab[i].name, stat))
114
 
                                printf ("%s ", stattab[i].desc);
115
 
                }
116
 
 
117
 
                stat = ptr;
118
 
        } 
119
 
        printf ("\n"); 
120
 
 
121
 
        printf ("</PRE></TD>\n");
122
 
 
123
 
        if (getupsvar (monhost, "battpct", battpct, sizeof(battpct)) > 0)
124
 
                printf ("<TD ROWSPAN=3><IMG SRC=\"upsimage.cgi?host=%s&amp;display=battpct\" WIDTH=130 HEIGHT=350 ALT=\"Battery charge: %s %%\">\n", monhost, battpct);
125
 
        else
126
 
                printf ("<TD ROWSPAN=3 VALIGN=TOP>Not supported\n");
127
 
        printf ("</TD>\n");
128
 
 
129
 
        if (getupsvar (monhost, "utility", utility, sizeof(utility)) > 0)
130
 
                printf ("<TD ROWSPAN=3><IMG SRC=\"upsimage.cgi?host=%s&amp;display=utility\" WIDTH=130 HEIGHT=350 ALT=\"Utility: %s VAC\">\n", monhost, utility);
131
 
        else {
132
 
                printf ("<TD ROWSPAN=3 VALIGN=TOP>Not supported\n");
133
 
                strcpy (utility, "");
134
 
        }
135
 
        printf ("</TD>\n");
136
 
 
137
 
        if (getupsvar (monhost, "loadpct", loadpct, sizeof(loadpct)) > 0)
138
 
                printf ("<TD ROWSPAN=3><IMG SRC=\"upsimage.cgi?host=%s&amp;display=loadpct\" WIDTH=130 HEIGHT=350 ALT=\"UPS Load: %s %%\">\n", monhost, loadpct);
139
 
        else
140
 
                printf ("<TD ROWSPAN=3 VALIGN=TOP>Not supported\n");
141
 
        printf ("</TD>\n");
142
 
 
143
 
        printf ("</TR>\n");
144
 
 
145
 
        printf ("<TR><TD BGCOLOR=\"#FFFFFF\"><PRE>\n");
146
 
 
147
 
        /* TODO: handle self tests */
148
 
        printf ("&nbsp;");
149
 
/*      printf ("Last UPS Self Test: Not Available\n"); */
150
 
/*      printf ("    Last Test Date: Not Available\n"); */
151
 
        printf ("</PRE></TD></TR>\n");
152
 
 
153
 
        printf ("<TR><TD BGCOLOR=\"#FFFFFF\"><PRE>\n");
154
 
 
155
 
        if (strlen(utility) != 0)
156
 
                printf ("   UPS Input: %s VAC\n", utility);
157
 
 
158
 
        if (getupsvar (monhost, "upstemp", upstemp, sizeof(upstemp)) > 0) {
159
 
                if (use_celsius)
160
 
                        printf ("    UPS Temp: %s &deg;C\n", upstemp);
161
 
                else {
162
 
                        tempf = (strtod (upstemp, 0) * 1.8) + 32;
163
 
                        printf ("    UPS Temp: %.1f &deg;F\n", tempf); 
164
 
                }
165
 
        }
166
 
 
167
 
        if (getupsvar (monhost, "acfreq", acfreq, sizeof(acfreq)) > 0)
168
 
                printf (" Output Freq: %s Hz\n", acfreq);
169
 
 
170
 
        printf ("</PRE><P>\n");
171
 
        printf ("<CENTER>Network UPS Tools upsstats %s</CENTER>\n", UPS_VERSION);
172
 
 
173
 
        printf ("</TD></TR>\n");
174
 
        printf ("</TABLE></CENTER>\n");
 
742
        printf("Content-type: text/html\n"); 
 
743
        printf("Pragma: no-cache\n");
 
744
        printf("\n");
 
745
 
 
746
        /* if a host is specified, use upsstats-single.html instead */
 
747
        if (monhost) {
 
748
                display_single();
 
749
                exit(0);
 
750
        }
 
751
 
 
752
        /* default: multimon replacement mode */
 
753
 
 
754
        load_hosts_conf();
 
755
 
 
756
        currups = ulhead;
 
757
 
 
758
        display_template("upsstats.html");
 
759
 
 
760
        upscli_disconnect(&ups);
175
761
 
176
762
        return 0;
177
763
}