企业实战(8)CentOS 6.8安装Zabbix-agent 5.0监控主机性能与Mysql数据库

2022-06-22 14:33:51 浏览数 (1)

环境介绍:

CentOS 6.8

Zabbix-agent 5.0

Zabbix-server:192.168.2.128

Zabbix-agent:192.168.2.131

一、Zabbix-server端安装配置

传送门 ---> Zabbix 5.0监控平台搭建与被监控端的添加

二、Zabbix-agent安装

1.设置Selinux为disabled

代码语言:javascript复制
[root@localhost ~]# getenforce
Enforcing

[root@localhost ~]# setenforce 0

[root@localhost ~]# getenforce
Permissive

[root@localhost ~]# vim /etc/sysconfig/selinux
SELINUX=disabled

2.设置Iptables防火墙规则

代码语言:javascript复制
[root@localhost ~]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination
ACCEPT     all  --  anywhere             anywhere            state RELATED,ESTABLISHED
ACCEPT     icmp --  anywhere             anywhere
ACCEPT     all  --  anywhere             anywhere
ACCEPT     tcp  --  anywhere             anywhere            state NEW tcp dpt:ssh
REJECT     all  --  anywhere             anywhere            reject-with icmp-host-prohibited

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination
REJECT     all  --  anywhere             anywhere            reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

[root@localhost ~]# iptables -I INPUT -p tcp --dport 10050 -j ACCEPT
----放行zabbix端口10050

[root@localhost ~]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination
ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:zabbix-agent
ACCEPT     all  --  anywhere             anywhere            state RELATED,ESTABLISHED
ACCEPT     icmp --  anywhere             anywhere
ACCEPT     all  --  anywhere             anywhere
ACCEPT     tcp  --  anywhere             anywhere            state NEW tcp dpt:ssh
REJECT     all  --  anywhere             anywhere            reject-with icmp-host-prohibited

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination
REJECT     all  --  anywhere             anywhere            reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

3.安装Zabbix Yum源仓库

代码语言:javascript复制
[root@localhost ~]# rpm -Uvh https://repo.zabbix.com/zabbix/5.0/rhel/6/x86_64/zabbix-release-5.0-1.el6.noarch.rpm
Retrieving https://repo.zabbix.com/zabbix/5.0/rhel/6/x86_64/zabbix-release-5.0-1.el6.noarch.rpm
warning: /var/tmp/rpm-tmp.8Lm2y3: Header V4 RSA/SHA512 Signature, key ID a14fe591: NOKEY
Preparing...                ########################################### [100%]
   1:zabbix-release         ########################################### [100%]

[root@localhost ~]# yum clean all
已加载插件:fastestmirror
Cleaning repos: base extras updates zabbix zabbix-non-supported
清理一切
Cleaning up list of fastest mirrors

4.安装Zabbix-agent

代码语言:javascript复制
[root@localhost ~]# yum -y install zabbix-agent
已加载插件:fastestmirror
设置安装进程
Loading mirror speeds from cached hostfile
 * base: mirrors.bfsu.edu.cn
 * extras: mirrors.bfsu.edu.cn
 * updates: mirrors.bfsu.edu.cn
解决依赖关系
--> 执行事务检查
---> Package zabbix-agent.x86_64 0:5.0.0-1.el6 will be 安装
--> 完成依赖关系计算

依赖关系解决

===============================================================================================================================
 软件包                           架构                       版本                             仓库                        大小
===============================================================================================================================
正在安装:
 zabbix-agent                     x86_64                     5.0.0-1.el6                      zabbix                     435 k

事务概要
===============================================================================================================================
Install       1 Package(s)

总下载量:435 k
Installed size: 1.7 M
下载软件包:
http://repo.zabbix.com/zabbix/5.0/rhel/6/x86_64/^H^H^H^Hzabbix-agent-5.0.0-1.el6.x86_64.rpm              18% [=======          zabbix-agent-5.0.0-1.el6.x86_64.rpm                                                                     | 435 kB     00:08
warning: rpmts_HdrFromFdno: Header V4 RSA/SHA512 Signature, key ID a14fe591: NOKEY
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX-A14FE591
Importing GPG key 0xA14FE591:
 Userid : Zabbix LLC <packager@zabbix.com>
 Package: zabbix-release-5.0-1.el6.noarch (installed)
 From   : /etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX-A14FE591
运行 rpm_check_debug
执行事务测试
事务测试成功
执行事务
Warning: RPMDB altered outside of yum.
  正在安装   : zabbix-agent-5.0.0-1.el6.x86_64                                                                             1/1
  Verifying  : zabbix-agent-5.0.0-1.el6.x86_64                                                                             1/1

已安装:
  zabbix-agent.x86_64 0:5.0.0-1.el6

完毕!

