zabbix監控nginx

2020-08-12 17:44:27

環境:

主機名 IP 服務
Sunako 192.168.32.129 lnmp,zabbix

設定lnmp環境詳見文章 lnmp架構

1. 安裝zabbix

#安裝依賴包
[root@Sunako ~]# yum -y install wget vim gcc gcc-c++ pcre-devel

#下載並解壓
[root@Sunako ~]# wget https://cdn.zabbix.com/zabbix/sources/stable/5.0/zabbix-5.0.2.tar.gz
[root@Sunako ~]# tar xf zabbix-5.0.2.tar.gz

#建立zabbix使用者和組
[root@Sunako ~]# useradd -r -M -s /sbin/nologin zabbix

#設定zabbix數據庫
[root@Sunako ~]# mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 589
Server version: 5.7.24 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> create database zabbix character set utf8 collate utf8_bin;
Query OK, 1 row affected (0.05 sec)

mysql> grant all privileges on zabbix.* to zabbix@localhost identified by 'zabbix123';
Query OK, 0 rows affected, 2 warnings (0.13 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.10 sec)

mysql> quit
Bye

[root@Sunako ~]# cd zabbix-5.0.2/database/mysql/
[root@Sunako mysql]# mysql -uzabbix -pzabbix123 zabbix < schema.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@Sunako mysql]# mysql -uzabbix -pzabbix123 zabbix < images.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@Sunako mysql]# mysql -uzabbix -pzabbix123 zabbix < data.sql
mysql: [Warning] Using a password on the command line interface can be insecure.

#編譯安裝
[root@Sunako zabbix-5.0.2]# ./configure --enable-server \
> --enable-agent \
> --with-mysql \
> --with-net-snmp \
> --with-libcurl \
> --with-libxml2
[root@Sunako zabbix-5.0.2]# make install

#組態檔
[root@Sunako zabbix-5.0.2]# cd /usr/local/etc/
[root@Sunako etc]# vim zabbix_server.conf
DBPassword=zabbix123    //設定zabbix數據庫連線密碼

#啓動server和agentd
[root@Sunako ~]# zabbix_server
[root@Sunako ~]# zabbix_agentd 
[root@Sunako ~]# ss -antl
State       Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN      0      128              *:10050                        *:*                  
LISTEN      0      128              *:10051                        *:*                  
LISTEN      0      128              *:9000                         *:*                  
LISTEN      0      128              *:80                           *:*                  
LISTEN      0      128              *:22                           *:*                  
LISTEN      0      100      127.0.0.1:25                           *:*                  
LISTEN      0      80              :::3306                        :::*                  
LISTEN      0      128             :::22                          :::*                  
LISTEN      0      100            ::1:25                          :::* 
#修改/etc/php.ini設定
[root@Sunako ~]# sed -ri 's/(post_max_size =).*/\1 16M/g' /etc/php.ini
[root@Sunako ~]# sed -ri 's/(max_execution_time =).*/\1 300/g' /etc/php.ini
[root@Sunako ~]# sed -ri 's/(max_input_time =).*/\1 300/g' /etc/php.ini
[root@Sunako ~]# sed -i '/;date.timezone/a date.timezone = Asia/Shanghai' /etc/php.ini

#重新啓動php-fpm
[root@Sunako ~]# service php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm  done
#建立數據存放目錄並修改屬主
[root@Sunako ~]# cd zabbix-5.0.2
[root@Sunako zabbix-5.0.2]# cp -a ui /usr/local/nginx/html/zabbix
[root@Sunako zabbix-5.0.2]# chown -R nginx.nginx /usr/local/nginx/html/zabbix/

#設定apache虛擬主機
[root@Sunako ~]# vim /usr/local/nginx/conf/nginx.conf
location / {
            root   html/zabbix; //新增zabbix
            index index.php index.html index.htm;
         } 
location ~ \.php$ {
            root           html/zabbix; //新增zabbix
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
         }
         

#設定許可權
[root@Sunako ~]# chmod 777 /usr/local/nginx/html/zabbix/conf
[root@Sunako ~]# ll -d /usr/local/nginx/html/zabbix/conf
drwxrwxrwx 3 nginx nginx 94 Jul  6 17:54 /usr/local/nginx/html/zabbix/conf

#重新啓動服務
[root@Sunako ~]# nginx -s reload
[root@Sunako ~]# ss -anlt
State       Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN      0      128              *:10050                        *:*                  
LISTEN      0      128              *:10051                        *:*                  
LISTEN      0      128              *:9000                         *:*                  
LISTEN      0      128              *:80                           *:*                  
LISTEN      0      128              *:22                           *:*                  
LISTEN      0      100      127.0.0.1:25                           *:*                  
LISTEN      0      80              :::3306                        :::*                  
LISTEN      0      128             :::22                          :::*                  
LISTEN      0      100            ::1:25                          :::* 

1.1 存取web介面

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

使用者名稱 密碼
Admin zabbix

恢復zabbix/conf目錄的許可權爲755

[root@Sunako ~]# chmod 755 /usr/local/nginx/html/zabbix/conf
[root@Sunako ~]# ll -d /usr/local/nginx/html/zabbix/conf
drwxr-xr-x 3 nginx nginx 117 Aug 11 11:46 /usr/local/nginx/html/zabbix/conf

1.2 設定nginx服務狀態頁面

[root@Sunako ~]# vim /usr/local/nginx/conf/nginx.conf
.....
        location /status {
            stub_status on;
            allow 192.168.32.0/24;
            deny all;
        }
.....
[root@Sunako ~]# nginx -s reload
[root@Sunako ~]# curl http://192.168.32.129/status
Active connections: 1 
server accepts handled requests
 95 95 110 
Reading: 0 Writing: 1 Waiting: 0 

在这里插入图片描述

1.3 設定監控指令碼

[root@Sunako ~]# mkdir /scripts
[root@Sunako ~]# vim /scripts/handled.sh
#!/bin/bash

status=$(curl -s http://192.168.32.129/status |awk 'NR==3{print $3}')
echo $status
[root@Sunako ~]# vim /scripts/Reading.sh
#!/bin/bash

status=$(curl -s http://192.168.32.129/status |awk 'NR==4{print $2}')
echo $status

[root@Sunako ~]# vim /scripts/Writing.sh
#!/bin/bash

status=$(curl -s http://192.168.32.129/status |awk 'NR==4{print $4}')
echo $status
[root@Sunako ~]# chmod +x /scripts/*
[root@Sunako ~]# vim /usr/local/etc/zabbix_agentd.conf
UnsafeUserParameters=1      //此行取消註釋,修改爲1

//新增以下內容
UserParameter=check_handled,/bin/bash /scripts/handled.sh 
UserParameter=check_Reading,/bin/bash /scripts/Reading.sh
UserParameter=check_Writing,/bin/bash /scripts/Writing.sh

#重新啓動並測試能否取出
[root@Sunako ~]# zabbix_server 
[root@Sunako ~]# zabbix_agentd 
[root@Sunako ~]# zabbix_get -s 127.0.0.1 -k check_handled
315
[root@Sunako ~]# zabbix_get -s 127.0.0.1 -k check_Reading
0
[root@Sunako ~]# zabbix_get -s 127.0.0.1 -k check_Writing
1

2. web介面設定

2.1 新增監控項

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

2.2 檢視數據

在这里插入图片描述

2.2.1 handled

在这里插入图片描述

2.2.2 Reading

在这里插入图片描述

2.2.3 Writing

在这里插入图片描述