Resolves merge request 6

This commit is contained in:
Lutchy Horace 2021-02-17 11:24:46 -05:00
parent c0a0c8c1ff
commit cb97128fb0
2 changed files with 98 additions and 38 deletions

View file

@ -1,7 +1,7 @@
#!/usr/bin/env bash
#
# Collection of reusable functions and variables
#
#
TODAY=`date`
HOSTNAME=`hostname`
@ -19,6 +19,37 @@ function err
exit 1
}
function warn
{
echo -e "WARNING: $1"
}
function validate_host
{
echo "$1" | grep -P '^(http|https):\/\/(([a-z0-9]|[a-z0-9][a-z0-9\-]*[a-z0-9])\.)*([a-z0-9]|[a-z0-9][a-z0-9\-]*[a-z0-9])(:[0-9]+)?$'
if [ $? -eq 0 ]; then
curl $1 > /dev/null 2>&1
fi
return $?
}
function validate_ip
{
local ip=$1
local stat=1
if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
OIFS=$IFS
IFS='.'
ip=($ip)
IFS=$OIFS
[[ ${ip[0]} -le 255 && ${ip[1]} -le 255 \
&& ${ip[2]} -le 255 && ${ip[3]} -le 255 ]]
stat=$?
fi
return $stat
}
function send_notification
{
opt=""
@ -26,7 +57,7 @@ function send_notification
local mailcmd=$(readlink -f `which mail`)
if [ "${mailcmd}" = "/usr/bin/bsd-mailx" ]; then
opt="-a 'From: ${FROM}'"
else
else
opt="-r $FROM"
fi
fi
@ -37,20 +68,20 @@ function send_notification
function check_values
{
a_name=$1[@]
a_array=("${!a_name}")
if [ -z ${!a_name+x} ]; then
return
fi
if [ ${a_array[$2]} == true ]; then
if [ "x${4}" = 'x' ]; then
echo $3
exit 1
fi
fi
}
function run_cmd {
@ -67,7 +98,7 @@ function run_cmd {
## Check if debug is set and send command output to shell
## Some commands send animated text that can corrupt the log
if test -n "${DEBUG}"; then
debug "DEBUG variable set, redirecting command output to console"
debug "DEBUG variable set, redirecting command output to console"
$1 $2
if [ $? -gt 0 ]; then
err "Failed running command '$1 $2'"
@ -85,5 +116,5 @@ function run_cmd {
fi
fi
}