Chinaunix首页 | 论坛 | 博客
  • 博客访问: 140614
  • 博文数量: 31
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 309
  • 用 户 组: 普通用户
  • 注册时间: 2014-06-06 11:27
个人简介

开启暴走模式。

文章分类

全部博文(31)

文章存档

2017年(19)

2016年(1)

2015年(11)

我的朋友

分类: LINUX

2017-02-28 17:56:05


点击(此处)折叠或打开

  1. #!/bin/bash
  2. # -------------------------------------------------------------------------------
  3. # Filename: 300_http_log_domain.sh
  4. # Revision: v0.1
  5. # Date: 2016/12/22
  6. # Author: Dily
  7. # Email: 312797697@qq.com
  8. # Metric value definition:
  9. #     0: normal
  10. #     1: Trigger the alarm
  11. # -------------------------------------------------------------------------------
  12. # Revision v0.1
  13. # 实现每5分钟统计日志抓取502和404超过总访问的10%报警
  14. # Revision v0.2
  15. # 增加url过滤小写字符功能
  16. # -------------------------------------------------------------------------------

  17. function http_status_count() {
  18.     # Time:
  19.     local TODAY=$(date +%Y%m%d)
  20.     local HOUR=$(date +'%Y%m%d%H')
  21.     local SECOND=$(date +'%Y:%H:%M:%S')
  22.     local FIVE_SECOND_AGO=$(date -d "5 minute ago" +'%Y:%H:%M:%S')
  23.    
  24.     # log:
  25.     local LOG_PATH=/cache/logs/heka-data
  26.     local LOG=$LOG_PATH/$TODAY/${HOUR}.log
  27.     
  28.     http_status_codes=(`cat ${LOG} | grep 'domain' | \
  29.         awk -F'[/ "]' '$6>="'"$FIVE_SECOND_AGO"'" && $6<="'"$SECOND"'"' | \
  30.      awk '{sub ("*/","",$7);print $7,$9}' | \
  31.         grep -vP '^[a-z].+' | \
  32.         awk '{
  33.                 if($2==404)
  34.                     {i++}
  35.                 else if($2==502)
  36.                     {j++}
  37.                 else if($2 >=100 && $2 <=600)
  38.                     {k++}
  39.         }END{
  40.         # 判断 i存在输出i,否则输出0
  41.          print i?i:0,j?j:0,k?k:0
  42.         }'
  43.         `)

  44.     echo ${http_status_codes[@]}
  45. }

  46. function alarm_502 {
  47.     local HTTP_CODE=(`http_status_count`)
  48.     local HTTP_CODE_502=${HTTP_CODE[1]}
  49.     local HTTP_CODE_ALL=${HTTP_CODE[2]}

  50.     # > 0.1 means more than 10%. +1 for prevent divide by zero.
  51.     local PERCENT_502_FALOT=$(echo "scale=2; $HTTP_CODE_502 / ($HTTP_CODE_ALL + 1)*100"|bc)
  52.     local PERCENT_502=`echo $PERCENT_502_FALOT | awk '{printf("%d\n",$1)}'`

  53.     if [ "${PERCENT_502}" -gt "10" ] ; then
  54.         echo Error
  55.         return 1
  56.     else
  57.         echo OK
  58.         return 0
  59.     fi
  60. }

  61. function alarm_404 {
  62.     local HTTP_CODE=(`http_status_count`)
  63.     local HTTP_CODE_404=${HTTP_CODE[0]}
  64.     local HTTP_CODE_ALL=${HTTP_CODE[2]}

  65.     # > 0.1 means more than 10%. +1 for prevent divide by zero.
  66.     local PERCENT_404_FALOT=$(echo "scale=2; $HTTP_CODE_404 / ($HTTP_CODE_ALL + 1)*100"|bc)
  67.     local PERCENT_404=`echo $PERCENT_404_FALOT | awk '{printf("%d\n",$1)}'`

  68.     if [ "${PERCENT_404}" -gt "10" ] ; then
  69.         echo Error
  70.         return 1
  71.     else
  72.         echo OK
  73.         return 0
  74.     fi
  75. }

  76. # Call function
  77. MSG_502=$(alarm_502)
  78. RETVAL_502=$?
  79. MSG_404=$(alarm_404)
  80. RETVAL_404=$?
  81. DATE=`date +%s`
  82. HOST=$HOSTNAME
  83. TAG=""

  84. # Send JSON message
  85. cat << EOF
  86. [
  87.   {
  88.   "endpoint" : "$HOST",
  89.   "tags" : "$TAG",
  90.   "timestamp" : $DATE,
  91.   "metric" : "http.log.domain.502",
  92.   "value" : $RETVAL_502,
  93.   "counterType": "GAUGE",
  94.   "step" : 300},
  95.   {
  96.   "endpoint" : "$HOST",
  97.   "tags" : "$TAG",
  98.   "timestamp" : $DATE,
  99.   "metric" : "http.log.domain.404",
  100.   "value" : $RETVAL_404,
  101.   "counterType": "GAUGE",
  102.   "step" : 300
  103.   }
  104. ]
  105. EOF

阅读(1761) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~