Skip to content

Instantly share code, notes, and snippets.

@blakethepatton
Last active January 13, 2024 16:53
Show Gist options
  • Save blakethepatton/08394817cd4aed1f19350a37664e8b13 to your computer and use it in GitHub Desktop.
Save blakethepatton/08394817cd4aed1f19350a37664e8b13 to your computer and use it in GitHub Desktop.
Cyberpower UPS -> Grafana

Stuff for setting up grafana monitoring of cyberpower ups

done on ubuntu 20.04

Install influx

wget -qO- https://repos.influxdata.com/influxdb.key | gpg --dearmor > /etc/apt/trusted.gpg.d/influxdb.gpg
export DISTRIB_ID=$(lsb_release -si); export DISTRIB_CODENAME=$(lsb_release -sc)
echo "deb [signed-by=/etc/apt/trusted.gpg.d/influxdb.gpg] https://repos.influxdata.com/${DISTRIB_ID,,} ${DISTRIB_CODENAME} stable" > /etc/apt/sources.list.d/influxdb.list

sudo apt-get update && sudo apt-get install influxdb 
sudo service influxdb start

sudo apt-get install influxdb-client

influx
CREATE DATABASE ups WITH DURATION 168h

exit

Install powerpanel

wget https://dl4jz3rbrsfum.cloudfront.net/software/ppb470-linux-x86_x64.sh
chmod +x ppb470-linux-x86_x64.sh
sudo ./ppb470-linux-x86_x64.sh
sudo /usr/local/ppbe/ppbed start

Go to {ip_address}:3052 log in with username admin, password admin, and change your password

Block public traffic (optional)

sudo ufw allow ssh
sudo ufw allow from {grafana_ip}

if you want to allow public access to the dashboard
sudo ufw allow 3052 

Create the shell script

touch upsmon.sh
chmod +x upsmon.sh
vi upsmon.sh

