文章目录
zabbix(二)配置并了解规则安装及配置 zabbix登录 zabbixzabbix 监控配置自动发现配置自动注册zabbix 的 API 接口
zabbix(二)配置并了解规则
节点
node1(172.25.0.1)zabbix-servernode2(172.25.0.2)zabbix-agentnode3(172.25.0.3)zabbix-agent
我们使用的是zabbix4.0
安装及配置 zabbix
下载相关安装包
yum
install -y zabbix-server-mysql-4.0.5-1.el7.x86_64.rpm
zabbix-web-mysql-4.0.5-1.el7.noarch.rpm zabbix-web-4.0.5-1.el7.noarch.rpm
zabbix-agent-4.0.5-1.el7.x86_64.rpm php-mbstring-5.4.16-42.el7.x86_64.rpm
php-bcmath-5.4.16-42.el7.x86_64.rpm
fping-3.10-1.el7.x86_64.rpm
iksemel-1.4-2.el7.centos.x86_64.rpm
[root@server1 ~
]
[root@server1 ~
]
[root@server1 mysql
]
MariaDB
[(none
)]> create database zabbix character
set utf8 collate utf8_bin
;
MariaDB
[(none
)]> grant all privileges on zabbix.* to zabbix@localhost
identified by
'redhat';
[root@server1 ~
]
| mysql -uzabbix -p zabbix
vi /etc/zabbix/zabbix_server.conf
DBHost
=localhost
DBName
=zabbix
DBUser
=zabbix
DBPassword
=<password
>
systemctl start zabbix-server
systemctl start zabbix-agent
vim /etc/httpd/conf.d/zabbix.conf
20 php_value date.timezone Asia/Shanghai
systemctl start httpd
http://172.25.0.1/zabbix
Password
: redhat
Name
: Zabbix server
登录 zabbix
Username
: Admin
Password
: zabbix
1
>设置中文
点击设置,language chinese,提交
2
>再开一台虚拟机做 agent
[root@server2 ~
]
3
>配置监控
点击 配置--
>主机
当前只有一个 server 自己,添加 server2
点击右上角创建主机
主机之间要有解析,给 server2 分配一个群组
设置好其他信息后点击添加
3
>添加监控模板
点击选择--
> 右上角过滤模板为操作系统
然后点击选择--
>再点击添加
(这里不点添加是无效的
) --
> 更新
此时看到此主机监控可用性栏里 ZBX 选项不生效,因为没有配置 zabbix-agent
4
>配置 zabbix-agent
Vim /etc/zabbix/zabbix_agentd.conf
98 Server
=172.25.0.1
139 ServerActive
=172.25.0.1
150 Hostname
=server2
systemctl start zabbix-agent
cat /var/log/zabbix/zabbix_agentd.log
cat /var/log/zabbix/zabbix_server.log
1312:20190417:105603.495 Zabbix agent item
"agent.ping" on host
"server2" failed: first network error,
wait for 15 seconds
1313:20190417:105703.173 temporarily disabling Zabbix agent checks on
host
"server2": host unavailable
1313:20190417:110503.298 enabling Zabbix agent checks on host
"server2":
host became available
看到最后检查成功
此时看到此主机监控可用性栏里 ZBX 选项生效
zabbix 监控配置自动发现
自动发现规则主要针对企业批量添加主机,如果使用传统办法去单个添加设备、分组、项目、图像……,这是一个任务量巨大的一个工程,由此引入自动发现规则。 自动发现规则由服务端主动发起,Zabbix Server开启发现进程,定时扫描局域网中IP服务器、设备,实现自动刚发现主机、自动将主机添加到主机组、自动加载模板、自动创建项目(item)、自动创建图像等功能。
1>先删除刚才添加的 server2 2>点击配置 --> 动作 --> 右上角选择自动发现 --> 点击激活 3>更改动作配置 然后点击添加 4>点击自动发现 配置发现 IP 范围并启用,设置更新间隔为 60s,不然太慢 5>回到主机页面,看到 server2 已经加入群组
配置自动注册
Zabbix的自动注册规则主要用于Agent主动且自动向Server注册,适用于当前云环境下的监控。 Zabbix-Server在收到Zabbix-Agent的注册请求时,如果没有收到IP地址的值,则使用Zabbix-Agent和Zabbix-Server建立TCP连接时使用的IP地址。 下面,详细介绍该模块的使用。
1
>再启动一台 server3 来做自动注册
[root@server3 ~
]
[root@server3 ~
]
98 Server
=172.25.0.1
139 ServerActive
=172.25.0.1
150 Hostname
=server3
2
>先禁用之前的自动发现规则
并删除自动发现添加的 server3
3>创建自动注册动作
4
>在 server3 上重启 zabbix-agent
systemctl restart zabbix-agent
稍后可以看到 server3 又加入了群组
zabbix 的 API 接口
1
>测试并获取身份令牌
[root@server1 ~
]
curl -XPOST -H
"Content-Type: application/json-rpc" -d
'
{
"jsonrpc": "2.0",
"method": "user.login",
"params": {
"user": "Admin",
"password": "zabbix"
},
"id": 1,
"auth": null
}' http://172.25.0.1/zabbix/api_jsonrpc.php
运行脚本
[root@server1 ~
]
{"jsonrpc":"2.0",
"result":"fb1f413d4e85da6459eb21275e243e5e",
"id":1
}
[root@server1 ~
]
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
}' http://172.25.0.1/zabbix/api_jsonrpc.php
| python -m json.tool
[root@server1 ~
]
{
"id": 1,
"jsonrpc": "2.0",
"result": "6ef5080fe8782e54f7fb476271269843"
}
2
>使用身份令牌检索主机
[root@server1 ~
]
curl -s -XPOST -H
"Content-Type: application/json-rpc" -d
'
{
"jsonrpc": "2.0",
"method": "host.get",
"params": {
"output": [
"hostid",
"host"
],
"selectInterfaces": [
"interfaceid",
"ip"
]
},
"id": 2,
"auth": "6ef5080fe8782e54f7fb476271269843"
}' http://172.25.0.1/zabbix/api_jsonrpc.php
| python -m json.tool
[root@server1 ~
]
运行可看到 zabbix 添加的主机
3
>尝试 api 添加删除主机
先禁用自动注册
[root@server1 ~
]
curl -s -XPOST -H
"Content-Type: application/json-rpc" -d
'
{
"jsonrpc": "2.0",
"method": "host.delete",
"params": [
"10267"
],
"id": 2,
"auth": "6ef5080fe8782e54f7fb476271269843"
}' http://172.25.0.1/zabbix/api_jsonrpc.php
| python -m json.tool
[root@server1 ~
]
{
"id": 2,
"jsonrpc": "2.0",
"result": {
"hostids": [
"10267"
]
}
}
查看,server3 已经删除
4
>使用 api 创建主机
(1
)复制官网配置并做修改
[root@server1 ~
]
curl -s -XPOST -H
"Content-Type: application/json-rpc" -d
'
{
"jsonrpc": "2.0",
"method": "host.create",
"params": {
"host": "server3",
"interfaces": [
{
"type": 1,
"main": 1,
"useip": 1,
"ip": "172.25.0.3",
"dns": "",
"port": "10050"
}
],
"groups": [
{
"groupid": "2"
}
],
"templates": [
{
"templateid": "10001"
}
]
},
"id": 2,
"auth": "6ef5080fe8782e54f7fb476271269843"
}' http://172.25.0.1/zabbix/api_jsonrpc.php
| python -m json.tool
(2)Groupid 可以在 web 界面浏览器路径中看到,如下图,groupid=2 (3)templatesid 同理
(4
)运行
[root@server1 ~
]
{
"id": 2,
"jsonrpc": "2.0",
"result": {
"hostids": [
"10269"
]
}
}
添加成功