Skip to content

Commit 9eab357

Browse files
philipprestondanielnelson
authored andcommitted
Add counter fields to pf input (influxdata#4216)
1 parent be8b870 commit 9eab357

File tree

3 files changed

+101
-18
lines changed

3 files changed

+101
-18
lines changed

plugins/inputs/pf/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,21 @@ telegraf ALL=(root) NOPASSWD: /sbin/pfctl -s info
3131
- searches (integer, count)
3232
- inserts (integer, count)
3333
- removals (integer, count)
34+
- match (integer, count)
35+
- bad-offset (integer, count)
36+
- fragment (integer, count)
37+
- short (integer, count)
38+
- normalize (integer, count)
39+
- memory (integer, count)
40+
- bad-timestamp (integer, count)
41+
- congestion (integer, count)
42+
- ip-option (integer, count)
43+
- proto-cksum (integer, count)
44+
- state-mismatch (integer, count)
45+
- state-insert (integer, count)
46+
- state-limit (integer, count)
47+
- src-limit (integer, count)
48+
- synproxy (integer, count)
3449

3550
### Example Output:
3651

plugins/inputs/pf/pf.go

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func errMissingData(tag string) error {
6767

6868
type pfctlOutputStanza struct {
6969
HeaderRE *regexp.Regexp
70-
ParseFunc func([]string, telegraf.Accumulator) error
70+
ParseFunc func([]string, map[string]interface{}) error
7171
Found bool
7272
}
7373

@@ -76,11 +76,16 @@ var pfctlOutputStanzas = []*pfctlOutputStanza{
7676
HeaderRE: regexp.MustCompile("^State Table"),
7777
ParseFunc: parseStateTable,
7878
},
79+
&pfctlOutputStanza{
80+
HeaderRE: regexp.MustCompile("^Counters"),
81+
ParseFunc: parseCounterTable,
82+
},
7983
}
8084

8185
var anyTableHeaderRE = regexp.MustCompile("^[A-Z]")
8286

8387
func (pf *PF) parsePfctlOutput(pfoutput string, acc telegraf.Accumulator) error {
88+
fields := make(map[string]interface{})
8489
scanner := bufio.NewScanner(strings.NewReader(pfoutput))
8590
for scanner.Scan() {
8691
line := scanner.Text()
@@ -91,10 +96,14 @@ func (pf *PF) parsePfctlOutput(pfoutput string, acc telegraf.Accumulator) error
9196
line = scanner.Text()
9297
for !anyTableHeaderRE.MatchString(line) {
9398
stanzaLines = append(stanzaLines, line)
94-
scanner.Scan()
95-
line = scanner.Text()
99+
more := scanner.Scan()
100+
if more {
101+
line = scanner.Text()
102+
} else {
103+
break
104+
}
96105
}
97-
if perr := s.ParseFunc(stanzaLines, acc); perr != nil {
106+
if perr := s.ParseFunc(stanzaLines, fields); perr != nil {
98107
return perr
99108
}
100109
s.Found = true
@@ -106,6 +115,8 @@ func (pf *PF) parsePfctlOutput(pfoutput string, acc telegraf.Accumulator) error
106115
return errParseHeader
107116
}
108117
}
118+
119+
acc.AddFields(measurement, fields, make(map[string]string))
109120
return nil
110121
}
111122

@@ -124,11 +135,40 @@ var StateTable = []*Entry{
124135

125136
var stateTableRE = regexp.MustCompile(`^ (.*?)\s+(\d+)`)
126137

127-
func parseStateTable(lines []string, acc telegraf.Accumulator) error {
138+
func parseStateTable(lines []string, fields map[string]interface{}) error {
139+
return storeFieldValues(lines, stateTableRE, fields, StateTable)
140+
}
141+
142+
var CounterTable = []*Entry{
143+
&Entry{"match", "match", -1},
144+
&Entry{"bad-offset", "bad-offset", -1},
145+
&Entry{"fragment", "fragment", -1},
146+
&Entry{"short", "short", -1},
147+
&Entry{"normalize", "normalize", -1},
148+
&Entry{"memory", "memory", -1},
149+
&Entry{"bad-timestamp", "bad-timestamp", -1},
150+
&Entry{"congestion", "congestion", -1},
151+
&Entry{"ip-option", "ip-option", -1},
152+
&Entry{"proto-cksum", "proto-cksum", -1},
153+
&Entry{"state-mismatch", "state-mismatch", -1},
154+
&Entry{"state-insert", "state-insert", -1},
155+
&Entry{"state-limit", "state-limit", -1},
156+
&Entry{"src-limit", "src-limit", -1},
157+
&Entry{"synproxy", "synproxy", -1},
158+
}
159+
160+
var counterTableRE = regexp.MustCompile(`^ (.*?)\s+(\d+)`)
161+
162+
func parseCounterTable(lines []string, fields map[string]interface{}) error {
163+
return storeFieldValues(lines, counterTableRE, fields, CounterTable)
164+
}
165+
166+
func storeFieldValues(lines []string, regex *regexp.Regexp, fields map[string]interface{}, entryTable []*Entry) error {
167+
128168
for _, v := range lines {
129-
entries := stateTableRE.FindStringSubmatch(v)
169+
entries := regex.FindStringSubmatch(v)
130170
if entries != nil {
131-
for _, f := range StateTable {
171+
for _, f := range entryTable {
132172
if f.PfctlTitle == entries[1] {
133173
var err error
134174
if f.Value, err = strconv.ParseInt(entries[2], 10, 64); err != nil {
@@ -139,15 +179,13 @@ func parseStateTable(lines []string, acc telegraf.Accumulator) error {
139179
}
140180
}
141181

142-
fields := make(map[string]interface{})
143-
for _, v := range StateTable {
182+
for _, v := range entryTable {
144183
if v.Value == -1 {
145184
return errMissingData(v.PfctlTitle)
146185
}
147186
fields[v.Field] = v.Value
148187
}
149188

150-
acc.AddFields(measurement, fields, make(map[string]string))
151189
return nil
152190
}
153191

plugins/inputs/pf/pf_test.go

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,25 @@ Counters
152152
measurements: []measurementResult{
153153
measurementResult{
154154
fields: map[string]interface{}{
155-
"entries": int64(2),
156-
"searches": int64(11325),
157-
"inserts": int64(5),
158-
"removals": int64(3)},
155+
"entries": int64(2),
156+
"searches": int64(11325),
157+
"inserts": int64(5),
158+
"removals": int64(3),
159+
"match": int64(11226),
160+
"bad-offset": int64(0),
161+
"fragment": int64(0),
162+
"short": int64(0),
163+
"normalize": int64(0),
164+
"memory": int64(0),
165+
"bad-timestamp": int64(0),
166+
"congestion": int64(0),
167+
"ip-option": int64(0),
168+
"proto-cksum": int64(0),
169+
"state-mismatch": int64(0),
170+
"state-insert": int64(0),
171+
"state-limit": int64(0),
172+
"src-limit": int64(0),
173+
"synproxy": int64(0)},
159174
tags: map[string]string{},
160175
},
161176
},
@@ -197,10 +212,25 @@ Counters
197212
measurements: []measurementResult{
198213
measurementResult{
199214
fields: map[string]interface{}{
200-
"entries": int64(649),
201-
"searches": int64(18421725761),
202-
"inserts": int64(156762508),
203-
"removals": int64(156761859)},
215+
"entries": int64(649),
216+
"searches": int64(18421725761),
217+
"inserts": int64(156762508),
218+
"removals": int64(156761859),
219+
"match": int64(473002784),
220+
"bad-offset": int64(0),
221+
"fragment": int64(2729),
222+
"short": int64(107),
223+
"normalize": int64(1685),
224+
"memory": int64(101),
225+
"bad-timestamp": int64(0),
226+
"congestion": int64(0),
227+
"ip-option": int64(152301),
228+
"proto-cksum": int64(108),
229+
"state-mismatch": int64(24393),
230+
"state-insert": int64(92),
231+
"state-limit": int64(0),
232+
"src-limit": int64(0),
233+
"synproxy": int64(0)},
204234
tags: map[string]string{},
205235
},
206236
},

0 commit comments

Comments
 (0)