Skip to content

Instantly share code, notes, and snippets.

@bensie
Forked from anonymous/gist:160878
Created August 3, 2009 23:15
Show Gist options
  • Save bensie/160881 to your computer and use it in GitHub Desktop.
Save bensie/160881 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'
require 'yaml'
##Globals##
raw_config = File.read('/etc/artemis.yml')
$config = YAML.load(raw_config).symbolize_keys
puts @config[:log_location] #$log_file = config.get_value('log_location') #loggin info
$log = Logger.new('log_file', 'monthly')
#email settings
puts @config[:email_server] #$email_svr = config.get_value('email_server') #smtp server info
puts @config[:email_from] #$email_from = config.get_value('email_from') #email address in which email is sent from
puts @config[:email_to] #$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
$svr_log = config.get_value('server_log')
$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_s.split(' ') #split smtp server string into an array
$smtp_log = config.get_value('smtp_logs') #get location of smtp logs
# Build config
n = 0
$svrs.each do |s| #creating array of server with hash for the different values
server[n] = Hash.new
server[n][:name] = s
server[n][:status] = true
server[n][:log] = $svr_log
n += 1
end
n = 0 #reset n to 0 for the iteration
$smtp_svr.each do |s|
smtp_svr[n] =Hash.new
smtp_svr[n][:name] = s
smtp_svr[n][:status] = true
n += 1
end
#### Temp Clases
class Resource
def check(ssh)
end
end
class Server < Resource
#class Server
attr_accessor :resources
def initialize(resources)
@resources = resources
end
ssh = Net::SSH.start("#{@server}", 'tester', :password => "!@retset")
def check(ssh)
begin
mnt_check_result = mnt_chk(ssh)
syslog_check_result = syslog_chk(ssh)
result = []
if mnt_check_result != true
result << mnt_check_result
end
if syslog_check_result != true
result << syslog_check_result
end
unless result
return true
else
return result
end
ensure
ssh.close
end
end
private
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 #{a} is down or hung."
return "A mount on #{@server} is down or hung."
end
end
end
def syslog_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)' /var/log/messages")
if syserr.nil?
# true
return puts "no syslog erros"
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
### end classes
#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
$svrs.each do
begin
Timeout::timeout(10) do
output = Server.new(resources)
results = output.check(resources)
results.each do |result|
#$log.error(result)
if output == nil
puts "everything is cool"
else
puts result
end
end
end
rescue Timeout::Error
$log.error("Timeout Error: Check network connection and/or NFS mounts on #{resource}")
puts(email_prg "Timeout Error: Check network connection and/or NFS mounts on #{resource}")
end
end
#sleep 120
end
# end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment