需求 目的:实时监控web访问状态,准确定位线上故障。
已有方案:
1.ELK stack : 采集数据到es中,es对每条日志索引存储。通过kibana多维度展示web
状态。但是若web站点日志比较多。随之监控系统本身存储也将增大。
2.一些日志处理命令 : 可以直接查看当前访问情况,比较方便。但是无数据存储。无法查看
历史状态。
方案 自己解析log实时解析。像ops监控server一样统一接口单位时间指标(访问量,错误数,
后台处理时间,响应时间)。然后实时统一push到监控系统的存储。通过dashboard查看。
type EMeta struct {
Recv int // 接受字节大小
Sent int // 发送字节大小
Inter string // api
Time string // 用于区分分钟时间
Status int // 状态码
ReqTime float64 // 响应时间
UpReqTime float64 // 处理时间
}
实现 1. nignx日志实时更新。需要一个tail功能
2. 解析日志。 接口名称,响应时间,响应码等等。
3. 单位时间内的统计,并求平均
4. 上报监控系统
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
func (*Alog) Gather() error { // 处理入口
go TailLog() // github.com/hpcloud/tail
/**
for {
t, _ := tail.TailFile(LogPathOB, tail.Config{Follow: true, Location: &tail.SeekInfo{Offset: 0, Whence: 2}}) // 在一个for,防止日志切割
for line := range t.Lines {
LineCh <- line.Text // 通过一个有缓冲的ch传输到日志解析协程
}
fmt.Println("lf change!!!")
time.Sleep(10 * time.Second)
}
**/
go LogHandler()
/**
for {
line := <-LineCh
entity, _ := p.ParseString(line) // 解析日志到entity结构
EntityCh <- entity // 进入HandlerEveryMin
}
**/
go HandlerEveryMin()
/**
分钟内数据进行汇总统计
通过channel传入 SummaryGRT
**/
go SummaryGRT()
/**
sort() // 根据访问量取出topN
push() // 上报监控存储机
**/
select {}
}