{"id":47,"date":"2011-01-31T08:04:00","date_gmt":"2011-01-30T22:04:00","guid":{"rendered":"https:\/\/www.serenux.com\/?p=47"},"modified":"2021-01-14T08:13:47","modified_gmt":"2021-01-13T22:13:47","slug":"howto-configure-an-apc-ups-to-communicate-with-your-ubuntu-desktop-or-server","status":"publish","type":"post","link":"https:\/\/www.serenux.com\/index.php\/2011\/01\/31\/howto-configure-an-apc-ups-to-communicate-with-your-ubuntu-desktop-or-server\/","title":{"rendered":"HowTo: Configure an APC UPS to communicate with your Ubuntu Desktop or Server"},"content":{"rendered":"\n<p>It\u2019s been a while since I\u2019ve written something here, so time to break the drought.<\/p>\n\n\n\n<p>APC make some great UPS products and they all have the ability to communicate with a host PC to advise of its state, eg: on mains, on battery, fault, etc. The support software is available aplenty for Windows and Mac, but what about Linux?<\/p>\n\n\n\n<p>This guide will show you how to hook up an APC UPS to an Ubuntu-based Desktop or Server PC and allow your UPS to email you when mains power has failed, when mains power has been restored, and also give your PC ample opportunity to shutdown when battery on the UPS gets to a critical low.<\/p>\n\n\n\n<p>Pre-requisites:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Ubuntu-based PC. I am using Ubuntu 10.04 Lucid Lynx in this guide.<\/li><li>An APC UPS. I\u2019m using a Back-UPS RS 800 in this guide.<\/li><li>Details about your ISP\u2019s mail server, or your own local mail server.<\/li><\/ul>\n\n\n\n<p>Configuration:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Setup your APC UPS as outlined in the instruction manual. Ensure that the USB or serial cable is connected from the UPS to the PC. This is what the UPS uses to communicate with the PC.<br><\/li><li>On your Ubuntu PC, go into a Terminal. If you are using Ubuntu Desktop, you can do this by going to the Applications menu, then Accessories and then Terminal. If you are using Ubuntu Server, simply login to your server by the console or SSH in remotely.<br><\/li><li>The Ubuntu repositories contain a UPS daemon specifically designed with APC UPS units in mind called APCUPSD, so let\u2019s install it:<br><br><code>$ sudo apt-get install apcupsd<\/code><br><\/li><li>Once installed, we need to configure it to suit your UPS. Bring up the configuration file by typing in:<br><br><code>$ sudo nano \/etc\/apcupsd\/apcupsd.conf<\/code><br><\/li><li>In the editor, scroll down to UPSNAME and give the UPS a name, eg:<br><br><code>UPSNAME MyAwesomeUPS<\/code><br><\/li><li>Scroll down to UPSCABLE and change the parameter to the type of cable you are using to connect to your UPS. In my case, I use a USB cable that came with the UPS, so I changed this line to read:<br><br><code>UPSCABLE usb<\/code><br><\/li><li>The next line to modify is the UPSTYPE line. Again, because I\u2019m using USB I specify:<br><br><code>UPSTYPE usb<\/code><br><br>If you have a DEVICE \/dev\/ttyS0 line after the UPSTYPE line, comment it out by adding a hash symbol at the start of the line like so:<br><br><code>#DEVICE \/dev\/ttyS0<\/code><br><\/li><li>We do not need to modify anything else, so press CTRL+X, then \u201cY\u201d and then Enter to save your changes and exit the Nano text editor.<br><\/li><li>Now we need to tell the system that the UPS daemon has been setup and is essentially ready to go. Type in:<br><br><code>$ sudo nano \/etc\/default\/apcupsd<\/code><br><\/li><li>In the file that appears, modify the ISCONFIGURED line to say Yes\u201d, ie:<br><br><code>ISCONFIGURED=yes<\/code><br><\/li><li>Press CTRL+X, then \u201cY\u201d and then Enter to save your changes and exit.<br><\/li><li>We\u2019re pretty much done here. Start the daemon now with the following command:<br><br><code>$ sudo service apcupsd restart<\/code><br><br>\u2026or simply reboot your PC. Your PC is now monitoring the state of the UPS.<br><\/li><li>Let\u2019s check the UPS now. Type in the following command:<br><br><code>$ apcaccess<\/code><br><br>\u2026and you will get output similar to the following:<br><br><code>$ apcaccess<br>APC\u00a0\u00a0\u00a0\u00a0\u00a0 : 001,043,1045<br>DATE\u00a0\u00a0\u00a0\u00a0 : Tue Feb 01 00:02:36 EST 2011<br>HOSTNAME : lamaar<br>VERSION\u00a0 : 3.14.6 (16 May 2009) debian<br>UPSNAME\u00a0 : LAMAAR<br>CABLE\u00a0\u00a0\u00a0 : USB Cable<br>MODEL\u00a0\u00a0\u00a0 : Back-UPS BR\u00a0 800<br>UPSMODE\u00a0 : Stand Alone<br>STARTTIME: Mon Jan 31 23:43:37 EST 2011<br>STATUS\u00a0\u00a0 : ONLINE<br>LINEV\u00a0\u00a0\u00a0 : 250.0 Volts<br>LOADPCT\u00a0 :\u00a0 40.0 Percent Load Capacity<br>BCHARGE\u00a0 : 100.0 Percent<br>TIMELEFT :\u00a0 19.9 Minutes<br>MBATTCHG : 5 Percent<br>MINTIMEL : 3 Minutes<br>MAXTIME\u00a0 : 0 Seconds<br>OUTPUTV\u00a0 : 230.0 Volts<br>SENSE\u00a0\u00a0\u00a0 : Medium<br>DWAKE\u00a0\u00a0\u00a0 : 000 Seconds<br>DSHUTD\u00a0\u00a0 : 000 Seconds<br>LOTRANS\u00a0 : 194.0 Volts<br>HITRANS\u00a0 : 264.0 Volts<br>RETPCT\u00a0\u00a0 : 000.0 Percent<br>ITEMP\u00a0\u00a0\u00a0 : 29.2 C Internal<br>ALARMDEL : Always<br>BATTV\u00a0\u00a0\u00a0 : 27.4 Volts<br>LINEFREQ : 50.0 Hz<br>LASTXFER : Low line voltage<br>NUMXFERS : 0<br>TONBATT\u00a0 : 0 seconds<br>CUMONBATT: 0 seconds<br>XOFFBATT : N\/A<br>SELFTEST : NO<br>STATFLAG : 0x07000008 Status Flag<br>SERIALNO : xxxxxxxxxxxx<br>BATTDATE : 2001-09-25<br>NOMOUTV\u00a0 : 230 Volts<br>NOMINV\u00a0\u00a0 : 230 Volts<br>NOMBATTV :\u00a0 24.0 Volts<br>NOMPOWER : 540 Watts<br>FIRMWARE : 9.o5 .I USB FW:o5<br>APCMODEL : Back-UPS BR\u00a0 800<br>END APC\u00a0 : Tue Feb 01 00:02:41 EST 2011<br>$<\/code><br><\/li><li>Pat yourself on the back, you\u2019re basically done.<\/li><\/ol>\n\n\n\n<p>But hang on, what about setting up email notifications? Read on.<\/p>\n\n\n\n<p><em><strong>Setting up Email Notifications<\/strong><\/em><\/p>\n\n\n\n<p>By default, APCUPSD is configured to use&nbsp;<em>sendmail<\/em>&nbsp;to send emails, but sendmail is finicky. There is a better way using a similar application called&nbsp;<em>send<strong>e<\/strong>mail<\/em>&nbsp;instead.<\/p>\n\n\n\n<p>Why use sendemail instead of sendmail? Well, sendemail is a simple command-line SMTP client. You construct a simple message with a recipient and off it goes, but the key difference here is that sendemail is a CLIENT, not a SERVER. This means for sendemail to work, you need a third-party mail server to send it through. You may already have a mail server on your network running Postfix or similar. If not, your ISP will most certainly have one that you can use.<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Let\u2019s start by installing sendemail:<br><br><code>$ sudo apt-get install sendemail<\/code><br><\/li><li>Now we need to configure apcupsd to use it. If you have a look inside the \/etc\/apcupsd directory, you will see several important files as follows:<ul><li>changeme\u00a0\u2013 notifies you if the UPS battery needs changing.<\/li><li>commfailure\u00a0\u2013 notifies you if the PC loses communication with the UPS.<\/li><li>commok\u00a0\u2013 notifies you if lost communication is restored with the UPS.<\/li><li>onbattery\u00a0\u2013 notifies you if the UPS engages battery mode due to mains power failure.<\/li><li>offbattery\u00a0\u2013 notifies you if the UPS returns to mains mode after a mains power failure.<br><\/li><\/ul><\/li><li>Let\u2019s modify the onbattery message:<br><br><code>$ sudo nano \/etc\/apcupsd\/onbattery<\/code><br><\/li><li>You will notice there are some lines in this file that define some variables followed by a message and then finally a command that sends an email using the sendmail app. Since we are not using sendmail, we need to modify a number of areas. Replace the content of the entire text file with the following:<br><br><code>#!\/bin\/sh<br>#<br># This shell script if placed in \/etc\/apcupsd<br># will be called by \/etc\/apcupsd\/apccontrol when the UPS<br># goes on batteries.<br>#<br>SYSADMIN=jbloggs@mydomain.com.au<br>APCUPSD_MAIL=\"\/usr\/bin\/sendemail\"<br>HOSTNAME=`hostname`<br>MSG=\"Ubuntu PC Power Failure !!!\"<br>#<br>(<br>echo \" \"<br>echo \" =====================================\"<br>echo \" POWER FAILURE ON $HOSTNAME !!\"<br>echo \" =====================================\"<br>echo \" \"<br>echo \"The UPS on your Ubuntu PC has experienced a power problem that has required the UPS to engage battery mode.\"<br>echo \" \"<br>echo \"Do not panic! Remain calm...\"<br>echo \" \"<br>echo \"Current UPS status:\"<br>echo \" \"<br>\/sbin\/apcaccess status<br>) | $APCUPSD_MAIL -u \"$MSG\" -f MyUbuntuPC@mydomain.com.au -t $SYSADMIN -s mail.myisp.com:25<br>exit 0<\/code><br><br>So what have we done here? The first part of the file sets some variables, namely the email address(es) of the people required to be notified. If you need to send to more than one recipient, separate email addresses with a comma, eg:<br><br><code>SYSADMIN=jbloggs@mydomain.com.au,fredsmith@thatotherdomain.com<\/code><br><br>We then define the path to the sendemail app, the hostname of the PC running the UPS daemon, a subject line for the email in question, and then a big email body explaining what\u2019s going on, in this case that the UPS battery mode has been engaged.All this is then piped into the sendemail application with a fake sender\u2019s address to give you an idea where it came from and we also specify the mail server that the email will be sent via. In this case, we are using our ISP\u2019s mail server that has the address\u00a0<em>mail.myisp.com<\/em><br><\/li><li>Save your changes by pressing CTRL+X, then \u201cY\u201d and then Enter.<br><\/li><li>You can now test the script by simply executing it (we don\u2019t have to disconnect the UPS\u2019 mains cable to trigger this until we\u2019re ready to test that). Execute the file using the command:<br><br><code>$ sh \/etc\/apcupsd\/onbattery<\/code><br><\/li><li>Check for any errors and then check to see if you got an email in your mailbox advising that the UPS is on battery (the UPS of course is not, we\u2019re just testing the email).<br><\/li><li>If all is well, continue to change the other four message files to be similar to the above, changing the message as you go of course.<br><\/li><li>Pat yourself on the back \u2013 you now have custom, explicit messages to let you know what\u2019s going on with the UPS.<\/li><\/ol>\n\n\n\n<p><em><strong>Setting up your own mail server to use instead<\/strong><\/em><\/p>\n\n\n\n<p>If, in your testing, you discover that your ISP\u2019s mail server does not allow you to randomly spam messages to it using fake from addresses, you have two choices \u2013 either specify a valid email address as the sender\u2019s address, or you can setup a simple email server of your own using Postfix:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ sudo apt-get install postfix<\/pre>\n\n\n\n<p>In the case of setting up your own email server, simply go with Postfix and set it up as an \u201cInternet Site\u201d. The basic configuration is good enough to deal with our needs, but make sure your basic network security is adequate to prevent outsiders from trying to use your mail server to send unsolicited mails, or spam, through it.<\/p>\n\n\n\n<p>This article will not describe how to setup or secure Postfix as it is beyond the scope of this article, however you do need to modify the outgoing mail server name in all your communication messages to reflect the change of name or internal IP address of your mail server instead of mail.myisp.com.<\/p>\n\n\n\n<p><em><strong>Testing<\/strong><\/em><\/p>\n\n\n\n<p>You can test your daemon setup easily enough by doing any of the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Remove mains power cable from UPS.<\/li><li>Remove the USB cable to the UPS.<\/li><li>Verify that an email is sent for either of the error conditions above.<\/li><\/ul>\n\n\n\n<p>Enjoy!&nbsp;<img decoding=\"async\" src=\"https:\/\/web.archive.org\/web\/20150311073137im_\/http:\/\/www.serenux.com\/wp-content\/themes\/grey-opaque\/images\/smilies\/icon_smile.gif\" alt=\"Smilie: :)\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>It\u2019s been a while since I\u2019ve written something here, so time to break the drought. APC make some great UPS products and they all have the ability to communicate with a host PC to advise of its state, eg: on mains, on battery, fault, etc. The support software is available aplenty for Windows and Mac, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-47","post","type-post","status-publish","format-standard","hentry","category-tutorials"],"_links":{"self":[{"href":"https:\/\/www.serenux.com\/index.php\/wp-json\/wp\/v2\/posts\/47","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.serenux.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.serenux.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.serenux.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.serenux.com\/index.php\/wp-json\/wp\/v2\/comments?post=47"}],"version-history":[{"count":1,"href":"https:\/\/www.serenux.com\/index.php\/wp-json\/wp\/v2\/posts\/47\/revisions"}],"predecessor-version":[{"id":48,"href":"https:\/\/www.serenux.com\/index.php\/wp-json\/wp\/v2\/posts\/47\/revisions\/48"}],"wp:attachment":[{"href":"https:\/\/www.serenux.com\/index.php\/wp-json\/wp\/v2\/media?parent=47"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.serenux.com\/index.php\/wp-json\/wp\/v2\/categories?post=47"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.serenux.com\/index.php\/wp-json\/wp\/v2\/tags?post=47"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}