1
0
mirror of https://github.com/limosek/zaf.git synced 2024-11-14 06:29:07 +01:00
limosek-zaf/lib/zaf.lib.sh

279 lines
5.9 KiB
Bash
Raw Normal View History

2016-03-23 17:25:18 +01:00
2016-04-01 15:51:45 +02:00
# Hardcoded variables
2016-04-18 10:38:43 +02:00
ZAF_VERSION="1.3"
ZAF_GITBRANCH="master"
2016-04-07 13:14:53 +02:00
ZAF_URL="https://github.com/limosek/zaf"
2016-04-12 10:36:17 +02:00
ZAF_RAW_URL="https://raw.githubusercontent.com/limosek/zaf"
2016-04-01 15:51:45 +02:00
2016-03-24 16:25:58 +01:00
############################################ Common routines
2016-03-24 20:37:03 +01:00
zaf_msg() {
echo $@
}
zaf_trc() {
2016-04-21 15:29:48 +02:00
[ "$ZAF_DEBUG" -ge "3" ] && logger -p user.info ${ZAF_LOG_STDERR} -t zaf-trace -- $@
2016-12-01 16:57:26 +01:00
return 0
}
zaf_dbg() {
2016-04-21 15:29:48 +02:00
[ "$ZAF_DEBUG" -ge "2" ] && logger -p user.debug ${ZAF_LOG_STDERR} -t zaf-debug -- $@
2016-12-01 16:57:26 +01:00
return 0
}
zaf_wrn() {
2016-04-21 15:29:48 +02:00
[ "$ZAF_DEBUG" -ge "1" ] && logger -p user.warn ${ZAF_LOG_STDERR} -t zaf-warning -- $@
2016-12-01 16:57:26 +01:00
return 0
}
zaf_err() {
2016-04-21 15:29:48 +02:00
logger ${ZAF_LOG_STDERR} -p user.err -t zaf-error -- $@
logger ${ZAF_LOG_STDERR} -p user.err -t zaf-error "Exiting with error!"
exit 1
2016-03-24 20:37:03 +01:00
}
2016-04-18 09:20:57 +02:00
# Help option
# $1 - key
# $2 -
zaf_hlp() {
local kl
local dl
local cols
cols=$COLUMNS
[ -z "$cols" ] && cols=120
kl=$(expr $cols / 3)
dl=$(expr $cols - $kl)
printf %-${kl}s%-${dl}s%b "$1" "$2" "\n"
}
# $1 if nonempty, log to stderr too
zaf_debug_init() {
[ -z "$ZAF_DEBUG" ] && ZAF_DEBUG=1
export ZAF_DEBUG
[ -n "$1" ] && export ZAF_LOG_STDERR="-s"
}
2016-03-24 20:37:03 +01:00
zaf_tmp_init() {
[ -z "$ZAF_TMP_DIR" ] && ZAF_TMP_DIR=/tmp/
! [ -w "$ZAF_TMP_DIR" ] && zaf_err "Tmp dir $ZAF_TMP_DIR is not writable."
}
2016-03-24 20:37:03 +01:00
2016-04-01 15:51:45 +02:00
zaf_version(){
echo $ZAF_VERSION
}
2016-04-21 15:29:48 +02:00
# Add parameter for agent check
# $1 parameter name (will be set to var)
# $2 if nonempty, it is default value. If empty, parameter is mandatory
# $3 if nonempty, regexp to test
zaf_agentparm(){
local name
local default
local regexp
name="$1"
default="$2"
regexp="$3"
[ -z "$value" ] && [ -z "$default" ] && zaf_err "$ITEM_KEY: Missing mandatory parameter $name."
if [ -z "$value" ]; then
value="$default"
else
if [ -n "$regexp" ]; then
echo "$value" | grep -qE "$regexp" || zaf_err "$ITEM_KEY: Bad parameter '$name' value '$value' (not in regexp '$regexp')."
fi
fi
eval $name=$value
zaf_trc "$ITEM_KEY: Param $name set to $value"
}
2016-03-24 09:38:34 +01:00
# Fetch url to stdout
# $1 url
# It supports real file, file:// and other schemes known by curl
2016-03-23 17:25:18 +01:00
zaf_fetch_url() {
local scheme
local uri
local insecure
local out
2016-03-23 17:25:18 +01:00
if zaf_fromcache "$1"; then
return
fi
2016-03-23 17:25:18 +01:00
scheme=$(echo $1|cut -d ':' -f 1)
uri=$(echo $1|cut -d '/' -f 3-)
if [ "$1" = "$scheme" ]; then
cat "$1"
fi
case $scheme in
http|https|ftp|file)
2016-04-07 13:14:53 +02:00
[ "$scheme" != "file" ] && [ -n "$ZAF_OFFLINE" ] && zaf_err "Cannot download $1 in offline mode!"
[ "${ZAF_CURL_INSECURE}" = "1" ] && insecure="-k"
2016-04-04 10:27:25 +02:00
zaf_dbg curl $insecure -f -s -L -o - $1
curl $insecure -f -s -L -o - "$1" | zaf_tocache_stdin "$1" 120
2016-03-23 17:25:18 +01:00
;;
esac
}
2016-03-24 09:38:34 +01:00
# Find and replace string
2016-03-23 17:25:18 +01:00
zaf_far(){
local f
local t
local sedcmd="sed"
i=1
while [ "$i" -lt "$#" ];
do
eval f=\$${i}
i=$(expr $i + 1)
eval t=\$${i}
i=$(expr $i + 1)
sedcmd="$sedcmd -e 's~$f~$t~g'"
done
eval $sedcmd
}
# Limit concurrent processes or continue
zaf_bglimit(){
local maxbg
local maxnumber
local cnumber
[ -z "$BASH_VERSION" ] && { zaf_dbg "Job server not available. Use bash!"; return 1; }
if [ $# -eq 0 ] ; then
maxbg=5
else
maxbg=$1
fi
maxnumber=$((0 + ${1:-0}))
while true; do
cnumber=$(jobs | wc -l)
if [ $cnumber -lt $maxnumber ]; then
break
fi
zaf_dbg "Limiting next job due to $maxbg limit of bg jobs"
sleep 1
done
}
2016-03-24 16:25:58 +01:00
# Initialises discovery function
2016-04-01 12:20:23 +02:00
zaf_discovery_begin(){
cat <<EOF
{
"data":[
EOF
2016-03-24 16:25:58 +01:00
}
# Add row(s) to discovery data
zaf_discovery_add_row(){
2016-04-01 12:20:23 +02:00
local rows
local row
rows=$1
row=$2
shift;shift
echo " {"
2016-03-24 16:25:58 +01:00
while [ -n "$1" ]; do
2016-04-01 15:51:45 +02:00
echo -n ' "'$1'":"'$2'" '
2016-04-01 12:20:23 +02:00
shift;shift
if [ -n "$1" ]; then
echo ","
else
echo ""
fi
2016-03-24 16:25:58 +01:00
done
2016-04-01 12:20:23 +02:00
if [ "$row" -lt "$rows" ]; then
echo " },"
else
echo " }"
fi
2016-03-24 16:25:58 +01:00
}
# Dumps json object
2016-04-01 12:20:23 +02:00
zaf_discovery_end(){
cat <<EOF
]
}
EOF
2016-03-24 16:25:58 +01:00
}
# Read standard input as discovery data. Columns are divided by space.
# Arguments are name of variables to discovery.
# Dumps json to stdout
zaf_discovery(){
2016-04-01 12:20:23 +02:00
local tmpfile
local rows
local a b c d e f g h i j row
tmpfile="${ZAF_TMP_DIR}/disc$$"
cat >$tmpfile
rows=$(wc -l <$tmpfile)
2016-03-24 16:25:58 +01:00
local a b c d e f g h i j;
2016-04-01 12:20:23 +02:00
zaf_discovery_begin
row=1
2016-03-24 16:25:58 +01:00
while read a b c d e f g h i j; do
2016-04-01 12:20:23 +02:00
zaf_discovery_add_row "$rows" "$row" "$1" "${1:+${a}}" "$2" "${2:+${b}}" "$3" "${3:+${c}}" "$4" "${4:+${d}}" "$5" "${5:+${e}}" "$6" "${6:+${f}}" "$7" "${7:+${g}}" "$8" "${8:+${h}}" "$9" "${9:+${i}}"
row=$(expr $row + 1)
done <$tmpfile
zaf_discovery_end
rm -f $tmpfile
2016-03-24 16:25:58 +01:00
}
############################################ Zaf internal routines
2016-03-24 09:38:34 +01:00
# Restart zabbix agent
2016-03-23 17:25:18 +01:00
zaf_restart_agent() {
2016-04-01 12:20:23 +02:00
zaf_wrn "Restarting agent (${ZAF_AGENT_RESTART})"
2016-04-07 13:14:53 +02:00
${ZAF_AGENT_RESTART} || zaf_err "Cannot restart Zabbix agent (${ZAF_AGENT_RESTART}). Try $ZAF_AGENT_BIN -f !";
2016-03-23 17:25:18 +01:00
}
2016-03-24 09:38:34 +01:00
# Check if zaf.version item is populated
2016-03-23 17:25:18 +01:00
zaf_check_agent_config() {
zaf_restart_agent
${ZAF_AGENT_BIN} -t zaf.version
2016-03-23 17:25:18 +01:00
}
2016-04-12 10:36:17 +02:00
zaf_tolower() {
tr 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' 'abcdefghijklmnopqrstuvwxyz'
}
zaf_toupper() {
tr 'abcdefghijklmnopqrstuvwxyz' 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
}
2016-03-23 17:25:18 +01:00
2016-04-14 10:44:50 +02:00
# Return simplified key with discarded special chars.
zaf_stripctrl() {
echo $1 | tr '[]*&;:.-' '________'
}
# Unescape string on stdin
# $1 - list of chars to unescape
zaf_strunescape() {
sed -e 's#\\\(['"$1"']\)#\1#g'
}
# Escape string on stdin
# $1 - list of chars to escape
zaf_strescape() {
sed -e 's#\(['"$1"']\)#\\\1#g'
}
# Add seconds to current date and return date in YYYY-MM-DD hh:mm:ss
# $1 seconds
zaf_date_add() {
date -d "$1 seconds" "+%Y-%m-%d %H:%M:%S" 2>/dev/null || date -d "$(expr $(date +%s) + $1)" -D %s "+%Y-%m-%d %H:%M:%S"
}
2016-04-21 15:29:48 +02:00
# Create temp file and return its name
# $1 prefix or empty
zaf_tmpfile() {
echo "$ZAF_TMP_DIR/tmp$1"
}
# return random number
zaf_random() {
hexdump -n 2 -e '/2 "%u"' /dev/urandom
}
# Emulate sudo
zaf_sudo() {
2016-11-21 17:05:13 +01:00
if zaf_is_root || ! zaf_which sudo >/dev/null 2>/dev/null; then
$@
else
sudo $@
fi
}