Skip to content

Commit 84201fb

Browse files
authored
Merge 957a4aa into 559230b
2 parents 559230b + 957a4aa commit 84201fb

5 files changed

Lines changed: 138 additions & 36 deletions

File tree

‎NEWS.adoc‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,8 @@ https://github.com/networkupstools/nut/milestone/13
340340
corresponding `nutdrv_qx` transports, which this code predates.
341341

342342
- `dummy-ups` driver updates:
343+
* Playback of `nut-recorder` sequences now handles recorded alarm
344+
descriptions as they appear, change, and clear. [PR #3867]
343345
* Repeater mode now marks data stale after a failed refresh, including
344346
an interrupted variable list, and restores freshness after a complete
345347
response. Runtime reconnection attempts continue as before. [PR #3648]

‎docs/man/dummy-ups.txt‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,16 @@ states. See below for an example of setting and resetting alarms:
248248
TIMER 5
249249
ALARM [UPS too cold to charge]
250250

251+
Recordings made with linkman:nut-recorder[8] use `ups.alarm:` assignments.
252+
The recorder quotes the description so spaces and characters such as
253+
`#` remain part of the text. Each assignment replaces the previous
254+
description. An empty assignment clears it. Older recordings omit the empty assignment. When their
255+
`ups.status` loses the `ALARM` token, the driver clears the recorded
256+
description. A status change by itself leaves messages raised with the
257+
`ALARM` instruction active. During playback, a connected linkman:upsd[8]
258+
may report changed text containing a double quote or backslash incorrectly
259+
until it reads the complete driver state again.
260+
251261
Repeater Mode
252262
~~~~~~~~~~~~~
253263

‎docs/man/nut-recorder.txt‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ and dump it in a `.seq` format.
2121
The `.seq` file can then be used by the linkman:dummy-ups[8] driver
2222
to replay the sequence.
2323

24+
After a failed poll, a server reporting `WAIT` may still be loading driver
25+
data. The recorder keeps new readings and defers clearing a missing alarm
26+
description until the status leaves `WAIT` or the device reports an explicit
27+
`ups.alarm` value. During uninterrupted polling, `WAIT` is recorded like
28+
any other status.
29+
2430
OPTIONS
2531
-------
2632

‎drivers/dummy-ups.c‎

Lines changed: 52 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
#include "dummy-ups.h"
4949

5050
#define DRIVER_NAME "Device simulation and repeater driver"
51-
#define DRIVER_VERSION "0.28"
51+
#define DRIVER_VERSION "0.29"
5252

5353
/* driver description structure */
5454
upsdrv_info_t upsdrv_info =
@@ -785,6 +785,22 @@ static void upsconf_err(const char *errmsg)
785785
upslogx(LOG_ERR, "Fatal error in parseconf(ups.conf): %s", errmsg);
786786
}
787787

788+
/* Join the words of an alarm message from the definition file. */
789+
static void parse_alarm_message(char *value, size_t size)
790+
{
791+
size_t i, used = 0;
792+
793+
value[0] = '\0';
794+
for (i = 1; i < ctx->numargs; i++) {
795+
int count = snprintf(value + used, size - used, "%s%s",
796+
i == 1 ? "" : " ", ctx->arglist[i]);
797+
798+
if (count < 0 || (size_t)count >= size - used)
799+
break;
800+
used += (size_t)count;
801+
}
802+
}
803+
788804
/* for dummy mode
789805
* parse the definition file and process its content
790806
*/
@@ -793,6 +809,8 @@ static int parse_data_file(TYPE_FD arg_upsfd)
793809
char fn[NUT_PATH_MAX + 1];
794810
char *ptr, var_value[MAX_STRING_SIZE];
795811
size_t value_args = 0, counter;
812+
static int recorded_alarm = 0; /* carry an assignment across TIMER lines */
813+
int alarm_assigned = 0;
796814
time_t now;
797815
NUT_UNUSED_VARIABLE(arg_upsfd);
798816

@@ -832,6 +850,7 @@ static int parse_data_file(TYPE_FD arg_upsfd)
832850
status_init(); /* in case no ups.status does it */
833851
upsdebugx(5, "%s: call alarm_init()", __func__);
834852
alarm_init(); /* reset alarms at start of parsing */
853+
recorded_alarm = 0;
835854
}
836855

837856
/* Reset the next call time, so that we can loop back on the file
@@ -873,23 +892,18 @@ static int parse_data_file(TYPE_FD arg_upsfd)
873892
/* ALARM instruction */
874893
if (!strncmp(ctx->arglist[0], "ALARM", 5))
875894
{
876-
if (ctx->numargs > 1) {
877-
for (counter = 1, value_args = ctx->numargs ;
878-
counter < value_args ; counter++)
879-
{
880-
if (counter == 1) /* don't append the first space separator */
881-
snprintf(var_value, sizeof(var_value), "%s", ctx->arglist[counter]);
882-
else
883-
snprintfcat(var_value, sizeof(var_value), " %s", ctx->arglist[counter]);
884-
}
885-
if (*var_value != '\0') {
886-
alarm_set(var_value);
887-
upsdebugx(3, "parse_data_file: ALARM instruction with value \"%s\"", var_value);
888-
889-
continue;
890-
}
895+
parse_alarm_message(var_value, sizeof(var_value));
896+
if (*var_value) {
897+
const st_tree_t *previous = dstate_tree_find("ups.alarm");
898+
if (recorded_alarm && previous)
899+
alarm_set(previous->raw);
900+
recorded_alarm = 0;
901+
alarm_set(var_value);
902+
upsdebugx(3, "parse_data_file: ALARM instruction with value \"%s\"", var_value);
903+
continue;
891904
}
892905

906+
recorded_alarm = 0;
893907
alarm_init();
894908
upsdebugx(3, "parse_data_file: ALARM instruction with no value (reset alarms)");
895909

@@ -914,13 +928,32 @@ static int parse_data_file(TYPE_FD arg_upsfd)
914928
/* special handler for status */
915929
if (!strncmp( ctx->arglist[0], "ups.status", 10))
916930
{
931+
int had_recorded_alarm = recorded_alarm && !alarm_assigned &&
932+
str_contains_token(dstate_getinfo("ups.status"), "ALARM");
933+
917934
status_init();
918935
for (counter = 1, value_args = ctx->numargs ;
919936
counter < value_args ; counter++)
920937
{
921938
status_set(ctx->arglist[counter]);
922939
}
923940
status_commit();
941+
942+
/* Older recordings omit deleted variables. Dropping ALARM
943+
* clears a description carried from a previous snapshot. */
944+
if (had_recorded_alarm && !str_contains_token(dstate_getinfo("ups.status"), "ALARM"))
945+
recorded_alarm = 0;
946+
}
947+
else if (!strcmp(ctx->arglist[0], "ups.alarm"))
948+
{
949+
char message[ST_MAX_VALUE_LEN];
950+
951+
parse_alarm_message(message, sizeof(message));
952+
alarm_init();
953+
alarm_assigned = 1;
954+
recorded_alarm = (*message != '\0');
955+
if (recorded_alarm)
956+
dstate_setinfo("ups.alarm", "%s", message);
924957
}
925958
else
926959
{
@@ -946,7 +979,9 @@ static int parse_data_file(TYPE_FD arg_upsfd)
946979
}
947980
}
948981

949-
alarm_commit(); /* needs to happen first */
982+
/* A snapshot publishes its description and status independently. */
983+
if (!recorded_alarm)
984+
alarm_commit(); /* needs to happen first */
950985
status_commit(); /* re-commit status for ALARM */
951986

952987
/* Cleanup parseconf if there is no pending action */

‎tools/nut-recorder.sh‎

Lines changed: 68 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,24 @@ if [ -z "$1" ]; then
3030
echo "$strUsage"
3131
exit
3232
else
33-
devName=$1
33+
devName="$1"
3434
fi
3535

3636
if [ -z "$2" ]; then
37-
outFile=$DEFAULT_OUTPUT
37+
outFile="$DEFAULT_OUTPUT"
3838
else
39-
outFile=$2
39+
outFile="$2"
4040
fi
4141

4242
if [ -z "$3" ]; then
43-
pollInterval=$DEFAULT_INTERVAL
43+
pollInterval="$DEFAULT_INTERVAL"
4444
else
45-
pollInterval=$3
45+
pollInterval="$3"
4646
fi
4747

4848
# initialize TIMER value
4949
curTimer=0
50+
recovering=false
5051

5152
# Test communication with the device
5253
testResult="`upsc \"$devName\" 2>&1 > /dev/null`"
@@ -56,45 +57,93 @@ if [ $? -gt 0 ]; then
5657
fi
5758

5859
# initialize output file
59-
echo "# dummy-ups sequence recorded with $0\n"> $outFile
60-
61-
# initialize data
62-
upsc $devName >> $outFile
63-
cp -f $outFile ${TEMP_DIR}/prevDump.tmp
60+
echo "# dummy-ups sequence recorded with $0\n"> "$outFile"
61+
62+
# Preserve the full alarm description as one quoted sequence-file value.
63+
quote_alarm() {
64+
sed '/^ups\.alarm: / {
65+
s/[\\"]/\\&/g
66+
s/^ups\.alarm: /ups.alarm: "/
67+
s/$/"/
68+
}'
69+
}
70+
71+
# Compare previous and current data collections, see more comments below.
72+
diff_dumps() {
73+
diff --unchanged-line-format=' %L' --old-line-format='-%L' --new-line-format='+%L' "${TEMP_DIR}/prevDump.tmp" "${TEMP_DIR}/curDump.tmp" \
74+
| awk '
75+
/^ ups\.alarm:/ { retained = substr($0, 2) }
76+
/^-ups\.alarm:/ { removed = 1 }
77+
/^[+]ups\.alarm:/ { added = 1 }
78+
/^[+]ups\.status:/ { status_changed = 1 }
79+
/^[+]/ { print substr($0, 2) }
80+
END {
81+
if (status_changed && retained)
82+
print retained
83+
else if (removed && !added && !retained)
84+
print "ups.alarm:"
85+
}' \
86+
| quote_alarm
87+
}
88+
89+
# Keep the last successful upsc snapshot in its original format for diff.
90+
upsc "$devName" > "${TEMP_DIR}/prevDump.tmp" || exit 1
91+
quote_alarm < "${TEMP_DIR}/prevDump.tmp" >> "$outFile"
6492

6593
echo "Initial data:\n"
66-
cat $outFile
94+
cat "$outFile"
6795

6896
while (true)
6997
do
7098
# rest a bit before getting fresh data
7199
sleep $pollInterval
72100

73101
# update the TIMER value
74-
curTimer=`expr $curTimer + $pollInterval`
102+
curTimer="`expr \"$curTimer\" + \"$pollInterval\"`"
75103

76104
# dump the current data
77105
testResult="`upsc \"$devName\" 2>&1 > \"${TEMP_DIR}/curDump.tmp\"`"
78106
if [ $? -gt 0 ]; then
79107
echo "$devName: $testResult"
80-
# FIXME: what to do (pause, exit)?
108+
recovering=true
109+
continue
110+
fi
111+
112+
# On reconnect, upsd can report WAIT before the driver dump is
113+
# complete. A device can also report WAIT with real readings.
114+
if "$recovering" && grep -Fqx 'ups.status: WAIT' "${TEMP_DIR}/curDump.tmp"; then
115+
if ! grep -Eqv '^(ups\.status: WAIT|driver\.state: .*)$' "${TEMP_DIR}/curDump.tmp"; then
116+
continue
117+
fi
118+
# Keep affirmative changes. upsc has no dump-complete marker,
119+
# so defer a missing description until a subsequent non-WAIT
120+
# snapshot or an explicit alarm value resolves it.
121+
if ! grep -q '^ups\.alarm:' "${TEMP_DIR}/curDump.tmp"; then
122+
grep '^ups\.alarm:' "${TEMP_DIR}/prevDump.tmp" >> "${TEMP_DIR}/curDump.tmp"
123+
else
124+
recovering=false
125+
fi
126+
else
127+
recovering=false
81128
fi
82129

83-
# do the diff
84-
dataDiff="`diff --unchanged-line-format='' --old-line-format='' --new-line-format='%L' ${TEMP_DIR}/prevDump.tmp ${TEMP_DIR}/curDump.tmp`"
130+
# Emit changed values. Repeat an unchanged description on a status change
131+
# so playback can distinguish this from a clear in an older recording.
132+
# Record a disappearing description explicitly.
133+
dataDiff="`diff_dumps`"
85134

86135
# dump actual, if any
87136
if [ ! -z "${dataDiff}" ]; then
88137
# dump differences
89-
echo "TIMER $curTimer" >> $outFile
90-
echo "$dataDiff" >> $outFile
138+
echo "TIMER $curTimer" >> "$outFile"
139+
printf '%s\n' "$dataDiff" >> "$outFile"
91140
# and echo out
92141
echo "TIMER $curTimer"
93-
echo "$dataDiff"
142+
printf '%s\n' "$dataDiff"
94143
curTimer=0
95144
fi
96145

97146
# rotate dumps
98-
mv ${TEMP_DIR}/curDump.tmp ${TEMP_DIR}/prevDump.tmp
147+
mv "${TEMP_DIR}/curDump.tmp" "${TEMP_DIR}/prevDump.tmp"
99148

100149
done

0 commit comments

Comments
 (0)