-
Notifications
You must be signed in to change notification settings - Fork 0
/
puppethealth.sh
executable file
·50 lines (47 loc) · 1.88 KB
/
puppethealth.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/bin/bash
#==============================================================================
# Copyright (c) 2018 zer0labs.net
# All rights reserved.
#==============================================================================
#
# Name: puppethealth.sh
# Type: shell script
# Authors: Trevor Viljoen <[email protected]>
#
# Purpose: To check the status of the puppet agent, discovering stale
# lock files and process ids.
#
#==============================================================================
PATH=$PATH:/opt/puppetlabs/puppet/bin
current_time=$(date +'%s')
statedir=$(puppet config print statedir)
last_run="$(cat ${statedir}/last_run_summary.yaml | grep last_run | cut -d: -f2 | tr -d ' ')"
last_run_date="$(date -d @${last_run})"
run_differential=$((${current_time} - ${last_run}))
hours=2
threshold=$((${hours} * 60 * 60)) #threshold in seconds
logit() {
logger -t $(basename $0) -i -p $1 $2
}
# if puppet has not run in the alloted time
if [[ "${run_differential}" -ge "${threshold}" ]]
then
if [ -f ${statedir}/agent_catalog_run.lock ]; then
pid="$(cat ${statedir}/agent_catalog_run.lock)"
ptime="$(ps -p ${pid} -o etime= | tr -d ' ')"
message="[WARN] Puppet process has been running for ${ptime}. Killing pid: ${pid}."
logit "auth.warn" "${message}"
kill -9 $pid
else
if [ $(ps -ef | grep puppet | grep onetime) ]; then
message="[ERROR] Possible hung puppet agent requires further investigation. Puppet last checked in: ${last_run_date}."
logit "auth.err" "${message}"
else
message="[ERROR] Missing lock file requires further investigation. Puppet last checked in: ${last_run_date}."
logit "auth.err" "${message}"
fi
fi
else
message="[INFO] Puppet ran ${run_differential} seconds ago, at ${last_run_date}."
logit "auth.info" "${message}"
fi