Harvesting facter values - puppet_watchdog
This script is meant for harvesting facter -p values from the puppet agents. It lives as in a process and pulls data from $PUPPET_YAML_HOME, then, it bump the data onto a sqlite db. This script should be setup at the puppetmaster node only.
It is good especially when you need to do some data analysis on your agents.
At the same times, you can also write your own facter codes and retrieve customized information of your agent nodes.
#!/bin/bash
# Hiu, Yen Onn
# This is the watchdog script that will harvest
# all the data of yaml into a db
PUPPET_YAML_HOME="/var/lib/puppet/yaml/node"
PUPPET_WATCHDOG_LOG="/var/log/puppet_watchdogd.log"
HADOOP_DB="/etc/puppet/web/test.db"
LATEST_PUPPET_NODE=""
[[ ! -e $PUPPET_WATCHDOG_LOG ]] && touch $PUPPET_WATCHDOG_LOG
function control_c()
{
echo -en "\n**** process `basename $0` is terminated. ****\n"
exit $?
}
function log()
{
while read data
do
echo "[$(date +"%D %T")] $data" >> $PUPPET_WATCHDOG_LOG
done
}
function harvest_data()
{
sleep 2
FILE="$PUPPET_YAML_HOME/$LATEST_PUPPET_NODE"
SQLITE_BIN="/usr/bin/sqlite3"
UPTIME=`grep "uptime_days" $FILE | awk '{print $2}'|sed -e 's/"//g'`
PROCESSERCOUNT=`grep "physicalprocessorcount" $FILE | awk '{print $2}'|sed -e 's/"//g'`
CLIENTCERT=`grep clientcert $FILE | awk '{print $2}'`
TIMESTAMP=`grep expiration $FILE | awk '{print $2 " " $3}'`
if [[ -e $HADOOP_DB ]]
then
SQL_CMD=""
# Query if it is a new record
SQL_CMD_NEW="SELECT hostname FROM PUPPET_FACTER WHERE hostname='$CLIENTCERT';"
newrecord=`$SQLITE_BIN $HADOOP_DB "$SQL_CMD_NEW"`
if [[ -z "$newrecord" ]]
then
SQL_CMD="INSERT INTO PUPPET_FACTER (hostname,timestamp,uptime,physicalprocessorcount) \
VALUES ('$CLIENTCERT','$TIMESTAMP','$UPTIME','$PROCESSERCOUNT');"
else
SQL_CMD="UPDATE PUPPET_FACTER SET uptime='$UPTIME', \
physicalprocessorcount='$PROCESSERCOUNT', \
timestamp='$TIMESTAMP' \
WHERE hostname='$CLIENTCERT';"
fi
if [[ -z "$SQL_CMD" ]]
then
echo "SQL String is null" | log
return false
fi
if ! `$SQLITE_BIN $HADOOP_DB "$SQL_CMD"`
then
return false
fi
else
exit 1
fi
}
# Main run
trap control_c SIGINT
while true
do
NODE=`ls -1t $PUPPET_YAML_HOME | grep -i yaml | head -n1`
if [[ ! -z "$NODE" ]] && [[ "$NODE" != "$LATEST_PUPPET_NODE" ]]
then
LATEST_PUPPET_NODE=$NODE
if harvest_data
then
echo -en "Data harvesting from $LATEST_PUPPET_NODE ...... [DONE]\n" | log
else
echo -en "Data harvesting from $LATEST_PUPPET_NODE ...... [FAILED]\n" | log
fi
fi
done
No comments:
Post a Comment