diff --git a/install.sh b/install.sh index 07c67ed..25b6bb0 100755 --- a/install.sh +++ b/install.sh @@ -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 zaf_install $i ${ZAF_LIB_DIR}/ 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}/ done zaf_install_dir ${ZAF_BIN_DIR} diff --git a/lib/ctrl.lib.sh b/lib/ctrl.lib.sh index 1218cc9..082c5f5 100644 --- a/lib/ctrl.lib.sh +++ b/lib/ctrl.lib.sh @@ -104,7 +104,7 @@ zaf_ctrl_get_item_option() { 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 + fi } # Get external item specific option (single or multiline) @@ -212,12 +212,12 @@ zaf_ctrl_install() { binaries=$(zaf_ctrl_get_global_option $2 "Install-bin") for b in $binaries; do 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 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" + zaf_fetch_url "$1/$f" >"${ZAF_TMP_DIR}/$f" + zaf_install "${ZAF_TMP_DIR}/$f" "$pdir" done true ) || zaf_err "Error during zaf_ctrl_install" @@ -241,54 +241,67 @@ zaf_ctrl_generate_items_cfg() { local zafparms items=$(zaf_ctrl_get_items <"$1") - tmpfile=$ZAF_TMP_DIR/gencfg$$ + tmpfile=$(zaf_tmpfile genparms) (set -e for i in $items; do - iscript=$(zaf_stripctrl $i) - zaf_ctrl_get_item_option $1 $i "Parameters" >$tmpfile - if [ -s "$tmpfile" ]; then - ikey="$2.$i[*]" - args="" - apos=1; - while read pname pdefault pregex prest; do - zafparams="$zafparams value=\"\$$apos\"; zaf_agentparm $pname $pdefault $pregex; export $pname; " - args="$args \$$apos" - apos=$(expr $apos + 1) - done <$tmpfile - else - ikey="$2.$i" - zafparams="" - args="" - 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; " - lock=$(zaf_ctrl_get_item_option $1 $i "Lock") - if [ -n "$lock" ]; then - lock="${ZAF_LIB_DIR}/zaflock $lock " - fi - cache=$(zaf_ctrl_get_item_option $1 $i "Cache") - if [ -n "$cache" ]; then - cache="${ZAF_LIB_DIR}/zafcache '$cache' " - fi - cmd=$(zaf_ctrl_get_item_option $1 $i "Cmd") - if [ -n "$cmd" ]; then - printf "%s" "UserParameter=$ikey,${env}${zafparams}${preload}${cache}${lock}${cmd}"; echo - continue - fi - cmd=$(zaf_ctrl_get_item_option $1 $i "Script") - if [ -n "$cmd" ]; then - ( echo "#!/bin/sh" + iscript=$(zaf_stripctrl $i) + zaf_ctrl_get_item_option $1 $i "Parameters" >$tmpfile + echo >>$tmpfile + zafparams=""; + if [ -s "$tmpfile" ]; then + ikey="$2.$i[*]" + args="" + apos=1; + 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; " + args="$args \$$apos" + apos=$(expr $apos + 1) + done <$tmpfile + else + ikey="$2.$i" + zafparams="" + args="" + 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; " + lock=$(zaf_ctrl_get_item_option $1 $i "Lock") + if [ -n "$lock" ]; then + lock="${ZAF_LIB_DIR}/zaflock $lock " + fi + cache=$(zaf_ctrl_get_item_option $1 $i "Cache") + if [ -n "$cache" ]; then + cache="${ZAF_LIB_DIR}/zafcache '$cache' " + 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") + + 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; " zaf_ctrl_get_item_option $1 $i "Script" ) >${ZAF_TMP_DIR}/${iscript}.sh; - [ -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 - rm -f ${ZAF_TMP_DIR}/${iscript}.sh - continue; - fi - zaf_err "Item $i declared in control file but has no Cmd, Function or Script!" + [ -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 + rm -f ${ZAF_TMP_DIR}/${iscript}.sh + continue; + fi + zaf_err "Item $i declared in control file but has no Cmd, Function or Script!" done ) || 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 @@ -309,48 +322,56 @@ zaf_ctrl_generate_extitems_cfg() { local zafparms items=$(zaf_ctrl_get_extitems <"$1") - tmpfile=$ZAF_TMP_DIR/gencfg$$ + tmpfile=$(zaf_tmpfile genparms) (set -e for i in $items; do - iscript=$(zaf_stripctrl $i) - zaf_ctrl_get_extitem_option $1 $i "Parameters" >$tmpfile - ikey="$2.$i" - if [ -s "$tmpfile" ]; then - args="" - apos=1; - while read pname pdefault pregex prest; do - zafparams="$zafparams value=\"\$$apos\"; zaf_agentparm $pname $pdefault $pregex; export $pname; " - args="$args \$$apos" - apos=$(expr $apos + 1) - done <$tmpfile - else - zafparams="" - args="" - 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; " - lock=$(zaf_ctrl_get_extitem_option $1 $i "Lock") - if [ -n "$lock" ]; then - lock="${ZAF_LIB_DIR}/zaflock $lock " - fi - cache=$(zaf_ctrl_get_extitem_option $1 $i "Cache") - if [ -n "$cache" ]; then - cache="${ZAF_LIB_DIR}/zafcache '$cache' " - fi - cmd=$(zaf_ctrl_get_extitem_option $1 $i "Cmd") - 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 - 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!" + iscript=$(zaf_stripctrl $i) + (zaf_ctrl_get_extitem_option $1 $i "Parameters"; echo) >$tmpfile + ikey="$2.$i" + if [ -s "$tmpfile" ]; then + args="" + apos=1; + while read pname pdefault pregex prest; do + zafparams="$zafparams value=\"\$$apos\"; zaf_agentparm $pname $pdefault $pregex; export $pname; " + args="$args \$$apos" + apos=$(expr $apos + 1) + done <$tmpfile + else + zafparams="" + args="" + 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; " + lock=$(zaf_ctrl_get_extitem_option $1 $i "Lock") + if [ -n "$lock" ]; then + lock="${ZAF_LIB_DIR}/zaflock $lock " + fi + cache=$(zaf_ctrl_get_extitem_option $1 $i "Cache") + if [ -n "$cache" ]; then + cache="${ZAF_LIB_DIR}/zafcache '$cache' " + fi + 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 + echo "#!/bin/sh" >"${ZAF_SERVER_EXTSCRIPTS}/$ikey" + chmod +x "${ZAF_SERVER_EXTSCRIPTS}/$ikey" + (printf "%s" "${env}${zafparams}${preload}${cache}${lock}${cmd}${retscr}"; echo) >>"${ZAF_SERVER_EXTSCRIPTS}/$ikey" + continue + 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 ) || zaf_err "Error during zaf_ctrl_generate_extitems_cfg" rm -f $tmpfile diff --git a/lib/plugin.lib.sh b/lib/plugin.lib.sh index 1094896..c3e1c17 100644 --- a/lib/plugin.lib.sh +++ b/lib/plugin.lib.sh @@ -81,8 +81,8 @@ zaf_prepare_plugin() { url=$(zaf_get_plugin_url "$1")/control.zaf || exit $? plugindir="$2" control=${plugindir}/control.zaf - if [ "$(zaf_url_info $1)" = "path" ] && cmp -s "$url" "$control"; then - zaf_err "prepare_plugin: Cannot install from itself!" + if [ "$(zaf_url_info $1)" = "path" ] && [ "$url" = "$control" ]; then + zaf_err "prepare_plugin: Cannot install from itself ($url,$control)!" fi zaf_install_dir "$plugindir" zaf_dbg "Fetching control file from $url ..." @@ -256,19 +256,27 @@ zaf_item_info() { local plugin local item local param + local tmp plugin=$(echo $1 | cut -d '.' -f 1) item=$(echo $1 | cut -d '.' -f 2-) if zaf_is_plugin $plugin; then if zaf_ctrl_get_items <$ZAF_PLUGINS_DIR/$plugin/control.zaf | grep -wq "$item"; then - echo "Item $1:" - echo -n "Cache: "; zaf_ctrl_get_item_option $ZAF_PLUGINS_DIR/$plugin/control.zaf "$item" "Cache"; echo - echo "Parameters:" - zaf_ctrl_get_item_option $ZAF_PLUGINS_DIR/$plugin/control.zaf "$item" "Parameters" ; echo - echo "Testparameters:" - zaf_ctrl_get_item_option $ZAF_PLUGINS_DIR/$plugin/control.zaf "$item" "Testparameters"; echo - echo "Precache:" - zaf_ctrl_get_item_option $ZAF_PLUGINS_DIR/$plugin/control.zaf "$item" "Precache"; echo + printf "%b" "Item $1\n\n" + tmp=$(zaf_ctrl_get_item_option $ZAF_PLUGINS_DIR/$plugin/control.zaf "$item" "Cache") + [ -n "$tmp" ] && printf "%b" "Cache: $tmp \n\n" + tmp=$(zaf_ctrl_get_item_option $ZAF_PLUGINS_DIR/$plugin/control.zaf "$item" "Parameters") + [ -n "$tmp" ] && printf "%b" "Parameters:\n$tmp\n\n" + tmp=$(zaf_ctrl_get_item_option $ZAF_PLUGINS_DIR/$plugin/control.zaf "$item" "Return") + [ -n "$tmp" ] && printf "%b" "Return: $tmp\n\n" + 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 else zaf_err "No such item $item." diff --git a/lib/zaf.lib.sh b/lib/zaf.lib.sh index fec646a..5643b39 100644 --- a/lib/zaf.lib.sh +++ b/lib/zaf.lib.sh @@ -146,6 +146,11 @@ zaf_far(){ eval $sedcmd } +# Trim spaces and newlines from string +zaf_trim(){ + tr -d '\n' +} + # Limit concurrent processes or continue zaf_bglimit(){ local maxbg @@ -255,7 +260,7 @@ zaf_toupper() { # Return simplified key with discarded special chars. zaf_stripctrl() { - echo $1 | tr '[]*&;:.-' '________' + echo $1 | tr '[]*&;:.-' '________' | tr -d '\n' } # Unescape string on stdin @@ -279,7 +284,7 @@ zaf_date_add() { # Create temp file and return its name # $1 prefix or empty zaf_tmpfile() { - echo "$ZAF_TMP_DIR/tmp$1" + echo "$ZAF_TMP_DIR/tmp$1$(zaf_random)" } # return random number diff --git a/lib/zafret b/lib/zafret new file mode 100755 index 0000000..ae67160 --- /dev/null +++ b/lib/zafret @@ -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 +