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

Added and documented itemsh command

This commit is contained in:
Lukas Macura 2016-12-11 16:20:06 +01:00
parent 6204eacd6a
commit 7f774f409d
4 changed files with 42 additions and 3 deletions

View File

@ -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.

View File

@ -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

View File

@ -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}'
}

13
zaf
View File

@ -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."