#!/usr/bin/env ruby # nagios.cfg must contain the following perfdata templates for host and service data (modified from the default # to include hoststate / servicestate, and a fake service 'HOST' for hostperfdata, so that the # fields match up: # # host_perfdata_file_template=[HOSTPERFDATA]\t$TIMET$\t$HOSTNAME$\tHOST\t$HOSTSTATE$\t$HOSTEXECUTIONTIME$\t$HOSTLATENCY$\t$HOSTOUTPUT$\t$HOSTPERFDATA$ # # service_perfdata_file_template=[SERVICEPERFDATA]\t$TIMET$\t$HOSTNAME$\t$SERVICEDESC$\t$SERVICESTATE$\t$SERVICEEXECUTIONTIME$\t$SERVICELATENCY$\t$SERVICEOUTPUT$\t$SERVICEPERFDATA$ # unless $:.include?(File.dirname(__FILE__) + '/../lib/') $: << File.dirname(__FILE__) + '/../lib' end require 'optparse' require 'ostruct' require 'redis' require 'oj' Oj.default_options = { :indent => 0, :mode => :strict } require 'dante' require 'flapjack/configuration' require 'flapjack/data/event' def process_input(opts) redis = opts[:redis] fifo = File.new(opts[:fifo]) begin while line = fifo.gets skip unless line split_line = line.split("\t") object_type, timestamp, entity, check, state, check_time, check_latency, check_output, check_perfdata, check_long_output = split_line case when ! (split_line.length > 8) puts "ERROR - rejecting this line as it doesn't split into at least 9 tab separated strings: [#{line}]" next when (timestamp !~ /^\d+$/) puts "ERROR - rejecting this line as second string doesn't look like a timestamp: [#{line}]" next when (not ((object_type == '[HOSTPERFDATA]') or (object_type == '[SERVICEPERFDATA]'))) puts "ERROR - rejecting this line as first string is neither '[HOSTPERFDATA]' nor '[SERVICEPERFDATA]': [#{line}]" next end puts "#{object_type}, #{timestamp}, #{entity}, #{check}, #{state}, #{check_output}, #{check_long_output}" state = 'ok' if state.downcase == 'up' state = 'critical' if state.downcase == 'down' details = check_long_output ? check_long_output.gsub(/\\n/, "\n") : nil event = { 'entity' => entity, 'check' => check, 'type' => 'service', 'state' => state, 'summary' => check_output, 'details' => details, 'time' => timestamp, } Flapjack::Data::Event.add(event, :redis => redis) end rescue Redis::CannotConnectError puts "Error, unable to to connect to the redis server (#{$!})" end end def main(opts) fifo = opts[:fifo] redis = Redis.new(opts[:redis_options]) while true process_input(:redis => redis, :fifo => fifo) puts "Whoops with the fifo, restarting main loop in 10 seconds" sleep 10 end end options = OpenStruct.new options.config = Flapjack::Configuration::DEFAULT_CONFIG_PATH options.daemonize = nil exe = File.basename(__FILE__) optparse = OptionParser.new do |opts| opts.banner = "Usage: #{exe} COMMAND [OPTIONS]" opts.separator "" opts.separator "Commands" opts.separator " start #{" " * 25} start #{exe}" opts.separator " stop #{" " * 26} stop #{exe}" opts.separator " restart #{" " * 23} (re)start #{exe}" opts.separator " status #{" " * 24} see if #{exe} is running" opts.separator "" opts.separator "Options" opts.on("-c", "--config [PATH]", String, "PATH to the config file to use") do |c| options.config = c end opts.on("-f", "--fifo FIFO", String, "Path to the nagios perfdata named pipe") do |f| options.fifo = f end opts.on("-d", "--[no-]daemonize", "Daemonize?") do |d| options.daemonize = d end opts.on("-p", "--pidfile [PATH]", String, "PATH to the pidfile to write to") do |pid| options.pidfile = pid end opts.on("-l", "--logfile [PATH]", String, "PATH to the logfile to write to") do |l| options.logfile = l end opts.on_tail("-h", "--help", "Show this usage message") do puts opts puts ' Required Nagios Configuration Changes ------------------------------------- flapjack-nagios-receiver reads events from a named pipe written to by Nagios. The named pipe needs creating, and Nagios needs to be told to write performance data output to it. To create the named pipe: mkfifo -m 0666 /var/cache/nagios3/event_stream.fifo nagios.cfg changes: # modified lines: enable_notifications=0 host_perfdata_file=/var/cache/nagios3/event_stream.fifo service_perfdata_file=/var/cache/nagios3/event_stream.fifo host_perfdata_file_template=[HOSTPERFDATA]\t$TIMET$\t$HOSTNAME$\tHOST\t$HOSTSTATE$\t$HOSTEXECUTIONTIME$\t$HOSTLATENCY$\t$HOSTOUTPUT$\t$HOSTPERFDATA$ service_perfdata_file_template=[SERVICEPERFDATA]\t$TIMET$\t$HOSTNAME$\t$SERVICEDESC$\t$SERVICESTATE$\t$SERVICEEXECUTIONTIME$\t$SERVICELATENCY$\t$SERVICEOUTPUT$\t$SERVICEPERFDATA$ host_perfdata_file_mode=p service_perfdata_file_mode=p Details on the wiki: https://github.com/flpjck/flapjack/wiki/USING#configuring-nagios ' exit end end optparse.parse!(ARGV) FLAPJACK_ENV = ENV['FLAPJACK_ENV'] || 'production' config = Flapjack::Configuration.new config.load(options.config) config_env = config.all redis_options = config.for_redis if config_env.nil? || config_env.empty? puts "No config data for environment '#{FLAPJACK_ENV}' found in '#{options.config}'" puts optparse exit 1 end config_nr = config_env['nagios-receiver'] || {} pidfile = options.pidfile.nil? ? (config_nr['pid_file'] || "/var/run/flapjack/#{exe}.pid") : options.pidfile logfile = options.logfile.nil? ? (config_nr['log_file'] || "/var/log/flapjack/#{exe}.log") : options.logfile fifo = options.fifo.nil? ? (config_nr['fifo'] || '/var/cache/nagios3/event_stream.fifo') : options.fifo daemonize = options.daemonize.nil? ? !!config_nr['daemonize'] : options.daemonize runner = Dante::Runner.new(exe, :pid_path => pidfile, :log_path => logfile) case ARGV[0] when "start", "restart" unless File.exist?(fifo) raise "No fifo (named pipe) file found at #{fifo}" end unless File.pipe?(fifo) raise "The file at #{fifo} is not a named pipe, try using mkfifo to make one" end unless File.readable?(fifo) raise "The fifo (named pipe) at #{fifo} is unreadable" end end case ARGV[0] when "start" if runner.daemon_running? puts "#{exe} is already running." exit 1 else print "#{exe} starting..." runner.execute(:daemonize => daemonize) { main(:redis_options => redis_options, :fifo => fifo) } puts " done." end when "stop" if runner.daemon_running? print "#{exe} stopping..." runner.execute(:kill => true) puts " done." else puts "#{exe} is not running." exit 1 end when "restart" print "#{exe} restarting..." runner.execute(:daemonize => true, :restart => true) { main(:redis_options => redis_options, :fifo => fifo) } puts " done." when "status" uptime = (runner.daemon_running?) ? Time.now - File.stat(pidfile).ctime : 0 if runner.daemon_running? puts "#{exe} is running: #{uptime}" else puts "#{exe} is not running" exit 3 end else if ARGV.nil? || ARGV.empty? puts "No command provided" else puts "Unknown command provided: '#{ARGV[0]}'" end puts "\n#{optparse}" exit 1 end