Make scripts nicer by unexpand

master
Lukas Macura 2016-12-07 14:25:34 +01:00
parent cfa056065b
commit 2ffc186d68
8 changed files with 523 additions and 523 deletions

View File

@ -16,7 +16,7 @@ fi
# Lite version of zaf_fetch_url, full version will be loaded later # Lite version of zaf_fetch_url, full version will be loaded later
zaf_fetch_url(){ zaf_fetch_url(){
if zaf_which curl >/dev/null 2>/dev/null; then if zaf_which curl >/dev/null 2>/dev/null; then
echo curl -f -k -s -L -o - "$1" >&2; echo curl -f -k -s -L -o - "$1" >&2;
curl -f -k -s -L -o - "$1" curl -f -k -s -L -o - "$1"
else else
wget --no-check-certificate -O - "$1" wget --no-check-certificate -O - "$1"
@ -38,8 +38,8 @@ zaf_which() {
# Lite version of zaf_err, full version will be loaded later # Lite version of zaf_err, full version will be loaded later
zaf_err() { zaf_err() {
logger ${ZAF_LOG_STDERR} -p user.err -t zaf-error -- $@ logger ${ZAF_LOG_STDERR} -p user.err -t zaf-error -- $@
logger ${ZAF_LOG_STDERR} -p user.err -t zaf-error "Exiting with error!" logger ${ZAF_LOG_STDERR} -p user.err -t zaf-error "Exiting with error!"
exit 1 exit 1
} }
# Download tgz and extract to tmpdir # Download tgz and extract to tmpdir
@ -59,12 +59,12 @@ zaf_get_option(){
local opt local opt
ZAF_HELP_OPTS="$ZAF_HELP_OPTS\n$1 $2 [$3]" ZAF_HELP_OPTS="$ZAF_HELP_OPTS\n$1 $2 [$3]"
eval opt=\$C_$1 eval opt=\$C_$1
if [ -n "$opt" ]; then if [ -n "$opt" ]; then
eval "$1='$opt'" eval "$1='$opt'"
zaf_dbg "Got '$2' <$1> from CLI: $opt" zaf_dbg "Got '$2' <$1> from CLI: $opt"
return return
fi fi
eval opt=\$$1 eval opt=\$$1
if [ -n "$opt" ] && ! [ "$4" = "user" ]; then if [ -n "$opt" ] && ! [ "$4" = "user" ]; then
eval "$1='$opt'" eval "$1='$opt'"
@ -180,7 +180,7 @@ zaf_configure_agent() {
local options local options
local changes local changes
zaf_install_dir "$ZAF_AGENT_CONFIGD" zaf_install_dir "$ZAF_AGENT_CONFIGD"
echo -n >"$ZAF_AGENT_CONFIGD/zaf_options.conf" || zaf_err "Cannot access $ZAF_AGENT_CONFIGD/zaf_options.conf" echo -n >"$ZAF_AGENT_CONFIGD/zaf_options.conf" || zaf_err "Cannot access $ZAF_AGENT_CONFIGD/zaf_options.conf"
! [ -f "$ZAF_AGENT_CONFIG" ] && zaf_install "$ZAF_AGENT_CONFIG" ! [ -f "$ZAF_AGENT_CONFIG" ] && zaf_install "$ZAF_AGENT_CONFIG"
for pair in "$@"; do for pair in "$@"; do
@ -208,7 +208,7 @@ zaf_configure_server() {
local options local options
local changes local changes
zaf_install_dir "$ZAF_SERVER_CONFIGD" zaf_install_dir "$ZAF_SERVER_CONFIGD"
echo -n >"$ZAF_SERVER_CONFIGD/zaf_options.conf" || zaf_err "Cannot access $ZAF_SERVER_CONFIGD/zaf_options.conf" echo -n >"$ZAF_SERVER_CONFIGD/zaf_options.conf" || zaf_err "Cannot access $ZAF_SERVER_CONFIGD/zaf_options.conf"
for pair in "$@"; do for pair in "$@"; do
echo $pair | grep -q '^S\_' || continue # Skip non S_ vars echo $pair | grep -q '^S\_' || continue # Skip non S_ vars
@ -229,12 +229,12 @@ zaf_configure_server() {
zaf_preconfigure(){ zaf_preconfigure(){
[ -z "$ZAF_OS" ] && zaf_detect_system [ -z "$ZAF_OS" ] && zaf_detect_system
zaf_os_specific zaf_preconfigure_os zaf_os_specific zaf_preconfigure_os
if ! zaf_is_root; then if ! zaf_is_root; then
[ -z "$INSTALL_PREFIX" ] && zaf_err "We are not root. Use INSTALL_PREFIX or become root." [ -z "$INSTALL_PREFIX" ] && zaf_err "We are not root. Use INSTALL_PREFIX or become root."
else else
[ "$1" != "reconf" ] && zaf_os_specific zaf_check_deps zaf && zaf_err "Zaf is installed as system package. Cannot install." [ "$1" != "reconf" ] && zaf_os_specific zaf_check_deps zaf && zaf_err "Zaf is installed as system package. Cannot install."
fi fi
} }
zaf_configure(){ zaf_configure(){
@ -263,7 +263,7 @@ zaf_configure(){
zaf_get_option ZAF_TMP_DIR "Tmp directory" "/tmp/" "$INSTALL_MODE" zaf_get_option ZAF_TMP_DIR "Tmp directory" "/tmp/" "$INSTALL_MODE"
zaf_get_option ZAF_CACHE_DIR "Cache directory" "/tmp/zafc" "$INSTALL_MODE" zaf_get_option ZAF_CACHE_DIR "Cache directory" "/tmp/zafc" "$INSTALL_MODE"
zaf_get_option ZAF_LIB_DIR "Libraries directory" "/usr/lib/zaf" "$INSTALL_MODE" zaf_get_option ZAF_LIB_DIR "Libraries directory" "/usr/lib/zaf" "$INSTALL_MODE"
zaf_get_option ZAF_BIN_DIR "Directory to put binaries" "/usr/bin" "$INSTALL_MODE" zaf_get_option ZAF_BIN_DIR "Directory to put binaries" "/usr/bin" "$INSTALL_MODE"
zaf_get_option ZAF_PLUGINS_DIR "Plugins directory" "${ZAF_LIB_DIR}/plugins" "$INSTALL_MODE" zaf_get_option ZAF_PLUGINS_DIR "Plugins directory" "${ZAF_LIB_DIR}/plugins" "$INSTALL_MODE"
[ "${ZAF_GIT}" = 1 ] && zaf_get_option ZAF_REPO_GITURL "Git plugins repository" "https://github.com/limosek/zaf-plugins.git" "$INSTALL_MODE" [ "${ZAF_GIT}" = 1 ] && zaf_get_option ZAF_REPO_GITURL "Git plugins repository" "https://github.com/limosek/zaf-plugins.git" "$INSTALL_MODE"
zaf_get_option ZAF_PROXY "http proxy used by zaf" "" "$INSTALL_MODE" zaf_get_option ZAF_PROXY "http proxy used by zaf" "" "$INSTALL_MODE"
@ -293,7 +293,7 @@ zaf_configure(){
zaf_err "Zabbix agent ($ZAF_AGENT_BIN) not installed? Use ZAF_AGENT_BIN env variable to specify location. Exiting." zaf_err "Zabbix agent ($ZAF_AGENT_BIN) not installed? Use ZAF_AGENT_BIN env variable to specify location. Exiting."
fi fi
[ -n "$INSTALL_PREFIX" ] && zaf_install_dir "/etc" [ -n "$INSTALL_PREFIX" ] && zaf_install_dir "/etc"
if ! [ -f "${ZAF_CFG_FILE}" ]; then if ! [ -f "${ZAF_CFG_FILE}" ]; then
touch "${ZAF_CFG_FILE}" || zaf_err "No permissions to ${ZAF_CFG_FILE}" touch "${ZAF_CFG_FILE}" || zaf_err "No permissions to ${ZAF_CFG_FILE}"
fi fi
@ -307,7 +307,7 @@ zaf_configure(){
zaf_set_option ZAF_TMP_DIR "$ZAF_TMP_DIR" zaf_set_option ZAF_TMP_DIR "$ZAF_TMP_DIR"
zaf_set_option ZAF_CACHE_DIR "$ZAF_CACHE_DIR" zaf_set_option ZAF_CACHE_DIR "$ZAF_CACHE_DIR"
zaf_set_option ZAF_LIB_DIR "$ZAF_LIB_DIR" zaf_set_option ZAF_LIB_DIR "$ZAF_LIB_DIR"
zaf_set_option ZAF_BIN_DIR "$ZAF_BIN_DIR" zaf_set_option ZAF_BIN_DIR "$ZAF_BIN_DIR"
zaf_set_option ZAF_PLUGINS_DIR "$ZAF_PLUGINS_DIR" zaf_set_option ZAF_PLUGINS_DIR "$ZAF_PLUGINS_DIR"
zaf_set_option ZAF_REPO_URL "$ZAF_REPO_URL" zaf_set_option ZAF_REPO_URL "$ZAF_REPO_URL"
zaf_set_option ZAF_PROXY "$ZAF_PROXY" zaf_set_option ZAF_PROXY "$ZAF_PROXY"
@ -344,7 +344,7 @@ zaf_configure(){
fi fi
if zaf_is_root; then if zaf_is_root; then
zaf_configure_agent $ZAF_AGENT_OPTIONS "$@" zaf_configure_agent $ZAF_AGENT_OPTIONS "$@"
zaf_add_zabbix_option "$ZAF_AGENT_CONFIG" "Include" "$ZAF_AGENT_CONFIGD" zaf_add_zabbix_option "$ZAF_AGENT_CONFIG" "Include" "$ZAF_AGENT_CONFIGD"
if [ -f "$ZAF_SERVER_BIN" ]; then if [ -f "$ZAF_SERVER_BIN" ]; then
zaf_configure_server $ZAF_SERVER_OPTIONS "$@" && zaf_add_zabbix_option "$ZAF_SERVER_CONFIG" "Include" "$ZAF_SERVER_CONFIGD" zaf_configure_server $ZAF_SERVER_OPTIONS "$@" && zaf_add_zabbix_option "$ZAF_SERVER_CONFIG" "Include" "$ZAF_SERVER_CONFIGD"
@ -379,26 +379,26 @@ zaf_install_all() {
done done
zaf_install_dir ${ZAF_PLUGINS_DIR} zaf_install_dir ${ZAF_PLUGINS_DIR}
zaf_install_dir ${ZAF_PLUGINS_DIR} zaf_install_dir ${ZAF_PLUGINS_DIR}
zaf_install_dir ${ZAF_BIN_DIR} zaf_install_dir ${ZAF_BIN_DIR}
} }
zaf_postconfigure() { zaf_postconfigure() {
if zaf_is_root; then if zaf_is_root; then
${INSTALL_PREFIX}/${ZAF_BIN_DIR}/zaf cache-clean ${INSTALL_PREFIX}/${ZAF_BIN_DIR}/zaf cache-clean
[ "${ZAF_GIT}" = 1 ] && ${INSTALL_PREFIX}/${ZAF_BIN_DIR}/zaf update [ "${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 reinstall zaf || zaf_err "Error installing zaf plugin."
${INSTALL_PREFIX}/${ZAF_BIN_DIR}/zaf agent-config || zaf_err "Error configuring agent." ${INSTALL_PREFIX}/${ZAF_BIN_DIR}/zaf agent-config || zaf_err "Error configuring agent."
zaf_os_specific zaf_postconfigure_os zaf_os_specific zaf_postconfigure_os
if zaf_is_root && ! zaf_test_item zaf.framework_version; then if zaf_is_root && ! zaf_test_item zaf.framework_version; then
echo "Something is wrong with zabbix agent config." echo "Something is wrong with zabbix agent config."
echo "Ensure that zabbix_agentd reads ${ZAF_AGENT_CONFIG}" echo "Ensure that zabbix_agentd reads ${ZAF_AGENT_CONFIG}"
echo "and there is Include=${ZAF_AGENT_CONFIGD} directive inside." echo "and there is Include=${ZAF_AGENT_CONFIGD} directive inside."
echo "Does ${ZAF_AGENT_RESTART} work?" echo "Does ${ZAF_AGENT_RESTART} work?"
exit 1 exit 1
fi fi
else else
[ "${ZAF_GIT}" = 1 ] && [ -n "${INSTALL_PREFIX}" ] && git clone "${ZAF_REPO_GITURL}" "${INSTALL_PREFIX}/${ZAF_REPO_DIR}" [ "${ZAF_GIT}" = 1 ] && [ -n "${INSTALL_PREFIX}" ] && git clone "${ZAF_REPO_GITURL}" "${INSTALL_PREFIX}/${ZAF_REPO_DIR}"
fi fi
zaf_wrn "Install done. Use 'zaf' to get started." zaf_wrn "Install done. Use 'zaf' to get started."
true true
} }
@ -416,7 +416,7 @@ if ! [ -f README.md ]; then
export https_proxy="$ZAF_PROXY" export https_proxy="$ZAF_PROXY"
fi fi
mkdir -p $ZAF_TMP_DIR mkdir -p $ZAF_TMP_DIR
if ! zaf_which curl >/dev/null && ! zaf_which wget >/dev/null; if ! zaf_which curl >/dev/null && ! zaf_which wget >/dev/null;
then then
zaf_err "Curl or wget not found. Cannot continue. Please install it." zaf_err "Curl or wget not found. Cannot continue. Please install it."
fi fi
@ -457,11 +457,11 @@ zaf_tmp_init
# Read options as config for ZAF # Read options as config for ZAF
for pair in "$@"; do for pair in "$@"; do
echo $pair | grep -q '^ZAF\_' || continue echo $pair | grep -q '^ZAF\_' || continue
option=$(echo $pair|cut -d '=' -f 1) option=$(echo $pair|cut -d '=' -f 1)
value=$(echo $pair|cut -d '=' -f 2-) value=$(echo $pair|cut -d '=' -f 2-)
eval "C_${option}='$value'" eval "C_${option}='$value'"
zaf_wrn "Overriding $option from cmdline." zaf_wrn "Overriding $option from cmdline."
done done
[ -z "$C_ZAF_TMP_DIR" ] && C_ZAF_TMP_DIR="/tmp/" [ -z "$C_ZAF_TMP_DIR" ] && C_ZAF_TMP_DIR="/tmp/"
if [ -n "$ZAF_PROXY" ]; then if [ -n "$ZAF_PROXY" ]; then
@ -470,7 +470,7 @@ fi
case $1 in case $1 in
interactive) interactive)
shift shift
INSTALL_MODE=interactive INSTALL_MODE=interactive
zaf_preconfigure zaf_preconfigure
zaf_configure "$@" zaf_configure "$@"
@ -478,52 +478,52 @@ interactive)
zaf_postconfigure zaf_postconfigure
;; ;;
auto) auto)
shift shift
INSTALL_MODE=auto INSTALL_MODE=auto
zaf_preconfigure zaf_preconfigure
zaf_configure "$@" zaf_configure "$@"
zaf_install_all zaf_install_all
zaf_postconfigure zaf_postconfigure
;; ;;
debug-auto) debug-auto)
shift; shift;
ZAF_DEBUG=4 ZAF_DEBUG=4
INSTALL_MODE=auto INSTALL_MODE=auto
zaf_preconfigure zaf_preconfigure
zaf_configure "$@" zaf_configure "$@"
zaf_install_all zaf_install_all
zaf_postconfigure zaf_postconfigure
;; ;;
debug-interactive) debug-interactive)
shift; shift;
ZAF_DEBUG=4 ZAF_DEBUG=4
INSTALL_MODE=interactive INSTALL_MODE=interactive
zaf_preconfigure zaf_preconfigure
zaf_configure "$@" zaf_configure "$@"
zaf_install_all zaf_install_all
zaf_postconfigure zaf_postconfigure
;; ;;
debug) debug)
shift; shift;
ZAF_DEBUG=4 ZAF_DEBUG=4
INSTALL_MODE=auto INSTALL_MODE=auto
zaf_preconfigure zaf_preconfigure
zaf_configure "$@" zaf_configure "$@"
zaf_install_all zaf_install_all
zaf_postconfigure zaf_postconfigure
;; ;;
reconf) reconf)
shift; shift;
rm -f $ZAF_CFG_FILE rm -f $ZAF_CFG_FILE
INSTALL_MODE=auto INSTALL_MODE=auto
zaf_preconfigure reconf zaf_preconfigure reconf
zaf_configure "$@" zaf_configure "$@"
zaf_postconfigure zaf_postconfigure
;; ;;
install) install)
INSTALL_MODE=auto INSTALL_MODE=auto
zaf_preconfigure nor zaf_preconfigure nor
zaf_configure "$@" zaf_configure "$@"
zaf_install_all zaf_install_all
zaf_postconfigure zaf_postconfigure
;; ;;
@ -531,12 +531,12 @@ install)
echo echo
echo "Please specify how to install." echo "Please specify how to install."
echo "install.sh {auto|interactive|debug-auto|debug-interactive|reconf} [Agent-Options] [Zaf-Options]" echo "install.sh {auto|interactive|debug-auto|debug-interactive|reconf} [Agent-Options] [Zaf-Options]"
echo "scratch means that config file will be created from scratch" echo "scratch means that config file will be created from scratch"
echo " Agent-Options: Z_Option=value [...]" echo " Agent-Options: Z_Option=value [...]"
echo " Server-Options: S_Option=value [...]" echo " Server-Options: S_Option=value [...]"
echo " Zaf-Options: ZAF_OPT=value [...]" echo " Zaf-Options: ZAF_OPT=value [...]"
echo " To unset Agent-Option use Z_Option=''" echo " To unset Agent-Option use Z_Option=''"
echo echo
echo "Example 1 (default install): install.sh auto" echo "Example 1 (default install): install.sh auto"
echo 'Example 2 (preconfigure agent options): install.sh auto Z_Server=zabbix.server Z_ServerActive=zabbix.server Z_Hostname=$(hostname)' echo 'Example 2 (preconfigure agent options): install.sh auto Z_Server=zabbix.server Z_ServerActive=zabbix.server Z_Hostname=$(hostname)'
echo 'Example 3 (preconfigure server options): install.sh auto S_StartPollers=10 S_ListenPort=10051' echo 'Example 3 (preconfigure server options): install.sh auto S_StartPollers=10 S_ListenPort=10051'

