1
0
mirror of https://github.com/limosek/zaf.git synced 2024-11-01 00:07:20 +01:00

Repaired touch variants

Added cache-list command
This commit is contained in:
Lukas Macura 2016-04-20 09:39:24 +02:00
parent ca3962ba05
commit 76352d7d12
5 changed files with 30 additions and 21 deletions

View File

@ -16,7 +16,7 @@ zaf_cache_clean(){
# Get cache key from requested param # Get cache key from requested param
zaf_cache_key(){ 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 # Put object into cache
@ -31,10 +31,10 @@ zaf_tocache(){
key=$(zaf_cache_key "$1") key=$(zaf_cache_key "$1")
echo "$2" >$ZAF_CACHE_DIR/$key 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") expiry=$(zaf_date_add "$3")
touch -m -d "$expiry" $ZAF_CACHE_DIR/$key.info zaf_trc "Cache: Saving entry $1[$key,expiry=$expiry]"
zaf_trc "Cache: Saving entry $1($key)" touch -m -d "$expiry" $ZAF_CACHE_DIR/${key}.info
} }
# Put object into cache from stdin and copy to stdout # Put object into cache from stdin and copy to stdout
@ -48,13 +48,13 @@ zaf_tocache_stdin(){
key=$(zaf_cache_key "$1") key=$(zaf_cache_key "$1")
cat >$ZAF_CACHE_DIR/$key cat >$ZAF_CACHE_DIR/$key
if [ -s $ZAF_CACHE_DIR/$key ]; then if [ -s $ZAF_CACHE_DIR/$key ]; then
zaf_trc "Cache: Saving entry $1($key)" expiry="$(zaf_date_add $2)"
echo "$1" >$ZAF_CACHE_DIR/$key.info echo "$1 [key=$key,expiry=$expiry]" >$ZAF_CACHE_DIR/${key}.info
expiry=$(zaf_date_add "$3") zaf_trc "Cache: Saving entry $1[key=$key,expiry=$expiry]"
touch -m -d "$expiry" $ZAF_CACHE_DIR/$key.info touch -m -d "$expiry" $ZAF_CACHE_DIR/$key.info
cat $ZAF_CACHE_DIR/$key cat $ZAF_CACHE_DIR/$key
else else
rm $ZAF_CACHE_DIR/$key rm -f "$ZAF_CACHE_DIR/$key"
fi fi
} }
@ -68,6 +68,16 @@ zaf_cache_delentry(){
rm -f "$ZAF_CACHE_DIR/$key*" 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 # Get object from cache
# $1 key # $1 key
zaf_fromcache(){ zaf_fromcache(){

View File

@ -17,6 +17,7 @@ zaf_configure_os_freebsd() {
ZAF_AGENT_CONFIGD="/usr/local/etc/zabbix3/zabbix_agentd.conf.d/" ZAF_AGENT_CONFIGD="/usr/local/etc/zabbix3/zabbix_agentd.conf.d/"
ZAF_AGENT_BIN="/usr/local/sbin/zabbix_agentd" ZAF_AGENT_BIN="/usr/local/sbin/zabbix_agentd"
ZAF_AGENT_RESTART="service zabbix_agentd restart" ZAF_AGENT_RESTART="service zabbix_agentd restart"
ZAF_SUDOERSD="/usr/local/etc/sudoers.d"
} }
zaf_detect_system() { zaf_detect_system() {

View File

@ -180,12 +180,6 @@ zaf_restart_agent() {
${ZAF_AGENT_RESTART} || zaf_err "Cannot restart Zabbix agent (${ZAF_AGENT_RESTART}). Try $ZAF_AGENT_BIN -f !"; ${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() { zaf_tolower() {
tr 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' 'abcdefghijklmnopqrstuvwxyz' tr 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' 'abcdefghijklmnopqrstuvwxyz'
} }
@ -205,7 +199,6 @@ zaf_strunescape() {
sed -e 's#\\\(['"$1"']\)#\1#g' sed -e 's#\\\(['"$1"']\)#\1#g'
} }
# Escape string on stdin # Escape string on stdin
# $1 - list of chars to escape # $1 - list of chars to escape
zaf_strescape() { zaf_strescape() {

View File

@ -3,11 +3,13 @@
# $1 - query string # $1 - query string
zaf_zbxapi_do() { zaf_zbxapi_do() {
local result local result
local query
local tmpfile local tmpfile
tmpfile=$ZAF_TMP_DIR/zapi$$ tmpfile=$ZAF_TMP_DIR/zapi$$
zaf_trc "Zabbix API: $1" query="$1"
curl -s -f -L -X POST -H 'Content-Type: application/json-rpc' -d "$1" "$ZAF_ZBXAPI_URL" >$tmpfile 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 if [ $? = 0 ] && $ZAF_LIB_DIR/JSON.sh -b <$tmpfile | grep -q '"result"'; then
zaf_trc "API OK" zaf_trc "API OK"
cat $tmpfile cat $tmpfile
@ -21,11 +23,13 @@ zaf_zbxapi_do() {
zaf_zbxapi_do_cache() { zaf_zbxapi_do_cache() {
local result local result
local tmpfile local tmpfile
local query
query="$(echo $1 | tr '\n' ' ')"
tmpfile=$ZAF_TMP_DIR/zcapi$$ tmpfile=$ZAF_TMP_DIR/zcapi$$
if ! zaf_fromcache "$1"; then if ! zaf_fromcache "$1"; then
zaf_zbxapi_do "$1" >$tmpfile 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 rm -f $tmpfile
fi fi
} }

7
zaf
View File

@ -69,6 +69,9 @@ case $1 in
cache-clean) cache-clean)
zaf_cache_clean zaf_cache_clean
;; ;;
cache-list)
zaf_cache_list
;;
version) version)
echo "$ZAF_VERSION<git $ZAF_GITBRANCH>" echo "$ZAF_VERSION<git $ZAF_GITBRANCH>"
@ -95,9 +98,6 @@ agent-config)
| zaf_far '{PLUGINDIR}' "${plugindir}" >${ZAF_AGENT_CONFIGD}/zaf_${plugin}.conf | zaf_far '{PLUGINDIR}' "${plugindir}" >${ZAF_AGENT_CONFIGD}/zaf_${plugin}.conf
done done
;; ;;
check-agent-config)
zaf_check_agent_config
;;
###### Plugins related commands ###### Plugins related commands
update) update)
@ -443,6 +443,7 @@ api)
zaf_hlp "$0 self-upgrade" "To self-upgrade zaf" zaf_hlp "$0 self-upgrade" "To self-upgrade zaf"
zaf_hlp "$0 self-remove" "To self-remove zaf and its config" 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-clean" "To remove all entries from cache"
zaf_hlp "$0 cache-list" "To show all entries in cache"
echo echo
[ -n "$1" ] && zaf_err "Bad command '$1'!" [ -n "$1" ] && zaf_err "Bad command '$1'!"
;; ;;