Created
May 18, 2012 14:31
-
-
Save jshiell/2725553 to your computer and use it in GitHub Desktop.
Dump status from Nagios.
This file contains 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
require 'rubygems' | |
require 'nokogiri' | |
require 'open-uri' | |
def get_nagios_status(server_host, username = 'monitor', password = 'monitor') | |
doc = Nokogiri::HTML(open("http://#{server_host}/nagios/cgi-bin/status.cgi?servicestatustypes=28&hoststatustypes=15", :http_basic_authentication => [username, password])) | |
hosts = [] | |
last_title = nil | |
last_link = nil | |
doc.css('table.status>tr').each do |row| | |
title_row = row.css('a[title]')[0] | |
next if title_row.nil? && last_title.nil? | |
next if !row.xpath("td[1]").attr('colspan').nil? | |
if title_row | |
host = title_row.attr('title') | |
link = title_row.attr('href') | |
else | |
host = last_title | |
link = last_link | |
end | |
service = row.xpath('td[2]')[0].content.strip | |
status = row.xpath('td[3]')[0].content | |
message = row.xpath('td[7]')[0].content | |
last_title = host | |
last_link = link | |
hosts << {:hostname => host, :service => service, :status => status, :link => link,:message => message} | |
end | |
hosts | |
end | |
if ARGV.length == 0 | |
puts "Usage: #{__FILE__} <server>" | |
exit 1 | |
end | |
server = ARGV[0] | |
status = get_nagios_status(server) | |
puts "Critical: #{status.reduce(0) {|count, status| count = if status[:status] == 'CRITICAL' then count + 1 else count end}}" | |
puts "Warning: #{status.reduce(0) {|count, status| count = if status[:status] == 'WARNING' then count + 1 else count end}}" | |
status.each do |host| | |
puts "Host = #{host[:hostname]}, service = #{host[:service]}, status = #{host[:status]}, message = #{host[:message]}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment