Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@anderiv
Created February 20, 2012 23:04
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anderiv/1872154 to your computer and use it in GitHub Desktop.
Save anderiv/1872154 to your computer and use it in GitHub Desktop.
nagios git pre-receive hook
#!/bin/bash
while read OLD_SHA1 NEW_SHA1 REFNAME; do
export GIT_WORK_TREE=/tmp/nagiosworkdir
/usr/bin/git checkout -f $NEW_SHA1
# sed -i "s|cfg_dir=CHANGEWITHGIT|cfg_dir=${GIT_WORK_TREE}\/config|g" $GIT_WORK_TREE/etc/nagios.cfg
sed -i "s|cfg_file=\/usr\/local\/nagios\/etc\/objects\/commands.cfg|cfg_file=${GIT_WORK_TREE}\/objects\/commands.cfg|g" $GIT_WORK_TREE/nagios.cfg
sed -i "s|cfg_file=\/usr\/local\/nagios\/etc\/objects\/contacts.cfg|cfg_file=${GIT_WORK_TREE}\/objects\/contacts.cfg|g" $GIT_WORK_TREE/nagios.cfg
sed -i "s|cfg_file=\/usr\/local\/nagios\/etc\/objects\/timeperiods.cfg|cfg_file=${GIT_WORK_TREE}\/objects\/timeperiods.cfg|g" $GIT_WORK_TREE/nagios.cfg
sed -i "s|cfg_file=\/usr\/local\/nagios\/etc\/objects\/templates.cfg|cfg_file=${GIT_WORK_TREE}\/objects\/templates.cfg|g" $GIT_WORK_TREE/nagios.cfg
sed -i "s|cfg_file=\/usr\/local\/nagios\/etc\/objects\/hosts.cfg|cfg_file=${GIT_WORK_TREE}\/objects\/hosts.cfg|g" $GIT_WORK_TREE/nagios.cfg
sed -i "s|cfg_file=\/usr\/local\/nagios\/etc\/objects\/hostgroups.cfg|cfg_file=${GIT_WORK_TREE}\/objects\/hostgroups.cfg|g" $GIT_WORK_TREE/nagios.cfg
sed -i "s|cfg_file=\/usr\/local\/nagios\/etc\/objects\/services.cfg|cfg_file=${GIT_WORK_TREE}\/objects\/services.cfg|g" $GIT_WORK_TREE/nagios.cfg
sudo -u root /bin/chgrp -R nagios $GIT_WORK_TREE
sudo -u root /usr/local/nagios/bin/nagios -v $GIT_WORK_TREE/nagios.cfg > $GIT_WORK_TREE/check.out
NAGIOS_CHECK_STATUS=$?
echo "Nagios Config Check Exit Status:" $NAGIOS_CHECK_STATUS
if [ "$NAGIOS_CHECK_STATUS" -ne 0 ]
then
echo "Your configs did not parse correctly, there was an error. Output follows."
cat $GIT_WORK_TREE/check.out
exit 1
else
echo "Your configs look good and parsed correctly."
exit 0
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment