通过API自动增加Zabbix监控主机

公司通过脚本自动初始化扩容服务器,但是存在一个问题,扩容后zabbix不能自动自动对服务器加入主机,需要手动添加,效率慢也容易出错,于是想着通过API自动添加主机监控。果然可以通过查看接口可以解决这个问题。https://www.zabbix.com/documentation/4.0/zh/manual/api

新增接口前需要做个登录,拿到登录的token,然后拿到这个token,再去添加服务器。脚本如下:

#!/bin/bash

hostname='192.168.0.219'    #被监控主机名称
ip='192.168.0.219'                 #被监控主机IP
port=10050                            #被监控主机zabbix_agentd端口
templateid=10001                 #模板ID
groupid=2                             #需要加入的服务器组
zabbixhost='http://www.zabbix.com'     #zabbix主机地址

header='Content-Type:application/json'
request_data='{"jsonrpc" : "2.0","method" : "user.login","params" : {"user" : "admin" ,"password" : "zabbix"} ,"id" : 1 }'
result=$(curl -s -XPOST -H ${header} -d "${request_data}" ${zabbixhost}/api_jsonrpc.php)
token=$(echo $result|sed 's/.\+"result":"\([0-9a-z]\+\)".\+/\1/')

save_request_data='{"jsonrpc": "2.0","method": "host.create","params": {"host": "'${hostname}'","interfaces": [{"type": 1,"main": 1,"useip": 1,"ip": "'${ip}'","dns": "","port": "'${port}'"}],"groups": [{"groupid": "'${groupid}'"}],"templates": [{"templateid": "'${templateid}'"}]},"auth": "'${token}'","id": 1 }'

curl -s -XPOST -H ${header} -d "${save_request_data}" ${zabbixhost}/api_jsonrpc.php

猜你喜欢

转载自blog.51cto.com/fengwan/2463504