-
-
Notifications
You must be signed in to change notification settings - Fork 349
/
upsmon.c
2028 lines (1545 loc) · 43.2 KB
/
upsmon.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* upsmon - monitor power status over the 'net (talks to upsd via TCP)
Copyright (C)
1998 Russell Kroll <rkroll@exploits.org>
2012 Arnaud Quette <arnaud.quette.free.fr>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "common.h"
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include "upsclient.h"
#include "upsmon.h"
#include "parseconf.h"
#include "timehead.h"
#ifdef HAVE_STDARG_H
#include <stdarg.h>
#endif
static char *shutdowncmd = NULL, *notifycmd = NULL;
static char *powerdownflag = NULL, *configfile = NULL;
static int minsupplies = 1, sleepval = 5, deadtime = 15;
/* default polling interval = 5 sec */
static int pollfreq = 5, pollfreqalert = 5;
/* slave hosts are given 15 sec by default to logout from upsd */
static int hostsync = 15;
/* sum of all power values from config file */
static int totalpv = 0;
/* default replace battery warning interval (seconds) */
static int rbwarntime = 43200;
/* default "all communications down" warning interval (seconds) */
static int nocommwarntime = 300;
/* default interval between the shutdown warning and the shutdown */
static int finaldelay = 5;
/* set by SIGHUP handler, cleared after reload finishes */
static int reload_flag = 0;
/* set after SIGINT, SIGQUIT, or SIGTERM */
static int exit_flag = 0;
/* userid for unprivileged process when using fork mode */
static char *run_as_user = NULL;
/* SSL details - where to find certs, whether to use them */
static char *certpath = NULL;
static char *certname = NULL;
static char *certpasswd = NULL;
static int certverify = 0; /* don't verify by default */
static int forcessl = 0; /* don't require ssl by default */
static int userfsd = 0, use_pipe = 1, pipefd[2];
static utype_t *firstups = NULL;
static int opt_af = AF_UNSPEC;
/* signal handling things */
static struct sigaction sa;
static sigset_t nut_upsmon_sigmask;
static void setflag(int *val, int flag)
{
*val |= flag;
}
static void clearflag(int *val, int flag)
{
*val ^= (*val & flag);
}
static int flag_isset(int num, int flag)
{
return ((num & flag) == flag);
}
static void wall(const char *text)
{
FILE *wf;
wf = popen("wall", "w");
if (!wf) {
upslog_with_errno(LOG_NOTICE, "Can't invoke wall");
return;
}
fprintf(wf, "%s\n", text);
pclose(wf);
}
static void notify(const char *notice, int flags, const char *ntype,
const char *upsname)
{
char exec[LARGEBUF];
int ret;
if (flag_isset(flags, NOTIFY_IGNORE))
return;
if (flag_isset(flags, NOTIFY_SYSLOG))
upslogx(LOG_NOTICE, "%s", notice);
/* fork here so upsmon doesn't get wedged if the notifier is slow */
ret = fork();
if (ret < 0) {
upslog_with_errno(LOG_ERR, "Can't fork to notify");
return;
}
if (ret != 0) /* parent */
return;
/* child continues and does all the work */
if (flag_isset(flags, NOTIFY_WALL))
wall(notice);
if (flag_isset(flags, NOTIFY_EXEC)) {
if (notifycmd != NULL) {
snprintf(exec, sizeof(exec), "%s \"%s\"", notifycmd, notice);
if (upsname)
setenv("UPSNAME", upsname, 1);
else
setenv("UPSNAME", "", 1);
setenv("NOTIFYTYPE", ntype, 1);
if (system(exec) == -1) {
upslog_with_errno(LOG_ERR, "%s", __func__);
}
}
}
exit(EXIT_SUCCESS);
}
static void do_notify(const utype_t *ups, int ntype)
{
int i;
char msg[SMALLBUF], *upsname = NULL;
/* grab this for later */
if (ups)
upsname = ups->sys;
for (i = 0; notifylist[i].name != NULL; i++) {
if (notifylist[i].type == ntype) {
upsdebugx(2, "%s: ntype 0x%04x (%s)", __func__, ntype,
notifylist[i].name);
snprintf(msg, sizeof(msg), notifylist[i].msg ? notifylist[i].msg : notifylist[i].stockmsg,
ups ? ups->sys : "");
notify(msg, notifylist[i].flags, notifylist[i].name,
upsname);
return;
}
}
/* not found ?! */
}
/* check for master permissions on the server for this ups */
static int checkmaster(utype_t *ups)
{
char buf[SMALLBUF];
/* don't bother if we're not configured as a master for this ups */
if (!flag_isset(ups->status, ST_MASTER))
return 1;
/* this shouldn't happen (LOGIN checks it earlier) */
if ((ups->upsname == NULL) || (strlen(ups->upsname) == 0)) {
upslogx(LOG_ERR, "Set master on UPS [%s] failed: empty upsname",
ups->sys);
return 0;
}
snprintf(buf, sizeof(buf), "MASTER %s\n", ups->upsname);
if (upscli_sendline(&ups->conn, buf, strlen(buf)) < 0) {
upslogx(LOG_ALERT, "Can't set master mode on UPS [%s] - %s",
ups->sys, upscli_strerror(&ups->conn));
return 0;
}
if (upscli_readline(&ups->conn, buf, sizeof(buf)) == 0) {
if (!strncmp(buf, "OK", 2))
return 1;
/* not ERR, but not caught by readline either? */
upslogx(LOG_ALERT, "Master privileges unavailable on UPS [%s]",
ups->sys);
upslogx(LOG_ALERT, "Response: [%s]", buf);
}
else { /* something caught by readraw's parsing call */
upslogx(LOG_ALERT, "Master privileges unavailable on UPS [%s]",
ups->sys);
upslogx(LOG_ALERT, "Reason: %s", upscli_strerror(&ups->conn));
}
return 0;
}
/* authenticate to upsd, plus do LOGIN and MASTER if applicable */
static int do_upsd_auth(utype_t *ups)
{
char buf[SMALLBUF];
if (!ups->un) {
upslogx(LOG_ERR, "UPS [%s]: no username defined!", ups->sys);
return 0;
}
snprintf(buf, sizeof(buf), "USERNAME %s\n", ups->un);
if (upscli_sendline(&ups->conn, buf, strlen(buf)) < 0) {
upslogx(LOG_ERR, "Can't set username on [%s]: %s",
ups->sys, upscli_strerror(&ups->conn));
return 0;
}
if (upscli_readline(&ups->conn, buf, sizeof(buf)) < 0) {
upslogx(LOG_ERR, "Set username on [%s] failed: %s",
ups->sys, upscli_strerror(&ups->conn));
return 0;
}
/* authenticate first */
snprintf(buf, sizeof(buf), "PASSWORD %s\n", ups->pw);
if (upscli_sendline(&ups->conn, buf, strlen(buf)) < 0) {
upslogx(LOG_ERR, "Can't set password on [%s]: %s",
ups->sys, upscli_strerror(&ups->conn));
return 0;
}
if (upscli_readline(&ups->conn, buf, sizeof(buf)) < 0) {
upslogx(LOG_ERR, "Set password on [%s] failed: %s",
ups->sys, upscli_strerror(&ups->conn));
return 0;
}
/* catch insanity from the server - not ERR and not OK either */
if (strncmp(buf, "OK", 2) != 0) {
upslogx(LOG_ERR, "Set password on [%s] failed - got [%s]",
ups->sys, buf);
return 0;
}
/* we require a upsname now */
if ((ups->upsname == NULL) || (strlen(ups->upsname) == 0)) {
upslogx(LOG_ERR, "Login to UPS [%s] failed: empty upsname",
ups->sys);
return 0;
}
/* password is set, let's login */
snprintf(buf, sizeof(buf), "LOGIN %s\n", ups->upsname);
if (upscli_sendline(&ups->conn, buf, strlen(buf)) < 0) {
upslogx(LOG_ERR, "Login to UPS [%s] failed: %s",
ups->sys, upscli_strerror(&ups->conn));
return 0;
}
if (upscli_readline(&ups->conn, buf, sizeof(buf)) < 0) {
upslogx(LOG_ERR, "Can't login to UPS [%s]: %s",
ups->sys, upscli_strerror(&ups->conn));
return 0;
}
/* catch insanity from the server - not ERR and not OK either */
if (strncmp(buf, "OK", 2) != 0) {
upslogx(LOG_ERR, "Login on UPS [%s] failed - got [%s]",
ups->sys, buf);
return 0;
}
/* finally - everything is OK */
upsdebugx(1, "Logged into UPS %s", ups->sys);
setflag(&ups->status, ST_LOGIN);
/* now see if we also need to test master permissions */
return checkmaster(ups);
}
/* set flags and make announcements when a UPS has been checked successfully */
static void ups_is_alive(utype_t *ups)
{
time_t now;
time(&now);
ups->lastpoll = now;
if (ups->commstate == 1) /* already known */
return;
/* only notify for 0->1 transitions (to ignore the first connect) */
if (ups->commstate == 0)
do_notify(ups, NOTIFY_COMMOK);
ups->commstate = 1;
}
/* handle all the notifications for a missing UPS in one place */
static void ups_is_gone(utype_t *ups)
{
time_t now;
/* first time: clear the flag and throw the first notifier */
if (ups->commstate != 0) {
ups->commstate = 0;
/* COMMBAD is the initial loss of communications */
do_notify(ups, NOTIFY_COMMBAD);
return;
}
time(&now);
/* first only act if we're <nocommtime> seconds past the last poll */
if ((now - ups->lastpoll) < nocommwarntime)
return;
/* now only complain if we haven't lately */
if ((now - ups->lastncwarn) > nocommwarntime) {
/* NOCOMM indicates a persistent condition */
do_notify(ups, NOTIFY_NOCOMM);
ups->lastncwarn = now;
}
}
static void ups_on_batt(utype_t *ups)
{
if (flag_isset(ups->status, ST_ONBATT)) { /* no change */
upsdebugx(4, "%s: %s (no change)", __func__, ups->sys);
return;
}
sleepval = pollfreqalert; /* bump up polling frequency */
ups->linestate = 0;
upsdebugx(3, "%s: %s (first time)", __func__, ups->sys);
/* must have changed from OL to OB, so notify */
do_notify(ups, NOTIFY_ONBATT);
setflag(&ups->status, ST_ONBATT);
clearflag(&ups->status, ST_ONLINE);
}
static void ups_on_line(utype_t *ups)
{
if (flag_isset(ups->status, ST_ONLINE)) { /* no change */
upsdebugx(4, "%s: %s (no change)", __func__, ups->sys);
return;
}
sleepval = pollfreq;
upsdebugx(3, "%s: %s (first time)", __func__, ups->sys);
/* ignore the first OL at startup, otherwise send the notifier */
if (ups->linestate != -1)
do_notify(ups, NOTIFY_ONLINE);
ups->linestate = 1;
setflag(&ups->status, ST_ONLINE);
clearflag(&ups->status, ST_ONBATT);
}
/* create the flag file if necessary */
static void set_pdflag(void)
{
FILE *pdf;
if (!powerdownflag)
return;
pdf = fopen(powerdownflag, "w");
if (!pdf) {
upslogx(LOG_ERR, "Failed to create power down flag!");
return;
}
fprintf(pdf, "%s", SDMAGIC);
fclose(pdf);
}
/* the actual shutdown procedure */
static void doshutdown(void)
{
int ret;
/* this should probably go away at some point */
upslogx(LOG_CRIT, "Executing automatic power-fail shutdown");
wall("Executing automatic power-fail shutdown\n");
do_notify(NULL, NOTIFY_SHUTDOWN);
sleep(finaldelay);
/* in the pipe model, we let the parent do this for us */
if (use_pipe) {
char ch;
ch = 1;
ret = write(pipefd[1], &ch, 1);
} else {
/* one process model = we do all the work here */
if (geteuid() != 0)
upslogx(LOG_WARNING, "Not root, shutdown may fail");
set_pdflag();
ret = system(shutdowncmd);
if (ret != 0)
upslogx(LOG_ERR, "Unable to call shutdown command: %s",
shutdowncmd);
}
exit(EXIT_SUCCESS);
}
/* set forced shutdown flag so other upsmons know what's going on here */
static void setfsd(utype_t *ups)
{
char buf[SMALLBUF];
int ret;
/* this shouldn't happen */
if (!ups->upsname) {
upslogx(LOG_ERR, "setfsd: programming error: no UPS name set [%s]",
ups->sys);
return;
}
upsdebugx(2, "Setting FSD on UPS %s", ups->sys);
snprintf(buf, sizeof(buf), "FSD %s\n", ups->upsname);
ret = upscli_sendline(&ups->conn, buf, strlen(buf));
if (ret < 0) {
upslogx(LOG_ERR, "FSD set on UPS %s failed: %s", ups->sys,
upscli_strerror(&ups->conn));
return;
}
ret = upscli_readline(&ups->conn, buf, sizeof(buf));
if (ret < 0) {
upslogx(LOG_ERR, "FSD set on UPS %s failed: %s", ups->sys,
upscli_strerror(&ups->conn));
return;
}
if (!strncmp(buf, "OK", 2))
return;
/* protocol error: upsd said something other than "OK" */
upslogx(LOG_ERR, "FSD set on UPS %s failed: %s", ups->sys, buf);
}
static void set_alarm(void)
{
alarm(NET_TIMEOUT);
}
static void clear_alarm(void)
{
signal(SIGALRM, SIG_IGN);
alarm(0);
}
static int get_var(utype_t *ups, const char *var, char *buf, size_t bufsize)
{
int ret;
unsigned int numq, numa;
const char *query[4];
char **answer;
/* this shouldn't happen */
if (!ups->upsname) {
upslogx(LOG_ERR, "get_var: programming error: no UPS name set [%s]",
ups->sys);
return -1;
}
numq = 0;
if (!strcmp(var, "numlogins")) {
query[0] = "NUMLOGINS";
query[1] = ups->upsname;
numq = 2;
}
if (!strcmp(var, "status")) {
query[0] = "VAR";
query[1] = ups->upsname;
query[2] = "ups.status";
numq = 3;
}
if (numq == 0) {
upslogx(LOG_ERR, "get_var: programming error: var=%s", var);
return -1;
}
upsdebugx(3, "%s: %s / %s", __func__, ups->sys, var);
ret = upscli_get(&ups->conn, numq, query, &numa, &answer);
if (ret < 0) {
/* detect old upsd */
if (upscli_upserror(&ups->conn) == UPSCLI_ERR_UNKCOMMAND) {
upslogx(LOG_ERR, "UPS [%s]: Too old to monitor",
ups->sys);
return -1;
}
/* some other error */
return -1;
}
if (numa < numq) {
upslogx(LOG_ERR, "%s: Error: insufficient data "
"(got %d args, need at least %d)",
var, numa, numq);
return -1;
}
snprintf(buf, bufsize, "%s", answer[numq]);
return 0;
}
static void slavesync(void)
{
utype_t *ups;
char temp[SMALLBUF];
time_t start, now;
int maxlogins, logins;
time(&start);
for (;;) {
maxlogins = 0;
for (ups = firstups; ups != NULL; ups = ups->next) {
/* only check login count on our master(s) */
if (!flag_isset(ups->status, ST_MASTER))
continue;
set_alarm();
if (get_var(ups, "numlogins", temp, sizeof(temp)) >= 0) {
logins = strtol(temp, (char **)NULL, 10);
if (logins > maxlogins)
maxlogins = logins;
}
clear_alarm();
}
/* if no UPS has more than 1 login (us), then slaves are gone */
if (maxlogins <= 1)
return;
/* after HOSTSYNC seconds, assume slaves are stuck and bail */
time(&now);
if ((now - start) > hostsync) {
upslogx(LOG_INFO, "Host sync timer expired, forcing shutdown");
return;
}
usleep(250000);
}
}
static void forceshutdown(void)
{
utype_t *ups;
int isamaster = 0;
upsdebugx(1, "Shutting down any UPSes in MASTER mode...");
/* set FSD on any "master" UPS entries (forced shutdown in progress) */
for (ups = firstups; ups != NULL; ups = ups->next)
if (flag_isset(ups->status, ST_MASTER)) {
isamaster = 1;
setfsd(ups);
}
/* if we're not a master on anything, we should shut down now */
if (!isamaster)
doshutdown();
/* must be the master now */
upsdebugx(1, "This system is a master... waiting for slave logout...");
/* wait up to HOSTSYNC seconds for slaves to logout */
slavesync();
/* time expired or all the slaves are gone, so shutdown */
doshutdown();
}
static int is_ups_critical(utype_t *ups)
{
time_t now;
/* FSD = the master is forcing a shutdown */
if (flag_isset(ups->status, ST_FSD))
return 1;
/* not OB or not LB = not critical yet */
if ((!flag_isset(ups->status, ST_ONBATT)) ||
(!flag_isset(ups->status, ST_LOWBATT)))
return 0;
/* must be OB+LB now */
/* if we're a master, declare it critical so we set FSD on it */
if (flag_isset(ups->status, ST_MASTER))
return 1;
/* must be a slave now */
/* FSD isn't set, so the master hasn't seen it yet */
time(&now);
/* give the master up to HOSTSYNC seconds before shutting down */
if ((now - ups->lastnoncrit) > hostsync) {
upslogx(LOG_WARNING, "Giving up on the master for UPS [%s]",
ups->sys);
return 1;
}
/* there's still time left */
return 0;
}
/* recalculate the online power value and see if things are still OK */
static void recalc(void)
{
utype_t *ups;
int val_ol = 0;
time_t now;
time(&now);
ups = firstups;
while (ups != NULL) {
/* promote dead UPSes that were last known OB to OB+LB */
if ((now - ups->lastpoll) > deadtime)
if (flag_isset(ups->status, ST_ONBATT)) {
upsdebugx(1, "Promoting dead UPS: %s", ups->sys);
setflag(&ups->status, ST_LOWBATT);
}
/* note: we assume that a UPS that isn't critical must be OK *
* *
* this means a UPS we've never heard from is assumed OL *
* whether this is really the best thing to do is undecided */
/* crit = (FSD) || (OB & LB) > HOSTSYNC seconds */
if (is_ups_critical(ups))
upsdebugx(1, "Critical UPS: %s", ups->sys);
else
val_ol += ups->pv;
ups = ups->next;
}
upsdebugx(3, "Current power value: %d", val_ol);
upsdebugx(3, "Minimum power value: %d", minsupplies);
if (val_ol < minsupplies)
forceshutdown();
}
static void ups_low_batt(utype_t *ups)
{
if (flag_isset(ups->status, ST_LOWBATT)) { /* no change */
upsdebugx(4, "%s: %s (no change)", __func__, ups->sys);
return;
}
upsdebugx(3, "%s: %s (first time)", __func__, ups->sys);
/* must have changed from !LB to LB, so notify */
do_notify(ups, NOTIFY_LOWBATT);
setflag(&ups->status, ST_LOWBATT);
}
static void upsreplbatt(utype_t *ups)
{
time_t now;
time(&now);
if ((now - ups->lastrbwarn) > rbwarntime) {
do_notify(ups, NOTIFY_REPLBATT);
ups->lastrbwarn = now;
}
}
static void ups_fsd(utype_t *ups)
{
if (flag_isset(ups->status, ST_FSD)) { /* no change */
upsdebugx(4, "%s: %s (no change)", __func__, ups->sys);
return;
}
upsdebugx(3, "%s: %s (first time)", __func__, ups->sys);
/* must have changed from !FSD to FSD, so notify */
do_notify(ups, NOTIFY_FSD);
setflag(&ups->status, ST_FSD);
}
/* cleanly close the connection to a given UPS */
static void drop_connection(utype_t *ups)
{
upsdebugx(2, "Dropping connection to UPS [%s]", ups->sys);
ups->commstate = 0;
ups->linestate = 0;
clearflag(&ups->status, ST_LOGIN);
clearflag(&ups->status, ST_CONNECTED);
upscli_disconnect(&ups->conn);
}
/* change some UPS parameters during reloading */
static void redefine_ups(utype_t *ups, int pv, const char *un,
const char *pw, const char *master)
{
ups->retain = 1;
if (ups->pv != pv) {
upslogx(LOG_INFO, "UPS [%s]: redefined power value to %d",
ups->sys, pv);
ups->pv = pv;
}
totalpv += ups->pv;
if (ups->un) {
if (strcmp(ups->un, un) != 0) {
upslogx(LOG_INFO, "UPS [%s]: redefined username",
ups->sys);
free(ups->un);
ups->un = xstrdup(un);
/*
* if not logged in force a reconnection since this
* may have been redefined to make a login work
*/
if (!flag_isset(ups->status, ST_LOGIN)) {
upslogx(LOG_INFO, "UPS [%s]: retrying connection",
ups->sys);
drop_connection(ups);
}
} /* if (strcmp(ups->un, un) != 0) { */
} else {
/* adding a username? (going to new style MONITOR line) */
if (un) {
upslogx(LOG_INFO, "UPS [%s]: defined username",
ups->sys);
ups->un = xstrdup(un);
/* possibly force reconnection - see above */
if (!flag_isset(ups->status, ST_LOGIN)) {
upslogx(LOG_INFO, "UPS [%s]: retrying connection",
ups->sys);
drop_connection(ups);
}
} /* if (un) */
}
/* paranoia */
if (!ups->pw)
ups->pw = xstrdup(""); /* give it a bogus, but non-NULL one */
/* obviously don't put the new password in the syslog... */
if (strcmp(ups->pw, pw) != 0) {
upslogx(LOG_INFO, "UPS [%s]: redefined password", ups->sys);
free(ups->pw);
ups->pw = xstrdup(pw);
/* possibly force reconnection - see above */
if (!flag_isset(ups->status, ST_LOGIN)) {
upslogx(LOG_INFO, "UPS [%s]: retrying connection",
ups->sys);
drop_connection(ups);
}
}
/* slave -> master */
if ((!strcasecmp(master, "master")) && (!flag_isset(ups->status, ST_MASTER))) {
upslogx(LOG_INFO, "UPS [%s]: redefined as master", ups->sys);
setflag(&ups->status, ST_MASTER);
/* reset connection to ensure master mode gets checked */
drop_connection(ups);
return;
}
/* master -> slave */
if ((!strcasecmp(master, "slave")) && (flag_isset(ups->status, ST_MASTER))) {
upslogx(LOG_INFO, "UPS [%s]: redefined as slave", ups->sys);
clearflag(&ups->status, ST_MASTER);
return;
}
}
static void addups(int reloading, const char *sys, const char *pvs,
const char *un, const char *pw, const char *master)
{
int pv;
utype_t *tmp, *last;
/* the username is now required - no more host-based auth */
if ((!sys) || (!pvs) || (!pw) || (!master) || (!un)) {
upslogx(LOG_WARNING, "Ignoring invalid MONITOR line in %s!", configfile);
upslogx(LOG_WARNING, "MONITOR configuration directives require five arguments.");
return;
}
pv = strtol(pvs, (char **) NULL, 10);
if (pv < 0) {
upslogx(LOG_WARNING, "UPS [%s]: ignoring invalid power value [%s]",
sys, pvs);
return;
}
last = tmp = firstups;
while (tmp) {
last = tmp;
/* check for duplicates */
if (!strcmp(tmp->sys, sys)) {
if (reloading)
redefine_ups(tmp, pv, un, pw, master);
else
upslogx(LOG_WARNING, "Warning: ignoring duplicate"
" UPS [%s]", sys);
return;
}
tmp = tmp->next;
}
tmp = xmalloc(sizeof(utype_t));
tmp->sys = xstrdup(sys);
tmp->pv = pv;
/* build this up so the user doesn't run with bad settings */
totalpv += tmp->pv;
if (un)
tmp->un = xstrdup(un);
else
tmp->un = NULL;
tmp->pw = xstrdup(pw);
tmp->status = 0;
tmp->retain = 1;
/* ignore initial COMMOK and ONLINE by default */
tmp->commstate = -1;
tmp->linestate = -1;
tmp->lastpoll = 0;
tmp->lastnoncrit = 0;
tmp->lastrbwarn = 0;
tmp->lastncwarn = 0;
if (!strcasecmp(master, "master"))
setflag(&tmp->status, ST_MASTER);
tmp->next = NULL;
if (last)
last->next = tmp;
else
firstups = tmp;
if (tmp->pv)
upslogx(LOG_INFO, "UPS: %s (%s) (power value %d)", tmp->sys,
flag_isset(tmp->status, ST_MASTER) ? "master" : "slave",
tmp->pv);
else
upslogx(LOG_INFO, "UPS: %s (monitoring only)", tmp->sys);
tmp->upsname = tmp->hostname = NULL;
if (upscli_splitname(tmp->sys, &tmp->upsname, &tmp->hostname,
&tmp->port) != 0) {
upslogx(LOG_ERR, "Error: unable to split UPS name [%s]",
tmp->sys);
}
if (!tmp->upsname)
upslogx(LOG_WARNING, "Warning: UPS [%s]: no upsname set!",
tmp->sys);
}
static void set_notifymsg(const char *name, const char *msg)
{
int i;
for (i = 0; notifylist[i].name != NULL; i++) {
if (!strcasecmp(notifylist[i].name, name)) {
free(notifylist[i].msg);
notifylist[i].msg = xstrdup(msg);
return;
}
}
upslogx(LOG_WARNING, "'%s' is not a valid notify event name", name);
}
static void set_notifyflag(const char *ntype, char *flags)
{
int i, pos;
char *ptr, *tmp;
/* find ntype */
pos = -1;
for (i = 0; notifylist[i].name != NULL; i++) {
if (!strcasecmp(notifylist[i].name, ntype)) {
pos = i;
break;
}
}
if (pos == -1) {
upslogx(LOG_WARNING, "Warning: invalid notify type [%s]", ntype);
return;
}
ptr = flags;