1
0
mirror of https://github.com/limosek/zaf.git synced 2025-11-01 01:57:38 +01:00

Working on plugin installation with newer control

This commit is contained in:
Lukas Macura
2016-03-31 15:56:23 +02:00
parent 5a811dedf6
commit 2e5d650145
5 changed files with 259 additions and 154 deletions

View File

@@ -1,14 +1,5 @@
# Control file related functions
# 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.zaf" >"$2"
}
# Get block from stdin
# $1 option
# $2 name
@@ -24,7 +15,10 @@ zaf_ctrl_get_item_block() {
getline;
if (/^\/Item/) exit;
print $0;
}}'
}};
END {
exit i==0;
}'
}
# Get global plugin block body from stdin
@@ -44,26 +38,44 @@ zaf_block_get_moption() {
awk '/^'$1'::$/ { i=0;
while (i==0) {
getline;
if (/^::$/) exit;
if (/^::$/) {i=1; continue;};
print $0;
}}'
}};
END {
exit i==0;
}
'
}
# Get item singleline option
# Get item singleline option from config block on stdin
# $1 optionname
zaf_block_get_option() {
grep "^$1:" | cut -d ' ' -f 2- | tr -d '\r\n'
}
# Get global option (single or multiline)
# $1 - control file
# $2 - option name
zaf_ctrl_get_global_option() {
zaf_ctrl_get_global_block <$1 | zaf_block_get_moption "$2" || zaf_ctrl_get_global_block <$1 | zaf_block_get_option "$2"
}
# Get item specific option (single or multiline)
# $1 - control file
# $2 - item name
# $3 - option name
zaf_ctrl_get_item_option() {
zaf_ctrl_get_item_block <$1 "$2" | zaf_block_get_moption "$3" || zaf_ctrl_get_item_block <$1 "$2" | zaf_block_get_option "$3"
}
zaf_ctrl_check_deps() {
local deps
deps=$(zaf_ctrl_get_global_block <$1 | zaf_block_get_option "Depends-${ZAF_PKG}" )
zaf_check_deps $deps
zaf_os_specific zaf_check_deps $deps
deps=$(zaf_ctrl_get_global_block <$1 | zaf_block_get_option "Depends-bin" )
for cmd in $deps; do
if ! which $cmd >/dev/null; then
echo "Missing binary dependency $cmd. Please install it first."
return 1
zaf_wrn "Missing binary dependency $cmd. Please install it first."
return 1
fi
done
}
@@ -80,11 +92,11 @@ zaf_ctrl_install() {
pdir="$2"
binaries=$(zaf_ctrl_get_global_block <$1 | zaf_block_get_option "Install-bin" | zaf_far '{PLUGINDIR}' "$plugindir" )
for b in $binaries; do
zaf_fetch_url "$url/$b" >"$pdir/$b"
chmod +x "$pdir/$b"
zaf_fetch_url "$url/$b" >"${ZAF_TMP_DIR}/$b"
zaf_install_bin "${ZAF_TMP_DIR}/$b" "$pdir"
done
script=$(zaf_ctrl_get_global_block <$1 | zaf_block_get_moption "Install-script" | zaf_far '{PLUGINDIR}' "$plugindir" )
[ -n "$script" ] && eval $script
[ -n "$script" ] && eval "$script"
cmd=$(zaf_ctrl_get_global_block <$1 | zaf_block_get_option "Install-cmd" | zaf_far '{PLUGINDIR}' "$plugindir" )
[ -n "$cmd" ] && $cmd
}
@@ -99,14 +111,24 @@ zaf_ctrl_generate_cfg() {
items=$(zaf_ctrl_get_items <"$1")
for i in $items; do
block=$(zaf_ctrl_get_item_block <$1 $i)
ilock=$(echo $i | tr -d '[]*&;:')
cmd=$(zaf_block_get_option <$1 "Cmd")
[ -n "$cmd" ] && { echo "UserParameter=$2.${i},$cmd"; continue; }
cmd=$(zaf_block_get_option <$1 "Function")
[ -n "$cmd" ] && { echo "UserParameter=$2.${i},${ZAF_LIB_DIR}/preload.sh $cmd"; continue; }
cmd=$(zaf_block_get_moption <$1 "Script")
[ -n "$cmd" ] && { zaf_block_get_moption <$1 "Script" >${ZAF_PLUGIN_DIR}/$2/$ilock.sh; echo "UserParameter=$2.${i},${ZAF_PLUGIN_DIR}/$ilock.sh"; continue; }
ilock=$(echo $i | tr -d '[]*&;:')
cmd=$(zaf_ctrl_get_item_option $1 $i "Cmd")
if [ -n "$cmd" ]; then
echo "UserParameter=$2.${i},$cmd";
continue
fi
cmd=$(zaf_ctrl_get_item_option $1 $i "Function")
if [ -n "$cmd" ]; then
echo "UserParameter=$2.${i},${ZAF_LIB_DIR}/preload.sh $cmd";
continue;
fi
cmd=$(zaf_ctrl_get_item_option $1 $i "Script")
if [ -n "$cmd" ]; then
zaf_ctrl_get_item_option $1 $i "Script" >${ZAF_TMP_DIR}/${ilock}.sh;
zaf_install_bin ${ZAF_TMP_DIR}/${ilock}.sh ${ZAF_PLUGINS_DIR}/$2/
echo "UserParameter=$2.${i},${ZAF_PLUGINS_DIR}/$2/${ilock}.sh";
continue;
fi
done
}

