Skip to content

Instantly share code, notes, and snippets.

@iDiogenes
Forked from anonymous/gist:158899
Created July 30, 2009 21:39
Show Gist options
  • Save iDiogenes/158949 to your computer and use it in GitHub Desktop.
Save iDiogenes/158949 to your computer and use it in GitHub Desktop.
#!/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
class Server
def initialize(servers = $svrs)
@servers = servers
end
end
##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
#!/usr/bin/env ruby
require 'net/ssh'
class Server
attr_accessor :server
def initialize(server_name)
@server = server_name
end
def mnt_chk(ssh)
uname = ssh.exec!("uname")
if uname.nil?
return "Failed to connect to #{@server} during mount check."
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
true
else
return "A mount on #{@server} is down or hung."
end
end
end
def syslg_chk(ssh)
uname = ssh.exec!("uname")
if uname.nil?
return "Failed to connect to #{@server} during syslog check."
else
syserr = ssh.exec!("egrep '(reject|warning|error|fatal|panic|err)' #{$smtp_log}")
if syserr.nil?
true
else
return syserr
end
end
end
def smtp_chk(ssh)
uname = ssh.exec!("uname")
if uname.nil?
return "Failed to connect to #{@server} during smtp check."
else
mailerr = ssh.exec!("egrep '(reject|warning|error|fatal|panic|err)' #{$smtp_log}")
if mailerr.nil?
true
else
return mailerr
end
end
end
end
#!/usr/bin/env ruby
class Site
attr_accessor :html_site, :jsp_site
def initialize(html_site, jsp_site)
@html_site = html_site
@jsp_site = jsp_site
end
def www_site
sess = Patron::Session.new
sess.timeout = 1
sess.base_url = @html_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
def java_site
sess = Patron::Session.new
sess.timeout = 6
sess.base_url = @jsp_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
#
#Config File for artemis.rb v1.x (JD Trout)
#
# All settings with multiple entries should be separated by a space only.
# Location of where artemis should log
log_location = "/var/log/artemis.log"
# SMTP Sever used to send email when an error occures (Best to not have this be the same server that you are checking for errors)
email_server = smtp.loni.ucla.edu
# The email address which which you want send from
email_from = artemis@loni.ucla.edu
# The email address where errors will be emailed
email_to = jd.trout@loni.ucla.edu
# Servers that require nfs and syslog checks
servers = viola-rs1.loni.ucla.edu viola-rs2.loni.ucla.edu viola-local.loni.ucla.edu aragon-rs1.loni.ucla.edu aragon-rs2.loni.ucla.edu aragon-rs3.loni.ucla.edu aragon-rs4.loni.ucla.edu aragon-rs5.loni.ucla.edu
# Servers in here will have /var/log/maillog scanned for errors. (Should not be confused with email_server)
smtp_servers = netserv1.loni.ucla.edu netserv2.loni.ucla.edu
# SMTP logs. The location of the smtp logs.
smtp_log = /var/log/maillog
# HTML sites. This value is for all websites that are not JSP
sites = www.loni.ucla.edu cms.loni.ucla.edu
#
# This is the URL
# Example:
# check_java = check.html
check_html = check.html
# This value is for JAVA web apps (glassfish/tomcat) sites
java_sites = ida.loni.ucla.edu utilities.loni.ucla.edu services.loni.ucla.edu
#
# This is the URL
# Example:
# check_java = index.jsp
check_java = check.jsp
loop do
if $svrs != nil
server = nil
begin
$svrs.each do |server|
Timeout::timeout(20) do
Server.new(server)
Server.mnt_chk(ssh)
Server.syslg_chk(ssh)
end
end
rescue Timeout::Error
$log.error("Timeout Error: Check network connection and/or NFS mounts on #{server}")
end
sleep 120
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment