@@ -67,7 +67,7 @@ func errMissingData(tag string) error {
6767
6868type 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
8185var anyTableHeaderRE = regexp .MustCompile ("^[A-Z]" )
8286
8387func (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
125136var 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
0 commit comments