lib/nagiosharder.rb in nagiosharder-0.1.2 vs lib/nagiosharder.rb in nagiosharder-0.1.3
- old
+ new
@@ -1,8 +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 'active_support/version' # double and triplely ensure ActiveSupport::VERSION is around
if ActiveSupport::VERSION::MAJOR > 2
require 'active_support/core_ext/array'
@@ -58,10 +60,41 @@
response = post(cmd_url, :body => request)
response.code == 200 && response.body =~ /successful/
end
+ def schedule_service_downtime(host, service, options = {})
+ request = {
+ :cmd_mod => 2,
+ :cmd_typ => 56,
+ :com_author => options[:author] || "#{@user} via nagiosharder",
+ :com_data => options[:comment] || 'scheduled downtime by nagiosharder',
+ :host => host,
+ :service => service,
+ :trigger => 0
+ }
+
+ request[:fixed] = case options[:type].to_sym
+ when :fixed then 1
+ when :flexible then 0
+ else 1
+ end
+
+
+ if request[:fixed] == 0
+ request[:hours] = options[:hours]
+ request[:minutes] = options[:minutes]
+ end
+
+ request[:start_time] = formatted_time_for(options[:start_time])
+ request[:end_time] = formatted_time_for(options[:end_time])
+
+ response = post(cmd_url, :body => request)
+
+ response.code == 200 && response.body =~ /successful/
+ end
+
def schedule_host_downtime(host, options = {})
request = {
:cmd_mod => 2,
:cmd_typ => 55,
:com_author => options[:author] || "#{@user} via nagiosharder",
@@ -71,17 +104,17 @@
:trigger => 0
}
# FIXME we could use some option checking...
- request[:type] = case options[:type].to_sym
- when :fixed then 1
- when :flexible then 0
- else 1 # default to fixed
- end
+ request[:fixed] = case options[:type].to_sym
+ when :fixed then 1
+ when :flexible then 0
+ else 1 # default to fixed
+ end
- if request[:type] == 0
+ if request[:fixed] == 0
request[:hours] = options[:hours]
request[:minutes] = options[:minutes]
end
request[:start_time] = formatted_time_for(options[:start_time])
@@ -221,10 +254,14 @@
def cmd_url
"#{nagios_url}/cmd.cgi"
end
+ def extinfo_url
+ "#{nagios_url}/extinfo.cgi"
+ end
+
private
def formatted_time_for(time)
if @version.to_i < 3
@@ -273,28 +310,30 @@
end
attempts = columns[5].inner_html if columns[5]
status_info = columns[6].inner_html.gsub(' ', '') if columns[6]
- status = {
- :host => host,
- :service => service,
- :status => status,
- :last_check => last_check,
- :duration => duration,
- :attempts => attempts,
- :started_at => started_at,
- :extended_info => status_info,
- :acknowledged => acknowledged
- }
-
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)}"
+
+ status = Hashie::Mash.new :host => host,
+ :service => service,
+ :status => status,
+ :last_check => last_check,
+ :duration => duration,
+ :attempts => attempts,
+ :started_at => started_at,
+ :extended_info => status_info,
+ :acknowledged => acknowledged,
+ :extinfo_url => service_extinfo_url
+
yield status
end
end
end
nil
end
end
+
end