Go 实现 nginx log 读取 分析 写入InfluxDB 并用Grafana 显示

Go 实现 nginx log 读取 分析 写入InfluxDB 并用Grafana 显示
参考:慕课网https://www.imooc.com/learn/982

1. 系统结构
用Go实现文件读取,并且将log 分析并写入InfluxDB,最后用通过配置Grafana 显示

log file –>log process –> influxdb –> grafana

监控需求:某个协议下的某个请求在某个请求方法的QPS和响应时间和流量

2. Go 接收
  1.Go 并发执行
        1.将复杂的任务拆分,通过goroutine去并发的执行
        2.通过channel做数据通信
2.Golang 中面向对象
        1.struct对象
        2.interface
        3.封装,继承,多态
封装:

type Foo struct {
    baz string
}
func (f * Foo) echo() {
    fmt.Println(f.baz)
}

继承:

type Foo struct {
    baz string
}
type Bar strct {
    Foo
}

Code 实现:https://github.com/itsmikej/imooc_logprocess, 通过看视频教程,应该可以能够很好的理解code, 以及理解如何一步步实现模块封装。

3. InfluxDB
是一个开源的时序性的数据库,使用GO编写,被广泛用于存储系统的监控数据,IOT行业的实时数据等场景,有以下特性:

- 部署简单,无外部依赖
- 内置http支持,使用http读写
- 类sql的灵活查询(max,min,sum等)

概念:database: 数据库;measurement: 数据库的表,points: 表里的一行数据(tags: 有索引的属性,fields:各种记录的值,time: 时间戳)

写入数据:

curl -i -XPOST 'http://localhost:8086/write?db=mydb' --data-binary 'cpu_usage,host=server01,region=us-west value=0.64 143405230000000'
1
读取数据:

curl -G 'http://localhost:8086/query?pretty=true' --data-urlencode "db=mydb" --data-urlencode "q=SELECT\"value\"FROM\"cpu_usage\"WHERE "region\"='us-west'"
1
针对本log 系统:
tags: path, method, scheme, status
Fileds: UpstreamTime, Requestime,BytesSent
Time: Timelocal

安装:

1.docker pull influxdb
2.创建DB, curl -G http://localhost:8086/query --data-urlencode "q=CREATE DATABASE imooc"
3.创建用户, curl "http://localhost:8086/query" --data-urlencode "q=CREATE USER imooc WITH PASSWORD 'imoocpass' WITH ALL PRIVILEGES"
4.启动container:
    1.docker run -itd -p 8086:8086 -v $PWD:/var/lib/influxd influxdb

4. Grafana
Grafana 可以集成多种数据源, ES, Mysql 等 可以通过创建漂亮的GUI,实时的显示数据。只需要配置下Datastore,以及DashBoad
安装

docker run -d --name=grafana -p 3000:3000 grafana/grafana
1
Add datastore: 添加 influxdb 的地址,数据库等信息。
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值