设为首页 收藏本站
查看: 1701|回复: 0

[经验分享] zabbix密钥监控-mysql-nginx-php

[复制链接]

尚未签到

发表于 2017-10-20 10:34:29 | 显示全部楼层 |阅读模式
密钥监控-mysql-nginx-php
一、zabbix监控mysql
在客户机上配置:
[iyunv@iZ25j76vlxnZ ~]# vim /usr/local/zabbix/etc/chk_mysql.sh
#!/bin/bash
# -------------------------------------------------------------------------------
# FileName:    check_mysql.sh
# Revision:    1.0
# Date:        2015/06/09
# Author:      DengYun
# Email:       dengyun@ttlsa.com
# Website:     www.ttlsa.com
# Description:
# Notes:       ~
# -------------------------------------------------------------------------------
# Copyright:   2015 (c) DengYun
# License:     
# 用户名
MYSQL_USER='root'
# 密码
MYSQL_PWD='YyIrpFNhIofsd'
# 主机地址/IP
MYSQL_HOST='127.0.0.1'
# 端口
MYSQL_PORT='3306'
# 数据连接
MYSQL_CONN="/usr/local/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
[iyunv@iZ25j76vlxnZ ~]# vim /usr/local/zabbix/etc/zabbix_agentd.conf
在后面添加:
# 获取mysql版本
UserParameter=mysql.version,mysql -V
#  获取mysql性能指标,这个是上面定义好的脚本
UserParameter=mysql.status
  • ,/usr/local/zabbix/etc/chk_mysql.sh $1
    #  获取mysql运行状态
    UserParameter=mysql.ping,mysqladmin -uroot -pYyIrpFNhIofsd -P3306 -h127.0.0.1  ping | grep -c alive
    服务端取值:
    [iyunv@iZ25t2tlsnqZ ~]# zabbix_get -s 10.44.37.221 -p10050 -k mysql.status[Com_update]
    297

    二、监控php-fpm
    [iyunv@iZ25j76vlxnZ ~]# vim /usr/local/php/etc/php-fpm.conf
    pm.status_path = /status    //把;好去掉,启用监控fpm状态
    [iyunv@iZ25j76vlxnZ ~]# vim /usr/local/nginx/conf/nginx.conf
    location ~ ^/(status|ping)$
             {
            fastcgi_pass   127.0.0.1:9000;
            include fastcgi_params;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
            allow 127.0.0.1;
            allow 10.170.236.125;
            deny all;
            }
    [iyunv@iZ25j76vlxnZ ~]# service nginx restart
    [iyunv@iZ25j76vlxnZ etc]# curl -s http://localhost/status  //测试是否可以看到php-fpm的状态,有信息即可
    创建监控脚本:
    [iyunv@iZ25j76vlxnZ ~]# vim /usr/local/zabbix/etc/chk_php-fpm.sh
    #!/bin/bash
    #monitor php-fpm status from zabbix
    #lincense:GPL
    #mail:admin@huxianglin.cn
    #date:2015.04.15
    source /etc/bashrc >/dev/null 2>&1
    source /etc/profile >/dev/null 2>&1
    LOG=/usr/local/zabbix/etc/phpfpm_status.log
    curl -s http://localhost/status >$LOG
    pool(){
    awk '/pool/ {print $NF}' $LOG
    }
    process_manager(){
    awk '/process manager/ {print $NF}' $LOG
    }
    start_since(){
    awk '/start since:/ {print $NF}' $LOG
    }
    accepted_conn(){
            awk '/accepted conn:/ {print $NF}' $LOG
    }
    listen_queue(){
            awk '/^(listen queue:)/ {print $NF}' $LOG
    }
    max_listen_queue(){
            awk '/max listen queue:/ {print $NF}' $LOG
    }
    listen_queue_len(){
            awk '/listen queue len:/ {print $NF}' $LOG
    }
    idle_processes(){
            awk '/idle processes:/ {print $NF}' $LOG
    }
    active_processes(){
            awk '/^(active processes:)/ {print $NF}' $LOG
    }
    total_processes(){
            awk '/total processes:/ {print $NF}' $LOG
    }
    max_active_processes(){
            awk '/max active processes:/ {print $NF}' $LOG
    }
    max_children_reached(){
            awk '/max children reached:/ {print $NF}' $LOG
    }
    slow_requests(){
            awk '/slow requests:/ {print $NF}' $LOG
    }
    case "$1" in
    pool)
    pool
    ;;
    process_manager)
    process_manager
    ;;
    start_since)
    start_since
    ;;
    accepted_conn)
    accepted_conn
    ;;
    listen_queue)
    listen_queue
    ;;
    max_listen_queue)
    max_listen_queue
    ;;
    listen_queue_len)
    listen_queue_len
    ;;
    idle_processes)
    idle_processes
    ;;
    active_processes)
    active_processes
    ;;
    total_processes)
    total_processes
    ;;
    max_active_processes)
    max_active_processes
    ;;
    max_children_reached)
    max_children_reached
    ;;
    slow_requests)
    slow_requests
    ;;
    *)
    echo "Usage: $0 {pool|process_manager|start_since|accepted_conn|listen_queue|max_listen_queue|listen_queue_len|idle_processes|active_processes|total_processes|max_active_processes|max_children_reached|slow_requests}"
    esac
    引用脚本文件
    [iyunv@iZ25j76vlxnZ ~]# vim /usr/local/zabbix/etc/zabbix_agentd.conf
    #UserParameter=php-fpm
  • ,/usr/local/zabbix/etc/chk_php-fpm.sh "$1"
    UserParameter=phpfpm.status.pool,/usr/local/zabbix/etc/chk_php-fpm.sh pool
    UserParameter=phpfpm.status.process.manager,/usr/local/zabbix/etc/chk_php-fpm.sh process_manager
    UserParameter=phpfpm.status.start.since,/usr/local/zabbix/etc/chk_php-fpm.sh start_since
    UserParameter=phpfpm.status.accepted.conn,/usr/local/zabbix/etc/chk_php-fpm.sh accepted_conn
    UserParameter=phpfpm.status.listen.queue,/usr/local/zabbix/etc/chk_php-fpm.sh listen_queue
    UserParameter=phpfpm.status.max.listen.queue,/usr/local/zabbix/etc/chk_php-fpm.sh max_listen_queue
    UserParameter=phpfpm.status.listen.queue.len,/usr/local/zabbix/etc/chk_php-fpm.sh listen_queue_len
    UserParameter=phpfpm.status.idle.processes,/usr/local/zabbix/etc/chk_php-fpm.sh idle_processes
    UserParameter=phpfpm.status.active.processes,/usr/local/zabbix/etc/chk_php-fpm.sh active_processes
    UserParameter=phpfpm.status.total.processes,/usr/local/zabbix/etc/chk_php-fpm.sh total_processes
    UserParameter=phpfpm.status.max.active.processes,/usr/local/zabbix/etc/chk_php-fpm.sh max_active_processes
    UserParameter=phpfpm.status.max.children.reached,/usr/local/zabbix/etc/chk_php-fpm.sh max_children_reached
    UserParameter=phpfpm.status.slow.requests,/usr/local/zabbix/etc/chk_php-fpm.sh slow_requests
    [iyunv@iZ25j76vlxnZ ~]# /etc/init.d/zabbix_agentd restart
    [iyunv@iZ25wrxqp21Z etc]# curl http://127.0.0.1/status
    pool:                 www
    process manager:      dynamic
    start time:           27/Apr/2016:14:21:33 +0800
    start since:          85601
    accepted conn:        37078
    listen queue:         0
    max listen queue:     13
    listen queue len:     128
    idle processes:       2
    active processes:     1
    total processes:      3
    max active processes: 5
    max children reached: 4
    slow requests:        0
    下面介绍每个参数的作用:
    pool:php-fpm池的名称,一般都是应该是www
    process manage:进程的管理方法,php-fpm支持三种管理方法,分别是static,dynamic和ondemand,一般情况下都是dynamic
    start time:php-fpm启动时候的时间,不管是restart或者reload都会更新这里的时间
    start since:php-fpm自启动起来经过的时间,默认为秒
    accepted conn:当前接收的连接数
    listen queue:在队列中等待连接的请求个数,如果这个数字为非0,那么最好增加进程的fpm个数
    max listen queue:从fpm启动以来,在队列中等待连接请求的最大值
    listen queue len:等待连接的套接字队列大小
    idle processes:空闲的进程个数
    active processes:活动的进程个数
    total processes:总共的进程个数
    max active processes:从fpm启动以来,活动进程的最大个数,如果这个值小于当前的max_children,可以调小此值
    max children reached:当pm尝试启动更多的进程,却因为max_children的限制,没有启动更多进程的次数。如果这个值非0,那么可以适当增加fpm的进程数
    slow requests:慢请求的次数,一般如果这个值未非0,那么可能会有慢的php进程,一般一个不好的mysql查询是最大的祸首。

    服务器验证:
    [iyunv@iZ25t2tlsnqZ ~]# zabbix_get -s 10.44.37.221 -p 10050 -k phpfpm.status.idle.processes
    127        //获取到数据说明配置没问题了,接下来在web页面上添加模板


    三、监控nginx
    [iyunv@iZ25j76vlxnZ ~]# vim /usr/local/nginx/conf/nginx.conf
    在php-pfm下增加:
       location /nginx-status {
            stub_status on;
            access_log on;
            allow 127.0.0.1;
            allow 10.170.236.125;
            deny all;
            }
    [iyunv@iZ25j76vlxnZ ~]# vim /usr/local/zabbix/etc/chk_nginx.sh
    #!/bin/bash
    # Script to fetch nginx statuses for tribily monitoring systems
    # Author: krish@toonheart.com
    # License: GPLv2
    # Set Variables
    BKUP_DATE=`/bin/date +%Y%m%d`
    LOG="/data/log/zabbix/webstatus.log"
    HOST=127.0.0.1
    PORT="80"
    # Functions to return nginx stats
    function active {
    /usr/bin/curl "http://$HOST:$PORT/nginx-status" 2>/dev/null| grep 'Active' | awk '{print $NF}'
    }
    function reading {
    /usr/bin/curl "http://$HOST:$PORT/nginx-status" 2>/dev/null| grep 'Reading' | awk '{print $2}'
    }
    function writing {
    /usr/bin/curl "http://$HOST:$PORT/nginx-status" 2>/dev/null| grep 'Writing' | awk '{print $4}'
    }
    function waiting {
    /usr/bin/curl "http://$HOST:$PORT/nginx-status" 2>/dev/null| grep 'Waiting' | awk '{print $6}'
    }
    function accepts {
    /usr/bin/curl "http://$HOST:$PORT/nginx-status" 2>/dev/null| awk NR==3 | awk '{print $1}'
    }
    function handled {
    /usr/bin/curl "http://$HOST:$PORT/nginx-status" 2>/dev/null| awk NR==3 | awk '{print $2}'
    }
    function requests {
    /usr/bin/curl "http://$HOST:$PORT/nginx-status" 2>/dev/null| awk NR==3 | awk '{print $3}'
    }
    # Run the requested function
    $1
    在被监控端配置:
    [liuguangshun@iZ25j76vlxnZ ~]$ vim /usr/local/zabbix/etc/zabbix_agentd.conf
    在最后添加:
    UserParameter=nginx.accepts,/usr/local/zabbix/etc/chk_nginx.sh accepts
    UserParameter=nginx.handled,/usr/local/zabbix/etc/chk_nginx.sh handled
    UserParameter=nginx.requests,/usr/local/zabbix/etc/chk_nginx.sh requests
    UserParameter=nginx.connections.active,/usr/local/zabbix/etc/chk_nginx.sh active
    UserParameter=nginx.connections.reading,/usr/local/zabbix/etc/chk_nginx.sh reading
    UserParameter=nginx.connections.writing,/usr/local/zabbix/etc/chk_nginx.sh writing
    UserParameter=nginx.connections.waiting,/usr/local/zabbix/etc/chk_nginx.sh waiting
    在被监控查看nginx状态:
    [iyunv@iZ25wrxqp21Z etc]# curl http://127.0.0.1/nginx-status
    Active connections: 1
    server accepts handled requests
    67561 67561 159860
    Reading: 0 Writing: 1 Waiting: 0
    Activeconnections:对后端发起的活动连接数;
    server accepts 67561:nginx 总共处理了67561个连接;
    handled:成功创建了67561次握手;
    requests:总共处理了159860请求。
    Reading:nginx读取客户端的header数;
    Writing: nginx 返回给客户端的header数;
    Waiting: nginx 请求处理完成,正在等待下一请求指令的连接。

    服务器上测试
    [iyunv@iZ25t2tlsnqZ ~]# zabbix_get -s 10.44.37.221 -p 10050 -k "nginx.accepts"
    3151
    注意:在web添加模板时,键值(密钥,web是通过键值来链接监控服务器的)要和监控端UserParameter后面的值一样,例如nginx.accepts




  • 运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
    2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
    3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
    4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
    5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
    6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
    7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
    8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

    所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-405654-1-1.html 上篇帖子: zabbix通过jmx监控tomcat 下篇帖子: zabbix 3.4新功能值解析——Preprocessing预处理
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    扫码加入运维网微信交流群X

    扫码加入运维网微信交流群

    扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

    扫描微信二维码查看详情

    客服E-mail:kefu@iyunv.com 客服QQ:1061981298


    QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


    提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


    本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



    合作伙伴: 青云cloud

    快速回复 返回顶部 返回列表