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

Going forward to release 2.2

This commit is contained in:
Lukas Macura 2016-04-18 09:20:57 +02:00
parent 60009d71e6
commit 83b4af050c
3 changed files with 192 additions and 177 deletions

View File

@ -24,6 +24,20 @@ zaf_err() {
logger -s -t zaf "Exiting with error!" logger -s -t zaf "Exiting with error!"
exit 1 exit 1
} }
# Help option
# $1 - key
# $2 -
zaf_hlp() {
local kl
local dl
local cols
cols=$COLUMNS
[ -z "$cols" ] && cols=120
kl=$(expr $cols / 3)
dl=$(expr $cols - $kl)
printf %-${kl}s%-${dl}s%b "$1" "$2" "\n"
}
zaf_version(){ zaf_version(){
echo $ZAF_VERSION echo $ZAF_VERSION

View File

@ -73,168 +73,139 @@ zaf_zbxapi_login(){
zaf_dbg "Logged into zabbix API ($ZAF_ZBXAPI_AUTH)" zaf_dbg "Logged into zabbix API ($ZAF_ZBXAPI_AUTH)"
} }
# Get object from zabbix API
# $1 object_type
# $2 filter
# $3 params
# $4 output
# $5 id
zaf_zbxapi_get_object() {
local obj
local filter
local params
local str
local output
local id
local result
obj=$1
filter=$2
params=$3
output=$4
id=$5
[ -z "$id" ] && id=1
[ -n "$filter" ] && filter='"filter": {'$filter'},';
[ -z "$output" ] && output="shorten";
if [ -n "$params" ]; then
params='"params": {'$params', '$filter' "output":"'$output'"}';
else
params='"params": {'$filter' "output":"'$output'"}';
fi
str='{ "method": "'$obj'.get", "jsonrpc": "2.0", "auth": "'$ZAF_ZBXAPI_AUTH'",'$params', "id": "'$id'" }'
result=$(zaf_zbxapi_do_cache "$str" | zaf_zbxapi_getresult)
[ -z "$result" ] && zaf_dbg "API call result empty or error! ($str)"
echo $result
}
# $1 hostgroup name # $1 hostgroup name
zaf_zbxapi_gethostgroupid() { zaf_zbxapi_gethostgroupid() {
local hstr local result
local filter
local gfilter result=$(zaf_zbxapi_get_object "hostgroup" '"name": ["'$1'"]')
local result [ -z "$result" ] && zaf_err "HostGroup $1 not found!"
echo $result |zaf_zbxapi_getvalue groupid
hstr='{
"method": "hostgroup.get",
"jsonrpc": "2.0",
"auth": "'$ZAF_ZBXAPI_AUTH'",
"params": {
"filter": {
"name": ["'$1'"]
},
"output": "shorten"
},
"id": 1
}'
result=$(zaf_zbxapi_do_cache "$hstr" | zaf_zbxapi_getresult | tr ',' '\n' | cut -d '"' -f 4)
[ -z "$result" ] && zaf_err "HostGroup $1 not found!"
echo $result
} }
# $1 hostname # $1 hostname
zaf_zbxapi_gethostid() { zaf_zbxapi_gethostid() {
local hstr local result
local host
local groupid result=$(zaf_zbxapi_get_object "host" '"host": ["'$1'"]')
local filter [ -z "$result" ] && zaf_err "Host $1 not found!"
local gfilter echo $result |zaf_zbxapi_getvalue hostid
local result
host="$1"
if [ -n "$host" ] ; then
filter='"filter": { "host": [ "'$host'" ] },'
fi
hstr='{
"method": "host.get",
"jsonrpc": "2.0",
"auth": "'$ZAF_ZBXAPI_AUTH'",
"params": {
'$filter'
"output": "shorten"
},
"id": 1
}'
result=$(zaf_zbxapi_do_cache "$hstr" | zaf_zbxapi_getresult | tr ',' '\n' | cut -d '"' -f 4)
[ -z "$result" ] && zaf_err "Host $1 not found!"
echo $result
} }
# $1 hostname # $1 hostname
zaf_zbxapi_gettemplateid() { zaf_zbxapi_gettemplateid() {
local hstr local result
local host
local groupid result=$(zaf_zbxapi_get_object "template" '"host": ["'$1'"]')
local filter [ -z "$result" ] && zaf_err "Template $1 not found!"
local gfilter echo $result |zaf_zbxapi_getvalue templateid
local result
host="$1"
if [ -n "$host" ] ; then
filter='"filter": { "host": [ "'$host'" ] },'
fi
hstr='{
"method": "template.get",
"jsonrpc": "2.0",
"auth": "'$ZAF_ZBXAPI_AUTH'",
"params": {
'$filter'
"output": "shorten"
},
"id": 1
}'
result=$(zaf_zbxapi_do_cache "$hstr" | zaf_zbxapi_getresult | tr ',' '\n' | cut -d '"' -f 4)
[ -z "$result" ] && zaf_err "Template $1 not found!"
echo $result
} }
# $1 hostid # $1 hostid
zaf_zbxapi_gethost() { zaf_zbxapi_gethost() {
local hstr local result
local host
local groupid result=$(zaf_zbxapi_get_object "host" '' '"hostids": ["'$1'"]' 'extend')
local filter [ -z "$result" ] && zaf_err "Hostid $1 not found!"
local gfilter echo $result | zaf_zbxapi_getvalue "host"
local result
hostid="$1"
if [ -n "$hostid" ] ; then
filter='"hostids": [ "'$hostid'" ],'
fi
hstr='{
"method": "host.get",
"jsonrpc": "2.0",
"auth": "'$ZAF_ZBXAPI_AUTH'",
"params": {
'$filter'
"output": "extend"
},
"id": 1
}'
result=$(zaf_zbxapi_do_cache "$hstr" | zaf_zbxapi_getresult | zaf_zbxapi_getvalue host)
[ -z "$result" ] && zaf_err "Hostid $1 not found!"
echo $result
} }
# $1 templateid # $1 templateid
zaf_zbxapi_gettemplate() { zaf_zbxapi_gettemplate() {
local hstr local result
local host
local groupid result=$(zaf_zbxapi_get_object "template" '' '"templateids": ["'$1'"]' 'extend')
local filter [ -z "$result" ] && zaf_err "Templateid $1 not found!"
local gfilter echo $result
local result
hostid="$1"
if [ -n "$hostid" ] ; then
filter='"templateids": [ "'$hostid'" ],'
fi
hstr='{
"method": "template.get",
"jsonrpc": "2.0",
"auth": "'$ZAF_ZBXAPI_AUTH'",
"params": {
'$filter'
"output": "extend"
},
"id": 1
}'
result=$(zaf_zbxapi_do_cache "$hstr" | zaf_zbxapi_getresult | zaf_zbxapi_getvalue host)
[ -z "$result" ] && zaf_err "Templateid $1 not found!"
echo $result
} }
# $1 hostgroupid
# $1 hostgroupid
zaf_zbxapi_gethostsingroup() { zaf_zbxapi_gethostsingroup() {
local hstr local result
local host
local groupid
local filter
local gfilter
groupid="$1" result=$(zaf_zbxapi_get_object "host" '' '"groupids": ["'$1'"]')
if [ -n "$groupid" ]; then [ -z "$result" ] && zaf_wrn "No hosts in groupid '$1'"
gfilter='"groupids": [ "'$groupid'" ],' echo $result | zaf_zbxapi_getvalue "hostid"
fi }
hstr='{ # Get all hostids in system
"method": "host.get", zaf_zbxapi_gethostids() {
"jsonrpc": "2.0", local result
"auth": "'$ZAF_ZBXAPI_AUTH'",
"params": { result=$(zaf_zbxapi_get_object "host")
'$gfilter' echo $result | zaf_zbxapi_getvalue "hostid"
'$filter' }
"output": "shorten"
}, # Get all templateids in system
"id": 1 zaf_zbxapi_gettemplateids() {
}' local result
zaf_zbxapi_do_cache "$hstr" | zaf_zbxapi_getresult | tr ',' '\n' | cut -d '"' -f 4
result=$(zaf_zbxapi_get_object "template")
echo $result | zaf_zbxapi_getvalue "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_getvalue "templateid"
}
# $1 map or null for all
zaf_zbxapi_getmapid() {
local result
if [ -n "$1" ]; then
result=$(zaf_zbxapi_get_object "map" '"name": ["'$1'"]')
else
result=$(zaf_zbxapi_get_object "map")
fi
[ -z "$result" ] && zaf_err "Map $1 not found"
echo $result | zaf_zbxapi_getvalue "sysmapid"
}
# $1 mapid
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"
} }
# Object backup # Object backup

