Zabbix 监控常见服务(5)


title: Zabbix 监控常见Web服务(5)
date: 2018-12-11 10:35:27
tags:

  • Zabbix
    categories: Zabbix
    copyright: true
    ---

Zabbix是一个非常强大的监控系统,是企业级的软件,来监控IT基础设施的可用性和性能.它是一个能够快速搭建起来的开源的监控系统,Zabbix能监视各种网络参数,保证服务器系统的安全运营,并提供灵活的通知机制以让系统管理员快速定位解决存在的各种问题,Zabbix系统几乎可用于任何系统的监控过程,它可以运行在Linux,Solaris,HP-UX,AIX,Free BSD,Open BSD,OS X,等平台上.



监控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 ->添加 -> 更新


猜你喜欢

转载自www.cnblogs.com/LyShark/p/10884643.html