-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathserver.go
More file actions
163 lines (138 loc) · 3.12 KB
/
server.go
File metadata and controls
163 lines (138 loc) · 3.12 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package main
import (
"github.com/hpcloud/tail"
"fmt"
"sync"
"github.com/astaxie/beego/logs"
"strings"
"encoding/json"
)
type TailMgr struct {
//因为我们的agent可能是读取多个日志文件,这里通过存储为一个map
tailObjMap map[string]*TailObj
lock sync.Mutex
}
type TailObj struct {
//这里是每个读取日志文件的对象
tail *tail.Tail
secLimit *SecondLimit
offset int64 //记录当前位置
//filename string
logConf logConfig
exitChan chan bool
}
var tailMgr *TailMgr
var waitGroup sync.WaitGroup
func NewTailMgr()(*TailMgr){
tailMgr = &TailMgr{
tailObjMap:make(map[string]*TailObj,16),
}
return tailMgr
}
func (t *TailMgr) AddLogFile(conf logConfig)(err error){
t.lock.Lock()
defer t.lock.Unlock()
_,ok := t.tailObjMap[conf.LogPath]
if ok{
err = fmt.Errorf("duplicate filename:%s\n",conf.LogPath)
return
}
tail,err := tail.TailFile(conf.LogPath,tail.Config{
ReOpen:true,
Follow:true,
Location:&tail.SeekInfo{Offset:0,Whence:2},
MustExist:false,
Poll:true,
})
tailobj := &TailObj{
secLimit:NewSecondLimit(int32(conf.SendRate)),
logConf:conf,
offset:0,
tail:tail,
exitChan:make(chan bool,1),
}
t.tailObjMap[conf.LogPath] = tailobj
logs.Info("map [%s]" ,t.tailObjMap)
go tailobj.readLog()
return
}
func(t *TailMgr) reloadConfig(logConfArr []logConfig)(err error){
for _, conf := range logConfArr{
tailObj,ok := t.tailObjMap[conf.LogPath]
if !ok{
logs.Debug("conf:%v -- tailobj:%v",conf,tailObj)
err = t.AddLogFile(conf)
if err != nil {
logs.Error("add log file failed,err:%v",err)
continue
}
continue
}
tailObj.logConf = conf
t.tailObjMap[conf.LogPath] = tailObj
logs.Info(t.tailObjMap)
}
// 处理删除的日志收集配置
for key,tailObj := range t.tailObjMap {
var found = false
for _, newValue := range logConfArr {
if key == newValue.LogPath {
found = true
break
}
}
if found == false {
logs.Warn("log path:%s is remove",key)
tailObj.exitChan <- true
delete(t.tailObjMap,key)
}
}
return
}
func (t *TailMgr) Process(){
logChan := GetLogConf()
for conf := range logChan {
logs.Debug("log conf :%v",conf)
var logConfArr []logConfig
err := json.Unmarshal([]byte(conf),&logConfArr)
if err != nil {
logs.Error("unmarshal failed,err:%v conf:%v",err,conf)
continue
}
logs.Debug("unmarshal succ conf:%v",logConfArr)
err = t.reloadConfig(logConfArr)
if err != nil {
logs.Error("realod config from etcd failed err:%v",err)
continue
}
logs.Debug("reaload from etcd success,config:%v",logConfArr)
}
}
func (t *TailObj) readLog(){
//读取每行日志内容
for line := range t.tail.Lines{
if line.Err != nil {
logs.Error("read line failed,err:%v",line.Err)
continue
}
str := strings.TrimSpace(line.Text)
if len(str)==0 || str[0] == '\n'{
continue
}
kafkaSender.addMessage(line.Text,t.logConf.Topic)
t.secLimit.Add(1)
t.secLimit.Wait()
select {
case <- t.exitChan:
logs.Warn("tail obj is exited,config:%v",t.logConf)
return
default:
}
}
waitGroup.Done()
}
func RunServer(){
tailMgr = NewTailMgr()
tailMgr.Process()
waitGroup.Wait()
}