1
0
mirror of https://github.com/limosek/zaf.git synced 2024-11-22 02:19:06 +01:00

API is working now, help updated

This commit is contained in:
Lukas Macura 2016-04-18 10:24:55 +02:00
parent 83b4af050c
commit e097408377
2 changed files with 55 additions and 22 deletions

View File

@ -116,6 +116,20 @@ zaf_zbxapi_gethostgroupid() {
echo $result |zaf_zbxapi_getvalue groupid echo $result |zaf_zbxapi_getvalue groupid
} }
# $1 hostid
# $2 property or null for all
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
else
echo $result |zaf_zbxapi_getvalue $2
fi
}
# $1 hostname # $1 hostname
zaf_zbxapi_gethostid() { zaf_zbxapi_gethostid() {
local result local result
@ -125,6 +139,20 @@ zaf_zbxapi_gethostid() {
echo $result |zaf_zbxapi_getvalue hostid echo $result |zaf_zbxapi_getvalue hostid
} }
# $1 hostname
# $2 inv field or empty for json
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!"
if [ -z "$2" ]; then
echo $result
else
echo $result |zaf_zbxapi_getvalue $2
fi
}
# $1 hostname # $1 hostname
zaf_zbxapi_gettemplateid() { zaf_zbxapi_gettemplateid() {
local result local result
@ -134,22 +162,18 @@ zaf_zbxapi_gettemplateid() {
echo $result |zaf_zbxapi_getvalue templateid echo $result |zaf_zbxapi_getvalue templateid
} }
# $1 hostid
zaf_zbxapi_gethost() {
local result
result=$(zaf_zbxapi_get_object "host" '' '"hostids": ["'$1'"]' 'extend')
[ -z "$result" ] && zaf_err "Hostid $1 not found!"
echo $result | zaf_zbxapi_getvalue "host"
}
# $1 templateid # $1 templateid
# $2 property or null for all
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!"
echo $result if [ -z "$2" ]; then
echo $result
else
echo $result |zaf_zbxapi_getvalue $2
fi
} }
# $1 hostgroupid # $1 hostgroupid
@ -200,12 +224,17 @@ zaf_zbxapi_getmapid() {
} }
# $1 mapid # $1 mapid
# $2 property or null for all
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"
echo $result | zaf_zbxapi_getvalue "name" if [ -z "$2" ]; then
echo $result
else
echo $result |zaf_zbxapi_getvalue $2
fi
} }
# Object backup # Object backup

26
zaf
View File

@ -260,17 +260,17 @@ self-remove)
api) api)
zaf_zbxapi_login zaf_zbxapi_login
case $2 in case $2 in
*-id) get-*-id)
obj=$(echo $2|cut -d '-' -f 1) 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"
;; ;;
id-*) get-byid-*|get-id-*)
obj=$(echo $2|cut -d '-' -f 2) obj=$(echo $2|cut -d '-' -f 3)
[ -z "$3" ] && zaf_err "$0 $1 $2 <id>" [ -z "$3" ] && zaf_err "$0 $1 $2 <id>"
eval zaf_zbxapi_get${obj} "$3" eval zaf_zbxapi_get${obj} "$3" "$4"
;; ;;
host-ids) get-host-ids)
if [ -n "$3" ]; then if [ -n "$3" ]; then
gid=$(zaf_zbxapi_gethostgroupid "$3") || exit 1 gid=$(zaf_zbxapi_gethostgroupid "$3") || exit 1
zaf_dbg "Selecting all hosts in group $3($gid)" zaf_dbg "Selecting all hosts in group $3($gid)"
@ -280,7 +280,11 @@ api)
zaf_zbxapi_gethostids zaf_zbxapi_gethostids
fi fi
;; ;;
template-ids) get-inventory)
[ -z "$3" ] && zaf_err "$0 $1 <host> [field]"
zaf_zbxapi_gethostinventory $3 $4
;;
get-template-ids)
if [ -n "$3" ]; then if [ -n "$3" ]; then
gid=$(zaf_zbxapi_gethostgroupid "$3") || exit 1 gid=$(zaf_zbxapi_gethostgroupid "$3") || exit 1
zaf_dbg "Selecting all templates in group $3($gid)" zaf_dbg "Selecting all templates in group $3($gid)"
@ -290,7 +294,7 @@ api)
zaf_zbxapi_gettemplateids zaf_zbxapi_gettemplateids
fi fi
;; ;;
map-ids) get-map-ids)
zaf_zbxapi_getmapid zaf_zbxapi_getmapid
;; ;;
export-hosts) export-hosts)
@ -384,10 +388,10 @@ api)
*) *)
echo "$0 api command [parameters]" echo "$0 api command [parameters]"
for i in host template map; do for i in host template map; do
zaf_hlp "${i}-id $i" "Get $i id" zaf_hlp "get-${i}-id $i" "Get $i id"
zaf_hlp "id-${i} id" "Get $i name from id" zaf_hlp "get-byid-${i} id [property]" "Get $i property from id. Leave empty property for JSON"
done done
zaf_hlp "hosts hostgroup" "Get hosts in group" zaf_hlp "get-inventory host [fields]" "Get inventory fields [or all fields]"
zaf_hlp "export-hosts dir [hg]" "Backup all hosts [in group hg] (get their config from zabbix and save to dir/hostname.xml)" zaf_hlp "export-hosts dir [hg]" "Backup all hosts [in group hg] (get their config from zabbix and save to dir/hostname.xml)"
zaf_hlp "export-host host" "Backup host (get config from zabbix to stdout)" zaf_hlp "export-host host" "Backup host (get config from zabbix to stdout)"
zaf_hlp "import-template {plugin|file}" "Import template for plugin or from file" zaf_hlp "import-template {plugin|file}" "Import template for plugin or from file"