.

zabbix是当下比较常用的监控软件,其自带监控模板对系统资源等监控很完善,也支持故障邮件报警及应急处理,自定义监控项等,去官网查看zabbix安装文档,嗯,嗯,嗯,不说了,zabbix详细安装配置如下:

软件 版本 安装方式
zabbix 4.0 源码包安装
tengine 2.1.3 源码包安装
mariadb 5.5.6 yum安装
php 5.4.6 yum安装

下载zabbix4.0源码包,创建zabbix与apache用户和用户组

1
2
3
4
5
wget https://pilotfiber.dl.sourceforge.net/project/zabbix/ZABBIX%20Latest%20Stable/4.0.0/zabbix-4.0.0.tar.gz
tar -zxvf ~/zabbix-4.0.0.tar.gz
mkdir /system/zabbix
groupadd zabbix
useradd -g zabbix zabbix

配置nginx与php-fpm会用到

1
2
3
4
groupadd -g 201 apache    
useradd -g apache -u 201 -m apache
cd ~/zabbix-4.0.0
yum -y groupinstall "Development Tools"

一、mariadb5.5.6安装配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#yum安装mariadb数据库
yum install -y mariadb mariadb-server mysql-devel
vim /etc/my.cnf
[mysqld]
#在mysqld中新增utf8避免数据是中文时显示"?"
character-set-server=utf8
collation-server=utf8_general_ci
#设置mariadb最大访问数量(默认为214)
max_connections=10000

[client]
default-character-set=utf8
vim /usr/lib/systemd/system/mariadb.service
[Service]
#在【service】中追加大概意思是 MySQL 能够支持的最大连接数量受限于操作系统,必要时可以增大open-files-limit.换言之,连接数与文件打开数有关。
LimitNOFILE=65535
LimitNPROC=65535
1
2
3
sudo systemctl daemon-reload        
sudo systemctl restart mariadb
systemctl enable mariadb.service
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
mysql -u root
MariaDB [(none)]> grant all privileges on *.* to 'root'@'%' identified by '123';
MariaDB [(none)]> grant all privileges on *.* to 'root'@'localhost' identified by '123';
MariaDB [(none)]> grant all on zabbix.* to utest@'%' identified by 'ptest';
Query OK, 0 rows affected (0.01 sec)

MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> exit;
#导入zabbix数据库
mysql -uroot -p1q2w3e zabbix < ~/zabbix-4.0.0/schema.sql
mysql -uroot -p1q2w3e zabbix < ~/zabbix-4.0.0/images.sql
mysql -uroot -p1q2w3e zabbix < ~/zabbix-4.0.0/data.sql
#设置登录zabbix系统web界面密码,用户名是admin
MariaDB [(none)]> use zabbix
MariaDB [(none)]> update users set passwd=md5("123") where name='Zabbix';
MariaDB [(none)]> exit;

二、源码包安装zabbix_server

1
2
3
4
5
6
yum install -y libxml2-devel net-snmp net-snmp-devel libevent-devel libcurl-devel
cd ~/zabbix-4.0.0
./configure --prefix=/system/zabbix --enable-server --enable-agent --with-
mysql --enable-ipv6 --with-net-snmp --with-libcurl --with-libxml2

make&&make install

三、安装php环境

1
2
3
yum -y install httpd php php-mysql php-common php-mbstring php-gd php-odbc php-pear curl curl-devel net-snmp net-snmp-devel perl-DBI php-xml ntpdate php-bcmath 

yum install -y php-mysql php-gd libjpeg* php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-bcmath php-mhash php-fpm

配置PHP

1
2
3
4
5
6
7
8
9
vim /etc/php.ini

date.timezone = Asia/Shanghai
max_execution_time = 300
max_input_time = 300
post_max_size = 32M
memory_limit = 128M
expose_php = Off #禁止显示php版本的信息
short_open_tag = ON #支持php短标签
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
sudo vim /etc/php-fpm.d/www.conf

listen = 127.0.0.1:9000
user = apache
group = apache
chdir = /system/nginx/html/zabbix/

sudo systemctl restart php-fpm.service
sudo cat /system/nginx/html/index.php

<?php

phpinfo();

?>

四、tengine源码包安装

1
2
3
4
5
6
7
mkdir /system/ngix
cd ~
wget http://tengine.taobao.org/download/tengine-2.1.2.tar.gz
tar zxvf tengine-2.1.2
cd tengine-2.1.2/
./configure --prefix= /system/nginx/
make&&make install

编写centos7中nginx服务启动脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
vim /usr/lib/systemd/system/nginx.service 

[Unit]
Description=nginx
After=syslog.target network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/system/nginx/logs/nginx.pid
ExecStart=/system/nginx/sbin/nginx -c /system/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target

修改nginx配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
cat /system/nginx/conf/nginx.conf
user apache apache;
#创建用户apache在apache组中,代理php-fpm配置文件中设置一致
worker_processes 3;
error_log /var/log/nginx/error.log info;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
charset utf-8;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;

