Last active
December 21, 2015 00:49
-
-
Save mark-cooper/6223158 to your computer and use it in GitHub Desktop.
SIP SERVER: band aid monitoring
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
# BAND AID SIP SERVER MONITOR | |
class SipServerMonitor | |
def initialize(opts = {}) | |
@sip = { | |
start: '/openils/bin/oils_ctl.sh -d /openils/var/run -s /openils/conf/oils_sip.xml -a start_sip', | |
stop: '/openils/bin/oils_ctl.sh -d /openils/var/run -s /openils/conf/oils_sip.xml -a stop_sip', | |
pid_file: '/openils/var/run/oils_sip.pid', | |
pid: nil, | |
name: "SIPServer", | |
warn: 30, | |
log: '/var/log/syslog', | |
email: nil, | |
}.merge! opts | |
@sip[:hostname] = `hostname` | |
end | |
def check_pid(restart = false) | |
pid = `cat #{@sip[:pid_file]}` | |
unless pid.empty? | |
@sip[:pid] = pid.to_i | |
else | |
report("OILS NO PID", "PID NOT FOUND IN #{@sip[:pid_file]}") | |
# restart_server if restart | |
end | |
end | |
def check_no_connections(restart = false) | |
connections = `pgrep -f #{@sip[:name]} | wc -l` | |
unless connections.empty? | |
connections = connections.to_i | |
if connections > @sip[:warn] | |
report("SIP WARN PROCESSES", "SIP WARN PROCESSES EXCEEDED: warn=#{@sip[:warn]}: current=#{connections.to_s}") | |
# restart_server if restart | |
end | |
else | |
report("SIP NO PROCESSES", "NO SIP SERVER PROCESSES WERE FOUND") | |
# restart_server if restart | |
end | |
end | |
def check_pid_match(restart = false) | |
raise "MUST CHECK PID FIRST" unless @sip[:pid] | |
ppid = `pgrep -f #{@sip[:name]} -P 1` | |
unless ppid.empty? | |
ppid = ppid.to_i | |
if ppid != @sip[:pid] | |
report("OILS PID AND SIP PID MISMATCH", "OILS PID AND SIP SERVER PROCID DO NOT MATCH") | |
# restart_server if restart | |
end | |
else | |
report("SIP NO PARENT PROCESS", "SIP SERVER PARENT NOT FOUND") | |
# restart_server if restart | |
end | |
end | |
def grep_log(search) | |
if system( "grep #{search} #{@sip[:log]} > /dev/null" ) | |
report("FOUND #{search}", "FOUND #{search} IN #{@sip[:log]}") | |
end | |
end | |
def report(subject, body) | |
subject += " -- #{@sip[:hostname]}" | |
system( "echo #{body} | mail -s #{subject} #{@sip[:email]}" ) if @sip[:email] | |
end | |
def restart_server | |
system( @sip[:stop] ) | |
system( "pkill -f #{@sip[:name]}" ) | |
system( "rm #{@sip[:pid_file]}" ) | |
system( @sip[:start] ) | |
end | |
end | |
begin | |
ssm = SipServerMonitor.new( { email: 'some.address@somewhere.com' } ) | |
ssm.check_pid # check oils_sip.pid | |
ssm.check_no_connections # check there are processes, but not too many | |
ssm.check_pid_match # check there is a parent and that it matches oils_sip.pid | |
ssm.grep_log 'handle_patron_status' # expected ... error, misconfigured workstation possible | |
rescue Exception => ex | |
puts ex.message | |
end | |
__END__ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This filled a hole for a short while. But really use Monit (or similar): http://mmonit.com/monit/