www.zabbix.com
环境介绍:
rhel7.3的虚拟机
zabbix-server ip地址: 172.25.40.11
1.安装zabbix 及httpd mariadb
yum install -y zabbix-agent-3.4.6-1.el7.x86_64.rpm zabbix-server-mysql-3.4.6-1.el7.x86_64.rpm zabbix-web-3.4.6-1.el7.noarch.rpm zabbix-web-mysql-3.4.6-1.el7.noarch.rpm iksemel-1.4-2.el7.centos.x86_64.rpm fping-3.10-1.el7.x86_64.rpm php-bcmath-5.4.16-42.el7.x86_64.rpm php-mbstring-5.4.16-42.el7.x86_64.rpm
yum install -y mariadb-server
yum install -y httpd
2.配置数据库
进入数据库后,
create database zabbix character set utf8 collate utf8_bin;
grant all privileges on zabbix.* to zabbix@localhost identified by 'westos';
然后导入表,
cd /usr/share/doc/zabbix-server-mysql-3.4.6/
zcat create.sql.gz | mysql zabbix #比较慢
3.配置zabbix
修改zabbix_server.conf
cd /etc/zabbix/
vim zabbix_server.conf
DBPassword=westos #数据库用户密码
4.配置http
cd /etc/httpd/conf.d/
vim zabbix.conf
php_value date.timezone Asia/Shanghai #修改php时区
5.打开服务
systemctl start httpd
systemctl start zabbix-server
iptables -F
浏览器访问本机ip加zabbix即可
然后按步骤安装
注:登陆用户为Admin 密码zabbix
这里server端开启agent即可看到主机开启
systemctl start zabbix-agent
同时设置zabbix-server zabbix-agent mariadb httpd 开机自启
systemctl enable mariadb
systemctl enable httpd
systemctl enable zabbix-server
systemctl enable zabbix-agent
1).手动添加agent主机
创建主机
添加模板:
注:agent配置文件添加Server=172.25.40.11
重启agent即可添加
2).自动发现agent主机:
首先删除之前手动添加的agent主机
添加自动发现规则(原有基础上修改)
启用发现规则
创建自动发现动作(原有基础上修改)
链接模版
启动动作
查看监测中仪表盘
此时查看主机
发现成功
3).自动注册agent主机:
首先删除agent主机,关闭发现规则,关闭自动发现动作
创建自动注册动作:
启动动作
重启agent端即可实现:
4).利用api接口实现(目前主要方式)
1.首先关闭所有发现规则及动作删除agent主机
2.编写api接口查看脚本
vim zabbix1.api
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://172.25.40.11/zabbix/api_jsonrpc.php | python -m json.tool
给予执行权限,执行,
chmod +x zabbix1.api
./zabbix1.api
result 即为api接口
3.编写添加主机脚本
vim zabbix2.api
curl -s -X POST -H 'Content-Type:application/json' -d '
{
"jsonrpc": "2.0",
"method": "host.create", #表明为添加主机
"params": {
"host": "server2", #主机名
"interfaces": [
{
"type": 1,
"main": 1,
"useip": 1,
"ip": "172.25.40.2", #主机ip
"dns": "",
"port": "10050" #端口
}
],
"groups": [
{
"groupid": "2" #查看主机群组Linux servers 可得
}
],
"templates": [
{
"templateid": "10001" #通过查看模版Template OS Linux 可得
}
]
},
"auth": "096e64eeebdef6576cbd7106fb817fc6", #此前的api接口
"id": 1
}' http://172.25.40.11/zabbix/api_jsonrpc.php | python -m json.tool
给予执行权限,执行
chmod +x zabbix2.api
./zabbix2.api
成功,此时查看,添加成功
4.删除指定agent
vim zabbix3.api
curl -s -X POST -H 'Content-Type:application/json' -d '
{
"jsonrpc": "2.0",
"method": "host.delete", #指明删除
"params": [
"10257" #通过查看agent主机信息 可得
],
"auth": "096e64eeebdef6576cbd7106fb817fc6", #api接口
"id": 1
}' http://172.25.40.11/zabbix/api_jsonrpc.php | python -m json.tool
给予执行权限,执行
chmod +x zabbix3.api
./zabbix3.api
此时查看agent已删除
这里是全部api操作脚本
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://172.25.0.4/zabbix/api_jsonrpc.php | python -m json.tool
curl -s -X POST -H 'Content-Type:application/json' -d '
{
"jsonrpc": "2.0",
"method": "host.get",
"params": {
"output": ["host"]
},
"auth": "9c9e60d2752e9b70e96a3a3c6adaba94",
"id": 1
}' http://172.25.0.4/zabbix/api_jsonrpc.php | python -m json.tool
curl -s -X POST -H 'Content-Type:application/json' -d '
{
"jsonrpc": "2.0",
"method": "host.delete",
"params": [
"10112"
],
"auth": "9c9e60d2752e9b70e96a3a3c6adaba94",
"id": 1
}' http://172.25.0.4/zabbix/api_jsonrpc.php | python -m json.tool
curl -s -X POST -H 'Content-Type:application/json' -d '
{
"jsonrpc": "2.0",
"method": "host.create",
"params": {
"host": "server6.example.com",
"interfaces": [
{
"type": 1,
"main": 1,
"useip": 1,
"ip": "172.25.0.6",
"dns": "",
"port": "10050"
}
],
"groups": [
{
"groupid": "2"
}
],
"templates": [
{
"templateid": "10110"
}
]
},
"auth": "9c9e60d2752e9b70e96a3a3c6adaba94",
"id": 1
}' http://172.25.0.4/zabbix/api_jsonrpc.php | python -m json.tool