Bug 265963 - sysutils/nut: add possibility to log status of several UPSes
Summary: sysutils/nut: add possibility to log status of several UPSes
Status: Closed FIXED
Alias: None
Product: Ports & Packages
Classification: Unclassified
Component: Individual Port(s) (show other bugs)
Version: Latest
Hardware: Any Any
: --- Affects Only Me
Assignee: Cy Schubert
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2022-08-20 21:20 UTC by Vladimir Druzenko
Modified: 2022-09-02 21:36 UTC (History)
0 users

See Also:
bugzilla: maintainer-feedback? (cy)


Attachments
add possibility to log status of several UPSes (1.93 KB, patch)
2022-08-20 21:20 UTC, Vladimir Druzenko
vvd: maintainer-approval?
Details | Diff
The concept I had in mind (3.07 KB, patch)
2022-08-21 05:10 UTC, Cy Schubert
no flags Details | Diff
Final patch (9.15 KB, patch)
2022-08-21 15:33 UTC, Cy Schubert
no flags Details | Diff
Added child process management (10.67 KB, patch)
2022-08-21 17:26 UTC, Cy Schubert
no flags Details | Diff
Add script as a sample (15.23 KB, patch)
2022-08-21 18:29 UTC, Cy Schubert
no flags Details | Diff
Makefile patch included (16.08 KB, patch)
2022-08-21 18:31 UTC, Cy Schubert
no flags Details | Diff
Part one of a two part patch (13.37 KB, patch)
2022-08-23 04:17 UTC, Cy Schubert
no flags Details | Diff
This is part two of two (6.69 KB, patch)
2022-08-23 04:18 UTC, Cy Schubert
no flags Details | Diff
Final patch (25.48 KB, patch)
2022-08-26 04:44 UTC, Cy Schubert
no flags Details | Diff
Removed a tautological check for child (25.32 KB, patch)
2022-08-26 14:09 UTC, Cy Schubert
no flags Details | Diff
Upstream cleaned up usage() (25.31 KB, patch)
2022-08-26 14:44 UTC, Cy Schubert
no flags Details | Diff
Fix brain fart (25.51 KB, patch)
2022-08-26 16:31 UTC, Cy Schubert
no flags Details | Diff
Debugging patch (5.40 KB, patch)
2022-08-29 05:47 UTC, Cy Schubert
no flags Details | Diff
Debugging patch with a sleep (7.57 KB, patch)
2022-08-29 14:53 UTC, Cy Schubert
no flags Details | Diff
Query multiple UPSs through a single TCP connection using a sigle daemon (21.39 KB, patch)
2022-08-30 05:38 UTC, Cy Schubert
no flags Details | Diff
Same patch, with man page (24.04 KB, patch)
2022-08-30 13:13 UTC, Cy Schubert
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Vladimir Druzenko freebsd_committer freebsd_triage 2022-08-20 21:20:49 UTC
Created attachment 236026 [details]
add possibility to log status of several UPSes

It isn't possible to log status of several UPSes on one host now.
nut_upslog can get only one UPS.

This patch add such possibility.

If you want to log several different UPSes:
ln -s nut_upslog /usr/local/etc/rc.d/NAME
then set variables NAME_enable, NAME_ups, NAME_pidbase, NAME_logfile and etc.
Comment 1 Cy Schubert freebsd_committer freebsd_triage 2022-08-20 22:56:52 UTC
This won't work. upslog writes its pid to upslog.pid. Multiple upslog daemons will overwrite each other's pid files causing rc to fail stopping upslog at shutdown.

And, the proposed solution suboptimal.

