This commit is contained in:
root 2024-07-22 13:44:29 -04:00
commit f3e42189f2
5 changed files with 94 additions and 0 deletions

16
README.md Normal file
View File

@ -0,0 +1,16 @@
# CyberPower Exporter
Export UPS data via HTTP for prometheus service
## Setup
### Prerequisites
- [Bun](https://bun.sh)
### Instructions
1. Download the [PowerPanel](https://cyberpower.com/global/en/product/sku/powerpanel_for_linux#downloads)
2. Install the package: `sudo dpkg -i powerpanel.deb`
3. Install the service:
4. Start service automatically: `sudo systemctl enable power-exporter && sudo systemctl start power-exporter`

29
install.sh Executable file
View File

@ -0,0 +1,29 @@
#!/bin/bash
if [ "$UID" != "0" ]; then
echo "Please run as root"
exit 1
fi
# Install UPS driver
if [ -z "$(which pwrstat)" ]; then
echo "Installing Power Panel..."
dpkg -i powerpanel.deb
fi
# Setup exporter
echo "Installing Exporter..."
cp power-exporter.js /usr/local/bin/power-exporter.js
chmod +x /usr/local/bin/power-exporter.js
# Setup service
echo "Stting Up Service..."
cp power-exporter.service /etc/systemd/system/power-exporter.service
chmod +x /etc/systemd/system/power-exporter.service
systemctl enable power-exporter
systemctl restart power-exporter
echo "Done!"
sleep 1
echo ""
journalctl -u power-exporter -n 1

39
power-exporter.js Executable file
View File

@ -0,0 +1,39 @@
import { $ } from 'bun';
import http from 'http';
const port = 3000;
async function scrape() {
const data = await $`pwrstat -status`.text();
const r = /^\s*(.+?)\.{3,} (.+)$/gm;
let found, map = {};
while(!!(found = r.exec(data)))
map[found[1]] = found[2];
return {
model: map["Model Name"],
vendor: map["Power Supply by"],
state: map["State"],
charge: Number(/\d+/g.exec(map["Battery Capacity"])[0]) / 100,
remaining: map["Remaining Runtime"],
rated_voltage: Number(/\d+/g.exec(map["Rating Voltage"])[0]),
rated_wattage: Number(/\d+/g.exec(map["Rating Power"])[0]),
input_voltage: Number(/\d+/g.exec(map["Utility Voltage"])[0]),
output_voltage: Number(/\d+/g.exec(map["Output Voltage"])[0]),
load_wattage: Number(/\d+/g.exec(map["Load"])[0]),
load_percentage: Number(/(\d+) %/g.exec(map["Load"])[1]) / 100,
last_test: map["Test Result"],
last_outage: map["Last Power Event"],
};
}
function format(a) {
return Object.entries(a).map(([key, value]) => `${key} ${typeof value == 'string' ? `"${value}"` : value}`).join('\n');
}
http.createServer(async (req, res) => {
res.end(format(await scrape()));
}).listen(port, () => {
console.log(`Exporter running on http://localhost:${port}`)
});

10
power-exporter.service Normal file
View File

@ -0,0 +1,10 @@
[Unit]
Description=Export UPS metrics to webpage for scraping
After=network.target
[Service]
ExecStart=/root/.bun/bin/bun /usr/local/bin/power-exporter.js
Restart=always
[Install]
WantedBy=multi-user.target

BIN
powerpanel.deb Normal file

Binary file not shown.