Skip to content

Commit

Permalink
clients/upsmon.c, NEWS.adoc: optimize parse_status() by not checking …
Browse files Browse the repository at this point in the history
…further strings if we had a match; report unexpected tokens [#415, #2708]

Signed-off-by: Jim Klimov <[email protected]>
  • Loading branch information
jimklimov committed Dec 3, 2024
1 parent c85e741 commit 97c6838
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 10 deletions.
2 changes: 2 additions & 0 deletions NEWS.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,8 @@ https://github.com/networkupstools/nut/milestone/11
from templates). [issue #321, PR #2383]
* added an `OBLBDURATION` (seconds) setting to optionally delay raising
the alarm for immediate shutdown in critical situation. [#321]
* optimized `parse_status()` by not checking further strings if we had
a match; report unexpected tokens in debug log. [#415]
- More systemd integration:
* Introduced a `nut-sleep.service` unit which stops `nut.target` when a
Expand Down
65 changes: 55 additions & 10 deletions clients/upsmon.c
Original file line number Diff line number Diff line change
Expand Up @@ -2432,6 +2432,7 @@ static int try_connect(utype_t *ups)
static void parse_status(utype_t *ups, char *status)
{
char *statword, *ptr;
int handled_stat_words = 0;

clear_alarm();

Expand Down Expand Up @@ -2467,38 +2468,82 @@ static void parse_status(utype_t *ups, char *status)

/* split up the status words and parse each one separately */
while (statword != NULL) {
int handled = 0;

ptr = strchr(statword, ' ');
if (ptr)
*ptr++ = '\0';

upsdebugx(3, "parsing: [%s]", statword);
handled_stat_words++;

if (!strcasecmp(statword, "OL"))
/* Keep in sync with "Status data" chapter of docs/new-drivers.txt */
if (!strcasecmp(statword, "OL")) {
ups_on_line(ups);
if (!strcasecmp(statword, "OB"))
handled++;
}
else if (!strcasecmp(statword, "OB")) {
ups_on_batt(ups);
if (!strcasecmp(statword, "LB"))
handled++;
}
else if (!strcasecmp(statword, "LB")) {
ups_low_batt(ups);
if (!strcasecmp(statword, "RB"))
handled++;
}
else if (!strcasecmp(statword, "RB")) {
upsreplbatt(ups);
if (!strcasecmp(statword, "CAL"))
handled++;
}
else if (!strcasecmp(statword, "CAL")) {
ups_is_cal(ups);
if (!strcasecmp(statword, "OFF"))
handled++;
}
else if (!strcasecmp(statword, "OFF")) {
ups_is_off(ups);
if (!strcasecmp(statword, "BYPASS"))
handled++;
}
else if (!strcasecmp(statword, "BYPASS")) {
ups_is_bypass(ups);
if (!strcasecmp(statword, "ECO"))
handled++;
}
else if (!strcasecmp(statword, "ECO")) {
ups_is_eco(ups);
if (!strcasecmp(statword, "ALARM"))
handled++;
}
else if (!strcasecmp(statword, "ALARM")) {
ups_is_alarm(ups);
handled++;
}
/* do it last to override any possible OL */
if (!strcasecmp(statword, "FSD"))
else if (!strcasecmp(statword, "FSD")) {
ups_fsd(ups);
handled++;
}
/* Known standard status tokens, some being obsoleted, no upsmon reaction assigned */
else if (
!strcasecmp(statword, "HB")
|| !strcasecmp(statword, "CHRG")
|| !strcasecmp(statword, "DISCHRG")
|| !strcasecmp(statword, "OVER")
|| !strcasecmp(statword, "TRIM")
|| !strcasecmp(statword, "BOOST")
) {
upsdebugx(4, "Known and ignored status token: [%s]", statword);
handled++;
}

if (!handled) {
/* Driver reported something non-standard? */
upsdebugx(1, "WARNING: Unexpected status token: [%s]", statword);
/* FIXME: Define a notification type like "OTHER" to report the un-handled status */
}

update_crittimer(ups);

statword = ptr;
}

upsdebugx(3, "Handled %d status tokens", handled_stat_words);
}

/* see what the status of the UPS is and handle any changes */
Expand Down

0 comments on commit 97c6838

Please sign in to comment.