Zabbix——基於LNMP架構搭建Zabbix監控

前言

zabbix)是一個基於WEB界面的提供分佈式系統監視以及網絡監視功能的企業級的開源解決方案。zabbix能監視各種網絡參數,保證服務器系統的安全運營;並提供靈活的通知機制以讓系統管理員快速定位/解決存在的各種問題。

zabbix由2部分構成,zabbix server與可選組件zabbix agent。zabbix server可以通過SNMP,zabbix agent,ping,端口監視等方法提供對遠程服務器/網絡狀態的監視,數據收集等功能,它可以運行在Linux,Solaris,HP-UX,AIX,Free BSD,Open BSD,OS X等平臺上。

zabbix監控範疇(全方位監控):

全面監控,適用於任何IT基礎架構、服務、應用程序和資源的監控

  • 網絡設備監控:採集網絡中的所有性能指標和事件數據,全面監控網絡性能,時間檢測網絡故障,排除故障並防止停機

  • 服務器監控:採集並監控物理和虛擬服務器可用性,CPU、磁盤空間和內存利用率等關鍵性能指標

  • Cloud監控:收集獲取雲資源的監控指標或用戶自定義的監控指標,探測服務可用性,以及針對指標設置告警

  • 應用監控:全面實時獲取應用性能數據,通過確保服務器與應用的正常健康運行,來保證關鍵業務系統的高可用性和性能

  • 服務監控:關注IT部門服務整體的可用性、SLA指標、現有IT基礎設施架構的結構,以及更高層面的監控信息

zabbix官網 https://www.zabbix.com/

網絡環境

節點 IP 部署軟件
server監控端 192.168.11.25 LNMP、zabbix
client客戶端 192.168.11.20 zabbix-agent

#清空防火牆規則,關閉核心防護

[root@server ~]# iptables -F
[root@server ~]# setenforce 0

#關閉網絡管理

[root@server ~]# systemctl stop NetworkManager
[root@server ~]# systemctl disable NetworkManager

一、LNMP環境部署

1、安裝nginx

① 通過官方下載nginx源

[root@server ~]# wget http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

如果下載進度條不動,再重新嘗試下

② 創建nginx yum安裝源

[root@server ~]# vim /etc/yum.repos.d/nginx.repo	
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1

其中baseurl=http://nginx.org/packages/OS/OSRELEASE/$basearch/, 把”OS”替換成”rhel”或者“centos”,把“OSRELEASE”替換成“6”
③ 安裝並啓動

[root@server ~]# yum list
[root@server ~]# yum install nginx -y
[root@server ~]# systemctl start nginx
[root@server ~]# systemctl enable nginx
[root@server ~]# netstat -ntap | grep nginx

2、安裝mysq1

① 安裝mariadb

[root@server ~]# yum install -y mariadb-server mariadb
[root@server ~]# systemctl enable mariadb.service
[root@server ~]# systemctl start mariadb.service
[root@server ~]# netstat -ntap | grep 3306

② 初始化配置

[root@server ~]# mysql_secure_installation 
Enter current password for root (enter for none): 	回車
Set root password? [Y/n]	yes
New password:			123
Re-enter new password:	123
Remove anonymous users?	no
Disallow root login remotely?	no
Remove test database and access to it?	no
Reload privilege tables now?	yes

③ 登錄驗證

[root@server ~]# mysql -uroot -p

在這裏插入圖片描述

3、安裝php

① 安裝PHP相關包

[root@server ~]# rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

或wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

#安裝yum源

[root@server ~]# cd /etc/yum.repos.d
[root@server yum.repos.d]# rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

#安裝環境包

[root@server yum.repos.d]# yum install -y php72w php72w-devel php72w-fpm php72w-gd php72w-mbstring php72w-mysql

#查看php版本

[root@server yum.repos.d]# php -v

在這裏插入圖片描述

② 修改php-fpm配置文件,把apache改爲nginx

[root@server yum.repos.d]# vim /etc/php-fpm.d/www.conf 	
    user = nginx		#8行
    group = nginx		#10行

③ 配置nginx支持php

[root@server yum.repos.d]# vim /etc/nginx/conf.d/default.conf
	index index.php index.html index.htm;	#10行添加index.php

