diff --git a/lib/zbxapi.lib.sh b/lib/zbxapi.lib.sh index 1b868e4..50c013a 100644 --- a/lib/zbxapi.lib.sh +++ b/lib/zbxapi.lib.sh @@ -116,6 +116,20 @@ zaf_zbxapi_gethostgroupid() { 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 zaf_zbxapi_gethostid() { local result @@ -125,6 +139,20 @@ zaf_zbxapi_gethostid() { 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 zaf_zbxapi_gettemplateid() { local result @@ -134,22 +162,18 @@ zaf_zbxapi_gettemplateid() { 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 +# $2 property or null for all zaf_zbxapi_gettemplate() { local result result=$(zaf_zbxapi_get_object "template" '' '"templateids": ["'$1'"]' 'extend') [ -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 @@ -200,12 +224,17 @@ zaf_zbxapi_getmapid() { } # $1 mapid +# $2 property or null for all zaf_zbxapi_getmap() { local result result=$(zaf_zbxapi_get_object "map" '' '"sysmapids": ["'$1'"]' 'extend') [ -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 diff --git a/zaf b/zaf index 1796368..589e9da 100755 --- a/zaf +++ b/zaf @@ -260,17 +260,17 @@ self-remove) api) zaf_zbxapi_login case $2 in - *-id) - obj=$(echo $2|cut -d '-' -f 1) + get-*-id) + obj=$(echo $2|cut -d '-' -f 2) [ -z "$3" ] && zaf_err "$0 $1 $2 <$obj>" eval zaf_zbxapi_get${obj}id "$3" ;; - id-*) - obj=$(echo $2|cut -d '-' -f 2) + get-byid-*|get-id-*) + obj=$(echo $2|cut -d '-' -f 3) [ -z "$3" ] && zaf_err "$0 $1 $2 " - eval zaf_zbxapi_get${obj} "$3" + eval zaf_zbxapi_get${obj} "$3" "$4" ;; - host-ids) + get-host-ids) if [ -n "$3" ]; then gid=$(zaf_zbxapi_gethostgroupid "$3") || exit 1 zaf_dbg "Selecting all hosts in group $3($gid)" @@ -280,7 +280,11 @@ api) zaf_zbxapi_gethostids fi ;; - template-ids) + get-inventory) + [ -z "$3" ] && zaf_err "$0 $1 [field]" + zaf_zbxapi_gethostinventory $3 $4 + ;; + get-template-ids) if [ -n "$3" ]; then gid=$(zaf_zbxapi_gethostgroupid "$3") || exit 1 zaf_dbg "Selecting all templates in group $3($gid)" @@ -290,7 +294,7 @@ api) zaf_zbxapi_gettemplateids fi ;; - map-ids) + get-map-ids) zaf_zbxapi_getmapid ;; export-hosts) @@ -384,10 +388,10 @@ api) *) echo "$0 api command [parameters]" for i in host template map; do - zaf_hlp "${i}-id $i" "Get $i id" - zaf_hlp "id-${i} id" "Get $i name from id" + zaf_hlp "get-${i}-id $i" "Get $i id" + zaf_hlp "get-byid-${i} id [property]" "Get $i property from id. Leave empty property for JSON" 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-host host" "Backup host (get config from zabbix to stdout)" zaf_hlp "import-template {plugin|file}" "Import template for plugin or from file"