106
zaf
View File

@ -260,18 +260,38 @@ self-remove)
api) api)
zaf_zbxapi_login zaf_zbxapi_login
case $2 in case $2 in
hostid) *-id)
zaf_zbxapi_gethostid "$3" obj=$(echo $2|cut -d '-' -f 1)
[ -z "$3" ] && zaf_err "$0 $1 $2 <$obj>"
eval zaf_zbxapi_get${obj}id "$3"
;; ;;
host) id-*)
zaf_zbxapi_gethost "$3" obj=$(echo $2|cut -d '-' -f 2)
;; [ -z "$3" ] && zaf_err "$0 $1 $2 <id>"
hostgroupid) eval zaf_zbxapi_get${obj} "$3"
zaf_zbxapi_gethostgroupid "$3" ;;
;; host-ids)
hosts) if [ -n "$3" ]; then
gid=$(zaf_zbxapi_gethostgroupid "$3") || exit 1 gid=$(zaf_zbxapi_gethostgroupid "$3") || exit 1
zaf_zbxapi_gethostsingroup $gid zaf_dbg "Selecting all hosts in group $3($gid)"
zaf_zbxapi_gethostsingroup $gid
else
zaf_dbg "Selecting all hosts in system"
zaf_zbxapi_gethostids
fi
;;
template-ids)
if [ -n "$3" ]; then
gid=$(zaf_zbxapi_gethostgroupid "$3") || exit 1
zaf_dbg "Selecting all templates in group $3($gid)"
zaf_zbxapi_gettemplatesingroup $gid
else
zaf_dbg "Selecting all templates in system"
zaf_zbxapi_gettemplateids
fi
;;
map-ids)
zaf_zbxapi_getmapid
;; ;;
export-hosts) export-hosts)
shift; shift shift; shift
@ -363,15 +383,16 @@ api)
;; ;;
*) *)
echo "$0 api command [parameters]" echo "$0 api command [parameters]"
echo "hostid 'host' Get hostid from hostname" for i in host template map; do
echo "host 'hostid' Get hostname from hostid" zaf_hlp "${i}-id $i" "Get $i id"
echo "hostgroupid 'hostgroup' Get hostgroup id from hostgroup" zaf_hlp "id-${i} id" "Get $i name from id"
echo "hosts 'hostgroup' Get hosts in group" done
echo "export-hosts dir [hostgroup] Backup all hosts [in group] (get their config from zabbix and save to dir/hostname.xml)" zaf_hlp "hosts hostgroup" "Get hosts in group"
echo "export-host 'host' Backup host (get config from zabbix to stdout)" zaf_hlp "export-hosts dir [hg]" "Backup all hosts [in group hg] (get their config from zabbix and save to dir/hostname.xml)"
echo "import-template {plugin|file} Import template for plugin or from file" zaf_hlp "export-host host" "Backup host (get config from zabbix to stdout)"
echo "export-template 'name' Export template to stdout" zaf_hlp "import-template {plugin|file}" "Import template for plugin or from file"
echo "export-templates dir Export all template to dir" zaf_hlp "export-template name" "Export template to stdout"
zaf_hlp "export-templates dir" "Export all template to dir"
echo echo
exit exit
;; ;;
@ -381,24 +402,33 @@ api)
*) *)
echo "$0 Version ${ZAF_VERSION}. Please use some of this commands:" echo "$0 Version ${ZAF_VERSION}. Please use some of this commands:"
echo "$0 Cmd [ZAF_OPTION=value] [ZAF_CTRL_Option=value] [ZAF_CTRLI_Item_Option=value] ..." echo "$0 Cmd [ZAF_OPTION=value] [ZAF_CTRL_Option=value] [ZAF_CTRLI_Item_Option=value] ..."
echo "Commands:" echo "Plugin manipulation commands:"
echo "$0 update To update repo (not plugins, similar to apt-get update)" zaf_hlp "$0 update" "To update repo (not plugins, similar to apt-get update)"
echo "$0 upgrade To upgrade installed plugins from repo" zaf_hlp "$0 upgrade" "To upgrade installed plugins from repo"
echo "$0 plugins To list installed plugins" zaf_hlp "$0 install plugin" "To install plugin"
echo "$0 show [plugin] To show installed plugins or plugin info" zaf_hlp "$0 remove plugin" "To remove plugin"
echo "$0 items [plugin] To list all suported items [for plugin]" echo
echo "$0 test [plugin[.item]] To test [all] suported items by zabbix_agentd [for plugin]" echo "Plugin info commands:"
echo "$0 get [plugin[.item]] To test [all] suported items by zabbix_get [for plugin]" zaf_hlp "$0 plugins" "To list installed plugins"
echo "$0 precache [plugin[.item]] To precache [all] suported items" zaf_hlp "$0 show [plugin]" "To show installed plugins or plugin info"
echo "$0 install plugin To install plugin" zaf_hlp "$0 items [plugin]" "To list all suported items [for plugin]"
echo "$0 remove plugin To remove plugin" echo
echo "$0 api To zabbix API functions. See $0 api for more info." echo "Plugin diagnostic commands:"
echo "$0 userparms See userparms generated from zaf on stdout" zaf_hlp "$0 test [plugin[.item]]" "To test [all] suported items by zabbix_agentd [for plugin]"
echo "$0 agent-config Reconfigure zabbix userparms in $ZAF_AGENT_CONFIGD" zaf_hlp "$0 get [plugin[.item]]" "To test [all] suported items by zabbix_get [for plugin]"
zaf_hlp "$0 precache [plugin[.item]]" "To precache [all] suported items"
echo "$0 self-upgrade To self-upgrade zaf" echo
echo "$0 self-remove To self-remove zaf and its config" echo "Zabbix API commands:"
echo "$0 cache-clean To remove all entries from cache" zaf_hlp "$0 api" "To zabbix API functions. See $0 api for more info."
echo
echo "Agent config info commands:"
zaf_hlp "$0 userparms" "See userparms generated from zaf on stdout"
zaf_hlp "$0 agent-config" "Reconfigure zabbix userparms in $ZAF_AGENT_CONFIGD"
echo
echo "Zaf related commands:"
zaf_hlp "$0 self-upgrade" "To self-upgrade zaf"
zaf_hlp "$0 self-remove" "To self-remove zaf and its config"
zaf_hlp "$0 cache-clean" "To remove all entries from cache"
echo echo
;; ;;