opentsdb输入信息格式为:put metric timestamp value tagname=tagvalue tag2=value2,在logstash-output-opentsdb插件metrics配置中默认已经输入timestamp,因此metrics需要配置的第一个参数为metricName,第二个参数为 value 之后依次为tagname,tagValue。
ruby { code => "fields = event['message'].split(/\r\n|\n/) length = fields.length-1 for i in 1..length do if fields[i].include?':' then field = fields[i].split(':') event[field[0]] = field[1].to_f end end " remove_field => [ "message" ] }
Gem::Specification.new do |s| s.name = 'logstash-input-example' s.version = '2.0.4' s.licenses = ['Apache License (2.0)'] s.summary = "This example input streams a string at a definable interval." s.description = "This gem is a Logstash plugin required to be installed on top of the Logstash core pipeline using $LS_HOME/bin/logstash-plugin install gemname. This gem is not a stand-alone program" s.authors = ["Elastic"] s.email = 'info@elastic.co' s.homepage = "http://www.elastic.co/guide/en/logstash/current/index.html" s.require_paths = ["lib"]
# Generate a repeating message. # # This plugin is intented only as an example.
class LogStash::Inputs::Example < LogStash::Inputs::Base config_name "example"
# If undefined, Logstash will complain, even if codec is unused. default :codec, "plain"
# The message string to use in the event. config :message, :validate => :string, :default => "Hello World!"
# Set how frequently messages should be sent. # # The default, `1`, means send a message every second. config :interval, :validate => :number, :default => 1
public def register @host = Socket.gethostname end # def register
def run(queue) # we can abort the loop if stop? becomes true while !stop? event = LogStash::Event.new("message" => @message, "host" => @host) decorate(event) queue << event # because the sleep interval can be big, when shutdown happens # we want to be able to abort the sleep # Stud.stoppable_sleep will frequently evaluate the given block # and abort the sleep(@interval) if the return value is true Stud.stoppable_sleep(@interval) { stop? } end # loop end # def run
def stop # nothing to do in this case so it is not necessary to define stop # examples of common "stop" tasks: # * close sockets (unblocking blocking reads/accepts) # * cleanup temporary files # * terminate spawned threads end end # class LogStash::Inputs::Example
def execute(command, queue) @logger.debug? && @logger.debug("Running exec", :command => command) begin @io = IO.popen(command) fields = (@io.read).split(/\r\n|\n/) puts fields length = fields.length-1 for i in 0..length do if fields[i].include?':' then field = fields[i].split(':') newcommand = "redis-cli -c -h #{field[0]} -p #{field[1]} info" @io = IO.popen(newcommand) @codec.decode(@io.read) do |event| decorate(event) event.set("host", @hostname) event.set("command", newcommand) queue << event end end
end rescue StandardError => e @logger.error("Error while running command", :command => command, :e => e, :backtrace => e.backtrace) rescue Exception => e @logger.error("Exception while running command", :command => command, :e => e, :backtrace => e.backtrace) ensure stop end end