Created
July 30, 2009 20:17
-
-
Save anonymous/158899 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'rubygems' | |
require 'logger' | |
require 'net/ssh' | |
require 'net/smtp' | |
require 'patron' | |
require 'timeout' | |
require 'parseconfig' | |
##Globals## | |
config = ParseConfig.new('/etc/artemis.conf') #config file location | |
$log_file = config.get_value('log_location') #loggin info | |
$log = Logger.new('log_file', 'monthly') | |
#email settings | |
$email_svr = config.get_value('email_server') #smtp server info | |
$email_from = config.get_value('email_from') #email address in which email is sent from | |
$email_to = config.get_value('email_to') #email address where the errors will be sent | |
svr = config.get_value('servers') #get servers | |
$svrs = svr.to_s.split(' ') #split the server string into an array | |
$www_doc = config.get_value('check_html') #get html/php test file | |
$jsp_doc = config.get_value('check_java') #get jstp test file | |
www = config.get_value('sites') #parseconf only passes strings | |
$www_sites = www.to_s.split(' ') #split the www string into an array | |
jsp = config.get_value('java_sites') #get jsp/tomcat sites | |
$jsp_sites = jsp.to_s.split(' ') #split jsp sites the string into an array | |
smtp = config.get_value('smtp_servers') #get smtp servers | |
$smtp_svr = smtp.to_ssplit(' ') #split smtp server string into an array | |
$smtp_log = config.get_value('smtp_logs') #get location of smtp logs | |
##Methods## | |
#check server mount points | |
def mnt_chk | |
server = nil | |
$svrs.each do |server| | |
Timeout::timeout(20) do | |
Net::SSH.start("#{server}", 'tester', :password => "!@retset") do |ssh| | |
uname = ssh.exec!("uname") | |
$log.info("Checking mounts system #{server}") | |
if uname == nil | |
$log.error("Cannot connect to #{server}") | |
puts(email_prg "Cannot connect to #{server}") | |
else | |
mntchk = ssh.exec!("mount | awk '{print $5}' | grep nfs | wc | awk '{print $1}'") | |
fstabchk = ssh.exec!("cat /etc/fstab | awk '{print $3}' | grep nfs | wc | awk '{print $1}'") | |
if mntchk != fstabchk | |
$log.error("A mount on #{server} is down.") | |
puts(email_prg "A mount on #{server} is down.") | |
end | |
end | |
end | |
end | |
end | |
rescue Timeout::Error | |
$log.error("Timeout Error: Check network connection and/or NFS mounts on #{server}") | |
puts(email_prg "Timeout Error: Check network connection and/or NFS mounts on #{server}") | |
end | |
#check syslog for errors | |
def syslg_chk | |
server = nil | |
$svrs.each do |server| | |
Timeout::timeout(15) do | |
Net::SSH.start("#{server}", 'tester', :password => "!@retset") do |ssh| | |
uname = ssh.exec!("uname") | |
$log.info("Checking logs on #{server}") | |
syserr = ssh.exec!("egrep '(reject|warning|error|fatal|panic|err)' #{$smtp_log}") | |
#checking smtp logs | |
if $smtp_svr != nil | |
mailerr = ssh.exec!("egrep '(reject|warning|error|fatal|panic|err)' #{$smtp_log}") | |
if mailerr != nil | |
$log.error("#{mailerr} on #{$mntsvr}") | |
puts(email_prg "#{mailerr} on #{$mntsvr}") | |
end | |
end | |
end | |
end | |
end | |
rescue Timeout::Error | |
$log.error("Timeout out while checking logs on #{server}") | |
puts(email_prg "Timeout out while checking logs on #{server}") | |
end | |
#checking websites | |
def web_chk | |
if $www_sites and $www_doc != nil | |
$www_sites.each do |site| | |
$log.info "Checking site #{site}" | |
sess = Patron::Session.new | |
sess.timeout = 1 | |
sess.base_url = "#{site}" | |
resp = sess.get($www_doc) | |
if resp.status > 400 | |
$log.error("#{site} is down.") | |
$log.error(resp.body) | |
else | |
$log.info("#{site} is up.") | |
end | |
end | |
end | |
if $jsp_sites and $jsp_doc != nil | |
$jsp_sites.each do |site| | |
$log.info "Checking site #{site}" | |
sess = Patron::Session.new | |
sess.timeout = 6 | |
sess.base_url = "#{site}" | |
resp = sess.get($jsp_doc) | |
if resp.status > 400 | |
$log.error("#{site} is down:") | |
$log.error(resp.body) | |
else | |
$log.info("#{site} is up.") | |
end | |
end | |
end | |
end | |
#email program | |
def email_prg(msgstr) | |
Net::SMTP.start($email_svr, 25) do |smtp| | |
smtp.send_message msgstr, '$email_from', '$email_to' | |
end | |
end | |
##Daemon## | |
loop do | |
if $svrs != nil | |
mnt_chk | |
syslg_chk | |
end | |
if $www_sites or $jsp_sites != nil | |
web_chk | |
end | |
sleep 120 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment