From 7f774f409d398612112af9dc3252e147e5a4aff2 Mon Sep 17 00:00:00 2001 From: Lukas Macura Date: Sun, 11 Dec 2016 16:20:06 +0100 Subject: [PATCH] Added and documented itemsh command --- README.md | 5 +++++ lib/ctrl.lib.sh | 16 +++++++++++++--- lib/plugin.lib.sh | 11 +++++++++++ zaf | 13 +++++++++++++ 4 files changed, 42 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 6d0db4a..7a8d8e7 100644 --- a/README.md +++ b/README.md @@ -231,6 +231,11 @@ To set parameters later: zaf plugin-set booked url http://booked.server/api zaf plugin-set booked username user ``` +Plugin parameters are available as environment variables in shell script/binary run by agent. You can simulate this environment by +``` +zaf itemsh booked.num_reservations +``` +Interactive shell will be spawn and you can test your commands or scripts in same way how zabbix agent would do it. Use exit command or ctrl-D to get back. ## How it works Zaf installer will do most of actions needed to monitor some specific plugin items. Configuration of plugin is very simple and text readable. Anybody can write its own plugin or make its plugin "zafable". It is enough to create *control.zaf" file. For example, look into https://github.com/limosek/zaf-plugins repository. This is default repository for zaf. diff --git a/lib/ctrl.lib.sh b/lib/ctrl.lib.sh index 082c5f5..b64bbc3 100644 --- a/lib/ctrl.lib.sh +++ b/lib/ctrl.lib.sh @@ -226,6 +226,8 @@ zaf_ctrl_install() { # Generates zabbix items cfg from control file # $1 control # $2 pluginname +# $3 if set, no script will be created +# $4 if set, cmd is set always to $4 zaf_ctrl_generate_items_cfg() { local items local cmd @@ -281,8 +283,11 @@ zaf_ctrl_generate_items_cfg() { else retscr=""; fi - cmd=$(zaf_ctrl_get_item_option $1 $i "Cmd") - + if [ -z "$4" ]; then + cmd=$(zaf_ctrl_get_item_option $1 $i "Cmd") + else + cmd="$4" + fi if [ -n "$cmd" ]; then printf "%s" "UserParameter=$ikey,${env}${zafparams}${preload}${cache}${lock}${cmd}${retscr}"; echo continue @@ -294,7 +299,12 @@ zaf_ctrl_generate_items_cfg() { 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 + if [ -z "$4" ]; then + script="${ZAF_PLUGINS_DIR}/$2/${iscript}.sh" + else + script="$4" + fi + printf "%s" "UserParameter=$ikey,${env}${preload}${zafparams}${cache}${lock}$script ${args}"; echo rm -f ${ZAF_TMP_DIR}/${iscript}.sh continue; fi diff --git a/lib/plugin.lib.sh b/lib/plugin.lib.sh index 2a79816..8546bf4 100644 --- a/lib/plugin.lib.sh +++ b/lib/plugin.lib.sh @@ -212,6 +212,17 @@ zaf_is_plugin() { false } +zaf_is_item() { + local plugin + local item + + plugin=$(echo $1|cut -d '.' -f 1) + item=$(echo $1|cut -d '.' -f 2) + [ -z "$plugin" ] || [ -z "$item" ] && return 1 + zaf_is_plugin "$plugin" && zaf_list_plugin_items "$plugin" | grep -qE "\.(${item}\$|${item}\[)" +} + + zaf_discovery_plugins() { zaf_list_plugins | zaf_discovery '{#PLUGIN}' } diff --git a/zaf b/zaf index 9956625..0ec84a5 100755 --- a/zaf +++ b/zaf @@ -175,6 +175,18 @@ test) done done ;; +itemsh) + shift + if zaf_is_item "$1"; then + plugin=$(echo $1|cut -d '.' -f 1) + item=$(echo $1|cut -d '.' -f 2) + script=$(zaf_ctrl_generate_items_cfg "${ZAF_PLUGINS_DIR}/${plugin}/control.zaf" "${plugin}" "" "sh -i #"| grep "UserParameter=$1" | cut -d ',' -f 2-) + eval $script + exit + else + zaf_err "No such plugin/item $1" + fi + ;; get) shift if echo $1|grep -q '\.'; then @@ -463,6 +475,7 @@ api) zaf_hlp "$0 test [plugin[.item]]" "To test [all] suported items by zabbix_agentd [for plugin]" zaf_hlp "$0 get [plugin[.item]]" "To test [all] suported items by zabbix_get [for plugin]" zaf_hlp "$0 precache [plugin[.item]]" "To precache [all] suported items" + zaf_hlp "$0 itemsh plugin.item" "To spawn interactive shell in item context (same as UserParameter)." echo echo "Zabbix API commands:" zaf_hlp "$0 api" "To zabbix API functions. See $0 api for more info."