LNMP架構基礎上zabbix監控平臺部署!

前言

一:zabbix部署

  • 主機分配

    主機 IP地址 部署服務
    server 192.168.233.136 LNMP、zabbix
    client 192.168.233.137 zabbix代理

1.1:開局優化

  • 關閉防火牆、網絡管理、主機名修改(server、client),僅展示server操作

    [root@192 ~]# hostnamectl set-hostname server
    [root@192 ~]# su
    [root@server ~]# 
    [root@server ~]# systemctl stop firewalld && systemctl disable firewalld
    Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
    Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
    [root@server ~]# setenforce 0 && sed -i "s/SELINUX=*/SELINUX=disabled/g" /etc/selinux/config
    [root@server ~]# systemctl stop NetworkManager && systemctl disable NetworkManager
    Removed symlink /etc/systemd/system/multi-user.target.wants/NetworkManager.service.
    Removed symlink /etc/systemd/system/dbus-org.freedesktop.NetworkManager.service.
    Removed symlink /etc/systemd/system/dbus-org.freedesktop.nm-dispatcher.service.
    Removed symlink /etc/systemd/system/network-online.target.wants/NetworkManager-wait-online.service.
    
    

1.2:LNMP安裝環境(使用yum安裝的方式)

nagios是一款比較老的監控工具,但是因爲插件太多,不是主流

cacti是一款圖形化的監控分析工具

zabbix是基於web界面的提供分佈式功能的監控工具,監控上線是300-500臺

zabbix可以監控網絡設備(例如思科、華爲等)、服務器(例如IBM、聯想、戴爾等)、應用監控(Apache、MQ、中間件、Tomcat等)和服務(HTTP、https、nginx、ssl等)等監控

zabbix使用的最穩定的版本是4.0,LTS是指測試,5.0版本在官網下載的時候比4.0版本多了一個nginx服務器的選擇

ajax:異步刷新

zcat:不解壓直接查看壓縮包

  • 1、安裝nginx

    [root@server ~]# wget 
     http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm	'//下載nginx軟件包'
    [root@server ~]# vim /etc/yum.repos.d/nginx.repo	'//編輯yum源'
    [nginx-stable]
    name=nginx stable repo
    baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
    gpgcheck=0
    enabled=1
    [root@server ~]# yum clean all
    已加載插件:fastestmirror, langpacks
    正在清理軟件源: base extras nginx-stable updates
    Cleaning up list of fastest mirrors
    [root@server ~]# yum makecache	'//建立yum緩存'
    [root@server ~]# systemctl start nginx
    [root@server ~]# systemctl enable nginx
    Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
    
    
  • 網頁測試:發現nginx搭建成功

    mark

  • 2、安裝mysql(mariadb)

    [root@server ~]# yum -y install mariadb-server mariadb
    [root@server ~]# systemctl start mariadb.service 
    [root@server ~]# systemctl enable mariadb.service 
    Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
    [root@server ~]# netstat -ntap |grep 3306
    tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      95170/mysqld  
    '//接下來執行mysql安全配置嚮導'
    [root@server ~]# 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	'//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] y	'//是否刪除匿名用戶,建議y刪除'
     ... Success!
    
    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] y	'//是否禁止root遠程登錄,建議y禁止'
     ... Success!
    
    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	'//是否刪除test數據庫,根據實際情況'
     ... skipping.
    
    Reloading the privilege tables will ensure that all changes made so far
    will take effect immediately.
    
    Reload privilege tables now? [Y/n] y	'//是否重新加載權限表,建議y'
     ... Success!
    
    Cleaning up...
    
    All done!  If you've completed all of the above steps, your MariaDB
    installation should now be secure.
    
    Thanks for using MariaDB!
    [root@server ~]# mysql -uroot -p	'//測試登陸mysql數據庫'
    Enter password: 
    Welcome to the MariaDB monitor.  Commands end with ; or \g.
    Your MariaDB connection id is 8
    Server version: 5.5.65-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)]> show databases;
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | mysql              |
    | performance_schema |
    | test               |
    +--------------------+
    4 rows in set (0.00 sec)
    
    MariaDB [(none)]> exit
    Bye
    
    
  • 3、安裝PHP

    [root@server ~]# rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm	'//升級rpm包'
    獲取https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
    警告:/var/tmp/rpm-tmp.xsEbUC: 頭V3 RSA/SHA256 Signature, 密鑰 ID 352c64e5: NOKEY
    準備中...                          ################################# [100%]
    正在升級/安裝...
       1:epel-release-7-12                ################################# [100%]
    [root@server ~]# rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
    獲取https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
    警告:/var/tmp/rpm-tmp.YidqBL: 頭V4 RSA/SHA1 Signature, 密鑰 ID 62e74ca5: NOKEY
    準備中...                          ################################# [100%]
    正在升級/安裝...
       1:webtatic-release-7-3             ################################# [100%]
    [root@server ~]# cd /etc/yum.repos.d/
    [root@server yum.repos.d]# yum -y install php72w php72w-devel php72w-fpm php72w-gd php72w-mbstring php72w-mysql	'//安裝PHP環境工具包'	
    [root@server yum.repos.d]# php -v	'//查看版本'
    PHP 7.2.27 (cli) (built: Jan 26 2020 15:49:49) ( NTS )
    Copyright (c) 1997-2018 The PHP Group
    Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
    
    
  • 4、配置php-fpm.d模塊的配置文件與nginx配置文件並關聯

    [root@server yum.repos.d]# vim /etc/php-fpm.d/www.conf 
    user = nginx	'//8行將apache修改爲nginx'
    ; RPM: Keep a group allowed to write in log dir.
    group = nginx	'//10行將apache修改爲nginx'
    [root@server yum.repos.d]# vim /etc/nginx/conf.d/default.conf 	'//修改nginx配置文件與php相關聯'
      index  index.php index.html index.htm;	'//第10行添加index.php文件'
    ...省略內容	'//下方是30-36行的字段,爲配置php請求被傳送到後端的php-fpm模塊的配置'
         location ~ \.php$ {
             root           /usr/share/nginx/html;	'//修改站點目錄'
             fastcgi_pass   127.0.0.1:9000;
             fastcgi_index  index.php; 34         fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;	'//將fastcgi_param中的/scripts改爲$document_root,root是配置php程序紡織的根目錄'
             include        fastcgi_params;
         }
    
    
  • 5、優化PHP配置文件

    [root@server yum.repos.d]# vim /etc/php.ini 
    short_open_tag = On	'//202行改爲On,支持php短標籤'
    expose_php = Off	'//359修改爲Off,隱藏php版本'
    '//以下都是zabbix的配置要求'
    max_execution_time = 300	'//執行時間,在一個程序執行的過程中能夠等待的執行時間,執行時間過程中如果沒有執行完會結束該程序,以防出現卡死,默認30秒'
    max_input_time = 300	'//378行,接受數據的等待時間'
    memory_limit = 128M		'//389行,每個腳本的佔用內存限制'
    post_max_size = 16M		'//656行,post數據的最大限制'
    upload_max_filesize = 2M	'//799,下載文件的大小限制'
    always_populate_raw_post_data = -1	'//800行添加此句,可以用$HTTP_RAW_POST_DATA接受post raw data(原始未處理數據)'
    date.timezone = Asia/Shanghai	'//878行,修改時區爲上海'
    [root@server yum.repos.d]# systemctl start php-fpm
    [root@server yum.repos.d]# systemctl enable php-fpm
    Created symlink from /etc/systemd/system/multi-user.target.wants/php-fpm.service to /usr/lib/systemd/system/php-fpm.service.
    [root@server yum.repos.d]# netstat -ntap |grep 9000
    tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      96130/php-fpm: mast 
    [root@server yum.repos.d]# systemctl restart nginx
    
    
  • 6、編寫PHP測試首頁並測試網頁

    [root@server yum.repos.d]# vim /usr/share/nginx/html/info.php
    <?php
     phpinfo();
    ?>
    
    
  • mark

  • 測試連接數據庫

    [root@server yum.repos.d]# vim /usr/share/nginx/html/info.php	'//重新修改首頁文件'
    <?php
     $link=mysqli_connect('127.0.0.1','root','123123');
     if ($link) echo "連接成功";
     else echo "連接失敗";
    ?>
    '//注:mysql_connetct擴展從PHP 5.5.0起開始廢棄,改用mysqli或pdo_mysql'
    

    mark

  • 7、mysql數據庫創建zabbix用戶

    [root@server yum.repos.d]# mysql -uroot -p
    Enter password: 
    Welcome to the MariaDB monitor.  Commands end with ; or \g.
    Your MariaDB connection id is 12
    Server version: 5.5.65-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)]> CREATE DATABASE zabbix character set utf8 collate utf8_bin;
    Query OK, 1 row affected (0.00 sec)
    
    MariaDB [(none)]> GRANT all privileges ON *.* TO 'zabbix'@'%' IDENTIFIED BY 'admin123';
    Query OK, 0 rows affected (0.00 sec)
    
    MariaDB [(none)]> flush privileges;
    Query OK, 0 rows affected (0.00 sec)
    
    MariaDB [(none)]> exit
    Bye
    '//collate的作用:對於mysql中的那些字符類型的列,如varchar,char,text類型的列,都需要有一個collate類型來告知mysql如何對該列進行排序和比較'
    [root@server yum.repos.d]# mysql -uzabbix -p	'//先測試數據庫是否能登陸'
    Enter password: 	
    Welcome to the MariaDB monitor.  Commands end with ; or \g.
    Your MariaDB connection id is 13
    Server version: 5.5.65-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)]> select user,host from mysql.user;	'//如果報1045的error,是因爲此處有空用戶佔用導致本地無法登陸,解決辦法是刪除相應的空用戶:drop user ''@localhost;然後flush privileges
    +--------+-----------+
    | user   | host      |
    +--------+-----------+
    | zabbix | %         |
    | root   | 127.0.0.1 |
    | root   | ::1       |
    | root   | localhost |
    +--------+-----------+
    4 rows in set (0.00 sec)
    
    
  • 重新修改PHP首頁文件並測試

    [root@server yum.repos.d]# vim /usr/share/nginx/html/info.php
    <?php
     $link=mysqli_connect('127.0.0.1','zabbix','admin123');
     if ($link) echo "zabbix數據庫連接成功";
     else echo "連接失敗";
    ?>
    
    
  • mark

