diff --git a/install.sh b/install.sh index 2065d03..a9a0d97 100755 --- a/install.sh +++ b/install.sh @@ -5,46 +5,61 @@ if ! [ "$(basename $0)" = "install.sh" ]; then url="https://raw.githubusercontent.com/limosek/zaf/master/" if ! which curl >/dev/null; then - echo "Curl not found. Cannot continue. Please install it." - exit 2 + zaf_err "Curl not found. Cannot continue. Please install it." fi - echo "Installing from url $url..." >&2 - [ -z "$*" ] && silent=silent + echo "Installing from url $url..." + [ -z "$*" ] && auto=auto set -e mkdir -p /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 \ - && exec ./install.sh $silent "$@" + && exec ./install.sh $auto "$@" exit fi -ZAF_CFG_FILE=/etc/zaf.conf -. $(dirname $0)/lib/zaf.lib.sh -. $(dirname $0)/lib/os.lib.sh -. $(dirname $0)/lib/ctrl.lib.sh +# 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 -zaf_msg() { - [ "$ZAF_DEBUG" = "1" ] && echo $@ -} +[ -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/os.lib.sh + . $(dirname $0)/lib/ctrl.lib.sh +fi # 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. # $1 - option name # $2 - option description # $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(){ 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 if [ -n "$opt" ] && ! [ "$4" = "user" ]; then eval "$1='$opt'" - zaf_msg "Got $2 <$1> from ENV: $opt" >&2 + zaf_dbg "Got '$2' <$1> from ENV: $opt" return else opt="$3" fi - if ! [ "$4" = "silent" ]; then + if ! [ "$4" = "auto" ]; then echo -n "$2 <$1> [$opt]: " read opt else @@ -52,9 +67,9 @@ zaf_get_option(){ fi if [ -z "$opt" ]; then opt="$3" - zaf_msg "Got $2 <$1> from Defaults: $opt" >&2 + zaf_dbg "Got '$2' <$1> from Defaults: $opt" >&2 else - zaf_msg "Got $2 <$1> from USER: $opt" + zaf_dbg "Got '$2' <$1> from USER: $opt" fi eval "$1='$opt'" } @@ -66,9 +81,9 @@ zaf_set_option(){ local description if ! grep -q "^$1=" ${ZAF_CFG_FILE}; then 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 - zaf_msg "Preserving $1 to $2 in ${ZAF_CFG_FILE}" >&2 + zaf_wrn "Preserving $1 to $2 in ${ZAF_CFG_FILE}" >&2 fi } @@ -81,27 +96,6 @@ zaf_getrest(){ 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 # $1 option # $2 value @@ -109,51 +103,51 @@ zaf_agent_set_option() { local option="$1" local value="$2" 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 fi echo "$option=$value" >> "$ZAF_AGENT_CONFIGD/zaf_options.conf" } # Automaticaly configure agent if supported -# Parameters are in format zabbixconfvar=value +# Parameters are in format Z_zabbixconfvar=value zaf_configure_agent() { local pair local option 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 - 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) value=$(echo $pair|cut -d '=' -f 2-) zaf_agent_set_option "$option" "$value" done } -zaf_no_perms(){ - echo "No permissions! to $1! Please become root or give perms. Exiting." - exit 2 -} - zaf_configure(){ - [ "$1" = "interactive" ] && ZAF_DEBUG=1 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_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_AGENT_PKG "Zabbix agent package" "$ZAF_AGENT_PKG" "$1" - if [ -n "$ZAF_AGENT_PKG" ]; then - if ! zaf_check_deps "$ZAF_AGENT_PKG"; then - if [ "$1" = "silent" ]; then - zaf_install_agent + if zaf_is_root && [ -n "$ZAF_AGENT_PKG" ]; then + if ! zaf_os_specific zaf_check_deps "$ZAF_AGENT_PKG"; then + if [ "$1" = "auto" ]; then + zaf_os_specific zaf_install_agent fi fi fi 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_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_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" @@ -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_RESTART "Zabbix agent restart cmd" "service zabbix-agent restart" "$1" - if ! which $ZAF_AGENT_BIN >/dev/null; then - echo "Zabbix agent not installed? Use ZAF_ZABBIX_AGENT_BIN env variable to specify location. Exiting." - exit 3 + if zaf_is_root && ! which $ZAF_AGENT_BIN >/dev/null; then + zaf_err "Zabbix agent not installed? Use ZAF_ZABBIX_AGENT_BIN env variable to specify location. Exiting." fi if which git >/dev/null; then ZAF_GIT=1 @@ -173,8 +166,9 @@ zaf_configure(){ ZAF_GIT="" fi + [ -n "$INSTALL_PREFIX" ] && zaf_install_dir "/etc" 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 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_TMP_BASE "$ZAF_TMP_BASE" 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_REPO "$ZAF_PLUGINS_REPO" zaf_set_option ZAF_REPO_DIR "$ZAF_REPO_DIR" @@ -198,44 +193,77 @@ zaf_configure(){ if [ -f "${ZAF_CFG_FILE}" ]; then . "${ZAF_CFG_FILE}" fi -ZAF_TMP_DIR="${ZAF_TMP_BASE}-${USER}-$$" +ZAF_TMP_DIR="${ZAF_TMP_BASE-/tmp/zaf}-${USER}-$$" case $1 in interactive) + shift zaf_configure interactive - $0 install + $0 install "$@" ;; -silent) - zaf_configure silent - zaf_configure_agent "$@" +auto) + 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 "$@" rm -rif ${ZAF_TMP_DIR} mkdir -p ${ZAF_TMP_DIR} - mkdir -p ${ZAF_LIB_DIR} - mkdir -p ${ZAF_PLUGINS_DIR} - zaf_install $(zaf_getrest lib/zaf.lib.sh) ${ZAF_LIB_DIR}/zaf.lib.sh - zaf_install $(zaf_getrest lib/jshn.sh) ${ZAF_LIB_DIR}/jshn.sh - zaf_install_exe $(zaf_getrest lib/zaflock) ${ZAF_LIB_DIR}/zaflock - mkdir -p ${ZAF_TMP_DIR}/p/zaf - mkdir -p ${ZAF_PLUGINS_DIR} - zaf_install_exe $(zaf_getrest zaf) /usr/bin/zaf - /usr/bin/zaf install zaf - if ! zaf_check_agent_config; then + zaf_install_dir ${ZAF_LIB_DIR} + zaf_install_dir ${ZAF_PLUGINS_DIR} + zaf_install $(zaf_getrest lib/zaf.lib.sh) ${ZAF_LIB_DIR} + zaf_install $(zaf_getrest lib/os.lib.sh) ${ZAF_LIB_DIR} + zaf_install $(zaf_getrest lib/ctrl.lib.sh) ${ZAF_LIB_DIR} + zaf_install $(zaf_getrest lib/jshn.sh) ${ZAF_LIB_DIR} + zaf_install_bin $(zaf_getrest lib/zaflock) ${ZAF_LIB_DIR} + zaf_install_dir ${ZAF_TMP_DIR}/p/zaf + zaf_install_dir ${ZAF_PLUGINS_DIR} + 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 "Ensure that zabbix_agentd reads ${ZAF_AGENT_CONFIG}" echo "and there is Include=${ZAF_AGENT_CONFIGD} directive inside." echo "Does ${ZAF_AGENT_RESTART} work?" exit 1 - fi + fi + fi rm -rif ${ZAF_TMP_DIR} echo "Install OK. Use 'zaf' without parameters to continue." ;; *) echo echo "Please specify how to install." - echo "ZAF_CONFIG_OPTION=value [...] install.sh {silent|interactive} Z_option=value [...]" - echo "Example 1 (default install): install.sh silent" - echo 'Example 2 (preconfigure agent options): install.sh silent Z_Server=zabbix.server Z_ServerActive=zabbix.server Z_Hostname=$(hostname)' - echo "Example 3 (preconfigure zaf packaging system to use): ZAF_PKG=dpkg install.sh silent" + echo "install.sh {auto|interactive|debug-auto|debug-interactive|reconf} [Agent-Options] [Zaf-Options]" + echo "scratch means that config file will be created from scratch" + echo " Agent-Options: A_Option=value [...]" + 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 exit 1 diff --git a/lib/ctrl.lib.sh b/lib/ctrl.lib.sh index 99b3be7..49a9834 100644 --- a/lib/ctrl.lib.sh +++ b/lib/ctrl.lib.sh @@ -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 } diff --git a/lib/os.lib.sh b/lib/os.lib.sh index 7aec92c..ba62be1 100644 --- a/lib/os.lib.sh +++ b/lib/os.lib.sh @@ -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 -} - diff --git a/lib/zaf.lib.sh b/lib/zaf.lib.sh index fbc1d26..b2fa1a6 100644 --- a/lib/zaf.lib.sh +++ b/lib/zaf.lib.sh @@ -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 } diff --git a/zaf b/zaf index 87becfb..ed637e5 100755 --- a/zaf +++ b/zaf @@ -1,25 +1,24 @@ #!/bin/sh -if [ -f /etc/zaf.conf ]; then - . /etc/zaf.conf +ZAF_CFG_FILE="/etc/zaf.conf" +if [ -f $ZAF_CFG_FILE ]; then + . $ZAF_CFG_FILE else - echo "Missing config file /etc/zaf.conf! Exiting." + echo "Missing config file $ZAF_CFG_FILE! Exiting." exit 2 fi +[ -d $(dirname $0)/.git ] && ZAF_LIB_DIR=$(dirname $0)/lib [ -z "$ZAF_TMP_BASE" ] && ZAF_TMP_BASE=/tmp/zaf ZAF_TMP_DIR="${ZAF_TMP_BASE}-${USER}-$$" trap "rm -rif ${ZAF_TMP_DIR}" EXIT ! [ -d "${ZAF_TMP_DIR}" ] && mkdir "${ZAF_TMP_DIR}" - -# Devel version -if [ -f $(dirname $0)/lib/zaf.lib.sh ]; then - ZAF_LIB_DIR=$(realpath $(dirname $0)/lib) -fi +[ -z "$ZAF_DEBUG" ] && ZAF_DEBUG=0 . ${ZAF_LIB_DIR}/zaf.lib.sh . ${ZAF_LIB_DIR}/os.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