zabbix性能优化及API

zabbix性能优化及API

zabbix性能调优

1) Zabbix属于写多读少的业务, 所以需要针对zabbix的MySQL进行拆分。MySQL一定要使用SSD固态盘

2) 将Zabbix-Agent被动监控模式, 调整为主动监控模式。

3) 使用zabbix-proxy分布式监控, 在大规模监控时用于缓解Zabbix-Server压力

4) 去掉无用监控项, 增加监控项的取值间隔, 减少历史数据保存周期(由housekeeper进程定时清理)

5)针对于Zabbix-server进程调优, 谁忙就加大谁的进程数量, 具体取决实际情况, 不是越大越好

[root@web02 ~]# vim /etc/zabbix/zabbix_server.conf
StartPollers=20
StartPollersUnreachable=20
...

6)针对于Zabbix-server缓存调优, 谁的剩余内存少, 就加大它的缓存值(zabbix cache usage图表)

[root@web02 ~]# vim /etc/zabbix/zabbix_server.conf
CacheSize=8M
HistoryCacheSize=16M
HistoryIndexCacheSize=4M

7) 关注管理->队列, 是否有被延迟执行的监控项

image-20230821113444867

jq解析

# 安装
yum install -y jq

# 使用方法
[root@db01 ~]# curl -XPOST -H 'Content-Type: application/json-rpc' http://zabbix.driverzeng.com/api_jsonrpc.php -d '
 {
     "jsonrpc": "2.0",
     "method": "user.login",
     "params": {
         "user": "Admin",
         "password": "zabbix"
     },
     "id": 1,
     "auth": null
 }'|jq

   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   239    0    68  100   171    800   2012 --:--:-- --:--:-- --:--:--  2035
{
  "jsonrpc": "2.0",
  "result": "7e93d9485cea0d0d31b0e8f37a5c27f1",
  "id": 1
}

zabbix API

概览

Zabbix API允许你以编程方式检索和修改Zabbix的配置,并提供对历史数据的访问。它广泛用于:

  • 创建新的应用程序以使用Zabbix;
  • 将Zabbix与第三方软件集成;
  • 自动执行常规任务。

Zabbix API是基于Web的API,作为Web前端的一部分提供。它使用JSON-RPC 2.0协议,这意味着两点:

  • 该API包含一组独立的方法;
  • 客户端和API之间的请求和响应使用JSON格式进行编码。

有关协议和JSON的更多信息可以在 JSON-RPC 2.0 规范JSON 格式主页中找到。

结构

Zabbix API由许多名义上分组的独立API方法组成。每个方法执行一个特定任务。例如,方法 host.create 隶属于 host 这个API分组 ,用于创建新主机。历史上,API分组有时被称为“类”。

大多数API至少包含四种方法: getcreateupdatedelete ,分别是检索,创建,更新和删除数据,但是某些API提供一套完全不同的方法。

执行请求

当完成了前端的安装配置后,你就可以使用远程HTTP请求来调用API。为此,需要向位于前端目录中的 api_jsonrpc.php 文件发送HTTP POST请求。例如,如果你的Zabbix前端安装在 http://company.com/zabbix, 那么用HTTP请求来调用 apiinfo.version 方法就如下面这样:

POST http://company.com/zabbix/api_jsonrpc.php HTTP/1.1
       Content-Type: application/json-rpc

       {"jsonrpc":"2.0","method":"apiinfo.version","id":1,"auth":null,"params":{}}

请求的 Content-Type 头部必须设置为以下值之一: application/json-rpc, application/jsonapplication/jsonrequest