1.3:部署zabbix的server

  • 1、安裝數據庫、Zabbix server,Web前端,agent

    [root@server yum.repos.d]# rpm -Uvh https://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-2.el7.noarch.rpm
    獲取https://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-2.el7.noarch.rpm
    警告:/var/tmp/rpm-tmp.i4nVQE: 頭V4 RSA/SHA512 Signature, 密鑰 ID a14fe591: NOKEY
    準備中...                          ################################# [100%]
    正在升級/安裝...
       1:zabbix-release-4.0-2.el7         ################################# [100%]
    [root@server yum.repos.d]# yum -y install zabbix-server-mysql zabbix-web-mysql zabbix-agent	'//因爲網絡問題,需要多次重試纔可下載完成'
    
    
  • 2、導入初始架構和數據,系統將提示您輸入新創建的密碼。

    [root@server yum.repos.d]# zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql -uzabbix -p zabbix
    Enter password: 
    [root@server yum.repos.d]# mysql -uzabbix -p	'//登陸數據庫查看導入的數據'
    Enter password: 
    Welcome to the MariaDB monitor.  Commands end with ; or \g.
    Your MariaDB connection id is 17
    Server version: 5.5.65-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)]> show databases;
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | mysql              |
    | performance_schema |
    | test               |
    | zabbix             |
    +--------------------+
    5 rows in set (0.01 sec)
    
    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]> show tables;	'//會發現導入的數據'
    '//慢日誌:記錄了執行速度特別慢的SQL語句'
    
  • 3、優化zabbix配置文件並修改圖表中文亂碼

    [root@server yum.repos.d]# vim /etc/zabbix/zabbix_server.conf 
    DBHost=localhost	'//91行取消註釋'
    DBPassword=admin123	'//124行設置密碼'
    [root@server yum.repos.d]# vim /usr/share/zabbix/include/defines.inc.php 
    '//:% s /graphfont/kaiti/g末行模式修改'
    [root@server yum.repos.d]# cd /usr/share/zabbix
    [root@server zabbix]# mkdir fonts
    [root@server zabbix]# cd fonts
    [root@server fonts]# rz -E
    rz waiting to receive.
    [root@server fonts]# ls
    STKAITI.TTF	'//上傳相應字體到指定目錄'
    
    
  • 4、給權限,開啓相關zabbix服務

    [root@server fonts]# cp -r /usr/share/zabbix/ /usr/share/nginx/html/
    [root@server fonts]# chown -R zabbix:zabbix /etc/zabbix
    [root@server fonts]# chown -R zabbix:zabbix /usr/share/nginx/
    [root@server fonts]# chown -R zabbix:zabbix /usr/lib/zabbix/
    [root@server fonts]# chmod -R 755 /etc/zabbix/web/
    [root@server fonts]# chmod -R 777 /var/lib/php/session/
    [root@server fonts]# systemctl start zabbix-server.service 
    [root@server fonts]# systemctl enable zabbix-server.service 
    Created symlink from /etc/systemd/system/multi-user.target.wants/zabbix-server.service to /usr/lib/systemd/system/zabbix-server.service.
    [root@server fonts]# systemctl start zabbix-agent.service 
    [root@server fonts]# systemctl enable zabbix-agent.service 
    Created symlink from /etc/systemd/system/multi-user.target.wants/zabbix-agent.service to /usr/lib/systemd/system/zabbix-agent.service.
    [root@server fonts]# netstat -ntap |grep 10051
    tcp        0      0 0.0.0.0:10051           0.0.0.0:*               LISTEN      97671/zabbix_server 
    tcp        0      0 127.0.0.1:10051         127.0.0.1:51692         TIME_WAIT   -                   
    tcp6       0      0 :::10051                :::*                    LISTEN      97671/zabbix_server 
    [root@server fonts]# systemctl restart php-fpm.service 
    [root@server fonts]# systemctl restart nginx
    
    
  • 登陸網頁操作:192.168.233.136/zabbix,用戶Admin,密碼zabbix

    mark

    mark

    mark

    mark

    [root@server fonts]# cd /etc/zabbix/web/
    [root@server web]# ls
    maintenance.inc.php
    [root@server web]# rz -E
    rz waiting to receive.
    [root@server web]# ls
    maintenance.inc.php  zabbix.conf.php
    [root@server web]# chown zabbix.zabbix /etc/zabbix/web/zabbix.conf.php [root@server web]# systemctl restart zabbix-server.service 
    

    mark

    mark

    密碼是zabbix

    mark

    mark

