-
Notifications
You must be signed in to change notification settings - Fork 30
/
influxdb-sqlserver.go
353 lines (305 loc) · 8.2 KB
/
influxdb-sqlserver.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net"
"os"
"os/signal"
"runtime"
"strconv"
"strings"
"sync"
"syscall"
"time"
"github.com/zensqlmonitor/influxdb-sqlserver/Godeps/_workspace/src/github.com/BurntSushi/toml"
cfg "github.com/zensqlmonitor/influxdb-sqlserver/config"
"github.com/zensqlmonitor/influxdb-sqlserver/etl"
"github.com/zensqlmonitor/influxdb-sqlserver/log"
)
var wg sync.WaitGroup
var exitChan = make(chan int)
var fConfig = flag.String("config", "influxdb-sqlserver.conf", "the configuration file in TOML format")
type TOMLConfig cfg.TOMLConfig
var config TOMLConfig
type DynMap map[string]interface{}
type Param struct {
connString string
fullFilePath string
pollingInterval int
url string
database string
username string
password string
precision string
}
//
// Listen to System Signals
//
func listenToSystemSignals() {
signalChan := make(chan os.Signal, 1)
code := 0
signal.Notify(signalChan, os.Interrupt)
signal.Notify(signalChan, os.Kill)
signal.Notify(signalChan, syscall.SIGTERM)
select {
case sig := <-signalChan:
log.Info("Received signal %s. shutting down", sig)
case code = <-exitChan:
switch code {
case 0:
log.Info("Shutting down")
default:
log.Warn("Shutting down")
}
}
log.Close()
os.Exit(code)
}
//
// Init logging
//
func (config *TOMLConfig) initLogging() {
var LogModes []string
var LogConfigs []DynMap
// Log Modes
LogModes = strings.Split(config.Logging.Modes, ",")
LogConfigs = make([]DynMap, len(LogModes))
for i, mode := range LogModes {
mode = strings.TrimSpace(mode)
//fmt.Println(mode)
// Log Level
var levelName string
if mode == "console" {
levelName = config.Logging.LevelConsole
} else {
levelName = config.Logging.LevelFile
}
level, ok := log.LogLevels[levelName]
if !ok {
log.Fatal(4, "Unknown log level: %s", levelName)
}
// Generate log configuration
switch mode {
case "console":
formatting := config.Logging.Formatting
LogConfigs[i] = DynMap{
"level": level,
"formatting": formatting,
}
case "file":
LogConfigs[i] = DynMap{
"level": level,
"filename": config.Logging.FileName,
"rotate": config.Logging.LogRotate,
"maxlines": config.Logging.MaxLines,
"maxsize": 1 << uint(config.Logging.MaxSizeShift),
"daily": config.Logging.DailyRotate,
"maxdays": config.Logging.MaxDays,
}
}
cfgJsonBytes, _ := json.Marshal(LogConfigs[i])
log.NewLogger(10000, mode, string(cfgJsonBytes))
}
}
// Validate adds default value, validates the config data
// and returns an error describing any problems or nil.
func (toml *TOMLConfig) Validate() error {
// defaults
if toml.Logging.FileName == "" {
toml.Logging.FileName = cfg.DefaultLogFileName
}
if toml.Logging.Modes == "" {
toml.Logging.Modes = cfg.DefaultModes
}
if toml.Logging.BufferLen == 0 {
toml.Logging.BufferLen = cfg.DefaultBufferLen
}
if toml.Logging.LevelConsole == "" {
toml.Logging.LevelConsole = cfg.DefaultLevelConsole
}
if toml.Logging.LevelFile == "" {
toml.Logging.LevelFile = cfg.DefaultLevelFile
}
if toml.Logging.MaxLines == 0 {
toml.Logging.MaxLines = cfg.DefaultMaxLines
}
if toml.Logging.MaxSizeShift == 0 {
toml.Logging.MaxSizeShift = cfg.DefaultMaxSizeShift
}
if toml.Logging.MaxDays == 0 {
toml.Logging.MaxDays = cfg.DefaultMaxDays
}
if toml.Polling.Interval == 0 {
toml.Polling.Interval = cfg.DefaultPollingInterval
}
if toml.Polling.IntervalIfError == 0 {
toml.Polling.IntervalIfError = cfg.DefaultPollingIntervalIfError
}
if toml.InfluxDB.Url == "" {
toml.InfluxDB.Url = cfg.DefaultInfluxDBUrl
}
if toml.InfluxDB.Database == "" {
toml.InfluxDB.Database = cfg.DefaultInfluxDBDatabase
}
if toml.InfluxDB.Precision == "" {
toml.InfluxDB.Precision = cfg.DefaultInfluxDBPrecision
}
if toml.InfluxDB.TimeOut == 0 {
toml.InfluxDB.TimeOut = cfg.DefaultInfluxDBTimeOut
}
// InfluxDB
fullUrl := strings.Replace(toml.InfluxDB.Url, "http://", "", -1)
host, portStr, err := net.SplitHostPort(fullUrl)
if err != nil {
return fmt.Errorf("InfluxDB url must be formatted as host:port but "+
"was '%s' (%v)", toml.InfluxDB.Url, err)
}
if len(host) == 0 {
return fmt.Errorf("InfluxDB url value ('%s') is missing a host",
toml.InfluxDB.Url)
}
port, err := strconv.Atoi(portStr)
if err != nil {
return fmt.Errorf("InfluxDB url port value ('%s') must be a number "+
"(%v)", portStr, err)
}
if port < 1 || port > 65535 {
return fmt.Errorf("InfluxDB url port must be within [1-65535] but "+
"was '%d'", port)
}
// SQL Server
servers := toml.Servers
if len(servers) == 0 {
return fmt.Errorf("You must at least define a SQL Server instance")
}
for _, server := range servers {
if server.IP == "" {
return fmt.Errorf("SQL Server instance IP is not defined")
}
if server.Port < 1 || server.Port > 65535 {
return fmt.Errorf("InfluxDB url port must be within [1-65535] but "+
"was '%d'", server.Port)
}
}
// Scripts
scripts := toml.Scripts
if len(scripts) == 0 {
return fmt.Errorf("You must at least define one SQL script")
}
for scriptName, script := range scripts {
if script.Interval < 15 {
toml.Scripts[scriptName].Interval = 15 // override
}
}
return nil
}
//
// Gather data
//
func (p *Param) gather() {
var wgi sync.WaitGroup
for {
wgi.Add(1)
go func(p *Param) {
defer wgi.Done()
// read script
sqlscript, err := ioutil.ReadFile(p.fullFilePath)
if err != nil {
// Handle error
log.Error(1, "Error while reading script", err)
}
// extract data
start := time.Now()
ext := etl.NewExtracter(p.connString, string(sqlscript))
err = ext.Extract()
if err != nil {
// Handle error
log.Error(1, "Error while executing script", err)
}
stringSlice := strings.Split(p.connString, ";")
log.Trace(fmt.Sprintf("<-- Extract | %v sec | %s,%s | %s | took %s", p.pollingInterval,
stringSlice[0], strings.Replace(stringSlice[1], "Port=", "", -1), p.fullFilePath,
time.Since(start)))
// load data
start = time.Now()
loa := etl.NewLoader(fmt.Sprintf("%s/write?db=%s&precision=%s", p.url, p.database, p.precision), ext.Result)
err = loa.Load()
if err != nil {
// Handle error
log.Error(1, "Error while loading data", err)
}
log.Trace(fmt.Sprintf("--> Load | %v sec | %s,%s | %s | took %s", p.pollingInterval,
stringSlice[0], strings.Replace(stringSlice[1], "Port=", "", -1), p.fullFilePath,
time.Since(start)))
}(p) // end go routine
//defer log.Info("Sleeping now for %d sec...", p.pollingInterval)
time.Sleep(time.Duration(p.pollingInterval) * time.Second)
}
wgi.Wait()
}
//
// Init
//
func init() {
runtime.GOMAXPROCS(runtime.NumCPU())
}
//
// Utils
//
func connectionString(server cfg.Server) string {
return fmt.Sprintf(
"Server=%s;Port=%v;User Id=%s;Password=%s;app name=influxdb-sqlserver;log=1",
server.IP, server.Port, server.Username, server.Password)
}
//
// Main
//
func main() {
// command-line flag parsing
flag.Parse()
// config data
if _, err := toml.DecodeFile(*fConfig, &config); err != nil {
// Handle error: panic
panic(err)
}
if err := (&config).Validate(); err != nil {
fmt.Println(err)
return
}
// init global logging
config.initLogging()
// listen to System Signals
go listenToSystemSignals()
// polling loop
log.Info("Starting influxdb-sqlserver")
scripts := config.Scripts
for _, server := range config.Servers { // foreach server
// set connString
connString := connectionString(server)
for _, script := range scripts { // foreach script
// test if path exists
scriptPath := cfg.DefaultSqlScriptPath + script.Name
scriptInterval := script.Interval
if _, err := os.Stat(scriptPath); err != nil {
// Handle error: panic
log.Error(3, "Script file path does not exist!", err)
panic(err)
}
// start collect within a go routine
wg.Add(1) // increment the WaitGroup counter
p := &Param{connString,
scriptPath,
scriptInterval,
config.InfluxDB.Url,
config.InfluxDB.Database,
config.InfluxDB.Username,
config.InfluxDB.Password,
config.InfluxDB.Precision}
go p.gather()
} // end foreach script
} // end foreach server
// Wait for goroutines to complete.
wg.Wait()
}