Skip to content

Commit 70dae7b

Browse files
authored
Merge pull request #3110 from jimklimov/issue-3105
`upssched`: Introduce passing of `NOTIFYMSG` to a `CMDSCRIPT`
2 parents df6a550 + 7bbedea commit 70dae7b

File tree

13 files changed

+354
-80
lines changed

13 files changed

+354
-80
lines changed

NEWS.adoc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,17 @@ https://github.com/networkupstools/nut/milestone/12
100100
* Introduced a `SHUTDOWN_HOSTSYNC` notification message, to report that
101101
the primary `upsmon` initiated the shutdown and has some secondaries
102102
to wait for first. [#3084]
103+
* Make sure an `FSD` notification is issued for each UPS when this primary
104+
`upsmon` instance sets it (and does not return to usual data processing
105+
loop to see and report it like secondaries do). This allows a `NOTIFYCMD`
106+
such as `upssched` on the primary to handle the pending power outage
107+
(e.g. begin stopping heavy services) even while `upsmon` waits for the
108+
secondaries to complete their shutdowns and log out of the `upsd` data
109+
server. [issue #3003, PR #3110]
110+
+
111+
NOTE: If using `upssched` and monitoring multiple UPSes, consider setting up
112+
a `START-TIMER-SHARED` rule with a short (approx. 1 second) timeout to group
113+
several `FSD` notifications into one executed action. [PR #3097]
103114

104115
- `upssched` tool updates:
105116
* Previously in PR #2896 (NUT releases v2.8.3 and v2.8.4) the `UPSNAME` and
@@ -120,6 +131,9 @@ https://github.com/networkupstools/nut/milestone/12
120131
* Introduced `upssched -l` mode to list currently tracked timers. [#3097]
121132
* Make use of `setproctag()` and `getproctag()` to report parent/child
122133
process names. [#3084]
134+
* Introduced optional passing of `NOTIFYMSG` text (normally originating
135+
from `upsmon` which calls `upssched`) as an environment variable into
136+
the ultimately executed `CMDSCRIPT` processes. [#3105]
123137

124138
- `configure` script options:
125139
* Introduced `--with-python{,2,3}-modules-dir` to specify PyNUT(Client)

clients/upsmon.c

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ static unsigned __stdcall async_notify(LPVOID param)
280280
if (notifycmd != NULL) {
281281
snprintf(exec, sizeof(exec), "%s \"%s\"", notifycmd, data->notice);
282282

283+
upsdebugx(6, "%s: Calling NOTIFYCMD: %s", __func__, exec);
283284
if (data->upsname)
284285
setenv("UPSNAME", data->upsname, 1);
285286
else
@@ -1132,8 +1133,20 @@ static void setfsd(utype_t *ups)
11321133
return;
11331134
}
11341135

1135-
if (!strncmp(buf, "OK", 2))
1136+
if (!strncmp(buf, "OK", 2)) {
1137+
upsdebugx(1, "%s: data server confirmed setting FSD for UPS [%s]", __func__, ups->sys);
1138+
1139+
/* Let NOTIFYCMD (if any) know, and have a chance to react */
1140+
if (ups->lastfsdnotify) {
1141+
/* e.g. upsd was still alive with a latched FSD
1142+
* status when this upsmon instance started */
1143+
upsdebugx(2, "%s: not notifying about FSD for UPS [%s] because it was recently reported already", __func__, ups->sys);
1144+
} else {
1145+
time(&(ups->lastfsdnotify));
1146+
do_notify(ups, NOTIFY_FSD, NULL);
1147+
}
11361148
return;
1149+
}
11371150

11381151
/* protocol error: upsd said something other than "OK" */
11391152
upslogx(LOG_ERR, "FSD set on UPS %s failed: %s", ups->sys, buf);
@@ -1842,9 +1855,12 @@ static void ups_fsd(utype_t *ups)
18421855

18431856
upsdebugx(3, "%s: %s (first time)", __func__, ups->sys);
18441857

1845-
/* must have changed from !FSD to FSD, so notify */
1858+
/* must have changed from !FSD to FSD, so notify; avoid duplicates though */
18461859

1847-
do_notify(ups, NOTIFY_FSD, NULL);
1860+
if (!(ups->lastfsdnotify)) {
1861+
time(&(ups->lastfsdnotify));
1862+
do_notify(ups, NOTIFY_FSD, NULL);
1863+
}
18481864
setflag(&ups->status, ST_FSD);
18491865
}
18501866

@@ -2112,6 +2128,8 @@ static void addups(int reloading, const char *sys, const char *pvs,
21122128
tmp->lastrbwarn = 0;
21132129
tmp->lastncwarn = 0;
21142130

2131+
tmp->lastfsdnotify = 0;
2132+
21152133
tmp->offsince = 0;
21162134
tmp->oblbsince = 0;
21172135
tmp->oversince = 0;
@@ -2867,8 +2885,10 @@ static void parse_status(utype_t *ups, char *status, char *buzzword, char *buzzw
28672885
/* clear these out early if they disappear */
28682886
if (!strstr(status, "LB"))
28692887
clearflag(&ups->status, ST_LOWBATT);
2870-
if (!strstr(status, "FSD"))
2888+
if (!strstr(status, "FSD")) {
28712889
clearflag(&ups->status, ST_FSD);
2890+
ups->lastfsdnotify = 0;
2891+
}
28722892

28732893
/* similar to above - clear these flags and send notifications */
28742894
if (!strstr(status, "CAL"))

clients/upsmon.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,12 @@ typedef struct {
9292
int pollfail_log_throttle_count; /* How many pollfreq loops this UPS was in this state since last logged report? */
9393

9494
time_t lastpoll; /* time of last successful poll */
95-
time_t lastnoncrit; /* time of last non-crit poll */
95+
time_t lastnoncrit; /* time of last non-crit poll */
9696
time_t lastrbwarn; /* time of last REPLBATT warning*/
9797
time_t lastncwarn; /* time of last NOCOMM warning */
9898

99+
time_t lastfsdnotify; /* time of last FSD notification (when first discovering the state, or setting it - avoid duplicate notification); 0 initially or if that state clears */
100+
99101
time_t offsince; /* time of recent entry into OFF state */
100102
time_t oblbsince; /* time of recent entry into OB LB state (normally this causes immediate shutdown alert, unless we are configured to delay it) */
101103
time_t oversince; /* time of recent entry into OVER state */

clients/upssched-cmd

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
echo "`date -u`: $0: THIS IS A SAMPLE SCRIPT, PLEASE TAILOR IT FOR YOUR DEPLOYMENT OF NUT!" >&2
1818
logger -t upssched-cmd "THIS IS A SAMPLE SCRIPT, PLEASE TAILOR IT FOR YOUR DEPLOYMENT OF NUT!"
1919

20-
printf "`date -u`: UPSNAME='%s'\tNOTIFYTYPE='%s'\targs=%s\n" "$UPSNAME" "$NOTIFYTYPE" "$@" >&2
21-
printf "UPSNAME='%s' NOTIFYTYPE='%s' args=%s\n" "$UPSNAME" "$NOTIFYTYPE" "$@" | logger -t upssched-cmd-received-NOTIFYTYPE
20+
printf "`date -u`: UPSNAME='%s'\tNOTIFYTYPE='%s'\tNOTIFYMSG='%s'\targs=%s\n" "$UPSNAME" "$NOTIFYTYPE" "$NOTIFYMSG" "$*" >&2
21+
printf "UPSNAME='%s' NOTIFYTYPE='%s' NOTIFYMSG='%s' args=%s\n" "$UPSNAME" "$NOTIFYTYPE" "$NOTIFYMSG" "$*" | logger -t upssched-cmd-received-NOTIFYTYPE
2222

2323
#set
2424

0 commit comments

Comments
 (0)