copy and configure the shell script from the gist
  • Add your influx server to your grafana server
  • You can take my example dashboard and use it (dashboard.json)
  • You need to run the shell script, you can set it up as a service or just run it in console if you're debugging
{
"annotations": {
"list": [
{
"builtIn": 1,
"datasource": "-- Grafana --",
"enable": true,
"hide": true,
"iconColor": "rgba(0, 211, 255, 1)",
"name": "Annotations & Alerts",
"type": "dashboard"
}
]
},
"editable": true,
"gnetId": null,
"graphTooltip": 0,
"id": 75,
"links": [],
"panels": [
{
"datasource": "InfluxDB - UPS",
"fieldConfig": {
"defaults": {
"color": {
"mode": "palette-classic"
},
"custom": {
"axisLabel": "",
"axisPlacement": "auto",
"barAlignment": 0,
"drawStyle": "line",
"fillOpacity": 10,
"gradientMode": "none",
"hideFrom": {
"graph": false,
"legend": false,
"tooltip": false
},
"lineInterpolation": "linear",
"lineWidth": 1,
"pointSize": 5,
"scaleDistribution": {
"type": "linear"
},
"showPoints": "never",
"spanNulls": true
},
"mappings": [],
"max": 125,
"min": 115,
"thresholds": {
"mode": "absolute",
"steps": [
{
"color": "green",
"value": null
},
{
"color": "red",
"value": 80
}
]
},
"unit": "volt"
},
"overrides": []
},
"gridPos": {
"h": 7,
"w": 7,
"x": 0,
"y": 0
},
"id": 6,
"options": {
"graph": {},
"legend": {
"calcs": [],
"displayMode": "list",
"placement": "bottom"
},
"tooltipOptions": {
"mode": "single"
}
},
"pluginVersion": "7.5.3",
"targets": [
{
"groupBy": [
{
"params": [
"$__interval"
],
"type": "time"
},
{
"params": [
"null"
],
"type": "fill"
}
],
"measurement": "ups_data",
"orderByTime": "ASC",
"policy": "default",
"refId": "A",
"resultFormat": "time_series",
"select": [
[
{
"params": [
"denhost1.value"
],
"type": "field"
},
{
"params": [],
"type": "last"
}
]
],
"tags": [
{
"key": "sensor",
"operator": "=",
"value": "battvoltage"
}
]
}
],
"timeFrom": null,
"timeShift": null,
"title": "Output Voltage",
"type": "timeseries"
},
{
"aliasColors": {},
"bars": false,
"dashLength": 10,
"dashes": false,
"datasource": "InfluxDB - UPS",
"fieldConfig": {
"defaults": {},
"overrides": []
},
"fill": 1,
"fillGradient": 0,
"gridPos": {
"h": 7,
"w": 7,
"x": 7,
"y": 0
},
"hiddenSeries": false,
"id": 2,
"legend": {
"avg": false,
"current": false,
"max": false,
"min": false,
"show": true,
"total": false,
"values": false
},
"lines": true,
"linewidth": 1,
"nullPointMode": "null",
"options": {
"alertThreshold": true
},
"percentage": false,
"pluginVersion": "7.5.3",
"pointradius": 2,
"points": false,
"renderer": "flot",
"seriesOverrides": [],
"spaceLength": 10,
"stack": false,
"steppedLine": false,
"targets": [
{
"alias": "Runtime",
"groupBy": [
{
"params": [
"$__interval"
],
"type": "time"
},
{
"params": [
"null"
],
"type": "fill"
}
],
"measurement": "ups_data",
"orderByTime": "ASC",
"policy": "autogen",
"refId": "A",
"resultFormat": "time_series",
"select": [
[
{
"params": [
"denhost1.value"
],
"type": "field"
},
{
"params": [],
"type": "last"
},
{
"params": [
" / 60"
],
"type": "math"
}
]
],
"tags": [
{
"key": "sensor",
"operator": "=",
"value": "runtime"
}
]
}
],
"thresholds": [],
"timeFrom": null,
"timeRegions": [],
"timeShift": null,
"title": "Runtime (minutes)",
"tooltip": {
"shared": true,
"sort": 0,
"value_type": "individual"
},
"type": "graph",
"xaxis": {
"buckets": null,
"mode": "time",
"name": null,
"show": true,
"values": []
},
"yaxes": [
{
"format": "short",
"label": null,
"logBase": 1,
"max": null,
"min": null,
"show": true
},
{
"format": "short",
"label": null,
"logBase": 1,
"max": null,
"min": null,
"show": true
}
],
"yaxis": {
"align": false,
"alignLevel": null
}
},
{
"aliasColors": {},
"bars": false,
"dashLength": 10,
"dashes": false,
"datasource": "InfluxDB - UPS",
"fieldConfig": {
"defaults": {},
"overrides": []
},
"fill": 1,
"fillGradient": 0,
"gridPos": {
"h": 7,
"w": 7,
"x": 14,
"y": 0
},
"hiddenSeries": false,
"id": 4,
"legend": {
"avg": false,
"current": false,
"max": false,
"min": false,
"show": true,
"total": false,
"values": false
},
"lines": true,
"linewidth": 1,
"nullPointMode": "null",
"options": {
"alertThreshold": true
},
"percentage": false,
"pluginVersion": "7.5.3",
"pointradius": 2,
"points": false,
"renderer": "flot",
"seriesOverrides": [],
"spaceLength": 10,
"stack": false,
"steppedLine": false,
"targets": [
{
"groupBy": [
{
"params": [
"$__interval"
],
"type": "time"
},
{
"params": [
"null"
],
"type": "fill"
}
],
"measurement": "ups_data",
"orderByTime": "ASC",
"policy": "autogen",
"refId": "A",
"resultFormat": "time_series",
"select": [
[
{
"params": [
"denhost1.value"
],
"type": "field"
},
{
"params": [],
"type": "last"
}
]
],
"tags": [
{
"key": "sensor",
"operator": "=",
"value": "battcharge"
}
]
}
],
"thresholds": [],
"timeFrom": null,
"timeRegions": [],
"timeShift": null,
"title": "Battery Charge",
"tooltip": {
"shared": true,
"sort": 0,
"value_type": "individual"
},
"type": "graph",
"xaxis": {
"buckets": null,
"mode": "time",
"name": null,
"show": true,
"values": []
},
"yaxes": [
{
"$$hashKey": "object:837",
"format": "percent",
"label": null,
"logBase": 1,
"max": null,
"min": null,
"show": true
},
{
"$$hashKey": "object:838",
"format": "short",
"label": null,
"logBase": 1,
"max": null,
"min": null,
"show": true
}
],
"yaxis": {
"align": false,
"alignLevel": null
}
},
{
"datasource": "InfluxDB - UPS",
"fieldConfig": {
"defaults": {
"color": {
"mode": "thresholds"
},
"mappings": [],
"thresholds": {
"mode": "percentage",
"steps": [
{
"color": "red",
"value": null
},
{
"color": "orange",
"value": 80
},
{
"color": "green",
"value": 90
}
]
},
"unit": "percent"
},
"overrides": []
},
"gridPos": {
"h": 7,
"w": 3,
"x": 21,
"y": 0
},
"id": 13,
"options": {
"reduceOptions": {
"calcs": [
"lastNotNull"
],
"fields": "",
"values": false
},
"showThresholdLabels": false,
"showThresholdMarkers": true,
"text": {}
},
"pluginVersion": "7.5.3",
"targets": [
{
"groupBy": [
{
"params": [
"$__interval"
],
"type": "time"
},
{
"params": [
"null"
],
"type": "fill"
}
],
"measurement": "ups_data",
"orderByTime": "ASC",
"policy": "autogen",
"refId": "A",
"resultFormat": "time_series",
"select": [
[
{
"params": [
"denhost1.value"
],
"type": "field"
},
{
"params": [],
"type": "last"
}
]
],
"tags": [
{
"key": "sensor",
"operator": "=",
"value": "battcharge"
}
]
}
],
"timeFrom": null,
"timeShift": null,
"title": "Battery Charge",
"type": "gauge"
},
{
"aliasColors": {},
"bars": false,
"dashLength": 10,
"dashes": false,
"datasource": "InfluxDB - UPS",
"fieldConfig": {
"defaults": {},
"overrides": []
},
"fill": 1,
"fillGradient": 0,
"gridPos": {
"h": 6,
"w": 7,
"x": 0,
"y": 7
},
"hiddenSeries": false,
"id": 8,
"legend": {
"avg": false,
"current": false,
"max": false,
"min": false,
"show": true,
"total": false,
"values": false
},
"lines": true,
"linewidth": 1,
"nullPointMode": "null",
"options": {
"alertThreshold": true
},
"percentage": false,
"pluginVersion": "7.5.3",
"pointradius": 2,
"points": false,
"renderer": "flot",
"seriesOverrides": [],
"spaceLength": 10,
"stack": false,
"steppedLine": false,
"targets": [
{
"groupBy": [
{
"params": [
"$__interval"
],
"type": "time"
},
{
"params": [
"null"
],
"type": "fill"
}
],
"measurement": "ups_data",
"orderByTime": "ASC",
"policy": "autogen",
"refId": "A",
"resultFormat": "time_series",
"select": [
[
{
"params": [
"denhost1.value"
],
"type": "field"
},
{
"params": [],
"type": "last"
}
]
],
"tags": [
{
"key": "sensor",
"operator": "=",
"value": "battload"
}
]
}
],
"thresholds": [],
"timeFrom": null,
"timeRegions": [],
"timeShift": null,
"title": "Battery Load (Percent)",
"tooltip": {
"shared": true,
"sort": 0,
"value_type": "individual"
},
"type": "graph",
"xaxis": {
"buckets": null,
"mode": "time",
"name": null,
"show": true,
"values": []
},
"yaxes": [
{
"$$hashKey": "object:1036",
"format": "percent",
"label": null,
"logBase": 1,
"max": null,
"min": null,
"show": true
},
{
"$$hashKey": "object:1037",
"format": "short",
"label": null,
"logBase": 1,
"max": null,
"min": null,
"show": true
}
],
"yaxis": {
"align": false,
"alignLevel": null
}
},
{
"aliasColors": {},
"bars": false,
"dashLength": 10,
"dashes": false,
"datasource": "InfluxDB - UPS",
"fieldConfig": {
"defaults": {},
"overrides": []
},
"fill": 1,
"fillGradient": 0,
"gridPos": {
"h": 6,
"w": 7,
"x": 7,
"y": 7
},
"hiddenSeries": false,
"id": 10,
"legend": {
"avg": false,
"current": false,
"max": false,
"min": false,
"show": true,
"total": false,
"values": false
},
"lines": true,
"linewidth": 1,
"nullPointMode": "null",
"options": {
"alertThreshold": true
},
"percentage": false,
"pluginVersion": "7.5.3",
"pointradius": 2,
"points": false,
"renderer": "flot",
"seriesOverrides": [],
"spaceLength": 10,
"stack": false,
"steppedLine": false,
"targets": [
{
"groupBy": [
{
"params": [
"$__interval"
],
"type": "time"
},
{
"params": [
"null"
],
"type": "fill"
}
],
"measurement": "ups_data",
"orderByTime": "ASC",
"policy": "default",
"refId": "A",
"resultFormat": "time_series",
"select": [
[
{
"params": [
"denhost1.value"
],
"type": "field"
},
{
"params": [],
"type": "last"
}
]
],
"tags": [
{
"key": "sensor",
"operator": "=",
"value": "battloadwatt"
}
]
}
],
"thresholds": [],
"timeFrom": null,
"timeRegions": [],
"timeShift": null,
"title": "Load (Watts)",
"tooltip": {
"shared": true,
"sort": 0,
"value_type": "individual"
},
"type": "graph",
"xaxis": {
"buckets": null,
"mode": "time",
"name": null,
"show": true,
"values": []
},
"yaxes": [
{
"$$hashKey": "object:1442",
"format": "watt",
"label": null,
"logBase": 1,
"max": null,
"min": null,
"show": true
},
{
"$$hashKey": "object:1443",
"format": "short",
"label": null,
"logBase": 1,
"max": null,
"min": null,
"show": true
}
],
"yaxis": {
"align": false,
"alignLevel": null
}
},
{
"datasource": "InfluxDB - UPS",
"fieldConfig": {
"defaults": {
"color": {
"mode": "thresholds"
},
"mappings": [],
"thresholds": {
"mode": "absolute",
"steps": [
{
"color": "red",
"value": null
},
{
"color": "green",
"value": 90
}
]
},
"unit": "volt"
},
"overrides": []
},
"gridPos": {
"h": 6,
"w": 7,
"x": 14,
"y": 7
},
"id": 12,
"options": {
"colorMode": "value",
"graphMode": "area",
"justifyMode": "auto",
"orientation": "horizontal",
"reduceOptions": {
"calcs": [
"lastNotNull"
],
"fields": "",
"values": false
},
"text": {},
"textMode": "auto"
},
"pluginVersion": "7.5.3",
"targets": [
{
"groupBy": [
{
"params": [
"$__interval"
],
"type": "time"
},
{
"params": [
"null"
],
"type": "fill"
}
],
"measurement": "ups_data",
"orderByTime": "ASC",
"policy": "autogen",
"refId": "A",
"resultFormat": "time_series",
"select": [
[
{
"params": [
"denhost1.value"
],
"type": "field"
},
{
"params": [],
"type": "last"
}
]
],
"tags": [
{
"key": "sensor",
"operator": "=",
"value": "inputvoltage"
}
]
}
],
"timeFrom": null,
"timeShift": null,
"title": "Input Voltage",
"type": "stat"
}
],
"refresh": "1m",
"schemaVersion": 27,
"style": "dark",
"tags": [],
"templating": {
"list": []
},
"time": {
"from": "now-6h",
"to": "now"
},
"timepicker": {},
"timezone": "",
"title": "UPS Dashboard",
"uid": "tFLdFZO7k",
"version": 4
}
#!/bin/bash
#This script pulls ups information from a Cyberpower UPS.
#The time we are going to sleep between readings
#Also used to calculate the current usage on the interface
#30 seconds seems to be ideal, any more frequent and the data
#gets really spikey. Since we are calculating on total octets
#you will never lose data by setting this to a larger value.
#modified to remove the synology stuff
sleeptime=30
get_ups_info() {
#-- Cyber Power Panel --
#Pull data from the Cyber Power Panel. Replace with your own IP.
cpp_json_data=$(curl http://localhost:3052/agent/ppbe.js/init_status.js)
#input voltage
cpp_involts=$(echo $cpp_json_data | grep -oP '(?<="voltage":")[^."]*' | head -1)
#battery voltage
cpp_battvolts=$(echo $cpp_json_data | grep -oP '(?<="voltage":")[^."]*' | tail -1)
#Load(Watts)
cpp_loadwatt=$(echo $cpp_json_data | grep -oP '(?<="watt":)[^,]*' | head -1)
#Capacity %
cpp_capacity=$(echo $cpp_json_data | grep -oP '(?<="capacity":)[^,]*' | head -1)
#Runtime
runtimeHour=$(echo $cpp_json_data | grep -oP '(?<="runtimeHour":)[^,]*' | head -1)
runtimeMinute=$(echo $cpp_json_data | grep -oP '(?<="runtimeMinute":)[^,]*' | head -1)
cpp_runtime=$(($runtimeHour*60*60+$runtimeMinute*60))
#Load %
cpp_loadpercent=$(echo $cpp_json_data | grep -oP '(?<="load":)[^,]*' | head -1)
}
print_data() {
echo "CPP UPS Battery Voltage: $cpp_battvolts"
echo "CPP UPS Battery Charge: $cpp_capacity"
echo "CPP UPS Load: $cpp_loadpercent"
echo "CPP UPS Load(Watts): $cpp_loadwatt"
echo "CPP UPS Input Voltage: $cpp_involts"
echo "CPP UPS Runtime: $cpp_runtime"
}
write_data() {
#Finally we can write it to the database
curl -i -XPOST 'http://localhost:8086/write?db=ups' --data-binary "ups_data,host=host1,sensor=battvoltage denhost1.value=$cpp_battvolts"
curl -i -XPOST 'http://localhost:8086/write?db=ups' --data-binary "ups_data,host=host1,sensor=battcharge denhost1.value=$cpp_capacity"
curl -i -XPOST 'http://localhost:8086/write?db=ups' --data-binary "ups_data,host=host1,sensor=battload denhost1.value=$cpp_loadpercent"
curl -i -XPOST 'http://localhost:8086/write?db=ups' --data-binary "ups_data,host=host1,sensor=battloadwatt denhost1.value=$cpp_loadwatt"
curl -i -XPOST 'http://localhost:8086/write?db=ups' --data-binary "ups_data,host=host1,sensor=inputvoltage denhost1.value=$cpp_involts"
curl -i -XPOST 'http://localhost:8086/write?db=ups' --data-binary "ups_data,host=host1,sensor=runtime denhost1.value=$cpp_runtime"
}
#Prepare to start the loop and warn the user
echo "Press [CTRL+C] to stop..."
while :
do
get_ups_info
if [[ $cpp_battvolts -le 0 || $cpp_capacity -le 0 || $cpp_loadpercent -le 0 || $cpp_involts -le 0 || $cpp_runtime -le 0 ]];
then
echo "Skip this datapoint - something went wrong with the read"
else
#Output console data for future reference
print_data
write_data
fi
#Sleep between readings
sleep "$sleeptime"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment