1
0
mirror of https://github.com/limosek/zaf.git synced 2024-11-24 11:29: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!"
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(){
echo $ZAF_VERSION

View File

@ -73,168 +73,139 @@ zaf_zbxapi_login(){
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
zaf_zbxapi_gethostgroupid() {
local hstr
local filter
local gfilter
local result
local result
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
result=$(zaf_zbxapi_get_object "hostgroup" '"name": ["'$1'"]')
[ -z "$result" ] && zaf_err "HostGroup $1 not found!"
echo $result |zaf_zbxapi_getvalue groupid
}
# $1 hostname
zaf_zbxapi_gethostid() {
local hstr
local host
local groupid
local filter
local gfilter
local result
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
result=$(zaf_zbxapi_get_object "host" '"host": ["'$1'"]')
[ -z "$result" ] && zaf_err "Host $1 not found!"
echo $result |zaf_zbxapi_getvalue hostid
}
# $1 hostname
zaf_zbxapi_gettemplateid() {
local hstr
local host
local groupid
local filter
local gfilter
local result
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
result=$(zaf_zbxapi_get_object "template" '"host": ["'$1'"]')
[ -z "$result" ] && zaf_err "Template $1 not found!"
echo $result |zaf_zbxapi_getvalue templateid
}
# $1 hostid
zaf_zbxapi_gethost() {
local hstr
local host
local groupid
local filter
local gfilter
local result
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
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
zaf_zbxapi_gettemplate() {
local hstr
local host
local groupid
local filter
local gfilter
local 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
result=$(zaf_zbxapi_get_object "template" '' '"templateids": ["'$1'"]' 'extend')
[ -z "$result" ] && zaf_err "Templateid $1 not found!"
echo $result
}
# $1 hostgroupid
zaf_zbxapi_gethostsingroup() {
local hstr
local host
local groupid
local filter
local gfilter
local result
groupid="$1"
if [ -n "$groupid" ]; then
gfilter='"groupids": [ "'$groupid'" ],'
fi
result=$(zaf_zbxapi_get_object "host" '' '"groupids": ["'$1'"]')
[ -z "$result" ] && zaf_wrn "No hosts in groupid '$1'"
echo $result | zaf_zbxapi_getvalue "hostid"
}
hstr='{
"method": "host.get",
"jsonrpc": "2.0",
"auth": "'$ZAF_ZBXAPI_AUTH'",
"params": {
'$gfilter'
'$filter'
"output": "shorten"
},
"id": 1
}'
zaf_zbxapi_do_cache "$hstr" | zaf_zbxapi_getresult | tr ',' '\n' | cut -d '"' -f 4
# Get all hostids in system
zaf_zbxapi_gethostids() {
local result
result=$(zaf_zbxapi_get_object "host")
echo $result | zaf_zbxapi_getvalue "hostid"
}
# Get all templateids in system
zaf_zbxapi_gettemplateids() {
local result
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

102
zaf
View File

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