1
0
mirror of https://github.com/limosek/zaf.git synced 2024-11-25 03:49:06 +01:00
limosek-zaf/lib/zaf.lib.sh

221 lines
4.6 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-14 10:44:50 +02:00
ZAF_VERSION="1.2"
2016-04-18 10:35:23 +02:00
ZAF_GITBRANCH="1.2"
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() {
[ "$ZAF_DEBUG" -ge "3" ] && logger -p user.info -s -t zaf-trace -- $@
}
zaf_dbg() {
[ "$ZAF_DEBUG" -ge "2" ] && logger -p user.debug -s -t zaf-debug -- $@
}
zaf_wrn() {
[ "$ZAF_DEBUG" -ge "1" ] && logger -p user.warn -s -t zaf-warning -- $@
}
zaf_err() {
logger -s -p user.err -t zaf-error -- $@
logger -s -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"
}
2016-03-24 20:37:03 +01:00
2016-04-01 15:51:45 +02:00
zaf_version(){
echo $ZAF_VERSION
}
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"
}