The path forward would be to have the nut project alter upslog. upslog should monitor multiple UPSs without additional scripting. I can put this on my todo list but there are four other higher priority problems ahead of this one.
Comment 2 Vladimir Druzenko freebsd_committer freebsd_triage 2022-08-20 23:27:15 UTC
(In reply to Cy Schubert from comment #1)
> This won't work. upslog writes its pid to upslog.pid.
> Multiple upslog daemons will overwrite each other's pid
> files causing rc to fail stopping upslog at shutdown.
I solved this in patch.
Check option "-p ${_pidbase}".
Comment 3 Cy Schubert freebsd_committer freebsd_triage 2022-08-21 05:10:15 UTC
Created attachment 236030 [details]
The concept I had in mind

Here's the concept I had in mind.

A new -m option which contains a colon separated list

ups@host:logfn:pidbase

For example upslog -m cps@cwfw:/var/log/nut/cps.log:/var/run/nut/upslog-cps.pid -m foobar@cwfw:/var/log/foobar.log:/var/run/nut/upslog-foobar.pid

I haven't made any changes to the rc script yet; this is something I cobbled up half an hour ago.

The new -m flag forks off two processes for the specified UPS each with its own logfn and pidbase.
Comment 4 Cy Schubert freebsd_committer freebsd_triage 2022-08-21 15:33:15 UTC
Created attachment 236045 [details]
Final patch

This will require a little extra documentation.

I will import this patch into my "prod" ports tree and run it on my ups server. Unfortunately I don't have multiple UPSs so my testing will be limited. Let me know what you think.
Comment 5 Cy Schubert freebsd_committer freebsd_triage 2022-08-21 17:26:26 UTC
Created attachment 236049 [details]
Added child process management

Hopefully this is the final final version.

The parent process writes its PID to the pidfile in -p. Killing the parent results in all children terminating as well. SIGINT and SIGTERM are caught.
Comment 6 Vladimir Druzenko freebsd_committer freebsd_triage 2022-08-21 17:46:20 UTC
With your patch we can't add logging new UPS without stop logging all other UPSes.
Comment 7 Cy Schubert freebsd_committer freebsd_triage 2022-08-21 18:01:55 UTC
(In reply to VVD from comment #6)
One can still run upslog -m with only one argument.

Your patch addresses the issue that the script as it is still only handles one UPS. That is an edge case and overly complicates the script. I'm not willing to complicate the script for one edge case.

If you wish we can add an option to create a new script that is installed as a sample that users can copy from /usr/local/share/examples to rc.d. That is certainly possible. But to modify the actual RC script so that it can be copied is not a good idea. Would you like to do that instead?
Comment 8 Cy Schubert freebsd_committer freebsd_triage 2022-08-21 18:29:04 UTC
Created attachment 236051 [details]
Add script as a sample

This new patch adds your script as a sample people can use to either add or replace the existing script in rc.d.

Having said that the problem with your script is it's a) complicated for the end user; there will be PRs asking how to use it and b) PRs asking to simplify it, and I will not be able to defend this decision (though you are certainly more qualified to defend the decision than I).

I suppose there is not much difference between copying the script from rc.d to rc.d than copying it from examples/nut to rc.d but it will reduce the number of PRs that will certainly arrive to reduce its complexity. Someone will undoubtedly submit a patch to reduce its complexity. Keeping the script in samples avoids such future technical debt.
Comment 9 Cy Schubert freebsd_committer freebsd_triage 2022-08-21 18:31:10 UTC
Created attachment 236052 [details]
Makefile patch included

I've already spent too much time on this forgetting to save the Makfile patches. Here they are.
Comment 10 Cy Schubert freebsd_committer freebsd_triage 2022-08-23 04:15:27 UTC
Upstream have conditionally accepted my patch. However, nut has to also support Windows and Windows doesn't support signals nor does it support POSIX processes. They may either incorporate my patch or opt to use select() to read from multiple UPSs. This is more work but a better approach than a single process per worker thread.
Comment 11 Cy Schubert freebsd_committer freebsd_triage 2022-08-23 04:17:02 UTC
Created attachment 236072 [details]
Part one of a two part patch

The patch is split into two commits. This patch implements the multi-process solution to this PR.
Comment 12 Cy Schubert freebsd_committer freebsd_triage 2022-08-23 04:18:42 UTC
Created attachment 236073 [details]
This is part two of two

This is the second of two patches. It implements the sample rc script.

The first patch must be applied first or this patch will fail to apply.
Comment 13 Cy Schubert freebsd_committer freebsd_triage 2022-08-23 04:29:24 UTC
We can apply these two patches for now or wait for our upstream to either accept my patch or implement the select() approach. The select() approach to solving this problem will also work under Windows. My patch resolves this problem on POSIX systems only.

The second of two patches is a non-portable workaround requiring greater end-user effort to implement.

However, it is suggested we wait for upstream developers to provide a permanent cross-platform solution. If you prefer not to wait the two patches can be committed in the interim.
Comment 14 Cy Schubert freebsd_committer freebsd_triage 2022-08-24 19:02:53 UTC
The nut upstream have produced a much improved version of my patch. I will test it here first.

Unfortunately it will only be in nut-devel. I am testing both their full patchi in nut-devel and a backported version in nut. I will post the complete proposed commit here first.

Do you still want the sample rc script in the port?
Comment 15 Cy Schubert freebsd_committer freebsd_triage 2022-08-26 04:44:56 UTC
Created attachment 236121 [details]
Final patch

This should be the final patch. The initial patch was written by me, submitted as a pull request to our upstream, updated by our upstream, and updated by me. I will submit the final updates before this weekend.

They have accepted the patch, pending these last updates, for the next release.

The patch, git formatted commit email, outlines how to use the patch.

    An rc.conf exemple of how to use this new feature:
    nut_upslog_flags="-p /var/db/nut/upslog.pid -m ups1@localhost,/var/log/nut\
    /ups1.log,/var/db/nut/ups1.pid -m ups1@localhost,/var/log/nut/ups2.log,/var\
    /db/nut/ups2.pid"
    
    Sending a TERM signal to the parent PID, kill `cat /var/db/nut/upslog.pid`,
    will terminate the entire process tree.

If a person wishes to run an ad-hoc upslog one can update rc.conf and restart nut_upslog or update rc.conf and run an ad-hoc command in the interim. The suggested shell script remains in /usr/local/share/examples/nut for those who prefer to implement using individual shell scripts.
Comment 16 Cy Schubert freebsd_committer freebsd_triage 2022-08-26 14:09:04 UTC
Created attachment 236127 [details]
Removed a tautological check for child

Removed a tautological check for child process. This has been pushed upstream.
Comment 17 Cy Schubert freebsd_committer freebsd_triage 2022-08-26 14:44:30 UTC
Created attachment 236128 [details]
Upstream cleaned up usage()

Hopefully this represents what the nut upstream will finally merge into their mainline. They applied to minor whitespace fixes which have been included here. The patch will remain in the nut port (2.8.0) until it is updated to 2.?.?. The patch will be removed when nut-devel after the enhancement is published.
Comment 18 Cy Schubert freebsd_committer freebsd_triage 2022-08-26 16:31:32 UTC
Created attachment 236132 [details]
Fix brain fart

I had removed some code that would run on the parent only. pointy hat to me.
Comment 19 commit-hook freebsd_committer freebsd_triage 2022-08-28 21:20:17 UTC
A commit in branch main references this bug:

URL: https://cgit.FreeBSD.org/ports/commit/?id=0b5e039674aa1f529e2ebc67da11bdadcb4db6e6

commit 0b5e039674aa1f529e2ebc67da11bdadcb4db6e6
Author:     Cy Schubert <cy@FreeBSD.org>
AuthorDate: 2022-08-21 15:21:51 +0000
Commit:     Cy Schubert <cy@FreeBSD.org>
CommitDate: 2022-08-28 21:16:40 +0000

    sysutils/nut*: Alternate upslog support for multiple UPSs

    Add  sample rc script to allow a user to copy a sample rc script to
    rc.d, allowing the user to use multiple instances of upslog(8) with
    multiple instances of the rc script to log multiple UPSs on the same
    system.

    The sample rc script was provided by vvd@unislabs.com.

    PR:             265963
    Reported by:    vvd@unislabs.com
    Submitted by:   vvd@unislabs.com

 sysutils/nut-devel/Makefile                      |  3 +-
 sysutils/nut-devel/files/nut_upslog.sample (new) | 52 ++++++++++++++++++++++++
 sysutils/nut-devel/pkg-plist                     |  1 +
 sysutils/nut/Makefile                            |  3 +-
 sysutils/nut/files/nut_upslog.sample (new)       | 52 ++++++++++++++++++++++++
 sysutils/nut/pkg-plist                           |  1 +
 6 files changed, 110 insertions(+), 2 deletions(-)
Comment 20 commit-hook freebsd_committer freebsd_triage 2022-08-28 21:20:18 UTC
A commit in branch main references this bug:

URL: https://cgit.FreeBSD.org/ports/commit/?id=dec4ecc3afecab035a4d4fa59c35093d9f53ffd5

commit dec4ecc3afecab035a4d4fa59c35093d9f53ffd5
Author:     Cy Schubert <cy@FreeBSD.org>
AuthorDate: 2022-08-21 15:21:51 +0000
Commit:     Cy Schubert <cy@FreeBSD.org>
CommitDate: 2022-08-28 21:16:40 +0000

    sysutils/nut*: Add upslog support for multiple UPSs

    upslog is a utility that logs UPS status at regular intervals, specified
    by the -i option. Unfortunately upslog supports only on UPS. For sites
    that need to monitor multiple UPSs the options are to cobble an rc script
    for each or doctor up the nut_upslog.in script to support cloning of the
    script. Unfortunately an rc script capable of being cloned would become
    the source of more PRs and would require significanly more tehcnical
    documentation that by itself might become confusing for the average
    system administrator.

    Therefore a new -m option is added to support multiple UPSs using the
    same invocation of upslog. The patch parses a -m option and forks
    almost immediately following the getopt(3) invokation to monitor each
    individual UPS using a separate upslog process. This is not ideal but
    better than scripting. Lightweight threads might be a better solution.

    An rc.conf exemple of how to use this new feature:
    nut_upslog_flags="-p /var/db/nut/upslog.pid -m ups1@localhost,/var/log/nut\
    /ups1.log,/var/db/nut/ups1.pid -m ups1@localhost,/var/log/nut/ups2.log,/var\
    /db/nut/ups2.pid"

    Sending a TERM signal to the parent PID, kill `cat /var/db/nut/upslog.pid`,
    will terminate the entire process tree.

    This patch submitted upstream, is in the process of being refined and
    accepted.

    PR:             265963
    Reported by:    vvd@unislabs.com

 sysutils/nut-devel/Makefile                        |   1 +
 .../nut-devel/files/patch-clients_upslog.c (new)   | 167 +++++++++++++++++++++
 .../files/patch-docs_man_upslog.txt (new)          |  16 ++
 sysutils/nut/Makefile                              |   2 +-
 sysutils/nut/files/patch-clients_upslog.c (new)    | 166 ++++++++++++++++++++
 sysutils/nut/files/patch-docs_man_upslog.8 (new)   |  16 ++
 .../nut/files/patch-include_nut__stdint.h (new)    |  31 ++++
 7 files changed, 398 insertions(+), 1 deletion(-)
Comment 21 Cy Schubert freebsd_committer freebsd_triage 2022-08-28 21:25:14 UTC
Fixed.

Upstream has accepted the upslog.c and man page patches,
Comment 22 Vladimir Druzenko freebsd_committer freebsd_triage 2022-08-29 01:38:11 UTC
Process for 2nd UPS exit just after start.

nut_upslog_flags="-p /var/db/nut/upslog.pid -m UPS2@localhost,/var/log/nut/ups2.log,/var/db/nut/ups2.pid -m UPS1@localhost,/var/log/nut/ups1.log,/var/db/nut/ups1.pid"

With this order it write logs for UPS2 only. If change order - for UPS1.

One more:
# ls -l /var/db/nut/ups[12].pid
-rw-r--r--  1 uucp  uucp  6 29 авг.  04:29 /var/db/nut/ups1.pid
-rw-r--r--  1 root  uucp  6 29 авг.  04:29 /var/db/nut/ups2.pid

Before start both files was removed.
ups2.pid owner is root, but ups1.pid is uucp.
1st -m option for UPS2, 2nd -m for UPS1 - after change order owners changed too.

P.S. Sorry, was very busy.
Comment 23 Cy Schubert freebsd_committer freebsd_triage 2022-08-29 04:25:27 UTC
I'm not able to reproduce that problem here.

cwfw# lh /var/db/nut
total 12
-rw-r--r--  1 uucp  uucp      6B Aug 28 21:15 cps.pid
-rw-r--r--  1 uucp  uucp      6B Aug 28 21:15 cps2.pid
-rw-r--r--  1 uucp  uucp      5B Aug 28 06:16 upsd.pid
-rw-r--r--  1 uucp  uucp      6B Aug 28 21:15 upslog.pid
-rw-r--r--  1 root  wheel     5B Aug 28 06:15 upsmon.pid
srw-rw----  1 uucp  uucp      0B Aug 28 06:16 usbhid-ups-cps
-rw-r--r--  1 uucp  uucp      5B Aug 28 06:16 usbhid-ups-cps.pid
cwfw# 

Can you provide relevant messages(5) output.

Also provide psauxww output after it has started. You should see output similar to this:

uucp     2202   0.0  0.1  16876   4484 v0- S    06:15     0:00.54 /usr/local/bin/upslog -p /var/db/nut/upslog.pid -m cps@localhost,/var/log/nut/cps.log,/var/db/nut/cps.pid
uucp    12847   0.0  0.1  16876   5728  1  I    21:18     0:00.00 /usr/local/bin/upslog -p /var/db/nut/upslog.pid -m cps@localhost,/var/log/nut/cps.log,/var/db/nut/cps.pid -m cps@cwfw,/var/log/nut/cps2.log,/var/db/nut/cps2.pid
uucp    12848   0.0  0.1  17012   6092  1  I    21:18     0:00.01 /usr/local/bin/upslog -p /var/db/nut/upslog.pid -m cps@localhost,/var/log/nut/cps.log,/var/db/nut/cps.pid -m cps@cwfw,/var/log/nut/cps2.log,/var/db/nut/cps2.pid
Comment 24 Cy Schubert freebsd_committer freebsd_triage 2022-08-29 04:53:09 UTC
Can you also tell me which processes are connected to TCP port 3493.
Comment 25 Cy Schubert freebsd_committer freebsd_triage 2022-08-29 05:47:36 UTC
Created attachment 236194 [details]
Debugging patch

Please apply this debugging patch and rebuild nut, Then run with -D -D -D options and report syslog and output.

I have a hunch it may be connecting to the upsd socket too quickly and upsd does not have enough time to respond. May require a sleep of a second or two between connection attempts. Let's run this first just to make sure.
Comment 26 Cy Schubert freebsd_committer freebsd_triage 2022-08-29 05:48:37 UTC
Keep this open while we determine what this next problem is.
Comment 27 Cy Schubert freebsd_committer freebsd_triage 2022-08-29 14:53:49 UTC
Created attachment 236219 [details]
Debugging patch with a sleep

This patch adds a sleep to avoid overwhelming upsd. Try this, please.
Comment 28 Cy Schubert freebsd_committer freebsd_triage 2022-08-29 15:17:07 UTC
Let me know as soon as you can. I prefer you work with rather than implement the shell scripts. Our upstream would like some feedback as to how this doesn't work. They're counting on you.
Comment 29 Vladimir Druzenko freebsd_committer freebsd_triage 2022-08-30 00:08:12 UTC
(In reply to Cy Schubert from comment #23)
# grep nut_upslog_flags /etc/rc.conf
nut_upslog_flags="-p /var/db/nut/upslog.pid -m UPS1@localhost,/var/log/nut/ups1.log,/var/db/nut/ups1.pid -m UPS2@localhost,/var/log/nut/ups2.log,/var/db/nut/ups2.pid"

# service nut_upslog start
Starting nut_upslog.
Network UPS Tools upslog 2.8.0
logging status of UPS1@localhost to /var/log/nut/ups1.log (30s intervals)

# ps axuww | grep upslog
uucp       25629   0.0  0.0   16824    5184  0  I    03:04        0:00.00 /usr/local/bin/upslog -p /var/db/nut/upslog.pid -m UPS1@localhost,/var/log/nut/ups1.log,/var/db/nut/ups1.pid -m UPS2@localhost,/var/log/nut/ups2.log,/var/db/nut/ups2.pid
uucp       25630   0.0  0.0   16960    5516  0  S    03:04        0:00.00 /usr/local/bin/upslog -p /var/db/nut/upslog.pid -m UPS1@localhost,/var/log/nut/ups1.log,/var/db/nut/ups1.pid -m UPS2@localhost,/var/log/nut/ups2.log,/var/db/nut/ups2.pid

ls -l /var/db/nut/
total 5
srw-rw----  1 uucp  uucp  0 Aug 29 04:17 snmp-ups-UPS2
-rw-r--r--  1 uucp  uucp  6 Aug 29 04:17 snmp-ups-UPS2.pid
-rw-r--r--  1 root  uucp  6 Aug 30 03:04 ups1.pid
-rw-r--r--  1 uucp  uucp  6 Aug 30 03:04 ups2.pid
-rw-r--r--  1 uucp  uucp  6 Aug 29 04:17 upsd.pid
-rw-r--r--  1 uucp  uucp  6 Aug 30 03:04 upslog.pid
-rw-r--r--  1 root  uucp  6 Aug 29 04:17 upsmon.pid
srw-rw----  1 uucp  uucp  0 Aug 29 04:17 usbhid-ups-UPS1
-rw-r--r--  1 uucp  uucp  6 Aug 29 04:17 usbhid-ups-UPS1.pid

# ls -l /var/log/nut/
total 140
-rw-r--r--  1 uucp  uucp  211742 Aug 30 03:07 ups1.log
-rw-r--r--  1 uucp  uucp  307574 Aug 30 02:58 ups2.log
Comment 30 Vladimir Druzenko freebsd_committer freebsd_triage 2022-08-30 00:09:50 UTC
(In reply to Cy Schubert from comment #24)
> Can you also tell me which processes are connected to TCP port 3493.
What do you mean?
lsof? netstat?
Process listen 3493?
Comment 31 Vladimir Druzenko freebsd_committer freebsd_triage 2022-08-30 00:20:34 UTC
# service nut_upslog start
Starting nut_upslog.
Network UPS Tools upslog 2.8.0
   0.000000     [D1] Initial parent terminated
   0.000000     [D1] Forking to log 2 devices
   0.000229     [D1] Forking to device
logging status of UPS1@localhost to /var/log/nut/ups1.log (30s intervals)
   0.002995     [D1] Saving PID 46719 into /var/db/nut/ups1.pid
   2.008944     [D1] Forking to device
   2.008993     [D1] Waiting for children
   2.009564     [D1] Saving PID 46720 into /var/db/nut/ups2.pid
   2.009623     [D1] Wait complete
   4.009759     [D1] Waiting for children
   4.010346     [D1] Saving PID 46718 into /var/db/nut/upslog.pid

# ps axuww | grep ups
uucp       46718   0,0  0,0   16824    5184  0  I    03:17             0:00,00 /usr/local/bin/upslog -DDDDDD -p /var/db/nut/upslog.pid -m UPS1@localhost,/var/log/nut/ups1.log,/var/db/nut/ups1.pid -m UPS2@localhost,/var/log/nut/ups2.log,/var/db/nut/ups2.pid
uucp       46719   0,0  0,0   16960    5516  0  S    03:17             0:00,00 /usr/local/bin/upslog -DDDDDD -p /var/db/nut/upslog.pid -m UPS1@localhost,/var/log/nut/ups1.log,/var/db/nut/ups1.pid -m UPS2@localhost,/var/log/nut/ups2.log,/var/db/nut/ups2.pid

# ls -l
total 5
srw-rw----  1 uucp  uucp  0 Aug 29 04:17 snmp-ups-UPS2
-rw-r--r--  1 uucp  uucp  6 Aug 29 04:17 snmp-ups-UPS2.pid
-rw-r--r--  1 root  uucp  6 Aug 30 03:17 ups1.pid
-rw-r--r--  1 uucp  uucp  6 Aug 30 03:17 ups2.pid
-rw-r--r--  1 uucp  uucp  6 Aug 29 04:17 upsd.pid
-rw-r--r--  1 uucp  uucp  6 Aug 30 03:17 upslog.pid
-rw-r--r--  1 root  uucp  6 Aug 29 04:17 upsmon.pid
srw-rw----  1 uucp  uucp  0 Aug 29 04:17 usbhid-ups-UPS1
-rw-r--r--  1 uucp  uucp  6 Aug 29 04:17 usbhid-ups-UPS1.pid
Comment 32 Cy Schubert freebsd_committer freebsd_triage 2022-08-30 00:39:50 UTC
(In reply to VVD from comment #31)

Yes, what is listening on that port? Can you telnet to it while upslog is connected to it?

There's a bug in upslog where it fails to send anything to syslog.

Next, I will give you a patch, which is partially complete, which will fix the logging to syslog in upslog, and a patch to enable debugging. It's calling upsdebug() but no debugging output is printed. The other daemons have a -D option that increments a debug flag while upslog has no such facility, yet it calls upsdebug().

I'm beginning to think that multiple processes may be the wrong solution to this problem but instead using a large loop. which it already does. Let me take this away and work on this other approach. It will use fewer resources too.

This may take a little time as I am juggling this PR with an other for the RTL8192EU USB WiFi dongle that fails to associate, which does affect many many people. I'll try to time share between these after $JOB. This problem looks like it's going into the rabbit hole.
Comment 33 Vladimir Druzenko freebsd_committer freebsd_triage 2022-08-30 00:57:33 UTC
(In reply to Cy Schubert from comment #32)
# grep 3493 /etc/services 
nut             3493/tcp   #Network UPS Tools
nut             3493/udp   #Network UPS Tools
# lsof | grep upsd | grep LISTEN
upsd      97852       uucp    4u    IPv4 0xfffffe01c3633950                0t0  TCP *:nut (LISTEN)

BTW, my UPS1 is connected via USB and UPS2 via Ethernet (SNMP driver).
Comment 34 Cy Schubert freebsd_committer freebsd_triage 2022-08-30 01:07:28 UTC
(In reply to VVD from comment #33)

This is not exactly what we're looking for. grep for LISTEN only gives us half the answer. The other, more important, half of the answer is what the clients are. Can you do the same without the LISTEN. The original ask was for all, not just LISTEN.
Comment 35 commit-hook freebsd_committer freebsd_triage 2022-08-30 01:11:53 UTC
A commit in branch main references this bug:

URL: https://cgit.FreeBSD.org/ports/commit/?id=dc2f6b92465447b81470fe2c56963053e04039fd

commit dc2f6b92465447b81470fe2c56963053e04039fd
Author:     Cy Schubert <cy@FreeBSD.org>
AuthorDate: 2022-08-30 01:08:02 +0000
Commit:     Cy Schubert <cy@FreeBSD.org>
CommitDate: 2022-08-30 01:11:05 +0000

    Revert "sysutils/nut*: Add upslog support for multiple UPSs"

    upslog is failing to make multiple connections to upsd resulting
    in some UPSs logging and others not. This is likely some bug
    in upsd's socket handling.

    Even though this patch worked for me locally it did not for the
    person reporting the problem.

    A better approach would be to use select() instead.

    PR:     265963

    This reverts commit dec4ecc3afecab035a4d4fa59c35093d9f53ffd5.

 sysutils/nut-devel/Makefile                        |   2 +-
 .../nut-devel/files/patch-clients_upslog.c (gone)  | 167 ---------------------
 .../files/patch-docs_man_upslog.txt (gone)         |  16 --
 sysutils/nut/Makefile                              |   2 +-
 sysutils/nut/files/patch-clients_upslog.c (gone)   | 166 --------------------
 sysutils/nut/files/patch-docs_man_upslog.8 (gone)  |  16 --
 .../nut/files/patch-include_nut__stdint.h (gone)   |  31 ----
 7 files changed, 2 insertions(+), 398 deletions(-)
Comment 36 Vladimir Druzenko freebsd_committer freebsd_triage 2022-08-30 01:14:27 UTC
(In reply to commit-hook from comment #35)
# lsof -n | grep :nut
upslog    46719       uucp    3u    IPv4 0xfffffe01c2282438                0t0  TCP 127.0.0.1:33064->127.0.0.1:nut (ESTABLISHED)
upsd      97852       uucp    4u    IPv4 0xfffffe01c3633950                0t0  TCP *:nut (LISTEN)
upsd      97852       uucp    7u    IPv4 0xfffffe0180226000                0t0  TCP 10.0.0.103:nut->10.0.0.103:24690 (ESTABLISHED)
upsd      97852       uucp    8u    IPv4 0xfffffe017ea4f950                0t0  TCP 127.0.0.1:nut->127.0.0.1:33064 (ESTABLISHED)
upsd      97852       uucp    9u    IPv4 0xfffffe01a0690ca8                0t0  TCP 10.0.0.103:nut->10.0.0.102:25816 (ESTABLISHED)
upsd      97852       uucp   10u    IPv4 0xfffffe017afdd000                0t0  TCP 10.0.0.103:nut->10.0.0.101:12693 (ESTABLISHED)
upsd      97852       uucp   11u    IPv4 0xfffffe01a06910e0                0t0  TCP 10.0.0.103:nut->10.0.0.4:57733 (ESTABLISHED)
upsd      97852       uucp   12u    IPv4 0xfffffe017ea4e000                0t0  TCP 10.0.0.103:nut->10.0.0.3:16698 (ESTABLISHED)
upsd      97852       uucp   13u    IPv4 0xfffffe0180196438                0t0  TCP 10.0.0.103:nut->10.0.0.105:50933 (ESTABLISHED)
upsd      97852       uucp   14u    IPv4 0xfffffe017127c000                0t0  TCP 10.0.0.103:nut->10.0.0.10:29182 (ESTABLISHED)
upsd      97852       uucp   15u    IPv4 0xfffffe0180197518                0t0  TCP 10.0.0.103:nut->10.0.0.104:12549 (ESTABLISHED)
upsd      97852       uucp   16u    IPv4 0xfffffe016f500518                0t0  TCP 10.0.0.103:nut->10.0.0.121:15447 (ESTABLISHED)
upsd      97852       uucp   17u    IPv4 0xfffffe0180224000                0t0  TCP 10.0.0.103:nut->10.0.0.123:48324 (ESTABLISHED)
upsmon    97876       uucp    4u    IPv4 0xfffffe016e425950                0t0  TCP 10.0.0.103:24690->10.0.0.103:nut (ESTABLISHED)

This?
Or can you write exact command?
Comment 37 Cy Schubert freebsd_committer freebsd_triage 2022-08-30 01:23:55 UTC
(In reply to VVD from comment #36)

This is good. Unlike mine there is only one upslog client connected to upsd.

I think the path forward is to use a single daemon which does select().
Comment 38 Cy Schubert freebsd_committer freebsd_triage 2022-08-30 05:38:12 UTC
Created attachment 236238 [details]
Query multiple UPSs through a single TCP connection using a sigle daemon

This patch performs the same function as the previous but using a single upslog process communicating with upsd through a single TCP socket,

Please test.

The -m argument has changed. There is no longer a pid requirement. It is now,
-m myups1@myhost1:port,/var/log/nut/foobar1 -m myups2@myhost2:port,/var/log/nut/foobar2.

The :port is optional and defaults to 3493.
Comment 39 Cy Schubert freebsd_committer freebsd_triage 2022-08-30 13:13:09 UTC
Created attachment 236243 [details]
Same patch, with man page

Same patch with man page.
Comment 40 Vladimir Druzenko freebsd_committer freebsd_triage 2022-08-30 13:37:13 UTC
(In reply to Cy Schubert from comment #38)
It work now as expected.
But I need to add -i to nut_upslog_flags. Default in upslog is 30 secs.
Comment 41 commit-hook freebsd_committer freebsd_triage 2022-08-30 18:12:24 UTC
A commit in branch main references this bug:

URL: https://cgit.FreeBSD.org/ports/commit/?id=9c4a120c36bfb61a307d04c843eeda4f3c43cf4c

commit 9c4a120c36bfb61a307d04c843eeda4f3c43cf4c
Author:     Cy Schubert <cy@FreeBSD.org>
AuthorDate: 2022-08-30 05:27:41 +0000
Commit:     Cy Schubert <cy@FreeBSD.org>
CommitDate: 2022-08-30 18:04:26 +0000

    sysutils/nut*: Add upslog support for multiple UPSs

    upslog is a utility that logs UPS status at regular intervals, specified
    by the -i option. Unfortunately upslog supports only on UPS. For sites
    that need to monitor multiple UPSs the options are to cobble an rc script
    for each or doctor up the nut_upslog.in script to support cloning of the
    script. Unfortunately an rc script capable of being cloned would become
    the source of more PRs and would require significanly more tehcnical
    documentation that by itself might become confusing for the average
    system administrator.

    Therefore a new -m option is added to support multiple UPSs using the
    same invocation of upslog. The patch parses a new -m option and using a
    single connection to upsd, queries it for stats about multiple UPSs.

    An rc.conf exemple of how to use this new feature:
    nut_upslog_flags="-p /var/db/nut/upslog.pid -m ups1@localhost,/var/log/nut\
    /ups1.log -m ups2@localhost,/var/log/nut/ups2.log"

    PR:             265963
    Reported by:    vvd@unislabs.com

 sysutils/nut-devel/Makefile                        |   2 +-
 .../nut-devel/files/patch-clients_upslog.c (new)   | 291 +++++++++++++++++++++
 .../files/patch-docs_man_upslog.txt (new)          |  14 +
 sysutils/nut/Makefile                              |   2 +-
 sysutils/nut/files/patch-clients_upslog.c (new)    | 291 +++++++++++++++++++++
 sysutils/nut/files/patch-docs_man_upslog.8 (new)   |  16 ++
 sysutils/nut/files/patch-docs_man_upslog.txt (new) |  14 +
 7 files changed, 628 insertions(+), 2 deletions(-)