注意:  如果下载的zabbix-agent 5.0安装包使用rpm安装时提示“错误:依赖检测失败:libpcre.so.0()(64bit)”,则可以使用zabbix 5.0 Yum源来安装zabbix-agent 5.0软件。

----> 传送门 安装zabbix-agent5.0 报错解决

5.修改zabbix-agent配置文件:

代码语言:javascript复制
[root@localhost ~]# vim /etc/zabbix/zabbix_agentd.conf
119 Server=127.0.0.1,192.168.2.128
160 ServerActive=192.168.2.128
171 Hostname=192.168.2.131

[root@localhost ~]# service zabbix-agent start
Starting Zabbix agent:                                     [确定]

[root@localhost ~]# service zabbix-agent enable
用法: /etc/init.d/zabbix-agent {start|stop|status|restart|try-restart|force-reload}

[root@localhost ~]# ss -antulp | grep :10050
tcp    LISTEN     0      128                   :::10050                :::*      users:(("zabbix_agentd",2963,5),("zabbix_agentd",2965,5),("zabbix_agentd",2966,5),("zabbix_agentd",2967,5),("zabbix_agentd",2968,5),("zabbix_agentd",2969,5))
tcp    LISTEN     0      128                    *:10050                 *:*      users:(("zabbix_agentd",2963,4),("zabbix_agentd",2965,4),("zabbix_agentd",2966,4),("zabbix_agentd",2967,4),("zabbix_agentd",2968,4),("zabbix_agentd",2969,4))

三、Zabbix-server端Web界面配置

--主机系统性能监控

1.添加Zabbix-agent主机

2.添加主机信息

3.选择主机性能监控模板

4.查看被监控主机状态

 现在可以看到在Zabbix-server端web页面上centos6主机状态显示绿色,说明被监控主机已经添加成功。

5.查看被监控主机系统性能监控信息

6.以图形形式查看性能监控信息

--Mysql数据库监控

Zabbix-agent端配置

1.基础环境配置

代码语言:javascript复制
[root@localhost ~]# yum install tree net-tool net-snmp vim ntpdate  wget                 #安装部分基础软件包

[root@localhost ~]# cd /etc/yum.repos.d/         #修改yum源 更改为阿里云 

[root@localhost ~]# mv CentOS-Base.repo CentOS-Base.repo.bak    #备份文件    

[root@localhost ~]# wget -O CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo            #下载阿里云镜像仓库

