返回列表 发帖

CentOS8_LNMP_编译安装Zabbix5.0.12

笺注:这是在 CentOS8安装LNMP+phpMyAdmin 的基础上进行的。


安装依赖软件包:
yum -y install gcc gcc-c++ wget autoconf automake zlib zlib-devel openssl openssl-devel pcre pcre-devel make

yum -y install net-snmp-devel libxml2-devel libevent libevent-devel libcurl mysql-devel curl-devel php-ldap *bcmath*


编译安装Zabbix:
tar -zxvf zabbix-5.0.12.tar.gz
cd zabbix-5.0.12
./configure --prefix=/usr/local/zabbix --enable-server --enable-agent --with-mysql --enable-ipv6 --with-net-snmp --with-libcurl --with-libxml2
make install



配置数据库:( 使用数据库管理员root@localhost ,此时没有设置密码,可无密码登录)
[root@centos8 ~]# mysql -u"root" -h"localhost"
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 189
Server version: 10.3.17-MariaDB MariaDB Server

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

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

MariaDB [(none)]>


查看当前登录的数据库用户:
MariaDB [(none)]> Select user();
+----------------+
| user()         |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.000 sec)

MariaDB [(none)]>


创建一个库zabbix:
MariaDB [(none)]> create database zabbix character set utf8 collate utf8_bin;
Query OK, 1 row affected (0.001 sec)


切换到库zabbix:
MariaDB [(none)]> use zabbix;
Database changed

导入Zabbix的表:
MariaDB [zabbix]> source /root/zabbix-5.0.12/database/mysql/schema.sql;
MariaDB [zabbix]> source /root/zabbix-5.0.12/database/mysql/images.sql;
MariaDB [zabbix]> source /root/zabbix-5.0.12/database/mysql/data.sql;


授权数据库用户zabbix@localhost对库zabbix有完全控制的权限,密码:168
MariaDB [(none)]> grant all privileges on zabbix.* to zabbix@localhost identified by '168';
Query OK, 0 rows affected (0.001 sec)



创建系统用户zabbix:
useradd -s /sbin/nologin zabbix

创建Zabbix的日志存放目录:
mkdir -p /usr/local/zabbix/logs

更改目录的属主、属组:
chown -R zabbix:zabbix /usr/local/zabbix



配置Zabbix本机的客户端:
[root@centos8 ~]# vi /usr/local/zabbix/etc/zabbix_agentd.conf
修改:
PidFile=/usr/local/zabbix/zabbix_agentd.pid
LogFile=/usr/local/zabbix/logs/zabbix_agentd.log
Server=127.0.0.1
ServerActive=127.0.0.1
Hostname=Zabbix server
Include=/usr/local/zabbix/etc/zabbix_agentd.conf.d/*.conf


配置Zabbix本机的服务端:
[root@centos8 ~]# vi /usr/local/zabbix/etc/zabbix_server.conf
修改:
DBHost=localhost
DBName=zabbix
DBUser=zabbix
DBPassword=168
DBSocket=/var/lib/mysql/mysql.sock
Timeout=4
LogSlowQueries=3000
Include=/usr/local/zabbix/etc/zabbix_server.conf.d/*.conf

备注:
[root@centos8 ~]# find / -name mysql.sock
/var/lib/mysql/mysql.sock



启动Zabbix本机的客户端和服务端:
/usr/local/zabbix/sbin/zabbix_agentd
/usr/local/zabbix/sbin/zabbix_server


开机自动启动Zabbix本机的客户端和服务端:
echo '/usr/local/zabbix/sbin/zabbix_agentd' >> /etc/rc.local
echo '/usr/local/zabbix/sbin/zabbix_server' >> /etc/rc.local
chmod a+x /etc/rc.d/rc.local


检测Zabbix本机的客户端有没有在运行:(在运行时)
[root@centos8 ~]# ps aux |grep zabbix_agentd |grep -v grep > /dev/null && echo $?
0
[root@centos8 ~]#

检测Zabbix本机的服务端有没有在运行:(在运行时)
[root@centos8 ~]# ps aux |grep zabbix_server |grep -v grep > /dev/null && echo $?
0
[root@centos8 ~]#



复制Zabbix的Web应用程序至Nginx网站根目录:
[root@centos8 ~]# mkdir -p /usr/share/nginx/html/zabbix/
[root@centos8 ~]# cp -r /root/zabbix-5.0.12/ui/* /usr/share/nginx/html/zabbix/


重启Nginx:
[root@centos8 ~]# systemctl restart nginx



修改PHP的配置文件:(这里不修改的话,在Web部分安装时会失败的)
[root@centos8 ~]# vi /etc/php.ini
修改:
max_execution_time = 300
post_max_size = 16M
max_input_time = 300
date.timezone = Asia/Shanghai

追加:
extension=bcmath.so


重启php-fpm:
[root@centos8 ~]# systemctl restart php-fpm





#########

在Windows客户端进行Web部分的安装:(建议使用Frefox浏览器)
http://192.168.168.154/zabbix/
图片1.png
2021-10-17 15:07



如果提示其他错误,可以修改PHP的配置文件:
图片2.png
2021-10-17 15:08



这里只需要输入数据库用户zabbix@localhost的密码,再点击“Next step”:
图片3.png
2021-10-17 15:08



这里直接点击“Next step”:
图片4.png
2021-10-17 15:08



这里直接点击“Next step”:
图片5.png
2021-10-17 15:09



来到这里会有问题:
图片6.png
2021-10-17 15:09


解决方法:
1. 下载配置文件;
2. 把配置文件复制到指定位置:
[root@centos8 ~]# cp zabbix.conf.php /usr/share/nginx/html/zabbix/conf/zabbix.conf.php

3.点击本页面底下的“Finish”按键。


来到这里,点击“Finish”按键就完成安装了:
图片7.png
2021-10-17 15:09




Admin的登录密码默认为:zabbix
图片8.png
2021-10-17 15:10



Zabbix的主页:
图片9.png
2021-10-17 15:10



默认是英文的:
Administration》Users》Admin:
图片10.png
2021-10-17 15:10



设置用户Admin的语言为中文:
图片11.png
2021-10-17 15:11



用户Admin的语言变为中文了:
图片12.png
2021-10-17 15:11




配置》主机:
默认就会监控Zabbix本机(Zabbix server):
图片13.png
2021-10-17 15:11



Zabbix本机自带的模板:( Template App Zabbix Server, Template OS Linux by Zabbix agent )
图片1.png
2022-8-17 09:35




查看Zabbix本机的自带图形:(监控项、触发器、图形都来源于所使用的模板)

监测》主机》左击主机名称Zabbix server》图形:
图片15.png
2021-10-17 15:12


图片16.png
2021-10-17 15:12


笺注:在这里可以看到图形界面上的中文出现乱码,这是因为Zabbix自身对中文简体的支持不完善,需要使用新的字体进行替换:
[root@centos8 ~]# find /usr/share/nginx/html/zabbix/ -name "*.ttf"
/usr/share/nginx/html/zabbix/assets/fonts/DejaVuSans.ttf

手动上传新的字体:
[root@centos8 ~]# ls *.ttc
simsun.ttc

[root@centos8 ~]# mv -f simsun.ttc /usr/share/nginx/html/zabbix/assets/fonts/DejaVuSans.ttf


接着,刷新一下页面就好了:

Zabbix本机的CPU使用率:
图片17.png
2021-10-17 15:13


图片18.png
2021-10-17 15:13



Zabbix本机的CPU usage:
图片19.png
2021-10-17 15:14


图片20.png
2021-10-17 15:14



Zabbix本机的CPU jumps:
图片21.png
2021-10-17 15:14


图片22.png
2021-10-17 15:14



Zabbix本机的内存使用率:
图片23.png
2021-10-17 15:15


图片24.png
2021-10-17 15:15






查看Zabbix本机的触发器:
图片2.png
2022-8-17 09:38


在“触发器”里使用“过滤器”:
图片3.png
2022-8-17 09:38


根据严重性的级别查找触发器:
图片4.png
2022-8-17 09:38


图片5.png
2022-8-17 09:39



模板Template OS Linux by Zabbix agent默认自带服务器重启的触发器:
{HOST.NAME} has been restarted (uptime < 10m)
图片6.png
2022-8-17 09:39

注释:
{HOST.NAME} 是预定义变量,即当前主机的名称;
默认的严重性为“警告”;


把严重性更改为“灾难”:
图片7.png
2022-8-17 09:40


此触发器默认已启用:(可以在这里关闭触发器)
图片8.png
2022-8-17 09:40



触发器“{HOST.NAME} has been restarted (uptime < 10m)”的严重性级别更改成功:
图片9.png
2022-8-17 09:40




测试一下以上的触发器:

以防关闭系统时卡死,关机、重启前,最好先关闭Zabbix所有相关的进程:
[root@centos8 ~]# pkill -9 -U zabbix
[root@centos8 ~]#


检测Zabbix本机的客户端有没有在运行:(没有运行时)
[root@centos8 ~]# ps aux |grep zabbix_agentd |grep -v grep > /dev/null && echo $?
[root@centos8 ~]#

检测Zabbix本机的服务端有没有在运行:(没有运行时)
[root@centos8 ~]# ps aux |grep zabbix_server |grep -v grep > /dev/null && echo $?
[root@centos8 ~]#


重启服务器:
[root@centos8 ~]# init 6



监测》仪表板:
当主机Zabbix server重启后,仪表板会如下图显示:(显示对应触发器的名称)
Zabbix server has been restarted (uptime < 10m)
图片10.png
2022-8-17 09:42


图片11.png
2022-8-17 09:42






############
############

在Zabbix服务器使用命令行,测试本机的指定监控TCP端口是否处于侦听状态:
(返回 0 - 未侦听;1 - 正在侦听)

[root@centos8 ~]# systemctl stop nginx

[root@centos8 ~]# /usr/local/zabbix/bin/zabbix_get -s 127.0.0.1 -k net.tcp.listen[80]
0


[root@centos8 ~]# systemctl start nginx

[root@centos8 ~]# /usr/local/zabbix/bin/zabbix_get -s 127.0.0.1 -k net.tcp.listen[80]
1



############

在Zabbix服务器使用命令行,测试本机的指定进程是否处于运行状态:

[root@centos8 ~]# systemctl stop nginx

[root@centos8 ~]# /usr/local/zabbix/bin/zabbix_get -s 127.0.0.1 -k proc.num[nginx]
0

[root@centos8 ~]# systemctl start nginx

[root@centos8 ~]# /usr/local/zabbix/bin/zabbix_get -s 127.0.0.1 -k proc.num[nginx]
2





#############
#############

自定义创建Zabbix的键值:

修改Zabbix服务器的Agent配置文件(配置Zabbix本机的客户端):
[root@centos8 ~]# find / -name zabbix_agentd.conf
/root/zabbix-5.0.12/conf/zabbix_agentd.conf
/usr/local/zabbix/etc/zabbix_agentd.conf


[root@centos8 ~]# vi /usr/local/zabbix/etc/zabbix_agentd.conf

# UnsafeUserParameters=0
修改为:(启用该功能)
UnsafeUserParameters=1

接着插入:
UserParameter=command_1,df -hl |grep /boot$ |awk '{print $5}' |awk -F% '{print $1}'

UserParameter=command_2,mysql -u"zabbix" -p"168" -h"localhost" -e"show status like 'threads_running'" |grep -i "threads_running" |awk '{print $2}'

如下图:(截图有省略)
图片12.png
2022-8-17 09:44



保存好配置文件后,重启一下Zabbix本机的客户端和服务端:
pkill -9 -U zabbix
/usr/local/zabbix/sbin/zabbix_agentd
/usr/local/zabbix/sbin/zabbix_server


获取本机的分区/boot的使用率:
[root@centos8 ~]# /usr/local/zabbix/bin/zabbix_get -s 127.0.0.1 -k command_1
80
[root@centos8 ~]#


获取本机的MariaDB的当前处于激活状态的连接数:
[root@centos8 ~]# /usr/local/zabbix/bin/zabbix_get -s 127.0.0.1 -k command_2
1
[root@centos8 ~]#





######
######

使用本地数据库用户zabbix@localhost登录MariaDB:
[root@centos8 ~]# mysql -u"zabbix" -p"168" -h"localhost"
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 269
Server version: 10.3.17-MariaDB MariaDB Server

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

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

MariaDB [(none)]>


查看当前登录的数据库用户:
MariaDB [(none)]> Select user();
+------------------+
| user()           |
+------------------+
| zabbix@localhost |
+------------------+
1 row in set (0.000 sec)

MariaDB [(none)]>


在库zabbix中创建表、插入测试的数据:
MariaDB [(none)]> use zabbix;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MariaDB [zabbix]>
MariaDB [zabbix]> Create table Table_1(Name varchar(20),Age tinyint);
Query OK, 0 rows affected (0.096 sec)

MariaDB [zabbix]>
MariaDB [zabbix]> INSERT into Table_1 (Name,Age) values ('Zhuohua',8);
Query OK, 1 row affected (0.003 sec)

MariaDB [zabbix]>
MariaDB [zabbix]> INSERT into Table_1 (Name,Age) values ('Python',3);
Query OK, 1 row affected (0.002 sec)

MariaDB [zabbix]>



Zabbix服务器的Python3的安装可参考:CentOS8安装Python3
第三方库PyMySQL的安装可参考:Python3脚本管理Linux下的MySQL



创建一个获取SQL语句执行后的返回值的Python3脚本:
[root@centos8 ~]# cat /scripts/xx.py
#coding=utf-8
import pymysql

def db_connect():

    #本地连接数据库、用户名、用户密码、其中一个库的名称(zabbix)、端口号、字符集
        db=pymysql.connect("localhost", "zabbix", "168", "zabbix",3306,charset="utf8")

        cursor=db.cursor()
        Sql_1 = "Select Age from Table_1 where Name = 'Zhuohua'"
        cursor.execute(Sql_1)
        Results=cursor.fetchone()

        #print (Results) #(8,)
        #print (type(Results)) #<class 'tuple'>

        print (Results[0])
        #print (type(Results[0])) #<class 'int'>

        db.close()

def main():
        try:
                db_connect()
        except Exception as e:
                print(e)

if __name__ == "__main__":
        main()


设置脚本权限:
[root@centos8 ~]# chmod a+x /scripts/xx.py
[root@centos8 ~]#
[root@centos8 ~]# ll /scripts/xx.py
-rwxr-xr-x 1 root root 630 8月  16 18:53 /scripts/xx.py
[root@centos8 ~]#



###

再创建一个获取SQL语句执行后的返回值的Python3脚本:
[root@centos8 ~]# cat /scripts/yy.py
#coding=utf-8
import pymysql

def db_connect():

        db=pymysql.connect("localhost", "zabbix", "168", "zabbix",3306,charset="utf8")

        cursor=db.cursor()
        Sql_1 = "Select Age from Table_1 where Name = 'Python'"
        cursor.execute(Sql_1)
        Results=cursor.fetchone()

        print (Results[0])

        db.close()

def main():
        try:
                db_connect()
        except Exception as e:
                print(e)

if __name__ == "__main__":
        main()


设置脚本权限:
[root@centos8 ~]# chmod a+x /scripts/yy.py
[root@centos8 ~]#
[root@centos8 ~]# ll /scripts/yy.py
-rwxr-xr-x 1 root root 405 8月  16 18:54 /scripts/yy.py
[root@centos8 ~]#



再次修改Zabbix服务器的Agent配置文件:
[root@centos8 ~]# vi /usr/local/zabbix/etc/zabbix_agentd.conf

接着插入:
UserParameter=command_3,/usr/bin/python3 /scripts/xx.py

UserParameter=command_4,/usr/bin/python3 /scripts/yy.py

如下图:(截图有省略)
图片13.png
2022-8-17 09:49



保存好配置文件后,重启一下Zabbix本机的客户端和服务端:
pkill -9 -U zabbix
/usr/local/zabbix/sbin/zabbix_agentd
/usr/local/zabbix/sbin/zabbix_server



Zabbix服务器获取SQL语句执行后的返回值:
[root@centos8 ~]# /usr/local/zabbix/bin/zabbix_get -s 127.0.0.1 -k command_3
8
[root@centos8 ~]#

[root@centos8 ~]# /usr/local/zabbix/bin/zabbix_get -s 127.0.0.1 -k command_4
3
[root@centos8 ~]#





######
######

使用本地数据库用户zabbix@localhost登录MariaDB:(假如MariaDB的端口为默认的TCP 3306,就可以不写)
[root@centos8 ~]# mysql -u"zabbix" -p"168" -h"localhost" -P"3306"
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 540
Server version: 10.3.17-MariaDB MariaDB Server

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

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

MariaDB [(none)]>


在库zabbix的表Table_1中更改数据:
MariaDB [(none)]> use zabbix;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MariaDB [zabbix]>
MariaDB [zabbix]> Update Table_1 set Age = 18 where Name = 'Zhuohua';
Query OK, 1 row affected (0.001 sec)
Rows matched: 1  Changed: 1  Warnings: 0

MariaDB [zabbix]>
MariaDB [zabbix]> Select Age from Table_1 where Name = 'Zhuohua';
+------+
| Age  |
+------+
|   18 |
+------+
1 row in set (0.000 sec)

MariaDB [zabbix]>



Zabbix服务器再次获取SQL语句执行后的返回值:
[root@centos8 ~]# /usr/local/zabbix/bin/zabbix_get -s 127.0.0.1 -k command_3
18
[root@centos8 ~]#





相关文章:
CentOS8_LAMP_编译安装Zabbix5.0.12
CentOS8_在Docker中安装Zabbix5.0.12

Zabbix5.0.12_使用Agent监控Windows2008R2
Zabbix5.0.12_使用SNMP监控iKuai路由器(SNMPv2c+SNMP OID)

Zabbix5.0.12_调用Python3脚本监控Linux下的MySQL
Zabbix5.0.12_调用Python3脚本监控Windows下的MSSQL

返回列表