Linefeed in pfSense email notifications (with Network UPS Tools)
-
I am using Network UPS Tools with my pfSense router to monitor my UPS. The settings can be found at Services / UPS / Settings / UPS Settings. In the "Advanced Settings" there is a field for "Additional configuration lines for upsmon.conf" where I have set the following:
NOTIFYMSG ONLINE "UPS %s on line power" NOTIFYMSG ONBATT "UPS %s on battery" NOTIFYMSG LOWBATT "UPS %s battery is low" NOTIFYMSG FSD "UPS %s: forced shutdown in progress" NOTIFYMSG COMMOK "Communications with UPS %s established" NOTIFYMSG COMMBAD "Communications with UPS %s lost" NOTIFYMSG SHUTDOWN "Auto logout and shutdown proceeding" NOTIFYMSG REPLBATT "UPS %s battery needs to be replaced" NOTIFYMSG NOCOMM "UPS %s is unavailable" NOTIFYMSG NOPARENT "upsmon parent process died - shutdown impossible" NOTIFYFLAG ONLINE SYSLOG+WALL+EXEC NOTIFYFLAG ONBATT SYSLOG+WALL+EXEC NOTIFYFLAG LOWBATT SYSLOG+WALL+EXEC NOTIFYFLAG FSD SYSLOG+WALL+EXEC NOTIFYFLAG COMMOK SYSLOG+WALL+EXEC NOTIFYFLAG COMMBAD SYSLOG+WALL+EXEC NOTIFYFLAG SHUTDOWN SYSLOG+WALL+EXEC NOTIFYFLAG REPLBATT SYSLOG+WALL+EXEC NOTIFYFLAG NOCOMM SYSLOG+WALL+EXEC NOTIFYFLAG NOPARENT SYSLOG+WALL+EXEC
If I hit the "Save" button on the bottom of the page, it will temporarily disconnect the UPS and result in an email with the body:
Notifications in this message: 3 ================================ 01:22:52 UPS Notification from xxxxxx - Wed, 22 Jan 2025 01:22:52 -0800 Communications with UPS APC_Smart-UPS lost 01:22:57 UPS Notification from xxxxxx - Wed, 22 Jan 2025 01:22:57 -0800 UPS APC_Smart-UPS is unavailable 01:23:02 UPS Notification from xxxxxx - Wed, 22 Jan 2025 01:23:02 -0800 Communications with UPS APC_Smart-UPS established
So everything works great, but the notifications in the email are grouped together in a way that makes it hard to read. I would like to insert a couple newline characters at the end of each message, for example:
NOTIFYMSG NOCOMM "UPS %s is unavailable\n\n"
The problem I'm having is that nothings seems to work. There isn't a "Content-Type" line in the email, but it appears to be plain text. The \n shows up as a "n" in the email. Out of desperation I've also tried <br>, <p>, \n, \r\n, and $0A. Nothing seems to work.
I'm pretty sure I can fix it if I edit some of the underlying files directly, but I am really trying to stick to using the web UI to maintain compatibility with pfSense+ across updates. Does anyone know how to insert a linefeed?
-
When you enable :
You add a hidden line to the upsmon.conf file :
Here it is :
#!/usr/local/bin/php-cgi -q <?php /* * nut_email.php ...... require_once("notices.inc"); $subject = "UPS Notification from " . gethostname(); $message = date('r') . "\n\n"; $message .= implode(' ', array_slice($argv, 1)); @notify_all_remote($subject . " - " . $message);
Adding lines like "\n\n"; doesn't work ?
-
Thanks for the response.
I can edit the /usr/local/pkg/nut/nut_email.php to get it to work:
/* * nut_email.php * * part of pfSense (https://www.pfsense.org) * Copyright (c) 2004-2024 Rubicon Communications, LLC (Netgate) * Copyright (c) 2016 Denny Page * All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ require_once("notices.inc"); $subject = "UPS Notification from " . gethostname(); $message = date('r') . "\n\n"; $message .= implode(' ', array_slice($argv, 1)); $message .= "\n--------------------------------------------------------------------------------"; @notify_all_remote($subject . " - " . $message);
But I was just hoping I could find a way that didn't require modifying the package files. If this file is changed in a future update, I'll have to go back and modify it again, right? I assume it's a best practice to avoid that, if possible?
It seems like the "\n" escape sequence in upsmon.conf doesn't get through the array_slice or implode functions. It just shows up as a "n" in the message. Using "\n" will result in a "\n" in the email text, but it isn't evaluated as a linefeed.
-
You can create it as a custom patch in the System Patches package. That will be stored in the config and is just one click to apply (or revert) it.
-
@rdibley said in Linefeed in pfSense email notifications (with Network UPS Tools):
I am using Network UPS Tools with my pfSense router to monitor my UPS. The settings can be found at Services / UPS / Settings / UPS Settings. In the "Advanced Settings" there is a field for "Additional configuration lines for upsmon.conf" where I have set the following:
I'm curious how you ended up setting all of these to begin with. The notifications you are setting are the same ones that are baked into standard NUT build. You don't need to set them (and you shouldn't).
All you need to do is enable Notifications on the UPS Settings page. Everything else is automatic.
-
The short answer is that I was copying the values from a known working configuration on another router and I didn't realize these messages were baked into NUT as defaults. I've since gone back to the documentation and verified what you've said. But NUT the documentation also says that the default for the NOTIFYFLAG section is SYSLOG+WALL, which might lead one to believe that the EXEC flag needs to be added to upsmon.conf, but it is controlled in pfSense by the "enable notifications" checkbox. So there's this mix of the NUT documentation being correct (if I just RTFM...) but also differences that aren't documented in the pfSense package.
There are other areas where I ran into trouble, an example was that I couldn't connect from a client using the "admin" user to configure the UPS using the upsrw command. I added the "admin" user in the upsd.users field in Advanced Settings. It didn't work because the "admin" user was already defined in the default upsd.users file in pfSense, and that was hidden unless you use the "Edit File" feature or log into the console.
So I guess I'd summarize by saying that the process of installing NUT on pfSense was a little difficult due to the limited documentation and examples. But then again, there were a lot of things that went smoothly also. Some more details on the netgate wiki would go a long way.
Regarding my original question, I looked into it a little more and found that, as it is implemented, the strings are evaluated as literals through the implode and array_slice functions. There's no way to add a linefeed other than modifying the nut_email.php script.
-
@rdibley said in Linefeed in pfSense email notifications (with Network UPS Tools):
There are other areas where I ran into trouble, an example was that I couldn't connect from a client using the "admin" user to configure the UPS using the upsrw command. I added the "admin" user in the upsd.users field in Advanced Settings. It didn't work because the "admin" user was already defined in the default upsd.users file in pfSense, and that was hidden unless you use the "Edit File" feature or log into the console.
This is intentional. The admin password for NUT is randomly generated for security. You can find it by looking in /usr/local/etc/nut/upsd.users in the rare circumstance you actually need to use the upsrw command line.
I'm a big believer in the "it just works" approach, and the reason that the Advanced section is hidden is because 95%+ of users don't need to do anything there.
FWIW, you can find a wealth of information on using NUT in the UPS Tools section of the forum.
So I guess I'd summarize by saying that the process of installing NUT on pfSense was a little difficult due to the limited documentation and examples. But then again, there were a lot of things that went smoothly also. Some more details on the netgate wiki would go a long way.
I don't manage the wiki, just the package.
-
-
Thanks for pointing out the UPS Tools forum section. I never noticed it since I was thinking it would be called "NUT".
I get what you're saying about the approach. I think I'm just biased from my last encounter with setting up NUT (15 years ago?) and am probably approaching it with a different mindset than someone who's doing it for the first time. (and that isn't necessarily a good thing)
So one more question: if most people don't use the Advanced section (and the users.conf is blank), how does a secondary system connect to the primary? Is it that most people don't define a user/password for monitoring, but that the primary is just open to any secondary system that might want to connect?
...and for what it's worth, thanks for managing the package. It's been working great for me.
-
@rdibley said in Linefeed in pfSense email notifications (with Network UPS Tools):
if most people don't use the Advanced section (and the users.conf is blank), how does a secondary system connect to the primary? Is it that most people don't define a user/password for monitoring, but that the primary is just open to any secondary system that might want to connect?
While it is a commonly discussed thing, the percentage of folk who use remote access isn't very high. That said, it's one of the things that is detailed in a pinned post.
NB: The default monitoring password is also randomly generated. It's intended for use by the local upsmon only.
-
@rdibley said in Linefeed in pfSense email notifications (with Network UPS Tools):
So one more question: if most people don't use the Advanced section (and the users.conf is blank), how does a secondary system connect to the primary? Is it that most people don't define a user/password for monitoring, but that the primary is just open to any secondary system that might want to connect?
I'm using it
The third entry is for my Synology NAS :
And the NAS side :
and done.
I've also installed "Windows NUT Client" on two PC's nearby.
Example : PC 1 with user name "pcrecep1" and password "secret1" :Works fine also.
So I've 4 devices protected with one big 1000 VA APC UPS, where pfSense is the NUT 'server' as it has a USB cabled UPS hooked up to it, and now 4 devices are controlled by this UPS instead of one.