Snippets
Created by
Walter Nasich
last modified
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 | <?php
/*
https://bitbucket.org/snippets/wnasich/7Kg89
Usage:
Create: /usr/local/bin/apc_collector.sh
----
#! /bin/bash
/sbin/apcaccess | /usr/bin/php -f /path/to/apc2telegraf.php
----
$ chmod a+x /usr/local/bin/apc_collector.sh
Edit your telegraf.conf , section of plugin 'exec':
----
...
[[inputs.exec]]
commands = ["/usr/local/bin/apc_collector.sh"]
data_format = "influx"
...
----
If you want collect ups events:
* Add to /etc/rc.local the line below:
$ touch /tmp/upsapcevents.log
* Add to /etc/apcupsd/apccontrol at line 32 the line below:
----
echo "ups_apc_event,ups_name=$1 status=\"$2\" `date +%s%N`" >> /tmp/upsapcevents.log
----
* Enable inputs.tail at /etc/telegraf/telegraf.conf
Spec next settings:
files = ["/tmp/upsapcevents.log"]
from_beginning = false
data_format = "influx"
Restart telegraf. Data will be collected under measurement 'ups_apc'.
*/
// Check php.ini for proper timezone or uncomment next line
// date_default_timezone_set('[your zone]'); // http://php.net/manual/en/timezones.php
$tagsMap = array(
'UPSNAME' => 'ups_name', // UPS name from configuration file (dumb) or EEPROM (smart)
'SERIALNO' => 'serial_no', // UPS serial number
);
$fieldsMap = array(
'DATE' => 'time', // Date and time of last update from UPS
'STATUS' => 'status', // UPS status (online, charging, on battery etc)
'LINEV' => 'input_line_voltage', // Current input line voltage
'LOADPCT' => 'load_capacity_used', // Percentage of UPS load capacity used as estimated by UPS
'BCHARGE' => 'battery_capacity_charge', // Current battery capacity charge percentage
'TIMELEFT' => 'runtime_left_minutes', // Remaining runtime left on battery as estimated by the UPS
'OUTPUTV' => 'output_voltage', // Current UPS output voltage
'ITEMP' => 'internal_temperature_celcius', // UPS internal temperature in degrees Celcius
'BATTV' => 'battery_voltage', // Current battery voltage
'LINEFREQ' => 'line_frequency_hz', // Current line frequency in Hertz
'TONBATT' => 'time_on_battery_seconds', // Seconds currently on battery
);
$influxTags = array();
$influxFields = array();
$timestamp = null;
while ($line = fgets(STDIN)) {
$match = array();
$result = preg_match('/([A-Z ]+):(.*)/', $line, $match);
if (!$result) {
continue;
}
$key = trim($match[1]);
$value = trim($match[2]);
if ($key === 'DATE') {
$time = new DateTime($value);
$timestamp = $time->getTimestamp();
continue;
}
if (array_key_exists($key, $tagsMap)) {
$value = str_replace(',', '\,', $value);
$value = str_replace(' ', '\ ', $value);
$influxTags[$tagsMap[$key]] = $value;
}
if (array_key_exists($key, $fieldsMap)) {
if ($key === 'STATUS') {
$value = '"' . trim($value) . '"';
} else {
$value = preg_replace('/[^0-9\.]*/', '', $value);
}
$influxFields[$fieldsMap[$key]] = $value;
}
}
if ($timestamp) {
// Tags should be sorted by key before being sent for best performance of InfluxDB
ksort($influxTags);
$lineProtocolTags = array();
foreach ($influxTags as $name => $value) {
$lineProtocolTags[] = $name . '=' . $value;
}
$lineProtocolFields = array();
foreach ($influxFields as $name => $value) {
$lineProtocolFields[] = $name . '=' . $value;
}
echo 'ups_apc,' . join(',', $lineProtocolTags) . ' ' . join(',', $lineProtocolFields) . ' ' . $timestamp . '000000000';
}
|
Comments (0)
You can clone a snippet to your computer for local editing. Learn more.