页面“Php-worker重启脚本”与“Zabbix 调用API 批量添加主机”之间的差异
(页面间的差异)
跳到导航
跳到搜索
第1行: | 第1行: | ||
− | = | + | [[category:ops]] [[category:zabbix]] |
+ | =pre= | ||
+ | |||
+ | 已有 salt | ||
+ | |||
+ | 可以见 批量添加 salt Salt-ssh批量自动安装被控端minion | ||
+ | |||
+ | =批量添加zabbix agent= | ||
+ | ==直接用cmd.run== | ||
+ | salt 'prod-mq03' cmd.run 'yum install -y zabbix-agent' #systemctl restart zabbix-agent systemctl enable zabbix-agent | ||
+ | |||
+ | ==zabbix state.sls == | ||
+ | 有空要试一下 | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | [https://www.cnblogs.com/LYCong/p/7879805.html salt 使用state文件来配置zabbix客户端文件] | ||
+ | |||
+ | [https://blog.csdn.net/u011075143/article/details/78615691 salt 使用state文件来配置zabbix客户端文件] | ||
+ | |||
+ | [https://www.cnblogs.com/Jackie-Chen/articles/10795003.html SaltStack批量安装zabbix-agent(yum安装)] | ||
+ | |||
+ | [https://www.cnblogs.com/xiewenming/p/7713660.html SaltStack安装zabbix-agent-第九篇] | ||
+ | |||
+ | [https://www.cnblogs.com/python-study/p/5504501.html SaltStack 使用pillar安装配置管理zabbix] | ||
+ | |||
+ | [https://blog.csdn.net/reblue520/article/details/76286843 saltstack自动化运维系列⑦SaltStack实践配置管理安装zabbix] | ||
+ | |||
+ | [https://www.cnblogs.com/LYCong/p/7879805.html salt 使用state文件来源码安装和配置zabbix客户端文件] | ||
+ | |||
+ | =批量修改zabbix agent配置= | ||
+ | <pre> | ||
+ | Server=148.66.11.55 | ||
+ | ServerActive=148.66.11.55 | ||
+ | Hostname=prod-sns-mq01 | ||
+ | |||
+ | |||
+ | |||
+ | sed -i '/^Hostname/ s/.*/Hostname=zabbix_hk/' /etc/zabbix/zabbix_agentd.conf | ||
+ | |||
+ | #zbx hostname | ||
+ | salt 'prod-mq03' cmd.run 'hostname' | ||
+ | prod-mq03: | ||
+ | prod-mq03 | ||
+ | |||
+ | |||
+ | #这里可以取出 hostname 作变量 和作循环 | ||
+ | Server=148.66.01.05 | ||
+ | ServerActive=148.66.01.05 | ||
+ | |||
+ | myhost=`salt 'prod-mq03' cmd.run 'hostname' |sed -n '2p' |awk '{print $1}'` | ||
+ | salt 'prod-sns-mq02' cmd.run "sed -i '/^Hostname/ s/.*/Hostname=$myhost/' /etc/zabbix/zabbix_agentd.conf" | ||
+ | |||
+ | #这个替换太多了 不太好写匹配 要如下添加 = or 直接 inser 就是了 | ||
+ | salt 'prod-mq03' cmd.run "sed -i '/^Server=/ s/.*/Server=$Server/' /etc/zabbix/zabbix_agentd.conf" | ||
+ | salt 'prod-mq03' cmd.run "sed -i '/^ServerActive=/ s/.*/ServerActive=$ServerActive/' /etc/zabbix/zabbix_agentd.conf" | ||
+ | |||
+ | |||
+ | 注意 "" 结果不太对的 | ||
+ | salt 'prod-sns-mq02' cmd.run "hostname |sed -n '2p' |awk '{print $1}'" | ||
+ | prod-sns-mq02: | ||
+ | |||
+ | </pre> | ||
+ | =利用api批量添加主机 = | ||
+ | zbx_server version 4.0 | ||
+ | |||
+ | == get token == | ||
+ | <pre> | ||
+ | cat aip.sh | ||
+ | # -*- coding:utf-8 -*- | ||
+ | import urllib2 | ||
+ | import json | ||
+ | |||
+ | url = 'http://zabbix.com/zabbix/api_jsonrpc.php' | ||
+ | header = {'Content-Type': 'application/json'} | ||
+ | |||
+ | req = json.dumps( | ||
+ | { | ||
+ | "jsonrpc": "2.0", | ||
+ | "method": "user.login", | ||
+ | "params": { | ||
+ | "user": "Admin", | ||
+ | "password": "123456" | ||
+ | }, | ||
+ | "id": 0, | ||
+ | } | ||
+ | ) | ||
+ | |||
+ | def auth(): | ||
+ | r = urllib2.Request(url=url, headers=header, data=req) | ||
+ | response = urllib2.urlopen(r) | ||
+ | token = json.loads(response.read()) | ||
+ | print(token) | ||
+ | |||
+ | if __name__ == '__main__': | ||
+ | |||
+ | auth() | ||
+ | |||
+ | #token | ||
+ | {u'jsonrpc': u'2.0', u'result': u'1794bcbe6d818069bff5aa499a07a960', u'id': 0} | ||
+ | |||
+ | </pre> | ||
+ | [https://blog.51cto.com/freshair/2132748 Python调用Zabbix api之从入门到放弃——登录并获取身份验证令牌] | ||
+ | |||
+ | [https://www.jianshu.com/p/7a014f316e35 zabbix api token获取] | ||
+ | == 我们获取必要的相关信息 ,eg所有主机list信息== | ||
+ | ===groupid === | ||
+ | <pre> | ||
+ | cat getgroupid | ||
+ | #获取指定groupid | ||
+ | curl -s -X POST -H 'Content-Type:application/json' -d ' | ||
+ | { | ||
+ | "jsonrpc": "2.0", | ||
+ | "method": "hostgroup.get", | ||
+ | "params": { | ||
+ | "output": "extend", | ||
+ | "filter": { | ||
+ | "name": [ | ||
+ | "Zabbix servers", | ||
+ | "Linux servers", | ||
+ | "sns servers" | ||
+ | ] | ||
+ | } | ||
+ | }, | ||
+ | "auth": "1794bcbe6d818069bff5aa423a07a960", | ||
+ | "id": 1 | ||
+ | }' http://zbx.com/zabbix/api_jsonrpc.php | python3 -m json.tool | ||
+ | </pre> | ||
+ | [https://www.cnblogs.com/zdoubly/p/9777122.html zabbix--api学习之路--get_hostgroup获取] | ||
+ | |||
+ | === templateid=== | ||
<pre> | <pre> | ||
− | |||
− | |||
− | |||
− | |||
#!/bin/bash | #!/bin/bash | ||
− | # | + | #跑在我的freebsd12 |
− | + | #获取指定groupid | |
− | + | curl -s -X POST -H 'Content-Type:application/json' -d ' | |
− | + | { | |
− | + | "jsonrpc": "2.0", | |
− | + | "method": "hostgroup.get", | |
− | + | "params": { | |
− | + | "output": "extend", | |
+ | "filter": { | ||
+ | "name": [ | ||
+ | "Zabbix servers", | ||
+ | "Linux servers" | ||
+ | ] | ||
+ | } | ||
+ | }, | ||
+ | "auth": "1794bcbe6d818069bff5aa423a07a960", | ||
+ | "id": 1 | ||
+ | }' http://148.66.11.55/zabbix/api_jsonrpc.php | python3 -m json.tool | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | #output | |
+ | "result": [ | ||
+ | { | ||
+ | "groupid": "2", | ||
+ | "name": "Linux servers", | ||
+ | "internal": "0", | ||
+ | "flags": "0" | ||
+ | }, | ||
+ | { | ||
+ | "groupid": "4", | ||
+ | "name": "Zabbix servers", | ||
</pre> | </pre> | ||
− | = | + | === hostadd=== |
<pre> | <pre> | ||
− | + | 批量添加版 用法 分别添加 ip and hostname | |
− | |||
− | + | #!/bin/bash | |
+ | #注释中文导致不成功 | ||
+ | #"host": "'prod-sns-mq02'", #这里一般改为你要的hostname | ||
+ | IP=( | ||
+ | 40.243.52 | ||
+ | 41.102.111 | ||
+ | 40.218.197 | ||
+ | 40.216.19 | ||
+ | ) | ||
+ | HOSTNAME=( | ||
+ | prod-sns-php01 | ||
+ | prod-sns-php02 | ||
+ | prod-sns-mq01 | ||
+ | prod-sns-mq03 | ||
+ | ) | ||
− | + | for no in `seq 0 echo ${#a[@]}' | |
+ | #for no in `seq 2 3` #这个是手工决定 数量 | ||
+ | do | ||
− | - | + | curl -s -X POST -H 'Content-Type:application/json' -d ' |
+ | { | ||
+ | "jsonrpc": "2.0", | ||
+ | "method": "host.create", | ||
+ | "params": { | ||
+ | "host": "'${HOSTNAME[$no]}'", | ||
+ | |||
+ | "interfaces": [ | ||
+ | { | ||
+ | "type": 1, | ||
+ | "main": 1, | ||
+ | "useip": 1, | ||
+ | "ip": "'${IP[$no]}'", | ||
+ | "dns": "", | ||
+ | "port": "10050" | ||
+ | } | ||
+ | ], | ||
+ | "groups": [ | ||
+ | { | ||
+ | "groupid": "2", | ||
+ | "groupid": "16" | ||
+ | } | ||
+ | ], | ||
+ | "templates": [ | ||
+ | { | ||
+ | "templateid": "10001" | ||
+ | } | ||
+ | ] | ||
+ | }, | ||
+ | "auth": "1794bcbe6d818069bff5aa4207a960", | ||
+ | "id": 1 | ||
+ | }' http://zbx.com/zabbix/api_jsonrpc.php | python -m json.tool | ||
+ | |||
+ | done | ||
− | + | #要添加多一个群组 | |
− | |||
− | |||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | #!/bin/bash | |
+ | #注释中文导致不成功 | ||
+ | #"host": "'prod-sns-mq02'", #这里一般改为你要的hostname | ||
+ | IP=" | ||
+ | 121.41.103.113 | ||
+ | " | ||
+ | for node_ip in ${IP}; | ||
+ | do | ||
+ | curl -s -X POST -H 'Content-Type:application/json' -d ' | ||
+ | { | ||
+ | "jsonrpc": "2.0", | ||
+ | "method": "host.create", | ||
+ | "params": { | ||
+ | "host": "'prod-sns-es01'", | ||
+ | |||
+ | "interfaces": [ | ||
+ | { | ||
+ | "type": 1, | ||
+ | "main": 1, | ||
+ | "useip": 1, | ||
+ | "ip": "'${node_ip}'", | ||
+ | "dns": "", | ||
+ | "port": "10050" | ||
+ | } | ||
+ | ], | ||
+ | "groups": [ | ||
+ | { | ||
+ | "groupid": "2", | ||
+ | "groupid": "16" | ||
+ | } | ||
+ | ], | ||
+ | "templates": [ | ||
+ | { | ||
+ | "templateid": "10001" | ||
+ | } | ||
+ | ] | ||
+ | }, | ||
+ | "auth": "1794bcbe6d818069bff5aa423a07a960", | ||
+ | "id": 1 | ||
+ | }' http://148.66.11.55/zabbix/api_jsonrpc.php | python -m json.tool | ||
+ | done | ||
+ | |||
+ | #要添加多一个群组 | ||
</pre> | </pre> | ||
− | = | + | =进阶= |
− | + | 写成通用的py脚本 ? | |
− | [https:// | + | [https://www.jianshu.com/p/e087cace8ddf 利用zabbix API(python)批量查询主机、模板、添加删除主机] |
− |
2019年12月27日 (五) 07:36的版本
目录
pre
已有 salt
可以见 批量添加 salt Salt-ssh批量自动安装被控端minion
批量添加zabbix agent
直接用cmd.run
salt 'prod-mq03' cmd.run 'yum install -y zabbix-agent' #systemctl restart zabbix-agent systemctl enable zabbix-agent
zabbix state.sls
有空要试一下
SaltStack批量安装zabbix-agent(yum安装)
SaltStack 使用pillar安装配置管理zabbix
saltstack自动化运维系列⑦SaltStack实践配置管理安装zabbix
salt 使用state文件来源码安装和配置zabbix客户端文件
批量修改zabbix agent配置
Server=148.66.11.55 ServerActive=148.66.11.55 Hostname=prod-sns-mq01 sed -i '/^Hostname/ s/.*/Hostname=zabbix_hk/' /etc/zabbix/zabbix_agentd.conf #zbx hostname salt 'prod-mq03' cmd.run 'hostname' prod-mq03: prod-mq03 #这里可以取出 hostname 作变量 和作循环 Server=148.66.01.05 ServerActive=148.66.01.05 myhost=`salt 'prod-mq03' cmd.run 'hostname' |sed -n '2p' |awk '{print $1}'` salt 'prod-sns-mq02' cmd.run "sed -i '/^Hostname/ s/.*/Hostname=$myhost/' /etc/zabbix/zabbix_agentd.conf" #这个替换太多了 不太好写匹配 要如下添加 = or 直接 inser 就是了 salt 'prod-mq03' cmd.run "sed -i '/^Server=/ s/.*/Server=$Server/' /etc/zabbix/zabbix_agentd.conf" salt 'prod-mq03' cmd.run "sed -i '/^ServerActive=/ s/.*/ServerActive=$ServerActive/' /etc/zabbix/zabbix_agentd.conf" 注意 "" 结果不太对的 salt 'prod-sns-mq02' cmd.run "hostname |sed -n '2p' |awk '{print $1}'" prod-sns-mq02:
利用api批量添加主机
zbx_server version 4.0
get token
cat aip.sh # -*- coding:utf-8 -*- import urllib2 import json url = 'http://zabbix.com/zabbix/api_jsonrpc.php' header = {'Content-Type': 'application/json'} req = json.dumps( { "jsonrpc": "2.0", "method": "user.login", "params": { "user": "Admin", "password": "123456" }, "id": 0, } ) def auth(): r = urllib2.Request(url=url, headers=header, data=req) response = urllib2.urlopen(r) token = json.loads(response.read()) print(token) if __name__ == '__main__': auth() #token {u'jsonrpc': u'2.0', u'result': u'1794bcbe6d818069bff5aa499a07a960', u'id': 0}
Python调用Zabbix api之从入门到放弃——登录并获取身份验证令牌
我们获取必要的相关信息 ,eg所有主机list信息
groupid
cat getgroupid #获取指定groupid curl -s -X POST -H 'Content-Type:application/json' -d ' { "jsonrpc": "2.0", "method": "hostgroup.get", "params": { "output": "extend", "filter": { "name": [ "Zabbix servers", "Linux servers", "sns servers" ] } }, "auth": "1794bcbe6d818069bff5aa423a07a960", "id": 1 }' http://zbx.com/zabbix/api_jsonrpc.php | python3 -m json.tool
zabbix--api学习之路--get_hostgroup获取
templateid
#!/bin/bash #跑在我的freebsd12 #获取指定groupid curl -s -X POST -H 'Content-Type:application/json' -d ' { "jsonrpc": "2.0", "method": "hostgroup.get", "params": { "output": "extend", "filter": { "name": [ "Zabbix servers", "Linux servers" ] } }, "auth": "1794bcbe6d818069bff5aa423a07a960", "id": 1 }' http://148.66.11.55/zabbix/api_jsonrpc.php | python3 -m json.tool #output "result": [ { "groupid": "2", "name": "Linux servers", "internal": "0", "flags": "0" }, { "groupid": "4", "name": "Zabbix servers",
hostadd
批量添加版 用法 分别添加 ip and hostname #!/bin/bash #注释中文导致不成功 #"host": "'prod-sns-mq02'", #这里一般改为你要的hostname IP=( 40.243.52 41.102.111 40.218.197 40.216.19 ) HOSTNAME=( prod-sns-php01 prod-sns-php02 prod-sns-mq01 prod-sns-mq03 ) for no in `seq 0 echo ${#a[@]}' #for no in `seq 2 3` #这个是手工决定 数量 do curl -s -X POST -H 'Content-Type:application/json' -d ' { "jsonrpc": "2.0", "method": "host.create", "params": { "host": "'${HOSTNAME[$no]}'", "interfaces": [ { "type": 1, "main": 1, "useip": 1, "ip": "'${IP[$no]}'", "dns": "", "port": "10050" } ], "groups": [ { "groupid": "2", "groupid": "16" } ], "templates": [ { "templateid": "10001" } ] }, "auth": "1794bcbe6d818069bff5aa4207a960", "id": 1 }' http://zbx.com/zabbix/api_jsonrpc.php | python -m json.tool done #要添加多一个群组 #!/bin/bash #注释中文导致不成功 #"host": "'prod-sns-mq02'", #这里一般改为你要的hostname IP=" 121.41.103.113 " for node_ip in ${IP}; do curl -s -X POST -H 'Content-Type:application/json' -d ' { "jsonrpc": "2.0", "method": "host.create", "params": { "host": "'prod-sns-es01'", "interfaces": [ { "type": 1, "main": 1, "useip": 1, "ip": "'${node_ip}'", "dns": "", "port": "10050" } ], "groups": [ { "groupid": "2", "groupid": "16" } ], "templates": [ { "templateid": "10001" } ] }, "auth": "1794bcbe6d818069bff5aa423a07a960", "id": 1 }' http://148.66.11.55/zabbix/api_jsonrpc.php | python -m json.tool done #要添加多一个群组
进阶
写成通用的py脚本 ?