1.4:代理端配置

  • 1、安裝yum源與zabbix代理

    [root@client ~]# rpm -Uvh https://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-2.el7.noarch.rpm
    獲取https://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-2.el7.noarch.rpm
    警告:/var/tmp/rpm-tmp.Qwfdol: 頭V4 RSA/SHA512 Signature, 密鑰 ID a14fe591: NOKEY
    準備中...                          ################################# [100%]
    正在升級/安裝...
       1:zabbix-release-4.0-2.el7         ################################# [100%]
    [root@client ~]# yum -y install zabbix-agent
    
    
  • 2、修改zabbix代理配置文件並開啓服務

    [root@client ~]# vim /etc/zabbix/zabbix_agentd.conf 
    Server=192.168.233.136	'//98行指向監控服務器地址'
    ServerActive=192.168.233.136	'//139行指向監控服務器地址'
    Hostname=Zabbix-test	'//150行修改名稱'
    [root@client ~]# systemctl start zabbix-agent.service 
    [root@client ~]# systemctl enable zabbix-agent.service 
    Created symlink from /etc/systemd/system/multi-user.target.wants/zabbix-agent.service to /usr/lib/systemd/system/zabbix-agent.service.
    [root@client ~]# netstat -ntap |grep zabbix
    tcp        0      0 0.0.0.0:10050           0.0.0.0:*               LISTEN      82119/zabbix_agentd 
    tcp6       0      0 :::10050                :::*                    LISTEN      82119/zabbix_agentd 
    
    

1.5:web頁面操作

  • 1、添加被控主機:配置–主機–創建主機

    mark

    mark

    mark

  • 2、回到首頁查看監控狀態,等待500秒(一個監控週期)

    mark

如有疑問可評論區交流!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章