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

[经验分享] zabbix监控PHP APC缓存

[复制链接]

尚未签到

发表于 2018-12-24 10:00:05 | 显示全部楼层 |阅读模式
  参考文章
  http://john88wang.blog.运维网.com/2165294/1579158
  https://github.com/zbal/zabbix/blob/master/scripts/zabbix_php-apc_check.sh
  

  

  1.监控原理
  将APC源码包中的apc.php放到/usr/local/bin/ 目录下
  添加nginx子配置文件php-apc_status.conf
server
{
        listen      80;
        server_name 127.0.0.1;
        access_log  off;
        client_max_body_size 5m;
        autoindex off;
        location / {
            root  /usr/local/zabbix/bin/;
            index index.php;
            autoindex off;
        }
        location ~ \.php$ {
            root           /usr/local/zabbix/bin/;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root/$fastcgi_script_name;
            include        fastcgi_params;
        }
}  

  通过links这个命令可以在命令行下方为HTML页面
  links 127.0.0.1/apc.php -dump 2>/dev/null
  

  如果没有这个命令可以用过yum -y install links 安装
  

  2.编写PHP-APC状态信息获取脚本
  php-apc_status.sh
#!/bin/bash
#This script is used to get php apc cache data from  apc.php page
#you need put nginx sub-config file php-apc_status.conf under conf/conf.d/ directory
apc_metric=$1
apc_status_url="http://127.0.0.1/apc.php"
apc_status_file=/tmp/php-apc_status.txt
/usr/bin/links $apc_status_url -dump 2>/dev/null > $apc_status_file
#echo $apc_status
#we need to convert GBytes,Mbytes,KBytes to Bytes
#echo "308.345 * 1024 * 1024 * 1024"|bc|cut -f1 -d.
#331082922721
convert_size() {
  value=$1
  unit=$2
  echo $value | grep -E '^[0-9.]+$' 2>&1 > /dev/null
  if [ $? -ne 0 ]; then
    echo "$value is not number"
    exit 1
  fi
  case $unit in
    Bytes)
          echo "$value"
        ;;
   KBytes)
          echo "$value * 1024" | bc | cut -f1 -d.
        ;;
   MBytes)
          echo "$value * 1024 * 1024" | bc | cut -f1 -d.
        ;;
   GBytes)
          echo "$value * 1024 * 1024 * 1024" | bc | cut -f1 -d.
        ;;
  esac
}
#get apc metric data
case $apc_metric in
            version)
                  cat $apc_status_file|grep "APC Version"|awk '{print $3}'
                 ;;
cached_files_count)
                  cat $apc_status_file|grep "Cached Files"|head -1|awk '{print $3}'
                 ;;
  cached_files_size)
                  convert_size   $(cat $apc_status_file|grep "Cached Files"|awk '{print $4 " " $5}'|sed -e 's/(//' -e 's/)//')        
                 ;;
               hits)
                  cat $apc_status_file|grep  "Hits"|head -1|awk '{print $2}'
                 ;;
              phits)
                  cat $apc_status_file|grep "Free"|head -1|awk '{print $7}'|sed -e 's/(//' -e s'/)//' -e 's/%//'
                 ;;
             misses)
                  cat $apc_status_file|grep  "Misses"|head -1|awk '{print $2}'
                 ;;
       request_rate)
                  cat $apc_status_file|grep  "Request Rate"|head -1|awk '{print $5}'
                 ;;        
           hit_rate)
                  cat $apc_status_file|grep "Hit Rate"|head -1|awk '{print $3}'
                 ;;
          miss_rate)
                  cat $apc_status_file|grep "Miss Rate"|head -1| awk '{print $3}'
                 ;;
        insert_rate)
                  cat $apc_status_file|grep "Insert Rate"|head -1| awk '{print $3}'
                 ;;
   cache_full_count)
                  cat $apc_status_file|grep "Cache full count"|head -1|awk '{print $4}'
                 ;;
           shm_size)
                   #apc.shm_size can be 400M or 2G , convert M or G to Bytes
                   shm=$(cat $apc_status_file|grep "apc.shm_size"|head -1| awk '{print $2}')
                   if [ $(echo "$shm"|grep 'M$') ];then
                     size=$(echo $shm|sed 's/M//')
                     echo "$size * 1024 * 1024"|bc
                   elif [ $(echo "$shm"|grep 'G$') ];then
                     size=$(echo $shm|sed 's/G//')
                     echo "$size * 1024 * 1024 * 1024"|bc
                   fi
                 ;;      
           mem_free)
                  convert_size $(cat $apc_status_file|grep "Free"|head -1|awk '{print $2 " " $3}')
                 ;;
           mem_used)
                  convert_size $(cat $apc_status_file|grep "Used"|head -1|awk '{print $2 " " $3}')
                 ;;
          mem_pused)
                  cat $apc_status_file|grep "Used"|head -1|awk '{print $4}'|sed -e 's/(//' -e 's/)//' -e 's/%//'
                 ;;
      fragmentation)
                  cat $apc_status_file|grep "Fragmentation:"|head -1|awk '{print $2}'|sed -e 's/%//'
                 ;;
                  *)
                  echo "please input the right parameter"
                 ;;     
esac  3.添加zabbix 子配置文件php-apc_status_zabbix.conf
UserParameter=php-apc
  • ,/usr/local/zabbix/bin/php-apc_status.sh $1  

      4.创建zabbix的PHP-APC 模板





  • 运维网声明 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-655157-1-1.html 上篇帖子: php插入数组指定位置 下篇帖子: 20岁的php技术总监成长之路
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

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

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

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

    扫描微信二维码查看详情

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


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


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


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



    合作伙伴: 青云cloud

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