View File

@ -1,8 +1,8 @@
#!/bin/sh #!/bin/sh
throw() { throw() {
echo "$*" >&2 echo "$*" >&2
exit 1 exit 1
} }
BRIEF=0 BRIEF=0
@ -12,197 +12,197 @@ NO_HEAD=0
NORMALIZE_SOLIDUS=0 NORMALIZE_SOLIDUS=0
usage() { usage() {
echo echo
echo "Usage: JSON.sh [-b] [-l] [-p] [-s] [-h]" echo "Usage: JSON.sh [-b] [-l] [-p] [-s] [-h]"
echo echo
echo "-p - Prune empty. Exclude fields with empty values." echo "-p - Prune empty. Exclude fields with empty values."
echo "-l - Leaf only. Only show leaf nodes, which stops data duplication." echo "-l - Leaf only. Only show leaf nodes, which stops data duplication."
echo "-b - Brief. Combines 'Leaf only' and 'Prune empty' options." echo "-b - Brief. Combines 'Leaf only' and 'Prune empty' options."
echo "-n - No-head. Do not show nodes that have no path (lines that start with [])." echo "-n - No-head. Do not show nodes that have no path (lines that start with [])."
echo "-s - Remove escaping of the solidus symbol (stright slash)." echo "-s - Remove escaping of the solidus symbol (stright slash)."
echo "-h - This help text." echo "-h - This help text."
echo echo
} }
parse_options() { parse_options() {
set -- "$@" set -- "$@"
local ARGN=$# local ARGN=$#
while [ "$ARGN" -ne 0 ] while [ "$ARGN" -ne 0 ]
do do
case $1 in case $1 in
-h) usage -h) usage
exit 0 exit 0
;; ;;
-b) BRIEF=1 -b) BRIEF=1
LEAFONLY=1 LEAFONLY=1
PRUNE=1 PRUNE=1
;; ;;
-l) LEAFONLY=1 -l) LEAFONLY=1
;; ;;
-p) PRUNE=1 -p) PRUNE=1
;; ;;
-n) NO_HEAD=1 -n) NO_HEAD=1
;; ;;
-s) NORMALIZE_SOLIDUS=1 -s) NORMALIZE_SOLIDUS=1
;; ;;
?*) echo "ERROR: Unknown option." ?*) echo "ERROR: Unknown option."
usage usage
exit 0 exit 0
;; ;;
esac esac
shift 1 shift 1
ARGN=$((ARGN-1)) ARGN=$((ARGN-1))
done done
} }
awk_egrep () { awk_egrep () {
local pattern_string=$1 local pattern_string=$1
gawk '{ gawk '{
while ($0) { while ($0) {
start=match($0, pattern); start=match($0, pattern);
token=substr($0, start, RLENGTH); token=substr($0, start, RLENGTH);
print token; print token;
$0=substr($0, start+RLENGTH); $0=substr($0, start+RLENGTH);
} }
}' pattern="$pattern_string" }' pattern="$pattern_string"
} }
tokenize () { tokenize () {
local GREP local GREP
local ESCAPE local ESCAPE
local CHAR local CHAR
if echo "test string" | egrep -ao --color=never "test" >/dev/null 2>&1 if echo "test string" | egrep -ao --color=never "test" >/dev/null 2>&1
then then
GREP='egrep -ao --color=never' GREP='egrep -ao --color=never'
else else
GREP='egrep -ao' GREP='egrep -ao'
fi fi
if echo "test string" | egrep -o "test" >/dev/null 2>&1 if echo "test string" | egrep -o "test" >/dev/null 2>&1
then then
ESCAPE='(\\[^u[:cntrl:]]|\\u[0-9a-fA-F]{4})' ESCAPE='(\\[^u[:cntrl:]]|\\u[0-9a-fA-F]{4})'
CHAR='[^[:cntrl:]"\\]' CHAR='[^[:cntrl:]"\\]'
else else
GREP=awk_egrep GREP=awk_egrep
ESCAPE='(\\\\[^u[:cntrl:]]|\\u[0-9a-fA-F]{4})' ESCAPE='(\\\\[^u[:cntrl:]]|\\u[0-9a-fA-F]{4})'
CHAR='[^[:cntrl:]"\\\\]' CHAR='[^[:cntrl:]"\\\\]'
fi fi
local STRING="\"$CHAR*($ESCAPE$CHAR*)*\"" local STRING="\"$CHAR*($ESCAPE$CHAR*)*\""
local NUMBER='-?(0|[1-9][0-9]*)([.][0-9]*)?([eE][+-]?[0-9]*)?' local NUMBER='-?(0|[1-9][0-9]*)([.][0-9]*)?([eE][+-]?[0-9]*)?'
local KEYWORD='null|false|true' local KEYWORD='null|false|true'
local SPACE='[[:space:]]+' local SPACE='[[:space:]]+'
# Force zsh to expand $A into multiple words # Force zsh to expand $A into multiple words
local is_wordsplit_disabled=$(unsetopt 2>/dev/null | grep -c '^shwordsplit$') local is_wordsplit_disabled=$(unsetopt 2>/dev/null | grep -c '^shwordsplit$')
if [ $is_wordsplit_disabled != 0 ]; then setopt shwordsplit; fi if [ $is_wordsplit_disabled != 0 ]; then setopt shwordsplit; fi
$GREP "$STRING|$NUMBER|$KEYWORD|$SPACE|." | egrep -v "^$SPACE$" $GREP "$STRING|$NUMBER|$KEYWORD|$SPACE|." | egrep -v "^$SPACE$"
if [ $is_wordsplit_disabled != 0 ]; then unsetopt shwordsplit; fi if [ $is_wordsplit_disabled != 0 ]; then unsetopt shwordsplit; fi
} }
parse_array () { parse_array () {
local index=0 local index=0
local ary='' local ary=''
read -r token read -r token
case "$token" in case "$token" in
']') ;; ']') ;;
*) *)
while : while :
do do
parse_value "$1" "$index" parse_value "$1" "$index"
index=$((index+1)) index=$((index+1))
ary="$ary""$value" ary="$ary""$value"
read -r token read -r token
case "$token" in case "$token" in
']') break ;; ']') break ;;
',') ary="$ary," ;; ',') ary="$ary," ;;
*) throw "EXPECTED , or ] GOT ${token:-EOF}" ;; *) throw "EXPECTED , or ] GOT ${token:-EOF}" ;;
esac esac
read -r token read -r token
done done
;; ;;
esac esac
[ "$BRIEF" -eq 0 ] && value=$(printf '[%s]' "$ary") || value= [ "$BRIEF" -eq 0 ] && value=$(printf '[%s]' "$ary") || value=
: :
} }
parse_object () { parse_object () {
local key local key
local obj='' local obj=''
read -r token read -r token
case "$token" in case "$token" in
'}') ;; '}') ;;
*) *)
while : while :
do do
case "$token" in case "$token" in
'"'*'"') key=$token ;; '"'*'"') key=$token ;;
*) throw "EXPECTED string GOT ${token:-EOF}" ;; *) throw "EXPECTED string GOT ${token:-EOF}" ;;
esac esac
read -r token read -r token
case "$token" in case "$token" in
':') ;; ':') ;;
*) throw "EXPECTED : GOT ${token:-EOF}" ;; *) throw "EXPECTED : GOT ${token:-EOF}" ;;
esac esac
read -r token read -r token
parse_value "$1" "$key" parse_value "$1" "$key"
obj="$obj$key:$value" obj="$obj$key:$value"
read -r token read -r token
case "$token" in case "$token" in
'}') break ;; '}') break ;;
',') obj="$obj," ;; ',') obj="$obj," ;;
*) throw "EXPECTED , or } GOT ${token:-EOF}" ;; *) throw "EXPECTED , or } GOT ${token:-EOF}" ;;
esac esac
read -r token read -r token
done done
;; ;;
esac esac
[ "$BRIEF" -eq 0 ] && value=$(printf '{%s}' "$obj") || value= [ "$BRIEF" -eq 0 ] && value=$(printf '{%s}' "$obj") || value=
: :
} }
parse_value () { parse_value () {
local jpath="${1:+$1,}$2" isleaf=0 isempty=0 print=0 local jpath="${1:+$1,}$2" isleaf=0 isempty=0 print=0
case "$token" in case "$token" in
'{') parse_object "$jpath" ;; '{') parse_object "$jpath" ;;
'[') parse_array "$jpath" ;; '[') parse_array "$jpath" ;;
# At this point, the only valid single-character tokens are digits. # At this point, the only valid single-character tokens are digits.
''|[!0-9]) throw "EXPECTED value GOT ${token:-EOF}" ;; ''|[!0-9]) throw "EXPECTED value GOT ${token:-EOF}" ;;
*) value=$token *) value=$token
# if asked, replace solidus ("\/") in json strings with normalized value: "/" # if asked, replace solidus ("\/") in json strings with normalized value: "/"
[ "$NORMALIZE_SOLIDUS" -eq 1 ] && value=$(echo "$value" | sed 's#\\/#/#g') [ "$NORMALIZE_SOLIDUS" -eq 1 ] && value=$(echo "$value" | sed 's#\\/#/#g')
isleaf=1 isleaf=1
[ "$value" = '""' ] && isempty=1 [ "$value" = '""' ] && isempty=1
;; ;;
esac esac
[ "$value" = '' ] && return [ "$value" = '' ] && return
[ "$NO_HEAD" -eq 1 ] && [ -z "$jpath" ] && return [ "$NO_HEAD" -eq 1 ] && [ -z "$jpath" ] && return
[ "$LEAFONLY" -eq 0 ] && [ "$PRUNE" -eq 0 ] && print=1 [ "$LEAFONLY" -eq 0 ] && [ "$PRUNE" -eq 0 ] && print=1
[ "$LEAFONLY" -eq 1 ] && [ "$isleaf" -eq 1 ] && [ $PRUNE -eq 0 ] && print=1 [ "$LEAFONLY" -eq 1 ] && [ "$isleaf" -eq 1 ] && [ $PRUNE -eq 0 ] && print=1
[ "$LEAFONLY" -eq 0 ] && [ "$PRUNE" -eq 1 ] && [ "$isempty" -eq 0 ] && print=1 [ "$LEAFONLY" -eq 0 ] && [ "$PRUNE" -eq 1 ] && [ "$isempty" -eq 0 ] && print=1
[ "$LEAFONLY" -eq 1 ] && [ "$isleaf" -eq 1 ] && \ [ "$LEAFONLY" -eq 1 ] && [ "$isleaf" -eq 1 ] && \
[ $PRUNE -eq 1 ] && [ $isempty -eq 0 ] && print=1 [ $PRUNE -eq 1 ] && [ $isempty -eq 0 ] && print=1
[ "$print" -eq 1 ] && printf "[%s]\t%s\n" "$jpath" "$value" [ "$print" -eq 1 ] && printf "[%s]\t%s\n" "$jpath" "$value"
: :
} }
parse () { parse () {
read -r token read -r token
parse_value parse_value
read -r token read -r token
case "$token" in case "$token" in
'') ;; '') ;;
*) throw "EXPECTED EOF GOT $token" ;; *) throw "EXPECTED EOF GOT $token" ;;
esac esac
} }
if ([ "$0" = "$BASH_SOURCE" ] || ! [ -n "$BASH_SOURCE" ]); if ([ "$0" = "$BASH_SOURCE" ] || ! [ -n "$BASH_SOURCE" ]);
then then
parse_options "$@" parse_options "$@"
tokenize | parse tokenize | parse
fi fi
# vi: expandtab sw=2 ts=2 # vi: expandtab sw=2 ts=2

