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
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(){

View File

@ -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() {

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 !";
}
# 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() {

View File

@ -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
}

7
zaf
View File

@ -69,6 +69,9 @@ case $1 in
cache-clean)
zaf_cache_clean
;;
cache-list)
zaf_cache_list
;;
version)
echo "$ZAF_VERSION<git $ZAF_GITBRANCH>"
@ -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'!"
;;