centos7安装配置zabbix4.0

时间:2021-09-29 06:54:41

zabbix01    198.8.8.211    zabbix-server4.0

zabbix02    198.8.8.212    zabbix-agent4.0

一:zabbix服务端环境部署:

  1.1部署LAMP环境:zabbix中web监控管理界面,需要LNMP架构支持

yum install -y httpd mariadb-server mariadb php php-mysql php-gd libjpeg* php-ldap php-odbc php-pear php-xml php-xmlrpc php-mhash  

    1.1.1:配置httpd:

      ServerName zabbix01.zm.com:80

      <IfModule dir_module>
        DirectoryIndex index.html index.php
      </IfModule>

    1.1.2:配置php:

      date.timezone = PRC

    1.1.3:启动httpd,mysql

      关闭防火墙

      systemctl stop firewalld

      systemctl disabled firewalld

      关闭selinux

      setenforce 0

      getenforce

      vim /etc/sysconfig/selinux

      SELINUX=enforcing改为SELINUX=disabled

      reboot

      systemctl start httpd

      systemctl start mariadb

      netstat -ntap | grep '(80|3306)'

    mysql启动后执行初始安全设置:

      

[root@zabbix01 ~]# mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

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] n
 ... skipping.

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] n
 ... skipping.

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] n
 ... skipping.

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

Reload privilege tables now? [Y/n] n
 ... skipping.

Cleaning up...

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

Thanks for using MariaDB!

  

    1.1.4:创建测试页:

      [root@zabbix01 ~]# vim /var/www/html/index.php

<?php

phpinfo();

?>

:wq

     centos7安装配置zabbix4.0

       [root@zabbix01 ~]# mysql -u root -p

      MariaDB [(none)]> CREATE DATABASE zabbix character set utf8 collate utf8_bin; 

        //创建zabbix数据库(中文编码格式)

      MariaDB [(none)]> GRANT all  ON zabbix.* TO 'zabbix'@'%' IDENTIFIED BY 'zzz';

         //授予zabbix用户zabbix数据库的所有权限,密码zzz

      MariaDB [(none)]> flush privileges;

        //刷新权限

      MariaDB [(none)]> quit

      [root@zabbix01 ~]# vim /var/www/html/index.php

         //修改测试页内容,测试zabbix用户是否能够登陆数据库    

<?php
$link=mysql_connect('198.8.8.211','zabbix','zz');
if($link) echo "<h1>Success!!</h1>";
else echo "Fail!!";
mysql_close();
?>

      查询mysql用户:

        SELECT User, Host, Password FROM mysql.user;  

MariaDB [(none)]> select User, Host  FROM mysql.user;
+--------+-----------+
| User   | Host      |
+--------+-----------+
| zabbix | %         |
| root   | 127.0.0.1 |
| root   | ::1       |
|        | localhost |
| root   | localhost |
|        | zabbix01  |
| root   | zabbix01  |
+--------+-----------+
7 rows in set (0.00 sec)

        新建zabbix用户

MariaDB [(none)]> GRANT all ON zabbix.* TO 'zabbix'@'%' IDENTIFIED BY 'zzz';
MariaDB [(none)]> GRANT all ON zabbix.* TO 'zabbix'@'localhost' IDENTIFIED BY 'zzz';
MariaDB [(none)]> GRANT all ON zabbix.* TO 'zabbix'@'zabbix01' IDENTIFIED BY 'zzz';

  

MariaDB [(none)]> select user,host from mysql.user;
+--------+-----------+
| user   | host      |
+--------+-----------+
| zabbix | %         |
| root   | 127.0.0.1 |
| root   | ::1       |
|        | localhost |
| root   | localhost |
| zabbix | localhost |
|        | zabbix01  |
| root   | zabbix01  |
| zabbix | zabbix01  |
+--------+-----------+
9 rows in set (0.00 sec)
MariaDB [(none)]> flush privileges; 

      浏览器访问http://198.8.8.211/

centos7安装配置zabbix4.0

       上图说明php连通数据库完成。

 

二:部署zabbix server:https://www.zabbix.com/download

  安装php支持的zabbix组件:

    [root@zabbix01 ~]# yum install php-bcmath php-mbstring -y

  yum源文件获取:

    [root@zabbix01 ~]# rpm -i https://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-1.el7.noarch.rpm

  安装zabbix组件:

    [root@zabbix01 ~]# yum install zabbix-server-mysql zabbix-web-mysql -y

  自动生成数据库文件:

    [root@zabbix01 ~]# zcat /usr/share/doc/zabbix-server-mysql-4.0.1/create.sql.gz | mysql -u zabbix(用户名) -p zabbix(库名)

  修改zabbix配置文件:

    grep -n '^'[a-Z] /etc/zabbix/zabbix_server.conf

    vim /etc/zabbix/zabbix_server.conf

      ListenPort=10051

      LogFile=/var/log/zabbix/zabbix_server.log

      LogFileSize=0

      PidFile=/var/run/zabbix/zabbix_server.pid

      SocketDir=/var/run/zabbix

      DBName=zabbix

      DBUser=zabbix

      DBPassword=zzz

      DBPort=3306

      SNMPTrapperFile=/var/log/snmptrap/snmptrap.log

      ListenIP=198.8.8.211

      Timeout=4

      AlertScriptsPath=/usr/lib/zabbix/alertscripts

      ExternalScripts=/usr/lib/zabbix/externalscripts

      LogSlowQueries=3000

    

   修改zabbix在httpd中的时区:

    vim /etc/httpd/conf.d/zabbix.conf

    <IfModule mod_php5.c>
        php_value date.timezone Asia/Shanghai
        php_value max_execution_time 300
        php_value memory_limit 128M
        php_value post_max_size 16M
        php_value upload_max_filesize 2M
        php_value max_input_time 300
        php_value max_input_vars 10000
        php_value always_populate_raw_post_data -1
        # php_value date.timezone Europe/Riga
    </IfModule>

  

  启动zabbix及httpd服务:

    systemctl enable zabbix-server

    systemctl start zabbix-server

    netstat -anpt | grep zabbix 

    systemctl restart httpd.service

  web界面访问:

    http://198.8.8.211/zabbix/

centos7安装配置zabbix4.0

 

centos7安装配置zabbix4.0

 

centos7安装配置zabbix4.0

 

centos7安装配置zabbix4.0

 

centos7安装配置zabbix4.0

centos7安装配置zabbix4.0

 

centos7安装配置zabbix4.0

 

centos7安装配置zabbix4.0

 

设置中文:Administrator-Users-Admin-Language

centos7安装配置zabbix4.0

 

web界面乱码:


方法一:

       如web界面中文乱码,需要复制相应文字,使zabbix识别

  下载STKAITI.TTF字体拷贝到如下路径:

    cp STKAITI.TTF /usr/share/zabbix/fonts/

  使zabbix系统识别字体

    vim /usr/share/zabbix/include/defines.inc.php

     :%s /graphfont/kaiti/g //采用全局替换模式

方法二:

  复制C:\Windows\Fonts下的简黑字体

   centos7安装配置zabbix4.0

 

  拷贝到/usr/share/fonts/dejavu/simhei.ttf

  进入/etc/alternatives

   删除名为zabbix-web-font的软连接

  重建软连接:

    ln -s /usr/share/fonts/dejavu/simhei.ttf zabbix-web-font

 

登陆后报错:

正常安装完zabbix后,登录后zabbix监控报错zabbix server is not running: the information displayed may not be current

[root@zabbix zabbix]# find / -name zabbix.conf.php
/etc/zabbix/web/zabbix.conf.php
[root@zabbix zabbix]# vim /etc/zabbix/web/zabbix.conf.php

<?php
// Zabbix GUI configuration file.
global $DB;

$DB['TYPE']     = 'MYSQL';
$DB['SERVER']   = '192.168.8.8';
$DB['PORT']     = '3306';
$DB['DATABASE'] = 'zabbix01';
$DB['USER']     = 'zabbix01';
$DB['PASSWORD'] = 'z';

// Schema name. Used for IBM DB2 and PostgreSQL.
$DB['SCHEMA'] = '';

$ZBX_SERVER      = '192.168.8.8';
$ZBX_SERVER_PORT = '10051';
$ZBX_SERVER_NAME = 'Zabbix01';

$IMAGE_FORMAT_DEFAULT = IMAGE_FORMAT_PNG;

 

 

三:配置被监控端-如果监控服务器需要监控自己,也需要安装配置:

  3.1安装zabbix-agent客户端

  3.1.1配置zabbix yum源:

     rpm -i https://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-1.el7.noarch.rpm

  3.1.2安装客户端:

    yum install -y zabbix-agent

  3.1.3修改agent配置文件:

    [root@zabbix02 ~]# vim /etc/zabbix/zabbix_agentd.conf 

      PidFile=/var/run/zabbix/zabbix_agentd.pid

      LogFile=/var/log/zabbix/zabbix_agentd.log

      LogFileSize=0

      Server=198.8.8.211

      ListenPort=10050

      ServerActive=198.8.8.211

      Hostname=zabbix01

      Include=/etc/zabbix/zabbix_agentd.d/*.conf

  3.1.4 关闭防火墙

      [root@zabbix02 ~]# systemctl stop firewalld

  3.1.5 关闭selinux

      [root@zabbix02 ~]# setenforce 0

      [root@zabbix02 ~]# vim /etc/selinux/config

#SELINUX=enforcing
SELINUX=disabled

  3.1.6启动zabbix-agent

      [root@zabbix02 ~]# systemctl start zabbix-agent.service

      [root@zabbix02 ~]# systemctl enable zabbix-agent.service

 

四:在zabbix服务器添加被监控机      

   4.1创建主机:

     配置-主机-创建主机

 centos7安装配置zabbix4.0

 

centos7安装配置zabbix4.0

 

centos7安装配置zabbix4.0

 

五:报警配置:

  5.1邮件报警:

    [root@zabbix01 ~]# yum install mailx -y

    [root@zabbix01 ~]# vim /etc/mail.rc

    添加:

set from=xxx@163.com
set smtp=smtp.163.com
set smtp-auth-user=xxx@163.com
set smtp-auth-password=123456
set smtp-auth=login

    测试邮件发送:

[root@zabbix01 ~]# echo 'hello' | mail -s 'testmail' zm_cm@163.com

  5.2设置邮件发送脚本:

[root@zabbix01 ~]# vim /usr/lib/zabbix/alertscripts/mailx.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
        chmod +x /usr/lib/zabbix/alertscripts/mailx.sh
        chown -R zabbix.zabbix /usr/lib/zabbix/

  添加可执行权限:

[root@zabbix01 ~]# chmod +x /usr/lib/zabbix/alertscripts/mailx.sh

  测试发件:

[root@zabbix01 ~]# /usr/lib/zabbix/alertscripts/mailx.sh zm_cm@163.com  '111''222'

  5.3配置zabbix自动触发脚本

centos7安装配置zabbix4.0

 

 centos7安装配置zabbix4.0

 

centos7安装配置zabbix4.0

 

centos7安装配置zabbix4.0

centos7安装配置zabbix4.0

告警主机:{HOST.NAME}
告警IP:{HOST.IP}
告警时间:{EVENT.DATE}-{EVENT.TIME}
告警等级:{TRIGGER.SEVERITY}
告警信息:{TRIGGER.NAME}:{ITEM.VALUE}
事件ID:{EVENT.ID}

持续时间需改为60s,否则会报错:字段 "esc_period": 必须在 "60" 和 "604800" 之间 值错误。

centos7安装配置zabbix4.0

 

 centos7安装配置zabbix4.0

 

 六:监控脚本配置

   6.1 io监控脚本:

UserParameter=custom.vfs.dev.read.ops[*],cat /proc/diskstats | grep $1 | head -1 | awk '{print $$4}' //磁盘读的次数 
UserParameter=custom.vfs.dev.read.ms[*],cat /proc/diskstats | grep $1 | head -1 | awk '{print $$7}' //磁盘读的毫秒数
UserParameter=custom.vfs.dev.write.ops[*],cat /proc/diskstats | grep $1 | head -1 | awk '{print $$8}' //磁盘写的次数
UserParameter=custom.vfs.dev.write.ms[*],cat /proc/diskstats | grep $1 | head -1 | awk '{print $$11}' //磁盘写的毫秒数
UserParameter=custom.vfs.dev.io.active[*],cat /proc/diskstats | grep $1 | head -1 | awk '{print $$12}'
UserParameter=custom.vfs.dev.io.ms[*],cat /proc/diskstats | grep $1 | head -1 | awk '{print $$13}' //花费在IO操作上的毫秒数
UserParameter=custom.vfs.dev.read.sectors[*],cat /proc/diskstats | grep $1 | head -1 | awk '{print $$6}' //读扇区的次数(一个扇区的等于512B)
UserParameter=custom.vfs.dev.write.sectors[*],cat /proc/diskstats | grep $1 | head -1 | awk '{print $$10}' //写扇区的次数(一个扇区的等于512B)

  6.2 重启zabbix-agent服务

    [root@zabbix01 ~]# systemctl restart zabbix-agent

  6.3 登陆web页面添加模板

 

 

参考:

https://blog.csdn.net/rujianxuezha/article/details/79842998

https://www.linuxidc.com/Linux/2018-10/154773.htm