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

[经验分享] zabbix监控nginx,Mysqld,Php状态,MySQL主从复制状态

[复制链接]

尚未签到

发表于 2019-1-25 13:19:42 | 显示全部楼层 |阅读模式
  http://xianglinhu.blog.运维网.com/5787032/1633016
  https://www.centos.bz/2015/01/zabbix-monitor-memcached-php-fpm-tomcat-mysql-nginx-log/
  http://john88wang.blog.运维网.com/2165294/1577269
  zabbix监控nginx状态
  Nginx自带监控模块ngx_http_stub_status_module提供Nginx的基本信息
  在编译安装Nginx时加参数 --with-http_stub_status_module
  安装好以后可以通过nginx -V|grep http_stub_status_module 查看状态模块是否已安装
  首先,在nginx的配置文件中添加如下一段代码
server  
{
  listen 88;
  server_name localhost;
  
location /nginxstatus {
  stub_status on;
  access_log off;
  allow 127.0.0.1;
  allow 192.168.1.165;
  deny all;
  }
  
location /php-fpm_status {
  
        fastcgi_pass 127.0.0.1:9000;
  
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  
        include fastcgi_params;
  
                                     }
  
         }
  
}
  保存之后重启nginx
$ curl  localhost:88/nginxstatusActive connections: 1 server accepts handled requests 788163 788163 788163 Reading: 0 Writing: 1 Waiting: 0  Active connections
  The current number of active client connections including Waiting connections.
  活跃客户端连接数,包括处于等待状态的连接数
  accepts
  The total number of accepted client connections.
  接收到的客户端连接总数
  handled
  The total number of handled connections. Generally, the parameter value is the same as accepts unless some resource limits have been reached
  处理请求的总数。通常情况下,这个值和accepts的值相同。除非达到了一些资源限制。例如设置worker_connections 1024;  设置一个worker进程能够打开的最大并发连接数。
  requests
  The total number of client requests.
  客户端请求总数
  Reading
  The current number of connections where nginx is reading the request header.
  当前Nginx正在读取请求头的连接数量
  Writing
  The current number of connections where nginx is writing the response back to the client.
  当前Nginx正在将响应写回到客户端的连接数量
  Waiting

  The current number of>  当前正在等待请求的闲置客户端连接数量
  在zabbix脚本目录下编写Nginx状态信息获取脚本
  nginx_status.sh
#!/bin/bash  
#check nginx status
  
#ip=$(ifconfig eth0|grep "inet addr"|sed  's/^.*addr://'|awk '{print $1}')
  
#echo $ip
  
ip=127.0.0.1
  
port=88
  
#echo $ip:$port
  
function active()  {
  
    /usr/bin/curl http://$ip:$port/nginx_status 2>/dev/null|grep "Active"|awk '{print $NF}'
  
                   }
  

  
function reading() {
  
    /usr/bin/curl http://$ip:$port/nginx_status 2>/dev/null|grep "Reading"|awk '{print $2}'
  
                   }
  

  
function writing() {
  
    /usr/bin/curl http://$ip:$port/nginx_status 2>/dev/null|grep "Writing"|awk '{print $4}'
  
                   }
  

  
function waiting() {
  

  
    /usr/bin/curl http://$ip:$port/nginx_status 2>/dev/null|grep "Waiting"|awk '{print $6}'
  
                   }
  

  
function accepts() {
  
    /usr/bin/curl http://$ip:$port/nginx_status 2>/dev/null|awk 'NR==3{print $1}'
  

  
                   }
  

  
function handled() {
  

  
    /usr/bin/curl http://$ip:$port/nginx_status 2>/dev/null|awk 'NR==3{print $2}'
  
                   }
  

  
function requests(){
  

  
    /usr/bin/curl http://$ip:$port/nginx_status 2>/dev/null|awk 'NR==3{print $3}'
  
                   }
  

  
case $1 in
  
   active)
  
          active
  
        ;;
  
  reading)
  
         reading
  
        ;;
  
  writing)
  
          writing
  
        ;;
  
  waiting)
  
          waiting
  
        ;;
  
  accepts)
  
          accepts
  
        ;;
  
  handled)
  
          handled
  
        ;;
  
  requests)
  
          requests
  
        ;;
  
       *)
  
          exit 1
  
        ;;
  
esac
  然后修改该文件的属主和属组为zabbix,并且具有执行权限
  chown zabbix.zabbix /data/zabbix/sbin/nginx_status.sh
  chmod 755 /data/zabbix/sbin/nginx_status.sh
  然后在zabbix_agentd.conf配置文件中添加zabbix的子配置文件
  nginx_status_zabbix.conf
### Option: UserParameter  
#       User-defined parameter to monitor. There can be several user-defined parameters.
  
#       Format: UserParameter=,
  
#       See 'zabbix_agentd' directory for examples.
  
#
  
# Mandatory: no
  
# Default:
  
# UserParameter=
  
# /data/zabbix/sbin/nginx_status.sh
  
UserParameter=nginx.accepts,/usr/local/zabbix/bin/nginx_status.sh accepts
  
UserParameter=nginx.handled,/usr/local/zabbix/bin/nginx_status.sh handled
  
UserParameter=nginx.requests,/usr/local/zabbix/bin/nginx_status.sh requests
  
UserParameter=nginx.connections.active,/usr/local/zabbix/bin/nginx_status.sh active
  
UserParameter=nginx.connections.reading,/usr/local/zabbix/bin/nginx_status.sh reading
  
UserParameter=nginx.connections.writing,/usr/local/zabbix/bin/nginx_status.sh writing
  
UserParameter=nginx.connections.waiting,/usr/local/zabbix/bin/nginx_status.sh waiting
  创建好了之后就可以在web页面配置item监控项了,模板在附件。
  zabbix监控mysql数据库状态
  Zabbix自己提供的模板可以监控mysql slow queries,mysqlversion,uptime,alive等。
  1.Zabbix官方提供的监控mysql的模板Template AppMySQL,可以看到相关的Items和key。
  2.把该模板Template App MySQL Link到相关的主机上面,发现Item的Status是不可用的,因为key的值是通过Mysql用户查看"showglobal status"信息或者用mysqladmin命令查看status或extended-status的信息而取的值。
mysql> show global status;  
mysql> show status;
  3.结合官方提供的key编写Shell脚本,从数据库中取出Items的key的值。
  cat check_mysql_performance.sh
#!/bin/bash  
host=loclhost
  
MYSQL_SOCK="/var/lib/mysql/mysql.sock"
  
MYSQL_PWD="9kdbserver123"
  
ARGS=1
  
if [ $# -ne "$ARGS" ];then
  
   echo "Please input onearguement:"
  
fi
  
case $1 in
  
Uptime)
  
    result=`mysqladmin -uroot -p${MYSQL_PWD} -S $MYSQL_SOCK status|cut -f2 -d ":" |cut -f1 -d "T"`
  
    echo $result
  
    ;;
  
Com_update)
  
    result=`mysqladmin -uroot -p${MYSQL_PWD} -S $MYSQL_SOCK extended-status |grep -w "Com_update"|cut -d "|" -f3`
  
    echo $result
  
    ;;
  
Slow_queries)
  
    result=`mysqladmin -uroot -p${MYSQL_PWD} -S $MYSQL_SOCK status |cut -f5 -d ":" |cut -f1 -d "O"`
  
    echo $result
  
    ;;
  
Com_select)
  
    result=`mysqladmin -uroot -p${MYSQL_PWD} -S $MYSQL_SOCK extended-status |grep -w "Com_select"| cut -d "|" -f3`
  
    echo $result
  
    ;;
  
Com_rollback)
  
    result=`mysqladmin -uroot -p${MYSQL_PWD} -S $MYSQL_SOCK extended-status |grep -w "Com_rollback"| cut -d "|" -f3`
  
    echo $result
  
    ;;
  
Questions)
  
    result=`mysqladmin -uroot -p${MYSQL_PWD} -S $MYSQL_SOCK status|cut -f4 -d ":"|cut -f1 -d "S"`
  
    echo $result
  
    ;;
  
Com_insert)
  
    esult=`mysqladmin -uroot -p${MYSQL_PWD} -S $MYSQL_SOCK extended-status |grep -w "Com_insert"|cut -d "|" -f3`
  
    echo $result
  
    ;;
  
Com_delete)
  
    result=`mysqladmin -uroot -p${MYSQL_PWD} -S $MYSQL_SOCK extended-status |grep -w "Com_delete"|cut -d "|" -f3`
  
    echo $result
  
    ;;
  
Com_commit)
  
    result=`mysqladmin -uroot -p${MYSQL_PWD} -S $MYSQL_SOCK extended-status |grep -w "Com_commit"|cut -d "|" -f3`
  
    echo $result
  
    ;;
  
Bytes_sent)
  
    result=`mysqladmin -uroot -p${MYSQL_PWD} -S $MYSQL_SOCK extended-status |grep -w "Bytes_sent"|cut -d "|" -f3`
  
    echo $result
  
    ;;
  
Bytes_received)
  
    result=`mysqladmin -uroot -p${MYSQL_PWD} -S $MYSQL_SOCK extended-status |grep -w "Bytes_received" |cut -d "|" -f3`
  
    echo $result
  
    ;;
  
Com_begin)
  
    result=`mysqladmin -uroot -p${MYSQL_PWD} -S $MYSQL_SOCK 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
  修改权限chmod 755 /data/zabbix/sbin/check_mysql_performance.sh
  4.在Zabbix_agentd.conf里面添加UserParameter,格式如下,对于Zabbix来说,脚本其实就是一个插件。
UserParameter=mysql.version,mysql -V  
UserParameter=mysql.ping,mysqladmin -uroot -pxxxxxx -S /tmp/mysql.sock ping | grep -c alive
  
UserParameter=mysql.status
  • ,/data/zabbix/sbin/check_mysql_performance.sh $1 $2
      5.重启agentd服务器,然后在zabbix server中添加模板Template AppMySQL。
      这时候可以连接zabbix自带的Templete app mysql 模板了进行监控,这里我将这个模板改了下名称放在附件里面提供下载。要是使用我这个模板只需要导入模板即可,就可以看到Template MySQL Porformance模板。
      6.在zabbix前端可以实时查看SQL语句每秒钟的操作次数。

      7.在zabbix前端可以实时查看mysql发送接收的字节数。其中bytes received表示从所有客户端接收到的字节数,bytes sent表示发送给所有客户端的字节数。

      总结
      把该脚本放到要监控的服务器上面(Modifymysql user and password),修改UserParameter的参数并重启agentd,Link官方提供的Template App MySQL模板即可。
      我这里是测试环境用root账号,线上服务器安全期间可以给mysql用户授权readonly权限。
      根据实际的需求,除了监控上述监控项之外,还可以监控mysqlprocesslist,Innodb等。
      zabbix监控php状态
      http://www.ttlsa.com/zabbix/zabbix-monitor-php-fpm-status/
      方法一:
      通过nginx调用php-fpm来查询php的状态信息
      首先,在php的配置文件中添加一行
      vim /usr/local/php/etc/php-fpm.conf
      pm.status_path = /phpfpmstatus
      保存后重启php-fpm
      在nginx配置文件中添加代码
      server
      {
      listen  80;
      server_name localhost;
      location /nginxstatus {
      stub_status on;
      access_log off;
      allow 127.0.0.1;
      allow 10.6.0.187;
      deny all;
      }
      location ~ ^/(phpfpmstatus)$ {
      include fastcgi_params;
      fastcgi_pass unix:/tmp/php-fcgi.sock;
      fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
      }
      }
      保存后重启nginx服务
      然后使用curl -s http://localhost/phpfpmstatus查看是否能够获取到php状态信息。
      然后,在zabbix的脚本目录里面编辑执行脚本
    #!/bin/bash  
    #monitor php-fpm status from zabbix
      

      
    source /etc/bashrc >/dev/null 2>&1
      
    source /etc/profile >/dev/null 2>&1
      

      
    LOG=/data/zabbix/sbin/phpfpmstatus.log
      
    curl -s http://localhost/phpfpmstatus >$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
      
    }
      
    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
      ;;
      
    *)
      
    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}"
      
    esac
      

      

      
    保存后退出,修改权限为
      
    -rwxr-xr-x 1 zabbix zabbix 1770 4月  15 14:50 phpstatus.sh
      

      
    然后编辑zabbix_agentd.conf文件,在最后添加上下面这段代码,并重启zabbix_agentd服务
    #to monitor php-fpmstatus  
    UserParameter=phpfpm.status.pool,/data/zabbix/sbin/phpstatus.sh pool
      
    UserParameter=phpfpm.status.process.manager,/data/zabbix/sbin/phpstatus.sh process_manager
      
    UserParameter=phpfpm.status.start.since,/data/zabbix/sbin/phpstatus.sh start_since
      
    UserParameter=phpfpm.status.accepted.conn,/data/zabbix/sbin/phpstatus.sh accepted_conn
      
    UserParameter=phpfpm.status.listen.queue,/data/zabbix/sbin/phpstatus.sh listen_queue
      
    UserParameter=phpfpm.status.max.listen.queue,/data/zabbix/sbin/phpstatus.sh max_listen_queue
      
    UserParameter=phpfpm.status.listen.queue.len,/data/zabbix/sbin/phpstatus.sh listen_queue_len
      
    UserParameter=phpfpm.status.idle.processes,/data/zabbix/sbin/phpstatus.sh idle_processes
      
    UserParameter=phpfpm.status.active.processes,/data/zabbix/sbin/phpstatus.sh active_processes
      
    UserParameter=phpfpm.status.total.processes,/data/zabbix/sbin/phpstatus.sh total_processes
      
    UserParameter=phpfpm.status.max.active.processes,/data/zabbix/sbin/phpstatus.sh max_active_processes
      
    UserParameter=phpfpm.status.max.children.reached,/data/zabbix/sbin/phpstatus.sh max_children_reached
      然后就可以在web端配置item监控php状态了

      方法二:
      1. 启用php-fpm状态功能
      # cat  /usr/local/php-5.5.10/etc/php-fpm.conf | grep status_path
      pm.status_path = /status
      默认情况下为/status,当然也可以改成其他的,例如/ttlsa_status等等。
      2. nginx配置
      在默认主机里面加上location或者你希望能访问到的主机里面。
      location ~ ^/(phpfpm_status)$ {
      root           /usr/local/nginx/html;
      include        fastcgi_params;
      fastcgi_pass   127.0.0.1:9000;
      fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
      }
      3. 重启nginx/php-fpm
      请依照你的环境重启你的nginx和php-fpm
      # service nginx restart
      # service php-fpm restart
      4. 打开status页面
      # curl http://www.ttlsa.com/status
      pool:                 www
      process manager:      dynamic
      start time:           14/May/2014:22:40:15 +0800
      start since:          58508
      accepted conn:        33
      listen queue:         0
      max listen queue:     8
      listen queue len:     0
      idle processes:       2
      active processes:     1
      total processes:      3
      max active processes: 5
      max children reached: 0
      slow requests:        2091
      5. php-fpm status详解
      pool – fpm池子名称,大多数为www
      process manager – 进程管理方式,值:static, dynamic or ondemand. dynamic
      start time – 启动日期,如果reload了php-fpm,时间会更新
      start since – 运行时长
      accepted conn – 当前池子接受的请求数
      listen queue – 请求等待队列,如果这个值不为0,那么要增加FPM的进程数量
      max listen queue – 请求等待队列最高的数量
      listen queue len – socket等待队列长度
      idle processes – 空闲进程数量
      active processes – 活跃进程数量
      total processes – 总进程数量
      max active processes – 最大的活跃进程数量(FPM启动开始算)
      max children reached - 大道进程最大数量限制的次数,如果这个数量不为0,那说明你的最大进程数量太小了,请改大一点。
      slow requests – 启用了php-fpm slow-log,缓慢请求的数量
    zabbix客户端配置
      增加自定义key
      # cat zabbix_agentd.conf | grep 'php-fpm'
      UserParameter=php-fpm.status
  • ,/usr/bin/curl -s "http://127.0.0.1/status?xml" | grep "" | awk -F'>|

  • 运维网声明 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-667489-1-1.html 上篇帖子: zabbix监控环境搭建部署 下篇帖子: zabbix2.2入门教程之配置文件配置(二)
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

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

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

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

    扫描微信二维码查看详情

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


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


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


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



    合作伙伴: 青云cloud

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