#配置php請求被傳送到後端的php-fpm模塊,去掉註釋並修改
	location ~ \.php$ {
]	    root                     /usr/share/nginx/html;
	    fastcgi_pass       127.0.0.1:9000;
	    fastcgi_index      index.php;
	    fastcgi_param SCRIPT_FILENAME   $document_root$fastcgi_script_name;
	    include                fastcgi_params;
                    }

把fastcgi_param中的/scripts改爲$document_root。 root是配置php程序放置的根目錄

④ 修改PHP配置文件

[root@server yum.repos.d]# vim /etc/php.ini
expose_php  = Off           	 #359行:隱藏php版本
short_open_tag = On    		 	 #202行:支持php短標籤

#爲PHP兼容zabbix進行修改,優化配置要求
max_execution_time = 300	 #368行:執行時間
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
date.timezone = Asia/Shanghai 	         #877行:時區

⑤ 開啓服務

[root@server yum.repos.d]# systemctl start php-fpm
[root@server yum.repos.d]# systemctl enable php-fpm
[root@server yum.repos.d]# netstat -ntap | grep 9000

⑥ 重啓下nginx服務

[root@server yum.repos.d]# systemctl restart nginx

4、測試

① 添加php測試首頁

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

#網頁訪問 192.168.11.25/info.php ,可看到PHP首頁面
在這裏插入圖片描述

② 測試連接數據庫

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

注: mysql_connect擴展自 PHP 5.5.0起已廢棄,改用mysqli或pdo_mysql

#網頁刷新 192.168.195.11/info.php ,可看到連接成功 !
在這裏插入圖片描述
③ 建立數據庫及用戶並且授權

[root@server yum.repos.d]# mysql -u root -p
MariaDB [(none)]> CREATE DATABASE zabbix character set utf8 collate utf8_bin;
MariaDB [(none)]> GRANT all privileges ON *.* TO 'zabbix'@'%' IDENTIFIED BY 'admin123';
MariaDB [(none)]> flush privileges;

注:collate的作用:對於mysql中那些字符類型的列,如VARCHAR,CHAR,TEXT類型的列,都需要有一個COLLATE類型來告知mysql

④ 測試連接zabbix數據庫

[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 "zabbix數據庫連接失敗 !";
?>

#本地連接測試

[root@server yum.repos.d]# mysql -uzabbix -padmin123

報錯:
ERROR 1045 ( 28000): Access denied for user ’ zabbix’@‘localhost’ (using password:YES)
原因:這是 由於被空用戶佔用資源
解決:刪除空用戶即可

[root@server yum.repos.d]# mysql -u root -p
MariaDB [(none)]> select user,host from mysql.user;

在這裏插入圖片描述

MariaDB [(none)]> drop user ''@localhost;

在這裏插入圖片描述
#重新登錄就可以登進去了

[root@server yum.repos.d]# mysql -uzabbix -padmin123

在這裏插入圖片描述

#網頁刷新 192.168.11.25/info.php ,就可以看到 zabbix數據庫連接成功 !
在這裏插入圖片描述

二、zabbix server部署

1、安裝部署zabbix

① 下載zabbix源並安裝

[root@server ~]# rpm -i https://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-1.el7.noarch.rpm
[root@server ~]# yum install zabbix-server-mysql zabbix-web-mysql zabbix-agent -y

可能會下載失敗,是源的問題,多次嘗試下載

② 導入數據庫腳本

[root@server ~]# zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql -uzabbix -p zabbix   #生成數據庫文件,注意密碼不要輸成root的
[root@server ~]# mysql -u zabbix -p
Enter password: 	#輸入zabbix密碼
MariaDB [(none)]> use zabbix;
MariaDB [zabbix]> show tables;

在這裏插入圖片描述

③ 修改zabbix配置文件

[root@server ~]# vim /etc/zabbix/zabbix_server.conf
38  LogFile=/var/log/zabbix/zabbix_server.log
49  LogFileSize=0
72  PidFile=/var/run/zabbix/zabbix_server.pid
82  SocketDir=/var/run/zabbix
91  DBHost=localhost	      #註釋去掉,數據庫存放地址,目前是本地
101 DBName=zabbix
117 DBUser=zabbix
125 DBPassword=admin123     #註釋去掉,zabbix的密碼
357 SNMPTrapperFile=/var/log/snmptrap/snmptrap.log
475 Timeout=4
518 AlertScriptsPath=/usr/lib/zabbix/alertscripts 
529 ExternalScripts=/usr/lib/zabbix/externalscripts
565 LogSlowQueries=3000

2、優化字體 ,牽涉到圖表中文亂碼的問題

[root@server ~]# vim /usr/share/zabbix/include/defines.inc.php
#將字體替換爲kaiti

在這裏插入圖片描述

:%s /graphfont/kaiti/g也可使用全文替換

#從微軟系統下複製相應的字體文件到/usr/share/zabbix/fonts 目錄中,目錄需要自信創建

[root@server ~]# mkdir /usr/share/zabbix/fonts 
[root@server zabbix]# ls			#上傳後查看確定已存在

在這裏插入圖片描述

#授權

[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
[root@server fonts]# systemctl start zabbix-agent.service
[root@server fonts]# systemctl enable zabbix-agent.service
[root@server fonts]# netstat -ntap | grep 10051
tcp    0   0 0.0.0.0:10051       0.0.0.0:*             LISTEN      60468/zabbix_server 
tcp    0   0 127.0.0.1:10051     127.0.0.1:40646       TIME_WAIT   -                   
tcp6   0   0 :::10051            :::*                  LISTEN      60468/zabbix_server
[root@server fonts]# systemctl restart php-fpm.service
[root@server fonts]# systemctl restart nginx

3、web頁面安裝

#網頁訪問 http://192.168.11.25/zabbix
在這裏插入圖片描述

按步驟安裝
在這裏插入圖片描述在這裏插入圖片描述在這裏插入圖片描述在這裏插入圖片描述

#這時會遇到報錯,按提示點擊下載配置文件zabbix.con.php並保存至/etc/zabbix/web/

[root@server fonts]# cd /etc/zabbix/web/
[root@server fonts]# ll

在這裏插入圖片描述

#授權並重啓服務

[root@server web]# chown zabbix.zabbix /etc/zabbix/web/zabbix.conf.php
[root@server web]# systemctl restart zabbix-server.service

#這時回到web頁面繼續安裝
在這裏插入圖片描述

#安裝後登錄 用戶名: Admin 密碼: zabbix
在這裏插入圖片描述

點擊右上角 用戶頭像 更改字體
在這裏插入圖片描述在這裏插入圖片描述在這裏插入圖片描述
字體更改完成

三、zabbix-agent端

配置代理端就是被控服務器

1、網絡環境配置

#清空防火牆規則,關閉核心防護

[root@client ~]# iptables -F
[root@client ~]# setenforce 0

#關閉網絡管理

[root@client ~]# systemctl stop NetworkManager
[root@client ~]# systemctl disable NetworkManager

2、部署zabbix-agent

① 下載源並安裝zabbix-agent

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

② 修改配置文件

檢查配置文件並指定監控服務器

[root@client ~]# vim /etc/zabbix/zabbix_agentd.conf
PidFile=/var/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
LogFileSize=0
Server=192.168.11.25		#指向監控服務器
ServerActive=192.168.11.25 	#指問監控服務器
Hostname=test1                  #指定名稱
Include=/etc/zabbix/zabbix_agentd.d/*.conf

③ 重啓服務

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

④ 檢查端口

[root@client ~]# netstat -anpt | grep zabbix      #監聽10050端口已開啓

在這裏插入圖片描述

四、監控驗證

1、添加被監控主機

在zabbix圖形界面—配置–主機–創建主機(定義主機名稱,添加羣組,填寫主機IP地址) --模板–選定要監控的模板 (httpd sshd 服務)–選擇–添加(添加連接模板)–添加(添加主機)

① 創建主機
在這裏插入圖片描述

② 添加模板
在這裏插入圖片描述在這裏插入圖片描述在這裏插入圖片描述
③ 添加主機完成
在這裏插入圖片描述

此時就能監聽httpd sshd 服務,出現問題就會顯示到web界面

2、查看主機服務狀態

① 異常查看

#查看client端並未安裝httpd服務

[root@client ~]# rpm -q httpd
[root@client ~]# systemctl status sshd

在這裏插入圖片描述

#zabbix的web監控頁面檢測到httpd服務異常
在這裏插入圖片描述
③ 正常恢復

#安裝並開啓httpd服務

[root@client ~]# yum install -y httpd
[root@client ~]# netstat -ntap | grep httpd

在這裏插入圖片描述

#zabbix監控頁面查看

httpd服務開啓後,異常已恢復
在這裏插入圖片描述

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