good luck! ------ 博客

mtail 添加histogram的一个演示

histogram demo 演示

本文以tomcat 的access log 为例

1.安装mtail

步骤简单

wget https://github.com/google/mtail/releases/download/v3.0.0-rc35/mtail_v3.0.0-rc35_linux_amd64
mv mtail_v3.0.0-rc35_linux_amd64  mtail
chmod 0755 mtail
cp -a mtail /usr/local/bin/
 mtail --version

2.设置配置文件

mkdir -p /etc/mtail
cd /etc/mtail/
 touch /etc/mtail/line_count.mtail  ##要求必须以此格式结尾

拷贝下面的内容到新建的文件/etc/mtail/line_count.mtail

# Parser for the common apache log format as follow.
# LogFormat "%h %l %u %t \"%r\" %>s %b %D \"%{Referer}i\" \"%{User-agent}i\"
counter apache_http_requests_total by request_method, http_version, status_code
counter apache_http_bytes_total by request_method, http_version, status_code
gauge apache_http_response_time by remote_host, request_method, request_uri, status_code
gauge apache_http_response_size by remote_host, request_method, request_uri, status_code

histogram apache_http_request_time_millseconds_bucket buckets 5, 10, 25, 50, 100, 250, 500, 1000, 2500, 5000, 10000, 15000 by status_code

/^/ +
/(?P<remote_host>[0-9A-Za-z\.:-]+) / + # %h
/(?P<remote_logname>[0-9A-Za-z-]+) / + # %l
/(?P<remote_username>[0-9A-Za-z-]+) / + # %u
/\[(?P<timestamp>\d{2}\/\w{3}\/\d{4}:\d{2}:\d{2}:\d{2} (\+|-)\d{4})\] / + # %u
/"(?P<request_method>[A-Z]+) (?P<request_uri>\S+) (?P<http_version>HTTP\/[0-9\.]+)" / + # \"%r\"
/(?P<status_code>\d{3}) / + # %>s
/((?P<response_size>\d+)|-) / + # %b
/(?P<response_time>\d+) / + # %D
/"(?P<referer>\S+)" / + # \"%{Referer}i\"
/"(?P<user_agent>[[:print:]]+)"/ + # \"%{User-agent}i\"
/$/ {
  strptime($timestamp, "02/Jan/2006:15:04:05 -0700") # for tests

  apache_http_requests_total[$request_method][$http_version][$status_code]++
  $response_size > 0 {
      apache_http_bytes_total[$request_method][$http_version][$status_code] += $response_size
      apache_http_response_size[$remote_host][$request_method][$request_uri][$status_code] += $response_size
  }
  apache_http_response_time[$remote_host][$request_method][$request_uri][$status_code] = $response_time
  #apache_http_request_time_seconds_bucket[$remote_host][$request_method][$request_uri][$status_code] = $response_time / 1000
  apache_http_request_time_millseconds_bucket[$status_code] = $response_time
}

getfilename() !~ /localhost_access_log.?txt/ {
  stop
}

4.设置tomcat 日志格式

pattern="%h %l %u %t &quot;%r&quot; %s %b %D &quot;%{Referer}i&quot; &quot;%{User-Agent}i&quot;" />

采集到的日志如图
示意图

5.重启tomcat

此步不用说了吧 都会的

6.启动mtail

mtail --progs /etc/mtail --logs '/opt/testtomcat/logs/localhost_access_log.*.txt' --logtostderr -v
--logs 表示你要采集的文件
--progs 表示你的mtail配置文件路径
默认端口3903

7.访问验证

curl -s http://127.0.0.1:3903|less

需要访问一下你的tomcat 才有新的日志被采集
不然没有数据
访问效果

搜索一下apache 就能看到你想要的指标了

注意问题

mtail 需要访问才有采集动作
还有几个坑的问题参考我的博客mtail 调式吧

posted @ 2020-06-12 19:39  木直  阅读(615)  评论(0编辑  收藏  举报