一、zabbix API 概述
Zabbix API允许你以编程方式检索和修改Zabbix的配置,并提供对历史数据的访问。它广泛用于:
创建新的应用程序以使用Zabbix;
将Zabbix与第三方软件集成;
自动执行常规任务。
Zabbix API是基于Web的API,作为Web前端的一部分提供。它使用JSON-RPC 2.0协议,这意味着两件事:
该API包含一组独立的方法;
客户端和API之间的请求和响应使用JSON格式进行编码。
二、获取令牌
[root@tomcat ~]# curl -s -X POST -H 'Content-Type: application/json' -d '{
"jsonrpc": "2.0",
"method": "user.login",
"params": {
"user": "Admin",
"password": "zabbix"
},
"id": 1,
"auth": null
}' http://10.0.0.71/zabbix/api_jsonrpc.php
{"jsonrpc":"2.0","result":"c6364595b488525afb17b9f3c9ee9376","id":1}
jsonrpc – API使用的JSON-RPC协议的版本; Zabbix API实现的JSON-RPC版本是2.0;
method – 被调用的API方法名;
params – 将被传递给API方法的参数;
id – 请求的任意标识符;
auth -用户认证令牌; 因为我们还没有一个,它的设置null。
method – 被调用的API方法名;
params – 将被传递给API方法的参数;
id – 请求的任意标识符;
auth -用户认证令牌; 因为我们还没有一个,它的设置null。
三、检索主机
curl -s -X POST -H 'Content-Type: application/json' -d '{
"jsonrpc": "2.0",
"method": "host.get",
"params": {
"output": [
"hostid",
"host"
],
"selectInterfaces": [
"interfaceid",
"ip"
]
},
"id": 2,
"auth": "7f59a9f697235782ff2ee0578c6cbb70"
}' http://10.0.0.110/zabbix/api_jsonrpc.php
[root@db01 ~]# curl -s -X POST -H 'Content-Type: application/json' -d '{
> "jsonrpc": "2.0",
> "method": "host.get",
> "params": {
> "output": [
> "hostid",
> "host"
> ],
> "selectInterfaces": [
> "interfaceid",
> "ip"
> ]
> },
> "id": 2,
> "auth": "7f59a9f697235782ff2ee0578c6cbb70"
> }' http://10.0.0.110/zabbix/api_jsonrpc.php
{"jsonrpc":"2.0","result":[{"hostid":"10084","host":"Zabbix server","interfaces":[{"interfaceid":"1","ip":"127.0.0.1"}]},{"hostid":"10287","host":"web01","interfaces":[{"interfaceid":"11","ip":"172.16.1.7"}]},{"hostid":"10288","host":"tomcat","interfaces":[{"interfaceid":"12","ip":"172.16.1.8"}]},{"hostid":"10289","host":"db01","interfaces":[{"interfaceid":"13","ip":"172.16.1.51"}]}],"id":2}
四、创建主机
curl -s -X POST -H 'Content-Type: application/json' -d '{
"jsonrpc": "2.0",
"method": "host.create",
"params": {
"host": "web01",
"interfaces": [
{
"type": 1,
"main": 1,
"useip": 1,
"ip": "172.16.1.7",
"dns": "",
"port": "10050"
}
],
"groups": [
{
"groupid": "15"
}
],
"templates": [
{
"templateid": "10285"
}
],
"inventory_mode": 0,
"inventory": {
"macaddress_a": "01234",
"macaddress_b": "56768"
}
},
"auth": "7f59a9f697235782ff2ee0578c6cbb70",
"id": 1
}' http://10.0.0.110/zabbix/api_jsonrpc.php
{"jsonrpc":"2.0","result":{"hostids":["10290"]},"id":1}
五、创建监控项
curl -s -X POST -H 'Content-Type: application/json' -d '{
"jsonrpc": "2.0",
"method": "item.create",
"params": {
"name": "用户登录的数量",
"key_": "login_number",
"hostid": "10290",
"type": 0,
"value_type": 3,
"interfaceid": "14",
"delay": 30
},
"auth": "7f59a9f697235782ff2ee0578c6cbb70",
"id": 3
}' http://10.0.0.110/zabbix/api_jsonrpc.php
{"jsonrpc":"2.0","result":{"itemids":["30228"]},"id":3}
六、删除主机
curl -s -X POST -H 'Content-Type: application/json' -d '{
"jsonrpc": "2.0",
"method": "host.delete",
"params": [
"10290"
],
"auth": "7f59a9f697235782ff2ee0578c6cbb70",
"id": 1
}' http://10.0.0.110/zabbix/api_jsonrpc.php
{"jsonrpc":"2.0","result":{"hostids":["10290"]},"id":1}
七、zabbix优化
1. Zabbix属于写多读少的业务,针对数据库做优化 磁盘使用固态 数据库引擎 tokudb innodb myisam
300*200=60000/30 = 2000个/s
2.将Zabbix-Agent改为主动模式,减轻server端压力
3.使用分布式监控 减轻server端压力
4.去掉没有的监控项,增加监控项的取值时间 减少历史保存周期
5.针对server端进程优化 谁的进程大就优化谁 根据具体情况进行优化,不是越大越好
6.针对server内存优化 谁的内存少就加大谁的内存
7.关注队列 是否有延迟的监控项
8.针对历史数据和趋势图进行分表

