{"id":2844,"date":"2025-09-30T10:35:26","date_gmt":"2025-09-30T02:35:26","guid":{"rendered":"https:\/\/blog.warbel.net\/?p=2844"},"modified":"2025-09-30T10:35:26","modified_gmt":"2025-09-30T02:35:26","slug":"configuring-network-ups-tools-to-use-nextclouds-push-notifications","status":"publish","type":"post","link":"https:\/\/blog.warbel.net\/index.php\/2025\/09\/30\/configuring-network-ups-tools-to-use-nextclouds-push-notifications\/","title":{"rendered":"Configuring Network UPS Tools to use Nextcloud&#8217;s Push Notifications"},"content":{"rendered":"<p>One of the problem with running servers in a home environment is the lack of 100% reliable electricity. I&#8217;ve noticed that since moving house nearly 2 years ago, going from underground to above ground power, has resulted in a significant reduction in grid reliability. There have been frequent outages due to replacing power poles, road accidents that have destroyed poles and other maintenance that just wasn&#8217;t an issue in our old place. To cut a long story short, this means I have a renewed interest in monitoring my uninterruptible power supplies (UPS) for outages so I can respond. Ordinarily I would use email for this, however my alerts go to a separate folder and I which I only check periodically and when I&#8217;m in the office or out on the weekend, I need to know instantly if there is an issue.<\/p>\n<p>This is where push notifications come in handy. Nextcloud has a function to allow administrators to send notifications so I needed to write a script that hooks into the API and sends the message. The steps are simple enough:<\/p>\n<ul>\n<li>Create a service account for sending API notifications in Nextcloud<\/li>\n<li>Create an API key for the above<\/li>\n<li>Create the script to be executed by the network UPS tool monitoring service that will send an email and call another script (below)<\/li>\n<li>Create a script that will send the push notification.<\/li>\n<\/ul>\n<p>Starting with the service account, however you would normally do it, depends on your circumstances. In mine, I created an service account in AD, then in Nextcloud granted the minimum required permissions to be able to send notifications to that account. If you&#8217;re doing it this way, you&#8217;ll need the ObjectGUID from AD later. If necessary create an app password in nextcloud for the service account.<\/p>\n<p>Edit the file \/etc\/nut\/upsmon.conf and add the following to the NOTIFTYCMD section:<\/p>\n<p><span>NOTIFYCMD \/usr\/local\/bin\/nut-email.sh<br \/>\n<\/span><\/p>\n<p>Create the script below in the correct location (<span>\/usr\/local\/bin\/nut-email.sh) and make it executable and owned by root. This script will send an email and then run another script to send the push notification.<\/span><\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism undefined-numbers lang-bash\" data-lang=\"Bash\"><code><span>#!\/bin\/bash \r\n# \/usr\/local\/bin\/nut-email.sh \r\n\r\nTO=\"youremail@yourdomain.tld\" \r\nSUBJECT=\"UPS Alert: $UPSNAME $NOTIFYTYPE\" \r\nBODY=\"Event: $NOTIFYTYPE occurred on $UPSNAME at $(date).\" \r\n\r\necho \"$BODY\" | mail -s \"$SUBJECT\" \"$TO\" \r\n\r\n# execute push notifcation \r\nexport SUBJECT \r\n\/usr\/local\/bin\/nut-push.py\r\n<\/span><\/code><\/pre>\n<\/div>\n<p>Create the script for interacting with nextcloud&#8217;s notification app in the location \/usr\/local\/bin\/nut-push.py. Ensure it is owned by root and executable.<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism undefined-numbers lang-bash\" data-lang=\"Bash\"><code><span>#!\/usr\/bin\/env python3 \r\n\r\nimport requests, os \r\nfrom requests.auth import HTTPBasicAuth \r\n\r\n# Your Nextcloud configuration \r\nNEXTCLOUD_URL = 'https:\/\/your.nextcloud.url' \r\nUSERNAME = 'service_account@yourdomain.tld' \r\nPASSWORD = 'account_password' \r\n\r\n# Notification content \r\nMESSAGE = '\uf6a8 '+os.getenv('SUBJECT') \r\n# MESSAGE = '\uf6a8 UPS warning: Power failure detected. System may shut down soon.' \r\n\r\n# API endpoint \r\nurl = f'{NEXTCLOUD_URL}\/ocs\/v2.php\/apps\/notifications\/api\/v2\/admin_notifications\/&lt;ObjectGUID&gt;' \r\n\r\n# Headers required by Nextcloud OCS API \r\nheaders = { \r\n&nbsp;&nbsp;&nbsp;'OCS-APIREQUEST': 'true', \r\n&nbsp;&nbsp;&nbsp;#'Content-Type': 'application\/x-www-form-urlencoded' \r\n} \r\n\r\n# Payload \r\npayload = { \r\n&nbsp;&nbsp;&nbsp;'shortMessage': MESSAGE \r\n} \r\n\r\n# Make the request \r\nresponse = requests.post(url, headers=headers, data=payload, auth=HTTPBasicAuth(USERNAME, PASSWORD)) \r\n\r\n# Check result \r\nif response.status_code == 200: \r\n&nbsp;&nbsp;&nbsp;print('Notification sent successfully!') \r\nelse: \r\n&nbsp;&nbsp;&nbsp;print(f'Failed to send notification: {response.status_code}, {response.text}')\r\n\r\n<\/span><\/code><\/pre>\n<\/div>\n<p>And thats it! You should be able to test this by executing the script above.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the problem with running servers in a home environment is the lack of 100% reliable electricity. I&#8217;ve noticed that since moving house nearly 2 years ago, going from underground to above ground power, has resulted in a significant &hellip; <a href=\"https:\/\/blog.warbel.net\/index.php\/2025\/09\/30\/configuring-network-ups-tools-to-use-nextclouds-push-notifications\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2844","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/blog.warbel.net\/index.php\/wp-json\/wp\/v2\/posts\/2844","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.warbel.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.warbel.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.warbel.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.warbel.net\/index.php\/wp-json\/wp\/v2\/comments?post=2844"}],"version-history":[{"count":2,"href":"https:\/\/blog.warbel.net\/index.php\/wp-json\/wp\/v2\/posts\/2844\/revisions"}],"predecessor-version":[{"id":2846,"href":"https:\/\/blog.warbel.net\/index.php\/wp-json\/wp\/v2\/posts\/2844\/revisions\/2846"}],"wp:attachment":[{"href":"https:\/\/blog.warbel.net\/index.php\/wp-json\/wp\/v2\/media?parent=2844"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.warbel.net\/index.php\/wp-json\/wp\/v2\/categories?post=2844"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.warbel.net\/index.php\/wp-json\/wp\/v2\/tags?post=2844"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}