Zabbix 監控常見服務


監控Apache性能

1.客戶端編譯安裝Apache服務,並在編譯選項中開啟監控頁面功能.

[root@localhost ~]# yum install -y gcc openssl openssl-devel zlib zlib-devel pcre pcre-devel expat-devel libxml2-devel

2.安裝Apr-1.6.3,主要為上層的應用程序提供一個可以跨越多操作系統平台使用的底層支持接口庫.

[root@localhost ~]# wget http://www-eu.apache.org/dist//apr/apr-1.6.3.tar.gz
[root@localhost ~]# tar -xzvf apr-1.6.3.tar.gz
[root@localhost ~]# cd apr-1.6.3/
[root@localhost ~]# CC="gcc -m64" ./configure --prefix=/usr/local/apr
[root@localhost ~]# ./configure --prefix=/usr/local/apr
[root@localhost ~]# make && make install

3.安裝Apr-util-1.6.1,是包含了一些常用的開發組件,這些組件與apache的關系更加密切一些,比如存儲段和存儲段組,加密等.

[root@localhost ~]# wget http://www-eu.apache.org/dist//apr/apr-util-1.6.1.tar.gz
[root@localhost ~]# tar -xzvf apr-util-1.6.1.tar.gz
[root@localhost ~]# cd apr-util-1.6.1/
[root@localhost ~]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
[root@localhost ~]# make && make install

4.安裝Apache-2.4.33

[root@localhost ~]# wget http://www-eu.apache.org/dist//httpd/httpd-2.4.33.tar.gz
[root@localhost ~]# tar -xzvf httpd-2.4.33.tar.gz
[root@localhost ~]# cd httpd-2.4.33/
[root@localhost ~]# ./configure --prefix=/usr/local/apache2 \
--enable-rewrite \
--enable-so \
--enable-headers \
--enable-expires \
--with-mpm=worker \
--enable-modules=most \
--enable-deflate \
--enable-ssl \
--with-apr=/usr/local/apr \
--with-apr-util=/usr/local/apr-util \
--with-pcre=/usr/local/pcre
[root@localhost ~]# make && make install

5.編輯apache配置文件開啟狀態頁面功能.

[root@localhost ~]# vim /usr/local/apache2/conf/httpd.conf

140 LoadModule status_module modules/mod_status.so

473 # Real-time info on requests and configuration
474 Include conf/extra/httpd-info.conf

[root@localhost ~]# vim /usr/local/apache2/conf/extra/httpd-info.conf

 12 # Change the ".example.com" to match your domain to enable.
 13 
 14 <Location /server-status>
 15     SetHandler server-status
 16     Order deny,allow
 17     Allow from all
 18 </Location>

6.重啟Apache,並測試相應頁面是否啟動了.

[root@localhost ~]# /usr/local/apache2/bin/httpd -k restart
[root@localhost ~]# curl http://localhost/server-status

7.下載zpache模板並解壓,設置相應的權限,放入監控目錄里即可.

[root@localhost ~]# wget https://github.com/lorf/zapache/archive/master.zip
[root@localhost ~]# unzip master.zip
[root@localhost ~]# cd zapache-master/
[root@localhost ~]# cp -a zapache /etc/zapache.sh
[root@localhost ~]# chmod 777 -R /etc/zapache.sh

8.修改客戶端的配置文件,寫入一個監控字段.修改下路徑指定一下腳本保存位置.

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

UserParameter=zapache[*],/etc/zapache.sh \$1

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

9.Zabbix服務端配置

模板 -> 導入 -> 選取文件 -> 導入,然后添加一個模板主機,即可實現監控了.

## 監控Nginx性能

1.配置Yum倉庫,安裝Nginx所依賴的包文件,以及編譯器.

[root@localhost ~]# yum install -y gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel

2.編譯安裝Nginx,在編譯過程中制定開啟狀態頁--with-http_stub_status_module.

[root@localhost ~]# useradd -s /sbin/nologin -M nginx
[root@localhost ~]# wget http://nginx.org/download/nginx-1.13.12.tar.gz
[root@localhost ~]# tar -xzvf nginx-1.13.12.tar.gz
[root@localhost ~]# cd nginx-1.13.12/
[root@localhost ~]# ./configure --prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_stub_status_module