View File

@ -162,7 +162,7 @@ zaf_ctrl_sudo() {
! [ -d "$ZAF_SUDOERSD" ] && { zaf_wrn "$ZAF_SUDOERSD nonexistent! Skipping sudo install!"; return 1; } ! [ -d "$ZAF_SUDOERSD" ] && { zaf_wrn "$ZAF_SUDOERSD nonexistent! Skipping sudo install!"; return 1; }
zaf_dbg "Installing sudoers entry $ZAF_SUDOERSD/zaf_$plugin" zaf_dbg "Installing sudoers entry $ZAF_SUDOERSD/zaf_$plugin"
[ -z "$sudo" ] && return # Nothing to install [ -z "$sudo" ] && return # Nothing to install
if ! zaf_which sudo >/dev/null; then if ! zaf_which sudo >/dev/null; then
zaf_wrn "Sudo needed bud not installed?" zaf_wrn "Sudo needed bud not installed?"
fi fi
@ -188,7 +188,7 @@ zaf_ctrl_cron() {
pdir="$3" pdir="$3"
plugin=$1 plugin=$1
cron=$(zaf_ctrl_get_global_option $2 "Cron") cron=$(zaf_ctrl_get_global_option $2 "Cron")
[ -z "$cron" ] || [ -z "$ZAF_CROND" ] && return [ -z "$cron" ] || [ -z "$ZAF_CROND" ] && return
! [ -d "$ZAF_CROND" ] && { zaf_wrn "$ZAF_CROND nonexistent! Skipping cron install!"; return 1; } ! [ -d "$ZAF_CROND" ] && { zaf_wrn "$ZAF_CROND nonexistent! Skipping cron install!"; return 1; }
zaf_dbg "Installing cron entry $ZAF_CROND/zaf_$plugin" zaf_dbg "Installing cron entry $ZAF_CROND/zaf_$plugin"
[ -z "$cron" ] && return # Nothing to install [ -z "$cron" ] && return # Nothing to install
@ -212,12 +212,12 @@ zaf_ctrl_install() {
binaries=$(zaf_ctrl_get_global_option $2 "Install-bin") binaries=$(zaf_ctrl_get_global_option $2 "Install-bin")
for b in $binaries; do for b in $binaries; do
zaf_fetch_url "$1/$b" >"${ZAF_TMP_DIR}/$b" zaf_fetch_url "$1/$b" >"${ZAF_TMP_DIR}/$b"
zaf_install_bin "${ZAF_TMP_DIR}/$b" "$pdir" zaf_install_bin "${ZAF_TMP_DIR}/$b" "$pdir"
done done
files=$(zaf_ctrl_get_global_option $2 "Install-files") files=$(zaf_ctrl_get_global_option $2 "Install-files")
for f in $files; do for f in $files; do
zaf_fetch_url "$1/$b" >"${ZAF_TMP_DIR}/$b" zaf_fetch_url "$1/$b" >"${ZAF_TMP_DIR}/$b"
zaf_install "${ZAF_TMP_DIR}/$b" "$pdir" zaf_install "${ZAF_TMP_DIR}/$b" "$pdir"
done done
true true
) || zaf_err "Error during zaf_ctrl_install" ) || zaf_err "Error during zaf_ctrl_install"
@ -244,9 +244,9 @@ zaf_ctrl_generate_items_cfg() {
tmpfile=$ZAF_TMP_DIR/gencfg$$ tmpfile=$ZAF_TMP_DIR/gencfg$$
(set -e (set -e
for i in $items; do for i in $items; do
iscript=$(zaf_stripctrl $i) iscript=$(zaf_stripctrl $i)
zaf_ctrl_get_item_option $1 $i "Parameters" >$tmpfile zaf_ctrl_get_item_option $1 $i "Parameters" >$tmpfile
if [ -s "$tmpfile" ]; then if [ -s "$tmpfile" ]; then
ikey="$2.$i[*]" ikey="$2.$i[*]"
args="" args=""
apos=1; apos=1;
@ -255,37 +255,37 @@ zaf_ctrl_generate_items_cfg() {
args="$args \$$apos" args="$args \$$apos"
apos=$(expr $apos + 1) apos=$(expr $apos + 1)
done <$tmpfile done <$tmpfile
else else
ikey="$2.$i" ikey="$2.$i"
zafparams="" zafparams=""
args="" args=""
fi fi
env="export ITEM_KEY='$ikey'; export PLUGIN='$2'; export PATH=${ZAF_PLUGINS_DIR}/$2:$ZAF_LIB_DIR:\$PATH; cd ${ZAF_PLUGINS_DIR}/$2; . $ZAF_LIB_DIR/preload.sh; " env="export ITEM_KEY='$ikey'; export PLUGIN='$2'; export PATH=${ZAF_PLUGINS_DIR}/$2:$ZAF_LIB_DIR:\$PATH; cd ${ZAF_PLUGINS_DIR}/$2; . $ZAF_LIB_DIR/preload.sh; "
lock=$(zaf_ctrl_get_item_option $1 $i "Lock") lock=$(zaf_ctrl_get_item_option $1 $i "Lock")
if [ -n "$lock" ]; then if [ -n "$lock" ]; then
lock="${ZAF_LIB_DIR}/zaflock $lock " lock="${ZAF_LIB_DIR}/zaflock $lock "
fi fi
cache=$(zaf_ctrl_get_item_option $1 $i "Cache") cache=$(zaf_ctrl_get_item_option $1 $i "Cache")
if [ -n "$cache" ]; then if [ -n "$cache" ]; then
cache="${ZAF_LIB_DIR}/zafcache '$cache' " cache="${ZAF_LIB_DIR}/zafcache '$cache' "
fi fi
cmd=$(zaf_ctrl_get_item_option $1 $i "Cmd") cmd=$(zaf_ctrl_get_item_option $1 $i "Cmd")
if [ -n "$cmd" ]; then if [ -n "$cmd" ]; then
printf "%s" "UserParameter=$ikey,${env}${zafparams}${preload}${cache}${lock}${cmd}"; echo printf "%s" "UserParameter=$ikey,${env}${zafparams}${preload}${cache}${lock}${cmd}"; echo
continue continue
fi fi
cmd=$(zaf_ctrl_get_item_option $1 $i "Script") cmd=$(zaf_ctrl_get_item_option $1 $i "Script")
if [ -n "$cmd" ]; then if [ -n "$cmd" ]; then
( echo "#!/bin/sh" ( echo "#!/bin/sh"
echo ". $ZAF_LIB_DIR/preload.sh; " echo ". $ZAF_LIB_DIR/preload.sh; "
zaf_ctrl_get_item_option $1 $i "Script" zaf_ctrl_get_item_option $1 $i "Script"
) >${ZAF_TMP_DIR}/${iscript}.sh; ) >${ZAF_TMP_DIR}/${iscript}.sh;
[ -z "$3" ] && zaf_install_bin ${ZAF_TMP_DIR}/${iscript}.sh ${ZAF_PLUGINS_DIR}/$2/ [ -z "$3" ] && zaf_install_bin ${ZAF_TMP_DIR}/${iscript}.sh ${ZAF_PLUGINS_DIR}/$2/
printf "%s" "UserParameter=$ikey,${env}${preload}${zafparams}${cache}${lock}${ZAF_PLUGINS_DIR}/$2/${iscript}.sh ${args}"; echo printf "%s" "UserParameter=$ikey,${env}${preload}${zafparams}${cache}${lock}${ZAF_PLUGINS_DIR}/$2/${iscript}.sh ${args}"; echo
rm -f ${ZAF_TMP_DIR}/${iscript}.sh rm -f ${ZAF_TMP_DIR}/${iscript}.sh
continue; continue;
fi fi
zaf_err "Item $i declared in control file but has no Cmd, Function or Script!" zaf_err "Item $i declared in control file but has no Cmd, Function or Script!"
done done
) || zaf_err "Error during zaf_ctrl_generate_items_cfg" ) || zaf_err "Error during zaf_ctrl_generate_items_cfg"
rm -f $tmpfile rm -f $tmpfile
@ -312,10 +312,10 @@ zaf_ctrl_generate_extitems_cfg() {
tmpfile=$ZAF_TMP_DIR/gencfg$$ tmpfile=$ZAF_TMP_DIR/gencfg$$
(set -e (set -e
for i in $items; do for i in $items; do
iscript=$(zaf_stripctrl $i) iscript=$(zaf_stripctrl $i)
zaf_ctrl_get_extitem_option $1 $i "Parameters" >$tmpfile zaf_ctrl_get_extitem_option $1 $i "Parameters" >$tmpfile
ikey="$2.$i" ikey="$2.$i"
if [ -s "$tmpfile" ]; then if [ -s "$tmpfile" ]; then
args="" args=""
apos=1; apos=1;
while read pname pdefault pregex prest; do while read pname pdefault pregex prest; do
@ -323,34 +323,34 @@ zaf_ctrl_generate_extitems_cfg() {
args="$args \$$apos" args="$args \$$apos"
apos=$(expr $apos + 1) apos=$(expr $apos + 1)
done <$tmpfile done <$tmpfile
else else
zafparams="" zafparams=""
args="" args=""
fi fi
env="export ITEM_KEY='$ikey'; export PLUGIN='$2'; export PATH=${ZAF_PLUGINS_DIR}/$2:$ZAF_LIB_DIR:\$PATH; cd ${ZAF_PLUGINS_DIR}/$2; . $ZAF_LIB_DIR/preload.sh; " env="export ITEM_KEY='$ikey'; export PLUGIN='$2'; export PATH=${ZAF_PLUGINS_DIR}/$2:$ZAF_LIB_DIR:\$PATH; cd ${ZAF_PLUGINS_DIR}/$2; . $ZAF_LIB_DIR/preload.sh; "
lock=$(zaf_ctrl_get_extitem_option $1 $i "Lock") lock=$(zaf_ctrl_get_extitem_option $1 $i "Lock")
if [ -n "$lock" ]; then if [ -n "$lock" ]; then
lock="${ZAF_LIB_DIR}/zaflock $lock " lock="${ZAF_LIB_DIR}/zaflock $lock "
fi fi
cache=$(zaf_ctrl_get_extitem_option $1 $i "Cache") cache=$(zaf_ctrl_get_extitem_option $1 $i "Cache")
if [ -n "$cache" ]; then if [ -n "$cache" ]; then
cache="${ZAF_LIB_DIR}/zafcache '$cache' " cache="${ZAF_LIB_DIR}/zafcache '$cache' "
fi fi
cmd=$(zaf_ctrl_get_extitem_option $1 $i "Cmd") cmd=$(zaf_ctrl_get_extitem_option $1 $i "Cmd")
if [ -n "$cmd" ]; then if [ -n "$cmd" ]; then
echo "#!/bin/sh" >${ZAF_SERVER_EXTSCRIPTS}/$ikey echo "#!/bin/sh" >${ZAF_SERVER_EXTSCRIPTS}/$ikey
chmod +x ${ZAF_SERVER_EXTSCRIPTS}/$ikey chmod +x ${ZAF_SERVER_EXTSCRIPTS}/$ikey
(printf "%s" "${env}${zafparams}${preload}${cache}${lock}${cmd}"; echo) >>${ZAF_SERVER_EXTSCRIPTS}/$ikey (printf "%s" "${env}${zafparams}${preload}${cache}${lock}${cmd}"; echo) >>${ZAF_SERVER_EXTSCRIPTS}/$ikey
continue continue
fi fi
cmd=$(zaf_ctrl_get_extitem_option $1 $i "Script") cmd=$(zaf_ctrl_get_extitem_option $1 $i "Script")
if [ -n "$cmd" ]; then if [ -n "$cmd" ]; then
echo "#!/bin/sh" >${ZAF_SERVER_EXTSCRIPTS}/$ikey echo "#!/bin/sh" >${ZAF_SERVER_EXTSCRIPTS}/$ikey
chmod +x ${ZAF_SERVER_EXTSCRIPTS}/$ikey chmod +x ${ZAF_SERVER_EXTSCRIPTS}/$ikey
(printf "%s" "${env}${zafparams}${preload}${cache}${lock}${cmd}"; echo) >>${ZAF_SERVER_EXTSCRIPTS}/$ikey (printf "%s" "${env}${zafparams}${preload}${cache}${lock}${cmd}"; echo) >>${ZAF_SERVER_EXTSCRIPTS}/$ikey
continue; continue;
fi fi
zaf_err "External item $i declared in control file but has no Cmd, Function or Script!" zaf_err "External item $i declared in control file but has no Cmd, Function or Script!"
done done
) || zaf_err "Error during zaf_ctrl_generate_extitems_cfg" ) || zaf_err "Error during zaf_ctrl_generate_extitems_cfg"
rm -f $tmpfile rm -f $tmpfile

View File

@ -1,23 +1,23 @@
# Os related functions # Os related functions
zaf_preconfigure_os_openwrt() { zaf_preconfigure_os_openwrt() {
ZAF_AGENT_RESTART="/etc/init.d/zabbix_agentd restart" ZAF_AGENT_RESTART="/etc/init.d/zabbix_agentd restart"
ZAF_AGENT_CONFIGD="/var/run/zabbix_agentd.conf.d/" ZAF_AGENT_CONFIGD="/var/run/zabbix_agentd.conf.d/"
ZAF_AGENT_CONFIG="/etc/zabbix_agentd.conf" ZAF_AGENT_CONFIG="/etc/zabbix_agentd.conf"
ZAF_AGENT_PKG="zabbix-agentd" ZAF_AGENT_PKG="zabbix-agentd"
ZAF_CURL_INSECURE=1 ZAF_CURL_INSECURE=1
} }
zaf_preconfigure_os_beesip() { zaf_preconfigure_os_beesip() {
zaf_preconfigure_os_openwrt zaf_preconfigure_os_openwrt
} }
zaf_preconfigure_os_freebsd() { zaf_preconfigure_os_freebsd() {
ZAF_AGENT_PKG="zabbix3-agent" ZAF_AGENT_PKG="zabbix3-agent"
ZAF_AGENT_CONFIG="/usr/local/etc/zabbix3/zabbix_agentd.conf" ZAF_AGENT_CONFIG="/usr/local/etc/zabbix3/zabbix_agentd.conf"
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_SUDOERSD="/usr/local/etc/sudoers.d"
} }
zaf_postconfigure_os_openwrt() { zaf_postconfigure_os_openwrt() {
@ -75,9 +75,9 @@ zaf_detect_system() {
ZAF_OS="unknown" ZAF_OS="unknown"
ZAF_OS_CODENAME="unknown" ZAF_OS_CODENAME="unknown"
ZAF_AGENT_PKG="" ZAF_AGENT_PKG=""
return return
fi fi
fi fi
fi fi
fi fi
} }
@ -86,72 +86,72 @@ zaf_detect_system() {
# $1 - name of the function. # $1 - name of the function.
# all variants will be tested. (name_os_codename, name_os, name_{dpkg|opkg|rpm}, name ) # all variants will be tested. (name_os_codename, name_os, name_{dpkg|opkg|rpm}, name )
zaf_os_specific(){ zaf_os_specific(){
local func="$1" local func="$1"
if type "${func}_${ZAF_OS}_${ZAF_OS_CODENAME}" >/dev/null 2>/dev/null; then if type "${func}_${ZAF_OS}_${ZAF_OS_CODENAME}" >/dev/null 2>/dev/null; then
eval "${func}_${ZAF_OS}_${ZAF_OS_CODENAME} $2 $3 $4 $5 $6" eval "${func}_${ZAF_OS}_${ZAF_OS_CODENAME} $2 $3 $4 $5 $6"
else if type "${func}_${ZAF_OS}" >/dev/null 2>/dev/null; then else if type "${func}_${ZAF_OS}" >/dev/null 2>/dev/null; then
eval "${func}_${ZAF_OS} $2 $3 $4 $5 $6" eval "${func}_${ZAF_OS} $2 $3 $4 $5 $6"
else if type "${func}_${ZAF_PKG}" >/dev/null 2>/dev/null; then else if type "${func}_${ZAF_PKG}" >/dev/null 2>/dev/null; then
eval "${func}_${ZAF_PKG} $2 $3 $4 $5 $6" eval "${func}_${ZAF_PKG} $2 $3 $4 $5 $6"
else else
zaf_dbg "No OS/packager specific implementation for $1" zaf_dbg "No OS/packager specific implementation for $1"
fi fi
fi fi
fi fi
} }
zaf_is_root(){ zaf_is_root(){
[ "$USER" = "root" ] || [ "$EUID" = "0" ] || [ -n "$ZAF_ISROOT" ] [ "$USER" = "root" ] || [ "$EUID" = "0" ] || [ -n "$ZAF_ISROOT" ]
} }
# Install file, bin or directory and respect install prefix # Install file, bin or directory and respect install prefix
# $1 - src file # $1 - src file
# $2 - directory # $2 - directory
zaf_install(){ zaf_install(){
zaf_dbg "Install file $1 to $INSTALL_PREFIX/$2/$(basename $1)" zaf_dbg "Install file $1 to $INSTALL_PREFIX/$2/$(basename $1)"
$ZAF_DO cp "$1" "$INSTALL_PREFIX/$2/$(basename $1)" || zaf_err "Cannot create file $INSTALL_PREFIX/$2/$(basename $1)!" $ZAF_DO cp "$1" "$INSTALL_PREFIX/$2/$(basename $1)" || zaf_err "Cannot create file $INSTALL_PREFIX/$2/$(basename $1)!"
} }
# $1 - src file # $1 - src file
# $2 - directory # $2 - directory
zaf_install_bin(){ zaf_install_bin(){
zaf_dbg "Install binary $1 to $INSTALL_PREFIX/$2/$(basename $1)" zaf_dbg "Install binary $1 to $INSTALL_PREFIX/$2/$(basename $1)"
$ZAF_DO cp "$1" "$INSTALL_PREFIX/$2/$(basename $1)" || zaf_err "Cannot create binary $INSTALL_PREFIX/$2/$(basename $1)!" $ZAF_DO cp "$1" "$INSTALL_PREFIX/$2/$(basename $1)" || zaf_err "Cannot create binary $INSTALL_PREFIX/$2/$(basename $1)!"
$ZAF_DO chmod +x "$INSTALL_PREFIX/$2/$(basename $1)" $ZAF_DO chmod +x "$INSTALL_PREFIX/$2/$(basename $1)"
} }
# $1 - directory # $1 - directory
zaf_install_dir(){ zaf_install_dir(){
zaf_dbg "Install directory $1 to $INSTALL_PREFIX/$1" zaf_dbg "Install directory $1 to $INSTALL_PREFIX/$1"
$ZAF_DO mkdir -p "$INSTALL_PREFIX/$1" || zaf_err "Cannot create directory $INSTALL_PREFIX/$1!" $ZAF_DO mkdir -p "$INSTALL_PREFIX/$1" || zaf_err "Cannot create directory $INSTALL_PREFIX/$1!"
} }
# $1 - file # $1 - file
zaf_touch(){ zaf_touch(){
zaf_dbg "Touch $INSTALL_PREFIX/$1" zaf_dbg "Touch $INSTALL_PREFIX/$1"
$ZAF_DO touch "$INSTALL_PREFIX/$1" $ZAF_DO touch "$INSTALL_PREFIX/$1"
} }
# $1 - directory # $1 - directory
zaf_uninstall(){ zaf_uninstall(){
if [ -n "$INSTALL_PREFIX" ]; then if [ -n "$INSTALL_PREFIX" ]; then
zaf_dbg "Removing $INSTALL_PREFIX/$1" zaf_dbg "Removing $INSTALL_PREFIX/$1"
$ZAF_DO rm -rf "$INSTALL_PREFIX/$1" $ZAF_DO rm -rf "$INSTALL_PREFIX/$1"
else else
zaf_dbg "Removing $1" zaf_dbg "Removing $1"
$ZAF_DO rm -rf "$1" $ZAF_DO rm -rf "$1"
fi fi
} }
# Automaticaly install agent on debian # Automaticaly install agent on debian
# For another os, create similar function (install_zabbix_centos) # For another os, create similar function (install_zabbix_centos)
zaf_install_agent_debian() { zaf_install_agent_debian() {
zaf_fetch_url "http://repo.zabbix.com/zabbix/3.0/debian/pool/main/z/zabbix-release/zabbix-release_3.0-1+${ZAF_OS_CODENAME}_all.deb" >"/tmp/zaf-installer/zabbix-release_3.0-1+${ZAF_OS_CODENAME}_all.deb" \ zaf_fetch_url "http://repo.zabbix.com/zabbix/3.0/debian/pool/main/z/zabbix-release/zabbix-release_3.0-1+${ZAF_OS_CODENAME}_all.deb" >"/tmp/zaf-installer/zabbix-release_3.0-1+${ZAF_OS_CODENAME}_all.deb" \
&& dpkg -i "/tmp/zaf-installer/zabbix-release_3.0-1+${ZAF_OS_CODENAME}_all.deb" \ && dpkg -i "/tmp/zaf-installer/zabbix-release_3.0-1+${ZAF_OS_CODENAME}_all.deb" \
&& apt-get update \ && apt-get update \
&& apt-get install -y -q $ZAF_AGENT_PKG && apt-get install -y -q $ZAF_AGENT_PKG
} }
zaf_install_agent_opkg() { zaf_install_agent_opkg() {
opkg update && \ opkg update && \
opkg install $ZAF_AGENT_PKG opkg install $ZAF_AGENT_PKG
} }
# Check if dpkg dependency is met # Check if dpkg dependency is met

View File

@ -19,7 +19,7 @@ zaf_get_plugin_url() {
local url local url
if [ "$(zaf_url_info $1)" = "path" ]; then if [ "$(zaf_url_info $1)" = "path" ]; then
url="$1" # plugin with path - from directory url="$1" # plugin with path - from directory
else else
if [ "$(zaf_url_info $1)" = "url" ]; then if [ "$(zaf_url_info $1)" = "url" ]; then
url="$1" # plugin with http[s] url url="$1" # plugin with http[s] url
@ -118,7 +118,7 @@ zaf_install_plugin() {
zaf_ctrl_sudo "$plugin" "${control}" "${plugindir}" zaf_ctrl_sudo "$plugin" "${control}" "${plugindir}"
zaf_ctrl_cron "$plugin" "${control}" "${plugindir}" zaf_ctrl_cron "$plugin" "${control}" "${plugindir}"
zaf_ctrl_generate_items_cfg "${control}" "${plugin}" \ zaf_ctrl_generate_items_cfg "${control}" "${plugin}" \
| zaf_far '{PLUGINDIR}' "${plugindir}" >${ZAF_AGENT_CONFIGD}/zaf_${plugin}.conf | zaf_far '{PLUGINDIR}' "${plugindir}" >${ZAF_AGENT_CONFIGD}/zaf_${plugin}.conf
zaf_dbg "Generated ${ZAF_AGENT_CONFIGD}/zaf_${plugin}.conf" zaf_dbg "Generated ${ZAF_AGENT_CONFIGD}/zaf_${plugin}.conf"
zaf_ctrl_generate_extitems_cfg "${control}" "${plugin}" zaf_ctrl_generate_extitems_cfg "${control}" "${plugin}"
else else
@ -146,7 +146,7 @@ zaf_postinstall_plugin() {
zaf_ctrl_sudo "$plugin" "${control}" "${plugindir}" zaf_ctrl_sudo "$plugin" "${control}" "${plugindir}"
zaf_ctrl_cron "$plugin" "${control}" "${plugindir}" zaf_ctrl_cron "$plugin" "${control}" "${plugindir}"
zaf_ctrl_generate_items_cfg "${control}" "${plugin}" \ zaf_ctrl_generate_items_cfg "${control}" "${plugin}" \
| zaf_far '{PLUGINDIR}' "${plugindir}" >${ZAF_AGENT_CONFIGD}/zaf_${plugin}.conf | zaf_far '{PLUGINDIR}' "${plugindir}" >${ZAF_AGENT_CONFIGD}/zaf_${plugin}.conf
zaf_dbg "Generated ${ZAF_AGENT_CONFIGD}/zaf_${plugin}.conf" zaf_dbg "Generated ${ZAF_AGENT_CONFIGD}/zaf_${plugin}.conf"
zaf_ctrl_generate_extitems_cfg "${control}" "${plugin}" zaf_ctrl_generate_extitems_cfg "${control}" "${plugin}"
} }
@ -306,7 +306,7 @@ zaf_test_item() {
} }
zaf_precache_item() { zaf_precache_item() {
cmd=$(grep "^UserParameter=$item" $ZAF_AGENT_CONFIGD/zaf*.conf | cut -d ',' -f 2- | sed -e "s/_cache/_nocache/") 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)" zaf_wrn "Precaching item $item[$(echo $*| tr ' ' ',')] ($cmd)"
eval $cmd eval $cmd
} }

View File

@ -24,8 +24,8 @@ zaf_wrn() {
} }
zaf_err() { zaf_err() {
logger ${ZAF_LOG_STDERR} -p user.err -t zaf-error -- $@ logger ${ZAF_LOG_STDERR} -p user.err -t zaf-error -- $@
logger ${ZAF_LOG_STDERR} -p user.err -t zaf-error "Exiting with error!" logger ${ZAF_LOG_STDERR} -p user.err -t zaf-error "Exiting with error!"
exit 1 exit 1
} }
# Help option # Help option
# $1 - key # $1 - key
@ -75,7 +75,7 @@ zaf_agentparm(){
value="$default" value="$default"
else else
if [ -n "$regexp" ]; then if [ -n "$regexp" ]; then
echo "$value" | grep -qE "$regexp" || zaf_err "$ITEM_KEY: Bad parameter '$name' value '$value' (not in regexp '$regexp')." echo "$value" | grep -qE "$regexp" || zaf_err "$ITEM_KEY: Bad parameter '$name' value '$value' (not in regexp '$regexp')."
fi fi
fi fi
eval $name=$value eval $name=$value
@ -116,7 +116,7 @@ zaf_fetch_url() {
} }
# Get info about url # Get info about url
# returns path or url # returns path or url
zaf_url_info() { zaf_url_info() {
if echo "$1" | grep -q '^/'; then if echo "$1" | grep -q '^/'; then
echo "path" echo "path"
@ -131,41 +131,41 @@ zaf_url_info() {
# Find and replace string # Find and replace string
zaf_far(){ zaf_far(){
local f local f
local t local t
local sedcmd="sed" local sedcmd="sed"
i=1 i=1
while [ "$i" -lt "$#" ]; while [ "$i" -lt "$#" ];
do do
eval f=\$${i} eval f=\$${i}
i=$(expr $i + 1) i=$(expr $i + 1)
eval t=\$${i} eval t=\$${i}
i=$(expr $i + 1) i=$(expr $i + 1)
sedcmd="$sedcmd -e 's~$f~$t~g'" sedcmd="$sedcmd -e 's~$f~$t~g'"
done done
eval $sedcmd eval $sedcmd
} }
# Limit concurrent processes or continue # Limit concurrent processes or continue
zaf_bglimit(){ zaf_bglimit(){
local maxbg local maxbg
local maxnumber local maxnumber
local cnumber local cnumber
[ -z "$BASH_VERSION" ] && { zaf_dbg "Job server not available. Use bash!"; return 1; } [ -z "$BASH_VERSION" ] && { zaf_dbg "Job server not available. Use bash!"; return 1; }
if [ $# -eq 0 ] ; then if [ $# -eq 0 ] ; then
maxbg=5 maxbg=5
else else
maxbg=$1 maxbg=$1
fi fi
maxnumber=$((0 + ${1:-0})) maxnumber=$((0 + ${1:-0}))
while true; do while true; do
cnumber=$(jobs | wc -l) cnumber=$(jobs | wc -l)
if [ $cnumber -lt $maxnumber ]; then if [ $cnumber -lt $maxnumber ]; then
break break
fi fi
zaf_dbg "Limiting next job due to $maxbg limit of bg jobs" zaf_dbg "Limiting next job due to $maxbg limit of bg jobs"
sleep 1 sleep 1
done done
} }
# Initialises discovery function # Initialises discovery function
@ -178,27 +178,27 @@ EOF
# Add row(s) to discovery data # Add row(s) to discovery data
zaf_discovery_add_row(){ zaf_discovery_add_row(){
local rows local rows
local row local row
rows=$1 rows=$1
row=$2 row=$2
shift;shift shift;shift
echo " {" echo " {"
while [ -n "$1" ]; do while [ -n "$1" ]; do
echo -n ' "'$1'":"'$2'" ' echo -n ' "'$1'":"'$2'" '
shift;shift shift;shift
if [ -n "$1" ]; then if [ -n "$1" ]; then
echo "," echo ","
else else
echo "" echo ""
fi fi
done done
if [ "$row" -lt "$rows" ]; then if [ "$row" -lt "$rows" ]; then
echo " }," echo " },"
else else
echo " }" echo " }"
fi fi
} }
# Dumps json object # Dumps json object
@ -213,22 +213,22 @@ EOF
# Arguments are name of variables to discovery. # Arguments are name of variables to discovery.
# Dumps json to stdout # Dumps json to stdout
zaf_discovery(){ zaf_discovery(){
local tmpfile local tmpfile
local rows local rows
local a b c d e f g h i j row local a b c d e f g h i j row
tmpfile="${ZAF_TMP_DIR}/disc$$" tmpfile="${ZAF_TMP_DIR}/disc$$"
cat >$tmpfile cat >$tmpfile
rows=$(wc -l <$tmpfile) rows=$(wc -l <$tmpfile)
local a b c d e f g h i j; local a b c d e f g h i j;
zaf_discovery_begin zaf_discovery_begin
row=1 row=1
while read a b c d e f g h i j; do while read a b c d e f g h i j; do
zaf_discovery_add_row "$rows" "$row" "$1" "${1:+${a}}" "$2" "${2:+${b}}" "$3" "${3:+${c}}" "$4" "${4:+${d}}" "$5" "${5:+${e}}" "$6" "${6:+${f}}" "$7" "${7:+${g}}" "$8" "${8:+${h}}" "$9" "${9:+${i}}" zaf_discovery_add_row "$rows" "$row" "$1" "${1:+${a}}" "$2" "${2:+${b}}" "$3" "${3:+${c}}" "$4" "${4:+${d}}" "$5" "${5:+${e}}" "$6" "${6:+${f}}" "$7" "${7:+${g}}" "$8" "${8:+${h}}" "$9" "${9:+${i}}"
row=$(expr $row + 1) row=$(expr $row + 1)
done <$tmpfile done <$tmpfile
zaf_discovery_end zaf_discovery_end
rm -f $tmpfile rm -f $tmpfile
} }
############################################ Zaf internal routines ############################################ Zaf internal routines

View File

@ -10,7 +10,7 @@ zaf_zbxapi_do() {
query="$1" query="$1"
zaf_trc "Zabbix API: $query" zaf_trc "Zabbix API: $query"
curl -s -f -L -X POST -H 'Content-Type: application/json-rpc' -d "$query" "$ZAF_ZBXAPI_URL" >$tmpfile 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
rm -f $tmpfile rm -f $tmpfile
@ -52,7 +52,7 @@ zaf_zbxapi_getstring() {
# Extract value from JSON response result # Extract value from JSON response result
# $1 key # $1 key
zaf_zbxapi_getvalues() { zaf_zbxapi_getvalues() {
$ZAF_LIB_DIR/JSON.sh -b | grep '\["result",.*,"'$1'"]' | tr '\t' ' ' | cut -d ' ' -f 2- | sed -e 's/^"//' -e 's/"$//' $ZAF_LIB_DIR/JSON.sh -b | grep '\["result",.*,"'$1'"]' | tr '\t' ' ' | cut -d ' ' -f 2- | sed -e 's/^"//' -e 's/"$//'
} }
# Zabbix API related functions # Zabbix API related functions
@ -66,17 +66,17 @@ zaf_zbxapi_login(){
[ -z "$ZAF_ZBXAPI_URL" ] || [ -z "$ZAF_ZBXAPI_USER" ] || [ -z "$ZAF_ZBXAPI_PASS" ] && zaf_err "Zabbix Api parameters not set! Set ZAF_ZBXAPI_URL, ZAF_ZBXAPI_USER and ZAF_ZBXAPI_PASS and try again." [ -z "$ZAF_ZBXAPI_URL" ] || [ -z "$ZAF_ZBXAPI_USER" ] || [ -z "$ZAF_ZBXAPI_PASS" ] && zaf_err "Zabbix Api parameters not set! Set ZAF_ZBXAPI_URL, ZAF_ZBXAPI_USER and ZAF_ZBXAPI_PASS and try again."
authstr='{ authstr='{
"method" : "user.login", "method" : "user.login",
"params" : { "params" : {
"password" : "'$ZAF_ZBXAPI_PASS'", "password" : "'$ZAF_ZBXAPI_PASS'",
"user" : "'$ZAF_ZBXAPI_USER'" "user" : "'$ZAF_ZBXAPI_USER'"
}, },
"id" : 0, "id" : 0,
"jsonrpc" : "2.0" "jsonrpc" : "2.0"
}' }'
if [ "$ZAF_ZBXAPI_AUTHTYPE" = "http" ] ; then if [ "$ZAF_ZBXAPI_AUTHTYPE" = "http" ] ; then
ZAF_ZBXAPI_URL=$(echo $ZAF_ZBXAPI_URL | cut -d '/' -f 1)//$ZAF_ZBXAPI_USER:$ZAF_ZBXAPI_PASS@$(echo $ZAF_ZBXAPI_URL | cut -d '/' -f 3-) ZAF_ZBXAPI_URL=$(echo $ZAF_ZBXAPI_URL | cut -d '/' -f 1)//$ZAF_ZBXAPI_USER:$ZAF_ZBXAPI_PASS@$(echo $ZAF_ZBXAPI_URL | cut -d '/' -f 3-)
fi fi
result=$(zaf_zbxapi_do_cache "$authstr") result=$(zaf_zbxapi_do_cache "$authstr")
ZAF_ZBXAPI_AUTH=$(echo $result |zaf_zbxapi_getstring) ZAF_ZBXAPI_AUTH=$(echo $result |zaf_zbxapi_getstring)
@ -122,9 +122,9 @@ zaf_zbxapi_get_object() {
zaf_zbxapi_gethostgroupid() { zaf_zbxapi_gethostgroupid() {
local result local result
result=$(zaf_zbxapi_get_object "hostgroup" '"name": ["'$1'"]') result=$(zaf_zbxapi_get_object "hostgroup" '"name": ["'$1'"]')
[ -z "$result" ] && zaf_err "HostGroup $1 not found!" [ -z "$result" ] && zaf_err "HostGroup $1 not found!"
echo $result |zaf_zbxapi_getvalues groupid echo $result |zaf_zbxapi_getvalues groupid
} }
# $1 hostid # $1 hostid
@ -132,10 +132,10 @@ zaf_zbxapi_gethostgroupid() {
zaf_zbxapi_gethost() { zaf_zbxapi_gethost() {
local result local result
result=$(zaf_zbxapi_get_object "host" '' '"hostids": ["'$1'"]' 'extend') result=$(zaf_zbxapi_get_object "host" '' '"hostids": ["'$1'"]' 'extend')
[ -z "$result" ] && zaf_err "Hostid $1 not found!" [ -z "$result" ] && zaf_err "Hostid $1 not found!"
if [ -z "$2" ]; then if [ -z "$2" ]; then
echo $result echo $result
else else
echo $result |zaf_zbxapi_getvalues $2 echo $result |zaf_zbxapi_getvalues $2
fi fi
@ -145,9 +145,9 @@ zaf_zbxapi_gethost() {
zaf_zbxapi_gethostid() { zaf_zbxapi_gethostid() {
local result local result
result=$(zaf_zbxapi_get_object "host" '"host": ["'$1'"]') result=$(zaf_zbxapi_get_object "host" '"host": ["'$1'"]')
[ -z "$result" ] && zaf_err "Host $1 not found!" [ -z "$result" ] && zaf_err "Host $1 not found!"
echo $result |zaf_zbxapi_getvalues hostid echo $result |zaf_zbxapi_getvalues hostid
} }
# $1 hostname # $1 hostname
@ -155,10 +155,10 @@ zaf_zbxapi_gethostid() {
zaf_zbxapi_gethostinventory() { zaf_zbxapi_gethostinventory() {
local result local result
result=$(zaf_zbxapi_get_object "host" '"host": ["'$1'"]' '"withInventory": "true", "selectInventory": "extend"') result=$(zaf_zbxapi_get_object "host" '"host": ["'$1'"]' '"withInventory": "true", "selectInventory": "extend"')
[ -z "$result" ] && zaf_err "Host $1 not found!" [ -z "$result" ] && zaf_err "Host $1 not found!"
if [ -z "$2" ]; then if [ -z "$2" ]; then
echo $result echo $result
else else
echo $result |zaf_zbxapi_getvalues $2 echo $result |zaf_zbxapi_getvalues $2
fi fi
@ -168,9 +168,9 @@ zaf_zbxapi_gethostinventory() {
zaf_zbxapi_gettemplateid() { zaf_zbxapi_gettemplateid() {
local result local result
result=$(zaf_zbxapi_get_object "template" '"host": ["'$1'"]') result=$(zaf_zbxapi_get_object "template" '"host": ["'$1'"]')
[ -z "$result" ] && zaf_err "Template $1 not found!" [ -z "$result" ] && zaf_err "Template $1 not found!"
echo $result |zaf_zbxapi_getvalues templateid echo $result |zaf_zbxapi_getvalues templateid
} }
# $1 templateid # $1 templateid
@ -178,10 +178,10 @@ zaf_zbxapi_gettemplateid() {
zaf_zbxapi_gettemplate() { zaf_zbxapi_gettemplate() {
local result local result
result=$(zaf_zbxapi_get_object "template" '' '"templateids": ["'$1'"]' 'extend') result=$(zaf_zbxapi_get_object "template" '' '"templateids": ["'$1'"]' 'extend')
[ -z "$result" ] && zaf_err "Templateid $1 not found!" [ -z "$result" ] && zaf_err "Templateid $1 not found!"
if [ -z "$2" ]; then if [ -z "$2" ]; then
echo $result echo $result
else else
echo $result |zaf_zbxapi_getvalues $2 echo $result |zaf_zbxapi_getvalues $2
fi fi
@ -191,34 +191,34 @@ zaf_zbxapi_gettemplate() {
zaf_zbxapi_gethostsingroup() { zaf_zbxapi_gethostsingroup() {
local result local result
result=$(zaf_zbxapi_get_object "host" '' '"groupids": ["'$1'"]') result=$(zaf_zbxapi_get_object "host" '' '"groupids": ["'$1'"]')
[ -z "$result" ] && zaf_wrn "No hosts in groupid '$1'" [ -z "$result" ] && zaf_wrn "No hosts in groupid '$1'"
echo $result | zaf_zbxapi_getvalues "hostid" echo $result | zaf_zbxapi_getvalues "hostid"
} }
# Get all hostids in system # Get all hostids in system
zaf_zbxapi_gethostids() { zaf_zbxapi_gethostids() {
local result local result
result=$(zaf_zbxapi_get_object "host") result=$(zaf_zbxapi_get_object "host")
echo $result | zaf_zbxapi_getvalues "hostid" echo $result | zaf_zbxapi_getvalues "hostid"
} }
# Get all templateids in system # Get all templateids in system
zaf_zbxapi_gettemplateids() { zaf_zbxapi_gettemplateids() {
local result local result
result=$(zaf_zbxapi_get_object "template") result=$(zaf_zbxapi_get_object "template")
echo $result | zaf_zbxapi_getvalues "templateid" echo $result | zaf_zbxapi_getvalues "templateid"
} }
# $1 hostgroupid # $1 hostgroupid
zaf_zbxapi_gettemplatesingroup() { zaf_zbxapi_gettemplatesingroup() {
local result local result
result=$(zaf_zbxapi_get_object "template" '' '"groupids": ["'$1'"]') result=$(zaf_zbxapi_get_object "template" '' '"groupids": ["'$1'"]')
[ -z "$result" ] && zaf_wrn "No templates in groupid '$1'" [ -z "$result" ] && zaf_wrn "No templates in groupid '$1'"
echo $result | zaf_zbxapi_getvalues "templateid" echo $result | zaf_zbxapi_getvalues "templateid"
} }
# $1 map or null for all # $1 map or null for all
@ -226,12 +226,12 @@ zaf_zbxapi_getmapid() {
local result local result
if [ -n "$1" ]; then if [ -n "$1" ]; then
result=$(zaf_zbxapi_get_object "map" '"name": ["'$1'"]') result=$(zaf_zbxapi_get_object "map" '"name": ["'$1'"]')
else else
result=$(zaf_zbxapi_get_object "map") result=$(zaf_zbxapi_get_object "map")
fi fi
[ -z "$result" ] && zaf_err "Map $1 not found" [ -z "$result" ] && zaf_err "Map $1 not found"
echo $result | zaf_zbxapi_getvalues "sysmapid" echo $result | zaf_zbxapi_getvalues "sysmapid"
} }
# $1 mapid # $1 mapid
@ -239,10 +239,10 @@ zaf_zbxapi_getmapid() {
zaf_zbxapi_getmap() { zaf_zbxapi_getmap() {
local result local result
result=$(zaf_zbxapi_get_object "map" '' '"sysmapids": ["'$1'"]' 'extend') result=$(zaf_zbxapi_get_object "map" '' '"sysmapids": ["'$1'"]' 'extend')
[ -z "$result" ] && zaf_err "Mapid $1 not found" [ -z "$result" ] && zaf_err "Mapid $1 not found"
if [ -z "$2" ]; then if [ -z "$2" ]; then
echo $result echo $result
else else
echo $result |zaf_zbxapi_getvalues $2 echo $result |zaf_zbxapi_getvalues $2
fi fi
@ -261,18 +261,18 @@ zaf_zbxapi_export_object(){
bkpstr=' bkpstr='
{ {
"method": "configuration.export", "method": "configuration.export",
"jsonrpc": "2.0", "jsonrpc": "2.0",
"params": { "params": {
"options": { "options": {
"'$obj'": [ "'$obj'": [
"'$id'" "'$id'"
] ]
}, },
"format": "xml" "format": "xml"
}, },
"auth": "'$ZAF_ZBXAPI_AUTH'", "auth": "'$ZAF_ZBXAPI_AUTH'",
"id": 1 "id": 1
}' }'
zaf_zbxapi_do_cache "$bkpstr" | zaf_zbxapi_getxml zaf_zbxapi_do_cache "$bkpstr" | zaf_zbxapi_getxml
} }
@ -303,67 +303,67 @@ zaf_zbxapi_import_config(){
local impstr local impstr
if [ -z "$1" ]; then if [ -z "$1" ]; then
xmlstr=$(zaf_strescape '"') xmlstr=$(zaf_strescape '"')
else else
! [ -f "$1" ] && return 1 ! [ -f "$1" ] && return 1
xmlstr=$(zaf_strescape '"\n\r' <$1) xmlstr=$(zaf_strescape '"\n\r' <$1)
fi fi
impstr=' impstr='
{ {
"method": "configuration.import", "method": "configuration.import",
"jsonrpc": "2.0", "jsonrpc": "2.0",
"params": { "params": {
"format": "xml", "format": "xml",
"rules": { "rules": {
"applications": { "applications": {
"createMissing": true, "createMissing": true,
"updateExisting": true "updateExisting": true
}, },
"discoveryRules": { "discoveryRules": {
"createMissing": true, "createMissing": true,
"updateExisting": true "updateExisting": true
}, },
"graphs": { "graphs": {
"createMissing": true, "createMissing": true,
"updateExisting": true "updateExisting": true
}, },
"hosts": { "hosts": {
"createMissing": true, "createMissing": true,
"updateExisting": true "updateExisting": true
}, },
"items": { "items": {
"createMissing": true, "createMissing": true,
"updateExisting": true "updateExisting": true
}, },
"templates": { "templates": {
"createMissing": true, "createMissing": true,
"updateExisting": true "updateExisting": true
}, },
"triggers": { "triggers": {
"createMissing": true, "createMissing": true,
"updateExisting": true "updateExisting": true
}, },
"maps": { "maps": {
"createMissing": true, "createMissing": true,
"updateExisting": true "updateExisting": true
}, },
"screens": { "screens": {
"createMissing": true, "createMissing": true,
"updateExisting": true "updateExisting": true
}, },
"items": { "items": {
"createMissing": true, "createMissing": true,
"updateExisting": true "updateExisting": true
}, },
"valueMaps": { "valueMaps": {
"createMissing": true, "createMissing": true,
"updateExisting": true "updateExisting": true
} }
}, },
"source": "'$xmlstr'" "source": "'$xmlstr'"
}, },
"auth": "'$ZAF_ZBXAPI_AUTH'", "auth": "'$ZAF_ZBXAPI_AUTH'",
"id": 3 "id": 3
}' }'
zaf_zbxapi_do "$impstr" | zaf_zbxapi_getresult | grep -q true zaf_zbxapi_do "$impstr" | zaf_zbxapi_getresult | grep -q true
} }

36
zaf
View File

@ -11,14 +11,14 @@ ZAF_CFG_FILE="/etc/zaf.conf"
# Read options as config for ZAF # Read options as config for ZAF
for pair in "$@"; do for pair in "$@"; do
if echo $pair | grep -qE '^ZAF\_(.*)='; then if echo $pair | grep -qE '^ZAF\_(.*)='; then
option=$(echo $pair|cut -d '=' -f 1) option=$(echo $pair|cut -d '=' -f 1)
value=$(echo $pair|cut -d '=' -f 2-) value=$(echo $pair|cut -d '=' -f 2-)
eval "${option}='$value'" eval "${option}='$value'"
export secondstage=1 export secondstage=1
else else
params="$params $pair" params="$params $pair"
fi fi
done done
export $(set |grep ^ZAF_ | cut -d '=' -f 1) export $(set |grep ^ZAF_ | cut -d '=' -f 1)
# If some variables in cmd were stripped, rerun only with right arguments and exported variables # If some variables in cmd were stripped, rerun only with right arguments and exported variables
@ -86,7 +86,7 @@ userparms)
plugindir=$ZAF_PLUGINS_DIR/$plugin plugindir=$ZAF_PLUGINS_DIR/$plugin
control=$plugindir/control.zaf control=$plugindir/control.zaf
zaf_ctrl_generate_items_cfg "${control}" "${plugin}" "noscripts" \ zaf_ctrl_generate_items_cfg "${control}" "${plugin}" "noscripts" \
| zaf_far '{PLUGINDIR}' "${plugindir}" | zaf_far '{PLUGINDIR}' "${plugindir}"
done done
;; ;;
agent-config) agent-config)
@ -102,10 +102,10 @@ agent-config)
for plugin in $(zaf_list_plugins); do for plugin in $(zaf_list_plugins); do
plugindir=$ZAF_PLUGINS_DIR/$plugin plugindir=$ZAF_PLUGINS_DIR/$plugin
control=$plugindir/control.zaf control=$plugindir/control.zaf
if ! [ -f "${ZAF_AGENT_CONFIGD}/zaf_${plugin}.conf" ] || [ "$control" -nt "${ZAF_AGENT_CONFIGD}/zaf_${plugin}.conf" ] || [ -n "$force" ]; then if ! [ -f "${ZAF_AGENT_CONFIGD}/zaf_${plugin}.conf" ] || [ "$control" -nt "${ZAF_AGENT_CONFIGD}/zaf_${plugin}.conf" ] || [ -n "$force" ]; then
zaf_dbg "Generating ${ZAF_AGENT_CONFIGD}/zaf_${plugin}.conf" zaf_dbg "Generating ${ZAF_AGENT_CONFIGD}/zaf_${plugin}.conf"
zaf_ctrl_generate_items_cfg "${control}" "${plugin}" \ zaf_ctrl_generate_items_cfg "${control}" "${plugin}" \
| zaf_far '{PLUGINDIR}' "${plugindir}" >${ZAF_AGENT_CONFIGD}/zaf_${plugin}.conf | zaf_far '{PLUGINDIR}' "${plugindir}" >${ZAF_AGENT_CONFIGD}/zaf_${plugin}.conf
fi fi
done done
;; ;;
@ -264,23 +264,23 @@ self-remove)
zaf_os_specific zaf_check_deps zaf && zaf_err "Zaf is installed as system package. Cannot self-remove." zaf_os_specific zaf_check_deps zaf && zaf_err "Zaf is installed as system package. Cannot self-remove."
. /etc/zaf.conf . /etc/zaf.conf
if [ "$1" = "force" ]; then if [ "$1" = "force" ]; then
rm -rf /etc/zaf.conf ${ZAF_PLUGINS_DIR} ${ZAF_REPO_DIR} ${ZAF_LIB_DIR} \ rm -rf /etc/zaf.conf ${ZAF_PLUGINS_DIR} ${ZAF_REPO_DIR} ${ZAF_LIB_DIR} \
${ZAF_BIN_DIR}/zaf ${ZAF_AGENT_CONFIGD}/zaf_* ${ZAF_BIN_DIR}/zaf ${ZAF_AGENT_CONFIGD}/zaf_*
else else
echo "This will remove zaf from this computer and erase all configuration." echo "This will remove zaf from this computer and erase all configuration."
echo "This command will be executed:" echo "This command will be executed:"
echo "rm -rf /etc/zaf.conf ${ZAF_PLUGINS_DIR} ${ZAF_REPO_DIR} ${ZAF_LIB_DIR} \ echo "rm -rf /etc/zaf.conf ${ZAF_PLUGINS_DIR} ${ZAF_REPO_DIR} ${ZAF_LIB_DIR} \
${ZAF_BIN_DIR}/zaf ${ZAF_AGENT_CONFIGD}/zaf_*" ${ZAF_BIN_DIR}/zaf ${ZAF_AGENT_CONFIGD}/zaf_*"
echo echo
echo "To continue, please do $0 self-remove force" echo "To continue, please do $0 self-remove force"
fi fi
;; ;;
###### API related commands ###### API related commands
api) api)
zaf_zbxapi_login zaf_zbxapi_login
case $2 in case $2 in
get-*-id) get-*-id)
obj=$(echo $2|cut -d '-' -f 2) obj=$(echo $2|cut -d '-' -f 2)
[ -z "$3" ] && zaf_err "$0 $1 $2 <$obj>" [ -z "$3" ] && zaf_err "$0 $1 $2 <$obj>"
eval zaf_zbxapi_get${obj}id "$3" eval zaf_zbxapi_get${obj}id "$3"
;; ;;