2016-04-14 14:31:01 +02:00
|
|
|
|
|
|
|
# Call api function and use cache
|
2016-04-12 15:28:29 +02:00
|
|
|
# $1 - query string
|
|
|
|
zaf_zbxapi_do() {
|
2016-04-14 14:31:01 +02:00
|
|
|
local result
|
|
|
|
zaf_trc "Zabbix API: $1"
|
|
|
|
result=$(curl -s -f -L -X POST -H 'Content-Type: application/json-rpc' -d "$1" "$ZAF_ZBXAPI_URL")
|
|
|
|
if [ $? = 0 ] && echo $result | grep -q '"result":'; then
|
|
|
|
zaf_trc "API OK"
|
|
|
|
echo $result
|
|
|
|
else
|
|
|
|
zaf_err "Error processing API request. ($?,$result)"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
# Call api function and cache results
|
|
|
|
# $1 - query string
|
|
|
|
zaf_zbxapi_do_cache() {
|
2016-04-12 15:28:29 +02:00
|
|
|
local result
|
|
|
|
if ! zaf_fromcache "$1"; then
|
2016-04-14 14:31:01 +02:00
|
|
|
result=$(zaf_zbxapi_do "$1")
|
|
|
|
[ -n "$result" ] && zaf_tocache "$1" "$result" 60
|
|
|
|
echo $result
|
2016-04-12 15:28:29 +02:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
# Extract result from JSON response
|
|
|
|
zaf_zbxapi_getresult() {
|
|
|
|
sed -e 's/\({"jsonrpc":"2.0","result":\)\(.*\),\("id":.*\)/\2/g' | sed -e 's/^\[\]$//'
|
|
|
|
}
|
|
|
|
|
|
|
|
# Extract XML result from JSON response
|
|
|
|
zaf_zbxapi_getxml() {
|
2016-04-14 14:31:01 +02:00
|
|
|
zaf_zbxapi_getresult | sed -e 's/{"jsonrpc":"2.0","result":"//' | sed -e 's/","id"\:1}//' | zaf_zbxapi_getstring | zaf_strunescape '<">/'
|
2016-04-12 15:28:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# Extract string from JSON response result
|
|
|
|
zaf_zbxapi_getstring() {
|
|
|
|
sed -e 's/^"//' -e 's/"$//' -e 's/\\n/'\\n'/g'
|
|
|
|
}
|
|
|
|
|
|
|
|
# Extract value from JSON response result
|
|
|
|
# $1 key
|
|
|
|
zaf_zbxapi_getvalue() {
|
|
|
|
tr ',' '\n' | grep "\"$1\":" | cut -d '"' -f 4
|
|
|
|
}
|
2016-04-12 10:36:39 +02:00
|
|
|
|
|
|
|
# Zabbix API related functions
|
|
|
|
# Parameters in global variables ZAF_ZBX_API_*
|
|
|
|
# returns auth on stdout or false
|
|
|
|
zaf_zbxapi_login(){
|
|
|
|
local authstr
|
|
|
|
local user
|
|
|
|
local pass
|
2016-04-12 15:28:29 +02:00
|
|
|
local result
|
2016-04-12 10:36:39 +02:00
|
|
|
|
|
|
|
[ -z "$ZAF_ZBXAPI_URL" ] || [ -z "$ZAF_ZBXAPI_USER" ] || [ -z "$ZAF_ZBXAPI_PASS" ] && zaf_err "Zabbix Api parameters not set!"
|
|
|
|
authstr='{
|
2016-04-12 15:28:29 +02:00
|
|
|
"method" : "user.login",
|
2016-04-12 10:36:39 +02:00
|
|
|
"params" : {
|
|
|
|
"password" : "'$ZAF_ZBXAPI_PASS'",
|
|
|
|
"user" : "'$ZAF_ZBXAPI_USER'"
|
|
|
|
},
|
|
|
|
"id" : 0,
|
2016-04-12 15:28:29 +02:00
|
|
|
"jsonrpc" : "2.0"
|
2016-04-12 10:36:39 +02:00
|
|
|
}'
|
2016-04-12 15:28:29 +02:00
|
|
|
|
|
|
|
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-)
|
|
|
|
fi
|
2016-04-14 14:31:01 +02:00
|
|
|
result=$(zaf_zbxapi_do_cache "$authstr")
|
2016-04-12 15:28:29 +02:00
|
|
|
ZAF_ZBXAPI_AUTH=$(echo $result |zaf_zbxapi_getresult| zaf_zbxapi_getstring)
|
2016-04-14 14:31:01 +02:00
|
|
|
[ -z "$ZAF_ZBXAPI_AUTH" ] && zaf_err "Cannot login into API"
|
2016-04-12 10:36:39 +02:00
|
|
|
zaf_dbg "Logged into zabbix API ($ZAF_ZBXAPI_AUTH)"
|
|
|
|
}
|
|
|
|
|
2016-04-12 15:28:29 +02:00
|
|
|
# $1 hostgroup name
|
|
|
|
zaf_zbxapi_gethostgroupid() {
|
|
|
|
local hstr
|
|
|
|
local filter
|
|
|
|
local gfilter
|
2016-04-14 14:31:01 +02:00
|
|
|
local result
|
2016-04-12 15:28:29 +02:00
|
|
|
|
|
|
|
hstr='{
|
|
|
|
"method": "hostgroup.get",
|
|
|
|
"jsonrpc": "2.0",
|
|
|
|
"auth": "'$ZAF_ZBXAPI_AUTH'",
|
|
|
|
"params": {
|
|
|
|
"filter": {
|
|
|
|
"name": ["'$1'"]
|
|
|
|
},
|
|
|
|
"output": "shorten"
|
|
|
|
},
|
|
|
|
"id": 1
|
|
|
|
}'
|
2016-04-14 14:31:01 +02:00
|
|
|
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
|
2016-04-12 15:28:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# $1 hostname
|
|
|
|
zaf_zbxapi_gethostid() {
|
|
|
|
local hstr
|
|
|
|
local host
|
|
|
|
local groupid
|
|
|
|
local filter
|
|
|
|
local gfilter
|
2016-04-14 14:31:01 +02:00
|
|
|
local result
|
2016-04-12 15:28:29 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
}'
|
2016-04-14 14:31:01 +02:00
|
|
|
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
|
|
|
|
zaf_zbxapi_gettemplateid() {
|
|
|
|
local hstr
|
|
|
|
local host
|
|
|
|
local groupid
|
|
|
|
local filter
|
|
|
|
local gfilter
|
|
|
|
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
|
2016-04-12 10:36:39 +02:00
|
|
|
}
|
|
|
|
|
2016-04-12 15:28:29 +02:00
|
|
|
# $1 hostid
|
|
|
|
zaf_zbxapi_gethost() {
|
2016-04-12 10:36:39 +02:00
|
|
|
local hstr
|
2016-04-12 15:28:29 +02:00
|
|
|
local host
|
|
|
|
local groupid
|
2016-04-12 10:36:39 +02:00
|
|
|
local filter
|
2016-04-12 15:28:29 +02:00
|
|
|
local gfilter
|
2016-04-14 14:31:01 +02:00
|
|
|
local result
|
2016-04-12 10:36:39 +02:00
|
|
|
|
2016-04-12 15:28:29 +02:00
|
|
|
hostid="$1"
|
|
|
|
if [ -n "$hostid" ] ; then
|
|
|
|
filter='"hostids": [ "'$hostid'" ],'
|
|
|
|
fi
|
2016-04-12 10:36:39 +02:00
|
|
|
hstr='{
|
2016-04-12 15:28:29 +02:00
|
|
|
"method": "host.get",
|
2016-04-12 10:36:39 +02:00
|
|
|
"jsonrpc": "2.0",
|
2016-04-12 15:28:29 +02:00
|
|
|
"auth": "'$ZAF_ZBXAPI_AUTH'",
|
|
|
|
"params": {
|
|
|
|
'$filter'
|
|
|
|
"output": "extend"
|
|
|
|
},
|
|
|
|
"id": 1
|
|
|
|
}'
|
2016-04-14 14:31:01 +02:00
|
|
|
result=$(zaf_zbxapi_do_cache "$hstr" | zaf_zbxapi_getresult | zaf_zbxapi_getvalue host)
|
|
|
|
[ -z "$result" ] && zaf_err "Hostid $1 not found!"
|
|
|
|
echo $result
|
2016-04-12 15:28:29 +02:00
|
|
|
}
|
|
|
|
|
2016-04-14 14:31:01 +02:00
|
|
|
# $1 templateid
|
|
|
|
zaf_zbxapi_gettemplate() {
|
|
|
|
local hstr
|
|
|
|
local host
|
|
|
|
local groupid
|
|
|
|
local filter
|
|
|
|
local gfilter
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-04-12 15:28:29 +02:00
|
|
|
# $1 hostgroupid
|
|
|
|
zaf_zbxapi_gethostsingroup() {
|
|
|
|
local hstr
|
|
|
|
local host
|
|
|
|
local groupid
|
|
|
|
local filter
|
|
|
|
local gfilter
|
|
|
|
|
|
|
|
groupid="$1"
|
|
|
|
if [ -n "$groupid" ]; then
|
|
|
|
gfilter='"groupids": [ "'$groupid'" ],'
|
|
|
|
fi
|
|
|
|
|
|
|
|
hstr='{
|
2016-04-12 10:36:39 +02:00
|
|
|
"method": "host.get",
|
2016-04-12 15:28:29 +02:00
|
|
|
"jsonrpc": "2.0",
|
2016-04-12 10:36:39 +02:00
|
|
|
"auth": "'$ZAF_ZBXAPI_AUTH'",
|
2016-04-12 15:28:29 +02:00
|
|
|
"params": {
|
|
|
|
'$gfilter'
|
|
|
|
'$filter'
|
|
|
|
"output": "shorten"
|
|
|
|
},
|
|
|
|
"id": 1
|
2016-04-12 10:36:39 +02:00
|
|
|
}'
|
2016-04-14 14:31:01 +02:00
|
|
|
zaf_zbxapi_do_cache "$hstr" | zaf_zbxapi_getresult | tr ',' '\n' | cut -d '"' -f 4
|
2016-04-12 10:36:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# Host backup
|
|
|
|
# $1 hostid
|
2016-04-14 14:31:01 +02:00
|
|
|
zaf_zbxapi_export_host(){
|
2016-04-12 10:36:39 +02:00
|
|
|
local bkpstr
|
2016-04-14 14:31:01 +02:00
|
|
|
local host
|
2016-04-12 10:36:39 +02:00
|
|
|
|
|
|
|
host="$1"
|
|
|
|
bkpstr='
|
|
|
|
{
|
|
|
|
"method": "configuration.export",
|
2016-04-12 15:28:29 +02:00
|
|
|
"jsonrpc": "2.0",
|
2016-04-12 10:36:39 +02:00
|
|
|
"params": {
|
|
|
|
"options": {
|
|
|
|
"hosts": [
|
|
|
|
"'$host'"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
"format": "xml"
|
|
|
|
},
|
|
|
|
"auth": "'$ZAF_ZBXAPI_AUTH'",
|
|
|
|
"id": 1
|
|
|
|
}'
|
2016-04-14 14:31:01 +02:00
|
|
|
zaf_zbxapi_do_cache "$bkpstr" | zaf_zbxapi_getxml
|
|
|
|
}
|
|
|
|
|
|
|
|
# Template backup
|
|
|
|
# $1 templateid
|
|
|
|
zaf_zbxapi_export_template(){
|
|
|
|
local bkpstr
|
|
|
|
local host
|
|
|
|
|
|
|
|
host="$1"
|
|
|
|
bkpstr='
|
|
|
|
{
|
|
|
|
"method": "configuration.export",
|
|
|
|
"jsonrpc": "2.0",
|
|
|
|
"params": {
|
|
|
|
"options": {
|
|
|
|
"templates": [
|
|
|
|
"'$host'"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
"format": "xml"
|
|
|
|
},
|
|
|
|
"auth": "'$ZAF_ZBXAPI_AUTH'",
|
|
|
|
"id": 1
|
|
|
|
}'
|
|
|
|
zaf_zbxapi_do_cache "$bkpstr" | zaf_zbxapi_getxml
|
|
|
|
}
|
|
|
|
|
|
|
|
# Import template into zabbix
|
|
|
|
# $1 template file or stdin
|
|
|
|
zaf_zbxapi_import_template(){
|
|
|
|
local tmplstr
|
|
|
|
local impstr
|
|
|
|
|
|
|
|
if [ -z "$1" ]; then
|
|
|
|
tmplstr=$(zaf_strescape '"')
|
|
|
|
else
|
|
|
|
! [ -f "$1" ] && return 1
|
|
|
|
tmplstr=$(zaf_strescape '"\n\r' <$1)
|
|
|
|
fi
|
|
|
|
impstr='
|
|
|
|
{
|
|
|
|
"method": "configuration.import",
|
|
|
|
"jsonrpc": "2.0",
|
|
|
|
"params": {
|
|
|
|
"format": "xml",
|
|
|
|
"rules": {
|
|
|
|
"hosts": {
|
|
|
|
"createMissing": true,
|
|
|
|
"updateExisting": true
|
|
|
|
},
|
|
|
|
"items": {
|
|
|
|
"createMissing": true,
|
|
|
|
"updateExisting": true,
|
|
|
|
"deleteMissing": true
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"source": "'$tmplstr'"
|
|
|
|
},
|
|
|
|
"auth": "'$ZAF_ZBXAPI_AUTH'",
|
|
|
|
"id": 3
|
|
|
|
}'
|
|
|
|
echo "$impstr";exit
|
|
|
|
zaf_zbxapi_do "$impstr" | zaf_zbxapi_getresult | grep -q true
|
2016-04-12 10:36:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|