#!/bin/bash
############################################################
# $Name: zabbix_json.lib
# $Version: v1.0
# $Functions:
#
# 1 function_hostgroup_create
# USGE: function_hostgroup_create "lb_server" "web_server" "mysql_server" \
# "store_server" "cache_server" "mage_server" ......
# 2 function_host_create
# USGE: function_host_create 'Template OS Linux' 'Template Nginx Status' \
# 'Template Linux TCP Status' 'Template Percona MySQL Server' ......
# 3 function_httptest_create
# USGE: function_httptest_create 'http://zabbix.lzbbg.cn'
#
# $Author: Jeson Li
# $organization: www.lzbbg.cn
# $Create Date: 2017-03-10
# $Mail: libobin@tom.com
# $Description: Linux Zabbix Web Json Installing Lib
############################################################
export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin"
source /etc/profile
Tik="\e[5m";Red="\e[31m";Grn="\e[32m";Ylw="\e[33m";Blu="\e[34m";Pup="\e[35m";Rst="\e[0m"
# use in user.login
user="Admin"
password="zabbix"
# use in hostgroup.create
lb_server="lb_server"
web_server="web_server"
mysql_server="mysql_server"
store_server="store_server"
cache_server="cache_server"
mage_server="mage_server"
# use in hostgroup.get
groupName="$(eval echo $`hostname|cut -d- -f1`_server)"
# use in host.create
API_URL='http://zabbix.lzbbg.cn/api_jsonrpc.php'
agent_port=10050
IP="`grep $(hostname) /etc/hosts|awk 'NR==1{print $1}'|head -1`"
# IIIP="`ip addr show eth0|awk -F"[ \t/]*" '$2~"^inet$"{print $3}'|head -1`"
# echo "hostName == $(hostname) IPetc == ${IP} IIIP == ${IIIP}" >>/tmp/zabbix_host.create.log
auth=$(echo "
{
'\"'jsonrpc'\"': '\"'2.0'\"',
'\"'method'\"': '\"'user.login'\"',
'\"'params'\"': {
'\"'user'\"': '\"'${user}'\"',
'\"'password'\"': '\"'${password}'\"'
},
'\"'id'\"': 1
}" |tr "\n" " "|xargs -i curl -s -X POST -H 'Content-Type:application/json' -d '{}' $API_URL|python -m json.tool|awk -F"[ \t\":]*" '$2 ~"result"{print $3}')
function_groupid_get(){
export groupid=$(echo "
{
'\"'jsonrpc'\"': '\"'2.0'\"',
'\"'method'\"': '\"'hostgroup.get'\"',
'\"'params'\"': {
'\"'output'\"': '\"'extend'\"',
'\"'filter'\"': {
'\"'name'\"': [
'\"'${groupName}'\"'
]
}
},
'\"'auth'\"': '\"'${auth}'\"',
'\"'id'\"': 1
}" |tr "\n" " "|xargs -i curl -s -X POST -H 'Content-Type:application/json' -d '{}' $API_URL|python -m json.tool|awk -F"[ \t\":]*" '$2 ~"groupid"{print $3}')
}
function_hostid_get(){
hostid=$(echo "
{
'\"'jsonrpc'\"': '\"'2.0'\"',
'\"'method'\"': '\"'host.get'\"',
'\"'params'\"': {
'\"'filter'\"': {
'\"'host'\"': [
'\"'$(hostname)'\"'
]
}
},
'\"'auth'\"': '\"'${auth}'\"',
'\"'id'\"': 1
}" |tr "\n" " "|xargs -i curl -s -X POST -H 'Content-Type:application/json' -d '{}' $API_URL|python -m json.tool|awk -F"[ \t\":]*" '$2 ~"^hostid$"{print $3}')
}
# USGE: function_hostgroup_create "lb_server" "web_server" "mysql_server" "store_server" "cache_server" "mage_server"
function_hostgroup_create()
{
for groupNames in $*
do
echo "
{
'\"'jsonrpc'\"': '\"'2.0'\"',
'\"'method'\"': '\"'hostgroup.create'\"',
'\"'params'\"': {
'\"'name'\"': '\"'${groupNames}'\"'
},
'\"'auth'\"': '\"'${auth}'\"',
'\"'id'\"': 1
}" | tr "\n" " " | xargs -i curl -s -X POST -H 'Content-Type:application/json' -d '{}' $API_URL|python -m json.tool
done
sleep 5
}
# USGE: function_host_create 'Template OS Linux' 'Template Nginx Status' 'Template Linux TCP Status' 'Template Percona MySQL Server' ......
function_host_create()
{
Tmp_Id=''
function_groupid_get
for ((;;))
do
[ -z "$1" ] && break
templateName="$1"
shift
Tmp_Id=$(echo "
{
'\"'jsonrpc'\"': '\"'2.0'\"',
'\"'method'\"': '\"'template.get'\"',
'\"'params'\"': {
'\"'output'\"': '\"'extend'\"',
'\"'filter'\"': {
'\"'host'\"': [
'\"'${templateName}'\"'
]
}
},
'\"'auth'\"': '\"'${auth}'\"',
'\"'id'\"': 1
}" | tr "\n" " "|xargs -i curl -s -X POST -H 'Content-Type:application/json' -d '{}' $API_URL|python -m json.tool|awk -F"[ \t\":]*" '$2 ~"templateid"{print $3}')
# Receive all templates variable to temp_fix_id and alter for json formart *****
if [ -n "$Tmp_Id" ];then
export `echo $templateName|sed -r 's#[ \t]+##g'`_id=$Tmp_Id
temp_fix_id="${temp_fix_id}{\'\\\"\'templateid\'\\\"\': \'\\\"\'\${`echo $templateName|sed -r 's#[ \t]+##g'`_id}\'\\\"\'},"
fi
########################################################################### *****
echo "`echo $templateName|sed -r 's#[ \t]+##g'`_id=$Tmp_Id">>/tmp/zabbix_host.create.log
Tmp_Id=''
done
echo "
{
'\"'jsonrpc'\"': '\"'2.0'\"',
'\"'method'\"': '\"'host.create'\"',
'\"'params'\"': {
'\"'host'\"': '\"'$(hostname)'\"',
'\"'interfaces'\"': [
{
'\"'type'\"': 1,
'\"'main'\"': 1,
'\"'useip'\"': 1,
'\"'ip'\"': '\"'${IP}'\"',
'\"'dns'\"': '\"''\"',
'\"'port'\"': '\"'${agent_port}'\"'
}
],
'\"'groups'\"': [
{
'\"'groupid'\"': '\"'${groupid}'\"'
}
],
'\"'templates'\"': [
`eval echo ${temp_fix_id%,}`
]
},
'\"'auth'\"': '\"'${auth}'\"',
'\"'id'\"': 1
}" | tr "\n" " "|xargs -i curl -s -X POST -H 'Content-Type:application/json' -d '{}' $API_URL|python -m json.tool
sleep 5
}
# USGE: function_httptest_create 'http://zabbix.lzbbg.cn'
function_httptest_create()
{
web_urls="$1"
function_hostid_get
for web_url in $(echo $web_urls)
do
web_comment="$(echo ${web_url}|awk -F"[:/.]*" '{print $2}')"
echo "
{
'\"'jsonrpc'\"': '\"'2.0'\"',
'\"'method'\"': '\"'httptest.create'\"',
'\"'params'\"': {
'\"'name'\"': '\"'${web_comment} Homepage check'\"',
'\"'hostid'\"': '\"'${hostid}'\"',
'\"'steps'\"': [
{
'\"'name'\"': '\"'${web_comment} Homepage'\"',
'\"'url'\"': '\"'${web_url}'\"',
'\"'status_codes'\"': '\"'200'\"',
'\"'no'\"': 1
}
]
},
'\"'auth'\"': '\"'$auth'\"',
'\"'id'\"': 1
}" | tr "\n" " " | xargs -i curl -s -X POST -H 'Content-Type:application/json' -d '{}' $API_URL|python -m json.tool
done
}