53 lines
1.3 KiB
Ruby
Executable file
53 lines
1.3 KiB
Ruby
Executable file
#!/usr/bin/env ruby
|
|
|
|
require "puppet"
|
|
require "socket"
|
|
|
|
Puppet.parse_config
|
|
|
|
if Puppet.version[/\d+/].to_i >= 2
|
|
puppetd = "puppet agent"
|
|
pidname = "agent.pid"
|
|
else
|
|
puppetd = "puppetd"
|
|
pidname = "puppetd.pid"
|
|
end
|
|
|
|
# get pid number for puppet or exit if not found
|
|
pidfile = File.join(Puppet.settings.value("rundir"), pidname)
|
|
begin
|
|
pid = File.new(pidfile, "r").read.to_i
|
|
rescue Errno::ENOENT
|
|
exit
|
|
end
|
|
|
|
# check that process is still alive
|
|
begin
|
|
Process.kill(0, pid)
|
|
|
|
# check memory usage and restart if over 256M
|
|
rss = `ps -o rss -p '#{pid}'`.to_a.last.to_i / 1024
|
|
if rss > 256
|
|
printf("puppetd memory usage too high (%d MB), on host '%s' sending HUP\n",
|
|
rss, Socket.gethostname)
|
|
Process.kill("HUP", pid)
|
|
end
|
|
|
|
# check if deadlock file is too old
|
|
dlock = Puppet.settings.value("puppetdlockfile")
|
|
if File.exists?(dlock) and Time.new - File.new(dlock).mtime > 43200
|
|
print "puppetd deadlock file is over 12 hours old, removing it and sending HUP to daemon\n"
|
|
File.unlink(dlock)
|
|
Process.kill("HUP", pid)
|
|
end
|
|
|
|
exit
|
|
rescue Errno::ESRCH
|
|
nil
|
|
end
|
|
|
|
# restart
|
|
printf("puppetd dead but pid file exists on host '%s', restarting\n",
|
|
Socket.gethostname)
|
|
system("service puppet start")
|
|
system(puppetd) if $?.exitstatus == 127
|
|
|