Skip to content

Instantly share code, notes, and snippets.

@beezly
Created March 20, 2014 15:25
Show Gist options
  • Save beezly/9666306 to your computer and use it in GitHub Desktop.
Save beezly/9666306 to your computer and use it in GitHub Desktop.
Send me an email when a machine becomes pingable
#!/usr/bin/env ruby
require 'rubygems'
require 'net/ping'
require 'net/smtp'
server=ARGV.first
smtp_server='mailhost.shef.ac.uk'
from_name="Pingy Test Thingy"
from_address="pingy@yourdomain.com"
to_name="Test User"
to_address="a.j.beresford@sheffield.ac.uk"
message=<<MESSAGE_END
From: #{from_name} <#{from_address}>
To: #{to_name} <#{to_address}>
Subject: #{server} came back
Good News Everybody! #{server} is back with us!
MESSAGE_END
pinger=Net::Ping::ICMP.new server
MISSED_PING_HYSTERESIS_LIMIT=3
SECONDS_BETWEEN_PINGS=5
missed_pings=0
already_alerted=false
loop do
if pinger.ping
puts "ACK: #{server}"
unless already_alerted
puts "Sending Mail"
Net::SMTP.start(smtp_server) { |smtp| smtp.send_message(message, from_address, to_address) }
already_alerted=true
missed_pings=0
end
else
puts "NACK: #{server}"
missed_pings++
already_alerted=false if missed_pings > MISSED_PING_HYSTERESIS_LIMIT
end
sleep SECONDS_BETWEEN_PINGS
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment