搭建Zabbix | Zabbix监控实例(一)

一,概述

zabbix是一个基于WEB界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案。zabbix能监视各种网络参数,保证服务器系统的安全运营;并提供灵活的通知机制以让系统管理员快速定位/解决存在的各种问题。zabbix由2部分构成,zabbix server与可选组件zabbix agent。zabbix server可以通过SNMP,zabbix agent,ping,端口监视等方法提供对远程服务器/网络状态的监视,数据收集等功能,它可以运行在Linux,Solaris,HP-UX,AIX,Free BSD,Open BSD,OS X等平台上。

二,zabbix实现与应用

server1:172.25.28.1 rhel7.3

server2:172.25.28.2 rhel6.3

 

1,安装zabbix服务端软件

[root@server1 ~]# ls

fping-3.10-1.el7.x86_64.rpm

iksemel-1.4-2.el7.centos.x86_64.rpm

zabbix-agent-3.4.6-1.el7.x86_64.rpm

zabbix-get-3.4.6-1.el7.x86_64.rpm

zabbix-java-gateway-3.4.6-1.el7.x86_64.rpm

zabbix-proxy-mysql-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

php-mbstring-5.4.16-42.el7.x86_64.rpm

php-bcmath-5.4.16-42.el7.x86_64.rpm

[root@server1 ~]# yum install -y 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 fping-3.10-1.el7.x86_64.rpm  iksemel-1.4-2.el7.centos.x86_64.rpm  php-*

Installed:

  fping.x86_64 0:3.10-1.el7                    iksemel.x86_64 0:1.4-2.el7.centos     

  php-bcmath.x86_64 0:5.4.16-42.el7            php-mbstring.x86_64 0:5.4.16-42.el7   

  zabbix-server-mysql.x86_64 0:3.4.6-1.el7     zabbix-web.noarch 0:3.4.6-1.el7        

  zabbix-web-mysql.noarch 0:3.4.6-1.el7   

   2,安装数据库,初始化,用户授权

[root@server1 ~]# yum install mariadb-server -y

[root@server1 ~]# systemctl start mariadb

[root@server1 ~]# mysql_secure_installation

[root@server1 ~]# mysql -predhat

Welcome to the MariaDB monitor.  Commands end with ; or \g.

Your MariaDB connection id is 10

Server version: 5.5.52-MariaDB MariaDB Server



Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.



Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.



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 zabbix@localhost identified by 'westos';

Query OK, 0 rows affected (0.00 sec)



MariaDB [(none)]> Bye

3,导入数据库

[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        第一个zabbix是用户,第二个是数据库

Enter password: 用户授权密码

4,开启密码

[root@server1 zabbix-server-mysql-3.4.6]# cd /etc/zabbix/

[root@server1 zabbix]# vim zabbix_server.conf

扫描二维码关注公众号,回复: 2878155 查看本文章

密码 本地连接

5,修改时区,启动zabbix-server服务

[root@server1 zabbix]# cd /etc/httpd/conf.d/

[root@server1 conf.d]# vim zabbix.conf

[root@server1 conf.d]# systemctl start zabbix-server

6,启动apache服务,安装zabbix-agent并启动

[root@server1 conf.d]# systemctl start httpd

[root@server1 ~]# yum install -y zabbix-agent-3.4.6-1.el7.x86_64.rpm

[root@server1 ~]# systemctl start zabbix-agent

 

浏览器访问:


注意:登录用户默认是Admin 密码是zabbix

启用主机

 

监听主机

1,安装zabbix-agent软件,修改配置文件

server2:

[root@server2 ~]# rpm -ivh zabbix-agent-3.4.6-1.el6.x86_64.rpm

[root@server2 ~]# cd /etc/zabbix/

zabbix_agentd.conf  zabbix_agentd.d

[root@server2 zabbix]# vim zabbix_agentd.conf

zabbix-server服务端地址

注意:Hostname是本机域名,server2必须被解析

2,启动服务,查看端口是10050

[root@server2 zabbix]# /etc/init.d/zabbix-agent start

Starting Zabbix agent:                                     [  OK  ]

[root@server2 zabbix]# netstat -antlp

Active Internet connections (servers and established)

Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name  

tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      895/sshd           

tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      971/master         

tcp        0      0 0.0.0.0:10050               0.0.0.0:*                   LISTEN      1433/zabbix_agentd 

tcp        0      0 172.25.28.2:22              172.25.28.250:59098         ESTABLISHED 1355/sshd          

tcp        0      0 :::22                       :::*                        LISTEN      895/sshd           

tcp        0      0 ::1:25                      :::*                        LISTEN      971/master         

tcp        0      0 :::10050                    :::*                        LISTEN      1433/zabbix_agentd 

 

多种方法添加主机

方法一:

1,ZBS必须是绿色的

2,在配置>动作>自动发现 启用自动发现

 

3,配置>自动发现

点击Local network

修改范围 启动 更新

4,等待被发现

 

方法二:

1,停用自动发现

2,停用自动发现规则

3,停用主机,删除主机

4,创建主机

模板>选择

先点小添加,再点大添加

5,添加

6,等待

方法三:

1,删除已经存在的主机

2,自动注册,创建动作

3,定义名称

4,添加操作(小字)

5,修改操作类型为 添加主机

6,点击添加(小字)

7,同样方法 点击新的 添加到主机群组 选择

8,同样方法添加 与模版关联

9,添加(小字)

10,添加(大字)

11,等待

 

方法四:API接口

https://www.zabbix.com/documentation/3.4/manual/api

[root@server1 ~]# systemctl start zabbix-agent

1,[root@server1 ~]# vim zabbix-api.sh

curl -s -X POST -H 'Content-Type:application/json' -d '

{

    "jsonrpc": "2.0",

    "method": "user.login",

    "params": {

        "user": "Admin",

        "password": "redhat"

    },

    "id": 1,

    "auth": null

}' http://172.25.28.1/zabbix/api_jsonrpc.php | python -m json.tool

[root@server1 ~]# chmod +x zabbix-api.sh

[root@server1 ~]# ./zabbix-api.sh

{

    "id": 1,

    "jsonrpc": "2.0",

    "result": "288a283c7a467b2df2529326c2e77e0d"

}

获取"result": "288a283c7a467b2df2529326c2e77e0d",这个很重要,必须一致

2,[root@server1 ~]# vim zabbix-api.sh

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": "2e71445ea0ae33cb19be5a77dfb439a5"

}' http://172.25.28.1/zabbix/api_jsonrpc.php | python -m json.tool

