From a9a0ff95287e23cc052205b873f88cd92747e258 Mon Sep 17 00:00:00 2001 From: Lukas Macura Date: Sun, 11 Dec 2016 13:06:05 +0100 Subject: [PATCH] Support for global plugin parameters --- lib/plugin.lib.sh | 57 ++++++++++++++++++++++++++++++++++++++++++----- lib/preload.sh | 12 ++++++++++ lib/zaf.lib.sh | 2 +- zaf | 17 +++++++++++++- 4 files changed, 80 insertions(+), 8 deletions(-) diff --git a/lib/plugin.lib.sh b/lib/plugin.lib.sh index c3e1c17..2a79816 100644 --- a/lib/plugin.lib.sh +++ b/lib/plugin.lib.sh @@ -40,14 +40,15 @@ zaf_plugin_info() { local items ! [ -f "$control" ] && zaf_err "Control file $control not found." - plugin=$(zaf_ctrl_get_global_block <"${control}" | zaf_block_get_option Plugin) - pdescription=$(zaf_ctrl_get_global_block <"${control}" | zaf_block_get_moption Description) - pmaintainer=$(zaf_ctrl_get_global_block <"${control}" | zaf_block_get_option Maintainer) - pversion=$(zaf_ctrl_get_global_block <"${control}" | zaf_block_get_option Version) - purl=$(zaf_ctrl_get_global_block <"${control}" | zaf_block_get_option Url) - phome=$(zaf_ctrl_get_global_block <"${control}" | zaf_block_get_option Home) + plugin=$(zaf_ctrl_get_global_option "${control}" Plugin) + pdescription=$(zaf_ctrl_get_global_option "${control}" Description) + pmaintainer=$(zaf_ctrl_get_global_option "${control}" Maintainer) + pversion=$(zaf_ctrl_get_global_option "${control}" Version) + purl=$(zaf_ctrl_get_global_option "${control}" Url) + phome=$(zaf_ctrl_get_global_option "${control}" Home) pitems=$(zaf_ctrl_get_items <"${control}") peitems=$(zaf_ctrl_get_extitems <"${control}") + params=$(zaf_ctrl_get_global_option "${control}" Parameters) echo echo -n "Plugin '$plugin' "; [ -n "$pversion" ] && echo -n "version ${pversion}"; echo ":" echo "$pdescription"; echo @@ -57,18 +58,41 @@ zaf_plugin_info() { echo if zaf_is_plugin "$(basename $plugin)"; then items=$(zaf_list_plugin_items $plugin) + if [ -n "$params" ]; then + printf "%b" "Plugin parameters: (name,default,actual value)\n" + zaf_ctrl_get_global_option "${control}" Parameters | while read param default ; do + printf "%b" "$param\t$default\t$(zaf_get_plugin_parameter $(dirname $1) $param)\n" + done + echo; + fi [ -n "$items" ] && { echo -n "Defined items: "; echo $items; } items=$(zaf_list_plugin_items $plugin test) [ -n "$items" ] && { echo -n "Test items: "; echo $items; } items=$(zaf_list_plugin_items $plugin precache) [ -n "$items" ] && { echo -n "Precache items: "; echo $items; } [ -n "$peitems" ] && { echo -n "External check items: "; echo $peitems; } + else echo "Items: $pitems" fi echo } +# Set plugin global parameter +# $1 plugindir +# $2 parameter +# $3 value +zaf_set_plugin_parameter() { + printf "%s" "$3" >"${INSTALL_PREFIX}/${1}/${2}.value" +} + +# Get plugin global parameter +# $1 plugindir +# $2 parameter +zaf_get_plugin_parameter() { + [ -f "${1}/${2}.value" ] && cat "${1}/${2}.value" +} + # Prepare plugin into dir # $1 is url, directory or plugin name (will be searched in default plugin dir). # $2 is directory to prepare. @@ -104,6 +128,9 @@ zaf_install_plugin() { local tmpplugindir local control local version + local eparam + local param + local default plugin=$(basename "$1") plugindir="${ZAF_PLUGINS_DIR}/${plugin}" @@ -125,6 +152,24 @@ zaf_install_plugin() { zaf_touch "${plugindir}/postinst.need" fi zaf_ctrl_install "$url" "${control}" "${plugindir}" + rm "${plugindir}/params" + zaf_touch "${plugindir}/params" + (zaf_ctrl_get_global_option "${control}" "Parameters"; echo) | \ + while read param default; do + [ -z "$param" ] && continue + echo $param >>"${plugindir}/params" + eval eparam=\$ZAF_${plugin}_${param} + if [ -z "$eparam" ] && ! zaf_get_plugin_parameter "$plugindir" "$param" >/dev/null; then + zaf_wrn "Do not forget to set parameter $param. Use zaf plugin-set $plugin $param value. Default is $default." + zaf_set_plugin_parameter "$plugindir" "$param" "$default" + else + if [ -n "$eparam" ]; then + zaf_dbg "Setting $param to $eparam from env." + zaf_set_plugin_parameter "$plugindir" "$param" "$eparam" + fi + fi + done + else zaf_err "Cannot install plugin '$plugin' to $plugindir!" fi diff --git a/lib/preload.sh b/lib/preload.sh index 2822c16..f90f63c 100644 --- a/lib/preload.sh +++ b/lib/preload.sh @@ -9,6 +9,14 @@ . ${ZAF_LIB_DIR}/zbxapi.lib.sh . ${ZAF_LIB_DIR}/cache.lib.sh +# Global plugin parameters +[ -f ./params ] && for p in $(cat ./params); do + var=$p + value="$(cat ${p}.value)" + zaf_trc "Global $p parameter $var=$value" + eval export $var="$value" +done + # Plugin specific functions if exists [ -f ./functions.sh ] && . ./functions.sh @@ -30,8 +38,12 @@ export PATH if [ "$(basename $0)" = "preload.sh" ] && [ -n "$*" ]; then tmpf=$(zaf_tmpfile preload) + export tmpf $@ 2>$tmpf [ -s $tmpf ] && zaf_wrn <$tmpf +else + tmpf=$(zaf_tmpfile preload) + export tmpf fi diff --git a/lib/zaf.lib.sh b/lib/zaf.lib.sh index 5643b39..a67f282 100644 --- a/lib/zaf.lib.sh +++ b/lib/zaf.lib.sh @@ -78,8 +78,8 @@ zaf_agentparm(){ echo "$value" | grep -qE "$regexp" || zaf_err "$ITEM_KEY: Bad parameter '$name' value '$value' (not in regexp '$regexp')." fi fi - eval $name=$value zaf_trc "$ITEM_KEY: Param $name set to $value" + eval $name=\"$value\" } # Fetch url to stdout diff --git a/zaf b/zaf index 76586c8..9956625 100755 --- a/zaf +++ b/zaf @@ -235,7 +235,7 @@ reinstall) done [ -n "$reinstalled" ] && zaf_is_root && zaf_restart_agent ;; -remove) +remove|uninstall) shift [ -z "$1" ] && echo "$0 remove plugin [plugin]..." for p in $@; do @@ -246,6 +246,21 @@ remove) done [ -n "$removed" ] && zaf_is_root && zaf_restart_agent ;; +plugin-set) + plugin="$2" + param="$3" + value="$4" + if [ -z "$plugin" ] || [ -z "$param" ]; then + zaf_err "$0 plugin-set plugin parameter value" + else + if zaf_is_plugin "$plugin"; then + plugindir="${ZAF_PLUGINS_DIR}/${plugin}" + zaf_set_plugin_parameter "$plugindir" "$param" "$value" + else + zaf_err "Plugin $plugin is not installed." + fi + fi + ;; ###### Zaf related commands self-upgrade)