2016-03-30 16:09:02 +02:00
|
|
|
# Control file related functions
|
|
|
|
|
2016-04-01 15:51:45 +02:00
|
|
|
# Get item list from control on stdin
|
2016-03-30 16:09:02 +02:00
|
|
|
zaf_ctrl_get_items() {
|
|
|
|
grep '^Item ' | cut -d ' ' -f 2 | cut -d ':' -f 1 | tr '\r\n' ' '
|
|
|
|
}
|
|
|
|
|
|
|
|
# Get item body from stdin
|
|
|
|
# $1 itemname
|
|
|
|
zaf_ctrl_get_item_block() {
|
2016-04-19 10:37:39 +02:00
|
|
|
grep -vE '^#[a-zA-Z ]' | awk '/^Item '$1'/ { i=0;
|
2016-03-30 16:09:02 +02:00
|
|
|
while (i==0) {
|
|
|
|
getline;
|
|
|
|
if (/^\/Item/) exit;
|
|
|
|
print $0;
|
2016-03-31 15:56:23 +02:00
|
|
|
}};
|
|
|
|
END {
|
|
|
|
exit i==0;
|
|
|
|
}'
|
2016-03-30 16:09:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# Get global plugin block body from stdin
|
|
|
|
# $1 itemname
|
|
|
|
zaf_ctrl_get_global_block() {
|
2016-04-19 10:37:39 +02:00
|
|
|
grep -vE '^#[a-zA-Z ]' | awk '{ i=0; print $0;
|
2016-03-30 16:09:02 +02:00
|
|
|
while (i==0) {
|
|
|
|
getline;
|
|
|
|
if (/^Item /) exit;
|
|
|
|
print $0;
|
|
|
|
}}'
|
|
|
|
}
|
|
|
|
|
|
|
|
# Get item multiline option
|
|
|
|
# $1 optionname
|
|
|
|
zaf_block_get_moption() {
|
2016-04-19 10:37:39 +02:00
|
|
|
awk '/^'$1'::$/ { i=0;
|
2016-03-30 16:09:02 +02:00
|
|
|
while (i==0) {
|
|
|
|
getline;
|
2016-03-31 15:56:23 +02:00
|
|
|
if (/^::$/) {i=1; continue;};
|
2016-03-30 16:09:02 +02:00
|
|
|
print $0;
|
2016-03-31 15:56:23 +02:00
|
|
|
}};
|
|
|
|
END {
|
|
|
|
exit i==0;
|
|
|
|
}
|
|
|
|
'
|
2016-03-30 16:09:02 +02:00
|
|
|
}
|
|
|
|
|
2016-03-31 15:56:23 +02:00
|
|
|
# Get item singleline option from config block on stdin
|
2016-03-30 16:09:02 +02:00
|
|
|
# $1 optionname
|
|
|
|
zaf_block_get_option() {
|
|
|
|
grep "^$1:" | cut -d ' ' -f 2- | tr -d '\r\n'
|
|
|
|
}
|
|
|
|
|
2016-03-31 15:56:23 +02:00
|
|
|
# Get global option (single or multiline)
|
|
|
|
# $1 - control file
|
|
|
|
# $2 - option name
|
|
|
|
zaf_ctrl_get_global_option() {
|
2016-04-14 10:44:50 +02:00
|
|
|
local ctrlvar
|
2016-04-14 09:57:59 +02:00
|
|
|
local ctrlopt
|
|
|
|
|
2016-04-14 10:44:50 +02:00
|
|
|
ctrlopt="ZAF_CTRL_$(zaf_stripctrl $2)"
|
|
|
|
eval ctrlvar=\$$ctrlopt
|
|
|
|
if [ -n "$ctrlvar" ]; then
|
|
|
|
zaf_dbg "Overriding control field $2 from env $ctrlopt($ctrlvar)"
|
2016-04-14 09:57:59 +02:00
|
|
|
echo $ctrlopt
|
|
|
|
else
|
|
|
|
zaf_ctrl_get_global_block <$1 | zaf_block_get_moption "$2" \
|
|
|
|
|| zaf_ctrl_get_global_block <$1 | zaf_block_get_option "$2"
|
|
|
|
fi
|
2016-03-31 15:56:23 +02:00
|
|
|
}
|
|
|
|
# Get item specific option (single or multiline)
|
|
|
|
# $1 - control file
|
|
|
|
# $2 - item name
|
|
|
|
# $3 - option name
|
|
|
|
zaf_ctrl_get_item_option() {
|
2016-04-14 10:44:50 +02:00
|
|
|
local ctrlvar
|
2016-04-14 09:57:59 +02:00
|
|
|
local ctrlopt
|
|
|
|
|
2016-04-14 10:44:50 +02:00
|
|
|
ctrlopt="ZAF_CTRLI_$(zaf_stripctrl $2)_$(zaf_stripctrl $3)"
|
|
|
|
eval ctrlvar=\$$ctrlopt
|
|
|
|
if [ -n "$ctrlvar" ]; then
|
|
|
|
zaf_dbg "Overriding item control field $2/$3 from env $ctrlopt($ctrlvar)"
|
2016-04-14 09:57:59 +02:00
|
|
|
echo $ctrlopt
|
|
|
|
else
|
|
|
|
zaf_ctrl_get_item_block <$1 "$2" | zaf_block_get_moption "$3" \
|
|
|
|
|| zaf_ctrl_get_item_block <$1 "$2" | zaf_block_get_option "$3"
|
|
|
|
fi
|
2016-03-31 15:56:23 +02:00
|
|
|
}
|
|
|
|
|
2016-04-01 12:20:23 +02:00
|
|
|
# Check dependencies based on control file
|
2016-03-30 16:09:02 +02:00
|
|
|
zaf_ctrl_check_deps() {
|
|
|
|
local deps
|
|
|
|
deps=$(zaf_ctrl_get_global_block <$1 | zaf_block_get_option "Depends-${ZAF_PKG}" )
|
2016-04-12 10:36:17 +02:00
|
|
|
if ! zaf_os_specific zaf_check_deps $deps; then
|
|
|
|
zaf_err "Missing one of dependend system packages: $deps"
|
|
|
|
fi
|
2016-03-30 16:09:02 +02:00
|
|
|
deps=$(zaf_ctrl_get_global_block <$1 | zaf_block_get_option "Depends-bin" )
|
|
|
|
for cmd in $deps; do
|
|
|
|
if ! which $cmd >/dev/null; then
|
2016-04-12 10:36:17 +02:00
|
|
|
zaf_err "Missing binary dependency $cmd. Please install it first."
|
2016-03-30 16:09:02 +02:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2016-04-12 18:38:19 +02:00
|
|
|
# Install sudo config from control
|
|
|
|
# $1 plugin
|
|
|
|
# $2 control
|
|
|
|
# $3 plugindir
|
|
|
|
zaf_ctrl_sudo() {
|
|
|
|
local pdir
|
|
|
|
local plugin
|
2016-04-13 12:42:12 +02:00
|
|
|
local sudo
|
|
|
|
local cmd
|
|
|
|
local parms
|
2016-04-12 18:38:19 +02:00
|
|
|
|
|
|
|
pdir="$3"
|
|
|
|
plugin=$1
|
2016-04-19 12:37:37 +02:00
|
|
|
! [ -d "$ZAF_SUDOERSD" ] && { zaf_wrn "$ZAF_SUDOERSD nonexistent! Skipping sudo install!"; return 1; }
|
2016-04-12 19:31:06 +02:00
|
|
|
zaf_dbg "Installing sudoers entry $ZAF_SUDOERSD/zaf_$plugin"
|
2016-04-13 12:42:12 +02:00
|
|
|
sudo=$(zaf_ctrl_get_global_option $2 "Sudo" | zaf_far '{PLUGINDIR}' "${plugindir}")
|
2016-04-14 09:57:59 +02:00
|
|
|
[ -z "$sudo" ] && return # Nothing to install
|
|
|
|
if ! which sudo >/dev/null; then
|
|
|
|
zaf_wrn "Sudo needed bud not installed?"
|
|
|
|
fi
|
2016-04-13 12:42:12 +02:00
|
|
|
cmd=$(echo $sudo | cut -d ' ' -f 1)
|
|
|
|
parms=$(echo $sudo | cut -d ' ' -f 2-)
|
|
|
|
if which $cmd >/dev/null ; then
|
2016-04-14 09:57:59 +02:00
|
|
|
(echo "zabbix ALL=NOPASSWD:SETENV: $(which $cmd) $(echo $parms | tr '%' '*')";echo) >$ZAF_SUDOERSD/zaf_$plugin || zaf_err "Error during zaf_ctrl_sudo"
|
|
|
|
chmod 0440 $ZAF_SUDOERSD/zaf_$plugin
|
2016-04-13 12:42:12 +02:00
|
|
|
else
|
2016-04-14 09:57:59 +02:00
|
|
|
zaf_err "Cannot find binary '$cmd' to put into sudoers."
|
2016-04-13 12:42:12 +02:00
|
|
|
fi
|
2016-04-12 18:38:19 +02:00
|
|
|
}
|
|
|
|
|
2016-04-12 19:31:06 +02:00
|
|
|
# Install crontab config from control
|
|
|
|
# $1 plugin
|
|
|
|
# $2 control
|
|
|
|
# $3 plugindir
|
|
|
|
zaf_ctrl_cron() {
|
|
|
|
local pdir
|
|
|
|
local plugin
|
2016-04-14 09:57:59 +02:00
|
|
|
local cron
|
2016-04-12 19:31:06 +02:00
|
|
|
|
|
|
|
pdir="$3"
|
|
|
|
plugin=$1
|
2016-04-19 12:37:37 +02:00
|
|
|
! [ -d "$ZAF_CROND" ] && { zaf_wrn "$ZAF_CROND nonexistent! Skipping cron install!"; return 1; }
|
2016-04-12 19:31:06 +02:00
|
|
|
zaf_dbg "Installing cron entry $ZAF_CROND/zaf_$plugin"
|
2016-04-14 09:57:59 +02:00
|
|
|
cron=$(zaf_ctrl_get_global_option $2 "Cron")
|
|
|
|
[ -z "$cron" ] && return # Nothing to install
|
|
|
|
zaf_ctrl_get_global_option $2 "Cron" | zaf_far '{PLUGINDIR}' "${plugindir}" >$ZAF_CROND/zaf_$plugin || zaf_err "Error during zaf_ctrl_cron"
|
2016-04-12 19:31:06 +02:00
|
|
|
}
|
|
|
|
|
2016-04-14 09:57:59 +02:00
|
|
|
# Install files defined to be installed in control to plugun directory
|
2016-04-01 15:51:45 +02:00
|
|
|
# $1 pluginurl
|
|
|
|
# $2 control
|
|
|
|
# $3 plugindir
|
2016-03-30 16:09:02 +02:00
|
|
|
zaf_ctrl_install() {
|
|
|
|
local binaries
|
|
|
|
local pdir
|
|
|
|
local script
|
2016-04-14 09:57:59 +02:00
|
|
|
local files
|
|
|
|
local f
|
|
|
|
local b
|
2016-03-30 16:09:02 +02:00
|
|
|
|
2016-04-01 15:51:45 +02:00
|
|
|
pdir="$3"
|
2016-04-14 09:57:59 +02:00
|
|
|
(set -e
|
2016-04-06 14:25:10 +02:00
|
|
|
binaries=$(zaf_ctrl_get_global_option $2 "Install-bin")
|
2016-03-30 16:09:02 +02:00
|
|
|
for b in $binaries; do
|
2016-04-01 15:51:45 +02:00
|
|
|
zaf_fetch_url "$1/$b" >"${ZAF_TMP_DIR}/$b"
|
2016-03-31 15:56:23 +02:00
|
|
|
zaf_install_bin "${ZAF_TMP_DIR}/$b" "$pdir"
|
2016-03-30 16:09:02 +02:00
|
|
|
done
|
2016-04-14 09:57:59 +02:00
|
|
|
files=$(zaf_ctrl_get_global_option $2 "Install-files")
|
|
|
|
for f in $files; do
|
|
|
|
zaf_fetch_url "$1/$b" >"${ZAF_TMP_DIR}/$b"
|
|
|
|
zaf_install "${ZAF_TMP_DIR}/$b" "$pdir"
|
|
|
|
done
|
2016-04-06 14:25:10 +02:00
|
|
|
script=$(zaf_ctrl_get_global_option $2 "Install-script")
|
2016-03-31 15:56:23 +02:00
|
|
|
[ -n "$script" ] && eval "$script"
|
2016-04-14 09:57:59 +02:00
|
|
|
true
|
|
|
|
) || zaf_err "Error during zaf_ctrl_install"
|
2016-03-30 16:09:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# Generates zabbix cfg from control file
|
|
|
|
# $1 control
|
|
|
|
# $2 pluginname
|
2016-04-19 10:37:39 +02:00
|
|
|
# $3 if nonempty, only return cfg, do not generate scripts
|
2016-03-30 16:09:02 +02:00
|
|
|
zaf_ctrl_generate_cfg() {
|
|
|
|
local items
|
|
|
|
local cmd
|
2016-04-01 15:51:45 +02:00
|
|
|
local iscript
|
2016-04-01 12:20:23 +02:00
|
|
|
local ikey
|
|
|
|
local lock
|
2016-04-12 18:38:19 +02:00
|
|
|
local cache
|
2016-04-21 15:29:48 +02:00
|
|
|
local tmpfile
|
|
|
|
local pname
|
|
|
|
local pdefault
|
|
|
|
local pregex
|
|
|
|
local prest
|
|
|
|
local zafparms
|
2016-03-30 16:09:02 +02:00
|
|
|
|
|
|
|
items=$(zaf_ctrl_get_items <"$1")
|
2016-04-21 15:29:48 +02:00
|
|
|
tmpfile=$ZAF_TMP_DIR/gencfg$$
|
2016-04-14 09:57:59 +02:00
|
|
|
(set -e
|
2016-03-30 16:09:02 +02:00
|
|
|
for i in $items; do
|
2016-04-14 10:44:50 +02:00
|
|
|
iscript=$(zaf_stripctrl $i)
|
2016-04-21 15:29:48 +02:00
|
|
|
zaf_ctrl_get_item_option $1 $i "Parameters" >$tmpfile
|
|
|
|
if [ -s "$tmpfile" ]; then
|
2016-04-01 15:51:45 +02:00
|
|
|
ikey="$2.$i[*]"
|
2016-04-04 14:00:44 +02:00
|
|
|
args=""
|
|
|
|
apos=1;
|
2016-04-21 15:29:48 +02:00
|
|
|
while read pname pdefault pregex prest; do
|
|
|
|
zafparams="$zafparams value=\"\$$apos\"; zaf_agentparm $pname $pdefault $pregex;"
|
2016-04-04 14:00:44 +02:00
|
|
|
args="$args \$$apos"
|
|
|
|
apos=$(expr $apos + 1)
|
2016-04-21 15:29:48 +02:00
|
|
|
done <$tmpfile
|
2016-04-01 15:51:45 +02:00
|
|
|
else
|
|
|
|
ikey="$2.$i"
|
|
|
|
fi
|
2016-04-01 12:20:23 +02:00
|
|
|
lock=$(zaf_ctrl_get_item_option $1 $i "Lock")
|
|
|
|
if [ -n "$lock" ]; then
|
|
|
|
lock="${ZAF_LIB_DIR}/zaflock $lock "
|
|
|
|
fi
|
2016-04-12 18:38:19 +02:00
|
|
|
cache=$(zaf_ctrl_get_item_option $1 $i "Cache")
|
|
|
|
if [ -n "$cache" ]; then
|
2016-04-19 10:37:39 +02:00
|
|
|
cache="${ZAF_LIB_DIR}/zafcache '$cache' "
|
2016-04-12 18:38:19 +02:00
|
|
|
fi
|
2016-03-31 15:56:23 +02:00
|
|
|
cmd=$(zaf_ctrl_get_item_option $1 $i "Cmd")
|
2016-04-19 10:37:39 +02:00
|
|
|
if [ -z "$cache" ] && [ -z "$lock" ]; then
|
|
|
|
preload="${ZAF_LIB_DIR}/preload.sh "
|
|
|
|
else
|
|
|
|
preload=""
|
|
|
|
fi
|
2016-03-31 15:56:23 +02:00
|
|
|
if [ -n "$cmd" ]; then
|
2016-04-21 15:29:48 +02:00
|
|
|
printf "%s" "UserParameter=$ikey,export ITEM_KEY='$ikey'; ${preload}${cache}${lock}${cmd}"; echo
|
2016-03-31 15:56:23 +02:00
|
|
|
continue
|
|
|
|
fi
|
|
|
|
cmd=$(zaf_ctrl_get_item_option $1 $i "Script")
|
|
|
|
if [ -n "$cmd" ]; then
|
2016-04-21 15:29:48 +02:00
|
|
|
( echo "#!/bin/sh"
|
|
|
|
echo ". $ZAF_LIB_DIR/preload.sh; "
|
|
|
|
echo "$zafparams"
|
|
|
|
zaf_ctrl_get_item_option $1 $i "Script"
|
|
|
|
) >${ZAF_TMP_DIR}/${iscript}.sh;
|
2016-04-19 10:37:39 +02:00
|
|
|
[ -z "$3" ] && zaf_install_bin ${ZAF_TMP_DIR}/${iscript}.sh ${ZAF_PLUGINS_DIR}/$2/
|
2016-04-21 15:29:48 +02:00
|
|
|
printf "%s" "UserParameter=$ikey,export ITEM_KEY='$ikey'; ${preload}${cache}${lock}${ZAF_PLUGINS_DIR}/$2/${iscript}.sh $args"; echo
|
2016-03-31 15:56:23 +02:00
|
|
|
continue;
|
|
|
|
fi
|
2016-04-01 15:51:45 +02:00
|
|
|
zaf_err "Item $i declared in control file but has no Cmd, Function or Script!"
|
2016-03-30 16:09:02 +02:00
|
|
|
done
|
2016-04-14 09:57:59 +02:00
|
|
|
) || zaf_err "Error during zaf_ctrl_generate_cfg"
|
2016-03-30 16:09:02 +02:00
|
|
|
}
|
|
|
|
|
2016-04-01 12:20:23 +02:00
|
|
|
|