forked from Griesbacher/nagflux
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Connector.go
68 lines (63 loc) · 1.61 KB
/
Connector.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
package livestatus
import (
"bufio"
"encoding/csv"
"fmt"
"github.com/kdar/factorlog"
"io"
"net"
"strings"
)
//Connector fetches data from livestatus.
type Connector struct {
Log *factorlog.FactorLog
LivestatusAddress string
ConnectionType string
}
//Queries livestatus and returns an list of list outer list are lines inner elements within the line.
func (connector Connector) connectToLivestatus(query string, result chan []string, outerFinish chan bool) {
var conn net.Conn
switch connector.ConnectionType {
case "tcp":
conn, _ = net.Dial("tcp", connector.LivestatusAddress)
case "file":
conn, _ = net.Dial("unix", connector.LivestatusAddress)
default:
connector.Log.Critical("Connection type is unkown, options are: tcp, file. Input:" + connector.ConnectionType)
outerFinish <- false
return
}
if conn == nil {
connector.Log.Critical("Unable to connect to livestatus: ", connector.LivestatusAddress)
outerFinish <- false
return
}
defer conn.Close()
fmt.Fprintf(conn, query)
reader := bufio.NewReader(conn)
length := 1
for length > 0 {
message, _, err := reader.ReadLine()
if err != nil {
if err == io.EOF {
break
} else {
connector.Log.Warn(err)
}
}
length = len(message)
if length > 0 {
csvReader := csv.NewReader(strings.NewReader(string(message)))
csvReader.Comma = ';'
csvReader.LazyQuotes = true
records, err := csvReader.Read()
if err != nil {
connector.Log.Warn("Query failed while csv parsing:" + query)
connector.Log.Warn(string(message))
connector.Log.Warn(err)
}
result <- records
}
}
outerFinish <- true
}