[root@localhost ~]# make && make install

3.檢查Nginx配置文件正確性,啟動關閉與重啟Nginx配置.

[root@localhost ~]# /usr/local/nginx/sbin/nginx -t      #檢測配置文件正確性
[root@localhost ~]# /usr/local/nginx/sbin/nginx         #啟動Nginx

4.編譯時開啟Nginx的狀態統計,並在Nginx主配置文件中加入以下內容.

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf

 19         location / {
 20             root   html;
 21             index  index.html index.htm;
 22         }
 #===============================================
 24          location /lyshark {      #添加以下字段,開啟統計頁面.
 25
 26             stub_status on;
 27             access_log off;
 28             allow 127.0.0.1;
 29             }
 #===============================================
 31         error_page   500 502 503 504  /50x.html;
 32         location = /50x.html {
 33             root   html;
 34         }

5.在客戶機上創建監控腳本,並配置好相應的權限.

[root@localhost ~]# vim /etc/nginx_status.sh

#!/bin/bash
	 
HOST="127.0.0.1"
PORT="80"
WEB="lyshark"

function ping {
/sbin/pidof nginx | wc -l 
}
function active {
	/usr/bin/curl "http://$HOST:$PORT/$WEB/" 2>/dev/null| grep 'Active' | awk '{print $NF}' 	
}
function reading {
	/usr/bin/curl "http://$HOST:$PORT/$WEB/" 2>/dev/null| grep 'Reading' | awk '{print $2}'
}
function writing {
	/usr/bin/curl "http://$HOST:$PORT/$WEB/" 2>/dev/null| grep 'Writing' | awk '{print $4}'
}
function waiting {
	/usr/bin/curl "http://$HOST:$PORT/$WEB/" 2>/dev/null| grep 'Waiting' | awk '{print $6}'
}
function accepts {
	/usr/bin/curl "http://$HOST:$PORT/$WEB/" 2>/dev/null| awk NR==3 | awk '{print $1}'
}
function handled {
	/usr/bin/curl "http://$HOST:$PORT/$WEB/" 2>/dev/null| awk NR==3 | awk '{print $2}'
}
function requests {
	/usr/bin/curl "http://$HOST:$PORT/$WEB/" 2>/dev/null| awk NR==3 | awk '{print $3}'
}

case $1 in

	"ping")		ping ;;
	"active") 	active ;;
	"reading")	reading ;;
	"writing")	writing ;;
	"waiting")	waiting ;;
	"accepts")	accepts ;;
	"handled")	handled ;;
	"requests")	requests ;;
	*)
        	echo "Usage: $0 { ping |active | accepts | handled | requests | reading | writing | waiting }" ;;
esac

[root@localhost ~]# chmod 755 -R /etc/nginx_status.sh

6.將自定義的UserParameter加入配置文件,然后重啟agentd.

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

UnsafeUserParameters=1
UserParameter=nginx.status[*],/etc/nginx_status.sh \$1

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

7.服務端使用zabbix_get獲取數據,並在web頁面導入模板即可.

[root@localhost ~]# yum install -y zabbix-get
[root@localhost ~]# zabbix_get -s 192.168.1.10 -k "nginx.status[ping]"

模板下載:http://www.ttlsa.com/wp-content/uploads/2015/10/zabbix_monitor_nginx_template_ttlsa_com.zip

配置 -> 模板 -> 導入 -> 導入模板 -> 選擇Nginx模板文件 -> 導入
配置 -> 主機 -> 模板 -> 鏈接模板 -> 選擇 -> Template App NGINX ->添加 -> 更新

## Zabbix監控MariaDB

1.首先在被控主機上,通過Yum方式安裝一個MariaDB數據庫.

[root@localhost ~]# yum install -y mariadb mariadb-server
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Package 1:mariadb-5.5.60-1.el7_5.x86_64 already installed and latest version
Package 1:mariadb-server-5.5.60-1.el7_5.x86_64 already installed and latest version
Nothing to do

2.配置數據庫的一個guest的用戶,用來監控狀態.

