-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
178 lines (151 loc) · 3.54 KB
/
main.go
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package main
import (
"bytes"
"github.com/luopengift/gohttp"
"github.com/luopengift/golibs/file"
"github.com/luopengift/golibs/kafka"
"github.com/luopengift/golibs/logger"
"io/ioutil"
"os"
"os/signal"
"runtime"
"runtime/pprof"
"strconv"
"time"
)
var (
p *kafka.Producer
)
const (
VERSION = "2017.08.25-0.0.1"
APP = "collect"
)
type RuntimeConfig struct {
DEBUG bool `json:"DEBUG"`
MAXPROCS int `json:"MAXPROCS"`
}
type KafkaConfig struct {
Addrs []string `json:"addrs"`
Topic string `json:"topic"`
MaxThreads int `json:"maxthreads"`
}
type FileConfig struct {
Name []string `json:name`
Offset int64 `json:offset`
Prefix string `json:"prefix"`
Suffix string `json:"suffix"`
}
type HttpConfig struct {
Addr string `json:"addr"`
}
type Config struct {
Runtime RuntimeConfig
Kafka KafkaConfig
File FileConfig
Http HttpConfig
Tags string `json:"tags"`
Version string `json:version`
}
func NewConfig(filename string) (*Config, error) {
config := &Config{}
conf := file.NewConfig("./config.json")
err := conf.Parse(config)
logger.Info("%+v", conf.String())
return config, err
}
type Monitor struct {
gohttp.HttpHandler
}
func (self *Monitor) GET() {
self.Output(p.ChanInfo())
}
func main() {
logger.Info("%v %v", APP, VERSION)
//获取全局panic
defer func() {
if err := recover(); err != nil {
logger.Error("Panic error:%v", err)
}
}()
//init config
config, err := NewConfig("./config.json")
if err != nil {
logger.Error("<config error> %+v", err)
return
}
if VERSION != config.Version {
logger.Error("The version of app and config is not match!exit...")
return
}
if config.Runtime.MAXPROCS == 0 {
if config.Runtime.DEBUG {
config.Runtime.MAXPROCS = 1
} else {
config.Runtime.MAXPROCS = runtime.NumCPU()
}
}
runtime.GOMAXPROCS(config.Runtime.MAXPROCS)
// http server
app := gohttp.Init()
app.Route("^/monitor$", &Monitor{})
go app.Run(config.Http.Addr)
//set debug cpu model
if config.Runtime.DEBUG {
//pprof file
s := make(chan os.Signal, 1)
signal.Notify(s, os.Interrupt, os.Kill)
f, err := os.OpenFile("./cpu.prof", os.O_RDWR|os.O_CREATE, 0644)
if err != nil {
logger.Error("<file open error> %v", err)
}
defer f.Close()
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
go func() {
select {
case sign := <-s:
logger.Error("Got signal:", sign)
pprof.StopCPUProfile()
f.Close()
os.Exit(-1)
}
}()
}
//init kafka producer
p = kafka.NewProducer(config.Kafka.Addrs, config.Kafka.Topic, config.Kafka.MaxThreads)
go p.WriteToTopic()
//main loop
for _, v := range config.File.Name {
logger.Warn("%v", v)
go func(v string) {
f := file.NewTail(v, file.TimeRule)
f.Seek(config.File.Offset)
//开启groutine,定时刷新offset文件
go func() {
for {
select {
case <-time.After(60 * time.Second):
str := strconv.FormatInt(f.Offset(), 10) + "\n"
err := ioutil.WriteFile("var/"+f.BaseName()+".offset", []byte(str), 0666)
if err != nil {
logger.Error("<write offset file error:%+v>", err)
}
}
}
}()
f.ReadLine()
for value := range f.NextLine() {
var buf bytes.Buffer
buf.WriteString(config.File.Prefix)
buf.Write(value)
buf.WriteString(config.File.Suffix)
p.Write(buf.Bytes())
// ret, err := p.Write([]byte(config.File.Prefix + *value + config.File.Suffix))
// logger.Debug("%v, %v, %v", *value, ret, err, f.Offset())
}
f.Stop()
}(v)
}
logger.Info("APP=%s VERSION=%s, running success.", APP, VERSION)
select {}
}