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

@ -216,8 +216,8 @@ zaf_ctrl_install() {
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,16 +241,20 @@ 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
echo >>$tmpfile
zafparams="";
if [ -s "$tmpfile" ]; then if [ -s "$tmpfile" ]; then
ikey="$2.$i[*]" ikey="$2.$i[*]"
args="" args=""
apos=1; apos=1;
while read pname pdefault pregex prest; do while read pname pdefault pregex prest; do
[ -z "$pname" ] && continue
zaf_trc "Adding param $pname ($pdefault $pregex) to $i"
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)
@ -269,9 +273,18 @@ zaf_ctrl_generate_items_cfg() {
if [ -n "$cache" ]; then if [ -n "$cache" ]; then
cache="${ZAF_LIB_DIR}/zafcache '$cache' " cache="${ZAF_LIB_DIR}/zafcache '$cache' "
fi fi
ret=$(zaf_ctrl_get_item_option $1 $i "Return")
retnull=$(zaf_ctrl_get_item_option $1 $i "Return-null")
reterr=$(zaf_ctrl_get_item_option $1 $i "Return-error")
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") cmd=$(zaf_ctrl_get_item_option $1 $i "Cmd")
if [ -n "$cmd" ]; then if [ -n "$cmd" ]; then
printf "%s" "UserParameter=$ikey,${env}${zafparams}${preload}${cache}${lock}${cmd}"; echo printf "%s" "UserParameter=$ikey,${env}${zafparams}${preload}${cache}${lock}${cmd}${retscr}"; echo
continue continue
fi fi
cmd=$(zaf_ctrl_get_item_option $1 $i "Script") cmd=$(zaf_ctrl_get_item_option $1 $i "Script")
@ -288,7 +301,7 @@ zaf_ctrl_generate_items_cfg() {
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,11 +322,11 @@ 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=""
@ -336,18 +349,26 @@ zaf_ctrl_generate_extitems_cfg() {
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")
retnull=$(zaf_ctrl_get_extitem_option $1 $i "Return-null")
reterr=$(zaf_ctrl_get_extitem_option $1 $i "Return-error")
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_extitem_option "$1" "$i" "Cmd")
if [ -n "$cmd" ]; then if [ -n "$cmd" ]; then
echo "#!/bin/sh" >${ZAF_SERVER_EXTSCRIPTS}/$ikey echo "#!/bin/sh" >"${ZAF_SERVER_EXTSCRIPTS}/$ikey"
chmod +x ${ZAF_SERVER_EXTSCRIPTS}/$ikey chmod +x "${ZAF_SERVER_EXTSCRIPTS}/$ikey"
(printf "%s" "${env}${zafparams}${preload}${cache}${lock}${cmd}"; echo) >>${ZAF_SERVER_EXTSCRIPTS}/$ikey (printf "%s" "${env}${zafparams}${preload}${cache}${lock}${cmd}${retscr}"; echo) >>"${ZAF_SERVER_EXTSCRIPTS}/$ikey"
continue continue
fi fi
cmd=$(zaf_ctrl_get_extitem_option $1 $i "Script") cmd=$(zaf_ctrl_get_extitem_option "$1" "$i" "Script")
if [ -n "$cmd" ]; then if [ -n "$cmd" ]; then
echo "#!/bin/sh" >${ZAF_SERVER_EXTSCRIPTS}/$ikey echo "#!/bin/sh" >"${ZAF_SERVER_EXTSCRIPTS}/$ikey"
chmod +x ${ZAF_SERVER_EXTSCRIPTS}/$ikey chmod +x "${ZAF_SERVER_EXTSCRIPTS}/$ikey"
(printf "%s" "${env}${zafparams}${preload}${cache}${lock}${cmd}"; echo) >>${ZAF_SERVER_EXTSCRIPTS}/$ikey (printf "%s" "${env}${zafparams}${preload}${cache}${lock}${cmd}"; echo) >>"${ZAF_SERVER_EXTSCRIPTS}/$ikey"
continue; continue;
fi fi
zaf_err "External item $i declared in control file but has no Cmd, Function or Script!" zaf_err "External item $i declared in control file but has no Cmd, Function or Script!"

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