-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcacheeasy_exporter.go
208 lines (198 loc) · 4.63 KB
/
cacheeasy_exporter.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
* cacheeasy_exporter - scrapes redis/predixy/machine stats and
* exports for prometheus.
* Copyright (C) 2017 Joyield, Inc. <[email protected]>
* All rights reserved.
*/
package main
import (
"flag"
machine_exporter "github.com/joyieldInc/machine_exporter/exporter"
predixy_exporter "github.com/joyieldInc/predixy_exporter/exporter"
redis_exporter "github.com/joyieldInc/redis_exporter/exporter"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"gopkg.in/yaml.v2"
"io/ioutil"
"log"
"net/http"
"strings"
"sync"
"time"
)
type Config struct {
Bind string `yaml:"bind"`
Redis []string `yaml:"redis"`
Predixy []string `yaml:"predixy"`
}
const (
RedisType = 0
PredixyType = 1
)
type Exporter struct {
addr string
name string
etype int
exporter interface{}
}
type CacheEasyCollector struct {
mtx sync.Mutex
exporters map[string]Exporter
}
func loadConfig(configfile string) (*Config, error) {
s, err := ioutil.ReadFile(configfile)
if err != nil {
return nil, err
}
cfg := &Config{}
err = yaml.Unmarshal(s, cfg)
if err != nil {
return nil, err
}
return cfg, nil
}
func (c *CacheEasyCollector) init(cfg *Config) error {
e, err := machine_exporter.NewExporter()
if err != nil {
return err
}
prometheus.MustRegister(e)
c.load(cfg)
return nil
}
func (c *CacheEasyCollector) load(cfg *Config) error {
c.mtx.Lock()
defer c.mtx.Unlock()
addrs := map[string]bool{}
for _, serv := range cfg.Redis {
ss := strings.Fields(serv)
if len(ss) != 2 {
log.Printf("redis server \"%s\" invalid, ignore it", serv)
continue
}
addrs[ss[0]] = true
exp, ok := c.exporters[ss[0]]
if ok {
if exp.etype == RedisType && exp.name == ss[1] {
continue
}
if exp.etype == RedisType {
e := exp.exporter.(*redis_exporter.Exporter)
prometheus.Unregister(e)
} else if exp.etype == PredixyType {
e := exp.exporter.(*predixy_exporter.Exporter)
prometheus.Unregister(e)
}
log.Printf("del exporter %s %s\n", exp.addr, exp.name)
}
e, err := redis_exporter.NewExporter(ss[0], ss[1])
if err != nil {
return err
}
prometheus.MustRegister(e)
c.exporters[ss[0]] = Exporter{
addr: ss[0],
name: ss[1],
etype: RedisType,
exporter: e,
}
log.Printf("add redis exporter %s\n", serv)
}
for _, serv := range cfg.Predixy {
ss := strings.Fields(serv)
if len(ss) != 2 {
log.Printf("predixy server \"%s\" invalid, ignore it", serv)
continue
}
addrs[ss[0]] = true
exp, ok := c.exporters[ss[0]]
if ok {
if exp.etype == PredixyType && exp.name == ss[1] {
continue
}
if exp.etype == RedisType {
e := exp.exporter.(*redis_exporter.Exporter)
prometheus.Unregister(e)
} else if exp.etype == PredixyType {
e := exp.exporter.(*predixy_exporter.Exporter)
prometheus.Unregister(e)
}
log.Printf("del exporter %s %s\n", exp.addr, exp.name)
}
e, err := predixy_exporter.NewExporter(ss[0], ss[1])
if err != nil {
return err
}
prometheus.MustRegister(e)
c.exporters[ss[0]] = Exporter{
addr: ss[0],
name: ss[1],
etype: PredixyType,
exporter: e,
}
log.Printf("add predixy exporter %s\n", serv)
}
for addr, _ := range c.exporters {
_, ok := addrs[addr]
if !ok {
addrs[addr] = false
}
}
for addr, ok := range addrs {
if !ok {
exp, _ := c.exporters[addr]
if exp.etype == RedisType {
e := exp.exporter.(*redis_exporter.Exporter)
prometheus.Unregister(e)
} else if exp.etype == PredixyType {
e := exp.exporter.(*predixy_exporter.Exporter)
prometheus.Unregister(e)
}
delete(c.exporters, addr)
log.Printf("del exporter %s %s\n", exp.addr, exp.name)
}
}
return nil
}
func refreshConfig(c *CacheEasyCollector, configfile string) {
for true {
time.Sleep(10 * time.Second)
cfg, err := loadConfig(configfile)
if err != nil {
log.Printf("Refresh config file error:%v", err)
continue
}
err = c.load(cfg)
if err != nil {
log.Printf("Refresh config load error:%v", err)
}
}
}
func main() {
var (
config = flag.String("config", "cacheeasy_exporter.yml", "Config file")
bind = flag.String("bind", "", "Listen address")
)
flag.Parse()
cfg, err := loadConfig(*config)
if err != nil {
log.Fatal(err)
}
if len(*bind) > 0 {
cfg.Bind = *bind
}
if len(cfg.Bind) == 0 {
cfg.Bind = ":9123"
}
c := &CacheEasyCollector{
exporters: make(map[string]Exporter),
}
err = c.init(cfg)
if err != nil {
log.Fatal(err)
}
log.Printf("CacheEasyExporter listen at %s\n", cfg.Bind)
go refreshConfig(c, *config)
http.Handle("/metrics", promhttp.Handler())
log.Fatal(http.ListenAndServe(cfg.Bind, nil))
}