Solved Getting Network UPS Tools (NUT) emailing notifications with events

Admittedly, this is not a FreeBSD issue, but I am hoping to take advantage of the level of expertise in this forum to make this work.

I defined the NOTIFYCMD in /usr/local/etc/nut/upsmon.conf as a simple script at /usr/local/bin/mailme which reads as follows.
sh:
#!/usr/bin/env sh

echo "$*" | /usr/bin/mail -s "There is a UPS event!" myemail@example.com

I got this idea from this excellent NUT documentation. However, the next step described there is restarting upsmon before testing the whole thing by pulling UPS plug off the wall. In my understanding upsmon is a client, not a server. I did reload and restart both nut and upsd services, but I didn't get the email notification as expected by simulating a power outage event. My script is working when I execute it from the command line; however, the message text is understandably empty. I feel like I am very close and any pointers will be greatly appreciated.

I also emailed the nut-users email list with this question for full disclosure. Thanks in advance for the guidance!
 
what does /var/log/messages say? How about /var/log/maillog?

Frequently issues like this are caused by the daemon executing with an unusual environment, but this is a simple enough script that it shouldn't be a problem. The only thing weird to us (and it shouldn't matter, to reiterate) is that you're doing #!/usr/bin/env sh — why? The Bourne shell is always at /bin/sh.
 
Thank you for the quick response!
what does /var/log/messages say? How about /var/log/maillog?

I realized that some to the configuration files (/usr/local/etc/nut/upsd.conf and /usr/local/etc/nut/upsmon.conf) were owned by root and therefore could not be accessed by the user nut, which is who runs the nut service. I changed the ownership of those files to nut and reloaded plus restarted the nut service. After that those messages disappeared.

/var/log/maillog does not report anything at the time power outage simulation is done. I made sure that the script was executable by all (755) although owned by root:wheel. However, it doesn't seem to be executed at the time of the power outage simulation based on no entry in /var/log/maillog.

Frequently issues like this are caused by the daemon executing with an unusual environment, but this is a simple enough script that it shouldn't be a problem. The only thing weird to us (and it shouldn't matter, to reiterate) is that you're doing #!/usr/bin/env sh — why? The Bourne shell is always at /bin/sh.
I have superficial knowledge about my version being better than starting the script with #! bin/sh for reasons I cannot recall. However, I may absolutely be wrong.
 
I tried running the script as user nut as below:
Code:
# su -l nut /usr/local/bin/mailme
su: no directory

# cat /etc/passwd | grep nut
nut:*:316:316:Network UPS Tools user:/nonexistent:/usr/sbin/nologin
Is this the reason? It looks like the user nut perhaps doesn't have a PATH?
 
env is good for situations where the interpreter path can vary, like bash on linux living in /bin but /usr/local/bin on freebsd.

at this point we'd start nut in debug mode to see what happens when it tries to send mail.
 
env is good for situations where the interpreter path can vary, like bash on linux living in /bin but /usr/local/bin on freebsd.
Thank you, yes, that was the reasoning!
at this point we'd start nut in debug mode to see what happens when it tries to send mail.
I cannot figure out how to keep upsd in the foreground in debug mode.
upsd -D -c reload does not keep it in the foreground. I did increase the DEBUG_MIN to 5 in upsd.conf but I am not sure where it logs this.

This is how /var/log/debug.log looks like:
Code:
Jan 10 18:44:25 myhost upsd[53056]: [D2] write: [destfd=7] [len=32] [VAR CPS_LX110g3 ups.status "OL"]
Jan 10 18:44:25 myhost upsd[53056]: [D2] mainloop: polling 4 filedescriptors
These messages repeat multiple times.

