Snippets

Walter Nasich Telegraf input for APC UPS status using exec plugin

Created by Walter Nasich last modified
<?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)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.