1
0
mirror of https://github.com/limosek/zaf.git synced 2024-11-22 02:19:06 +01:00
limosek-zaf/lib/zaf.lib.sh

357 lines
7.4 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-12-07 13:17:16 +01:00
ZAF_VERSION="1.4"
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 -- $@
2016-12-07 14:25:34 +01:00
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
2016-12-07 14:25:34 +01:00
echo "$value" | grep -qE "$regexp" || zaf_err "$ITEM_KEY: Bad parameter '$name' value '$value' (not in regexp '$regexp')."
2016-04-21 15:29:48 +02:00
fi
fi
zaf_trc "$ITEM_KEY: Param $name set to $value"
2016-12-11 13:06:05 +01:00
eval $name=\"$value\"
2016-04-21 15:29:48 +02:00
}
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!"
2016-12-05 12:03:13 +01:00
if zaf_which curl >/dev/null 2>/dev/null; then
[ "${ZAF_CURL_INSECURE}" = "1" ] && insecure="-k"
zaf_dbg curl $insecure -f -s -L -o - $1
curl $insecure -f -s -L -o - "$1" | zaf_tocache_stdin "$1" 120
else
[ "${ZAF_CURL_INSECURE}" = "1" ] && insecure="--no-check-certificate"
zaf_dbg wget $insecure -O - $1
wget $insecure -O - "$1" | zaf_tocache_stdin "$1" 120
fi
2016-03-23 17:25:18 +01:00
;;
esac
}
2016-12-05 11:30:10 +01:00
# Get info about url
2016-12-07 14:25:34 +01:00
# returns path or url
2016-12-05 11:30:10 +01:00
zaf_url_info() {
if echo "$1" | grep -q '^/'; then
echo "path"
else
if echo "$1" | grep -q ^http; then
echo "url"
else
echo "unknown"
fi
fi
}
2016-03-24 09:38:34 +01:00
# Find and replace string
2016-03-23 17:25:18 +01:00
zaf_far(){
2016-12-07 14:25:34 +01:00
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
2016-03-23 17:25:18 +01:00
}
2016-12-09 12:40:25 +01:00
# Trim spaces and newlines from string
zaf_trim(){
tr -d '\n'
}
# Limit concurrent processes or continue
zaf_bglimit(){
2016-12-07 14:25:34 +01:00
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-12-07 14:25:34 +01:00
local rows
local row
rows=$1
row=$2
shift;shift
echo " {"
while [ -n "$1" ]; do
echo -n ' "'$1'":"'$2'" '
shift;shift
if [ -n "$1" ]; then
2016-04-01 12:20:23 +02:00
echo ","
2016-12-07 14:25:34 +01:00
else
2016-04-01 12:20:23 +02:00
echo ""
2016-12-07 14:25:34 +01:00
fi
done
if [ "$row" -lt "$rows" ]; then
echo " },"
else
2016-04-01 12:20:23 +02:00
echo " }"
2016-12-07 14:25:34 +01:00
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-12-07 14:25:34 +01: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)
local a b c d e f g h i j;
zaf_discovery_begin
row=1
while read a b c d e f g h i j; do
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() {
2016-12-09 12:40:25 +01:00
echo $1 | tr '[]*&;:.-' '________' | tr -d '\n'
2016-04-14 10:44:50 +02:00
}
# 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() {
2016-12-09 12:40:25 +01:00
echo "$ZAF_TMP_DIR/tmp$1$(zaf_random)"
2016-04-21 15:29:48 +02:00
}
# 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
}
2017-01-30 10:32:29 +01:00
# Get item name from plugin.item[parms]
zaf_get_plugin_name() {
echo $1|cut -d '.' -f 1
}
# Get item name from plugin.item[parms]
zaf_get_item_name() {
echo $1|cut -d '.' -f 2-|cut -d '[' -f 1
}
# Get item params from plugin.item[parms]
zaf_get_item_params() {
echo $1|cut -d '[' -f 2|cut -d ']' -f 1
}
# Convert Zabbix style parameters [param1,param2,..] into $1 $2 $3
zaf_paramstosh() {
local parms
local IFS
parms=$(echo $*|cut -d '[' -f 2 | cut -d ']' -f 1| tr ',' ':')
IFS=:; for i in $parms; do
if [ -n "$i" ]; then
printf "$i "
else
printf "'' "
fi
done
}
#Returns either actual hostname or configured one
zaf_hostname() {
if [ -z "$ZAF_HOSTNAME" ]; then
hostname
else
echo $ZAF_HOSTNAME
fi
}
# Send active agent query (used for auto registration of host)
# $1 hostname
# $2 metadata
zaf_register() {
local query
local result
zaf_dbg "zaf_register $1 $2"
query="{\"host\": \"$1\", \"host_metadata\": \"$2\", \"request\": \"active checks\"}"
result="$(echo $query| nc -q1 $ZAF_ZBXSRV_HOST $ZAF_ZBXSRV_PORT | tail -c +14)"
if echo $result |grep -q failed; then
zaf_wrn $result
fi
}