Skip to content

Instantly share code, notes, and snippets.

@sarguru
Created November 30, 2012 03:49
Show Gist options
  • Save sarguru/4173671 to your computer and use it in GitHub Desktop.
Save sarguru/4173671 to your computer and use it in GitHub Desktop.
puppetparser and stomp
#!/usr/bin/ruby
require 'yaml'
require 'puppet'
require 'json'
require 'stomp'
def empty_event
{ "name" => "puppet-report",
"metrics" => {},
"metric" => {},
"messages" => {},
"severity" => 0,
"event_time" => "",
"subject" => "",
"type" => "",
"origin" => "puppet",
}
end
report_dir='/var/lib/puppet/reports'
Dir.foreach(report_dir) do |subdir|
next if subdir == '.' or subdir == '..'
Dir.foreach("#{report_dir}/#{subdir}") do |report|
next if report == '.' or report == '..' or report == 'false'
logfile = File.open("#{report_dir}/#{subdir}/#{report}")
report = YAML::load(logfile)
nodehostname=subdir
log_event = empty_event
metric_event = empty_event
log_event["subject"] = nodehostname
metric_event["subject"] = nodehostname
log_event["type"] = "log"
metric_event["type"] = "metric"
date_timestamp = `date --date="#{report.time}" "+%s"`
log_event["event_time"] = date_timestamp.chomp
metric_event["event_time"] = date_timestamp.chomp
report.logs.each do |line|
line.source.gsub!(".","-")
log_event ["messages"] [line.source] = line.message if "#{line.level}" == "notice"
end
report.metrics.values.each do |metricstype|
metricstype.values.each do |metricsvalues|
key = [metricstype.name,metricsvalues[0]].join(".")
key_db = [metricstype.name,metricsvalues[0]].join("-")
metric_event ["metrics"][key] = metricsvalues[2]
metric_event ["metric"][key_db] = metricsvalues[2]
metric_event["severity"]=1 if "#{metricsvalues[0]}" == "failure" and metricsvalues[2].to_i > 0
log_event["severity"]=1 if "#{metricsvalues[0]}" == "failure" and metricsvalues[2].to_i > 0
metric_event["severity"]=2 if "#{metricsvalues[0]}" == "failure" and metricsvalues[2].to_i > 4
log_event["severity"]=2 if "#{metricsvalues[0]}" == "failure" and metricsvalues[2].to_i > 4
end
end
logfile.close
begin
Timeout::timeout(2) do
stomp = Stomp::Client.new("", "", "puppet.deeproot.in", 61613)
stomp.publish("/queue/Consumer.mongo.VirtualTopic.monitor", log_event.to_json,:persistent => true)
stomp.publish("/topic/VirtualTopic.monitor", metric_event.to_json)
stomp.close
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment