From 0ef05853a4606a1cbd50fba488c97fba56809446 Mon Sep 17 00:00:00 2001 From: Lukas Macura Date: Fri, 2 Dec 2016 13:12:13 +0100 Subject: [PATCH] Plugin installation is possible offline Plugin postconfiguration will be called on zaf agent-config --- README.md | 1 + deb.mk | 15 ++------ files/debian/postinst.template | 14 +------- install.sh | 7 +++- lib/plugin.lib.sh | 64 ++++++++++++++++++++++------------ zaf | 14 ++++++-- 6 files changed, 64 insertions(+), 51 deletions(-) diff --git a/README.md b/README.md index c552a02..a786377 100644 --- a/README.md +++ b/README.md @@ -81,6 +81,7 @@ ZAF_ZBXAPI_URL|Zabbix API url|http://localhost/zabbix/api_jsonrpc.php ZAF_ZBXAPI_USER|Zabbix API user|zaf ZAF_ZBXAPI_PASS|Zabbix API password|empty ZAF_ZBXAPI_AUTHTYPE|Zabbix API authentication type|internal +ZAF_PLUGINS|Plugins to autoinstall|empty Installer will try to autoguess suitable config options for your system. Now everything was tested on Debian and OpenWrt. If somebody is interrested in, you can help and test with some rpm specific functions. Remember that on some systems, default zabbix agent config is empty so you *need to* enter essential config options as parameters. diff --git a/deb.mk b/deb.mk index cee1979..4c6a996 100644 --- a/deb.mk +++ b/deb.mk @@ -28,7 +28,7 @@ deb-control: deb-scripts: @. lib/zaf.lib.sh; \ . lib/ctrl.lib.sh; \ - cat files/debian/postinst.template | zaf_far '{PLUGINS}' "$(PLUGINS)" | zaf_far "{IPLUGINS}" "$(IPLUGINS)" | zaf_far '{ZAF_LIB_DIR}' "/usr/lib/zaf" >$(DEBIAN_CTRL)/postinst + cat files/debian/postinst.template >$(DEBIAN_CTRL)/postinst @chmod +x $(DEBIAN_CTRL)/postinst @cp files/debian/preinst.template $(DEBIAN_CTRL)/preinst @chmod +x $(DEBIAN_CTRL)/preinst @@ -37,20 +37,12 @@ deb-scripts: deb-cp: @mkdir -p $(DEBIAN_DIR) - @set -e; INSTALL_PREFIX=$(DEBIAN_DIR) ZAF_DEBUG=$(ZAF_DEBUG) ./install.sh auto $(ZAF_OPTIONS) $(AGENT_OPTIONS) - @. lib/zaf.lib.sh; \ - . lib/ctrl.lib.sh; \ - for p in $(PLUGINS); do \ - plugin=$$(zaf_ctrl_get_global_option $$p/control.zaf Plugin) ; \ - mkdir -p $(DEBIAN_DIR)/usr/lib/zaf/prepackaged/$$plugin/; \ - cp -R $$p/* $(DEBIAN_DIR)/usr/lib/zaf/prepackaged/$$plugin/; \ - done + @set -e; INSTALL_PREFIX=$(DEBIAN_DIR) ZAF_DEBUG=$(ZAF_DEBUG) ./install.sh auto $(ZAF_OPTIONS) ZAF_PLUGINS="$(ZAF_PLUGINS)" $(AGENT_OPTIONS) @cat lib/*lib.sh install.sh >$(DEBIAN_DIR)/usr/lib/zaf/install.sh @chmod +x $(DEBIAN_DIR)/usr/lib/zaf/install.sh @rm -rf $(DEBIAN_DIR)/tmp @cp $(DEBIAN_DIR)/etc/zaf.conf tmp/zaf.conf @grep -E "$$(echo $(ZAF_EXPORT_OPTS) | tr ' ' '|')=" tmp/zaf.conf >$(DEBIAN_DIR)/etc/zaf.conf - @echo "ZAF_PREPACKAGED_DIR=\"/usr/lib/zaf/prepackaged\"" >>$(DEBIAN_DIR)/etc/zaf.conf ifneq ($(AGENT_OPTIONS),) @echo "ZAF_AGENT_OPTIONS=\"$(AGENT_OPTIONS)\"" >>$(DEBIAN_DIR)/etc/zaf.conf endif @@ -59,8 +51,7 @@ deb-package: @dpkg-deb -b $(DEBIAN_DIR) $(DEBIAN_PKG) @echo "\nCheck configuration:" @cat $(DEBIAN_DIR)/etc/zaf.conf - @echo PLUGINS embedded: $(PLUGINS) - @echo PLUGINS in postinst: $(IPLUGINS) + @echo PLUGINS embedded: $(ZAF_PLUGINS) @echo diff --git a/files/debian/postinst.template b/files/debian/postinst.template index 07301f7..06d8f5f 100644 --- a/files/debian/postinst.template +++ b/files/debian/postinst.template @@ -2,19 +2,7 @@ case $1 in configure) - if [ -f /usr/lib/zaf/install.sh ] && [ -f /etc/zaf.conf ] && [ -f /usr/lib/zaf/zaf.lib.sh ] && which zaf >/dev/null; then - . /etc/zaf.conf - . /usr/lib/zaf/zaf.lib.sh - . /usr/lib/zaf/os.lib.sh - . /usr/lib/zaf/ctrl.lib.sh - cd /usr/lib/zaf && /usr/lib/zaf/install.sh reconf - [ -n "{PLUGINS}" ] && for p in {PLUGINS}; do - if ! zaf_is_plugin $p;then - zaf install $ZAF_PREPACKAGED_DIR/$p; - fi - done - [ -n "{IPLUGINS}" ] && zaf reinstall {IPLUGINS} - fi + zaf agent-config ;; esac diff --git a/install.sh b/install.sh index 4dcc3b8..eda664e 100755 --- a/install.sh +++ b/install.sh @@ -280,6 +280,7 @@ zaf_configure(){ zaf_get_option ZAF_ZBXAPI_USER "Zabbix API user" "zaf" "$INSTALL_MODE" zaf_get_option ZAF_ZBXAPI_PASS "Zabbix API password" "" "$INSTALL_MODE" zaf_get_option ZAF_ZBXAPI_AUTHTYPE "Zabbix API authentication type" "internal" "$INSTALL_MODE" + zaf_get_option ZAF_PLUGINS "Plugins to autoinstall" "" "$INSTALL_MODE" 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." @@ -329,7 +330,11 @@ zaf_configure(){ zaf_set_option ZAF_ZBXAPI_USER "$ZAF_ZBXAPI_USER" zaf_set_option ZAF_ZBXAPI_PASS "$ZAF_ZBXAPI_PASS" zaf_set_option ZAF_ZBXAPI_AUTHTYPE "$ZAF_ZBXAPI_AUTHTYPE" - [ -n "$ZAF_PREPACKAGED_DIR" ] && zaf_set_option ZAF_PREPACKAGED_DIR "$ZAF_PREPACKAGED_DIR" + if [ -n "$ZAF_PLUGINS" ]; then + for p in $ZAF_PLUGINS; do + zaf_install_plugin $p + done + fi if zaf_is_root; then zaf_configure_agent $ZAF_AGENT_OPTIONS "$@" diff --git a/lib/plugin.lib.sh b/lib/plugin.lib.sh index 8a31fe6..de55c26 100644 --- a/lib/plugin.lib.sh +++ b/lib/plugin.lib.sh @@ -84,16 +84,20 @@ zaf_prepare_plugin() { local url local plugindir local control + local pluginname url=$(zaf_get_plugin_url "$1")/control.zaf || exit $? plugindir="$2" control=${plugindir}/control.zaf zaf_install_dir "$plugindir" zaf_dbg "Fetching control file from $url ..." - if zaf_fetch_url "$url" >"${control}"; then - zaf_ctrl_check_deps "${control}" + if zaf_fetch_url "$url" >"${INSTALL_PREFIX}/${control}"; then + [ -z "${INSTALL_PREFIX}" ] && zaf_ctrl_check_deps "${control}" + pluginname=$(zaf_ctrl_get_global_block <"${INSTALL_PREFIX}/${control}" | zaf_block_get_option Plugin) + [ "$(basename $plugindir)" != "$pluginname" ] && zaf_err "prepare_plugin: Plugin name mismach ($plugindir vs $pluginname)!" + true else - zaf_err "prepare_plugin: Cannot fetch or write control file $control from url $url!" + zaf_err "prepare_plugin: Cannot fetch or write control file ${INSTALL_PREFIX}/$control from url $url!" fi } @@ -106,36 +110,52 @@ zaf_install_plugin() { local version plugin=$(basename "$1") - tmpplugindir="${ZAF_TMP_DIR}/zaf-installer/$plugin" - mkdir -p $tmpplugindir - if zaf_prepare_plugin "$1" "$tmpplugindir"; then - url=$(zaf_get_plugin_url "$1") - control="$tmpplugindir/control.zaf" - plugin=$(zaf_ctrl_get_global_option $control Plugin) - version=$(zaf_ctrl_get_global_option $control Version) - plugindir="${ZAF_PLUGINS_DIR}/$plugin" - if [ -n "$plugin" ] && zaf_prepare_plugin "$1" $plugindir; then - zaf_wrn "Installing plugin $plugin version $version" - zaf_dbg "Source url: $url, Destination dir: $plugindir" - control=${plugindir}/control.zaf - [ "$ZAF_DEBUG" -gt 1 ] && zaf_plugin_info "${control}" + plugindir="${ZAF_PLUGINS_DIR}/${plugin}" + if [ -n "$plugin" ] && zaf_prepare_plugin "$1" $plugindir; then + zaf_wrn "Installing plugin $plugin" + zaf_dbg "Source url: $url, Destination dir: $plugindir" + control=${plugindir}/control.zaf + [ "$ZAF_DEBUG" -gt 1 ] && zaf_plugin_info "${control}" + if [ -z "${INSTALL_PREFIX}" ]; then zaf_ctrl_check_deps "${control}" zaf_ctrl_sudo "$plugin" "${control}" "${plugindir}" zaf_ctrl_cron "$plugin" "${control}" "${plugindir}" zaf_ctrl_generate_items_cfg "${control}" "${plugin}" \ | zaf_far '{PLUGINDIR}' "${plugindir}" >${ZAF_AGENT_CONFIGD}/zaf_${plugin}.conf zaf_dbg "Generated ${ZAF_AGENT_CONFIGD}/zaf_${plugin}.conf" - zaf_ctrl_generate_extitems_cfg "${control}" "${plugin}" - zaf_ctrl_install "$url" "${control}" "${plugindir}" + zaf_ctrl_generate_extitems_cfg "${control}" "${plugin}" else - zaf_err "Cannot install plugin '$plugin' to $plugindir!" + zaf_touch "${plugindir}/needinstall" fi - else - zaf_err "Cannot prepare plugin $1" + zaf_ctrl_install "$url" "${control}" "${plugindir}" + else + zaf_err "Cannot install plugin '$plugin' to $plugindir!" fi - rm -rf $tmpplugindir } +zaf_postinstall_plugin() { + local url + local plugin + local plugindir + local tmpplugindir + local control + local version + + plugin=$(basename "$1") + plugindir="${ZAF_PLUGINS_DIR}/${plugin}" + control=${plugindir}/control.zaf + [ "$ZAF_DEBUG" -gt 1 ] && zaf_plugin_info "${control}" + zaf_ctrl_check_deps "${control}" + zaf_ctrl_sudo "$plugin" "${control}" "${plugindir}" + zaf_ctrl_cron "$plugin" "${control}" "${plugindir}" + zaf_ctrl_generate_items_cfg "${control}" "${plugin}" \ + | zaf_far '{PLUGINDIR}' "${plugindir}" >${ZAF_AGENT_CONFIGD}/zaf_${plugin}.conf + zaf_dbg "Generated ${ZAF_AGENT_CONFIGD}/zaf_${plugin}.conf" + zaf_ctrl_generate_extitems_cfg "${control}" "${plugin}" + zaf_ctrl_install "$url" "${control}" "${plugindir}" +} + + # List installed plugins # $1 - plugin zaf_list_plugins() { diff --git a/zaf b/zaf index 16152a7..76d8b8b 100755 --- a/zaf +++ b/zaf @@ -88,13 +88,21 @@ userparms) done ;; agent-config) + for p in $(zaf_list_plugins); do + if [ -f "$ZAF_PLUGINS_DIR/$p/needinstall" ]; then + zaf_wrn "Calling postinstalation of plugin $p" + zaf_postinstall_plugin $p && rm -f "$ZAF_PLUGINS_DIR/$p/needinstall" + fi + done zaf_wrn "Generating config files in $ZAF_AGENT_CONFIGD ..." for plugin in $(zaf_list_plugins); do plugindir=$ZAF_PLUGINS_DIR/$plugin control=$plugindir/control.zaf - zaf_dbg "Generating ${ZAF_AGENT_CONFIGD}/zaf_${plugin}.conf" - zaf_ctrl_generate_items_cfg "${control}" "${plugin}" \ + if ! [ -f "${ZAF_AGENT_CONFIGD}/zaf_${plugin}.conf" ] || [ "$control" -nt "${ZAF_AGENT_CONFIGD}/zaf_${plugin}.conf" ] || [ -n "$2" ]; then + zaf_dbg "Generating ${ZAF_AGENT_CONFIGD}/zaf_${plugin}.conf" + zaf_ctrl_generate_items_cfg "${control}" "${plugin}" \ | zaf_far '{PLUGINDIR}' "${plugindir}" >${ZAF_AGENT_CONFIGD}/zaf_${plugin}.conf + fi done ;; server-config) @@ -442,7 +450,7 @@ api) echo echo "Agent config info commands:" zaf_hlp "$0 userparms" "See userparms generated from zaf on stdout" - zaf_hlp "$0 agent-config" "Reconfigure zabbix userparms in $ZAF_AGENT_CONFIGD" + zaf_hlp "$0 agent-config [force]" "Reconfigure zabbix userparms in $ZAF_AGENT_CONFIGD" echo echo "Zaf related commands:" zaf_hlp "$0 self-upgrade" "To self-upgrade zaf"