欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Zabbix 部署

程序员文章站 2022-07-12 18:14:17
...

Linux下常用的系统监控软件有Nagios、Cacti、Zabbix、Monit等,这些开源的软件,可以帮助我们更好的管理机器,在第一时间内发现,并警告系统维护人员。

今天开始研究下Zabbix,使用Zabbix的目的,是为了能够更好的监控mysql数据库服务器,并且能够生成图形报表,虽然Nagios也能够生成图形报表,但没有Zabbix这么强大。

首先,我们先来介绍下Zabblx:

一.Zabbix简介

   zabbix是一个基于WEB界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案。
   zabbix由zabbix server与可选组件zabbix agent两部门组成。
   zabbix server可以通过SNMP,zabbix agent,ping,端口监视等方法提供对远程服务器/网络状态的监视。
   zabbix agent需要安装在被监视的目标服务器上,它主要完成对硬件信息或与操作系统有关的内存,CPU等信息的收集。

zabbix的主要特点:
- 安装与配置简单,学习成本低
- 支持多语言(包括中文)
- 免费开源
- 自动发现服务器与网络设备
- 分布式监视以及WEB集中管理功能
- 可以无agent监视
- 用户安全认证和柔软的授权方式
- 通过WEB界面设置或查看监视结果
- email等通知功能
等等

Zabbix主要功能:
- CPU负荷
- 内存使用
- 磁盘使用
- 网络状况
- 端口监视
- 日志监视

官方也提供了安装资料:http://www.zabbix.com/wiki/howto/monitor

二.Zabbix安装

1、 zabbix WEB环境搭建

[aaa@qq.com ~]# ls
anaconda-ks.cfg                          zabbix-api
fping-3.10-1.el7.x86_64.rpm              zabbix-get-3.4.6-1.el7.x86_64.rpm
iksemel-1.4-2.el7.centos.x86_64.rpm      zabbix-java-gateway-3.4.6-1.el7.x86_64.rpm
php-bcmath-5.4.16-36.el7_1.x86_64.rpm    zabbix-proxy-mysql-3.4.6-1.el7.x86_64.rpm
php-mbstring-5.4.16-36.el7_1.x86_64.rpm  zabbix-server-mysql-3.4.6-1.el7.x86_64.rpm
zabbix-agent-3.4.6-1.el6.x86_64.rpm      zabbix-web-3.4.6-1.el7.noarch.rpm
zabbix-agent-3.4.6-1.el7.x86_64.rpm      zabbix-web-mysql-3.4.6-1.el7.noarch.rpm
[aaa@qq.com ~]# rm -fr zabbix-agent-3.4.6-1.el6.x86_64.rpm   企业6,冲突,删除
[aaa@qq.com ~]# yum install -y *.rpm

Zabbix 部署

2、 zabbix 数据库设置

 zabbix数据库可以和zabbix服务器分离,采用用专门的mysql服务器存储数据,此时要给zabbix数据库受相应的权限。
[aaa@qq.com ~]# cd /etc/zabbix/
[aaa@qq.com zabbix]# ls
web                 zabbix_java_gateway.conf         zabbix_server.conf
zabbix_agentd.conf  zabbix_java_gateway_logback.xml
zabbix_agentd.d     zabbix_proxy.conf
[aaa@qq.com zabbix]# yum install mariadb-server -y
[aaa@qq.com zabbix]# systemctl start mariadb
[aaa@qq.com zabbix]# mysql_secure_installation 
Enter current password for root (enter for none): 
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] y
New password: 
Re-enter new password: 
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] y
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] y
 ... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!
[aaa@qq.com zabbix]# mysql -pwestos
MariaDB [(none)]> create database zabbix character set utf8 collate utf8_bin;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> grant all privileges on zabbix.* to aaa@qq.com identified by 'westos';
Query OK, 0 rows affected (0.00 sec)

Zabbix 部署

3、安装zabbix服务

[root@server1 ~]# cd /usr/share/doc/zabbix-server-mysql-3.4.6/
[root@server1 zabbix-server-mysql-3.4.6]# ls
AUTHORS  ChangeLog  COPYING  create.sql.gz  NEWS  README
[root@server1 zabbix-server-mysql-3.4.6]# zcat create.sql.gz | mysql -u zabbix -p zabbix
Enter password:  //输入授权用户zabbix的密码,然后等3分钟
[root@server1 zabbix-server-mysql-3.4.6]# cd /etc/zabbix
[root@server1 zabbix]# vim zabbix_server.conf 

Zabbix 部署

[root@server1 zabbix]# yum install httpd
[root@server1 zabbix]# vim /etc/httpd/conf.d/zabbix.conf  ##改时区

Zabbix 部署

4、浏览器访问:

http://172.25.254.11/zabbix/setup.php

Zabbix 部署

Zabbix 部署

Zabbix 部署

Zabbix 部署

Zabbix 部署

Zabbix 部署

输入zabbix原始用户名Admin,密码zabbix

Zabbix 部署

Zabbix 部署

点击人头修改语言和密码,重新登陆

Zabbix 部署

Zabbix 部署

5、添加主机:

添加方式一:

动作->事件源:自动发现

自动发现,点击Local network添加网段

Zabbix 部署

点击启用

Zabbix 部署

就会自动发现 server3

Zabbix 部署

添加方式二:先关闭自动发现,再关闭动作

点击创建主机:

Zabbix 部署

Zabbix 部署

主机名称为客户端主机名称,群组,

Zabbix 部署

Zabbix 部署

Zabbix 部署

添加完毕:

Zabbix 部署

添加方式三:

删除刚才添加的 server 3 172.25.50.3

选择主机 -> 自动注册 -> 点击创建动作

Zabbix 部署

点击新的

Zabbix 部署

添加主机、添加到主机群组选择linux servers、连接到模版选择Template OS linux

Zabbix 部署

稍等刷新,就也可以看到添加成功

Zabbix 部署

6、用api删除:


[aaa@qq.com ~]# vim zabbix-api.sh 
curl -s -X POST -H 'Content-Type:application/json' -d '
{
    "jsonrpc": "2.0",
    "method": "user.login",
    "params": {
        "user": "Admin",
        "password": "westos"
    },
    "id": 1,
    "auth": null
}' http://172.25.254.11/zabbix/api_jsonrpc.php | python -m json.tool
[aaa@qq.com ~]# chmod +x ./zabbix-api.sh 
[aaa@qq.com ~]# ./zabbix-api.sh   ##执行脚本查询
{
    "id": 1,
    "jsonrpc": "2.0",
    "result": "3b31002c01ecc1bf9164c67eb17f5e14"   ##将这串数字写入 zabbix-api.sh 中的 auth 中
}

[aaa@qq.com ~]# vim zabbix-api.sh
curl -s -X POST -H 'Content-Type:application/json' -d '
{
    "jsonrpc": "2.0",
    "method": "host.get",
    "params": {
        "output": ["host"]
    },
    "auth": "3b31002c01ecc1bf9164c67eb17f5e14",
    "id": 1
}' http://172.25.254.11/zabbix/api_jsonrpc.php | python -m json.tool
[aaa@qq.com ~]# ./zabbix-api.sh 
{
    "id": 1,
    "jsonrpc": "2.0",
    "result": [
        {
            "host": "Zabbix server",
            "hostid": "10084"
        },
        {
            "host": "server3",
            "hostid": "10261"
        }
    ]
}

[aaa@qq.com ~]# vim zabbix-api.sh 
curl -s -X POST -H 'Content-Type:application/json' -d '
{
    "jsonrpc": "2.0",
    "method": "host.delete",
    "params": [
        "10261"
    ],
    "auth": "3b31002c01ecc1bf9164c67eb17f5e14",
    "id": 1
}' http://172.25.254.11/zabbix/api_jsonrpc.php | python -m json.tool
[aaa@qq.com ~]# ./zabbix-api.sh 
{
    "id": 1,
    "jsonrpc": "2.0",
    "result": {
        "hostids": [
            "10261"
        ]
    }
}
刷新浏览器,刚才添加的已经删除:

Zabbix 部署

7、用api添加:


[aaa@qq.com ~]# vim zabbix-api.sh 
curl -s -X POST -H 'Content-Type:application/json' -d '
{
    "jsonrpc": "2.0",
    "method": "host.create",
    "params": {
        "host": "server3",
        "interfaces": [
            {
                "type": 1,
                "main": 1,
                "useip": 1,
                "ip": "172.25.50.11",
                "dns": "",
                "port": "10050"
            }
        ],
        "groups": [
            {
                "groupid": "2"
            }
        ],
        "templates": [
            {
                "templateid": "10001"
            }
        ]
    },
    "auth": "3b31002c01ecc1bf9164c67eb17f5e14",
    "id": 1
}' http://172.25.254.11/zabbix/api_jsonrpc.php | python -m json.tool

[aaa@qq.com ~]# ./zabbix-api.sh 
{
    "error": {
        "code": -32602,
        "data": "Host with the same name \"server6\" already exists.",
        "message": "Invalid params."
    },
    "id": 1,
    "jsonrpc": "2.0"
}
浏览器查看已经添加:

Zabbix 部署

[root@server1 ~]# mkdir /var/lib/zabbix
[root@server1 ~]# cd /var/lib/zabbix/
[root@server1 zabbix]# vim .my.cnf
[mysql]
user=root
password=westos
socket=/var/lib/mysql/mysql.scok
[mysqladmin]
user=root
password=westos
socket=/var/lib/mysql/mysql.scok
[root@server1 zabbix]# systemctl restart zabbix-agent
添加mysql和http模块

Zabbix 部署

Zabbix 部署

Zabbix 部署