View File

@@ -1,41 +1,102 @@
# Os related functions
zaf_configure_os_openwrt() {
ZAF_AGENT_RESTART="/etc/init.d/zabbix_agentd restart"
ZAF_AGENT_CONFIGD="/var/run/zabbix_agentd.conf.d/"
ZAF_AGENT_CONFIG="/etc/zabbix_agentd.conf"
ZAF_AGENT_PKG="zabbix-agentd"
ZAF_CURL_INSECURE=1
}
zaf_detect_system() {
if which dpkg >/dev/null; then
ZAF_PKG="dpkg"
ZAF_OS=$(lsb_release -is)
ZAF_OS_CODENAME=$(lsb_release -cs)
ZAF_PKG=dpkg
ZAF_OS=$(lsb_release -is|tr '[:upper:]' '[:lower:]')
ZAF_OS_CODENAME=$(lsb_release -cs|tr '[:upper:]' '[:lower:]')
ZAF_CURL_INSECURE=0
ZAF_AGENT_PKG="zabbix-agent"
return
else if which rpm >/dev/null; then
ZAF_PKG="rpm"
ZAF_OS=$(lsb_release -is)
ZAF_OS_CODENAME=$(lsb_release -cs)
ZAF_OS=$(lsb_release -is|tr '[:upper:]' '[:lower:]')
ZAF_OS_CODENAME=$(lsb_release -cs|tr '[:upper:]' '[:lower:]')
ZAF_CURL_INSECURE=0
ZAF_AGENT_PKG="zabbix-agent"
return
else if which opkg >/dev/null; then
ZAF_PKG="opkg"
. /etc/openwrt_release
ZAF_OS="$DISTRIB_ID"
ZAF_OS_CODENAME="$DISTRIB_CODENAME"
ZAF_AGENT_RESTART="/etc/init.d/zabbix_agentd restart"
ZAF_AGENT_CONFIGD="/var/run/zabbix_agentd.conf.d/"
ZAF_AGENT_CONFIG="/etc/zabbix_agentd.conf"
ZAF_AGENT_PKG="zabbix-agentd"
ZAF_CURL_INSECURE=1
ZAF_OS="$(echo $DISTRIB_ID|tr '[:upper:]' '[:lower:]')"
ZAF_OS_CODENAME="$(echo $DISTRIB_CODENAME|tr '[:upper:]' '[:lower:]')"
return
else
ZAF_PKG="unknown"
ZAF_OS="unknown"
ZAF_OS_CODENAME="unknown"
ZAF_AGENT_PKG=""
return
fi
fi
fi
}
# Run OS specific command
# $1 - name of the function.
# all variants will be tested. (name_os_codename, name_os, name_{dpkg|opkg|rpm}, name )
zaf_os_specific(){
local func="$1"
if type "${func}_${ZAF_OS}_${ZAF_OS_CODENAME}" >/dev/null 2>/dev/null; then
eval "${func}_${ZAF_OS}_${ZAF_OS_CODENAME} $2 $3 $4 $5 $6"
else if type "${func}_${ZAF_OS}" >/dev/null 2>/dev/null; then
eval "${func}_${ZAF_OS} $2 $3 $4 $5 $6"
else if type "${func}_${ZAF_PKG}" >/dev/null 2>/dev/null; then
eval "${func}_${ZAF_PKG} $2 $3 $4 $5 $6"
else
zaf_dbg "No OS/packager specific implementation for $1"
fi
fi
fi
}
zaf_is_root(){
[ "$USER" = "root" ]
}
# Install file, bin or directory and respect install prefix
# $1 - src file
# $2 - directory
zaf_install(){
zaf_dbg "Install file $1 to $INSTALL_PREFIX/$2/$(basename $1)"
cp "$1" "$INSTALL_PREFIX/$2/$(basename $1)"
}
# $1 - src file
# $2 - directory
zaf_install_bin(){
zaf_dbg "Install binary $1 to $INSTALL_PREFIX/$2/$(basename $1)"
cp "$1" "$INSTALL_PREFIX/$2/$(basename $1)"
chmod +x "$INSTALL_PREFIX/$2/$(basename $1)"
}
# $1 - directory
zaf_install_dir(){
zaf_dbg "Install directory $1 to $INSTALL_PREFIX/$1"
mkdir -p "$INSTALL_PREFIX/$1"
}
# $1 - file
zaf_touch(){
zaf_dbg "Touch $INSTALL_PREFIX/$1"
touch "$INSTALL_PREFIX/$1"
}
# Automaticaly install agent on debian
# For another os, create similar function (install_zabbix_centos)
zaf_install_agent_debian() {
zaf_fetch_url "http://repo.zabbix.com/zabbix/3.0/debian/pool/main/z/zabbix-release/zabbix-release_3.0-1+${ZAF_CODENAME}_all.deb" >"/tmp/zaf-installer/zabbix-release_3.0-1+${ZAF_CODENAME}_all.deb" \
&& dpkg -i "/tmp/zaf-installer/zabbix-release_3.0-1+${ZAF_CODENAME}_all.deb" \
&& apt-get update \
&& apt-get install $ZAF_AGENT_PKG
}
# Check if dpkg dependency is met
# $* - packages
zaf_check_deps_dpkg() {
@@ -57,18 +118,4 @@ zaf_check_deps_opkg() {
done
}
# Check dependency based on system
zaf_check_deps() {
case $ZAF_PKG in
dpkg) zaf_check_deps_dpkg $*
;;
opkg) zaf_check_deps_opkg $*
;;
rpm) zaf_check_deps_rpm $*
;;
*) return
;;
esac
}

