Skip to content

Instantly share code, notes, and snippets.

@davidkrider
Last active November 14, 2024 04:48
Show Gist options
  • Save davidkrider/849139 to your computer and use it in GitHub Desktop.
Save davidkrider/849139 to your computer and use it in GitHub Desktop.
Check Nagios Script
#!/usr/bin/env ruby
require 'net/https'
require 'net/smtp'
username = 'username'
password = 'password'
# If you're accessing through SSL:
#http = Net::HTTP.new("nagios.domain.com", 443)
#http.use_ssl = true
# Otherwise:
http = Net::HTTP.new("nagios.domain.com", 80)
http.use_ssl = false
http.start do |http|
req = Net::HTTP::Get.new("/nagios3/cgi-bin/status.cgi?host=all",
{"User-Agent" => "nagios_checker"})
req.basic_auth(username, password)
response = http.request(req)
resp = response.body
host_problems_flag = service_problems_flag = false
msg = ""
msg << "From: Nagios <[email protected]>\n"
msg << "To: Network Operations <[email protected]>\n"
status = resp.scan(/Notifications are disabled/)
if status.size > 0
msg << "Subject: Nagios Status: WARNING!\n"
msg << "\n"
msg << "WARNING: ALL notifications are DISABLED!\n"
else
msg << "Subject: Nagios Status\n"
msg << "\n"
end
host = service = nil
hosts = resp.scan(/A HREF='extinfo.cgi\?type=1&(.*?)'><IMG SRC=.*ALT='Notifications/)
msg << "\n*** Notifications are disabled for the following hosts ***\n"
hosts.each do |h|
h.to_s.split("&").each do |e|
key, value = e.split("=")
msg << "Host: #{value}\n"
end
host_problems_flag = true
end
services = resp.scan(/A HREF='extinfo.cgi\?type=2&(.*?)'><IMG SRC=.*ALT='Notifications/)
msg << "\n*** Notifications are disabled for the following services ***\n"
services.each do |s|
s.to_s.split("&").each do |e|
key, value = e.split("=")
if key == 'host'
host = value
elsif key == 'service'
service = value.gsub('+', ' ')
end
end
msg << "Host: #{host}, Service: #{service}\n"
service_problems_flag = true
end
if host_problems_flag || service_problems_flag
smtp = Net::SMTP.start('mail.domain.com', 25)
smtp.send_message msg, '[email protected]', '[email protected]'
smtp.finish
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment