1
0
mirror of https://github.com/limosek/zaf.git synced 2024-11-22 02:19:06 +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

@ -5,46 +5,61 @@ if ! [ "$(basename $0)" = "install.sh" ]; then
url="https://raw.githubusercontent.com/limosek/zaf/master/" url="https://raw.githubusercontent.com/limosek/zaf/master/"
if ! which curl >/dev/null; if ! which curl >/dev/null;
then then
echo "Curl not found. Cannot continue. Please install it." zaf_err "Curl not found. Cannot continue. Please install it."
exit 2
fi fi
echo "Installing from url $url..." >&2 echo "Installing from url $url..."
[ -z "$*" ] && silent=silent [ -z "$*" ] && auto=auto
set -e set -e
mkdir -p /tmp/zaf-installer \ mkdir -p /tmp/zaf-installer \
&& cd /tmp/zaf-installer \ && cd /tmp/zaf-installer \
&& (for i in lib/zaf.lib.sh lib/os.lib.sh lib/ctrl.lib.sh install.sh ; do curl -f -k -s -L -o - "$url/$i") >install.sh \ && (for i in lib/zaf.lib.sh lib/os.lib.sh lib/ctrl.lib.sh install.sh ; do curl -f -k -s -L -o - "$url/$i"; done) >install.sh \
&& chmod +x install.sh \ && chmod +x install.sh \
&& exec ./install.sh $silent "$@" && exec ./install.sh $auto "$@"
exit exit
fi fi
ZAF_CFG_FILE=/etc/zaf.conf # Read options as config for ZAF
for pair in "$@"; do
echo $pair | grep -q '^ZAF\_' || continue
option=$(echo $pair|cut -d '=' -f 1)
value=$(echo $pair|cut -d '=' -f 2-)
eval "C_${option}='$value'"
done
[ -z "$ZAF_CFG_FILE" ] && ZAF_CFG_FILE=$INSTALL_PREFIX/etc/zaf.conf
[ -n "$C_ZAF_DEBUG" ] && ZAF_DEBUG=$C_ZAF_DEBUG
[ -z "$ZAF_DEBUG" ] && ZAF_DEBUG=0
if [ -f $(dirname $0)/lib/zaf.lib.sh ]; then
. $(dirname $0)/lib/zaf.lib.sh . $(dirname $0)/lib/zaf.lib.sh
. $(dirname $0)/lib/os.lib.sh . $(dirname $0)/lib/os.lib.sh
. $(dirname $0)/lib/ctrl.lib.sh . $(dirname $0)/lib/ctrl.lib.sh
fi
zaf_msg() {
[ "$ZAF_DEBUG" = "1" ] && echo $@
}
# Read option. If it is already set in zaf.conf, it is skipped. If env variable is set, it is used instead of default # Read option. If it is already set in zaf.conf, it is skipped. If env variable is set, it is used instead of default
# It sets global variable name on result. # It sets global variable name on result.
# $1 - option name # $1 - option name
# $2 - option description # $2 - option description
# $3 - default # $3 - default
# $4 - if $4="silent" , use autoconf. if $4="user", force asking. # $4 - if $4="auto" , use autoconf. if $4="user", force asking.
zaf_get_option(){ zaf_get_option(){
local opt local opt
eval opt=\$C_$1
if [ -n "$opt" ]; then
eval "$1='$opt'"
zaf_dbg "Got '$2' <$1> from CLI: $opt"
return
fi
eval opt=\$$1 eval opt=\$$1
if [ -n "$opt" ] && ! [ "$4" = "user" ]; then if [ -n "$opt" ] && ! [ "$4" = "user" ]; then
eval "$1='$opt'" eval "$1='$opt'"
zaf_msg "Got $2 <$1> from ENV: $opt" >&2 zaf_dbg "Got '$2' <$1> from ENV: $opt"
return return
else else
opt="$3" opt="$3"
fi fi
if ! [ "$4" = "silent" ]; then if ! [ "$4" = "auto" ]; then
echo -n "$2 <$1> [$opt]: " echo -n "$2 <$1> [$opt]: "
read opt read opt
else else
@ -52,9 +67,9 @@ zaf_get_option(){
fi fi
if [ -z "$opt" ]; then if [ -z "$opt" ]; then
opt="$3" opt="$3"
zaf_msg "Got $2 <$1> from Defaults: $opt" >&2 zaf_dbg "Got '$2' <$1> from Defaults: $opt" >&2
else else
zaf_msg "Got $2 <$1> from USER: $opt" zaf_dbg "Got '$2' <$1> from USER: $opt"
fi fi
eval "$1='$opt'" eval "$1='$opt'"
} }
@ -66,9 +81,9 @@ zaf_set_option(){
local description local description
if ! grep -q "^$1=" ${ZAF_CFG_FILE}; then if ! grep -q "^$1=" ${ZAF_CFG_FILE}; then
echo "$1='$2'" >>${ZAF_CFG_FILE} echo "$1='$2'" >>${ZAF_CFG_FILE}
zaf_msg "Saving $1 to $2 in ${ZAF_CFG_FILE}" >&2 zaf_dbg "Saving $1 to $2 in ${ZAF_CFG_FILE}" >&2
else else
zaf_msg "Preserving $1 to $2 in ${ZAF_CFG_FILE}" >&2 zaf_wrn "Preserving $1 to $2 in ${ZAF_CFG_FILE}" >&2
fi fi
} }
@ -81,27 +96,6 @@ zaf_getrest(){
fi fi
} }
zaf_install(){
cp "$1" "$2"
}
zaf_install_exe(){
cp "$1" "$2"
chmod +x "$2"
}
# Automaticaly install agent if supported
zaf_install_agent() {
case $ZAF_OS in
Debian)
curl "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
;;
esac
}
# Set config option in zabbix agent # Set config option in zabbix agent
# $1 option # $1 option
# $2 value # $2 value
@ -109,51 +103,51 @@ zaf_agent_set_option() {
local option="$1" local option="$1"
local value="$2" local value="$2"
if grep ^$option\= $ZAF_AGENT_CONFIG; then if grep ^$option\= $ZAF_AGENT_CONFIG; then
echo "Moving option $option to zaf config part." zaf_wrn "Moving option $option to zaf config part."
sed -i "s/$option=/#$option=/" $ZAF_AGENT_CONFIG sed -i "s/$option=/#$option=/" $ZAF_AGENT_CONFIG
fi fi
echo "$option=$value" >> "$ZAF_AGENT_CONFIGD/zaf_options.conf" echo "$option=$value" >> "$ZAF_AGENT_CONFIGD/zaf_options.conf"
} }
# Automaticaly configure agent if supported # Automaticaly configure agent if supported
# Parameters are in format zabbixconfvar=value # Parameters are in format Z_zabbixconfvar=value
zaf_configure_agent() { zaf_configure_agent() {
local pair local pair
local option local option
local value local value
touch "$ZAF_AGENT_CONFIGD/zaf_options.conf" zaf_install_dir "$ZAF_AGENT_CONFIGD"
zaf_touch "$ZAF_AGENT_CONFIGD/zaf_options.conf" || zaf_err "Cannot access $ZAF_AGENT_CONFIGD/zaf_options.conf"
for pair in "$@"; do for pair in "$@"; do
echo $pair | grep -q '^Z\_' || continue echo $pair | grep -q '^Z\_' || continue # Skip non Z_ vars
option=$(echo $pair|cut -d '=' -f 1|cut -d '_' -f 2) option=$(echo $pair|cut -d '=' -f 1|cut -d '_' -f 2)
value=$(echo $pair|cut -d '=' -f 2-) value=$(echo $pair|cut -d '=' -f 2-)
zaf_agent_set_option "$option" "$value" zaf_agent_set_option "$option" "$value"
done done
} }
zaf_no_perms(){
echo "No permissions! to $1! Please become root or give perms. Exiting."
exit 2
}
zaf_configure(){ zaf_configure(){
[ "$1" = "interactive" ] && ZAF_DEBUG=1
zaf_detect_system zaf_detect_system
zaf_os_specific zaf_configure_os
if ! zaf_is_root; then
[ -z "$INSTALL_PREFIX" ] && zaf_err "We are not root. Use INSTALL_PREFIX or become root."
fi
zaf_get_option ZAF_PKG "Packaging system to use" "$ZAF_PKG" "$1" zaf_get_option ZAF_PKG "Packaging system to use" "$ZAF_PKG" "$1"
zaf_get_option ZAF_OS "Operating system to use" "$ZAF_OS" "$1" zaf_get_option ZAF_OS "Operating system to use" "$ZAF_OS" "$1"
zaf_get_option ZAF_OS_CODENAME "Operating system codename" "$ZAF_OS_CODENAME" "$1" zaf_get_option ZAF_OS_CODENAME "Operating system codename" "$ZAF_OS_CODENAME" "$1"
zaf_get_option ZAF_AGENT_PKG "Zabbix agent package" "$ZAF_AGENT_PKG" "$1" zaf_get_option ZAF_AGENT_PKG "Zabbix agent package" "$ZAF_AGENT_PKG" "$1"
if [ -n "$ZAF_AGENT_PKG" ]; then if zaf_is_root && [ -n "$ZAF_AGENT_PKG" ]; then
if ! zaf_check_deps "$ZAF_AGENT_PKG"; then if ! zaf_os_specific zaf_check_deps "$ZAF_AGENT_PKG"; then
if [ "$1" = "silent" ]; then if [ "$1" = "auto" ]; then
zaf_install_agent zaf_os_specific zaf_install_agent
fi fi
fi fi
fi fi
zaf_get_option ZAF_CURL_INSECURE "Insecure curl (accept all certificates)" "1" "$1" zaf_get_option ZAF_CURL_INSECURE "Insecure curl (accept all certificates)" "1" "$1"
zaf_get_option ZAF_TMP_BASE "Tmp directory prefix (\$USER will be added)" "/tmp/zaf" "$1" zaf_get_option ZAF_TMP_BASE "Tmp directory prefix (\$USER will be added)" "/tmp/zaf" "$1"
zaf_get_option ZAF_LIB_DIR "Libraries directory" "/usr/lib/zaf" "$1" zaf_get_option ZAF_LIB_DIR "Libraries directory" "/usr/lib/zaf" "$1"
zaf_get_option ZAF_BIN_DIR "Directory to put binaries" "/usr/bin" "$1"
zaf_get_option ZAF_PLUGINS_DIR "Plugins directory" "${ZAF_LIB_DIR}/plugins" "$1" zaf_get_option ZAF_PLUGINS_DIR "Plugins directory" "${ZAF_LIB_DIR}/plugins" "$1"
zaf_get_option ZAF_PLUGINS_REPO "Plugins reposiory" "https://raw.githubusercontent.com/limosek/zaf-plugins/master/" "$1" zaf_get_option ZAF_PLUGINS_REPO "Plugins reposiory" "https://raw.githubusercontent.com/limosek/zaf-plugins/master/" "$1"
zaf_get_option ZAF_REPO_DIR "Plugins directory" "${ZAF_LIB_DIR}/repo" "$1" zaf_get_option ZAF_REPO_DIR "Plugins directory" "${ZAF_LIB_DIR}/repo" "$1"
@ -163,9 +157,8 @@ zaf_configure(){
zaf_get_option ZAF_AGENT_BIN "Zabbix agent binary" "/usr/sbin/zabbix_agentd" "$1" zaf_get_option ZAF_AGENT_BIN "Zabbix agent binary" "/usr/sbin/zabbix_agentd" "$1"
zaf_get_option ZAF_AGENT_RESTART "Zabbix agent restart cmd" "service zabbix-agent restart" "$1" zaf_get_option ZAF_AGENT_RESTART "Zabbix agent restart cmd" "service zabbix-agent restart" "$1"
if ! which $ZAF_AGENT_BIN >/dev/null; then if zaf_is_root && ! which $ZAF_AGENT_BIN >/dev/null; then
echo "Zabbix agent not installed? Use ZAF_ZABBIX_AGENT_BIN env variable to specify location. Exiting." zaf_err "Zabbix agent not installed? Use ZAF_ZABBIX_AGENT_BIN env variable to specify location. Exiting."
exit 3
fi fi
if which git >/dev/null; then if which git >/dev/null; then
ZAF_GIT=1 ZAF_GIT=1
@ -173,8 +166,9 @@ zaf_configure(){
ZAF_GIT="" ZAF_GIT=""
fi fi
[ -n "$INSTALL_PREFIX" ] && zaf_install_dir "/etc"
if ! [ -f "${ZAF_CFG_FILE}" ]; then if ! [ -f "${ZAF_CFG_FILE}" ]; then
touch "${ZAF_CFG_FILE}" || zaf_no_perms "${ZAF_CFG_FILE}" touch "${ZAF_CFG_FILE}" || zaf_err "No permissions to ${ZAF_CFG_FILE}"
fi fi
zaf_set_option ZAF_PKG "${ZAF_PKG}" zaf_set_option ZAF_PKG "${ZAF_PKG}"
@ -185,6 +179,7 @@ zaf_configure(){
zaf_set_option ZAF_CURL_INSECURE "${ZAF_CURL_INSECURE}" zaf_set_option ZAF_CURL_INSECURE "${ZAF_CURL_INSECURE}"
zaf_set_option ZAF_TMP_BASE "$ZAF_TMP_BASE" zaf_set_option ZAF_TMP_BASE "$ZAF_TMP_BASE"
zaf_set_option ZAF_LIB_DIR "$ZAF_LIB_DIR" zaf_set_option ZAF_LIB_DIR "$ZAF_LIB_DIR"
zaf_set_option ZAF_BIN_DIR "$ZAF_BIN_DIR"
zaf_set_option ZAF_PLUGINS_DIR "$ZAF_PLUGINS_DIR" zaf_set_option ZAF_PLUGINS_DIR "$ZAF_PLUGINS_DIR"
zaf_set_option ZAF_PLUGINS_REPO "$ZAF_PLUGINS_REPO" zaf_set_option ZAF_PLUGINS_REPO "$ZAF_PLUGINS_REPO"
zaf_set_option ZAF_REPO_DIR "$ZAF_REPO_DIR" zaf_set_option ZAF_REPO_DIR "$ZAF_REPO_DIR"
@ -198,44 +193,77 @@ zaf_configure(){
if [ -f "${ZAF_CFG_FILE}" ]; then if [ -f "${ZAF_CFG_FILE}" ]; then
. "${ZAF_CFG_FILE}" . "${ZAF_CFG_FILE}"
fi fi
ZAF_TMP_DIR="${ZAF_TMP_BASE}-${USER}-$$" ZAF_TMP_DIR="${ZAF_TMP_BASE-/tmp/zaf}-${USER}-$$"
case $1 in case $1 in
interactive) interactive)
shift
zaf_configure interactive zaf_configure interactive
$0 install $0 install "$@"
;; ;;
silent) auto)
zaf_configure silent shift
zaf_configure auto
$0 install "$@"
;;
debug-auto)
shift;
ZAF_DEBUG=3 $0 auto "$@"
;;
debug-interactive)
shift;
ZAF_DEBUG=3 $0 interactive "$@"
;;
debug)
shift;
ZAF_DEBUG=3 $0 install "$@"
;;
reconf)
shift;
rm -f $ZAF_CFG_FILE
$0 install "$@"
;;
install)
zaf_configure auto
zaf_configure_agent "$@" zaf_configure_agent "$@"
rm -rif ${ZAF_TMP_DIR} rm -rif ${ZAF_TMP_DIR}
mkdir -p ${ZAF_TMP_DIR} mkdir -p ${ZAF_TMP_DIR}
mkdir -p ${ZAF_LIB_DIR} zaf_install_dir ${ZAF_LIB_DIR}
mkdir -p ${ZAF_PLUGINS_DIR} zaf_install_dir ${ZAF_PLUGINS_DIR}
zaf_install $(zaf_getrest lib/zaf.lib.sh) ${ZAF_LIB_DIR}/zaf.lib.sh zaf_install $(zaf_getrest lib/zaf.lib.sh) ${ZAF_LIB_DIR}
zaf_install $(zaf_getrest lib/jshn.sh) ${ZAF_LIB_DIR}/jshn.sh zaf_install $(zaf_getrest lib/os.lib.sh) ${ZAF_LIB_DIR}
zaf_install_exe $(zaf_getrest lib/zaflock) ${ZAF_LIB_DIR}/zaflock zaf_install $(zaf_getrest lib/ctrl.lib.sh) ${ZAF_LIB_DIR}
mkdir -p ${ZAF_TMP_DIR}/p/zaf zaf_install $(zaf_getrest lib/jshn.sh) ${ZAF_LIB_DIR}
mkdir -p ${ZAF_PLUGINS_DIR} zaf_install_bin $(zaf_getrest lib/zaflock) ${ZAF_LIB_DIR}
zaf_install_exe $(zaf_getrest zaf) /usr/bin/zaf zaf_install_dir ${ZAF_TMP_DIR}/p/zaf
/usr/bin/zaf install zaf zaf_install_dir ${ZAF_PLUGINS_DIR}
if ! zaf_check_agent_config; then zaf_install_dir ${ZAF_BIN_DIR}
zaf_install_bin $(zaf_getrest zaf) ${ZAF_BIN_DIR}
export INSTALL_PREFIX ZAF_CFG_FILE
if zaf_is_root; then
${INSTALL_PREFIX}/${ZAF_BIN_DIR}/zaf install zaf || zaf_err "Error installing zaf plugin."
if zaf_is_root && ! zaf_check_agent_config; then
echo "Something is wrong with zabbix agent config." echo "Something is wrong with zabbix agent config."
echo "Ensure that zabbix_agentd reads ${ZAF_AGENT_CONFIG}" echo "Ensure that zabbix_agentd reads ${ZAF_AGENT_CONFIG}"
echo "and there is Include=${ZAF_AGENT_CONFIGD} directive inside." echo "and there is Include=${ZAF_AGENT_CONFIGD} directive inside."
echo "Does ${ZAF_AGENT_RESTART} work?" echo "Does ${ZAF_AGENT_RESTART} work?"
exit 1 exit 1
fi fi
fi
rm -rif ${ZAF_TMP_DIR} rm -rif ${ZAF_TMP_DIR}
echo "Install OK. Use 'zaf' without parameters to continue." echo "Install OK. Use 'zaf' without parameters to continue."
;; ;;
*) *)
echo echo
echo "Please specify how to install." echo "Please specify how to install."
echo "ZAF_CONFIG_OPTION=value [...] install.sh {silent|interactive} Z_option=value [...]" echo "install.sh {auto|interactive|debug-auto|debug-interactive|reconf} [Agent-Options] [Zaf-Options]"
echo "Example 1 (default install): install.sh silent" echo "scratch means that config file will be created from scratch"
echo 'Example 2 (preconfigure agent options): install.sh silent Z_Server=zabbix.server Z_ServerActive=zabbix.server Z_Hostname=$(hostname)' echo " Agent-Options: A_Option=value [...]"
echo "Example 3 (preconfigure zaf packaging system to use): ZAF_PKG=dpkg install.sh silent" echo " Zaf-Options: ZAF_OPT=value [...]"
echo
echo "Example 1 (default install): install.sh auto"
echo 'Example 2 (preconfigure agent options): install.sh auto A_Server=zabbix.server A_ServerActive=zabbix.server A_Hostname=$(hostname)'
echo "Example 3 (preconfigure zaf packaging system to use): install.sh auto ZAF_PKG=opkg"
echo "Example 4 (interactive): install.sh interactive" echo "Example 4 (interactive): install.sh interactive"
echo echo
exit 1 exit 1

View File

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

View File

@ -1,41 +1,102 @@
# Os related functions # 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() { zaf_detect_system() {
if which dpkg >/dev/null; then if which dpkg >/dev/null; then
ZAF_PKG="dpkg" ZAF_PKG=dpkg
ZAF_OS=$(lsb_release -is) ZAF_OS=$(lsb_release -is|tr '[:upper:]' '[:lower:]')
ZAF_OS_CODENAME=$(lsb_release -cs) ZAF_OS_CODENAME=$(lsb_release -cs|tr '[:upper:]' '[:lower:]')
ZAF_CURL_INSECURE=0 ZAF_CURL_INSECURE=0
ZAF_AGENT_PKG="zabbix-agent" ZAF_AGENT_PKG="zabbix-agent"
return return
else if which rpm >/dev/null; then else if which rpm >/dev/null; then
ZAF_PKG="rpm" ZAF_PKG="rpm"
ZAF_OS=$(lsb_release -is) ZAF_OS=$(lsb_release -is|tr '[:upper:]' '[:lower:]')
ZAF_OS_CODENAME=$(lsb_release -cs) ZAF_OS_CODENAME=$(lsb_release -cs|tr '[:upper:]' '[:lower:]')
ZAF_CURL_INSECURE=0 ZAF_CURL_INSECURE=0
ZAF_AGENT_PKG="zabbix-agent" ZAF_AGENT_PKG="zabbix-agent"
return return
else if which opkg >/dev/null; then else if which opkg >/dev/null; then
ZAF_PKG="opkg" ZAF_PKG="opkg"
. /etc/openwrt_release . /etc/openwrt_release
ZAF_OS="$DISTRIB_ID" ZAF_OS="$(echo $DISTRIB_ID|tr '[:upper:]' '[:lower:]')"
ZAF_OS_CODENAME="$DISTRIB_CODENAME" ZAF_OS_CODENAME="$(echo $DISTRIB_CODENAME|tr '[:upper:]' '[:lower:]')"
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
return return
else else
ZAF_PKG="unknown" ZAF_PKG="unknown"
ZAF_OS="unknown" ZAF_OS="unknown"
ZAF_OS_CODENAME="unknown" ZAF_OS_CODENAME="unknown"
ZAF_AGENT_PKG="" ZAF_AGENT_PKG=""
return
fi fi
fi 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 # Check if dpkg dependency is met
# $* - packages # $* - packages
zaf_check_deps_dpkg() { zaf_check_deps_dpkg() {
@ -57,18 +118,4 @@ zaf_check_deps_opkg() {
done 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 ############################################ Common routines
zaf_msg() { 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 # Fetch url to stdout
@ -87,13 +98,13 @@ zaf_version() {
# Restart zabbix agent # Restart zabbix agent
zaf_restart_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 # Check if zaf.version item is populated
zaf_check_agent_config() { zaf_check_agent_config() {
zaf_restart_agent zaf_restart_agent
zabbix_agentd -t zaf.version ${ZAF_AGENT_BIN} -t zaf.version
} }
# Update repo # Update repo
@ -103,7 +114,6 @@ zaf_update_repo() {
[ -n "${ZAF_PLUGINS_REPO}" ] && cd ${ZAF_REPO_DIR} && git pull [ -n "${ZAF_PLUGINS_REPO}" ] && cd ${ZAF_REPO_DIR} && git pull
} }
# Construct url from plugin name # Construct url from plugin name
# It can be http[s]://url # It can be http[s]://url
# /path (from file) # /path (from file)
@ -153,26 +163,25 @@ zaf_plugin_info() {
# Prepare plugin into tmp dir # Prepare plugin into tmp dir
# $1 is url, directory or plugin name (will be searched in default plugin dir). # $1 is url, directory or plugin name (will be searched in default plugin dir).
# $2 is directory to prepare. # $2 is directory to prepare.
# $3 plugin name
zaf_prepare_plugin() { zaf_prepare_plugin() {
url=$(zaf_get_plugin_url "$1") url=$(zaf_get_plugin_url "$1")/control.zaf
plugindir="$2" plugindir="$2"
control=${plugindir}/control.zaf control=${plugindir}/control.zaf
echo "Fetching control file from $url ..." zaf_dbg "Fetching control file from $url ..."
if zaf_plugin_fetch_control "$url" "${control}"; then if zaf_fetch_url "$url" >"${control}"; then
zaf_plugin_info "${control}" zaf_plugin_info "${control}"
zaf_ctrl_check_deps "${control}" zaf_ctrl_check_deps "${control}"
else else
echo "Cannot fetch control file!" zaf_err "Cannot fetch or write control file!"
return 1
fi fi
} }
zaf_install_plugin() { zaf_install_plugin() {
mkdir "${ZAF_TMP_DIR}/plugin" mkdir "${ZAF_TMP_DIR}/plugin"
if zaf_prepare_plugin "$1" "${ZAF_TMP_DIR}/plugin"; then 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 plugindir="${ZAF_PLUGINS_DIR}"/$plugin
echo $plugindir;exit;
if zaf_prepare_plugin "$1" $plugindir; then if zaf_prepare_plugin "$1" $plugindir; then
zaf_ctrl_check_deps "${control}" zaf_ctrl_check_deps "${control}"
zaf_ctrl_install "${control}" "${plugin}" zaf_ctrl_install "${control}" "${plugin}"
@ -184,10 +193,10 @@ zaf_install_plugin() {
zaf_far '{ZAFLOCK}' "${ZAF_LIB_DIR}/zaflock '$plugin' " \ zaf_far '{ZAFLOCK}' "${ZAF_LIB_DIR}/zaflock '$plugin' " \
>$plugindir/zabbix.conf >$plugindir/zabbix.conf
else else
echo "Bad control file $control ($url)!" zaf_err "Cannot install plugin $plugin to $plugindir!"
cat $control
exit 4
fi fi
else
return 1
fi fi
} }

15
zaf
View File

@ -1,25 +1,24 @@
#!/bin/sh #!/bin/sh
if [ -f /etc/zaf.conf ]; then ZAF_CFG_FILE="/etc/zaf.conf"
. /etc/zaf.conf if [ -f $ZAF_CFG_FILE ]; then
. $ZAF_CFG_FILE
else else
echo "Missing config file /etc/zaf.conf! Exiting." echo "Missing config file $ZAF_CFG_FILE! Exiting."
exit 2 exit 2
fi fi
[ -d $(dirname $0)/.git ] && ZAF_LIB_DIR=$(dirname $0)/lib
[ -z "$ZAF_TMP_BASE" ] && ZAF_TMP_BASE=/tmp/zaf [ -z "$ZAF_TMP_BASE" ] && ZAF_TMP_BASE=/tmp/zaf
ZAF_TMP_DIR="${ZAF_TMP_BASE}-${USER}-$$" ZAF_TMP_DIR="${ZAF_TMP_BASE}-${USER}-$$"
trap "rm -rif ${ZAF_TMP_DIR}" EXIT trap "rm -rif ${ZAF_TMP_DIR}" EXIT
! [ -d "${ZAF_TMP_DIR}" ] && mkdir "${ZAF_TMP_DIR}" ! [ -d "${ZAF_TMP_DIR}" ] && mkdir "${ZAF_TMP_DIR}"
[ -z "$ZAF_DEBUG" ] && ZAF_DEBUG=0
# Devel version
if [ -f $(dirname $0)/lib/zaf.lib.sh ]; then
ZAF_LIB_DIR=$(realpath $(dirname $0)/lib)
fi
. ${ZAF_LIB_DIR}/zaf.lib.sh . ${ZAF_LIB_DIR}/zaf.lib.sh
. ${ZAF_LIB_DIR}/os.lib.sh . ${ZAF_LIB_DIR}/os.lib.sh
. ${ZAF_LIB_DIR}/ctrl.lib.sh . ${ZAF_LIB_DIR}/ctrl.lib.sh
[ -f ${ZAF_LIB_DIR}/zaf.${ZAF_OS}.sh ] && . ${ZAF_LIB_DIR}/zaf.${ZAF_OS}.sh
case $1 in case $1 in