[root@localhost ~]# systemctl restart mariadb
[root@localhost ~]# systemctl enable mariadb

[root@localhost ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.60-MariaDB MariaDB Server

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

MariaDB [(none)]> grant select on *.* to guest@localhost identified by "guest";
Query OK, 0 rows affected (0.00 sec)

3.配置一個MariaDB腳本文件用來獲取用戶信息.

[root@localhost ~]# vim /etc/mysql_status.sh

#!/bin/bash
 
MYSQL_USER="guest"
MYSQL_PWD="guest"
MYSQL_HOST="localhost"
MYSQL_PORT="3306"
	 
# 數據連接
MYSQL_CONN="/usr/bin/mysqladmin -u${MYSQL_USER} -p${MYSQL_PWD} -h${MYSQL_HOST} -P${MYSQL_PORT}"
	 
# 參數是否正確
if [ $# -ne "1" ]
then 
	echo "arg error!" 
fi 
	 
# 獲取數據
case $1 in 
	Uptime) 
			result=`${MYSQL_CONN} status|cut -f2 -d":"|cut -f1 -d"T"` 
			echo $result 
		;; 
	Com_update) 
			result=`${MYSQL_CONN} extended-status |grep -w "Com_update"|cut -d"|" -f3` 
			echo $result 
		;; 
	Slow_queries) 
			result=`${MYSQL_CONN} status |cut -f5 -d":"|cut -f1 -d"O"` 
			echo $result 
		;; 
	Com_select) 
			result=`${MYSQL_CONN} extended-status |grep -w "Com_select"|cut -d"|" -f3` 
			echo $result 
		;; 
	Com_rollback) 
			result=`${MYSQL_CONN} extended-status |grep -w "Com_rollback"|cut -d"|" -f3` 
			echo $result 
		;; 
	Questions) 
			result=`${MYSQL_CONN} status|cut -f4 -d":"|cut -f1 -d"S"` 
			echo $result 
		;; 
	Com_insert) 
			result=`${MYSQL_CONN} extended-status |grep -w "Com_insert"|cut -d"|" -f3` 
			echo $result 
		;; 
	Com_delete) 
			result=`${MYSQL_CONN} extended-status |grep -w "Com_delete"|cut -d"|" -f3` 
			echo $result 
		;; 
	Com_commit) 
			result=`${MYSQL_CONN} extended-status |grep -w "Com_commit"|cut -d"|" -f3` 
			echo $result 
		;; 
	Bytes_sent) 
			result=`${MYSQL_CONN} extended-status |grep -w "Bytes_sent" |cut -d"|" -f3` 
			echo $result 
		;; 
	Bytes_received) 
			result=`${MYSQL_CONN} extended-status |grep -w "Bytes_received" |cut -d"|" -f3` 
			echo $result 
		;; 
	Com_begin) 
			result=`${MYSQL_CONN} extended-status |grep -w "Com_begin"|cut -d"|" -f3` 
			echo $result 
		;; 			
		*) 
			echo "Usage:$0(Uptime|Com_update|Slow_queries|Com_select|Com_rollback|Questions|Com_insert|Com_delete|Com_commit|Bytes_sent|Bytes_received|Com_begin)" 
		;; 
	esac

4.將自定義的UserParameter加入配置文件,然后重啟agentd

[root@localhost ~]# chmod 755 -R /etc/mysql_status.sh
[root@localhost ~]# vim /etc/zabbix/zabbix_agentd.conf

UnsafeUserParameters=1
UserParameter=mysql.status[*],/etc/mysql_status.sh \$1

[root@localhost ~]# rm -fr /etc/zabbix/zabbix_agentd.d/userparameter_mysql.conf
[root@localhost ~]# systemctl restart zabbix-agent

5.服務端使用zabbix_get獲取數據,並在Web主機配置好模板.

[root@localhost ~]# yum install -y zabbix-get
[root@localhost ~]# zabbix_get -s 192.168.1.10 -k "mysql.status[Uptime]"

配置 -> 主機 -> 模板 -> 鏈接模板 -> 選擇 -> Template DB MySQL ->添加 -> 更新


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM