1
0
mirror of https://github.com/limosek/zaf.git synced 2024-11-10 20:57:21 +01:00
limosek-zaf/install.sh

286 lines
9.4 KiB
Bash
Raw Normal View History

2016-03-23 14:37:51 +01:00
#!/bin/sh
2016-03-24 16:59:49 +01:00
if ! [ "$(basename $0)" = "install.sh" ]; then
# We are runing from stdin
2016-03-30 16:09:02 +02:00
url="https://raw.githubusercontent.com/limosek/zaf/master/"
if ! which curl >/dev/null;
then
zaf_err "Curl not found. Cannot continue. Please install it."
2016-03-30 16:09:02 +02:00
fi
echo "Installing from url $url..."
[ -z "$*" ] && auto=auto
2016-03-24 16:59:49 +01:00
set -e
mkdir -p /tmp/zaf-installer \
&& cd /tmp/zaf-installer \
2016-04-04 14:49:38 +02:00
&& (for i in lib/zaf.lib.sh lib/os.lib.sh lib/ctrl.lib.sh install.sh ; do echo curl -f -k -s -L -o - "$url/$i" >&2; curl -f -k -s -L -o - "$url/$i"; done) >install.sh \
2016-03-24 16:59:49 +01:00
&& chmod +x install.sh \
&& exec ./install.sh $auto "$@"
2016-03-30 16:09:02 +02:00
exit
2016-03-24 16:59:49 +01:00
fi
# 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
2016-03-24 09:38:34 +01:00
[ -z "$ZAF_CFG_FILE" ] && ZAF_CFG_FILE=$INSTALL_PREFIX/etc/zaf.conf
[ -n "$C_ZAF_DEBUG" ] && ZAF_DEBUG=$C_ZAF_DEBUG
2016-04-01 12:20:23 +02:00
[ -z "$ZAF_DEBUG" ] && ZAF_DEBUG=1
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
2016-03-24 15:46:42 +01:00
2016-03-24 09:38:34 +01:00
# 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="auto" , use autoconf. if $4="user", force asking.
2016-03-24 09:38:34 +01:00
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
2016-03-24 09:38:34 +01:00
eval opt=\$$1
if [ -n "$opt" ] && ! [ "$4" = "user" ]; then
eval "$1='$opt'"
zaf_dbg "Got '$2' <$1> from ENV: $opt"
2016-03-24 09:38:34 +01:00
return
else
opt="$3"
fi
if ! [ "$4" = "auto" ]; then
2016-03-24 09:38:34 +01:00
echo -n "$2 <$1> [$opt]: "
read opt
else
opt=""
fi
if [ -z "$opt" ]; then
opt="$3"
zaf_dbg "Got '$2' <$1> from Defaults: $opt" >&2
2016-03-24 09:38:34 +01:00
else
zaf_dbg "Got '$2' <$1> from USER: $opt"
2016-03-24 09:38:34 +01:00
fi
2016-03-30 16:09:02 +02:00
eval "$1='$opt'"
2016-03-24 09:38:34 +01:00
}
# Sets option to zaf.conf
# $1 option name
# $2 option value
zaf_set_option(){
2016-03-30 16:09:02 +02:00
local description
2016-03-24 09:38:34 +01:00
if ! grep -q "^$1=" ${ZAF_CFG_FILE}; then
echo "$1='$2'" >>${ZAF_CFG_FILE}
zaf_dbg "Saving $1 to $2 in ${ZAF_CFG_FILE}" >&2
2016-03-24 15:46:42 +01:00
else
zaf_wrn "Preserving $1 to $2 in ${ZAF_CFG_FILE}" >&2
2016-03-24 09:38:34 +01:00
fi
2016-03-23 14:37:51 +01:00
}
2016-03-24 16:59:49 +01:00
zaf_getrest(){
2016-03-23 14:37:51 +01:00
if [ -f "$(dirname $0)/$1" ]; then
echo "$(dirname $0)/$1"
else
2016-03-24 16:59:49 +01:00
curl -f -k -s -L -o - https://raw.githubusercontent.com/limosek/zaf/master/$1 >${ZAF_TMP_DIR}/$(basename $1)
2016-03-23 14:37:51 +01:00
echo ${ZAF_TMP_DIR}/$(basename $1)
fi
}
2016-03-30 16:09:02 +02:00
# Set config option in zabbix agent
# $1 option
# $2 value
2016-04-04 10:27:25 +02:00
# $3 if nonempty, do not remove opion from config, just add to the end
zaf_set_agent_option() {
2016-03-30 16:09:02 +02:00
local option="$1"
local value="$2"
2016-04-04 10:27:25 +02:00
if [ -n "$3" ]; then
if ! grep -q "^$1=$2" $ZAF_AGENT_CONFIG; then
zaf_dbg "Adding option $option to $ZAF_AGENT_CONFIG."
echo "$option=$value" >>$ZAF_AGENT_CONFIG
fi
return
fi
2016-03-30 16:09:02 +02:00
if grep ^$option\= $ZAF_AGENT_CONFIG; then
zaf_wrn "Moving option $option to zaf config part."
2016-03-30 16:09:02 +02:00
sed -i "s/$option=/#$option=/" $ZAF_AGENT_CONFIG
2016-03-24 09:38:34 +01:00
fi
[ -n "$2" ] && echo "$option=$value" >> "$ZAF_AGENT_CONFIGD/zaf_options.conf"
2016-03-30 16:09:02 +02:00
}
# Automaticaly configure agent if supported
# Parameters are in format Z_zabbixconfvar=value
2016-03-30 16:09:02 +02:00
zaf_configure_agent() {
local pair
local option
local value
zaf_install_dir "$ZAF_AGENT_CONFIGD"
echo -n >"$ZAF_AGENT_CONFIGD/zaf_options.conf" || zaf_err "Cannot access $ZAF_AGENT_CONFIGD/zaf_options.conf"
2016-03-30 16:09:02 +02:00
for pair in "$@"; do
echo $pair | grep -q '^Z\_' || continue # Skip non Z_ vars
2016-03-30 16:09:02 +02:00
option=$(echo $pair|cut -d '=' -f 1|cut -d '_' -f 2)
value=$(echo $pair|cut -d '=' -f 2-)
2016-04-04 10:27:25 +02:00
zaf_set_agent_option "$option" "$value"
2016-03-30 16:09:02 +02:00
done
2016-03-24 09:38:34 +01:00
}
2016-03-23 17:25:18 +01:00
2016-03-24 09:38:34 +01:00
zaf_configure(){
2016-03-30 16:09:02 +02:00
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
2016-03-30 16:09:02 +02:00
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 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
2016-03-30 16:09:02 +02:00
fi
fi
fi
2016-04-01 15:51:45 +02:00
if which git >/dev/null; then
ZAF_GIT=1
else
ZAF_GIT=""
fi
2016-03-24 16:59:49 +01:00
zaf_get_option ZAF_CURL_INSECURE "Insecure curl (accept all certificates)" "1" "$1"
2016-03-24 15:46:42 +01:00
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"
2016-03-24 15:46:42 +01:00
zaf_get_option ZAF_PLUGINS_DIR "Plugins directory" "${ZAF_LIB_DIR}/plugins" "$1"
2016-04-04 14:00:44 +02:00
[ "${ZAF_GIT}" = 1 ] && zaf_get_option ZAF_PLUGINS_GITURL "Git plugins repository" "https://github.com/limosek/zaf-plugins.git" "$1"
2016-04-01 15:51:45 +02:00
zaf_get_option ZAF_PLUGINS_URL "Plugins http[s] repository" "https://raw.githubusercontent.com/limosek/zaf-plugins/master/" "$1"
2016-03-24 15:46:42 +01:00
zaf_get_option ZAF_REPO_DIR "Plugins directory" "${ZAF_LIB_DIR}/repo" "$1"
zaf_get_option ZAF_AGENT_CONFIG "Zabbix agent config" "/etc/zabbix/zabbix_agentd.conf" "$1"
2016-03-24 17:54:01 +01:00
! [ -d "${ZAF_AGENT_CONFIGD}" ] && [ -d "/etc/zabbix/zabbix_agentd.d" ] && ZAF_AGENT_CONFIGD="/etc/zabbix/zabbix_agentd.d"
2016-03-24 15:46:42 +01:00
zaf_get_option ZAF_AGENT_CONFIGD "Zabbix agent config.d" "/etc/zabbix/zabbix_agentd.conf.d/" "$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"
2016-03-24 09:38:34 +01:00
2016-04-04 16:08:07 +02:00
if zaf_is_root && ! [ -x $ZAF_AGENT_BIN ]; then
zaf_err "Zabbix agent ($ZAF_AGENT_BIN) not installed? Use ZAF_AGENT_BIN env variable to specify location. Exiting."
2016-03-23 14:37:51 +01:00
fi
2016-03-24 15:46:42 +01:00
[ -n "$INSTALL_PREFIX" ] && zaf_install_dir "/etc"
2016-03-24 09:38:34 +01:00
if ! [ -f "${ZAF_CFG_FILE}" ]; then
touch "${ZAF_CFG_FILE}" || zaf_err "No permissions to ${ZAF_CFG_FILE}"
2016-03-23 14:37:51 +01:00
fi
2016-03-24 09:38:34 +01:00
zaf_set_option ZAF_PKG "${ZAF_PKG}"
2016-03-30 16:09:02 +02:00
zaf_set_option ZAF_OS "${ZAF_OS}"
zaf_set_option ZAF_OS_CODENAME "${ZAF_OS_CODENAME}"
zaf_set_option ZAF_AGENT_PKG "${ZAF_AGENT_PKG}"
2016-03-24 15:46:42 +01:00
zaf_set_option ZAF_GIT "${ZAF_GIT}"
2016-03-24 16:59:49 +01:00
zaf_set_option ZAF_CURL_INSECURE "${ZAF_CURL_INSECURE}"
2016-03-24 15:46:42 +01:00
zaf_set_option ZAF_TMP_BASE "$ZAF_TMP_BASE"
2016-03-24 09:38:34 +01:00
zaf_set_option ZAF_LIB_DIR "$ZAF_LIB_DIR"
zaf_set_option ZAF_BIN_DIR "$ZAF_BIN_DIR"
2016-03-24 09:38:34 +01:00
zaf_set_option ZAF_PLUGINS_DIR "$ZAF_PLUGINS_DIR"
2016-04-01 15:51:45 +02:00
zaf_set_option ZAF_PLUGINS_URL "$ZAF_PLUGINS_URL"
2016-04-04 14:00:44 +02:00
[ "${ZAF_GIT}" = 1 ] && zaf_set_option ZAF_PLUGINS_GITURL "$ZAF_PLUGINS_GITURL"
2016-03-24 15:46:42 +01:00
zaf_set_option ZAF_REPO_DIR "$ZAF_REPO_DIR"
2016-03-24 09:38:34 +01:00
zaf_set_option ZAF_AGENT_CONFIG "$ZAF_AGENT_CONFIG"
zaf_set_option ZAF_AGENT_CONFIGD "$ZAF_AGENT_CONFIGD"
zaf_set_option ZAF_AGENT_BIN "$ZAF_AGENT_BIN"
2016-03-24 15:46:42 +01:00
zaf_set_option ZAF_AGENT_RESTART "$ZAF_AGENT_RESTART"
ZAF_TMP_DIR="${ZAF_TMP_BASE}-${USER}-$$"
2016-03-23 14:37:51 +01:00
}
2016-03-24 09:38:34 +01:00
if [ -f "${ZAF_CFG_FILE}" ]; then
. "${ZAF_CFG_FILE}"
fi
ZAF_TMP_DIR="${ZAF_TMP_BASE-/tmp/zaf}-${USER}-$$"
2016-03-24 09:38:34 +01:00
2016-03-23 14:37:51 +01:00
case $1 in
2016-03-30 16:09:02 +02:00
interactive)
shift
2016-03-30 16:09:02 +02:00
zaf_configure interactive
$0 install "$@"
2016-03-24 15:46:42 +01:00
;;
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 "$@"
2016-04-04 10:27:25 +02:00
zaf_set_agent_option "Include" "$ZAF_AGENT_CONFIGD" append
2016-03-23 14:37:51 +01:00
rm -rif ${ZAF_TMP_DIR}
2016-03-24 16:59:49 +01:00
mkdir -p ${ZAF_TMP_DIR}
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_bin $(zaf_getrest lib/zaflock) ${ZAF_LIB_DIR}
2016-04-01 12:20:23 +02:00
zaf_install_bin $(zaf_getrest lib/preload.sh) ${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}
2016-04-04 16:08:07 +02:00
export INSTALL_PREFIX ZAF_CFG_FILE ZAF_DEBUG
if zaf_is_root; then
2016-04-04 14:00:44 +02:00
[ "${ZAF_GIT}" = 1 ] && ${INSTALL_PREFIX}/${ZAF_BIN_DIR}/zaf update
2016-04-01 15:51:45 +02:00
${INSTALL_PREFIX}/${ZAF_BIN_DIR}/zaf reinstall zaf || zaf_err "Error installing zaf plugin."
2016-04-04 10:27:25 +02:00
if zaf_is_root && ! zaf_test_item zaf.framework_version; then
2016-03-23 17:25:18 +01:00
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
2016-03-23 17:25:18 +01:00
rm -rif ${ZAF_TMP_DIR}
2016-03-24 15:46:42 +01:00
echo "Install OK. Use 'zaf' without parameters to continue."
2016-03-23 14:37:51 +01:00
;;
2016-03-30 16:09:02 +02:00
*)
echo
echo "Please specify how to install."
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"
2016-03-30 16:09:02 +02:00
echo "Example 4 (interactive): install.sh interactive"
echo
exit 1
2016-03-23 14:37:51 +01:00
esac