mirror of
https://github.com/limosek/zaf.git
synced 2025-11-01 18:17:37 +01:00
Make scripts nicer by unexpand
This commit is contained in:
324
lib/JSON.sh
324
lib/JSON.sh
@@ -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
|
||||
|
||||
114
lib/ctrl.lib.sh
114
lib/ctrl.lib.sh
@@ -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
|
||||
|
||||
@@ -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"
|
||||
|
||||
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
|
||||
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
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
134
lib/zaf.lib.sh
134
lib/zaf.lib.sh
@@ -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
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user