Repaired caching

Added testparameters and precacheparameters
1.1
Lukas Macura 2016-04-13 16:04:00 +02:00
parent ce4df86f46
commit 2a0d6848a7
4 changed files with 61 additions and 21 deletions

View File

@ -20,13 +20,14 @@ zaf_cache_key(){
# $2 value
# $3 lifetime in seconds
zaf_tocache(){
! [ -w $ZAF_CACHE_DIR ] && return 1
local key
local value
local lifetime
key=$(zaf_cache_key "$1")
echo "$2" >$ZAF_CACHE_DIR/$key
echo "$1" >$ZAF_CACHE_DIR/$key.key
touch -m -d "$3 seconds" $ZAF_CACHE_DIR/$key.tme
echo "$1" >$ZAF_CACHE_DIR/$key.info
touch -m -d "$3 seconds" $ZAF_CACHE_DIR/$key.info
zaf_trc "Cache: Saving entry $1($key)"
}
@ -34,14 +35,15 @@ zaf_tocache(){
# $1 key
# $2 lifetime in seconds
zaf_tocache_stdin(){
! [ -w $ZAF_CACHE_DIR ] && return 1
local key
local lifetime
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.key
touch -m -d "$3 seconds" $ZAF_CACHE_DIR/$key.tme
echo "$1" >$ZAF_CACHE_DIR/$key.info
touch -m -d "$2 seconds" $ZAF_CACHE_DIR/$key.info
cat $ZAF_CACHE_DIR/$key
else
rm $ZAF_CACHE_DIR/$key
@ -51,6 +53,7 @@ zaf_tocache_stdin(){
# Remove entry from cache
# $1 key
zaf_cache_delentry(){
! [ -w $ZAF_CACHE_DIR ] && return 1
local key
key=$(zaf_cache_key "$1")
zaf_trc "Cache: removing $1($key) from cache"
@ -60,11 +63,12 @@ zaf_cache_delentry(){
# Get object from cache
# $1 key
zaf_fromcache(){
! [ -r $ZAF_CACHE_DIR ] || [ -n "$ZAF_NOCACHE" ] && return 1
local key
local value
key=$(zaf_cache_key "$1")
if [ -f $ZAF_CACHE_DIR/$key ]; then
if [ "$ZAF_CACHE_DIR/$key.tme" -nt "$ZAF_CACHE_DIR/$key" ]; then
if [ "$ZAF_CACHE_DIR/$key.info" -nt "$ZAF_CACHE_DIR/$key" ]; then
zaf_trc "Cache: serving $1($key) from cache"
cat $ZAF_CACHE_DIR/$key
else

View File

@ -21,7 +21,8 @@ export ZAF_LIB_DIR
export ZAF_TMP_DIR
export ZAF_PLUGINS_DIR
if [ "$1" = "_cache" ]; then
if [ "$1" = "_cache" ] || [ "$1" = "_nocache" ] ; then
[ "$1" = "_nocache" ] && export ZAF_NOCACHE=1
shift
seconds=$1
shift

View File

@ -231,8 +231,14 @@ zaf_plugin_info() {
[ -n "$pmaintainer" ] && echo "Maintainer: $pmaintainer"
[ -n "$purl" ] && echo "Url: $purl"
[ -n "$phome" ] && echo "Home: $phome"
echo
echo "Items: $pitems"
echo
if zaf_is_plugin "$(basename $plugin)"; then
echo -n "Defined items: "; zaf_list_plugin_items $plugin
echo -n "Test items: "; zaf_list_plugin_items $plugin test
echo -n "Precache items: "; zaf_list_plugin_items $plugin precache
else
echo "Items: $pitems"
fi
echo
}
@ -338,11 +344,15 @@ zaf_plugin_template_url() {
echo $(zaf_plugin_option "$1" Url)/template.xml
}
# $1 plugin
# $2 test to get test items, precache to get items to precache
zaf_list_plugin_items() {
local items
local i
local p
local key
local testparms
local precache
if ! zaf_is_plugin "$1"; then
zaf_err "Missing plugin name or plugin $1 unknown. ";
@ -352,12 +362,25 @@ zaf_list_plugin_items() {
items=$(zaf_ctrl_get_items <$cfile)
for i in $items; do
p=$(zaf_ctrl_get_item_option $cfile $i "Parameters")
testparms=$(zaf_ctrl_get_item_option $cfile $i "Testparameters")
precache=$(zaf_ctrl_get_item_option $cfile $i "Precache")
if [ -n "$p" ]; then
key="$1.$i[]"
if [ -n "$testparms" ] && [ "$2" = "test" ]; then
for tp in $testparms; do
echo -n "$1.$i[$tp] "
done
else
if [ -n "$precache" ] && [ "$2" = "precache" ]; then
for tp in $precache; do
echo -n "$1.$i[$tp] "
done
fi
[ "$2" != "test" ] && key="$1.$i[]"
fi
else
key="$1.$i"
fi
echo -n "$key "
[ "$2" != "precache" ] && echo -n "$key "
done
echo
}
@ -379,12 +402,12 @@ zaf_get_item() {
}
zaf_test_item() {
[ "$USER" != "zabbix" ] && zaf_wrn "You are not zabbix user. Test will be run with your privileges and sudo access!"
$ZAF_AGENT_BIN -t "$1"
}
zaf_precache_item() {
cmd=$(grep "^UserParameter=$item" $ZAF_AGENT_CONFIGD/zaf*.conf | cut -d ',' -f 2-)
cmd=$(grep "^UserParameter=$item" $ZAF_AGENT_CONFIGD/zaf*.conf | cut -d ',' -f 2- | sed -e "s/_cache/_nocache/")
zaf_wrn "Precaching item $item[$(echo $*| tr ' ' ',')] ($cmd)"
eval $cmd
}

26
zaf
View File

@ -144,6 +144,7 @@ items)
;;
test)
[ "$USER" != "zabbix" ] && zaf_wrn "You are not zabbix user. Test will be run with your privileges and sudo access!"
shift
shift $(zaf_shift "$@")
if echo $1|grep -q '\.'; then
@ -157,8 +158,9 @@ test)
fi
for p in $plugins; do
! zaf_is_plugin $p && zaf_err "Unknown plugin $p"
for i in $(zaf_list_plugin_items $p); do
for i in $(zaf_list_plugin_items $p test); do
echo $i: $(zaf_test_item $i)
echo
done
done
;;
@ -175,8 +177,9 @@ get)
plugins="$(zaf_list_plugins)"
fi
for p in $plugins; do
for i in $(zaf_list_plugin_items $p); do
for i in $(zaf_list_plugin_items $p test); do
echo $i: $(zaf_get_item $i)
echo
done
done
;;
@ -185,9 +188,17 @@ 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
if zaf_is_plugin $i; then
for j in $(zaf_list_plugin_items $i precache); do
item=$(echo $j | cut -d '[' -f 1)
params=$(echo $j | cut -d '[' -f 2 | cut -d ']' -f 1 | tr ',' ' ')
zaf_precache_item $params >/dev/null
done
else
item=$(echo $i | cut -d '[' -f 1)
params=$(echo $i | cut -d '[' -f 2 | cut -d ']' -f 1 | tr ',' ' ')
zaf_precache_item $params >/dev/null
fi
done
;;
@ -196,8 +207,8 @@ install)
shift $(zaf_shift "$@")
[ -z "$1" ] && echo "$0 install plugin [plugin]..."
for p in $@; do
if zaf_is_plugin "$p"; then
zaf_wrn "Plugin $p already installed. Skipping installation."
if zaf_is_plugin "$(basename $p)"; then
zaf_wrn "Plugin $(basename $p) already installed. Skipping installation."
continue
fi
zaf_install_plugin "$p"
@ -334,6 +345,7 @@ api)
echo "$0 items [plugin] To list all suported items [for plugin]"
echo "$0 test [plugin[.item]] To test [all] suported items by zabbix_agentd [for plugin]"
echo "$0 get [plugin[.item]] To test [all] suported items by zabbix_get [for plugin]"
echo "$0 precache [plugin[.item]] To precache [all] suported items"
echo "$0 install plugin To install plugin"
echo "$0 remove plugin To remove plugin"
echo "$0 api To zabbix API functions. See $0 api for more info."