265 lines
7.0 KiB
Plaintext
265 lines
7.0 KiB
Plaintext
# bashlib
|
|
# frequently used shell/bash functions
|
|
|
|
|
|
# **** check for root privileges ****
|
|
amiroot()
|
|
{
|
|
local user=$(whoami)
|
|
case $1 in
|
|
not) #not needed
|
|
log debug "amiroot - checking for unnecessary root privileges" #debug log
|
|
if [ "$(whoami)" = "root" ]; then
|
|
log error "amiroot - you probably should not run this as root. it could mess up your karma."
|
|
exit 1
|
|
fi
|
|
;;
|
|
|
|
*) #required
|
|
log debug "amiroot - checking for necessary root privileges" #debug log
|
|
if [ "$(whoami)" != "root" ]; then
|
|
log error "amiroot - $USER, you need to gain root privileges to do this."
|
|
exit 1;
|
|
fi
|
|
;;
|
|
esac
|
|
}
|
|
|
|
# **** generate timestamp ****
|
|
gettimestamp()
|
|
{
|
|
if [ -z $1 ]; then
|
|
date '+%Y%m%d%H%M'
|
|
elif [ $1 = "nice" ]; then
|
|
date '+%d.%m.%Y %H:%M'
|
|
elif [ $1 = "short" ]; then
|
|
date '+%Y%m%d'
|
|
fi
|
|
}
|
|
|
|
# **** bashtrap ****
|
|
bashtrap()
|
|
{
|
|
log debug "bashtrap - triggered"
|
|
clear
|
|
echo "CTRL+C detected.. exiting!"
|
|
exit 1
|
|
}
|
|
|
|
|
|
# this gives your bash scripts the ability to get updated
|
|
# basically it does a simple git pull
|
|
|
|
# **** help ****
|
|
# global vars needed: $repopath, $bashlibpath
|
|
# it depends on bashlib
|
|
|
|
|
|
# **** update (git pull) ****
|
|
update()
|
|
{
|
|
|
|
# checking for root privileges
|
|
amiroot
|
|
|
|
# update start message
|
|
log info "update - starting update of $logwhat and bashlib"
|
|
|
|
# bashlib update
|
|
cd $bashlibpath
|
|
git pull origin master | grep "files changed"
|
|
local returncode=$?
|
|
if [ $returncode = "0" ]; then
|
|
log info "update - bashlib has been updated"
|
|
else
|
|
log info "update - bashlib is already up-to-date"
|
|
fi
|
|
|
|
# update
|
|
cd $repopath
|
|
git pull origin master | grep "files changed"
|
|
local returncode=$?
|
|
if [ $returncode = "0" ]; then
|
|
log info "update - $logwhat has been updated"
|
|
else
|
|
log info "update - $logwhat is already up-to-date"
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
# **** dialog helper ****
|
|
graph()
|
|
{
|
|
local bgtitle="kvm-tools $version | $1"
|
|
|
|
if [ $2 = "--inputbox" ]; then
|
|
local size="7 80"
|
|
elif [ $2 = "--yesno" ]; then
|
|
local size="20 80"
|
|
elif [ $2 = "--fselect" ]; then
|
|
local size="12 80"
|
|
else
|
|
local size="20 80 14"
|
|
fi
|
|
|
|
dialog --backtitle "$bgtitle" --no-cancel "$2" "$3" $size $4 2> /tmp/dialog
|
|
local returncode=$?
|
|
clear
|
|
dialogresult=$(cat /tmp/dialog)
|
|
rm /tmp/dialog
|
|
return $returncode
|
|
|
|
}
|
|
|
|
|
|
# log engine
|
|
|
|
# **** help ****
|
|
# this is basically a collection of bash functions
|
|
# for making a decent logging in a bash script
|
|
# amazingly easy.
|
|
#
|
|
# in order to use this little logging script
|
|
# you need to have some global variables defined
|
|
# loglevel=<0-4>
|
|
# 0 -- no logging
|
|
# 1 -- just errors
|
|
# 2 -- errors & warnings
|
|
# 3 -- even infos (something like "user was created")
|
|
# 4 -- debug (more than everything)
|
|
#
|
|
# logfile="/path/to/logfile.log"
|
|
# well.. should be understandable..
|
|
#
|
|
# logwhat="kvm-tools"
|
|
# tells the engine what is actually been logged (example: name of calling script)
|
|
# just used for syslog
|
|
#
|
|
# log2syslog="0|1"
|
|
# (de)activates logging to the syslog deamen
|
|
#
|
|
# log2stdout="0|1"
|
|
# (de)activates logging to stdout
|
|
# useful if you don't want to deal with output in your script
|
|
#
|
|
# log2file="0|1"
|
|
# (de)activates logging to logfile
|
|
#
|
|
#
|
|
# usage:
|
|
# log error|warning|info|debug "log message"
|
|
|
|
# **** log function ****
|
|
log()
|
|
{
|
|
# getting local hostname
|
|
local hostname=$(cat /etc/hostname)
|
|
|
|
# if no loglevel is defined the script will end
|
|
if [ -z $loglevel ]; then
|
|
echo "logengine[error]: no loglevel defined"
|
|
exit 1
|
|
|
|
# if loglevel is 0, skip logging
|
|
elif [ $loglevel = "0" ]; then
|
|
# logging is disabled
|
|
return 0
|
|
|
|
# loglevel has to be something between 0 and 5
|
|
elif [ ! $loglevel -gt "0" -o ! $loglevel -lt "5" ]; then
|
|
# loglevel unknown
|
|
echo "logengine[error]: unknown loglevel"
|
|
exit 1
|
|
fi
|
|
|
|
|
|
# stdout routine
|
|
if [ -z $log2stdout ]; then
|
|
log2stdout="0"
|
|
elif [ $log2stdout = "0" ]; then
|
|
# stdout is disabled
|
|
:
|
|
elif [ $log2stdout = "1" ]; then
|
|
# here goes the actual logging process (stdout)
|
|
if [ $1 = "error" -a $loglevel -gt "0" ]; then
|
|
# errors
|
|
echo "[$1]: $2"
|
|
elif [ $1 = "warning" -a $loglevel -gt "1" ]; then
|
|
# warnings
|
|
echo "[$1]: $2"
|
|
elif [ $1 = "info" -a $loglevel -gt "2" ]; then
|
|
# informational
|
|
echo "[$1]: $2"
|
|
elif [ $1 = "debug" -a $loglevel -gt "3" ]; then
|
|
# debug messages
|
|
echo "[$1]: $2"
|
|
fi
|
|
fi
|
|
|
|
# syslog routine
|
|
if [ -z $log2syslog ]; then
|
|
log2syslog="0"
|
|
elif [ $log2syslog = "0" ]; then
|
|
# syslog is disabled
|
|
:
|
|
elif [ -z $logwhat ]; then
|
|
# for syslogging logwhat has to be defined
|
|
echo "logengine[error]: logwhat not defined"
|
|
elif [ $log2syslog = "1" ]; then
|
|
# here goes the actual logging process (syslog)
|
|
if [ $1 = "error" -a $loglevel -gt "0" ]; then
|
|
# errors
|
|
logger -t "$logwhat[$1]" "$2"
|
|
elif [ $1 = "warning" -a $loglevel -gt "1" ]; then
|
|
# warnings
|
|
logger -t "$logwhat[$1]" "$2"
|
|
elif [ $1 = "info" -a $loglevel -gt "2" ]; then
|
|
# informational
|
|
logger -t "$logwhat[$1]" "$2"
|
|
elif [ $1 = "debug" -a $loglevel -gt "3" ]; then
|
|
# debug messages
|
|
logger -t "$logwhat[$1]" "$2"
|
|
fi
|
|
fi
|
|
|
|
# logfile routine
|
|
if [ -z $log2file ]; then
|
|
log2file="0"
|
|
elif [ $log2file = "0" ]; then
|
|
# file is disabled
|
|
:
|
|
elif [ $log2file = "1" ]; then
|
|
if [ -z $logfile ]; then
|
|
# if no logfile is defined the script will end
|
|
echo "logengine[error]: no logfile defined"
|
|
exit 1
|
|
elif ! [ -a $logfile ]; then
|
|
echo "logengine[warning]: logfile not found"
|
|
echo "logengine[info]: creating new logfile"
|
|
touch $logfile
|
|
if [ $? != 0 ]; then
|
|
# probably a permission denied error
|
|
echo "logengine[error]: could not create logfile."
|
|
exit 1
|
|
fi
|
|
fi
|
|
# here goes the actual logging process (logfile)
|
|
if [ $1 = "error" -a $loglevel -gt "0" ]; then
|
|
# errors
|
|
echo "$(gettimestamp nice) [$1]: $2" >> $logfile
|
|
elif [ $1 = "warning" -a $loglevel -gt "1" ]; then
|
|
# warnings
|
|
echo "$(gettimestamp nice) [$1]: $2" >> $logfile
|
|
elif [ $1 = "info" -a $loglevel -gt "2" ]; then
|
|
# informational
|
|
echo "$(gettimestamp nice) [$1]: $2" >> $logfile
|
|
elif [ $1 = "debug" -a $loglevel -gt "3" ]; then
|
|
# debug messages
|
|
echo "$(gettimestamp nice) [$1]: $2" >> $logfile
|
|
fi
|
|
fi
|
|
|
|
}
|
|
|