From ddbdff55d91a104bad8e61b8fc7441fb488653d7 Mon Sep 17 00:00:00 2001 From: Lukas Macura Date: Tue, 19 Apr 2016 10:37:39 +0200 Subject: [PATCH] Repaired userparms creation and including libraries --- install.sh | 4 +++- lib/ctrl.lib.sh | 22 ++++++++++++++-------- lib/preload.sh | 15 +++------------ lib/zaf.lib.sh | 10 ++++++++-- lib/zafcache | 17 +++++++++++++++++ lib/zaflock | 7 ++----- zaf | 2 +- 7 files changed, 48 insertions(+), 29 deletions(-) create mode 100644 lib/zafcache diff --git a/install.sh b/install.sh index 506bad9..baea979 100755 --- a/install.sh +++ b/install.sh @@ -281,7 +281,7 @@ zaf_install_all() { for i in lib/zaf.lib.sh lib/os.lib.sh lib/ctrl.lib.sh lib/cache.lib.sh lib/zbxapi.lib.sh README.md; do zaf_install $i ${ZAF_LIB_DIR}/ || zaf_err "Error installing $i" done - for i in lib/zaflock lib/preload.sh; do + for i in lib/zaflock lib/zafcache lib/preload.sh; do zaf_install_bin $i ${ZAF_LIB_DIR}/ || zaf_err "Error installing $i" done zaf_install_dir ${ZAF_BIN_DIR} @@ -295,8 +295,10 @@ zaf_install_all() { zaf_postconfigure() { if zaf_is_root; then + ${INSTALL_PREFIX}/${ZAF_BIN_DIR}/zaf cache-clean [ "${ZAF_GIT}" = 1 ] && ${INSTALL_PREFIX}/${ZAF_BIN_DIR}/zaf update ${INSTALL_PREFIX}/${ZAF_BIN_DIR}/zaf reinstall zaf || zaf_err "Error installing zaf plugin." + ${INSTALL_PREFIX}/${ZAF_BIN_DIR}/zaf agent-config || zaf_err "Error configuring agent." if zaf_is_root && ! zaf_test_item zaf.framework_version; then echo "Something is wrong with zabbix agent config." echo "Ensure that zabbix_agentd reads ${ZAF_AGENT_CONFIG}" diff --git a/lib/ctrl.lib.sh b/lib/ctrl.lib.sh index 8386ba6..4dc4a7c 100644 --- a/lib/ctrl.lib.sh +++ b/lib/ctrl.lib.sh @@ -8,7 +8,7 @@ zaf_ctrl_get_items() { # Get item body from stdin # $1 itemname zaf_ctrl_get_item_block() { - grep -v '^#' | awk '/^Item '$1'/ { i=0; + grep -vE '^#[a-zA-Z ]' | awk '/^Item '$1'/ { i=0; while (i==0) { getline; if (/^\/Item/) exit; @@ -22,7 +22,7 @@ zaf_ctrl_get_item_block() { # Get global plugin block body from stdin # $1 itemname zaf_ctrl_get_global_block() { - grep -v '^#' | awk '{ i=0; print $0; + grep -vE '^#[a-zA-Z ]' | awk '{ i=0; print $0; while (i==0) { getline; if (/^Item /) exit; @@ -33,7 +33,7 @@ zaf_ctrl_get_global_block() { # Get item multiline option # $1 optionname zaf_block_get_moption() { - awk '/^'$1'::$/ { i=0; print $0; + awk '/^'$1'::$/ { i=0; while (i==0) { getline; if (/^::$/) {i=1; continue;}; @@ -181,6 +181,7 @@ zaf_ctrl_install() { # Generates zabbix cfg from control file # $1 control # $2 pluginname +# $3 if nonempty, only return cfg, do not generate scripts zaf_ctrl_generate_cfg() { local items local cmd @@ -211,20 +212,25 @@ zaf_ctrl_generate_cfg() { fi cache=$(zaf_ctrl_get_item_option $1 $i "Cache") if [ -n "$cache" ]; then - cache="_cache '$cache' " + cache="${ZAF_LIB_DIR}/zafcache '$cache' " fi cmd=$(zaf_ctrl_get_item_option $1 $i "Cmd") + if [ -z "$cache" ] && [ -z "$lock" ]; then + preload="${ZAF_LIB_DIR}/preload.sh " + else + preload="" + fi if [ -n "$cmd" ]; then - $(which echo) "UserParameter=$ikey,${ZAF_LIB_DIR}/preload.sh $cache $lock$cmd"; + printf "%s" "UserParameter=$ikey,${preload}${cache}${lock}${cmd}"; echo continue fi cmd=$(zaf_ctrl_get_item_option $1 $i "Script") if [ -n "$cmd" ]; then zaf_ctrl_get_item_option $1 $i "Script" | \ - zaf_far '{INCLUDES}' '. /etc/zaf.conf; . ${ZAF_LIB_DIR}/zaf.lib.sh; . ${ZAF_LIB_DIR}/ctrl.lib.sh; . ${ZAF_LIB_DIR}/zbxapi.lib.sh; . ${ZAF_LIB_DIR}/cache.lib.sh; ' \ + zaf_far '{INCLUDES}' ". $ZAF_LIB_DIR/preload.sh; " \ >${ZAF_TMP_DIR}/${iscript}.sh; - zaf_install_bin ${ZAF_TMP_DIR}/${iscript}.sh ${ZAF_PLUGINS_DIR}/$2/ - $(which echo) "UserParameter=$ikey,$cache $lock${ZAF_PLUGINS_DIR}/$2/${iscript}.sh $args"; + [ -z "$3" ] && zaf_install_bin ${ZAF_TMP_DIR}/${iscript}.sh ${ZAF_PLUGINS_DIR}/$2/ + printf "%s" "UserParameter=$ikey,${preload}${cache}${lock}${ZAF_PLUGINS_DIR}/$2/${iscript}.sh $args"; echo continue; fi zaf_err "Item $i declared in control file but has no Cmd, Function or Script!" diff --git a/lib/preload.sh b/lib/preload.sh index 46a8881..ec182cf 100644 --- a/lib/preload.sh +++ b/lib/preload.sh @@ -19,19 +19,10 @@ trap "rm -rif ${ZAF_TMP_DIR}" EXIT export ZAF_LIB_DIR export ZAF_TMP_DIR +export ZAF_CACHE_DIR export ZAF_PLUGINS_DIR -if [ "$1" = "_cache" ] || [ "$1" = "_nocache" ] ; then - [ "$1" = "_nocache" ] && export ZAF_NOCACHE=1 - shift - seconds=$1 - shift - parms=$(echo $*|tr -d ' ') - if ! zaf_fromcache $parms; then - ([ "$(basename $0)" = "preload.sh" ] && [ -n "$*" ] && $@ ) | zaf_tocache_stdin $parms $seconds - fi -else - [ "$(basename $0)" = "preload.sh" ] && [ -n "$*" ] && $@ -fi +[ "$(basename $0)" = "preload.sh" ] && [ -n "$*" ] && $@ + diff --git a/lib/zaf.lib.sh b/lib/zaf.lib.sh index e592495..b2244fe 100644 --- a/lib/zaf.lib.sh +++ b/lib/zaf.lib.sh @@ -412,7 +412,8 @@ zaf_list_items() { zaf_get_item() { if which zabbix_get >/dev/null; then - zabbix_get -s localhost -k "$1" || zaf_wrn "Cannot reach agent on localhost. Please localhost to Server list." + zaf_dbg zabbix_get -s localhost -k "'$1'" + (zabbix_get -s localhost -k "$1" | tr '\n' ' '; echo) || zaf_wrn "Cannot reach agent on localhost. Please localhost to Server list." return 11 else zaf_wrn "Please install zabbix_get binary to check items over network." @@ -421,7 +422,12 @@ zaf_get_item() { } zaf_test_item() { - $ZAF_AGENT_BIN -t "$1" + if $ZAF_AGENT_BIN -t "$1" | grep ZBX_NOTSUPPORTED; then + return 1 + else + $ZAF_AGENT_BIN -t "$1" | tr '\n' ' ' + echo + fi } zaf_precache_item() { diff --git a/lib/zafcache b/lib/zafcache new file mode 100644 index 0000000..0a98c2d --- /dev/null +++ b/lib/zafcache @@ -0,0 +1,17 @@ +#!/bin/sh + +. $(dirname $0)/preload.sh + +help() { + zaf_err "$0 seconds cmd..." +} + +seconds=$1 +shift +[ -z "$*" ] && help + +parms=$(echo $*|tr -d ' ') +if ! zaf_fromcache "$parms"; then + $@ | zaf_tocache_stdin "$parms" "$seconds" +fi + diff --git a/lib/zaflock b/lib/zaflock index 5c004b8..a983f94 100644 --- a/lib/zaflock +++ b/lib/zaflock @@ -1,12 +1,9 @@ #!/bin/sh -. /etc/zaf.conf - -. ${ZAF_LIB_DIR}/zaf.lib.sh +. $(dirname $0)/preload.sh help() { - echo "$0 key cmd" - exit 2 + zaf_err "$0 key cmd" } lkey="$1" diff --git a/zaf b/zaf index d02bbde..05ba1bf 100755 --- a/zaf +++ b/zaf @@ -77,7 +77,7 @@ userparms) for plugin in $(zaf_list_plugins); do plugindir=$ZAF_PLUGINS_DIR/$plugin control=$plugindir/control.zaf - zaf_ctrl_generate_cfg "${control}" "${plugin}" \ + zaf_ctrl_generate_cfg "${control}" "${plugin}" "noscripts" \ | zaf_far '{PLUGINDIR}' "${plugindir}" done ;;