View File

@@ -2,7 +2,18 @@
############################################ Common routines
zaf_msg() {
[ "$ZAF_DEBUG" = "1" ] && echo $@
echo $@
}
zaf_dbg() {
[ "$ZAF_DEBUG" -ge "3" ] && logger -s -t zaf $@
}
zaf_wrn() {
[ "$ZAF_DEBUG" -ge "2" ] && logger -s -t zaf $@
}
zaf_err() {
logger -s -t zaf $@
logger -s -t zaf "Exiting with error!"
exit 1
}
# Fetch url to stdout
@@ -87,13 +98,13 @@ zaf_version() {
# Restart zabbix agent
zaf_restart_agent() {
${ZAF_AGENT_RESTART}
${ZAF_AGENT_RESTART} || zaf_err "Cannot restart Zabbix agent (${ZAF_AGENT_RESTART})!"
}
# Check if zaf.version item is populated
zaf_check_agent_config() {
zaf_restart_agent
zabbix_agentd -t zaf.version
${ZAF_AGENT_BIN} -t zaf.version
}
# Update repo
@@ -103,7 +114,6 @@ zaf_update_repo() {
[ -n "${ZAF_PLUGINS_REPO}" ] && cd ${ZAF_REPO_DIR} && git pull
}
# Construct url from plugin name
# It can be http[s]://url
# /path (from file)
@@ -153,26 +163,25 @@ zaf_plugin_info() {
# Prepare plugin into tmp dir
# $1 is url, directory or plugin name (will be searched in default plugin dir).
# $2 is directory to prepare.
# $3 plugin name
zaf_prepare_plugin() {
url=$(zaf_get_plugin_url "$1")
url=$(zaf_get_plugin_url "$1")/control.zaf
plugindir="$2"
control=${plugindir}/control.zaf
echo "Fetching control file from $url ..."
if zaf_plugin_fetch_control "$url" "${control}"; then
zaf_dbg "Fetching control file from $url ..."
if zaf_fetch_url "$url" >"${control}"; then
zaf_plugin_info "${control}"
zaf_ctrl_check_deps "${control}"
else
echo "Cannot fetch control file!"
return 1
zaf_err "Cannot fetch or write control file!"
fi
}
zaf_install_plugin() {
mkdir "${ZAF_TMP_DIR}/plugin"
if zaf_prepare_plugin "$1" "${ZAF_TMP_DIR}/plugin"; then
plugin=$(zaf_ctrl_get_global_block <"${ZAF_TMP_DIR}/plugin/control.zaf" | zaf_block_get_option Plugin)
plugindir="${ZAF_PLUGINS_DIR}"/$plugin
echo $plugindir;exit;
if zaf_prepare_plugin "$1" $plugindir; then
zaf_ctrl_check_deps "${control}"
zaf_ctrl_install "${control}" "${plugin}"
@@ -184,10 +193,10 @@ zaf_install_plugin() {
zaf_far '{ZAFLOCK}' "${ZAF_LIB_DIR}/zaflock '$plugin' " \
>$plugindir/zabbix.conf
else
echo "Bad control file $control ($url)!"
cat $control
exit 4
zaf_err "Cannot install plugin $plugin to $plugindir!"
fi
else
return 1
fi
}