Skip to content

Instantly share code, notes, and snippets.

@aerosol
Created February 7, 2011 17:44
Show Gist options
  • Select an option

  • Save aerosol/814812 to your computer and use it in GitHub Desktop.

Select an option

Save aerosol/814812 to your computer and use it in GitHub Desktop.
A simple shell script for environment testing
#!/bin/bash
echo -e "\033[1mStarting environment audit\033[0m"
echo "----------------------------------------------------------------------------"
ERRORS=0
function h {
echo -e " > $1 ... \c"
}
function warn {
echo -e " \033[4mWARNING! $1\033[0m"
ERRORS=$[ERRORS+1]
}
function crit {
warn $1
ERRORS=$[ERRORS+1]
exit 1
}
function ok {
echo -e "\033[1mOK\033[0m"
}
function pyimports {
h "Python import test: \033[1m$1\033[0m"
/usr/bin/env python <<EOP
import sys
try:
import $1
exit(0)
except Exception, e:
exit(1)
EOP
if [ $? -eq 1 ]
then
warn "Import failed!"
else
ok
fi
}
function runs {
h "Checking \033[1m$1\033[0m process"
if ps ax | grep -v grep | grep $1 > /dev/null
then
ok
else
if [ "$2" == "brute" ]
then
crit "$1 is probably down!"
else
warn "$1 is probably down!"
fi
fi
}
function extrun {
h "Running external test: \033[1m$1\033[0m"
HEADER=`head -n1 $1`
if [ ${HEADER:0:2} == "#!" ]
then
INTERPRET=`head -n1 $1 | cut -d ! -f 2`
$INTERPRET $1
elif [ -x $1 ]
then
./$1
else
warn "Don't know how to execute $1. No shebang or +x."
return
fi
EXIT_CODE=$?
if [ $EXIT_CODE -gt 0 ]
then
warn "Exit code: $EXIT_CODE"
else
ok
fi
}
source $1
echo "----------------------------------------------------------------------------"
echo -e "\033[1mAudit finished with $ERRORS error(s)\033[0m."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment