1
0
mirror of https://github.com/limosek/zaf.git synced 2024-11-21 18:09:06 +01:00

Added return parameters checking

This commit is contained in:
Lukas Macura 2016-12-09 12:40:25 +01:00
parent 2ffc186d68
commit 63b00e70aa
5 changed files with 184 additions and 98 deletions

View File

@ -370,7 +370,7 @@ zaf_install_all() {
for i in lib/zaf.lib.sh lib/plugin.lib.sh lib/os.lib.sh lib/ctrl.lib.sh lib/cache.lib.sh lib/zbxapi.lib.sh lib/JSON.sh; do for i in lib/zaf.lib.sh lib/plugin.lib.sh lib/os.lib.sh lib/ctrl.lib.sh lib/cache.lib.sh lib/zbxapi.lib.sh lib/JSON.sh; do
zaf_install $i ${ZAF_LIB_DIR}/ zaf_install $i ${ZAF_LIB_DIR}/
done done
for i in lib/zaflock lib/zafcache lib/preload.sh; do for i in lib/zaflock lib/zafcache lib/preload.sh lib/zafret; do
zaf_install_bin $i ${ZAF_LIB_DIR}/ zaf_install_bin $i ${ZAF_LIB_DIR}/
done done
zaf_install_dir ${ZAF_BIN_DIR} zaf_install_dir ${ZAF_BIN_DIR}

View File

@ -212,12 +212,12 @@ zaf_ctrl_install() {
binaries=$(zaf_ctrl_get_global_option $2 "Install-bin") binaries=$(zaf_ctrl_get_global_option $2 "Install-bin")
for b in $binaries; do for b in $binaries; do
zaf_fetch_url "$1/$b" >"${ZAF_TMP_DIR}/$b" zaf_fetch_url "$1/$b" >"${ZAF_TMP_DIR}/$b"
zaf_install_bin "${ZAF_TMP_DIR}/$b" "$pdir" zaf_install_bin "${ZAF_TMP_DIR}/$b" "$pdir"
done done
files=$(zaf_ctrl_get_global_option $2 "Install-files") files=$(zaf_ctrl_get_global_option $2 "Install-files")
for f in $files; do for f in $files; do
zaf_fetch_url "$1/$b" >"${ZAF_TMP_DIR}/$b" zaf_fetch_url "$1/$f" >"${ZAF_TMP_DIR}/$f"
zaf_install "${ZAF_TMP_DIR}/$b" "$pdir" zaf_install "${ZAF_TMP_DIR}/$f" "$pdir"
done done
true true
) || zaf_err "Error during zaf_ctrl_install" ) || zaf_err "Error during zaf_ctrl_install"
@ -241,54 +241,67 @@ zaf_ctrl_generate_items_cfg() {
local zafparms local zafparms
items=$(zaf_ctrl_get_items <"$1") items=$(zaf_ctrl_get_items <"$1")
tmpfile=$ZAF_TMP_DIR/gencfg$$ tmpfile=$(zaf_tmpfile genparms)
(set -e (set -e
for i in $items; do for i in $items; do
iscript=$(zaf_stripctrl $i) iscript=$(zaf_stripctrl $i)
zaf_ctrl_get_item_option $1 $i "Parameters" >$tmpfile zaf_ctrl_get_item_option $1 $i "Parameters" >$tmpfile
if [ -s "$tmpfile" ]; then echo >>$tmpfile
ikey="$2.$i[*]" zafparams="";
args="" if [ -s "$tmpfile" ]; then
apos=1; ikey="$2.$i[*]"
while read pname pdefault pregex prest; do args=""
zafparams="$zafparams value=\"\$$apos\"; zaf_agentparm $pname $pdefault $pregex; export $pname; " apos=1;
args="$args \$$apos" while read pname pdefault pregex prest; do
apos=$(expr $apos + 1) [ -z "$pname" ] && continue
done <$tmpfile zaf_trc "Adding param $pname ($pdefault $pregex) to $i"
else zafparams="$zafparams value=\"\$$apos\"; zaf_agentparm $pname $pdefault $pregex; export $pname; "
ikey="$2.$i" args="$args \$$apos"
zafparams="" apos=$(expr $apos + 1)
args="" done <$tmpfile
fi else
env="export ITEM_KEY='$ikey'; export PLUGIN='$2'; export PATH=${ZAF_PLUGINS_DIR}/$2:$ZAF_LIB_DIR:\$PATH; cd ${ZAF_PLUGINS_DIR}/$2; . $ZAF_LIB_DIR/preload.sh; " ikey="$2.$i"
lock=$(zaf_ctrl_get_item_option $1 $i "Lock") zafparams=""
if [ -n "$lock" ]; then args=""
lock="${ZAF_LIB_DIR}/zaflock $lock " fi
fi env="export ITEM_KEY='$ikey'; export PLUGIN='$2'; export PATH=${ZAF_PLUGINS_DIR}/$2:$ZAF_LIB_DIR:\$PATH; cd ${ZAF_PLUGINS_DIR}/$2; . $ZAF_LIB_DIR/preload.sh; "
cache=$(zaf_ctrl_get_item_option $1 $i "Cache") lock=$(zaf_ctrl_get_item_option $1 $i "Lock")
if [ -n "$cache" ]; then if [ -n "$lock" ]; then
cache="${ZAF_LIB_DIR}/zafcache '$cache' " lock="${ZAF_LIB_DIR}/zaflock $lock "
fi fi
cmd=$(zaf_ctrl_get_item_option $1 $i "Cmd") cache=$(zaf_ctrl_get_item_option $1 $i "Cache")
if [ -n "$cmd" ]; then if [ -n "$cache" ]; then
printf "%s" "UserParameter=$ikey,${env}${zafparams}${preload}${cache}${lock}${cmd}"; echo cache="${ZAF_LIB_DIR}/zafcache '$cache' "
continue fi
fi ret=$(zaf_ctrl_get_item_option $1 $i "Return")
cmd=$(zaf_ctrl_get_item_option $1 $i "Script") retnull=$(zaf_ctrl_get_item_option $1 $i "Return-null")
if [ -n "$cmd" ]; then reterr=$(zaf_ctrl_get_item_option $1 $i "Return-error")
( echo "#!/bin/sh" if [ -n "$ret" ] || [ -n "$reterr" ] || [ -n "$retnull" ]; then
retscr=" 1>\${tmpf}o 2>\${tmpf}e; ${ZAF_LIB_DIR}/zafret \${tmpf}o \${tmpf}e \$? '$ret' '$retnull' '$retempty' ";
else
retscr="";
fi
cmd=$(zaf_ctrl_get_item_option $1 $i "Cmd")
if [ -n "$cmd" ]; then
printf "%s" "UserParameter=$ikey,${env}${zafparams}${preload}${cache}${lock}${cmd}${retscr}"; echo
continue
fi
cmd=$(zaf_ctrl_get_item_option $1 $i "Script")
if [ -n "$cmd" ]; then
( echo "#!/bin/sh"
echo ". $ZAF_LIB_DIR/preload.sh; " echo ". $ZAF_LIB_DIR/preload.sh; "
zaf_ctrl_get_item_option $1 $i "Script" zaf_ctrl_get_item_option $1 $i "Script"
) >${ZAF_TMP_DIR}/${iscript}.sh; ) >${ZAF_TMP_DIR}/${iscript}.sh;
[ -z "$3" ] && zaf_install_bin ${ZAF_TMP_DIR}/${iscript}.sh ${ZAF_PLUGINS_DIR}/$2/ [ -z "$3" ] && zaf_install_bin ${ZAF_TMP_DIR}/${iscript}.sh ${ZAF_PLUGINS_DIR}/$2/
printf "%s" "UserParameter=$ikey,${env}${preload}${zafparams}${cache}${lock}${ZAF_PLUGINS_DIR}/$2/${iscript}.sh ${args}"; echo printf "%s" "UserParameter=$ikey,${env}${preload}${zafparams}${cache}${lock}${ZAF_PLUGINS_DIR}/$2/${iscript}.sh ${args}"; echo
rm -f ${ZAF_TMP_DIR}/${iscript}.sh rm -f ${ZAF_TMP_DIR}/${iscript}.sh
continue; continue;
fi fi
zaf_err "Item $i declared in control file but has no Cmd, Function or Script!" zaf_err "Item $i declared in control file but has no Cmd, Function or Script!"
done done
) || zaf_err "Error during zaf_ctrl_generate_items_cfg" ) || zaf_err "Error during zaf_ctrl_generate_items_cfg"
rm -f $tmpfile [ "$ZAF_DEBUG" -lt 4 ] && rm -f $tmpfile
} }
# Generates zabbix items cfg from control file # Generates zabbix items cfg from control file
@ -309,48 +322,56 @@ zaf_ctrl_generate_extitems_cfg() {
local zafparms local zafparms
items=$(zaf_ctrl_get_extitems <"$1") items=$(zaf_ctrl_get_extitems <"$1")
tmpfile=$ZAF_TMP_DIR/gencfg$$ tmpfile=$(zaf_tmpfile genparms)
(set -e (set -e
for i in $items; do for i in $items; do
iscript=$(zaf_stripctrl $i) iscript=$(zaf_stripctrl $i)
zaf_ctrl_get_extitem_option $1 $i "Parameters" >$tmpfile (zaf_ctrl_get_extitem_option $1 $i "Parameters"; echo) >$tmpfile
ikey="$2.$i" ikey="$2.$i"
if [ -s "$tmpfile" ]; then if [ -s "$tmpfile" ]; then
args="" args=""
apos=1; apos=1;
while read pname pdefault pregex prest; do while read pname pdefault pregex prest; do
zafparams="$zafparams value=\"\$$apos\"; zaf_agentparm $pname $pdefault $pregex; export $pname; " zafparams="$zafparams value=\"\$$apos\"; zaf_agentparm $pname $pdefault $pregex; export $pname; "
args="$args \$$apos" args="$args \$$apos"
apos=$(expr $apos + 1) apos=$(expr $apos + 1)
done <$tmpfile done <$tmpfile
else else
zafparams="" zafparams=""
args="" args=""
fi fi
env="export ITEM_KEY='$ikey'; export PLUGIN='$2'; export PATH=${ZAF_PLUGINS_DIR}/$2:$ZAF_LIB_DIR:\$PATH; cd ${ZAF_PLUGINS_DIR}/$2; . $ZAF_LIB_DIR/preload.sh; " env="export ITEM_KEY='$ikey'; export PLUGIN='$2'; export PATH=${ZAF_PLUGINS_DIR}/$2:$ZAF_LIB_DIR:\$PATH; cd ${ZAF_PLUGINS_DIR}/$2; . $ZAF_LIB_DIR/preload.sh; "
lock=$(zaf_ctrl_get_extitem_option $1 $i "Lock") lock=$(zaf_ctrl_get_extitem_option $1 $i "Lock")
if [ -n "$lock" ]; then if [ -n "$lock" ]; then
lock="${ZAF_LIB_DIR}/zaflock $lock " lock="${ZAF_LIB_DIR}/zaflock $lock "
fi fi
cache=$(zaf_ctrl_get_extitem_option $1 $i "Cache") cache=$(zaf_ctrl_get_extitem_option $1 $i "Cache")
if [ -n "$cache" ]; then if [ -n "$cache" ]; then
cache="${ZAF_LIB_DIR}/zafcache '$cache' " cache="${ZAF_LIB_DIR}/zafcache '$cache' "
fi fi
cmd=$(zaf_ctrl_get_extitem_option $1 $i "Cmd") ret=$(zaf_ctrl_get_extitem_option $1 $i "Return")
if [ -n "$cmd" ]; then retnull=$(zaf_ctrl_get_extitem_option $1 $i "Return-null")
echo "#!/bin/sh" >${ZAF_SERVER_EXTSCRIPTS}/$ikey reterr=$(zaf_ctrl_get_extitem_option $1 $i "Return-error")
chmod +x ${ZAF_SERVER_EXTSCRIPTS}/$ikey if [ -n "$ret" ] || [ -n "$reterr" ] || [ -n "$retnull" ]; then
(printf "%s" "${env}${zafparams}${preload}${cache}${lock}${cmd}"; echo) >>${ZAF_SERVER_EXTSCRIPTS}/$ikey retscr=" 1>\${tmpf}o 2>\${tmpf}e; ${ZAF_LIB_DIR}/zafret \${tmpf}o \${tmpf}e \$? '$ret' '$retnull' '$retempty' \$*";
continue else
fi retscr="";
cmd=$(zaf_ctrl_get_extitem_option $1 $i "Script") fi
if [ -n "$cmd" ]; then cmd=$(zaf_ctrl_get_extitem_option "$1" "$i" "Cmd")
echo "#!/bin/sh" >${ZAF_SERVER_EXTSCRIPTS}/$ikey if [ -n "$cmd" ]; then
chmod +x ${ZAF_SERVER_EXTSCRIPTS}/$ikey echo "#!/bin/sh" >"${ZAF_SERVER_EXTSCRIPTS}/$ikey"
(printf "%s" "${env}${zafparams}${preload}${cache}${lock}${cmd}"; echo) >>${ZAF_SERVER_EXTSCRIPTS}/$ikey chmod +x "${ZAF_SERVER_EXTSCRIPTS}/$ikey"
continue; (printf "%s" "${env}${zafparams}${preload}${cache}${lock}${cmd}${retscr}"; echo) >>"${ZAF_SERVER_EXTSCRIPTS}/$ikey"
fi continue
zaf_err "External item $i declared in control file but has no Cmd, Function or Script!" fi
cmd=$(zaf_ctrl_get_extitem_option "$1" "$i" "Script")
if [ -n "$cmd" ]; then
echo "#!/bin/sh" >"${ZAF_SERVER_EXTSCRIPTS}/$ikey"
chmod +x "${ZAF_SERVER_EXTSCRIPTS}/$ikey"
(printf "%s" "${env}${zafparams}${preload}${cache}${lock}${cmd}"; echo) >>"${ZAF_SERVER_EXTSCRIPTS}/$ikey"
continue;
fi
zaf_err "External item $i declared in control file but has no Cmd, Function or Script!"
done done
) || zaf_err "Error during zaf_ctrl_generate_extitems_cfg" ) || zaf_err "Error during zaf_ctrl_generate_extitems_cfg"
rm -f $tmpfile rm -f $tmpfile

View File

@ -81,8 +81,8 @@ zaf_prepare_plugin() {
url=$(zaf_get_plugin_url "$1")/control.zaf || exit $? url=$(zaf_get_plugin_url "$1")/control.zaf || exit $?
plugindir="$2" plugindir="$2"
control=${plugindir}/control.zaf control=${plugindir}/control.zaf
if [ "$(zaf_url_info $1)" = "path" ] && cmp -s "$url" "$control"; then if [ "$(zaf_url_info $1)" = "path" ] && [ "$url" = "$control" ]; then
zaf_err "prepare_plugin: Cannot install from itself!" zaf_err "prepare_plugin: Cannot install from itself ($url,$control)!"
fi fi
zaf_install_dir "$plugindir" zaf_install_dir "$plugindir"
zaf_dbg "Fetching control file from $url ..." zaf_dbg "Fetching control file from $url ..."
@ -256,19 +256,27 @@ zaf_item_info() {
local plugin local plugin
local item local item
local param local param
local tmp
plugin=$(echo $1 | cut -d '.' -f 1) plugin=$(echo $1 | cut -d '.' -f 1)
item=$(echo $1 | cut -d '.' -f 2-) item=$(echo $1 | cut -d '.' -f 2-)
if zaf_is_plugin $plugin; then if zaf_is_plugin $plugin; then
if zaf_ctrl_get_items <$ZAF_PLUGINS_DIR/$plugin/control.zaf | grep -wq "$item"; then if zaf_ctrl_get_items <$ZAF_PLUGINS_DIR/$plugin/control.zaf | grep -wq "$item"; then
echo "Item $1:" printf "%b" "Item $1\n\n"
echo -n "Cache: "; zaf_ctrl_get_item_option $ZAF_PLUGINS_DIR/$plugin/control.zaf "$item" "Cache"; echo tmp=$(zaf_ctrl_get_item_option $ZAF_PLUGINS_DIR/$plugin/control.zaf "$item" "Cache")
echo "Parameters:" [ -n "$tmp" ] && printf "%b" "Cache: $tmp \n\n"
zaf_ctrl_get_item_option $ZAF_PLUGINS_DIR/$plugin/control.zaf "$item" "Parameters" ; echo tmp=$(zaf_ctrl_get_item_option $ZAF_PLUGINS_DIR/$plugin/control.zaf "$item" "Parameters")
echo "Testparameters:" [ -n "$tmp" ] && printf "%b" "Parameters:\n$tmp\n\n"
zaf_ctrl_get_item_option $ZAF_PLUGINS_DIR/$plugin/control.zaf "$item" "Testparameters"; echo tmp=$(zaf_ctrl_get_item_option $ZAF_PLUGINS_DIR/$plugin/control.zaf "$item" "Return")
echo "Precache:" [ -n "$tmp" ] && printf "%b" "Return: $tmp\n\n"
zaf_ctrl_get_item_option $ZAF_PLUGINS_DIR/$plugin/control.zaf "$item" "Precache"; echo tmp=$(zaf_ctrl_get_item_option $ZAF_PLUGINS_DIR/$plugin/control.zaf "$item" "Return-null")
[ -n "$tmp" ] && printf "%b" "Return-null: $tmp\n\n"
tmp=$(zaf_ctrl_get_item_option $ZAF_PLUGINS_DIR/$plugin/control.zaf "$item" "Return-empty")
[ -n "$tmp" ] && printf "%b" "Return-empty: $tmp\n\n"
tmp=$(zaf_ctrl_get_item_option $ZAF_PLUGINS_DIR/$plugin/control.zaf "$item" "Testparameters")
[ -n "$tmp" ] && printf "%b" "Testparameters: $tmp\n\n"
tmp=$(zaf_ctrl_get_item_option $ZAF_PLUGINS_DIR/$plugin/control.zaf "$item" "Precache")
[ -n "$tmp" ] && printf "%b" "Precache: $tmp\n\n"
grep "UserParameter=$1" $ZAF_AGENT_CONFIGD/zaf_${plugin}.conf grep "UserParameter=$1" $ZAF_AGENT_CONFIGD/zaf_${plugin}.conf
else else
zaf_err "No such item $item." zaf_err "No such item $item."

View File

@ -146,6 +146,11 @@ zaf_far(){
eval $sedcmd eval $sedcmd
} }
# Trim spaces and newlines from string
zaf_trim(){
tr -d '\n'
}
# Limit concurrent processes or continue # Limit concurrent processes or continue
zaf_bglimit(){ zaf_bglimit(){
local maxbg local maxbg
@ -255,7 +260,7 @@ zaf_toupper() {
# Return simplified key with discarded special chars. # Return simplified key with discarded special chars.
zaf_stripctrl() { zaf_stripctrl() {
echo $1 | tr '[]*&;:.-' '________' echo $1 | tr '[]*&;:.-' '________' | tr -d '\n'
} }
# Unescape string on stdin # Unescape string on stdin
@ -279,7 +284,7 @@ zaf_date_add() {
# Create temp file and return its name # Create temp file and return its name
# $1 prefix or empty # $1 prefix or empty
zaf_tmpfile() { zaf_tmpfile() {
echo "$ZAF_TMP_DIR/tmp$1" echo "$ZAF_TMP_DIR/tmp$1$(zaf_random)"
} }
# return random number # return random number

52
lib/zafret Executable file
View File

@ -0,0 +1,52 @@
#!/bin/sh
. $(dirname $0)/preload.sh
if [ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ]; then
zaf_err "zafret valuefile stderrfile retcode [return return_if_null return_if_error]"
exit 1
fi
value="$(cat $1)"
rm -f $1
shift
stderr="$(cat $1)"
rm -f $1
shift
retcode="$1"
shift
if [ -n "$1" ]; then
ret="$1"
shift
fi
if [ -n "$1" ]; then
retnull="$1"
shift
fi
if [ -n "$1" ]; then
reterr="$1"
shift
fi
if [ "$retcode" != 0 ]; then
echo "$reterr"
zaf_wrn "${ITEM_KEY}("$*"): Error code '$retcode', returning '$reterr' ($stderr)"
exit $retcode
fi
if [ -z "$value" ]; then
echo "$retnull"
zaf_wrn "${ITEM_KEY}("$*"): Got empty result, returning '$retnull' ($stderr)"
exit
fi
echo $value
if [ -n "$stderr" ]; then
zaf_wrn "${ITEM_KEY}("$*"): $stderr"
fi