1
0
mirror of https://github.com/limosek/zaf.git synced 2024-11-21 18:09:06 +01:00

Make scripts nicer by unexpand

This commit is contained in:
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
zaf_fetch_url(){
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"
else
wget --no-check-certificate -O - "$1"
@ -38,8 +38,8 @@ zaf_which() {
# Lite version of zaf_err, full version will be loaded later
zaf_err() {
logger ${ZAF_LOG_STDERR} -p user.err -t zaf-error -- $@
logger ${ZAF_LOG_STDERR} -p user.err -t zaf-error "Exiting with error!"
exit 1
logger ${ZAF_LOG_STDERR} -p user.err -t zaf-error "Exiting with error!"
exit 1
}
# Download tgz and extract to tmpdir
@ -59,12 +59,12 @@ zaf_get_option(){
local opt
ZAF_HELP_OPTS="$ZAF_HELP_OPTS\n$1 $2 [$3]"
eval opt=\$C_$1
eval opt=\$C_$1
if [ -n "$opt" ]; then
eval "$1='$opt'"
zaf_dbg "Got '$2' <$1> from CLI: $opt"
return
fi
eval "$1='$opt'"
zaf_dbg "Got '$2' <$1> from CLI: $opt"
return
fi
eval opt=\$$1
if [ -n "$opt" ] && ! [ "$4" = "user" ]; then
eval "$1='$opt'"
@ -180,7 +180,7 @@ zaf_configure_agent() {
local options
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"
! [ -f "$ZAF_AGENT_CONFIG" ] && zaf_install "$ZAF_AGENT_CONFIG"
for pair in "$@"; do
@ -208,7 +208,7 @@ zaf_configure_server() {
local options
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"
for pair in "$@"; do
echo $pair | grep -q '^S\_' || continue # Skip non S_ vars
@ -229,12 +229,12 @@ zaf_configure_server() {
zaf_preconfigure(){
[ -z "$ZAF_OS" ] && zaf_detect_system
zaf_os_specific zaf_preconfigure_os
zaf_os_specific zaf_preconfigure_os
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
[ "$1" != "reconf" ] && zaf_os_specific zaf_check_deps zaf && zaf_err "Zaf is installed as system package. Cannot install."
fi
fi
}
zaf_configure(){
@ -263,7 +263,7 @@ zaf_configure(){
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_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_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"
@ -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."
fi
[ -n "$INSTALL_PREFIX" ] && zaf_install_dir "/etc"
[ -n "$INSTALL_PREFIX" ] && zaf_install_dir "/etc"
if ! [ -f "${ZAF_CFG_FILE}" ]; then
touch "${ZAF_CFG_FILE}" || zaf_err "No permissions to ${ZAF_CFG_FILE}"
fi
@ -307,7 +307,7 @@ zaf_configure(){
zaf_set_option ZAF_TMP_DIR "$ZAF_TMP_DIR"
zaf_set_option ZAF_CACHE_DIR "$ZAF_CACHE_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_REPO_URL "$ZAF_REPO_URL"
zaf_set_option ZAF_PROXY "$ZAF_PROXY"
@ -344,7 +344,7 @@ zaf_configure(){
fi
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"
if [ -f "$ZAF_SERVER_BIN" ]; then
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
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() {
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."
zaf_os_specific zaf_postconfigure_os
if zaf_is_root && ! zaf_test_item zaf.framework_version; 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."
zaf_os_specific zaf_postconfigure_os
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}"
echo "and there is Include=${ZAF_AGENT_CONFIGD} directive inside."
echo "Does ${ZAF_AGENT_RESTART} work?"
exit 1
fi
fi
else
[ "${ZAF_GIT}" = 1 ] && [ -n "${INSTALL_PREFIX}" ] && git clone "${ZAF_REPO_GITURL}" "${INSTALL_PREFIX}/${ZAF_REPO_DIR}"
fi
[ "${ZAF_GIT}" = 1 ] && [ -n "${INSTALL_PREFIX}" ] && git clone "${ZAF_REPO_GITURL}" "${INSTALL_PREFIX}/${ZAF_REPO_DIR}"
fi
zaf_wrn "Install done. Use 'zaf' to get started."
true
}
@ -416,7 +416,7 @@ if ! [ -f README.md ]; then
export https_proxy="$ZAF_PROXY"
fi
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
zaf_err "Curl or wget not found. Cannot continue. Please install it."
fi
@ -457,11 +457,11 @@ zaf_tmp_init
# Read options as config for ZAF
for pair in "$@"; do
echo $pair | grep -q '^ZAF\_' || continue
option=$(echo $pair|cut -d '=' -f 1)
value=$(echo $pair|cut -d '=' -f 2-)
eval "C_${option}='$value'"
zaf_wrn "Overriding $option from cmdline."
echo $pair | grep -q '^ZAF\_' || continue
option=$(echo $pair|cut -d '=' -f 1)
value=$(echo $pair|cut -d '=' -f 2-)
eval "C_${option}='$value'"
zaf_wrn "Overriding $option from cmdline."
done
[ -z "$C_ZAF_TMP_DIR" ] && C_ZAF_TMP_DIR="/tmp/"
if [ -n "$ZAF_PROXY" ]; then
@ -470,7 +470,7 @@ fi
case $1 in
interactive)
shift
shift
INSTALL_MODE=interactive
zaf_preconfigure
zaf_configure "$@"
@ -478,52 +478,52 @@ interactive)
zaf_postconfigure
;;
auto)
shift
shift
INSTALL_MODE=auto
zaf_preconfigure
zaf_configure "$@"
zaf_install_all
zaf_install_all
zaf_postconfigure
;;
;;
debug-auto)
shift;
shift;
ZAF_DEBUG=4
INSTALL_MODE=auto
zaf_preconfigure
zaf_configure "$@"
zaf_configure "$@"
zaf_install_all
zaf_postconfigure
;;
;;
debug-interactive)
shift;
ZAF_DEBUG=4
shift;
ZAF_DEBUG=4
INSTALL_MODE=interactive
zaf_preconfigure
zaf_configure "$@"
zaf_install_all
zaf_postconfigure
;;
;;
debug)
shift;
ZAF_DEBUG=4
shift;
ZAF_DEBUG=4
INSTALL_MODE=auto
zaf_preconfigure
zaf_configure "$@"
zaf_install_all
zaf_postconfigure
;;
;;
reconf)
shift;
rm -f $ZAF_CFG_FILE
shift;
rm -f $ZAF_CFG_FILE
INSTALL_MODE=auto
zaf_preconfigure reconf
zaf_configure "$@"
zaf_postconfigure
;;
;;
install)
INSTALL_MODE=auto
zaf_preconfigure nor
zaf_configure "$@"
zaf_configure "$@"
zaf_install_all
zaf_postconfigure
;;
@ -531,12 +531,12 @@ install)
echo
echo "Please specify how to install."
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 " Agent-Options: Z_Option=value [...]"
echo "scratch means that config file will be created from scratch"
echo " Agent-Options: Z_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
echo
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 3 (preconfigure server options): install.sh auto S_StartPollers=10 S_ListenPort=10051'

View File

@ -1,8 +1,8 @@
#!/bin/sh
throw() {
echo "$*" >&2
exit 1
echo "$*" >&2
exit 1
}
BRIEF=0
@ -12,197 +12,197 @@ NO_HEAD=0
NORMALIZE_SOLIDUS=0
usage() {
echo
echo "Usage: JSON.sh [-b] [-l] [-p] [-s] [-h]"
echo
echo "-p - Prune empty. Exclude fields with empty values."
echo "-l - Leaf only. Only show leaf nodes, which stops data duplication."
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 "-s - Remove escaping of the solidus symbol (stright slash)."
echo "-h - This help text."
echo
echo
echo "Usage: JSON.sh [-b] [-l] [-p] [-s] [-h]"
echo
echo "-p - Prune empty. Exclude fields with empty values."
echo "-l - Leaf only. Only show leaf nodes, which stops data duplication."
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 "-s - Remove escaping of the solidus symbol (stright slash)."
echo "-h - This help text."
echo
}
parse_options() {
set -- "$@"
local ARGN=$#
while [ "$ARGN" -ne 0 ]
do
case $1 in
-h) usage
exit 0
;;
-b) BRIEF=1
LEAFONLY=1
PRUNE=1
;;
-l) LEAFONLY=1
;;
-p) PRUNE=1
;;
-n) NO_HEAD=1
;;
-s) NORMALIZE_SOLIDUS=1
;;
?*) echo "ERROR: Unknown option."
usage
exit 0
;;
esac
shift 1
ARGN=$((ARGN-1))
done
set -- "$@"
local ARGN=$#
while [ "$ARGN" -ne 0 ]
do
case $1 in
-h) usage
exit 0
;;
-b) BRIEF=1
LEAFONLY=1
PRUNE=1
;;
-l) LEAFONLY=1
;;
-p) PRUNE=1
;;
-n) NO_HEAD=1
;;
-s) NORMALIZE_SOLIDUS=1
;;
?*) echo "ERROR: Unknown option."
usage
exit 0
;;
esac
shift 1
ARGN=$((ARGN-1))
done
}
awk_egrep () {
local pattern_string=$1
local pattern_string=$1
gawk '{
while ($0) {
start=match($0, pattern);
token=substr($0, start, RLENGTH);
print token;
$0=substr($0, start+RLENGTH);
}
}' pattern="$pattern_string"
gawk '{
while ($0) {
start=match($0, pattern);
token=substr($0, start, RLENGTH);
print token;
$0=substr($0, start+RLENGTH);
}
}' pattern="$pattern_string"
}
tokenize () {
local GREP
local ESCAPE
local CHAR
local GREP
local ESCAPE
local CHAR
if echo "test string" | egrep -ao --color=never "test" >/dev/null 2>&1
then
GREP='egrep -ao --color=never'
else
GREP='egrep -ao'
fi
if echo "test string" | egrep -ao --color=never "test" >/dev/null 2>&1
then
GREP='egrep -ao --color=never'
else
GREP='egrep -ao'
fi
if echo "test string" | egrep -o "test" >/dev/null 2>&1
then
ESCAPE='(\\[^u[:cntrl:]]|\\u[0-9a-fA-F]{4})'
CHAR='[^[:cntrl:]"\\]'
else
GREP=awk_egrep
ESCAPE='(\\\\[^u[:cntrl:]]|\\u[0-9a-fA-F]{4})'
CHAR='[^[:cntrl:]"\\\\]'
fi
if echo "test string" | egrep -o "test" >/dev/null 2>&1
then
ESCAPE='(\\[^u[:cntrl:]]|\\u[0-9a-fA-F]{4})'
CHAR='[^[:cntrl:]"\\]'
else
GREP=awk_egrep
ESCAPE='(\\\\[^u[:cntrl:]]|\\u[0-9a-fA-F]{4})'
CHAR='[^[:cntrl:]"\\\\]'
fi
local STRING="\"$CHAR*($ESCAPE$CHAR*)*\""
local NUMBER='-?(0|[1-9][0-9]*)([.][0-9]*)?([eE][+-]?[0-9]*)?'
local KEYWORD='null|false|true'
local SPACE='[[:space:]]+'
local STRING="\"$CHAR*($ESCAPE$CHAR*)*\""
local NUMBER='-?(0|[1-9][0-9]*)([.][0-9]*)?([eE][+-]?[0-9]*)?'
local KEYWORD='null|false|true'
local SPACE='[[:space:]]+'
# Force zsh to expand $A into multiple words
local is_wordsplit_disabled=$(unsetopt 2>/dev/null | grep -c '^shwordsplit$')
if [ $is_wordsplit_disabled != 0 ]; then setopt shwordsplit; fi
$GREP "$STRING|$NUMBER|$KEYWORD|$SPACE|." | egrep -v "^$SPACE$"
if [ $is_wordsplit_disabled != 0 ]; then unsetopt shwordsplit; fi
# Force zsh to expand $A into multiple words
local is_wordsplit_disabled=$(unsetopt 2>/dev/null | grep -c '^shwordsplit$')
if [ $is_wordsplit_disabled != 0 ]; then setopt shwordsplit; fi
$GREP "$STRING|$NUMBER|$KEYWORD|$SPACE|." | egrep -v "^$SPACE$"
if [ $is_wordsplit_disabled != 0 ]; then unsetopt shwordsplit; fi
}
parse_array () {
local index=0
local ary=''
read -r token
case "$token" in
']') ;;
*)
while :
do
parse_value "$1" "$index"
index=$((index+1))
ary="$ary""$value"
read -r token
case "$token" in
']') break ;;
',') ary="$ary," ;;
*) throw "EXPECTED , or ] GOT ${token:-EOF}" ;;
esac
read -r token
done
;;
esac
[ "$BRIEF" -eq 0 ] && value=$(printf '[%s]' "$ary") || value=
:
local index=0
local ary=''
read -r token
case "$token" in
']') ;;
*)
while :
do
parse_value "$1" "$index"
index=$((index+1))
ary="$ary""$value"
read -r token
case "$token" in
']') break ;;
',') ary="$ary," ;;
*) throw "EXPECTED , or ] GOT ${token:-EOF}" ;;
esac
read -r token
done
;;
esac
[ "$BRIEF" -eq 0 ] && value=$(printf '[%s]' "$ary") || value=
:
}
parse_object () {
local key
local obj=''
read -r token
case "$token" in
'}') ;;
*)
while :
do
case "$token" in
'"'*'"') key=$token ;;
*) throw "EXPECTED string GOT ${token:-EOF}" ;;
esac
read -r token
case "$token" in
':') ;;
*) throw "EXPECTED : GOT ${token:-EOF}" ;;
esac
read -r token
parse_value "$1" "$key"
obj="$obj$key:$value"
read -r token
case "$token" in
'}') break ;;
',') obj="$obj," ;;
*) throw "EXPECTED , or } GOT ${token:-EOF}" ;;
esac
read -r token
done
;;
esac
[ "$BRIEF" -eq 0 ] && value=$(printf '{%s}' "$obj") || value=
:
local key
local obj=''
read -r token
case "$token" in
'}') ;;
*)
while :
do
case "$token" in
'"'*'"') key=$token ;;
*) throw "EXPECTED string GOT ${token:-EOF}" ;;
esac
read -r token
case "$token" in
':') ;;
*) throw "EXPECTED : GOT ${token:-EOF}" ;;
esac
read -r token
parse_value "$1" "$key"
obj="$obj$key:$value"
read -r token
case "$token" in
'}') break ;;
',') obj="$obj," ;;
*) throw "EXPECTED , or } GOT ${token:-EOF}" ;;
esac
read -r token
done
;;
esac
[ "$BRIEF" -eq 0 ] && value=$(printf '{%s}' "$obj") || value=
:
}
parse_value () {
local jpath="${1:+$1,}$2" isleaf=0 isempty=0 print=0
case "$token" in
'{') parse_object "$jpath" ;;
'[') parse_array "$jpath" ;;
# At this point, the only valid single-character tokens are digits.
''|[!0-9]) throw "EXPECTED value GOT ${token:-EOF}" ;;
*) value=$token
# if asked, replace solidus ("\/") in json strings with normalized value: "/"
[ "$NORMALIZE_SOLIDUS" -eq 1 ] && value=$(echo "$value" | sed 's#\\/#/#g')
isleaf=1
[ "$value" = '""' ] && isempty=1
;;
esac
[ "$value" = '' ] && return
[ "$NO_HEAD" -eq 1 ] && [ -z "$jpath" ] && return
local jpath="${1:+$1,}$2" isleaf=0 isempty=0 print=0
case "$token" in
'{') parse_object "$jpath" ;;
'[') parse_array "$jpath" ;;
# At this point, the only valid single-character tokens are digits.
''|[!0-9]) throw "EXPECTED value GOT ${token:-EOF}" ;;
*) value=$token
# if asked, replace solidus ("\/") in json strings with normalized value: "/"
[ "$NORMALIZE_SOLIDUS" -eq 1 ] && value=$(echo "$value" | sed 's#\\/#/#g')
isleaf=1
[ "$value" = '""' ] && isempty=1
;;
esac
[ "$value" = '' ] && return
[ "$NO_HEAD" -eq 1 ] && [ -z "$jpath" ] && return
[ "$LEAFONLY" -eq 0 ] && [ "$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 1 ] && [ "$isleaf" -eq 1 ] && \
[ $PRUNE -eq 1 ] && [ $isempty -eq 0 ] && print=1
[ "$print" -eq 1 ] && printf "[%s]\t%s\n" "$jpath" "$value"
:
[ "$LEAFONLY" -eq 0 ] && [ "$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 1 ] && [ "$isleaf" -eq 1 ] && \
[ $PRUNE -eq 1 ] && [ $isempty -eq 0 ] && print=1
[ "$print" -eq 1 ] && printf "[%s]\t%s\n" "$jpath" "$value"
:
}
parse () {
read -r token
parse_value
read -r token
case "$token" in
'') ;;
*) throw "EXPECTED EOF GOT $token" ;;
esac
read -r token
parse_value
read -r token
case "$token" in
'') ;;
*) throw "EXPECTED EOF GOT $token" ;;
esac
}
if ([ "$0" = "$BASH_SOURCE" ] || ! [ -n "$BASH_SOURCE" ]);
then
parse_options "$@"
tokenize | parse
parse_options "$@"
tokenize | parse
fi
# 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; }
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
zaf_wrn "Sudo needed bud not installed?"
fi
@ -188,7 +188,7 @@ zaf_ctrl_cron() {
pdir="$3"
plugin=$1
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; }
zaf_dbg "Installing cron entry $ZAF_CROND/zaf_$plugin"
[ -z "$cron" ] && return # Nothing to install
@ -212,12 +212,12 @@ zaf_ctrl_install() {
binaries=$(zaf_ctrl_get_global_option $2 "Install-bin")
for b in $binaries; do
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
files=$(zaf_ctrl_get_global_option $2 "Install-files")
for f in $files; do
zaf_fetch_url "$1/$b" >"${ZAF_TMP_DIR}/$b"
zaf_install "${ZAF_TMP_DIR}/$b" "$pdir"
zaf_install "${ZAF_TMP_DIR}/$b" "$pdir"
done
true
) || zaf_err "Error during zaf_ctrl_install"
@ -244,9 +244,9 @@ zaf_ctrl_generate_items_cfg() {
tmpfile=$ZAF_TMP_DIR/gencfg$$
(set -e
for i in $items; do
iscript=$(zaf_stripctrl $i)
zaf_ctrl_get_item_option $1 $i "Parameters" >$tmpfile
if [ -s "$tmpfile" ]; then
iscript=$(zaf_stripctrl $i)
zaf_ctrl_get_item_option $1 $i "Parameters" >$tmpfile
if [ -s "$tmpfile" ]; then
ikey="$2.$i[*]"
args=""
apos=1;
@ -255,37 +255,37 @@ zaf_ctrl_generate_items_cfg() {
args="$args \$$apos"
apos=$(expr $apos + 1)
done <$tmpfile
else
else
ikey="$2.$i"
zafparams=""
args=""
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; "
lock=$(zaf_ctrl_get_item_option $1 $i "Lock")
if [ -n "$lock" ]; then
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; "
lock=$(zaf_ctrl_get_item_option $1 $i "Lock")
if [ -n "$lock" ]; then
lock="${ZAF_LIB_DIR}/zaflock $lock "
fi
cache=$(zaf_ctrl_get_item_option $1 $i "Cache")
if [ -n "$cache" ]; then
fi
cache=$(zaf_ctrl_get_item_option $1 $i "Cache")
if [ -n "$cache" ]; then
cache="${ZAF_LIB_DIR}/zafcache '$cache' "
fi
cmd=$(zaf_ctrl_get_item_option $1 $i "Cmd")
if [ -n "$cmd" ]; then
printf "%s" "UserParameter=$ikey,${env}${zafparams}${preload}${cache}${lock}${cmd}"; echo
continue
fi
cmd=$(zaf_ctrl_get_item_option $1 $i "Script")
if [ -n "$cmd" ]; then
( echo "#!/bin/sh"
echo ". $ZAF_LIB_DIR/preload.sh; "
zaf_ctrl_get_item_option $1 $i "Script"
) >${ZAF_TMP_DIR}/${iscript}.sh;
[ -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
fi
cmd=$(zaf_ctrl_get_item_option $1 $i "Cmd")
if [ -n "$cmd" ]; then
printf "%s" "UserParameter=$ikey,${env}${zafparams}${preload}${cache}${lock}${cmd}"; echo
continue
fi
cmd=$(zaf_ctrl_get_item_option $1 $i "Script")
if [ -n "$cmd" ]; then
( echo "#!/bin/sh"
echo ". $ZAF_LIB_DIR/preload.sh; "
zaf_ctrl_get_item_option $1 $i "Script"
) >${ZAF_TMP_DIR}/${iscript}.sh;
[ -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
rm -f ${ZAF_TMP_DIR}/${iscript}.sh
continue;
fi
zaf_err "Item $i declared in control file but has no Cmd, Function or Script!"
continue;
fi
zaf_err "Item $i declared in control file but has no Cmd, Function or Script!"
done
) || zaf_err "Error during zaf_ctrl_generate_items_cfg"
rm -f $tmpfile
@ -312,10 +312,10 @@ zaf_ctrl_generate_extitems_cfg() {
tmpfile=$ZAF_TMP_DIR/gencfg$$
(set -e
for i in $items; do
iscript=$(zaf_stripctrl $i)
zaf_ctrl_get_extitem_option $1 $i "Parameters" >$tmpfile
ikey="$2.$i"
if [ -s "$tmpfile" ]; then
iscript=$(zaf_stripctrl $i)
zaf_ctrl_get_extitem_option $1 $i "Parameters" >$tmpfile
ikey="$2.$i"
if [ -s "$tmpfile" ]; then
args=""
apos=1;
while read pname pdefault pregex prest; do
@ -323,34 +323,34 @@ zaf_ctrl_generate_extitems_cfg() {
args="$args \$$apos"
apos=$(expr $apos + 1)
done <$tmpfile
else
else
zafparams=""
args=""
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; "
lock=$(zaf_ctrl_get_extitem_option $1 $i "Lock")
if [ -n "$lock" ]; then
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; "
lock=$(zaf_ctrl_get_extitem_option $1 $i "Lock")
if [ -n "$lock" ]; then
lock="${ZAF_LIB_DIR}/zaflock $lock "
fi
cache=$(zaf_ctrl_get_extitem_option $1 $i "Cache")
if [ -n "$cache" ]; then
fi
cache=$(zaf_ctrl_get_extitem_option $1 $i "Cache")
if [ -n "$cache" ]; then
cache="${ZAF_LIB_DIR}/zafcache '$cache' "
fi
cmd=$(zaf_ctrl_get_extitem_option $1 $i "Cmd")
if [ -n "$cmd" ]; then
fi
cmd=$(zaf_ctrl_get_extitem_option $1 $i "Cmd")
if [ -n "$cmd" ]; then
echo "#!/bin/sh" >${ZAF_SERVER_EXTSCRIPTS}/$ikey
chmod +x ${ZAF_SERVER_EXTSCRIPTS}/$ikey
(printf "%s" "${env}${zafparams}${preload}${cache}${lock}${cmd}"; echo) >>${ZAF_SERVER_EXTSCRIPTS}/$ikey
continue
fi
cmd=$(zaf_ctrl_get_extitem_option $1 $i "Script")
if [ -n "$cmd" ]; then
echo "#!/bin/sh" >${ZAF_SERVER_EXTSCRIPTS}/$ikey
(printf "%s" "${env}${zafparams}${preload}${cache}${lock}${cmd}"; echo) >>${ZAF_SERVER_EXTSCRIPTS}/$ikey
continue
fi
cmd=$(zaf_ctrl_get_extitem_option $1 $i "Script")
if [ -n "$cmd" ]; then
echo "#!/bin/sh" >${ZAF_SERVER_EXTSCRIPTS}/$ikey
chmod +x ${ZAF_SERVER_EXTSCRIPTS}/$ikey
(printf "%s" "${env}${zafparams}${preload}${cache}${lock}${cmd}"; echo) >>${ZAF_SERVER_EXTSCRIPTS}/$ikey
continue;
fi
zaf_err "External item $i declared in control file but has no Cmd, Function or Script!"
(printf "%s" "${env}${zafparams}${preload}${cache}${lock}${cmd}"; echo) >>${ZAF_SERVER_EXTSCRIPTS}/$ikey
continue;
fi
zaf_err "External item $i declared in control file but has no Cmd, Function or Script!"
done
) || zaf_err "Error during zaf_ctrl_generate_extitems_cfg"
rm -f $tmpfile

View File

@ -1,23 +1,23 @@
# Os related functions
zaf_preconfigure_os_openwrt() {
ZAF_AGENT_RESTART="/etc/init.d/zabbix_agentd restart"
ZAF_AGENT_CONFIGD="/var/run/zabbix_agentd.conf.d/"
ZAF_AGENT_CONFIG="/etc/zabbix_agentd.conf"
ZAF_AGENT_PKG="zabbix-agentd"
ZAF_CURL_INSECURE=1
ZAF_AGENT_RESTART="/etc/init.d/zabbix_agentd restart"
ZAF_AGENT_CONFIGD="/var/run/zabbix_agentd.conf.d/"
ZAF_AGENT_CONFIG="/etc/zabbix_agentd.conf"
ZAF_AGENT_PKG="zabbix-agentd"
ZAF_CURL_INSECURE=1
}
zaf_preconfigure_os_beesip() {
zaf_preconfigure_os_openwrt
zaf_preconfigure_os_openwrt
}
zaf_preconfigure_os_freebsd() {
ZAF_AGENT_PKG="zabbix3-agent"
ZAF_AGENT_CONFIG="/usr/local/etc/zabbix3/zabbix_agentd.conf"
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_AGENT_PKG="zabbix3-agent"
ZAF_AGENT_CONFIG="/usr/local/etc/zabbix3/zabbix_agentd.conf"
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_postconfigure_os_openwrt() {
@ -75,9 +75,9 @@ zaf_detect_system() {
ZAF_OS="unknown"
ZAF_OS_CODENAME="unknown"
ZAF_AGENT_PKG=""
return
fi
fi
return
fi
fi
fi
fi
}
@ -86,72 +86,72 @@ zaf_detect_system() {
# $1 - name of the function.
# all variants will be tested. (name_os_codename, name_os, name_{dpkg|opkg|rpm}, name )
zaf_os_specific(){
local func="$1"
local func="$1"
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"
else if type "${func}_${ZAF_OS}" >/dev/null 2>/dev/null; then
eval "${func}_${ZAF_OS} $2 $3 $4 $5 $6"
else if type "${func}_${ZAF_PKG}" >/dev/null 2>/dev/null; then
eval "${func}_${ZAF_PKG} $2 $3 $4 $5 $6"
else
zaf_dbg "No OS/packager specific implementation for $1"
fi
fi
fi
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"
else if type "${func}_${ZAF_OS}" >/dev/null 2>/dev/null; then
eval "${func}_${ZAF_OS} $2 $3 $4 $5 $6"
else if type "${func}_${ZAF_PKG}" >/dev/null 2>/dev/null; then
eval "${func}_${ZAF_PKG} $2 $3 $4 $5 $6"
else
zaf_dbg "No OS/packager specific implementation for $1"
fi
fi
fi
}
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
# $1 - src file
# $2 - directory
zaf_install(){
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_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)!"
}
# $1 - src file
# $2 - directory
zaf_install_bin(){
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 chmod +x "$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 chmod +x "$INSTALL_PREFIX/$2/$(basename $1)"
}
# $1 - directory
zaf_install_dir(){
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_dbg "Install directory $1 to $INSTALL_PREFIX/$1"
$ZAF_DO mkdir -p "$INSTALL_PREFIX/$1" || zaf_err "Cannot create directory $INSTALL_PREFIX/$1!"
}
# $1 - file
zaf_touch(){
zaf_dbg "Touch $INSTALL_PREFIX/$1"
$ZAF_DO touch "$INSTALL_PREFIX/$1"
zaf_dbg "Touch $INSTALL_PREFIX/$1"
$ZAF_DO touch "$INSTALL_PREFIX/$1"
}
# $1 - directory
zaf_uninstall(){
if [ -n "$INSTALL_PREFIX" ]; then
zaf_dbg "Removing $INSTALL_PREFIX/$1"
if [ -n "$INSTALL_PREFIX" ]; then
zaf_dbg "Removing $INSTALL_PREFIX/$1"
$ZAF_DO rm -rf "$INSTALL_PREFIX/$1"
else
else
zaf_dbg "Removing $1"
$ZAF_DO rm -rf "$1"
fi
fi
}
# Automaticaly install agent on debian
# For another os, create similar function (install_zabbix_centos)
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" \
&& apt-get update \
&& apt-get install -y -q $ZAF_AGENT_PKG
}
zaf_install_agent_opkg() {
opkg update && \
opkg install $ZAF_AGENT_PKG
opkg update && \
opkg install $ZAF_AGENT_PKG
}
# Check if dpkg dependency is met

View File

@ -19,7 +19,7 @@ zaf_get_plugin_url() {
local url
if [ "$(zaf_url_info $1)" = "path" ]; then
url="$1" # plugin with path - from directory
url="$1" # plugin with path - from directory
else
if [ "$(zaf_url_info $1)" = "url" ]; then
url="$1" # plugin with http[s] url
@ -118,7 +118,7 @@ zaf_install_plugin() {
zaf_ctrl_sudo "$plugin" "${control}" "${plugindir}"
zaf_ctrl_cron "$plugin" "${control}" "${plugindir}"
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_ctrl_generate_extitems_cfg "${control}" "${plugin}"
else
@ -146,7 +146,7 @@ zaf_postinstall_plugin() {
zaf_ctrl_sudo "$plugin" "${control}" "${plugindir}"
zaf_ctrl_cron "$plugin" "${control}" "${plugindir}"
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_ctrl_generate_extitems_cfg "${control}" "${plugin}"
}
@ -306,7 +306,7 @@ zaf_test_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)"
eval $cmd
}

View File

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

View File

@ -10,7 +10,7 @@ zaf_zbxapi_do() {
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
if [ $? = 0 ] && $ZAF_LIB_DIR/JSON.sh -b <$tmpfile | grep -q '"result"'; then
zaf_trc "API OK"
cat $tmpfile
rm -f $tmpfile
@ -52,7 +52,7 @@ zaf_zbxapi_getstring() {
# Extract value from JSON response result
# $1 key
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
@ -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."
authstr='{
"method" : "user.login",
"params" : {
"password" : "'$ZAF_ZBXAPI_PASS'",
"user" : "'$ZAF_ZBXAPI_USER'"
},
"id" : 0,
"jsonrpc" : "2.0"
"method" : "user.login",
"params" : {
"password" : "'$ZAF_ZBXAPI_PASS'",
"user" : "'$ZAF_ZBXAPI_USER'"
},
"id" : 0,
"jsonrpc" : "2.0"
}'
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
result=$(zaf_zbxapi_do_cache "$authstr")
ZAF_ZBXAPI_AUTH=$(echo $result |zaf_zbxapi_getstring)
@ -122,9 +122,9 @@ zaf_zbxapi_get_object() {
zaf_zbxapi_gethostgroupid() {
local result
result=$(zaf_zbxapi_get_object "hostgroup" '"name": ["'$1'"]')
[ -z "$result" ] && zaf_err "HostGroup $1 not found!"
echo $result |zaf_zbxapi_getvalues groupid
result=$(zaf_zbxapi_get_object "hostgroup" '"name": ["'$1'"]')
[ -z "$result" ] && zaf_err "HostGroup $1 not found!"
echo $result |zaf_zbxapi_getvalues groupid
}
# $1 hostid
@ -132,10 +132,10 @@ zaf_zbxapi_gethostgroupid() {
zaf_zbxapi_gethost() {
local result
result=$(zaf_zbxapi_get_object "host" '' '"hostids": ["'$1'"]' 'extend')
[ -z "$result" ] && zaf_err "Hostid $1 not found!"
if [ -z "$2" ]; then
echo $result
result=$(zaf_zbxapi_get_object "host" '' '"hostids": ["'$1'"]' 'extend')
[ -z "$result" ] && zaf_err "Hostid $1 not found!"
if [ -z "$2" ]; then
echo $result
else
echo $result |zaf_zbxapi_getvalues $2
fi
@ -145,9 +145,9 @@ zaf_zbxapi_gethost() {
zaf_zbxapi_gethostid() {
local result
result=$(zaf_zbxapi_get_object "host" '"host": ["'$1'"]')
[ -z "$result" ] && zaf_err "Host $1 not found!"
echo $result |zaf_zbxapi_getvalues hostid
result=$(zaf_zbxapi_get_object "host" '"host": ["'$1'"]')
[ -z "$result" ] && zaf_err "Host $1 not found!"
echo $result |zaf_zbxapi_getvalues hostid
}
# $1 hostname
@ -155,10 +155,10 @@ zaf_zbxapi_gethostid() {
zaf_zbxapi_gethostinventory() {
local result
result=$(zaf_zbxapi_get_object "host" '"host": ["'$1'"]' '"withInventory": "true", "selectInventory": "extend"')
[ -z "$result" ] && zaf_err "Host $1 not found!"
result=$(zaf_zbxapi_get_object "host" '"host": ["'$1'"]' '"withInventory": "true", "selectInventory": "extend"')
[ -z "$result" ] && zaf_err "Host $1 not found!"
if [ -z "$2" ]; then
echo $result
echo $result
else
echo $result |zaf_zbxapi_getvalues $2
fi
@ -168,9 +168,9 @@ zaf_zbxapi_gethostinventory() {
zaf_zbxapi_gettemplateid() {
local result
result=$(zaf_zbxapi_get_object "template" '"host": ["'$1'"]')
[ -z "$result" ] && zaf_err "Template $1 not found!"
echo $result |zaf_zbxapi_getvalues templateid
result=$(zaf_zbxapi_get_object "template" '"host": ["'$1'"]')
[ -z "$result" ] && zaf_err "Template $1 not found!"
echo $result |zaf_zbxapi_getvalues templateid
}
# $1 templateid
@ -178,10 +178,10 @@ zaf_zbxapi_gettemplateid() {
zaf_zbxapi_gettemplate() {
local result
result=$(zaf_zbxapi_get_object "template" '' '"templateids": ["'$1'"]' 'extend')
[ -z "$result" ] && zaf_err "Templateid $1 not found!"
if [ -z "$2" ]; then
echo $result
result=$(zaf_zbxapi_get_object "template" '' '"templateids": ["'$1'"]' 'extend')
[ -z "$result" ] && zaf_err "Templateid $1 not found!"
if [ -z "$2" ]; then
echo $result
else
echo $result |zaf_zbxapi_getvalues $2
fi
@ -191,34 +191,34 @@ zaf_zbxapi_gettemplate() {
zaf_zbxapi_gethostsingroup() {
local result
result=$(zaf_zbxapi_get_object "host" '' '"groupids": ["'$1'"]')
[ -z "$result" ] && zaf_wrn "No hosts in groupid '$1'"
echo $result | zaf_zbxapi_getvalues "hostid"
result=$(zaf_zbxapi_get_object "host" '' '"groupids": ["'$1'"]')
[ -z "$result" ] && zaf_wrn "No hosts in groupid '$1'"
echo $result | zaf_zbxapi_getvalues "hostid"
}
# Get all hostids in system
zaf_zbxapi_gethostids() {
local result
result=$(zaf_zbxapi_get_object "host")
echo $result | zaf_zbxapi_getvalues "hostid"
result=$(zaf_zbxapi_get_object "host")
echo $result | zaf_zbxapi_getvalues "hostid"
}
# Get all templateids in system
zaf_zbxapi_gettemplateids() {
local result
result=$(zaf_zbxapi_get_object "template")
echo $result | zaf_zbxapi_getvalues "templateid"
result=$(zaf_zbxapi_get_object "template")
echo $result | zaf_zbxapi_getvalues "templateid"
}
# $1 hostgroupid
zaf_zbxapi_gettemplatesingroup() {
local result
result=$(zaf_zbxapi_get_object "template" '' '"groupids": ["'$1'"]')
[ -z "$result" ] && zaf_wrn "No templates in groupid '$1'"
echo $result | zaf_zbxapi_getvalues "templateid"
result=$(zaf_zbxapi_get_object "template" '' '"groupids": ["'$1'"]')
[ -z "$result" ] && zaf_wrn "No templates in groupid '$1'"
echo $result | zaf_zbxapi_getvalues "templateid"
}
# $1 map or null for all
@ -226,12 +226,12 @@ zaf_zbxapi_getmapid() {
local result
if [ -n "$1" ]; then
result=$(zaf_zbxapi_get_object "map" '"name": ["'$1'"]')
result=$(zaf_zbxapi_get_object "map" '"name": ["'$1'"]')
else
result=$(zaf_zbxapi_get_object "map")
result=$(zaf_zbxapi_get_object "map")
fi
[ -z "$result" ] && zaf_err "Map $1 not found"
echo $result | zaf_zbxapi_getvalues "sysmapid"
[ -z "$result" ] && zaf_err "Map $1 not found"
echo $result | zaf_zbxapi_getvalues "sysmapid"
}
# $1 mapid
@ -239,10 +239,10 @@ zaf_zbxapi_getmapid() {
zaf_zbxapi_getmap() {
local result
result=$(zaf_zbxapi_get_object "map" '' '"sysmapids": ["'$1'"]' 'extend')
[ -z "$result" ] && zaf_err "Mapid $1 not found"
if [ -z "$2" ]; then
echo $result
result=$(zaf_zbxapi_get_object "map" '' '"sysmapids": ["'$1'"]' 'extend')
[ -z "$result" ] && zaf_err "Mapid $1 not found"
if [ -z "$2" ]; then
echo $result
else
echo $result |zaf_zbxapi_getvalues $2
fi
@ -261,18 +261,18 @@ zaf_zbxapi_export_object(){
bkpstr='
{
"method": "configuration.export",
"jsonrpc": "2.0",
"params": {
"options": {
"'$obj'": [
"'$id'"
]
},
"format": "xml"
},
"auth": "'$ZAF_ZBXAPI_AUTH'",
"id": 1
"method": "configuration.export",
"jsonrpc": "2.0",
"params": {
"options": {
"'$obj'": [
"'$id'"
]
},
"format": "xml"
},
"auth": "'$ZAF_ZBXAPI_AUTH'",
"id": 1
}'
zaf_zbxapi_do_cache "$bkpstr" | zaf_zbxapi_getxml
}
@ -303,67 +303,67 @@ zaf_zbxapi_import_config(){
local impstr
if [ -z "$1" ]; then
xmlstr=$(zaf_strescape '"')
xmlstr=$(zaf_strescape '"')
else
! [ -f "$1" ] && return 1
xmlstr=$(zaf_strescape '"\n\r' <$1)
! [ -f "$1" ] && return 1
xmlstr=$(zaf_strescape '"\n\r' <$1)
fi
impstr='
{
"method": "configuration.import",
"jsonrpc": "2.0",
"params": {
"format": "xml",
"rules": {
"applications": {
"createMissing": true,
"updateExisting": true
},
"discoveryRules": {
"createMissing": true,
"updateExisting": true
},
"graphs": {
"createMissing": true,
"updateExisting": true
},
"hosts": {
"createMissing": true,
"updateExisting": true
},
"items": {
"createMissing": true,
"updateExisting": true
},
"templates": {
"createMissing": true,
"updateExisting": true
},
"triggers": {
"createMissing": true,
"updateExisting": true
},
"maps": {
"createMissing": true,
"updateExisting": true
},
"screens": {
"createMissing": true,
"updateExisting": true
},
"items": {
"createMissing": true,
"updateExisting": true
},
"valueMaps": {
"createMissing": true,
"updateExisting": true
}
},
"method": "configuration.import",
"jsonrpc": "2.0",
"params": {
"format": "xml",
"rules": {
"applications": {
"createMissing": true,
"updateExisting": true
},
"discoveryRules": {
"createMissing": true,
"updateExisting": true
},
"graphs": {
"createMissing": true,
"updateExisting": true
},
"hosts": {
"createMissing": true,
"updateExisting": true
},
"items": {
"createMissing": true,
"updateExisting": true
},
"templates": {
"createMissing": true,
"updateExisting": true
},
"triggers": {
"createMissing": true,
"updateExisting": true
},
"maps": {
"createMissing": true,
"updateExisting": true
},
"screens": {
"createMissing": true,
"updateExisting": true
},
"items": {
"createMissing": true,
"updateExisting": true
},
"valueMaps": {
"createMissing": true,
"updateExisting": true
}
},
"source": "'$xmlstr'"
},
"auth": "'$ZAF_ZBXAPI_AUTH'",
"id": 3
},
"auth": "'$ZAF_ZBXAPI_AUTH'",
"id": 3
}'
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
for pair in "$@"; do
if echo $pair | grep -qE '^ZAF\_(.*)='; then
option=$(echo $pair|cut -d '=' -f 1)
value=$(echo $pair|cut -d '=' -f 2-)
eval "${option}='$value'"
if echo $pair | grep -qE '^ZAF\_(.*)='; then
option=$(echo $pair|cut -d '=' -f 1)
value=$(echo $pair|cut -d '=' -f 2-)
eval "${option}='$value'"
export secondstage=1
else
else
params="$params $pair"
fi
fi
done
export $(set |grep ^ZAF_ | cut -d '=' -f 1)
# 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
control=$plugindir/control.zaf
zaf_ctrl_generate_items_cfg "${control}" "${plugin}" "noscripts" \
| zaf_far '{PLUGINDIR}' "${plugindir}"
| zaf_far '{PLUGINDIR}' "${plugindir}"
done
;;
agent-config)
@ -102,10 +102,10 @@ agent-config)
for plugin in $(zaf_list_plugins); do
plugindir=$ZAF_PLUGINS_DIR/$plugin
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_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
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."
. /etc/zaf.conf
if [ "$1" = "force" ]; then
rm -rf /etc/zaf.conf ${ZAF_PLUGINS_DIR} ${ZAF_REPO_DIR} ${ZAF_LIB_DIR} \
${ZAF_BIN_DIR}/zaf ${ZAF_AGENT_CONFIGD}/zaf_*
rm -rf /etc/zaf.conf ${ZAF_PLUGINS_DIR} ${ZAF_REPO_DIR} ${ZAF_LIB_DIR} \
${ZAF_BIN_DIR}/zaf ${ZAF_AGENT_CONFIGD}/zaf_*
else
echo "This will remove zaf from this computer and erase all configuration."
echo "This command will be executed:"
echo "rm -rf /etc/zaf.conf ${ZAF_PLUGINS_DIR} ${ZAF_REPO_DIR} ${ZAF_LIB_DIR} \
echo "This will remove zaf from this computer and erase all configuration."
echo "This command will be executed:"
echo "rm -rf /etc/zaf.conf ${ZAF_PLUGINS_DIR} ${ZAF_REPO_DIR} ${ZAF_LIB_DIR} \
${ZAF_BIN_DIR}/zaf ${ZAF_AGENT_CONFIGD}/zaf_*"
echo
echo "To continue, please do $0 self-remove force"
echo
echo "To continue, please do $0 self-remove force"
fi
;;
;;
###### API related commands
api)
zaf_zbxapi_login
case $2 in
get-*-id)
obj=$(echo $2|cut -d '-' -f 2)
obj=$(echo $2|cut -d '-' -f 2)
[ -z "$3" ] && zaf_err "$0 $1 $2 <$obj>"
eval zaf_zbxapi_get${obj}id "$3"
;;