forked from beenanner/rsyslog_exporter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
exporter.go
210 lines (188 loc) · 4.16 KB
/
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
209
210
package main
import (
"bufio"
"bytes"
"fmt"
"log"
"os"
"sync"
"github.com/prometheus/client_golang/prometheus"
)
type rsyslogType int
const (
rsyslogUnknown rsyslogType = iota
rsyslogAction
rsyslogInput
rsyslogQueue
rsyslogResource
rsyslogDynStat
rsyslogDynafileCache
rsyslogInputIMDUP
rsyslogForward
rsyslogKubernetes
)
type rsyslogExporter struct {
started bool
logfile *os.File
scanner *bufio.Scanner
pointStore
}
func newRsyslogExporter() *rsyslogExporter {
e := &rsyslogExporter{
scanner: bufio.NewScanner(os.Stdin),
pointStore: pointStore{
pointMap: make(map[string]*point),
lock: &sync.RWMutex{},
},
}
return e
}
func (re *rsyslogExporter) handleStatLine(rawbuf []byte) error {
s := bytes.SplitN(rawbuf, []byte(" "), 4)
if len(s) != 4 {
return fmt.Errorf("failed to split log line, expected 4 columns, got: %v", len(s))
}
buf := s[3]
pstatType := getStatType(buf)
switch pstatType {
case rsyslogAction:
a, err := newActionFromJSON(buf)
if err != nil {
return err
}
for _, p := range a.toPoints() {
re.set(p)
}
case rsyslogInput:
i, err := newInputFromJSON(buf)
if err != nil {
return err
}
for _, p := range i.toPoints() {
re.set(p)
}
case rsyslogInputIMDUP:
u, err := newInputIMUDPFromJSON(buf)
if err != nil {
return err
}
for _, p := range u.toPoints() {
re.set(p)
}
case rsyslogQueue:
q, err := newQueueFromJSON(buf)
if err != nil {
return err
}
for _, p := range q.toPoints() {
re.set(p)
}
case rsyslogResource:
r, err := newResourceFromJSON(buf)
if err != nil {
return err
}
for _, p := range r.toPoints() {
re.set(p)
}
case rsyslogDynStat:
s, err := newDynStatFromJSON(buf)
if err != nil {
return err
}
for _, p := range s.toPoints() {
re.set(p)
}
case rsyslogDynafileCache:
d, err := newDynafileCacheFromJSON(buf)
if err != nil {
return err
}
for _, p := range d.toPoints() {
re.set(p)
}
case rsyslogForward:
f, err := newForwardFromJSON(buf)
if err != nil {
return err
}
for _, p := range f.toPoints() {
re.set(p)
}
case rsyslogKubernetes:
k, err := newKubernetesFromJSON(buf)
if err != nil {
return err
}
for _, p := range k.toPoints() {
re.set(p)
}
default:
return fmt.Errorf("unknown pstat type: %v", pstatType)
}
return nil
}
// Describe sends the description of currently known metrics collected
// by this Collector to the provided channel. Note that this implementation
// does not necessarily send the "super-set of all possible descriptors" as
// defined by the Collector interface spec, depending on the timing of when
// it is called. The rsyslog exporter does not know all possible metrics
// it will export until the first full batch of rsyslog impstats messages
// are received via stdin. This is ok for now.
func (re *rsyslogExporter) Describe(ch chan<- *prometheus.Desc) {
ch <- prometheus.NewDesc(
prometheus.BuildFQName("", "rsyslog", "scrapes"),
"times exporter has been scraped",
nil, nil,
)
keys := re.keys()
for _, k := range keys {
p, err := re.get(k)
if err != nil {
ch <- p.promDescription()
}
}
}
// Collect is called by Prometheus when collecting metrics.
func (re *rsyslogExporter) Collect(ch chan<- prometheus.Metric) {
keys := re.keys()
for _, k := range keys {
p, err := re.get(k)
if err != nil {
continue
}
labelValues := []string{}
if p.promLabelValue() != "" {
labelValues = []string{p.promLabelValue()}
}
metric := prometheus.MustNewConstMetric(
p.promDescription(),
p.promType(),
p.promValue(),
labelValues...,
)
ch <- metric
}
}
func (re *rsyslogExporter) run(silent bool) {
errorPoint := &point{
Name: "stats_line_errors",
Type: counter,
Description: "Counts errors during stats line handling",
}
re.set(errorPoint)
for re.scanner.Scan() {
err := re.handleStatLine(re.scanner.Bytes())
if err != nil {
errorPoint.Value += 1
if !silent {
log.Printf("error handling stats line: %v, line was: %s", err, re.scanner.Bytes())
}
}
}
if err := re.scanner.Err(); err != nil {
log.Printf("error reading input: %v", err)
}
log.Print("input ended, exiting normally")
os.Exit(0)
}