Skip to content

Instantly share code, notes, and snippets.

@chris3000
Last active March 17, 2017 14:57
Show Gist options
  • Save chris3000/4740404 to your computer and use it in GitHub Desktop.
Save chris3000/4740404 to your computer and use it in GitHub Desktop.
Check if Asterisk is running and notify by email if it's not.
#!/usr/bin/ruby
require 'pony' #pony gem takes care of email
require 'syslog' #syslog logs to /var/log/syslog, the core log file for Ubuntu.
def log(msg)
# $0 is the current script name
Syslog.open($0, Syslog::LOG_PID | Syslog::LOG_CONS) { |s| s.warning msg }
end
ps=`ps ax`
#look for Asterisk in the list of running programs
exists = ps.include?("/usr/sbin/asterisk")
message = "Asterisk is running."
#skip all of this if asterisk is running...
unless exists
message = "Asterisk is NOT running."
begin
Pony.mail({
:to => '[email protected]',
:from => '[email protected]',
:subject => 'Redial Asterisk is down',
:body => 'I can\'t find the asterisk process. Please investigate',
:via => :smtp,
:via_options => {
:address => 'smtp.gmail.com',
:port => '587',
:enable_starttls_auto => true,
:user_name => 'your-gmail-username',
:password => 'your-password',
:authentication => :plain, # :plain, :login, :cram_md5, no auth by default
:domain => "asterisk.yourserver.com" # the HELO domain provided by the client to the server
}
})
rescue Exception => e
message = "Asterisk has failed and I can't send Email! Help!"
end
end
#log the status to syslog.
log message
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment