Skip to content

Instantly share code, notes, and snippets.

@aluetjen
Created January 13, 2014 13:49
Show Gist options
  • Save aluetjen/8400615 to your computer and use it in GitHub Desktop.
Save aluetjen/8400615 to your computer and use it in GitHub Desktop.
Check Puppet Agent
#!/usr/bin/python
import datetime
import os
import argparse
import logging
import sh
FORMAT = '%(asctime)-15s %(levelname)s %(message)s'
logging.basicConfig(format=FORMAT, level=logging.INFO)
LOG = logging.getLogger()
parser = argparse.ArgumentParser()
parser.add_argument('--lock_file', default='/var/lib/puppet/state/agent_catalog_run.lock')
parser.add_argument('--maxage', type=int, default=3600, help="Maximum age in seconds after which to kill the process.")
args = parser.parse_args()
if os.path.exists(args.lock_file):
LOG.info("Found lock file %s", args.lock_file)
lock_modification_time = datetime.datetime.fromtimestamp(os.path.getmtime(args.lock_file))
LOG.info("Mod time=%s", lock_modification_time)
with open(args.lock_file, 'r') as lock_file:
lock_pid = int(lock_file.read().strip())
LOG.info("PID=%s", lock_pid)
age = datetime.datetime.now() - lock_modification_time
LOG.info("Age=%s", age.seconds)
if age.seconds > args.maxage:
LOG.info("Kill and restart...")
sh.killall('puppet', _ok_code=[0, 1])
try:
os.remove(args.lock_file)
except:
LOG.error("Failed to delete lock file.")
LOG.info(sh.service.puppet.restart())
else:
try:
LOG.info(sh.service.puppet.status())
except sh.ErrorReturnCode_3:
LOG.info(sh.service.puppet.start())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment