This guide won’t go into detail on how every setting in Grafana works, mainly because I don’t really know 🙂 This will mostly be about adding UPS stats to Grafana and calculating the cost of running your server 24/7.

Update! There is an updated 2019 blog post on this subject!

Installation

Install the container and follow the instructions.

InfluxDB

Note: A database called telegraf is created when you start the telegraf container for the first time. I will be using that for this guide.



EDIT: I recommend not using the telegraf database and creating a new database purely for UPS data. Read more here

Create a new database with

docker exec -it Influxdb bash

influx -execute 'CREATE DATABASE <name>' e.g. ‘ CREATE DATABASE UPS'

You can check existing databases with docker exec.

influx -execute 'SHOW DATABASES'

> SHOW DATABASES name: databases name ---- _internal telegraf UPS

To be able to have Grafana read the UPS metrics we need to send them to the UPS database in InfluxDB.

I’m doing that using this awesome script by Viaduct on the Unraid forums.

https://lime-technology.com/forums/topic/50495-scripts-for-server-monitoring-using-influx-db-and-grafana-without-telegraf-agent/?tab=comments#comment-496966

#!/usr/bin/php <?php $command = "apcaccess"; $args = "status"; $tagsArray = array( "LOADPCT", "ITEMP", "TIMELEFT", "TONBATT", "BCHARGE" ); //do system call $call = $command." ".$args; $output = shell_exec($call); //parse output for tag and value foreach ($tagsArray as $tag) { preg_match("/".$tag."\s*:\s([\d|\.]+)/si", $output, $match); //send measurement, tag and value to influx sendDB($match[1], $tag); } //end system call //send to influxdb function sendDB($val, $tagname) { $curl = "curl -i -XPOST 'http://influxDBIP:8086/write?db=UPS' --data-binary 'APC,host=Tower,region=us-west " .$tagname."=".$val."'"; $execsr = exec($curl); } ?>

(If you need to use SNMP scroll down to the bottom!)

By using the User Scripts plugin you can easily set up a cronjob every minute.

Click on Add New Script → Enter the name → Click on the name of the script and then Edit Script

Copy the script and add your ip address to InfluxDB, which database to write to, name of the data table, host name and region.

$curl = "curl -i -XPOST 'http://192.168.1.34:8086/write?db=UPS' --data-binary 'APC,host=Nostromo,region=eu-west-3 "

http://192.168.1.34:8086 : is the IP to InfluxDB

db=UPS : is the name of the database in InfluxDB.

APC : is the name of the data table

host : is the name of your host. In my case Nostromo.

region : is the region you live in.

Click Save Changes when you’re done.

Set the schedule to custom and add * * * * * for it to run every minute.

Click Run In Background and then Apply

The downside of using crontab is that it’s limited to only run every 1 minute. If you know of a smarter solution that will run the script more often please tell me in the comments!

Grafana

Go to “Configuration” and click + Add data source.

Set it to type InfluxDB

Add your URL to InfluxDB

And add the database UPS

Click Save & Test

Create a new dashboard and add your panels.

Here is a link to the Dashboard ID so you can import the whole thing:

https://grafana.com/dashboards/7197

And here is a link to my Unraid System Dashboard

https://grafana.com/dashboards/7233

Final Notes

The reason yearly and or monthly/weekly (depending on when you read this) costs are the same is it hasn’t been a week/month/year yet after I set this up. These numbers will grow and stop depending on the time frame of the stats.

To be able to get the Last Month Power Consumptions/Cost panel to display correctly I have set the dashboard to previous month. And added a custom time range to the other panels.

math

Note 28.07.18: I’ve added some variables, so now you only need to select the kWh price and Max watt output on your UPS. You will still need to edit the currency on the panels and probably edit the values on the kWh Price variable.





Note 27.12.18:

I’ve added a variable that lets you choose the datasource of your UPS data. Making it even easier to set up. No need to change the panels!

For it to show correct Wattage for your UPS you need to edit the math part.

For example on the Current UPS Load panel.

SELECT mean("LOADPCT") / 100*865 FROM "APC" WHERE $timeFilter GROUP BY time($__interval) fill(none)

The number 865 is my UPS max capacity of watt output. So to calculate the watt load you take the load percentage and divide it with 100(%) and multiply it with the max output (865W)

If you want to show kWh load you divide that by 1000

And remember to change the currency!

If you have any comments on how I can do this smarter/better please do share!

– Thank you Samuel Hoyt, for letting me know my math was wrong! 🙂

If you have to use SNMP you can use this python script. Thanks ardaingeal!

#!/usr/bin/python3 from easysnmp import snmp_get, Session from influxdb import InfluxDBClient #Query UPS Load snmpqueryload = snmp_get('.1.3.6.1.4.1.318.1.1.1.4.2.3.0', hostname='IP/hostname', security_username='SNMP user', version=3) resultload = str(snmpqueryload) lpct = (int(resultload[21:23])) #Query Runtime remaining snmpqueryruntime = snmp_get('.1.3.6.1.4.1.318.1.1.1.2.2.3.0', hostname='hostname='IP/hostname', security_username='SNMP user', version=3) resultruntime = str(snmpqueryruntime) runtime = (int(resultruntime[21:27])) #Query Battery Capacity snmpquerycharge = snmp_get('.1.3.6.1.4.1.318.1.1.1.2.2.1.0', hostname='hostname='IP/hostname', security_username='SNMP user', version=3) resultcharge = str(snmpquerycharge) capacity = (int(resultcharge[21:24])) #Insert into InfluxDB client = InfluxDBClient(host='127.0.0.1', port='8086', username='influxdbusername', password='influxdbpassword') #client.create_database('UPS') client.switch_database('UPS') json_body = [ { "measurement": "APC", "tags": { "Model": "Smart-UPS2200", "Location": "Home" }, "fields": { "LOADPCT": lpct, "BCHARGE": capacity, "TIMELEFT": runtime } } ] client.write_points(json_body)

For any questions you can find me here:

Sources:

https://lime-technology.com/forums/topic/50495-scripts-for-server-monitoring-using-influx-db-and-grafana-without-telegraf-agent/

https://www.reddit.com/r/homelab/comments/65yn4p/calculating_power_consumption_costs_in_grafana/

https://fablab.dyn.nerd2nerd.org/grafana/dashboard/db/fablab-power?refresh=1m&orgId=1

https://docs.influxdata.com/influxdb/v1.5/tools/shell/

https://www.reddit.com/r/unRAID/comments/7c2l2w/howto_monitor_unraid_with_grafana_influxdb_and/