And this is how it looks during the power outage simulation:
Code:
Jan 10 18:48:16 myhost syslogd: last message repeated 4 times
Jan 10 18:48:17 myhost upsd[53056]: [D2] write: [destfd=7] [len=32] [VAR CPS_LX110g3 ups.status "OL"]
Jan 10 18:48:17 myhost upsd[53056]: [D2] mainloop: polling 4 filedescriptors
Jan 10 18:48:21 myhost syslogd: last message repeated 5 times
Jan 10 18:48:22 myhost upsd[53056]: [D2] write: [destfd=7] [len=40] [VAR CPS_LX110g3 ups.status "OB DISCHRG"]
Jan 10 18:48:22 myhost upsd[53056]: [D2] mainloop: polling 4 filedescriptors
Jan 10 18:48:22 myhost syslogd: last message repeated 2 times
Jan 10 18:48:25 myhost upsd[53056]: [D2] mainloop: no data available
Jan 10 18:48:25 myhost upsd[53056]: [D2] mainloop: polling 4 filedescriptors
Jan 10 18:48:25 myhost syslogd: last message repeated 2 times
Jan 10 18:48:27 myhost upsd[53056]: [D2] mainloop: no data available
Jan 10 18:48:27 myhost upsd[53056]: [D2] mainloop: polling 4 filedescriptors
Jan 10 18:48:27 myhost syslogd: last message repeated 3 times
Jan 10 18:48:27 myhost upsd[53056]: [D2] write: [destfd=7] [len=40] [VAR CPS_LX110g3 ups.status "OB DISCHRG"]
Jan 10 18:48:27 myhost upsd[53056]: [D2] mainloop: polling 4 filedescriptors
Jan 10 18:48:29 myhost syslogd: last message repeated 2 times
Jan 10 18:48:31 myhost upsd[53056]: [D2] mainloop: no data available
Jan 10 18:48:31 myhost upsd[53056]: [D2] mainloop: polling 4 filedescriptors
Jan 10 18:48:31 myhost syslogd: last message repeated 2 times
Jan 10 18:48:32 myhost upsd[53056]: [D2] write: [destfd=7] [len=40] [VAR CPS_LX110g3 ups.status "OB DISCHRG"]
Jan 10 18:48:32 myhost upsd[53056]: [D2] mainloop: polling 4 filedescriptors
Jan 10 18:48:35 myhost syslogd: last message repeated 5 times
Jan 10 18:48:37 myhost upsd[53056]: [D2] mainloop: no data available
Jan 10 18:48:37 myhost upsd[53056]: [D2] mainloop: polling 4 filedescriptors
Jan 10 18:48:37 myhost syslogd: last message repeated 2 times
Jan 10 18:48:37 myhost upsd[53056]: [D2] write: [destfd=7] [len=40] [VAR CPS_LX110g3 ups.status "OB DISCHRG"]
Jan 10 18:48:37 myhost upsd[53056]: [D2] mainloop: polling 4 filedescriptors
Jan 10 18:48:39 myhost syslogd: last message repeated 2 times
Jan 10 18:48:41 myhost upsd[53056]: [D2] mainloop: no data available
Jan 10 18:48:41 myhost upsd[53056]: [D2] mainloop: polling 4 filedescriptors
Jan 10 18:48:41 myhost syslogd: last message repeated 7 times
Jan 10 18:48:42 myhost upsd[53056]: [D2] write: [destfd=7] [len=40] [VAR CPS_LX110g3 ups.status "OB DISCHRG"]
Jan 10 18:48:42 myhost upsd[53056]: [D2] mainloop: polling 4 filedescriptors
Jan 10 18:48:43 myhost syslogd: last message repeated 2 times
Jan 10 18:48:45 myhost upsd[53056]: [D2] mainloop: no data available
Jan 10 18:48:45 myhost upsd[53056]: [D2] mainloop: polling 4 filedescriptors
Jan 10 18:48:47 myhost syslogd: last message repeated 4 times
Jan 10 18:48:47 myhost upsd[53056]: [D2] write: [destfd=7] [len=40] [VAR CPS_LX110g3 ups.status "OL DISCHRG"]
Jan 10 18:48:47 myhost upsd[53056]: [D2] mainloop: polling 4 filedescriptors
Jan 10 18:48:49 myhost syslogd: last message repeated 2 times
Jan 10 18:48:51 myhost upsd[53056]: [D2] mainloop: no data available
Jan 10 18:48:51 myhost upsd[53056]: [D2] mainloop: polling 4 filedescriptors
Jan 10 18:48:51 myhost syslogd: last message repeated 2 times
Jan 10 18:48:53 myhost upsd[53056]: [D2] write: [destfd=7] [len=32] [VAR CPS_LX110g3 ups.status "OL"]
Jan 10 18:48:53 myhost upsd[53056]: [D2] mainloop: polling 4 filedescriptors
 
I got it! My script was actually located in /root/scripts/mailme.sh with permissions 755 and the /usr/local/bin/mailme was a symlink to that. I believe this wasn't working because the /root directory has permissions 750. I unlinked the symlink and copied my script under /usr/local/bin/mailme and I can now get emails from my UPS, at least for the "on battery" and "online" events that I tested. Thank you for your help atax1a !
 
Back
Top