index index.html index.htm index.php index.jsp;

location / {
root /system/nginx/html/zabbix;
index index.php index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
#必须要有此配置,否则php网页显示错误或会下载index.php网页
location ~ \.php$ {
root /system/nginx/html/zabbix;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi.conf;
fastcgi_buffer_size 128k;
fastcgi_buffers 32 32k;

}
}
}
1
2
sudo /system/nginx/sbin/nginx -s reload        
sudo systemctl restart nginx

网页测试http://127.0.0.1/index.php

如果测试不通,请管理selinux。临时关闭setenforce 0
永久关闭,vim /etc/selinux/config
将这行改为SELINUX=disabled
参考链接:https://blog.csdn.net/leonnew/article/details/79738593

用nignx代理php页面(zabbix的web配置界面就可以配置与数据库的连接等信息)

1
2
cp -a frontends/php/* /system/nginx/html/zabbix/
chmod 777 /system/nginx/html/zabbix/conf

五、安装zabbix客户端

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
yum -y groupinstall "Development Tools"
yum install -y libxml2-devel net-snmp net-snmp-devel libevent-devel libcurl-devel
./configure --prefix=/system/zabbix --with-net-snmp --enable-agent

sudo vim /system/zabbix/etc/zabbix_agentd.conf #此处千万别写成了zabbix_agent.conf,否则配置了不生效
#填写Server的IP地址
Server=127.0.0.1
#修改为Server的IP地址
ServerActive=127.0.0.1
#填写本机的HostName,注意Server端要能解析
Hostname=test
#是否允许自定义的key,1为允许,0为不允许
UnsafeUserParameters=1
#自定义的agentd配置文件(key)可以在这里面写;
Include=/system/zabbix/etc/zabbix_agentd.conf.d/
启动zabbix agent端

/system/zabbix/sbin/zabbix_agentd -c /system/zabbix/etc/zabbix_agentd.conf

六、使用zabbix过程中遇到的问题及处理办法

zabbix邮件报警设置

参考:https://www.jianshu.com/p/9daaf8251068

  • server端安装邮件报警所需插件

    1
    2
    3
    4
    5
    6
    7
    8
    yum install mailx -y
    在/etc/mail.rc后面追加
    # send mail
    set from=qqzgqq@126.com
    set smtp=smtp.126.com
    set smtp-auth-user=qqzgqq@126.com
    set smtp-auth-password=*******
    set smtp-auth=login
  • 写邮件脚本

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    cat /system/zabbix/share/zabbix/alertscripts/mail.sh

    #!/bin/bash
    #send mail
    messages=`echo $3 | tr '\r\n' '\n'`
    subject=`echo $2 | tr '\r\n' '\n'`
    echo "${messages}" | mail -s "${subject}" $1 >>/tmp/mailx.log 2>&1

    touch /tmp/mailx.log
    chown -R zabbix:zabbix /tmp/mailx.log
    chown +x /system/zabbix/share/zabbix/alertscripts/mail.sh
    chown -R zabbix:zabbix /system/zabbix/share/zabbix/alertscripts/mail.sh

    手动执行下脚本验证下看是否可以发邮件

  • 新建主机

    主机

  • 主机中添加监控项
    监控项

  • 触发器
    触发器

  • 动作
    动作
    动作

zabbix发送远程命令

1
2
3
vim /etc/sudoers

zabbix ALL=NOPASSWD:ALL
1
2
3
4
5
6
7
8
cat /system/zabbix/etc/zabbix_agentd.conf|grep -v "^$"|grep -v "#"
LogFile=/tmp/zabbix_agentd.log
EnableRemoteCommands=1
Server=192.168.26.135
ServerActive=192.168.26.135
Hostname=k8s3
killall zabbix_agentd
/system/zabbix/sbin/zabbix_agentd -c /system/zabbix/etc/zabbix_agentd.conf

zabbix监控页面中文乱码

将目录中/system/nginx/html/zabbix/fonts/替换为windows系统中字体

zabbix汉化方法

1
2
cd /system/nginx/html/zabbix/include/
sudo vim locales.inc.php display改成true

自定义zabbix监控项的key

1
2
3
4
5
6
7
8
cat /system/zabbix/etc/zabbix_agentd.conf|grep -v "^$"|grep -v "#"
LogFile=/tmp/zabbix_agentd.log
EnableRemoteCommands=1
Server=192.168.26.135
ServerActive=192.168.26.135
Hostname=k8s2
Include=/system/zabbix/etc/zabbix_agentd.conf.d/
UnsafeUserParameters=1
1
2
3
cat /system/zabbix/etc/zabbix_agentd.conf.d/http_code.conf

UserParameter=get.http.code,curl -I -s -o /dev/null -w %{http_code} 127.0.0.1

server端验证下是否可用

1
2
zabbix_get -s 192.168.26.136 -k "get.http.code"
200
  • 启用监控项
    启用监控项