Skip to content

Commit

Permalink
Merge branch 'master' into issue-2666
Browse files Browse the repository at this point in the history
  • Loading branch information
jimklimov authored Jan 5, 2025
2 parents 7888880 + 09ad17e commit 181d456
Show file tree
Hide file tree
Showing 9 changed files with 267 additions and 24 deletions.
9 changes: 9 additions & 0 deletions NEWS.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,10 @@ https://github.com/networkupstools/nut/milestone/11
* in `cps-hid` subdriver, `cps_fix_report_desc()` method should now handle
mismatched `LogMax` ranges for input and output voltages, whose USB Report
Descriptors are wrongly encoded by some firmware versions. [#1512]
* in `cps-hid` subdriver, try to fix frequency scaling based on the values
we see from the device and/or configuration overrides (low, nominal, high)
so `499.0 Hz` reading that comes from some firmware versions gets reported
properly as `49.9Hz`. [#2717]
* USB parameters (per `usb_communication_subdriver_t`) are now set back to
their default values during enumeration after probing each subdriver.
Having an unrelated device connected with a VID:PID matching the
Expand Down Expand Up @@ -313,6 +317,11 @@ https://github.com/networkupstools/nut/milestone/11
as "OTHER" notification type (whenever the set of such tokens appears
or changes) or "NOTOTHER" when they disappear. [#415]
- upslog:
* Added support for limiting the loop count. Using in NIT (NUT Integration
Test) suite for double profit (checking the tool and fallback in NIT).
* Added systemd and SMF service integration. [#1803]
- More systemd integration:
* Introduced a `nut-sleep.service` unit which stops `nut.target` when a
system sleep was requested, and starts it when the sleep is finished.
Expand Down
4 changes: 4 additions & 0 deletions UPGRADING.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ Changes from 2.8.2 to 2.8.3
For fallback with older systemd, a `nut-sleep.service` is provided now.
[#1070, #2596, #2597]
- Added systemd and SMF service integration for `upslog` as a `nut-logger`
service (disabled by default, needs a `upslog.conf` file to deliver the
`UPSLOG_ARGS=...` setting for actual monitoring and logging). [#1803]
- Handling of per-UPS `ALARM` state was introduced to `upsmon`, allowing it
to optionally treat it as a factor in deciding that the device is in a
"critical" state (polled more often, assumed dead if communications are
Expand Down
1 change: 1 addition & 0 deletions clients/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ upsc_SOURCES = upsc.c upsclient.h
upscmd_SOURCES = upscmd.c upsclient.h
upsrw_SOURCES = upsrw.c upsclient.h
upslog_SOURCES = upslog.c upsclient.h upslog.h
upslog_LDADD = $(LDADD_FULL)
upsmon_SOURCES = upsmon.c upsmon.h upsclient.h
upsmon_LDADD = $(LDADD_FULL)
if HAVE_WINDOWS_SOCKETS
Expand Down
52 changes: 46 additions & 6 deletions clients/upslog.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,14 @@
#include "timehead.h"
#include "nut_stdint.h"
#include "upslog.h"
#include "str.h"

#ifdef WIN32
#include "wincompat.h"
#endif

static int reopen_flag = 0, exit_flag = 0;
static size_t max_loops = 0;
static char *upsname;
static UPSCONN_t *ups;

Expand Down Expand Up @@ -78,7 +80,8 @@ static void reopen_log(void)
{
for (monhost_ups_current = monhost_ups_anchor;
monhost_ups_current != NULL;
monhost_ups_current = monhost_ups_current->next) {
monhost_ups_current = monhost_ups_current->next
) {
if (monhost_ups_current->logfile == stdout) {
upslogx(LOG_INFO, "logging to stdout");
return;
Expand Down Expand Up @@ -153,6 +156,7 @@ static void help(const char *prog)
printf(" -f <format> - Log format. See below for details.\n");
printf(" - Use -f \"<format>\" so your shell doesn't break it up.\n");
printf(" -i <interval> - Time between updates, in seconds\n");
printf(" -d <count> - Exit after specified amount of updates\n");
printf(" -l <logfile> - Log file name, or - for stdout (foreground by default)\n");
printf(" -F - stay foregrounded even if logging into a file\n");
printf(" -B - stay backgrounded even if logging to stdout\n");
Expand Down Expand Up @@ -434,7 +438,7 @@ static void run_flist(struct monhost_ups *monhost_ups_print)
int main(int argc, char **argv)
{
int interval = 30, i, foreground = -1;
size_t monhost_len = 0;
size_t monhost_len = 0, loop_count = 0;
const char *prog = xbasename(argv[0]);
time_t now, nextpoll = 0;
const char *user = NULL;
Expand All @@ -446,7 +450,7 @@ int main(int argc, char **argv)

print_banner_once(prog, 0);

while ((i = getopt(argc, argv, "+hs:l:i:f:u:Vp:FBm:")) != -1) {
while ((i = getopt(argc, argv, "+hs:l:i:d:f:u:Vp:FBm:")) != -1) {
switch(i) {
case 'h':
help(prog);
Expand Down Expand Up @@ -501,6 +505,31 @@ int main(int argc, char **argv)
interval = atoi(optarg);
break;

case 'd':
{ /* scoping */
unsigned long ul = 0;
if (str_to_ulong(optarg, &ul, 10)) {
#if (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_PUSH_POP) && ( (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TYPE_LIMITS) || (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TAUTOLOGICAL_CONSTANT_OUT_OF_RANGE_COMPARE) )
# pragma GCC diagnostic push
#endif
#ifdef HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TYPE_LIMITS
# pragma GCC diagnostic ignored "-Wtype-limits"
#endif
#ifdef HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TAUTOLOGICAL_CONSTANT_OUT_OF_RANGE_COMPARE
# pragma GCC diagnostic ignored "-Wtautological-constant-out-of-range-compare"
#endif
if (ul < SIZE_MAX)
max_loops = (size_t)ul;
else
upslogx(LOG_ERR, "Invalid max loops");
#if (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_PUSH_POP) && ( (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TYPE_LIMITS) || (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TAUTOLOGICAL_CONSTANT_OUT_OF_RANGE_COMPARE) )
# pragma GCC diagnostic pop
#endif
} else
upslogx(LOG_ERR, "Invalid max loops");
}
break;

case 'f':
logformat = optarg;
break;
Expand Down Expand Up @@ -594,7 +623,8 @@ int main(int argc, char **argv)

for (monhost_ups_current = monhost_ups_anchor;
monhost_ups_current != NULL;
monhost_ups_current = monhost_ups_current->next) {
monhost_ups_current = monhost_ups_current->next
) {
printf("logging status of %s to %s (%is intervals)\n",
monhost_ups_current->monhost, monhost_ups_current->logfn, interval);
if (upscli_splitname(monhost_ups_current->monhost, &(monhost_ups_current->upsname), &(monhost_ups_current->hostname), &(monhost_ups_current->port)) != 0) {
Expand Down Expand Up @@ -668,7 +698,8 @@ int main(int argc, char **argv)

for (monhost_ups_current = monhost_ups_anchor;
monhost_ups_current != NULL;
monhost_ups_current = monhost_ups_current->next) {
monhost_ups_current = monhost_ups_current->next
) {
ups = monhost_ups_current->ups; /* XXX Not ideal */
upsname = monhost_ups_current->upsname; /* XXX Not ideal */
/* reconnect if necessary */
Expand All @@ -683,14 +714,23 @@ int main(int argc, char **argv)
upscli_disconnect(ups);
}
}

if (max_loops > 0) {
loop_count++;
if (loop_count >= max_loops || loop_count > (SIZE_MAX - 1)) {
upslogx(LOG_INFO, "%" PRIuSIZE " loops have elapsed", max_loops);
exit_flag = 1;
}
}
}

upslogx(LOG_INFO, "Signal %d: exiting", exit_flag);
upsnotify(NOTIFY_STATE_STOPPING, "Signal %d: exiting", exit_flag);

for (monhost_ups_current = monhost_ups_anchor;
monhost_ups_current != NULL;
monhost_ups_current = monhost_ups_current->next) {
monhost_ups_current = monhost_ups_current->next
) {

if (monhost_ups_current->logfile != stdout)
fclose(monhost_ups_current->logfile);
Expand Down
5 changes: 5 additions & 0 deletions docs/man/upslog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ Wait this many seconds between polls. This defaults to 30 seconds.
If you require tighter timing, you should write your own logger using
the linkman:upsclient[3] library.

*-d* 'count'::

Exit after specified amount of updates. Default is 0 for infinite loop
(until interrupted otherwise).

*-l* 'logfile'::

Store the results in this file.
Expand Down
Loading

0 comments on commit 181d456

Please sign in to comment.