Hello NUT developers,

docs/nut-names.txt asks for new variable names to be requested on this
list, so I would like to propose one: driver.data.age.

Problem
-------

A client currently cannot tell fresh UPS data from data the driver failed
to refresh. When reads from the device fail or return nothing, the driver
keeps the last value. upsd only reports "Data stale" when the driver itself
stops answering (PING/PONG, MAXAGE). A driver that is alive but gets no data
from the device therefore looks healthy.

I hit this with a CyberPower VP1600ELCD (USB 0764:0501, usbhid-ups, xHCI
host):

- After the HID Report Descriptor was read corrupted at startup (handled in
  a separate change), the driver polled nothing usable for about 4 hours.
  During that time "upsc" kept showing "ups.status: OL" and
  "driver.state: quiet", with no "Data stale".
- With "pollonly", the status report (0x22) failed in about 93% of the
  reads, and "upsc" served the last good values without any hint of their
  age.

Proposal
--------

Name:      driver.data.age
Type:      integer, seconds
Meaning:   seconds since the driver last obtained data from the device
           (or since driver start, if it never did); updated once per
           poll loop.

Implementation (branch prepared, PR to follow after this discussion):

- drivers/dstate.c: new dstate_data_received() records the time data was
  obtained. By default dstate_dataok() also records it, so every driver
  gets an age that matches its existing dstate_dataok() behaviour without
  code changes.
- A driver that can tell more precisely calls
  dstate_data_received_explicit() once at init and then
  dstate_data_received() only when the device really delivered data.
  usbhid-ups does this: after a poll in which at least one item was read,
  and after an interrupt event that was mapped to a NUT variable.
- drivers/main.c publishes driver.data.age after each upsdrv_updateinfo().
  dstate_setinfo() only notifies upsd on change, so a healthy driver
  (age 0) causes no extra traffic.
- upsc: when listing all variables ("upsc ups", plain or -j), it prints a
  warning to stderr if driver.data.age > 3 * max(pollinterval, pollfreq),
  for example:

    Warning: UPS data is 95 s old; the driver could not get data from the
    device recently. Check the driver log and the connection to the device.

  stdout is unchanged except for the one new variable in the listing.
  Single-variable queries, -l/-L and -c are unchanged.

What deliberately does not change
---------------------------------

- dstate_dataok()/dstate_datastale(), the "Data stale" logic in upsd, and
  driver reconnect behaviour are untouched.
- upsmon does not read the new variable. This is on purpose: upsmon
  treats an on-battery UPS that stops communicating as critical and shuts
  down, so tying stale semantics to the data age could cause unexpected
  shutdowns. The variable is informational only.
- Mixed versions: a new upsc with an old driver sees no variable and prints
  no warning; an old upsc with a new driver just lists one more variable.

Questions
---------

1. Is "driver.data.age" acceptable, or would you prefer another name, or
   experimental.driver.data.age until the concept settles?
2. Relative seconds versus an absolute timestamp (e.g. ISO 8601, as
   nut-names.txt prefers for dates)? I chose relative seconds because they
   do not depend on clocks matching between the driver host and the client
   host, and they are easy to compare against a threshold.
3. Is the upsc stderr warning welcome, and is 3 * max(pollinterval,
   pollfreq) a sensible threshold?
4. Should other drivers be encouraged to opt in to precise reporting, and
   should later tools (e.g. upsmon logging, NUT-Monitor) use the value?

Testing so far: make check and make distcheck-light pass; tested on the
hardware above with usbhid-ups (libusb-1.0) on Linux, plus a fake upsd for
the upsc warning cases. Not tested: libusb-0.1, Windows, other drivers and
UPS models.

Disclosure: I prepared the analysis and the code with the help of an AI
coding assistant (Claude Code, model Claude Opus 5.5). I reviewed the
changes and take responsibility for them.

Thanks for your feedback,
Klaus Lenssen

_______________________________________________
Nut-upsdev mailing list
[email protected]
https://alioth-lists.debian.net/cgi-bin/mailman/listinfo/nut-upsdev

Reply via email to