lib/nagiosharder.rb in nagiosharder-0.2.4 vs lib/nagiosharder.rb in nagiosharder-0.3.0

- old
+ new

@@ -19,21 +19,27 @@ require 'httparty' class NagiosHarder class Site - attr_accessor :nagios_url, :user, :password, :default_options, :default_cookies, :version + attr_accessor :nagios_url, :user, :password, :default_options, :default_cookies, :version, :nagios_time_format include HTTParty::ClassMethods def initialize(nagios_url, user, password, version = 3) @nagios_url = nagios_url.gsub(/\/$/, '') @user = user @password = password @default_options = {} @default_cookies = {} @version = version basic_auth(@user, @password) if @user && @password + + @nagios_time_format = if @version.to_i < 3 + "%m-%d-%Y %H:%M:%S" + else + "%Y-%m-%d %H:%M:%S" + end end def acknowledge_service(host, service, comment) # extra options: sticky_arg, send_notification, persistent @@ -241,14 +247,59 @@ services = {} parse_status_html(response) do |status| services[status[:service]] = status end - + services end + def disable_service_notifications(host, service, options = {}) + request = { + :cmd_mod => 2, + :cmd_typ => 23, + :host => host, + :service => service, + } + + response = post(cmd_url, :body => request) + if response.code == 200 && response.body =~ /successful/ + # TODO enable waiting. seems to hang intermittently + #if options[:wait] + # sleep(3) until service_notifications_disabled?(host, service) + #end + true + else + false + end + end + + def enable_service_notifications(host, service, options = {}) + request = { + :cmd_mod => 2, + :cmd_typ => 22, + :host => host, + :service => service, + } + + response = post(cmd_url, :body => request) + if response.code == 200 && response.body =~ /successful/ + # TODO enable waiting. seems to hang intermittently + #if options[:wait] + # sleep(3) while service_notifications_disabled?(host, service) + #end + true + else + false + end + end + + def service_notifications_disabled?(host, service) + self.host_status(host)[service].notifications_disabled + end + + def status_url "#{nagios_url}/status.cgi" end def cmd_url @@ -259,19 +310,10 @@ "#{nagios_url}/extinfo.cgi" end private - - def nagios_time_format - if @version.to_i < 3 - "%m-%d-%Y %H:%M:%S" - else - "%Y-%m-%d %H:%M:%S" - end - end - def formatted_time_for(time) time.strftime(nagios_time_format) end def parse_status_html(response) @@ -280,11 +322,11 @@ last_host = nil rows.each do |row| columns = Nokogiri::HTML(row.inner_html).css('body > td').to_a if columns.any? - + host = columns[0].inner_text.gsub(/\n/, '') # for a given host, the host details are blank after the first row if host != '' # remember it for next time @@ -308,10 +350,13 @@ end acknowledged = other_links.any? do |link| link.css('img').attribute('src').to_s =~ /ack\.gif/ end + notifications_disabled = other_links.any? do |link| + link.css('img').attribute('src').to_s =~ /ndisabled\.gif/ + end extra_service_notes_link = other_links.detect do |link| link.css('img').any? do |img| img.attribute('src').to_s =~ /notes\.gif/ end @@ -355,18 +400,19 @@ :extended_info => status_info, :acknowledged => acknowledged, :service_extinfo_url => service_extinfo_url, :flapping => flapping, :comments_url => comments_url, - :extra_service_notes_url => extra_service_notes_url + :extra_service_notes_url => extra_service_notes_url, + :notifications_disabled => notifications_disabled yield status end end end nil end - + end end