mirror of
https://github.com/limosek/zaf.git
synced 2025-11-01 18:17:37 +01:00
Support for plugin subcommands
This commit is contained in:
@@ -195,7 +195,7 @@ zaf_ctrl_cron() {
|
||||
zaf_ctrl_get_global_option $2 "Cron" | zaf_far '{PLUGINDIR}' "${plugindir}" >$ZAF_CROND/zaf_$plugin || zaf_err "Error during zaf_ctrl_cron"
|
||||
}
|
||||
|
||||
# Install files defined to be installed in control to plugun directory
|
||||
# Install files defined to be installed in control to plugin directory
|
||||
# $1 pluginurl
|
||||
# $2 control
|
||||
# $3 plugindir
|
||||
@@ -209,7 +209,7 @@ zaf_ctrl_install() {
|
||||
|
||||
pdir="$3"
|
||||
(set -e
|
||||
binaries=$(zaf_ctrl_get_global_option $2 "Install-bin")
|
||||
binaries=$(zaf_ctrl_get_global_option $2 "Install-bin")" "$(zaf_ctrl_get_global_option $2 "Install-cmd")
|
||||
for b in $binaries; do
|
||||
zaf_fetch_url "$1/$b" >"${ZAF_TMP_DIR}/$b"
|
||||
zaf_install_bin "${ZAF_TMP_DIR}/$b" "$pdir"
|
||||
|
||||
@@ -223,7 +223,7 @@ zaf_is_item() {
|
||||
local item
|
||||
|
||||
plugin=$(echo $1|cut -d '.' -f 1)
|
||||
item=$(echo $1|cut -d '.' -f 2)
|
||||
item=$(echo $1|cut -d '.' -f 2- | cut -d '[' -f 1)
|
||||
[ -z "$plugin" ] || [ -z "$item" ] && return 1
|
||||
zaf_is_plugin "$plugin" && zaf_list_plugin_items "$plugin" | grep -qE "\.(${item}\$|${item}\[)"
|
||||
}
|
||||
@@ -314,6 +314,48 @@ zaf_list_plugin_items() {
|
||||
echo
|
||||
}
|
||||
|
||||
zaf_list_plugin_commands() {
|
||||
local plugindir
|
||||
local commands
|
||||
|
||||
if ! zaf_is_plugin "$1"; then
|
||||
zaf_err "Missing plugin name or plugin $1 unknown. ";
|
||||
fi
|
||||
plugindir="${ZAF_PLUGINS_DIR}/$1"
|
||||
commands=$(zaf_ctrl_get_global_option "${plugindir}/control.zaf" Install-cmd)
|
||||
if [ -n "$commands" ]; then
|
||||
echo $commands;
|
||||
else
|
||||
zaf_wrn "Plugin $1 has no commands."
|
||||
fi
|
||||
}
|
||||
|
||||
# $1 plugin
|
||||
# $2 cmd
|
||||
# $3...$9 arguments
|
||||
zaf_plugin_run_command() {
|
||||
local plugin
|
||||
local cmd
|
||||
local args
|
||||
|
||||
plugin=$1
|
||||
cmd=$2
|
||||
args="$3 $4 $5 $6 $7 $8 $9"
|
||||
if ! zaf_is_plugin "$plugin"; then
|
||||
zaf_err "Missing plugin name or plugin $olugin unknown. ";
|
||||
fi
|
||||
plugindir="${ZAF_PLUGINS_DIR}/$plugin"
|
||||
commands=$(zaf_ctrl_get_global_option "${plugindir}/control.zaf" Install-cmd)
|
||||
if echo $commands |grep -q $cmd; then
|
||||
export PATH=$plugindir:$PATH
|
||||
cd $plugindir
|
||||
[ -f functions.sh ] && source functions.sh
|
||||
exec $cmd $args;
|
||||
else
|
||||
zaf_err "Plugin $1 has no command $cmd"
|
||||
fi
|
||||
}
|
||||
|
||||
zaf_item_info() {
|
||||
local plugin
|
||||
local item
|
||||
@@ -375,6 +417,15 @@ zaf_test_item() {
|
||||
fi
|
||||
}
|
||||
|
||||
zaf_run_item() {
|
||||
local item
|
||||
item=$(echo $1| cut -d '[' -f 1)
|
||||
params=$(echo $1| cut -d '[' -f 2- | tr ',' ' '| tr -d ']')
|
||||
cmd=$(grep "^UserParameter=$item" $ZAF_AGENT_CONFIGD/zaf*.conf | cut -d ',' -f 2-)
|
||||
echo $cmd >/tmp/a
|
||||
sh /tmp/a $params
|
||||
}
|
||||
|
||||
zaf_precache_item() {
|
||||
cmd=$(grep "^UserParameter=$item" $ZAF_AGENT_CONFIGD/zaf*.conf | cut -d ',' -f 2- | sed -e "s/_cache/_nocache/")
|
||||
zaf_wrn "Precaching item $item[$(echo $*| tr ' ' ',')] ($cmd)"
|
||||
|
||||
@@ -301,3 +301,56 @@ zaf_sudo() {
|
||||
fi
|
||||
}
|
||||
|
||||
# Get item name from plugin.item[parms]
|
||||
zaf_get_plugin_name() {
|
||||
echo $1|cut -d '.' -f 1
|
||||
}
|
||||
|
||||
# Get item name from plugin.item[parms]
|
||||
zaf_get_item_name() {
|
||||
echo $1|cut -d '.' -f 2-|cut -d '[' -f 1
|
||||
}
|
||||
|
||||
# Get item params from plugin.item[parms]
|
||||
zaf_get_item_params() {
|
||||
echo $1|cut -d '[' -f 2|cut -d ']' -f 1
|
||||
}
|
||||
|
||||
# Convert Zabbix style parameters [param1,param2,..] into $1 $2 $3
|
||||
zaf_paramstosh() {
|
||||
local parms
|
||||
local IFS
|
||||
parms=$(echo $*|cut -d '[' -f 2 | cut -d ']' -f 1| tr ',' ':')
|
||||
IFS=:; for i in $parms; do
|
||||
if [ -n "$i" ]; then
|
||||
printf "$i "
|
||||
else
|
||||
printf "'' "
|
||||
fi
|
||||
done
|
||||
}
|
||||
|
||||
#Returns either actual hostname or configured one
|
||||
zaf_hostname() {
|
||||
if [ -z "$ZAF_HOSTNAME" ]; then
|
||||
hostname
|
||||
else
|
||||
echo $ZAF_HOSTNAME
|
||||
fi
|
||||
}
|
||||
|
||||
# Send active agent query (used for auto registration of host)
|
||||
# $1 hostname
|
||||
# $2 metadata
|
||||
zaf_register() {
|
||||
local query
|
||||
local result
|
||||
zaf_dbg "zaf_register $1 $2"
|
||||
query="{\"host\": \"$1\", \"host_metadata\": \"$2\", \"request\": \"active checks\"}"
|
||||
result="$(echo $query| nc -q1 $ZAF_ZBXSRV_HOST $ZAF_ZBXSRV_PORT | tail -c +14)"
|
||||
if echo $result |grep -q failed; then
|
||||
zaf_wrn $result
|
||||
fi
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user