1
0
mirror of https://github.com/limosek/zaf.git synced 2024-09-21 05:11:38 +02:00
limosek-zaf/lib/zaf.lib.sh

165 lines
3.4 KiB
Bash
Raw Normal View History

2016-03-23 17:25:18 +01:00
2016-03-24 09:38:34 +01:00
# Fetch url to stdout
# $1 url
# It supports real file, file:// and other schemes known by curl
2016-03-23 17:25:18 +01:00
zaf_fetch_url() {
local scheme
local uri
scheme=$(echo $1|cut -d ':' -f 1)
uri=$(echo $1|cut -d '/' -f 3-)
if [ "$1" = "$scheme" ]; then
cat "$1"
fi
case $scheme in
http|https|ftp|file)
curl -f -s -L -o - "$1";
;;
esac
}
2016-03-24 09:38:34 +01:00
# Find and replace string
2016-03-23 17:25:18 +01:00
zaf_far(){
local f
local t
local sedcmd="sed"
i=1
while [ "$i" -lt "$#" ];
do
eval f=\$${i}
i=$(expr $i + 1)
eval t=\$${i}
i=$(expr $i + 1)
sedcmd="$sedcmd -e 's~$f~$t~g'"
done
eval $sedcmd
}
2016-03-24 09:38:34 +01:00
# Restart zabbix agent
2016-03-23 17:25:18 +01:00
zaf_restart_agent() {
${ZAF_AGENT_RESTART}
}
2016-03-24 09:38:34 +01:00
# Check if zaf.version item is populated
2016-03-23 17:25:18 +01:00
zaf_check_agent_config() {
zaf_restart_agent
zabbix_agentd -t zaf.version
}
2016-03-23 14:37:51 +01:00
# Update repo
zaf_update_repo() {
2016-03-23 17:25:18 +01:00
[ -n "${ZAF_PLUGINS_REPO}" ] && cd ${ZAF_REPO_DIR} && git pull
2016-03-23 14:37:51 +01:00
}
# List installed plugins
zaf_list_installed_plugins() {
2016-03-23 17:25:18 +01:00
cd ${ZAF_PLUGINS_DIR}; ls --hide '.'
}
# Check plugin url
# $1 plugin uri
# $2 local file to fetch
zaf_plugin_fetch_control() {
[ -z "$1" ] && return -1
local name=$(basename "$1")
zaf_fetch_url "$1/control" >"$2"
}
# Get option from control file
# $1 control file
# $2 option
zaf_ctrl_get_option() {
grep -E '^(.*): ' "$1" | grep -F "$2:" | cut -d ' ' -f 2-
}
zaf_ctrl_binary_deps() {
local deps
deps=$(zaf_ctrl_get_option "$1" Binary-Depends)
for cmd in $deps; do
if ! which $cmd >/dev/null; then
echo "Missing binary dependency $cmd. Please install it first."
exit 5
fi
done
}
zaf_ctrl_install_bin() {
local binaries
local pdir
binaries=$(zaf_ctrl_get_option "$1" Install-bin)
pdir="${ZAF_PLUGINS_DIR}/${2}/"
for b in $binaries; do
zaf_fetch_url "$url/$b" >"$pdir/$b"
chmod +x "$pdir/$b"
done
}
# Generates zabbix cfg from control file
# $1 control
# $2 pluginname
zaf_ctrl_generate_cfg() {
local items
local cmd
items=$(zaf_ctrl_get_option "$1" Item)
for i in $items; do
cmd=$(zaf_ctrl_get_option "$1" "Item-cmd-$i")
echo "UserParameter=$2.${i},$cmd"
done
2016-03-23 14:37:51 +01:00
}
# Install plugin.
# Parameter is url, directory or plugin name (will be searched in default plugin dir)
zaf_install_plugin() {
2016-03-23 17:25:18 +01:00
local url
local control
local plugin
local plugindir
if echo "$1" | grep -qv '/'; then
url="${ZAF_REPO_DIR}/$1"
else
url="$1"
fi
plugin=$(basename "$url")
echo Installing plugin $plugin from $url...
rm -rf ${ZAF_TMP_DIR}/${plugin}
control=${ZAF_TMP_DIR}/${plugin}/control
plugindir="${ZAF_PLUGINS_DIR}/${plugin}"
mkdir -p "${ZAF_TMP_DIR}/${plugin}"
if zaf_plugin_fetch_control "$url" "${control}"; then
set -e
zaf_ctrl_binary_deps "${control}"
mkdir -p $plugindir
zaf_ctrl_install_bin "${control}" "${plugin}"
zaf_ctrl_generate_cfg "${control}" "${plugin}" | zaf_far '{PLUGINDIR}' "$plugindir" >${ZAF_AGENT_CONFIGD}/zaf_${plugin}.conf
zaf_restart_agent
cp $control "$plugindir"/
zaf_fetch_url $url/template.xml >"$plugindir"/template.xml
else
echo "Cannot fetch control file!"
exit 4
fi
}
zaf_plugin_info() {
local items
local plugindir
plugindir="${ZAF_PLUGINS_DIR}/$1"
if [ -d "$plugindir" ]; then
items=$(zaf_ctrl_get_option "$plugindir/control" Item)
echo "Items supported:"
echo "$items"
else
echo "Plugin $1 not installed"
fi
}
zaf_remove_plugin() {
rm -rf ${ZAF_PLUGINS_DIR}/$1
rm -f ${ZAF_AGENT_CONFIGD}/zaf_${plugin}.conf
2016-03-23 14:37:51 +01:00
}
2016-03-23 17:25:18 +01:00