From a99c9b828058e07887d2e840a7b15cd2f82dc306 Mon Sep 17 00:00:00 2001 From: Lukas Macura Date: Tue, 12 Apr 2016 19:31:06 +0200 Subject: [PATCH] Added cron support Added precache support --- install.sh | 2 ++ lib/cache.lib.sh | 2 ++ lib/ctrl.lib.sh | 17 ++++++++++++++++- lib/preload.sh | 4 ++-- lib/zaf.lib.sh | 5 +++++ zaf | 22 ++++++++++++++++++++-- 6 files changed, 47 insertions(+), 5 deletions(-) diff --git a/install.sh b/install.sh index f1e1e24..a09132b 100755 --- a/install.sh +++ b/install.sh @@ -226,6 +226,7 @@ zaf_configure(){ zaf_get_option ZAF_AGENT_BIN "Zabbix agent binary" "/usr/sbin/zabbix_agentd" "$INSTALL_MODE" zaf_get_option ZAF_AGENT_RESTART "Zabbix agent restart cmd" "service zabbix-agent restart" "$INSTALL_MODE" zaf_get_option ZAF_SUDOERSD "Sudo sudoers.d directory" "/etc/sudoers.d" "$INSTALL_MODE" + zaf_get_option ZAF_CROND "Cron.d directory" "/etc/cron.d" "$INSTALL_MODE" zaf_get_option ZAF_ZBXAPI_URL "Zabbix API url" "http://localhost/zabbix/api_jsonrpc.php" "$INSTALL_MODE" zaf_get_option ZAF_ZBXAPI_USER "Zabbix API user" "zaf" "$INSTALL_MODE" zaf_get_option ZAF_ZBXAPI_PASS "Zabbix API password" "" "$INSTALL_MODE" @@ -258,6 +259,7 @@ zaf_configure(){ zaf_set_option ZAF_AGENT_BIN "$ZAF_AGENT_BIN" zaf_set_option ZAF_AGENT_RESTART "$ZAF_AGENT_RESTART" zaf_set_option ZAF_SUDOERSD "$ZAF_SUDOERSD" + zaf_set_option ZAF_CROND "$ZAF_CROND" zaf_set_option ZAF_ZBXAPI_URL "$ZAF_ZBXAPI_URL" zaf_set_option ZAF_ZBXAPI_USER "$ZAF_ZBXAPI_USER" zaf_set_option ZAF_ZBXAPI_PASS "$ZAF_ZBXAPI_PASS" diff --git a/lib/cache.lib.sh b/lib/cache.lib.sh index 93d8695..54884b4 100644 --- a/lib/cache.lib.sh +++ b/lib/cache.lib.sh @@ -25,6 +25,7 @@ zaf_tocache(){ local lifetime key=$(zaf_cachekey $1) echo "$2" >$ZAF_CACHE_DIR/$key + echo "$1" >$ZAF_CACHE_DIR/$key.key touch -m -d "$3 seconds" $ZAF_CACHE_DIR/$key.tme zaf_trc "Cache: Saving entry $1($key)" } @@ -38,6 +39,7 @@ zaf_tocache_stdin(){ key=$(zaf_cachekey $1) cat >$ZAF_CACHE_DIR/$key + echo "$1" >$ZAF_CACHE_DIR/$key.key touch -m -d "$3 seconds" $ZAF_CACHE_DIR/$key.tme zaf_trc "Cache: Saving entry $1($key)" cat $ZAF_CACHE_DIR/$key diff --git a/lib/ctrl.lib.sh b/lib/ctrl.lib.sh index 35612dc..8d79499 100644 --- a/lib/ctrl.lib.sh +++ b/lib/ctrl.lib.sh @@ -95,11 +95,26 @@ zaf_ctrl_sudo() { fi pdir="$3" plugin=$1 - (echo -n "zabbix ALL=NOPASSWD: " + zaf_dbg "Installing sudoers entry $ZAF_SUDOERSD/zaf_$plugin" + (echo -n "zabbix ALL=NOPASSWD:SETENV: " zaf_ctrl_get_global_option $2 "Sudo" | zaf_far '{PLUGINDIR}' "${plugindir}"; echo ) >$ZAF_SUDOERSD/zaf_$plugin } +# Install crontab config from control +# $1 plugin +# $2 control +# $3 plugindir +zaf_ctrl_cron() { + local pdir + local plugin + + pdir="$3" + plugin=$1 + zaf_dbg "Installing cron entry $ZAF_CROND/zaf_$plugin" + zaf_ctrl_get_global_option $2 "Cron" | zaf_far '{PLUGINDIR}' "${plugindir}" >$ZAF_CROND/zaf_$plugin +} + # Install sudo options from control # $1 pluginurl # $2 control diff --git a/lib/preload.sh b/lib/preload.sh index fefde66..c2cbc50 100644 --- a/lib/preload.sh +++ b/lib/preload.sh @@ -4,9 +4,9 @@ [ -z "$ZAF_TMP_BASE" ] && ZAF_TMP_BASE=/tmp/zaf ZAF_TMP_DIR="${ZAF_TMP_BASE}-${USER}" -[ -z "$ZAF_CACHE_DIR" ] && ZAF_CACHE_DIR=${ZAF_TMP_BASE}-${USER}c +[ -z "$ZAF_CACHE_DIR" ] && ZAF_CACHE_DIR=${ZAF_TMP_BASE}c -#trap "rm -rif ${ZAF_TMP_DIR}" EXIT +trap "rm -rif ${ZAF_TMP_DIR}" EXIT ! [ -d "${ZAF_TMP_DIR}" ] && mkdir "${ZAF_TMP_DIR}" ! [ -d "${ZAF_CACHE_DIR}" ] && mkdir "${ZAF_CACHE_DIR}" [ -z "$ZAF_DEBUG" ] && ZAF_DEBUG=1 diff --git a/lib/zaf.lib.sh b/lib/zaf.lib.sh index 5294cc0..093dfdc 100644 --- a/lib/zaf.lib.sh +++ b/lib/zaf.lib.sh @@ -382,6 +382,11 @@ zaf_test_item() { $ZAF_AGENT_BIN -t "$1" } +zaf_precache_item() { + cmd=$(grep "^UserParameter=$item" $ZAF_AGENT_CONFIGD/zaf*.conf | cut -d ',' -f 2-) + eval $cmd +} + zaf_remove_plugin() { ! [ -d ${ZAF_PLUGINS_DIR}/$1 ] && { zaf_err "Plugin $1 not installed!"; } zaf_wrn "Removing plugin $1" diff --git a/zaf b/zaf index 11c289f..275a413 100755 --- a/zaf +++ b/zaf @@ -18,7 +18,7 @@ done [ -z "$ZAF_TMP_BASE" ] && ZAF_TMP_BASE=/tmp/zaf ZAF_TMP_DIR="${ZAF_TMP_BASE}-${USER}" -[ -z "$ZAF_CACHE_DIR" ] && ZAF_CACHE_DIR=${ZAF_TMP_BASE}-${USER}c +[ -z "$ZAF_CACHE_DIR" ] && ZAF_CACHE_DIR=${ZAF_TMP_BASE}c [ -z "$ZAF_DEBUG" ] && ZAF_DEBUG=1 export ZAF_DEBUG @@ -29,7 +29,9 @@ if [ "$ZAF_DEBUG" -le 3 ]; then fi ! [ -d "${ZAF_TMP_DIR}" ] && mkdir "${ZAF_TMP_DIR}" -! [ -d "${ZAF_CACHE_DIR}" ] && mkdir "${ZAF_CACHE_DIR}" +if ! [ -d "${ZAF_CACHE_DIR}" ]; then + mkdir "${ZAF_CACHE_DIR}" +fi if [ -f ./lib/zaf.lib.sh ]; then . ./lib/zaf.lib.sh @@ -47,6 +49,11 @@ else [ -f ${ZAF_LIB_DIR}/zaf.${ZAF_OS}.sh ] && . ${ZAF_LIB_DIR}/zaf.${ZAF_OS}.sh fi +if zaf_is_root; then + chgrp zabbix "${ZAF_CACHE_DIR}" + chmod g+w "${ZAF_CACHE_DIR}" +fi + zaf_shift(){ local s @@ -173,6 +180,17 @@ get) done done ;; + +precache) + shift + shift $(zaf_shift "$@") + for i in $*; do + item=$(echo $i | cut -d '[' -f 1) + params=$(echo $i | cut -d '[' -f 2 | cut -d ']' -f 1 | tr ',' ' ') + zaf_precache_item $params + done + ;; + install) shift shift $(zaf_shift "$@")