[root@localhost ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo     #下载epel仓库

[root@localhost ~]# yum clean all    #清理缓存            

[root@localhost ~]# yum makecache   #重新生成缓存 

2.安装Mysql数据库

Mysql5.7下载

代码语言:javascript复制
[root@localhost ~]# wget https://repo.mysql.com//mysql57-community-release-el6-9.noarch.rpm     //下载Mysql5.7 Yum源

[root@localhost ~]# ls mysql57-community-release-el6-9.noarch.rpm
mysql57-community-release-el6-9.noarch.rpm

[root@localhost ~]# rpm -ivh mysql57-community-release-el6-9.noarch.rpm      //安装Mysql5.7 Yum源

[root@localhost ~]# yum -y install mysql-server    //安装Mysql
...
已安装:
  mysql-community-libs.x86_64 0:5.7.30-1.el6                  mysql-community-libs-compat.x86_64 0:5.7.30-1.el6
  mysql-community-server.x86_64 0:5.7.30-1.el6

作为依赖被安装:
  mysql-community-client.x86_64 0:5.7.30-1.el6   mysql-community-common.x86_64 0:5.7.30-1.el6   numactl.x86_64 0:2.0.9-2.el6

作为依赖被升级:
  postfix.x86_64 2:2.6.6-8.el6

完毕!

[root@localhost ~]# service mysqld start    //启动Mysqld服务
初始化 MySQL 数据库:                                      [确定]
正在启动 mysqld:                                          [确定]

[root@localhost ~]# service mysqld enable    //开机自启mysql
用法:/etc/init.d/mysqld {start|stop|status|restart|condrestart|try-restart|reload|force-reload}

[root@localhost ~]# ss -antulp | grep :3306
tcp    LISTEN     0      80                    :::3306                 :::*      users:(("mysqld",23473,21))

[root@localhost ~]# grep 'root@localhost:' /var/log/mysqld.log   //查看初始密码
2020-05-26T21:29:44.571409Z 1 [Note] A temporary password is generated for root@localhost: haiwhAT<g1O 

[root@localhost ~]# mysql -hlocalhost -uroot -p'haiwhAT<g1O '  //使用初始密码登陆
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 3
Server version: 5.7.30
...
mysql>

mysql> alter user root@"localhost" identified by "123qqq...A";   修改登陆密码

[root@localhost ~]# mysql -uroot -p123qqq...A    //使用新密码登陆

3.授权Zabbix用户,用于发送数据

代码语言:javascript复制
[root@localhost ~]# mysql -uroot -p123qqq...A
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 5
Server version: 5.7.30 MySQL Community Server (GPL)
...
mysql> grant all on *.* to zabbix@"localhost" identified by "123qqq...A";
Query OK, 0 rows affected, 1 warning (0.06 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.04 sec)

4.测试使用Zabbix用户登录数据库

代码语言:javascript复制
[root@localhost ~]# mysql -uzabbix -p123qqq...A
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 6
Server version: 5.7.30 MySQL Community Server (GPL)
...
mysql> show databases;
 -------------------- 
| Database           |
 -------------------- 
| information_schema |
| mysql              |
| performance_schema |
| sys                |
 -------------------- 
4 rows in set (0.01 sec)

mysql> select user();
 ------------------ 
| user()           |
 ------------------ 
| zabbix@localhost |
 ------------------ 
1 row in set (0.01 sec)

5.查看mysql的sock文件路径

代码语言:javascript复制
[root@localhost ~]# find / -name mysql.sock
/var/lib/mysql/mysql.sock

6.查找并拷贝Zabbix监控Mysql模板文件

代码语言:javascript复制
[root@localhost ~]# find / -name user*_mysql.conf
/usr/share/doc/zabbix-agent-5.0.0/userparameter_mysql.conf

[root@localhost ~]# cp /usr/share/doc/zabbix-agent-5.0.0/userparameter_mysql.conf /etc/zabbix/zabbix_agentd.d/

[root@localhost ~]# id zabbix
uid=498(zabbix) gid=498(zabbix) 组=498(zabbix)

[root@localhost ~]# chown -R zabbix:zabbix /etc/zabbix/zabbix_agentd.d/userparameter_mysql.conf   //设置模板文件归属

[root@localhost ~]# ll /etc/zabbix/zabbix_agentd.d/userparameter_mysql.conf
-rw-r--r--. 1 zabbix zabbix 1081 5月  27 06:07 /etc/zabbix/zabbix_agentd.d/userparameter_mysql.conf

7.查看Zabbix自带的监控模板文件

由配置文件可以知道,在Linux系统中需要在zabbix-agent home目录中创建.my.cnf(默认情况下为/var/lib/zabbix),并且文件必须以下有三个字符串:

  • [client]
  • user=zbx_monitor
  • password=``
代码语言:javascript复制
[root@localhost ~]# cat /etc/zabbix/zabbix_agentd.d/userparameter_mysql.conf
#template_db_mysql.conf created by Zabbix for "Template DB MySQL" and Zabbix 4.2
#For OS Linux: You need create .my.cnf in zabbix-agent home directory (/var/lib/zabbix by default)
#For OS Windows: You need add PATH to mysql and mysqladmin and create my.cnf in %WINDIR%my.cnf,C:my.cnf,BASEDIRmy.cnf https://dev.mysql.com/doc/refman/5.7/en/option-files.html
#The file must have three strings:
#[client]
#user=zbx_monitor
#password=<password>
#
UserParameter=mysql.ping[*], mysqladmin -h"$1" -P"$2" ping
UserParameter=mysql.get_status_variables[*], mysql -h"$1" -P"$2" -sNX -e "show global status"
UserParameter=mysql.version[*], mysqladmin -s -h"$1" -P"$2" version
UserParameter=mysql.db.discovery[*], mysql -h"$1" -P"$2" -sN -e "show databases"
UserParameter=mysql.dbsize[*], mysql -h"$1" -P"$2" -sN -e "SELECT SUM(DATA_LENGTH   INDEX_LENGTH) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='$3'"
UserParameter=mysql.replication.discovery[*], mysql -h"$1" -P"$2" -sNX -e "show slave status"
UserParameter=mysql.slave_status[*], mysql -h"$1" -P"$2" -sNX -e "show slave status"

8.在/var/lib/zabbix目录下创建.my.cnf配置文件

代码语言:javascript复制
[root@localhost ~]# mkdir /var/lib/zabbix

[root@localhost ~]# vim /var/lib/zabbix/.my.cnf
[mysql]
host=localhost
user=zabbix           //数据库中授权的zabbix用户
password=123qqq...A     //授权用户密码
socket=/var/lib/mysql/mysql.sock   

[mysqladmin]
host=localhost
user=zabbix
password=123qqq...A
socket=/vsr/lib/mysql/mysql.sock

[root@localhost ~]# service zabbix-agent restart    //重启Zabbix-agent服务
Shutting down Zabbix agent:                                [确定]
Starting Zabbix agent:                                     [确定]

Zabbix-server Web端配置

1.选择Mysql监控模板

2.查看Mysql监控数据

 要查看单个监控项的数据图形,直接点击监控项末尾的“图形”即可。

3.查看所有监控项图形信息

0 人点赞