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.

Installation

Install the containers 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

You can check existing databases with docker exec.

docker exec -it Influxdb bash
influx -execute 'SHOW DATABASES'

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

Telegraf

Note: You don’t really need Telegraf for this guide as the script writes directly to the InfluxDB telegraf database, but if this is the first time you’re setting everything up you might as well install and configure Telegraf.

Edit telegraf.conf and add the influxdb ip. It’s on line 92 and 93 in the telegraf.conf file.

urls = ["http://192.168.1.34:8086"] # required
 ## The target database for metrics (telegraf will create it if not exists).
 database = "telegraf" # required

User Scripts

To be able to have Grafana read the UPS metrics we need to send them to the telegraf 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=telegraf' --data-binary 'APC,host=Tower,region=us-west "
.$tagname."=".$val."'";
$execsr = exec($curl);

}

?>

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=telegraf' --data-binary 'APC,host=Nostromo,region=eu-west-3 "

http://192.168.1.34:8086: is the IP to InfluxDB
db=telegraf: 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 telegraf
Click Save & Test

Create a new dashboard and add your panels.

This is how I have setup my dashboard. Here you can copy the Panel JSON.
https://grafana.technicalramblings.com/d/FBsdas/unraid-ups-dashboard-tr?refresh=10s&orgId=1

Here is a link to the Dashboard ID if you want to 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.

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! 🙂

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/