實驗環境php
實驗用2到2臺機器,實驗所用機器系統環境以下,能夠看到2臺機器的主機名和IP地址html
[root@linux-node1 ~]# cat /etc/redhat-release CentOS Linux release 7.1.1503 (Core) [root@linux-node1 ~]# cat /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.58.11 linux-node1 linux-node1.nmap.com 192.168.58.12 linux-node2 linux-node2.nmap.com [root@linux-node1 ~]# hostname linux-node1.nmap.com [root@linux-node1 ~]#
nginx自帶的監控介紹node
先學習下nginx自帶的的狀態監控mysql
如下軟件包是編譯安裝nginx須要的包linux
[root@linux-node1 ~]# yum install -y glibc gcc-c++ pcre-devel gcc openssl-devel Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: mirrors.zju.edu.cn * epel: mirrors.tuna.tsinghua.edu.cn * extras: mirrors.aliyun.com * updates: mirrors.aliyun.com Package glibc-2.17-157.el7_3.1.x86_64 already installed and latest version Package gcc-c++-4.8.5-11.el7.x86_64 already installed and latest version Package pcre-devel-8.32-15.el7_2.1.x86_64 already installed and latest version Package gcc-4.8.5-11.el7.x86_64 already installed and latest version Package 1:openssl-devel-1.0.1e-60.el7_3.1.x86_64 already installed and latest version Nothing to do [root@linux-node1 ~]#
下載nginx穩定版並解壓ios
[root@linux-node1 ~]# cd /usr/local/src/ [root@linux-node1 src]# ls [root@linux-node1 src]# wget http://nginx.org/download/nginx-1.10.3.tar.gz --2017-03-19 18:04:23-- http://nginx.org/download/nginx-1.10.3.tar.gz Resolving nginx.org (nginx.org)... 95.211.80.227, 206.251.255.63 Connecting to nginx.org (nginx.org)|95.211.80.227|:80... connected. HTTP request sent, awaiting response... 200 OK Length: 911509 (890K) [application/octet-stream] Saving to: ‘nginx-1.10.3.tar.gz’ 100%[==================================================>] 911,509 303KB/s in 2.9s 2017-03-19 18:04:27 (303 KB/s) - ‘nginx-1.10.3.tar.gz’ saved [911509/911509] [root@linux-node1 src]# tar xfz nginx-1.10.3.tar.gz [root@linux-node1 src]# cd nginx-1.10.3 [root@linux-node1 nginx-1.10.3]#
新建一個用戶www,用於以普通用戶運行nginx ;配置,生成Makefile文件nginx
[root@linux-node1 nginx-1.10.3]# useradd -s /sbin/nologin -M www [root@linux-node1 nginx-1.10.3]# ./configure --prefix=/usr/local/nginx-1.10.3 \ > --user=www --group=www \ > --with-http_ssl_module \ > --with-http_stub_status_module
配置完畢,pcre是它默認加的,不少人編譯使用--with-pcre 其實使用系統的就夠了c++
checking for getaddrinfo() ... found checking for PCRE library ... found checking for PCRE JIT support ... found checking for OpenSSL library ... found checking for zlib library ... found creating objs/Makefile Configuration summary + using system PCRE library + using system OpenSSL library + md5: using OpenSSL library + sha1: using OpenSSL library + using system zlib library nginx path prefix: "/usr/local/nginx-1.10.3" nginx binary file: "/usr/local/nginx-1.10.3/sbin/nginx" nginx modules path: "/usr/local/nginx-1.10.3/modules" nginx configuration prefix: "/usr/local/nginx-1.10.3/conf" nginx configuration file: "/usr/local/nginx-1.10.3/conf/nginx.conf" nginx pid file: "/usr/local/nginx-1.10.3/logs/nginx.pid" nginx error log file: "/usr/local/nginx-1.10.3/logs/error.log" nginx http access log file: "/usr/local/nginx-1.10.3/logs/access.log" nginx http client request body temporary files: "client_body_temp" nginx http proxy temporary files: "proxy_temp" nginx http fastcgi temporary files: "fastcgi_temp" nginx http uwsgi temporary files: "uwsgi_temp" nginx http scgi temporary files: "scgi_temp" [root@linux-node1 nginx-1.10.3]#
編譯和安裝web
[root@linux-node1 nginx-1.10.3]# make && make install
以上安裝完畢,安裝結尾部分以下sql
cp conf/nginx.conf '/usr/local/nginx-1.10.3/conf/nginx.conf.default' test -d '/usr/local/nginx-1.10.3/logs' \ || mkdir -p '/usr/local/nginx-1.10.3/logs' test -d '/usr/local/nginx-1.10.3/logs' \ || mkdir -p '/usr/local/nginx-1.10.3/logs' test -d '/usr/local/nginx-1.10.3/html' \ || cp -R html '/usr/local/nginx-1.10.3' test -d '/usr/local/nginx-1.10.3/logs' \ || mkdir -p '/usr/local/nginx-1.10.3/logs' make[1]: Leaving directory `/usr/local/src/nginx-1.10.3' [root@linux-node1 nginx-1.10.3]# echo $? 0 [root@linux-node1 nginx-1.10.3]#
作軟連接
[root@linux-node1 nginx-1.10.3]# cd [root@linux-node1 ~]# ln -s /usr/local/nginx-1.10.3/ /usr/local/nginx [root@linux-node1 ~]#
怎麼監控nginx,能夠搜索一些作監控的產品他們作的文檔,比較好,網站上不少人寫的博客其實有的有錯誤,監控寶這種即便出現錯誤也不會太大
搜索監控寶的nginx相關的wiki
http://www.jiankongbao.com/search.php?q=nginx
http://wiki.jiankongbao.com/doku.php/%E6%96%87%E6%A1%A3:%E5%AE%89%E5%85%A8%E6%8C%87%E5%BC%95#nginx_監控
須要注意的是,Nginx默認安裝不包含狀態模塊stub_status,因此,在編譯Nginx的時候,須要添加如下參數:
--with-http_stub_status_module
一旦包含stub_status模塊後,咱們就能夠在配置文件nginx.conf中開啓狀態頁面:
http { server { listen 80; server_name localhost; location / { root html; index index.html index.htm; } location /nginx-status { stub_status on; access_log off; } } }
以上配置文件中,咱們實際增長的部分是:
location /nginx-status { stub_status on; access_log off; }
一樣,假如Nginx所在服務器的IP爲10.0.0.1,同時指向它的域名爲www.domain.com,這樣一來,Nginx的狀態頁面即是:
http://10.0.0.1/nginx-status
或者
http://www.domain.com/nginx-status
一樣,建議您將以上示例中的nginx-status修改成其它字符串。
另外,Nginx的stub_status也支持受權IP的配置,您能夠參考Nginx的手冊,監控寶提供的服務監控點IP地址爲:
開始操做修改nginx.conf
[root@linux-node1 ~]# cd /usr/local/nginx/conf/ [root@linux-node1 conf]# vim nginx.conf
編輯以下,順便加上訪問控制
修改完畢,檢測語法,啓動,檢查
[root@linux-node1 conf]# vim nginx.conf [root@linux-node1 conf]# /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx-1.10.3/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx-1.10.3/conf/nginx.conf test is successful [root@linux-node1 conf]# /usr/local/nginx/sbin/nginx [root@linux-node1 conf]# lsof -i:80 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME nginx 10245 root 6u IPv4 157419 0t0 TCP *:http (LISTEN) nginx 10246 www 6u IPv4 157419 0t0 TCP *:http (LISTEN) [root@linux-node1 conf]#
網頁上查看
關於狀態頁面的解釋
Active connections: 4
server accepts handled requests
4 4 23
Reading: 0 Writing: 3 Waiting: 0
Active connections:對後端發起的活動鏈接數。
Server accepts handled requests:Nginx總共處理了4個鏈接,成功建立4次握手(證實中間沒有失敗的),總共處理了23個請求。
Reading:Nginx 讀取到客戶端的Header信息數。
Writing:Nginx 返回給客戶端的Header信息數。
Waiting:開啓keep-alive的狀況下,這個值等於 active – (reading + writing),意思就是Nginx已經處理完成,正在等候下一次請求指令的駐留鏈接。
監控要作的事,最基礎的部分以下:
採集,存儲,展現,告警
nagios和zabbix對於一些大量主機須要監控的場景時,nagios不如zabbix
nagios監控就有瓶頸了。由於server端主動訪問,1000臺就有瓶頸了
zabbix主動模式徹底解決,頂多就是server端的讀寫壓力
安裝zabbix3.0
在安裝以前請確保防火牆以及selinux關閉,並作好時間同步
1.一、yum源配置
rpm -ivh http://mirrors.aliyun.com/zabbix/zabbix/3.0/rhel/7/x86_64/zabbix-release-3.0-1.el7.noarch.rpm
1.二、安裝相關軟件
zabbix把數據放到了庫裏,它支持mysql,centos7以後mysql默認是mariadb
yum install zabbix-server zabbix-web zabbix-server-mysql zabbix-web-mysql mariadb-server mariadb zabbix-agent -y
zabbix運行的web容器是apache,它默認把apache做爲依賴安裝上了
安裝完畢,留意下版本
[root@linux-node1 ~]# yum install zabbix-server zabbix-web zabbix-server-mysql zabbix-web-mysql mariadb-server mariadb zabbix-agent -y Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: mirrors.zju.edu.cn * epel: mirrors.tuna.tsinghua.edu.cn * extras: mirrors.aliyun.com * updates: mirrors.aliyun.com Package zabbix-server-mysql-3.0.8-2.el7.x86_64 already installed and latest version Package zabbix-web-3.0.8-2.el7.noarch already installed and latest version Package zabbix-server-mysql-3.0.8-2.el7.x86_64 already installed and latest version Package zabbix-web-mysql-3.0.8-2.el7.noarch already installed and latest version Package 1:mariadb-server-5.5.52-1.el7.x86_64 already installed and latest version Package 1:mariadb-5.5.52-1.el7.x86_64 already installed and latest version Package zabbix-agent-3.0.8-2.el7.x86_64 already installed and latest version Nothing to do [root@linux-node1 ~]#
1.三、修改PHP時區配置
因爲註釋裏帶#,這裏's###g' 改爲's@@@g' 結構替換
sed -i 's@# php_value date.timezone Europe/Riga@php_value date.timezone Asia/Shanghai@g' /etc/httpd/conf.d/zabbix.conf
替換完畢。
2、數據庫配置
Centos7上MySQL已經變成了mariadb。
2.一、啓動數據庫
systemctl start mariadb
2.二、建立zabbix所用的數據庫及用戶
mysql create database zabbix character set utf8 collate utf8_bin; grant all on zabbix.* to zabbix@'localhost' identified by 'zabbix'; exit
導入數據
zcat命令用於不真正解壓縮文件,就能顯示壓縮包中文件的內容的場合。
[root@linux-node1 ~]# cd /usr/share/doc/zabbix-server-mysql-3.0.8/ [root@linux-node1 zabbix-server-mysql-3.0.8]# ls AUTHORS ChangeLog COPYING create.sql.gz NEWS README [root@linux-node1 zabbix-server-mysql-3.0.8]# zcat create.sql.gz | mysql -uzabbix -pzabbix zabbix [root@linux-node1 zabbix-server-mysql-3.0.8]#
2.三、修改zabbix配置
vim /etc/zabbix/zabbix_server.conf
取消下面註釋,並修改爲正確值
DBHost=localhost #數據庫所在主機
DBName=zabbix #數據庫名
DBUser=zabbix #數據庫用戶
DBPassword=zabbix #數據庫密碼
2.四、啓動zabbix及http
遇到問題,zabbix-server啓動失敗,httpd啓動成功
[root@linux-node1 ~]# systemctl start zabbix-server Job for zabbix-server.service failed because a fatal signal was delivered to the control process. See "systemctl status zabbix-server.service" and "journalctl -xe" for details. [root@linux-node1 ~]# systemctl start httpd [root@linux-node1 ~]# getenforce Disabled [root@linux-node1 ~]# journalctl -xe -- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel -- -- Unit zabbix-server.service has begun starting up. Mar 19 19:12:59 linux-node1.nmap.com kernel: zabbix_server[11200]: segfault at 18 ip 00007fd7 Mar 19 19:12:59 linux-node1.nmap.com systemd[1]: zabbix-server.service: control process exite Mar 19 19:13:01 linux-node1.nmap.com systemd[1]: Failed to start Zabbix Server. -- Subject: Unit zabbix-server.service has failed -- Defined-By: systemd -- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel -- -- Unit zabbix-server.service has failed. -- -- The result is failed. Mar 19 19:13:01 linux-node1.nmap.com systemd[1]: Unit zabbix-server.service entered failed st Mar 19 19:13:01 linux-node1.nmap.com systemd[1]: zabbix-server.service failed. [root@linux-node1 ~]#
升級 trousers以後,啓動zabbix-server成功(系統版本centos7-1503)
[root@linux-node1 ~]# yum update trousers Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: mirrors.zju.edu.cn * epel: mirror.premi.st * extras: mirrors.aliyun.com * updates: mirrors.aliyun.com Resolving Dependencies --> Running transaction check ---> Package trousers.x86_64 0:0.3.11.2-3.el7 will be updated ---> Package trousers.x86_64 0:0.3.13-1.el7 will be an update
3、經過瀏覽器配置zabbix-server
瀏覽器裏輸入下面內容,經過頁面安裝配置
http://192.168.58.11/zabbix
一切OK
密碼也是zabbix
name這裏能夠自定義,它出出如今登陸後的右上角
其實以上步驟就是配置了下面文件
點擊Finish出現登陸界面,用戶名和密碼默認是Admin/zabbix
啓動zabbix-agent,它默認監聽10050端口
zabbix-server默認監聽10051端口
[root@linux-node1 ~]# systemctl start zabbix-agent [root@linux-node1 ~]# netstat -lntp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1/systemd tcp 0 0 192.168.122.1:53 0.0.0.0:* LISTEN 1104/dnsmasq tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 922/sshd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1044/master tcp 0 0 0.0.0.0:10050 0.0.0.0:* LISTEN 1793/zabbix_agentd tcp 0 0 0.0.0.0:10051 0.0.0.0:* LISTEN 1316/zabbix_server tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 1584/mysqld tcp6 0 0 :::111 :::* LISTEN 1/systemd tcp6 0 0 :::80 :::* LISTEN 1708/httpd tcp6 0 0 :::22 :::* LISTEN 922/sshd tcp6 0 0 ::1:25 :::* LISTEN 1044/master tcp6 0 0 :::10050 :::* LISTEN 1793/zabbix_agentd tcp6 0 0 :::10051 :::* LISTEN 1316/zabbix_server [root@linux-node1 ~]#
zabbix頁面登陸和介紹
登陸成功的界面
啓用此監控
它是php-gd繪製的
練習建立3個組:web,memcached,lb
zabbix也支持snmp,這臺機器開啓了snmp,這裏咱們就用snmp來監控
snmp這裏因爲是本機監控本機能夠寫127.0.0.1
其實推薦寫成真實IP
模板這裏添加snmp的模板
宏就是下面這個值
[root@linux-node1 ~]# cat /etc/snmp/snmpd.conf rocommunity nmap 192.168.58.11 [root@linux-node1 ~]#
輸入以下內容
添加完畢,看到有不少模板顯示,其實括號裏的都是模板的連接
若是snmp沒啓動,須要先啓動它
[root@linux-node1 ~]# systemctl enable snmpd Created symlink from /etc/systemd/system/multi-user.target.wants/snmpd.service to /usr/lib/systemd/system/snmpd.service. [root@linux-node1 ~]# systemctl start snmpd [root@linux-node1 ~]# netstat -luntp | grep 161 udp 0 0 0.0.0.0:161 0.0.0.0:* 2549/snmpd [root@linux-node1 ~]#
頁面裏查看,snmp監控也成功了
screen是幾個圖形放一塊兒;2.4須要在configure下面建立;在3.0能夠在monitor下建立
[root@linux-node1 ~]# cd /usr/local/nginx/conf/ [root@linux-node1 conf]# vim nginx.conf [root@linux-node1 conf]# /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx-1.10.3/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx-1.10.3/conf/nginx.conf test is successful [root@linux-node1 conf]# /usr/local/nginx/sbin/nginx [root@linux-node1 conf]# lsof -i:8080 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME nginx 2714 root 6u IPv4 35149 0t0 TCP *:webcache (LISTEN) nginx 2715 www 6u IPv4 35149 0t0 TCP *:webcache (LISTEN) [root@linux-node1 conf]#
先用命令方式獲取活動鏈接數
[root@linux-node1 conf]# curl http://192.168.58.11:8080/nginx-status Active connections: 1 server accepts handled requests 4 4 4 Reading: 0 Writing: 1 Waiting: 0 [root@linux-node1 conf]# curl http://192.168.58.11:8080/nginx-status | grep Active % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 97 100 97 0 0 54525 0 --:--:-- --:--:-- --:--:-- 97000 Active connections: 1 [root@linux-node1 conf]# curl -s http://192.168.58.11:8080/nginx-status | grep Active Active connections: 1 [root@linux-node1 conf]# curl -s http://192.168.58.11:8080/nginx-status | grep Active |awk '{print $3}' 1 [root@linux-node1 conf]# curl -s http://192.168.58.11:8080/nginx-status | grep Active |awk '{print $NF}' 1 [root@linux-node1 conf]#
vim /etc/zabbix/zabbix_agentd.conf
默認zabbix-agent的配置文件會加載下面目錄,所以能夠單獨新建一個文件,用於存放自定義的key
寫一個名叫nginx.conf的配置文件
[root@linux-node1 zabbix_agentd.d]# pwd /etc/zabbix/zabbix_agentd.d [root@linux-node1 zabbix_agentd.d]# cat nginx.conf UserParameter=nginx.active,curl -s http://192.168.58.11:8080/nginx-status | grep Active |awk '{print $NF}' [root@linux-node1 zabbix_agentd.d]#
優化一下,腳本里的命令使用絕對路徑
[root@linux-node1 zabbix_agentd.d]# which curl /usr/bin/curl [root@linux-node1 zabbix_agentd.d]# vim nginx.conf [root@linux-node1 zabbix_agentd.d]# cat nginx.conf UserParameter=nginx.active,/usr/bin/curl -s http://192.168.58.11:8080/nginx-status | grep Active |awk '{print $NF}' [root@linux-node1 zabbix_agentd.d]#
重啓agent,安裝zabbix-get工具,它是個測試工具,只有它能獲取到了自定義的key的值,才能加入到zabbix-server裏。
[root@linux-node1 ~]# systemctl restart zabbix-agent [root@linux-node1 ~]# yum install -y zabbix-get
get一下,發現有問題
[root@linux-node1 ~]# zabbix_get -s 192.168.58.11 -p 10050 -k"nginx.active" zabbix_get [3070]: Check access restrictions in Zabbix agent configuration [root@linux-node1 ~]#
由於安裝zabbix-agent的時候,Server那裏寫的是127.0.0.1,agent和server在一臺
Server那裏要寫192.168.58.11
zabbix-get或者這裏寫127.0.0.1
下面改爲192.168.58.11,這裏是只容許哪一個server的IP訪問本身,server這裏也支持多Ip
改完重啓下agent
[root@linux-node1 ~]# vim /etc/zabbix/zabbix_agentd.conf [root@linux-node1 ~]# systemctl restart zabbix-agent [root@linux-node1 ~]#
再次獲取,成功
[root@linux-node1 ~]# zabbix_get -s 192.168.58.11 -p 10050 -k"nginx.active" 1 [root@linux-node1 ~]#
網頁上這裏儘可能也改掉
改爲以下,不然會報鏈接故障
總結添加自定義監控項的步驟
一、命令行或腳本先獲取成功
二、配置文件裏添加用戶自定義參數
三、重啓zabbix-agent
四、server端使用zabbix_get測試獲取
五、web界面建立item
這裏還缺最後一步,web頁面添加自定義監控項
application這裏寫個自定義的,它是一組item的集合
建立圖形
找到nginx.active
查看圖形
手動製造點鏈接數數據
[root@linux-node1 ~]# ab -c 10 -n 100000 http://192.168.58.11:8080/ This is ApacheBench, Version 2.3 <$Revision: 1430300 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking 192.168.58.11 (be patient) Completed 10000 requests Completed 20000 requests Completed 30000 requests Completed 40000 requests Completed 50000 requests Completed 60000 requests Completed 70000 requests Completed 80000 requests Completed 90000 requests Completed 100000 requests Finished 100000 requests Server Software: nginx/1.10.3 Server Hostname: 192.168.58.11 Server Port: 8080 Document Path: / Document Length: 612 bytes Concurrency Level: 10 Time taken for tests: 8.674 seconds Complete requests: 100000 Failed requests: 0 Write errors: 0 Total transferred: 84500000 bytes HTML transferred: 61200000 bytes Requests per second: 11529.06 [#/sec] (mean) Time per request: 0.867 [ms] (mean) Time per request: 0.087 [ms] (mean, across all concurrent requests) Transfer rate: 9513.73 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 0 0 0.2 0 3 Processing: 0 1 0.2 1 8 Waiting: 0 1 0.2 1 8 Total: 0 1 0.2 1 8 Percentage of the requests served within a certain time (ms) 50% 1 66% 1 75% 1 80% 1 90% 1 95% 1 98% 1 99% 2 100% 8 (longest request) [root@linux-node1 ~]#
查看圖形數據
以堆疊方式顯示
堆疊方式就是這種
建立自定義的screen
把nginx.active這個圖形加上去
把相關聯的加到一個screen裏面
其實能夠顯示不少,好比文本
zabbix 還能夠自定義map