[root@server1 ~]# ./zabbix-api.sh

{

    "id": 2,

    "jsonrpc": "2.0",

    "result": [

        {

            "host": "Zabbix server",

            "hostid": "10084",

            "interfaces": [

                {

                    "interfaceid": "1",

                    "ip": "127.0.0.1"

                }

            ]

        },

        {

            "host": "server2",

            "hostid": "10254",

            "interfaces": [

                {

                    "interfaceid": "2",

                    "ip": "172.25.28.2"

                }

            ]

        }

    ]

}

3,删除主机

[root@server1 ~]# vim zabbix-api.sh

curl -s -X POST -H 'Content-Type:application/json' -d '

{

    "jsonrpc": "2.0",

    "method": "host.delete",

    "params": [

        "10254"

    ],

    "id":2,

    "auth": "288a283c7a467b2df2529326c2e77e0d"

}' http://172.25.28.1/zabbix/api_jsonrpc.php | python -m json.tool



[root@server1 ~]# ./zabbix-api.sh

{

    "id": 2,

    "jsonrpc": "2.0",

    "result": {

        "hostids": [

            "10254"

        ]

    }

}

浏览器监控查看主机被删除

4,添加主机

[root@server1 ~]# vim zabbix-api.sh

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.28.2",

                "dns": "",

                "port": "10050"

            }

        ],

        "groups": [

            {

                "groupid": "2"

            }

        ],

        "templates": [

            {

                "templateid": "10001"

            }

        ],

       "inventory_mode":0,

       "inventory":{

       "macaddress_a": "01234",

       "macaddress_b": "56768"

       }

       },

    "id": 2,

    "auth": "2e71445ea0ae33cb19be5a77dfb439a5"

}' http://172.25.28.1/zabbix/api_jsonrpc.php | python -m json.tool



[root@server1 ~]# ./zabbix-api.sh

{

    "id": 2,

    "jsonrpc": "2.0",

    "result": {

        "hostids": [

            "10254"

        ]

    }

}

浏览器查看:

groupid确定的方法:

点击linux servers

浏览器地址栏出现groupid

templateid确定方法同理(也可以搜索过滤)

二,监听apache服务

1,添加主机

2,点击server2

3,模板>选择  

添加>更新

 

三,监听数据库

[root@server1 ~]# cd /etc/zabbix/zabbix_agentd.d/

[root@server1 zabbix_agentd.d]# vim userparameter_mysql.conf

[root@server1 zabbix_agentd.d]# cd /var/lib/zabbix  没有这个目录,需要建立

-bash: cd: /var/lib/zabbix: No such file or directory

[root@server1 zabbix_agentd.d]# mkdir /var/lib/zabbix

[root@server1 zabbix_agentd.d]# cd /var/lib/zabbix/  建立隐藏配置文件

[root@server1 zabbix]# vim .my.cnf

[mysql]

user=root

password=redhat  数据库登录密码

socket=/var/lib/mysql/mysql.sock



[mysqladmin]

user=root

password=redhat

socket=/var/lib/mysql/mysql.sock

这两个用户都是userparameter_mysql.conf里面提到的,需要写相关语句块

[root@server1 zabbix_agentd.d]# systemctl restart zabbix-agent

浏览器添加模板更新

对数据库数据操作观察图像

 

猜你喜欢

转载自blog.csdn.net/ha_weii/article/details/81713963