# HOWTO: UPS with Gentoo Linux using NUT with MRTG Statistics

## tuxian

HOWTO: UPS with Gentoo Linux using NUT with MRTG Statistics

1.) Introduction

I use the Mustek Powermust USB 2000VA UPS  

This UPS is supported by nut version >= 2.0.1  

Important Notice:  

I recommend you to use the "powermust" driver, as the "mustek" driver doesn't support 24V

UPSes and then you get as battery.value always 100% charge.

Besides, the "powermust" driver has more features.  

2.) Install necessary packages

```
emerge nut apache2 mrtg  
```

Add apache to your default runlevel if you don't have done it yet:  

```
rc-update add apache2 default  
```

Notice: If you also have a Mustek UPS it might be necessary to use the ~x86 version of nut

because at the moment the latest stable version of nut in the portage tree is version 2.0.0-r1

and this version of nut doesn't support the Mustek UPS series.

So you might do  

```
echo "www-apps/nut ~x86" >> /etc/portage/package.keywords  
```

before merging nut!  

If you don't have installed a cron daemon install one, e.g.: emerge vixie-cron  

3.) Configure nut

Notice: The following configuration files are adapted for a Mustek UPS, if you use another

UPS you will need to change this files for your UPS!

My UPS is connected to COM1 (/dev/ttyS0).  

The configuration files of nut are located under /etc/nut!  

Index of ups.conf:  

[server]

       driver = powermust

       port = /dev/ttyS0

Index of upsd.conf:  

ACL all 0.0.0.0/0

ACL localhost 127.0.0.1/32

ACCEPT localhost

REJECT all

Index of upsd.users:  

       [admin]

              password = yoursecretpassword

              actions = SET

              instcmds = ALL

       [monuser]

              password = yoursecretpassword

              allowfrom = localhost markus

Index of upsmon.conf:  

RUN_AS_USER root

MONITOR server@localhost 1 monuser yoursecretpassword master

MINSUPPLIES 1

SHUTDOWNCMD "/sbin/shutdown -h +0"

POLLFREQ 5

POLLFREQALERT 5

HOSTSYNC 15

DEADTIME 15

POWERDOWNFLAG /etc/killpower

RBWARNTIME 43200

NOCOMMWARNTIME 300

FINALDELAY 5  

Index of upssched.conf:  

CMDSCRIPT /usr/local/ups/bin/upssched-cmd  

Start the daemons an add them to the default runlevel:  

```
/etc/init.d/upsd start

/etc.init.d/upsmon start

rc-update add upsd default

rc-update add upsmon default
```

Test if you are able to check the status of your UPS:

```
upsc server@localhost  
```

You should get a output like this:  

battery.charge: 75.0

battery.voltage: 25.4

battery.voltage.nominal: 24.0

driver.name: powermust

driver.parameter.port: /dev/ttyS0

driver.version: 2.0.2

driver.version.internal: 1.1

input.voltage: 228.6

input.voltage.fault: 228.1

input.voltage.maximum: 234.4

input.voltage.minimum: 227.6

output.frequency: 50.1

output.voltage: 225.7

output.voltage.target.battery: 220.0

ups.delay.shutdown: 2

ups.delay.start: 3

ups.load: 9.0

ups.mfr: Mustek

ups.model: PowerMust

ups.serial: unknown

ups.status: OL    

Notice: If your ouput is different you might have to need change your mrtg config files - see next chapter!  

4.) Configure MRTG

Create the directory /etc/mrtg/ups/:  

```
mkdirhier /etc/mrtg/ups/  
```

Download this tar archive and extract it in this directory:  index.tar

```
cd /etc/mrtg/ups/

tar xvf index.tar  
```

Make all *.sh-files executable: 

```
chmod 755 *.sh  
```

You might have to adapt this files for your system!  

Create the index.html file:  

```
/usr/bin/indexmaker \

--output=/var/www/localhost/htdocs/ups/index.html \

--title="USV Status" --sort=name --enumerate \

/etc/mrtg/ups/voltage.cfg \

/etc/mrtg/ups/batteryvoltage.cfg \

/etc/mrtg/ups/batterycharge.cfg \

/etc/mrtg/ups/load.cfg \

/etc/mrtg/ups/frequency.cfg \

/etc/mrtg/ups/status.cfg \  
```

Create the directory /etc/cron.mrtg an create a file named ups.sh with this content...  

#!/bin/sh

/usr/bin/mrtg /etc/mrtg/ups/voltage.cfg

/usr/bin/mrtg /etc/mrtg/ups/batteryvoltage.cfg

/usr/bin/mrtg /etc/mrtg/ups/batterycharge.cfg

/usr/bin/mrtg /etc/mrtg/ups/status.cfg

/usr/bin/mrtg /etc/mrtg/ups/load.cfg

/usr/bin/mrtg /etc/mrtg/ups/frequency.cfg  

... and make it executable:

```
 chmod 755 ups.sh  
```

Execute this script (/etc/cron.mrtg/ups.sh) at least three times (don't care about the warnings)!  

Run the command crontab -e and add this line to update the statistics every 5 minutes:  

*/5 * * * * /bin/run-parts /etc/cron.mrtg 1> /dev/null  

5.) Testing

Now open your web browser and open the page http://yourserver/ups

Your should now see your statistics, here a picture of mine:  http://www.layr.at/markus/images/linux/ups/ups.jpg

6.) Sending an email when UPS is offline (Optitional) 

I use ssmtp to send my an email via my yahoo account to an another email-address of an ISP

which allows forwarding emails to sms, so I get a sms to my mobile phone when the UPS is offine.  

Configure ssmtp (/etc/ssmtp/ssmtp.conf):  

root=youraddress@isp.com

mailhub=smtp.mail.yahoo.com

rewriteDomain=yahoo.de

FromLineOverride=YES  

Create a file named upsmail.sh in your home directory (root) with this content...  

if [ "`upsc server@localhost ups.status|grep OL`" == "" ]

then

echo "Subject: USV OFFLINE" > /tmp/usv.msg

echo "" >> /tmp/usv.msg

upsc server@localhost |grep "battery.charge" >> /tmp/usv.msg

upsc server@localhost |grep "battery.voltage:" >> /tmp/usv.msg

upsc server@localhost |grep "input.voltage" |grep "input.voltage:" >> /tmp/usv.msg

upsc server@localhost |grep "output.voltage" |grep "output.voltage:" >> /tmp/usv.msg

upsc server@localhost |grep "ups.status" >> /tmp/usv.msg

cat /tmp/usv.msg | /usr/sbin/ssmtp -au yourusername -ap yourpassword tothisaddress@isp.com

fi   

... and make it executable: 

```
chmod 755 upsmail.sh  
```

Run the command 

```
crontab -e
```

and add this line to executes this script every 5 minutes:  

*/5 * * * * /root/usvmail.sh 

Now you should get an email if your UPS is offline, I use the sms.at gateway to send my this emails as sms.Last edited by tuxian on Thu Jul 26, 2007 6:09 pm; edited 2 times in total

----------

## NightDragon

Great Howto!!!

Works fine.

But it seems that the tar-Archive is corrupt. The file batterycharge.cfg inside looks like an archive... 

Could you post the content of the file? Or please correct the archive.

Thx,

Nighty

----------

## tuxian

Thanks for the hint, I corrected the archive.

----------

## kensh

Hello,

I know that's a verry old threat, but anyone have the archive . Link is dead.

Or just a model of config.

Thanks you very much.

----------

## tuxian

Hello,

I fixed the link.

The name of the driver has changed to megatec (I use sys-power/nut-2.0.5-r2 now).

----------

## kensh

Hello Tuxian,

Thanks you very much for this quickly answer   :Wink: 

----------

## sbranz

hi, it's possible to turn POWER ON when return the electricity?

----------

## keyson

Hi sbranz,

You mean that you want the PC to start up after the ups had shutdown and when the line power is restored.

Yes this is possible. You set it in the bios of your pc.

You find someting like "Power State After AC Loss" in the settings.

And you may have "On", "Off", "Last State"

Set it to "last state" because if you have it off and don't want it to start, setting it to

"on" would start the PC after a power loss.

This info is in the doc's of nut also.

But you must have setup nut right so it does a shutdown return on power loss.

Regards

Kjell

----------

## sbranz

OKK tnx a lot  :Wink: 

----------