你可以使用任何HTTP客户端或JSON-RPC测试工具手动执行API请求,但对于开发应用程序,我们建议使用 [社区维护的程序库](http://zabbix.org/wiki/Docs/api/libraries)。 :::

## 示例

以下部分将详细介绍一些使用示例。

### 验证

在访问Zabbix中的任何数据之前,你需要登录并获取身份验证令牌。这可以使用该 [user.login](https://www.zabbix.com/documentation/5.0/zh/manual/5.0/zh/manual/api/reference/user/login) 方法完成。让我们假设你想要以标准Zabbix Admin用户身份登录。然后,你的JSON请求将如下所示:

```
{
"jsonrpc": "2.0",
"method": "user.login",
"params": {
"user": "Admin",
"password": "zabbix"
},
"id": 1,
"auth": null
}
```

#### 验证zabbix登录用户获取token(身份验证令牌)

```bash
[root@db01 ~]# curl -XPOST -H 'Content-Type: application/json-rpc' http://zabbix.driverzeng.com/api_jsonrpc.php -d '
{
"jsonrpc": "2.0",
"method": "user.login",
"params": {
"user": "Admin",
"password": "zabbix"
},
"id": 1,
"auth": null
}'
{"jsonrpc":"2.0","result":"56436a2cec8f20422089416e85c47041","id":1}

```

![image-20230821120419056](https://www.xiaoyuanwiki.com/image/image-20230821120419056.png)

让我们仔细看看示例请求对象。它具有以下属性:

- jsonrpc - API使用的JSON-RPC协议的版本; Zabbix API实现的JSON-RPC版本是2.0;
- method - 被调用的API方法名;
- params - 将被传递给API方法的参数;
- id - 请求的任意标识符;
- auth -用户认证令牌; 如果没有的话可以设置为null。

**如果你正确提供了凭据,API返回的响应将包含用户身份验证令牌:**

```json
{
"jsonrpc": "2.0",
"result": "0424bd59b807674191e7d77572075f33",
"id": 1
}
```

响应对象又包含以下属性:

- jsonrpc - JSON-RPC协议的版本;
- result - 请求返回的数据;
- id - 相应请求的id。

### 主机

#### 检索主机

我们现在有一个有效的用户身份验证令牌,可以用来访问Zabbix中的数据。 例如,让我们使用 [host.get](https://www.zabbix.com/documentation/5.0/zh/manual/zh/manual/api/reference/host/get) 方法检索所有已配置[主机](https://www.zabbix.com/documentation/5.0/zh/manual/zh/manual/api/reference/host/object)的ID,主机名和接口 :

```json
{
"jsonrpc": "2.0",
"method": "host.get",
"params": {
"output": [
"hostid",
"host"
],
"selectInterfaces": [
"interfaceid",
"ip"
]
},
"id": 2,
"auth": "0424bd59b807674191e7d77572075f33"
}
```

**请注意, auth 属性现在设置为我们通过调用user.login方法获得的身份验证令牌。**

响应对象将包含有关主机的请求的数据:

```json
{
"jsonrpc": "2.0",
"result": [
{
"hostid": "10084",
"host": "Zabbix server",
"interfaces": [
{
"interfaceid": "1",
"ip": "127.0.0.1"
}
]
}
],
"id": 2
}
```

出于性能原因,我们建议始终列出要检索的对象属性,并避免检索所有内容。 :::

#### 获取主机组ID

```bash
# 主机
curl -s -XPOST -H 'Content-Type: application/json-rpc' -d '
{
"jsonrpc": "2.0",
"method": "hostgroup.get",
"params": {
"output": "extend",
"filter": {
"name": [
"自定义监控项"
]
}
},
"id": 1,
"auth": "56436a2cec8f20422089416e85c47041"
}' http://zabbix.driverzeng.com/api_jsonrpc.php|jq -r '.result[0].groupid'

# 结果
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 487 0 125 100 362 2984 8643 --:--:-- --:--:-- --:--:-- 8829
{
"jsonrpc": "2.0",
"result": [
{
"groupid": "18",
"name": "自定义监控项",
"internal": "0",
"flags": "0"
}
],
"id": 1
}
```

![image-20230821162543102](https://www.xiaoyuanwiki.com/image/image-20230821162543102.png)

### 获取模板ID

```bash
# 模板
curl -XPOST -H 'Content-Type: application/json-rpc' -d '
{
"jsonrpc": "2.0",
"method": "template.get",
"params": {
"output": "extend",
"filter": {
"host": [
"Diy item"
]
}
},
"id": 1,
"auth": "56436a2cec8f20422089416e85c47041"
}' http://zabbix.driverzeng.com/api_jsonrpc.php|jq

# 结果
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 1180 0 829 100 351 11226 4753 --:--:-- --:--:-- --:--:-- 11356
{
"jsonrpc": "2.0",
"result": [
{
"proxy_hostid": "0",
"host": "Diy item",
"status": "3",
"disable_until": "0",
"error": "",
"available": "0",
"errors_from": "0",
"lastaccess": "0",
"ipmi_authtype": "-1",
"ipmi_privilege": "2",
"ipmi_username": "",
"ipmi_password": "",
"ipmi_disable_until": "0",
"ipmi_available": "0",
"snmp_disable_until": "0",
"snmp_available": "0",
"maintenanceid": "0",
"maintenance_status": "0",
"maintenance_type": "0",
"maintenance_from": "0",
"ipmi_errors_from": "0",
"snmp_errors_from": "0",
"ipmi_error": "",
"snmp_error": "",
"jmx_disable_until": "0",
"jmx_available": "0",
"jmx_errors_from": "0",
"jmx_error": "",
"name": "自定义监控项",
"flags": "0",
"templateid": "10439",
"description": "",
"tls_connect": "1",
"tls_accept": "1",
"tls_issuer": "",
"tls_subject": "",
"tls_psk_identity": "",
"tls_psk": "",
"proxy_address": "",
"auto_compress": "1"
}
],
"id": 1
}
```

![image-20230821163341700](https://www.xiaoyuanwiki.com/image/image-20230821163341700.png)

### 创建

#### 使用zabbix API创建主机

需要修改的重要信息有:

对于[标准的主机属性](https://www.zabbix.com/documentation/5.0/zh/manual/api/reference/host/object#host),该方法接受下列参数。

| 属性 | 类型 | 描述 |
| :-------------------- | :------------------------- | ------------------------------------------------------------ |
| **groups** (必选) | 对象/数组 添加主机组 | 主机组必须已定义groupid属性。 |
| **interfaces** (必选) | 对象/数组 为主机的信息 | 主机创建的ip、端口等各种信息 |
| tags | 对象/数组 主机标签 | 为主机命名标签 |
| templates | 对象/数组 链接到主机的模板 | 模板必须是已查询过的templateid属性。 |
| macros | 对象/数组 为主机创建用户宏 | 主机创建的[用户宏](https://www.zabbix.com/documentation/5.0/zh//zh/manual/api/reference/usermacro/object)。 |
| inventory | 对象 主机 | [资产清单](https://www.zabbix.com/documentation/5.0/zh//zh/manual/api/reference/host/object#host-inventory)属性。 |

```bash
curl -XPOST -H 'Content-Type: application/json-rpc' -d '
{
"jsonrpc": "2.0",
"method": "host.create",
"params": {
"host": "web04",
"interfaces": [
{
"type": 1,
"main": 1,
"useip": 1,
"ip": "10.0.0.18",
"dns": "",
"port": "10050"
}
],
"groups": [
{
"groupid": "18"
}
],
"tags": [
{
"tag": "Host name",
"value": "Linux server"
}
],
"templates": [
{
"templateid": "10439"
}
],
"macros": [
{
"macro": "{$USER_ID}",
"value": "123321"
},
{
"macro": "{$USER_LOCATION}",
"value": "0:0:0",
"description": "latitude, longitude and altitude coordinates"
}
],
"inventory_mode": 0,
"inventory": {
"macaddress_a": "01234",
"macaddress_b": "56768"
}
},
"auth": "56436a2cec8f20422089416e85c47041",
"id": 1
}' http://zabbix.driverzeng.com/api_jsonrpc.php

```

![image-20230821163540189](https://www.xiaoyuanwiki.com/image/image-20230821163540189.png)

#### 创建新监控项

让我们使用从上一个请求host.get中获得的数据,在主机 “Zabbix server” 上创建一个新 [监控项](https://www.zabbix.com/documentation/5.0/zh/manual/zh/manual/api/reference/item/object) 。这个可以通过使用方法 [item.create](https://www.zabbix.com/documentation/5.0/zh/manual/zh/manual/api/reference/item/create):

```json
{
"jsonrpc": "2.0",
"method": "item.create",
"params": {
"name": "Free disk space on $1",
"key_": "vfs.fs.size[/home/joe/,free]",
"hostid": "10084",
"type": 0,
"value_type": 3,
"interfaceid": "1",
"delay": 30
},
"auth": "0424bd59b807674191e7d77572075f33",
"id": 3
}
```

成功的响应将包含新创建监控项的ID,可用于在以后请求中引用监控项:

```
{
"jsonrpc": "2.0",
"result": {
"itemids": [
"24759"
]
},
"id": 3
}
```

item.create 方法和其他的创建(create)方法,也可以接受对象数组,并通过一次API调用中创建多个监控项。

#### 创建多个触发器

因此,如果create方法接受数组,我们可以添加多个[触发器](https://www.zabbix.com/documentation/5.0/zh/manual/zh/manual/api/reference/trigger/object),像这样^-^:

```json
{
"jsonrpc": "2.0",
"method": "trigger.create",
"params": [
{
"description": "Processor load is too high on {HOST.NAME}",
"expression": "{Linux server:system.cpu.load[percpu,avg1].last()}>5",
},
{
"description": "Too many processes on {HOST.NAME}",
"expression": "{Linux server:proc.num[].avg(5m)}>300",
}
],
"auth": "0424bd59b807674191e7d77572075f33",
"id": 4
}
```

操作成功的响应将包含新创建的触发器的ID:

```json
{
"jsonrpc": "2.0",
"result": {
"triggerids": [
"17369",
"17370"
]
},
"id": 4
}
```

### 更新

#### 更新监控项

启用监控项,即将其状态设置为“0”:

```json
{
"jsonrpc": "2.0",
"method": "item.update",
"params": {
"itemid": "10092",
"status": 0
},
"auth": "0424bd59b807674191e7d77572075f33",
"id": 5
}
```

操作成功的响应将包含被更新的触发器的ID:

```json
{
"jsonrpc": "2.0",
"result": {
"itemids": [
"10092"
]
},
"id": 5
}
```

**item.update 方法以及其他更新方法也可以接受对象数组,并通过一次API调用更新多个监控项。**

#### 更新多个触发器

启用多个触发器,即将其状态设置为0:

```json
{
"jsonrpc": "2.0",
"method": "trigger.update",
"params": [
{
"triggerid": "13938",
"status": 0
},
{
"triggerid": "13939",
"status": 0
}
],
"auth": "0424bd59b807674191e7d77572075f33",
"id": 6
}
```

成功的响应将包含被更新的触发器的ID数组:

```json
{
"jsonrpc": "2.0",
"result": {
"triggerids": [
"13938",
"13939"
]
},
"id": 6
}
```

这是更新的首选方法。一些API方法 host.massupdate 允许编写更简单的代码,但不建议使用这些方法,因为它们将在未来的版本中删除。

### 错误处理

到目前为止,我们试过的一切工作正常。但是,如果我们尝试对API调用不正确会发生什么?让我们尝试通过调用[host.create](https://www.zabbix.com/documentation/5.0/zh/manual/zh/manual/api/reference/host/create)创建另一个主机, 但省略一个必填 groups 参数。

```json
{
"jsonrpc": "2.0",
"method": "host.create",
"params": {
"host": "Linux server",
"interfaces": [
{
"type": 1,
"main": 1,
"useip": 1,
"ip": "192.168.3.1",
"dns": "",
"port": "10050"
}
]
},
"id": 7,
"auth": "0424bd59b807674191e7d77572075f33"
}
```

这个请求的返回会包含一个错误信息:

```json
{
"jsonrpc": "2.0",
"error": {
"code": -32602,
"message": "Invalid params.",
"data": "No groups for host \"Linux server\"."
},
"id": 7
}
```

如果发生错误,响应对象将包含error 而不是 result 属性,同时error将具有以下数据的属性:

- code - 错误代码;
- message - 一个简短的错误摘要;
- data - 更详细的错误消息。

错误可能发生在不同的情况下,例如,使用不正确的输入值,会话超时或试图访问不存在的对象。你的应用程序应该能够优雅地处理这些类型的错误。

## 调用zabbix接口,创建主机脚本

```bash
#!/bin/bash
mkdir json
read -p "输入创建主机IP:" ip
read -p "输入创建主机名:" hostname
read -p "连接的模板名:" template
read -p "连接的主机组名:" hostgroups
url=http://zabbix.driverzeng.com/api_jsonrpc.php
# 查询token值
auth=$(curl -s -XPOST -H 'Content-Type: application/json-rpc' -d '
{
"jsonrpc": "2.0",
"method": "user.login",
"params": {
"user": "Admin",
"password": "zabbix"
},
"id": 1,
"auth": null
}' $url| jq -r '.result')

# 查询主机组的id
cat > ./json/groupid.json<<E
{
"jsonrpc": "2.0",
"method": "hostgroup.get",
"params": {
"output": "extend",
"filter": {
"name": [
"$hostgroups"
]
}
},
"id": 1,
"auth": "$auth"
}
E
groupid=$(curl -s -XPOST -H 'Content-Type: application/json-rpc' -d "
$(cat ./json/groupid.json)" $url| jq -r '.result[0].groupid')

# 查询模板的id
cat > ./json/template.json<<E
{
"jsonrpc": "2.0",
"method": "template.get",
"params": {
"output": "extend",
"filter": {
"host": [
"$template"
]
}
},
"id": 1,
"auth": "$auth"
}
E
templateid=$(curl -s -XPOST -H 'Content-Type: application/json-rpc' -d "$(cat ./json/template.json)" $url| jq -r '.result[0].templateid')

# 创建主机
cat > ./json/host.json<<E
{
"jsonrpc": "2.0",
"method": "host.create",
"params": {
"host": "$hostname",
"interfaces": [
{
"type": 1,
"main": 1,
"useip": 1,
"ip": "$ip",
"dns": "",
"port": "10050"
}
],
"groups": [
{
"groupid": "$groupid"
}
],
"tags": [
{
"tag": "Host name",
"value": "Linux server"
}
],
"templates": [
{
"templateid": "$templateid"
}
],
"inventory_mode": 0,
"inventory": {
"macaddress_a": "01234",
"macaddress_b": "56768"
}
},
"auth": "$auth",
"id": 1
}
E
curl -s -XPOST -H 'Content-Type: application/json-rpc' -d "$(cat ./json/host.json)" $url |jq
```

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