Bash-script-framework: Difference between revisions
(Created page with "<pre> #!/bin/bash #=============================================================================== # # FILE: FILENAME.sh # # USAGE: ./FILENAME.sh < options...") |
No edit summary |
||
Line 22: | Line 22: | ||
====Use function==== | ====Use function==== | ||
<pre> | <pre> | ||
usage (){ | usage (){ | ||
cat << EOF | cat << EOF | ||
Line 39: | Line 33: | ||
EOF | EOF | ||
} | |||
</pre> | |||
====Verify dependencies==== | |||
<pre> | |||
verify_deps() { | |||
# needed="xmllint curl w3m snmptrap cut egrep expr" | |||
needed="snmp_drop_oid.sh grep awk sed" | |||
for i in `echo $needed` | |||
do | |||
type $i >/dev/null 2>&1 | |||
if [ $? -eq 1 ]; then | |||
echo "Status WARNING - I am missing mandatory component: $i"; exit 1 | |||
fi | |||
done | |||
} | |||
</pre> | |||
====Logging framework==== | |||
logger() { | |||
if [ ! -e ${canonicaldirname}/logs ];then | |||
mkdir ${canonicaldirname}/logs | |||
fi | |||
local MESSAGE=${1} | |||
# "Message" "ERR, or UOW" "Context id" | |||
if [ -z ${2} ];then | |||
# If there is no var 2, then this is NOT an error or UOW of some kind. | |||
local LOGGING="INFO" | |||
local CONTEXT1=${RANDOM} | |||
else | |||
local LOGGING="${2}" | |||
if [ -z ${3} ];then | |||
local CONTEXT1=${RANDOM} | |||
else | |||
local CONTEXT1="${3}" | |||
fi | |||
fi | |||
LOGFILE="${canonicaldirname}/logs/$(date +%F)_someFile.log" | |||
touch ${LOGFILE} | |||
chmod 664 ${LOGFILE} | |||
local APP_NAME=`echo $0 | awk -F'/' '{print $NF}'` | |||
#echo "[[\"$(date -u +%FT%H:%M:%S)\", \"Event\", \"\", \"${LOGGING}\", \"`hostname`\", \"${APP_NAME}\", \"$$\", \"0\", \"${CONTEXT1}\", ,\"OPS\", \"${APP_NAME}\"], {\"Event\":\"${MESSAGE}\"}]" >> ${LOGFILE} | |||
echo "\"$(date -u +%FT%H:%M:%S.%3N)\": [[ \"${LOGGING}\", \"$(hostname)\", \"${APP_NAME}\", \"$$\", \"${CONTEXT1}\"], {\"Event\":\"${MESSAGE}\"}]," >> ${LOGFILE} | |||
} | |||
# Begin the log file so JSON is valid | |||
beginlog() { | |||
if [ ! -e ${canonicaldirname}/logs ];then | |||
mkdir ${canonicaldirname}/logs | |||
fi | |||
LOGFILE="${canonicaldirname}/logs/$(date +%F)_someLog.log" | |||
touch ${LOGFILE} | |||
chmod 664 ${LOGFILE} | |||
local DATA=$(tail -1 ${LOGFILE} | wc -m) | |||
# character count is 2 due to carriage return in output | |||
if [[ ${DATA} -gt 2 ]] ;then | |||
echo '}' >> ${LOGFILE} | |||
fi | |||
} | |||
endlog() { | |||
if [ ! -e ${canonicaldirname}/logs ];then | |||
mkdir ${canonicaldirname}/logs | |||
fi | |||
LOGFILE="${canonicaldirname}/logs/$(date +%F)_someLog.log" | |||
touch ${LOGFILE} | |||
chmod 664 ${LOGFILE} | |||
local MESSAGE=${1} | |||
# "Message" "ERR, or UOW" "Context id" | |||
if [ -z ${2} ];then | |||
# If there is no var 2, then this is NOT an error or UOW of some kind. | |||
local LOGGING="INFO" | |||
local CONTEXT1=${RANDOM} | |||
else | |||
local LOGGING="${2}" | |||
if [ -z ${3} ];then | |||
local CONTEXT1=${RANDOM} | |||
else | |||
local CONTEXT1="${3}" | |||
fi | |||
fi | |||
echo "\"$(date -u +%FT%H:%M:%S.%3N)\": [[\"${LOGGING}\", \"$(hostname)\", \"${APP_NAME}\", \"$$\", \"${CONTEXT1}\"], {\"Event\":\"${MESSAGE}\"}]" >> ${LOGFILE} | |||
echo '}' >> ${LOGFILE} | |||
} | } | ||
</pre> | </pre> |
Revision as of 14:58, 15 October 2021
#!/bin/bash #=============================================================================== # # FILE: FILENAME.sh # # USAGE: ./FILENAME.sh < options > # # REQUIREMENTS: ifconfig snmptrap tr sed awk # AUTHOR: Christopher S. Hubbard (), chubbard@iwillfearnoevil.com # COMPANY: I Will Fear No Evil DOT com # VERSION: 2.0.1 # CREATED: 02/18/2011 06:49:51 PM PST # REVISION: Mary #===============================================================================
- Details
- Revision starts at the letter A and goes through M
- Version starts at 0.0.1 and increments when major changes happen
- Requirements are what should be validated before running the script
Use function
usage (){ cat << EOF Usage: $0 options Options: -h show this help screen -x enable debug mode example: EOF }
Verify dependencies
verify_deps() { # needed="xmllint curl w3m snmptrap cut egrep expr" needed="snmp_drop_oid.sh grep awk sed" for i in `echo $needed` do type $i >/dev/null 2>&1 if [ $? -eq 1 ]; then echo "Status WARNING - I am missing mandatory component: $i"; exit 1 fi done }
Logging framework
logger() { if [ ! -e ${canonicaldirname}/logs ];then
mkdir ${canonicaldirname}/logs
fi
local MESSAGE=${1}
- "Message" "ERR, or UOW" "Context id"
if [ -z ${2} ];then # If there is no var 2, then this is NOT an error or UOW of some kind. local LOGGING="INFO" local CONTEXT1=${RANDOM} else local LOGGING="${2}" if [ -z ${3} ];then local CONTEXT1=${RANDOM} else local CONTEXT1="${3}" fi fi
LOGFILE="${canonicaldirname}/logs/$(date +%F)_someFile.log" touch ${LOGFILE} chmod 664 ${LOGFILE}
local APP_NAME=`echo $0 | awk -F'/' '{print $NF}'`
- echo "[[\"$(date -u +%FT%H:%M:%S)\", \"Event\", \"\", \"${LOGGING}\", \"`hostname`\", \"${APP_NAME}\", \"$$\", \"0\", \"${CONTEXT1}\", ,\"OPS\", \"${APP_NAME}\"], {\"Event\":\"${MESSAGE}\"}]" >> ${LOGFILE}
echo "\"$(date -u +%FT%H:%M:%S.%3N)\": [[ \"${LOGGING}\", \"$(hostname)\", \"${APP_NAME}\", \"$$\", \"${CONTEXT1}\"], {\"Event\":\"${MESSAGE}\"}]," >> ${LOGFILE} }
- Begin the log file so JSON is valid
beginlog() { if [ ! -e ${canonicaldirname}/logs ];then
mkdir ${canonicaldirname}/logs
fi
LOGFILE="${canonicaldirname}/logs/$(date +%F)_someLog.log" touch ${LOGFILE} chmod 664 ${LOGFILE} local DATA=$(tail -1 ${LOGFILE} | wc -m)
- character count is 2 due to carriage return in output
if [[ ${DATA} -gt 2 ]] ;then
echo '}' >> ${LOGFILE}
fi } endlog() { if [ ! -e ${canonicaldirname}/logs ];then
mkdir ${canonicaldirname}/logs
fi
LOGFILE="${canonicaldirname}/logs/$(date +%F)_someLog.log" touch ${LOGFILE} chmod 664 ${LOGFILE} local MESSAGE=${1}
- "Message" "ERR, or UOW" "Context id"
if [ -z ${2} ];then # If there is no var 2, then this is NOT an error or UOW of some kind. local LOGGING="INFO" local CONTEXT1=${RANDOM} else local LOGGING="${2}" if [ -z ${3} ];then local CONTEXT1=${RANDOM} else local CONTEXT1="${3}" fi fi echo "\"$(date -u +%FT%H:%M:%S.%3N)\": [[\"${LOGGING}\", \"$(hostname)\", \"${APP_NAME}\", \"$$\", \"${CONTEXT1}\"], {\"Event\":\"${MESSAGE}\"}]" >> ${LOGFILE} echo '}' >> ${LOGFILE}
}
Getopts framework
while getopts "hx" OPTION; do case ${OPTION} in h) usage; exit 0 ;; x) export PS4='+(${BASH_SOURCE}:${LINENO}): ${FUNCNAME[0]:+${FUNCNAME[0]}(): }'; set -v ;set -x ;; *) echo "Status FATAL - Unexpected arguments given. Try -h. Used $@"; exit 2 ;; esac done