lib/nagiosharder.rb in nagiosharder-0.4.0.rc1 vs lib/nagiosharder.rb in nagiosharder-0.4.0
- old
+ new
@@ -1,11 +1,10 @@
require 'restclient'
require 'nokogiri'
require 'active_support' # fine, we'll just do all of activesupport instead of the parts I want. thank Rails 3 for shuffling requires around.
require 'cgi'
require 'hashie'
-require 'nagiosharder/filters'
# :(
require 'active_support/version' # double and triplely ensure ActiveSupport::VERSION is around
if ActiveSupport::VERSION::MAJOR > 2
require 'active_support/core_ext/array'
@@ -23,32 +22,29 @@
class NagiosHarder
class Site
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_time_format = nil)
+ def initialize(nagios_url, user, password, version = 3)
@nagios_url = nagios_url.gsub(/\/$/, '')
@user = user
@password = password
@default_options = {}
@default_cookies = {}
@version = version
- debug_output if ENV['DEBUG']
basic_auth(@user, @password) if @user && @password
- @nagios_time_format = if nagios_time_format == 'us'
- "%m-%d-%Y %H:%M:%S"
- else
- if @version.to_i < 3
- "%m-%d-%Y %H:%M:%S"
- else
- "%Y-%m-%d %H:%M:%S"
- end
- end
- self
+
+ @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
+
request = {
:cmd_typ => 34,
:cmd_mod => 2,
:com_author => @user,
:com_data => comment,
@@ -61,26 +57,10 @@
response = post(cmd_url, :body => request)
response.code == 200 && response.body =~ /successful/
end
- def acknowledge_host(host, comment)
- request = {
- :cmd_typ => 33,
- :cmd_mod => 2,
- :com_author => @user,
- :com_data => comment,
- :host => host,
- :send_notification => true,
- :persistent => false,
- :sticky_ack => true
- }
-
- response = post(cmd_url, :body => request)
- response.code == 200 && response.body =~ /successful/
- end
-
def unacknowledge_service(host, service)
request = {
:cmd_typ => 52,
:cmd_mod => 2,
:host => host,
@@ -188,46 +168,61 @@
:cmd_mod => 2
})
response.code == 200 && response.body =~ /successful/
end
- def service_status(options = {})
- params = {}
+ def service_status(type, options = {})
+ service_status_type = case type
+ when :ok then 2
+ when :warning then 4
+ when :unknown then 8
+ when :critical then 16
+ when :pending then 1
+ when :all_problems then 28
+ when :all then nil
+ else
+ raise "Unknown type"
+ end
- {
- :host_status_types => :notification_host,
- :service_status_types => :notification_service,
- :sort_type => :sort,
- :sort_option => :sort,
- :host_props => :host,
- :service_props => :service,
- }.each do |key, val|
- if options[key] && (options[key].is_a?(Array) || options[key].is_a?(Symbol))
- params[key.to_s.gsub(/_/, '')] = Nagiosharder::Filters.value(val, *options[key])
- end
- end
+ sort_type = case options[:sort_type]
+ when :asc then 1
+ when :desc then 2
+ when nil then nil
+ else
+ raise "Invalid options[:sort_type]"
+ end
- # if any of the standard filter params are already integers, those win
- %w(
- :hoststatustypes,
- :servicestatustypes,
- :sorttype,
- :sortoption,
- :hostprops,
- :serviceprops,
- ).each do |key|
- params[key.to_s] = options[:val] if !options[:val].nil? && options[:val].match(/^\d*$/)
- end
+ sort_option = case options[:sort_option]
+ when :host then 1
+ when :service then 2
+ when :status then 3
+ when :last_check then 4
+ when :duration then 6
+ when :attempts then 5
+ when nil then nil
+ else
+ raise "Invalid options[:sort_option]"
+ end
+ service_group = options[:group]
+
+
+ params = {
+ 'hoststatustype' => options[:hoststatustype] || 15,
+ 'servicestatustypes' => options[:servicestatustypes] || service_status_type,
+ 'sorttype' => options[:sorttype] || sort_type,
+ 'sortoption' => options[:sortoption] || sort_option,
+ 'hoststatustypes' => options[:hoststatustypes],
+ 'serviceprops' => options[:serviceprops]
+ }
+
if @version == 3
- params['servicegroup'] = options[:group] || 'all'
+ params['servicegroup'] = service_group || 'all'
params['style'] = 'detail'
- params['embedded'] = '1'
- params['noheader'] = '1'
else
- if options[:group]
- params['servicegroup'] = options[:group]
+ if service_group
+ params['servicegroup'] = service_group
params['style'] = 'detail'
else
params['host'] = 'all'
end
end
@@ -245,11 +240,11 @@
statuses
end
def host_status(host)
- host_status_url = "#{status_url}?host=#{host}&embedded=1&noheader=1"
+ host_status_url = "#{status_url}?host=#{host}"
response = get(host_status_url)
raise "wtf #{host_status_url}? #{response.code}" unless response.code == 200
services = {}
@@ -342,24 +337,21 @@
last_host = nil
rows.each do |row|
columns = Nokogiri::HTML(row.inner_html).css('body > td').to_a
if columns.any?
- # Host column
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
last_host = host
else
# or save it for later
host = last_host
end
- debug 'parsed host column'
- # Service Column
if columns[1]
service_links = columns[1].css('td a')
service_link, other_links = service_links[0], service_links[1..-1]
if service_links.size > 1
comments_link = other_links.detect do |link|
@@ -386,45 +378,29 @@
extra_service_notes_url = extra_service_notes_link.attribute('href').to_s if extra_service_notes_link
end
service = service_links[0].inner_html
end
- debug 'parsed service column'
- # Status
status = columns[2].inner_html if columns[2]
- debug 'parsed status column'
-
- # Last Check
last_check = if columns[3] && columns[3].inner_html != 'N/A'
last_check_str = columns[3].inner_html
- debug "Need to parse #{columns[3].inner_html} in #{nagios_time_format}"
+
DateTime.strptime(columns[3].inner_html, nagios_time_format).to_s
end
- debug 'parsed last check column'
-
- # Duration
duration = columns[4].inner_html.squeeze(' ').gsub(/^ /, '') if columns[4]
started_at = if duration && match_data = duration.match(/^\s*(\d+)d\s+(\d+)h\s+(\d+)m\s+(\d+)s\s*$/)
(
match_data[1].to_i.days +
match_data[2].to_i.hours +
match_data[3].to_i.minutes +
match_data[4].to_i.seconds
).ago
end
- debug 'parsed duration column'
-
- # Attempts
attempts = columns[5].inner_html if columns[5]
- debug 'parsed attempts column'
+ status_info = columns[6].inner_html.gsub(' ', '') if columns[6]
- # Status info
- status_info = columns[6].inner_html.gsub(' ', '').gsub("\302\240", '') if columns[6]
- debug 'parsed status info column'
-
-
if host && service && status && last_check && duration && attempts && started_at && status_info
service_extinfo_url = "#{extinfo_url}?type=2&host=#{host}&service=#{CGI.escape(service)}"
host_extinfo_url = "#{extinfo_url}?type=1&host=#{host}"
status = Hashie::Mash.new :host => host,
@@ -449,11 +425,8 @@
end
nil
end
- def debug(*args)
- $stderr.puts *args if ENV['DEBUG']
- end
-
end
+
end