Created
September 26, 2017 13:21
-
-
Save hanynowsky/00429460b1b2f52779f121edfd19eab8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# Magic Online 2016 | |
# # This extension mutator is supposed to format sensu events to InfluxDb line format | |
#! /usr/bin/env ruby | |
# | |
# mutator-influxdb-line-protocol | |
# | |
# DESCRIPTION: | |
# Mutates check results to conform to InfluxDB's line protocol format | |
# | |
# Place this file in /etc/sensu/extensions and modify your handlers JSON config | |
# | |
# handlers.json | |
# { | |
# "influxdb_udp": { | |
# "type": "udp", | |
# "mutator": "mutator-influxdb-line-protocol", | |
# "socket": { | |
# "host": "mgt-monitor-db1", | |
# "port": 8090 | |
# } | |
# } | |
# } | |
require 'sensu/extension' | |
module Sensu | |
module Extension | |
class InfluxDBLineProtocol < Mutator | |
def name | |
'influxdb_line_protocol' | |
end | |
def description | |
"returns check output formatted for InfluxDB's line protocol" | |
end | |
def run(event) | |
host = event[:client][:name] | |
metric = event[:check][:name] | |
output = event[:check][:output] | |
formahost = host.gsub(".",'_') | |
data = [] | |
output.split("\n").each do |result| | |
m = result.split | |
next unless m.count == 3 | |
key = m[0].split('.', 2)[1] | |
key.tr!('.', '_') | |
fkey= key.gsub("#{formahost}_","") # Cutsom: replaced key with fkey | |
value = m[1].to_f | |
time = m[2].ljust(19, '0') | |
data << "#{fkey},host=#{host},metric=#{metric} value=#{value} #{time}" | |
end | |
yield data.join("\n"), 0 | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment