From 76352d7d12c823cb160dbe3c7b3f5e32c184abb3 Mon Sep 17 00:00:00 2001 From: Lukas Macura Date: Wed, 20 Apr 2016 09:39:24 +0200 Subject: [PATCH] Repaired touch variants Added cache-list command --- lib/cache.lib.sh | 26 ++++++++++++++++++-------- lib/os.lib.sh | 1 + lib/zaf.lib.sh | 7 ------- lib/zbxapi.lib.sh | 10 +++++++--- zaf | 7 ++++--- 5 files changed, 30 insertions(+), 21 deletions(-) diff --git a/lib/cache.lib.sh b/lib/cache.lib.sh index 80a6036..0cc3285 100644 --- a/lib/cache.lib.sh +++ b/lib/cache.lib.sh @@ -16,7 +16,7 @@ zaf_cache_clean(){ # Get cache key from requested param zaf_cache_key(){ - echo "$1" | md5sum - | cut -d ' ' -f 1 + echo "$1" | (md5sum - ||md5) 2>/dev/null | cut -d ' ' -f 1 } # Put object into cache @@ -31,10 +31,10 @@ zaf_tocache(){ key=$(zaf_cache_key "$1") echo "$2" >$ZAF_CACHE_DIR/$key - echo "$1" >$ZAF_CACHE_DIR/$key.info + echo "$1" >$ZAF_CACHE_DIR/${key}.info expiry=$(zaf_date_add "$3") - touch -m -d "$expiry" $ZAF_CACHE_DIR/$key.info - zaf_trc "Cache: Saving entry $1($key)" + zaf_trc "Cache: Saving entry $1[$key,expiry=$expiry]" + touch -m -d "$expiry" $ZAF_CACHE_DIR/${key}.info } # Put object into cache from stdin and copy to stdout @@ -48,13 +48,13 @@ zaf_tocache_stdin(){ key=$(zaf_cache_key "$1") cat >$ZAF_CACHE_DIR/$key if [ -s $ZAF_CACHE_DIR/$key ]; then - zaf_trc "Cache: Saving entry $1($key)" - echo "$1" >$ZAF_CACHE_DIR/$key.info - expiry=$(zaf_date_add "$3") + expiry="$(zaf_date_add $2)" + echo "$1 [key=$key,expiry=$expiry]" >$ZAF_CACHE_DIR/${key}.info + zaf_trc "Cache: Saving entry $1[key=$key,expiry=$expiry]" touch -m -d "$expiry" $ZAF_CACHE_DIR/$key.info cat $ZAF_CACHE_DIR/$key else - rm $ZAF_CACHE_DIR/$key + rm -f "$ZAF_CACHE_DIR/$key" fi } @@ -68,6 +68,16 @@ zaf_cache_delentry(){ rm -f "$ZAF_CACHE_DIR/$key*" } +# List entries in cache +zaf_cache_list(){ + local i + ls $ZAF_CACHE_DIR/*info >/dev/null 2>/dev/null || return 1 + local key + for i in $ZAF_CACHE_DIR/*info; do + cat $i + done +} + # Get object from cache # $1 key zaf_fromcache(){ diff --git a/lib/os.lib.sh b/lib/os.lib.sh index 744396a..d10ed66 100644 --- a/lib/os.lib.sh +++ b/lib/os.lib.sh @@ -17,6 +17,7 @@ zaf_configure_os_freebsd() { ZAF_AGENT_CONFIGD="/usr/local/etc/zabbix3/zabbix_agentd.conf.d/" ZAF_AGENT_BIN="/usr/local/sbin/zabbix_agentd" ZAF_AGENT_RESTART="service zabbix_agentd restart" + ZAF_SUDOERSD="/usr/local/etc/sudoers.d" } zaf_detect_system() { diff --git a/lib/zaf.lib.sh b/lib/zaf.lib.sh index 25eaca0..1856134 100644 --- a/lib/zaf.lib.sh +++ b/lib/zaf.lib.sh @@ -180,12 +180,6 @@ zaf_restart_agent() { ${ZAF_AGENT_RESTART} || zaf_err "Cannot restart Zabbix agent (${ZAF_AGENT_RESTART}). Try $ZAF_AGENT_BIN -f !"; } -# Check if zaf.version item is populated -zaf_check_agent_config() { - zaf_restart_agent - ${ZAF_AGENT_BIN} -t zaf.version -} - zaf_tolower() { tr 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' 'abcdefghijklmnopqrstuvwxyz' } @@ -205,7 +199,6 @@ zaf_strunescape() { sed -e 's#\\\(['"$1"']\)#\1#g' } - # Escape string on stdin # $1 - list of chars to escape zaf_strescape() { diff --git a/lib/zbxapi.lib.sh b/lib/zbxapi.lib.sh index a867a6b..7bbe4e4 100644 --- a/lib/zbxapi.lib.sh +++ b/lib/zbxapi.lib.sh @@ -3,11 +3,13 @@ # $1 - query string zaf_zbxapi_do() { local result + local query local tmpfile tmpfile=$ZAF_TMP_DIR/zapi$$ - zaf_trc "Zabbix API: $1" - curl -s -f -L -X POST -H 'Content-Type: application/json-rpc' -d "$1" "$ZAF_ZBXAPI_URL" >$tmpfile + query="$1" + zaf_trc "Zabbix API: $query" + curl -s -f -L -X POST -H 'Content-Type: application/json-rpc' -d "$query" "$ZAF_ZBXAPI_URL" >$tmpfile if [ $? = 0 ] && $ZAF_LIB_DIR/JSON.sh -b <$tmpfile | grep -q '"result"'; then zaf_trc "API OK" cat $tmpfile @@ -21,11 +23,13 @@ zaf_zbxapi_do() { zaf_zbxapi_do_cache() { local result local tmpfile + local query + query="$(echo $1 | tr '\n' ' ')" tmpfile=$ZAF_TMP_DIR/zcapi$$ if ! zaf_fromcache "$1"; then zaf_zbxapi_do "$1" >$tmpfile - [ -s "$tmpfile" ] && cat $tmpfile | zaf_tocache_stdin "$1" 60 + [ -s "$tmpfile" ] && cat $tmpfile | zaf_tocache_stdin "$query" 60 rm -f $tmpfile fi } diff --git a/zaf b/zaf index c78211e..18383d8 100755 --- a/zaf +++ b/zaf @@ -69,6 +69,9 @@ case $1 in cache-clean) zaf_cache_clean ;; +cache-list) + zaf_cache_list + ;; version) echo "$ZAF_VERSION" @@ -95,9 +98,6 @@ agent-config) | zaf_far '{PLUGINDIR}' "${plugindir}" >${ZAF_AGENT_CONFIGD}/zaf_${plugin}.conf done ;; -check-agent-config) - zaf_check_agent_config - ;; ###### Plugins related commands update) @@ -443,6 +443,7 @@ api) zaf_hlp "$0 self-upgrade" "To self-upgrade zaf" zaf_hlp "$0 self-remove" "To self-remove zaf and its config" zaf_hlp "$0 cache-clean" "To remove all entries from cache" + zaf_hlp "$0 cache-list" "To show all entries in cache" echo [ -n "$1" ] && zaf_err "Bad command '$1'!" ;;