-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsyslog-ng-exporter.go
132 lines (117 loc) · 3.31 KB
/
syslog-ng-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
package main
import (
"log"
"net"
"path"
"regexp"
"strings"
"strconv"
"gopkg.in/alecthomas/kingpin.v2"
"github.com/prometheus/common/version"
"github.com/prometheus/client_golang/prometheus"
"gopkg.in/ini.v1"
)
func stringInSlice(a string, list []string) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}
func parseReceivedData(data string) map[string]map[string]float64 {
var result = map[string]map[string]float64{}
for _, sid := range syslogngconfig.sourceIds {
result[sid] = map[string]float64{}
}
for _, line := range strings.Split(data, "\n") {
match := pattern.FindStringSubmatch(line)
if (stringInSlice(match[2], syslogngconfig.sourceIds)) {
if num, err := strconv.ParseFloat(match[6], 64); err == nil {
result[match[2]][match[5]] = num
}
}
}
return result
}
func querySyslogNG() map[string]map[string]float64 {
var result = make(map[string]map[string]float64)
c, err := net.Dial("unix", syslogngconfig.socket)
if err != nil {
log.Print("Dial error", err)
return result
}
defer c.Close()
msg := "stats"
_, err = c.Write([]byte(msg))
if err != nil {
log.Print("Write error:", err)
return result
}
buf := make([]byte, 10240)
_, err = c.Read(buf[:])
if err != nil {
return result
}
result = parseReceivedData(string(buf))
return result
}
var (
pattern = regexp.MustCompile(`(.*);(.*);(.*);(.*);(.*);(.*)`)
config = kingpin.Flag(
"config.syslog-ng",
"Path to .syslog-ng.cnf file for some information. (socket location, list of source ids separated by ,)",
).Default(path.Join(path.Base(""), ".syslog-ng.cnf")).String()
syslogngconfig = new(syslogNgConfig)
)
type syslogNgCollector struct {
syslogNgMetric *prometheus.Desc
}
func newSyslogNgCollector() *syslogNgCollector {
return &syslogNgCollector{
syslogNgMetric: prometheus.NewDesc("syslog_ng_metric",
"Shows syslo-ng destination statistics based on given config",
[]string{"source_id", "type"}, nil,
),
}
}
func (collector *syslogNgCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- collector.syslogNgMetric
}
func (collector *syslogNgCollector) Collect(ch chan<- prometheus.Metric) {
var currentData = querySyslogNG()
for source := range currentData {
for stateType := range currentData[source] {
ch <- prometheus.MustNewConstMetric(collector.syslogNgMetric, prometheus.CounterValue, currentData[source][stateType], source, stateType)
}
}
}
type syslogNgConfig struct {
socket string
sourceIds []string
}
func parseConfig(config interface{}) {
opts := ini.LoadOptions{
AllowBooleanKeys: true,
}
cfg, err := ini.LoadSources(opts, config)
if err != nil {
log.Fatal("Failed to read config")
return
}
syslogngconfig.socket = "/tmp/go.sock"
if (cfg.Section("").HasKey("socket")) {
syslogngconfig.socket = cfg.Section("").Key("socket").String()
}
syslogngconfig.sourceIds = append(syslogngconfig.sourceIds, "d_splunk")
if (cfg.Section("").HasKey("sourceids")) {
syslogngconfig.sourceIds = strings.Split(cfg.Section("").Key("sourceids").String(), ",")
}
}
func init() {
kingpin.Version(version.Print("syslog_ng_exporter"))
kingpin.HelpFlag.Short('h')
kingpin.Parse()
parseConfig(*config)
prometheus.MustRegister(newSyslogNgCollector())
}