Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
telegraf-nftables
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Due to inactivity, this project is scheduled to be deleted on 2035-04-24.
Why is this scheduled?
Show more breadcrumbs
vqgroup
telegraf-nftables
Commits
d5f48e3e
Commit
d5f48e3e
authored
7 years ago
by
Daniel Nelson
Committed by
GitHub
7 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Fix skipped line with empty target in iptables (#3235)
parent
7a41d2c5
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
plugins/inputs/iptables/iptables.go
+6
-13
6 additions, 13 deletions
plugins/inputs/iptables/iptables.go
plugins/inputs/iptables/iptables_test.go
+69
-52
69 additions, 52 deletions
plugins/inputs/iptables/iptables_test.go
with
75 additions
and
65 deletions
plugins/inputs/iptables/iptables.go
+
6
−
13
View file @
d5f48e3e
...
@@ -95,7 +95,7 @@ const measurement = "iptables"
...
@@ -95,7 +95,7 @@ const measurement = "iptables"
var
errParse
=
errors
.
New
(
"Cannot parse iptables list information"
)
var
errParse
=
errors
.
New
(
"Cannot parse iptables list information"
)
var
chainNameRe
=
regexp
.
MustCompile
(
`^Chain\s+(\S+)`
)
var
chainNameRe
=
regexp
.
MustCompile
(
`^Chain\s+(\S+)`
)
var
fieldsHeaderRe
=
regexp
.
MustCompile
(
`^\s*pkts\s+bytes\s+`
)
var
fieldsHeaderRe
=
regexp
.
MustCompile
(
`^\s*pkts\s+bytes\s+`
)
var
comment
Re
=
regexp
.
MustCompile
(
`\s*/\*\s*(.+?)\s*\*/\s*`
)
var
values
Re
=
regexp
.
MustCompile
(
`
^
\s*
(\d+)\s+(\d+)\s+.*?
/\*\s*(.+?)\s*\*/\s*`
)
func
(
ipt
*
Iptables
)
parseAndGather
(
data
string
,
acc
telegraf
.
Accumulator
)
error
{
func
(
ipt
*
Iptables
)
parseAndGather
(
data
string
,
acc
telegraf
.
Accumulator
)
error
{
lines
:=
strings
.
Split
(
data
,
"
\n
"
)
lines
:=
strings
.
Split
(
data
,
"
\n
"
)
...
@@ -110,21 +110,14 @@ func (ipt *Iptables) parseAndGather(data string, acc telegraf.Accumulator) error
...
@@ -110,21 +110,14 @@ func (ipt *Iptables) parseAndGather(data string, acc telegraf.Accumulator) error
return
errParse
return
errParse
}
}
for
_
,
line
:=
range
lines
[
2
:
]
{
for
_
,
line
:=
range
lines
[
2
:
]
{
tokens
:=
strings
.
Fields
(
line
)
matches
:=
valuesRe
.
FindStringSubmatch
(
line
)
if
len
(
tokens
)
<
10
{
if
len
(
matches
)
!=
4
{
continue
continue
}
}
pkts
:=
tokens
[
0
]
pkts
:=
matches
[
1
]
bytes
:=
tokens
[
1
]
bytes
:=
matches
[
2
]
end
:=
strings
.
Join
(
tokens
[
9
:
],
" "
)
comment
:=
matches
[
3
]
matches
:=
commentRe
.
FindStringSubmatch
(
end
)
if
matches
==
nil
{
continue
}
comment
:=
matches
[
1
]
tags
:=
map
[
string
]
string
{
"table"
:
ipt
.
Table
,
"chain"
:
mchain
[
1
],
"ruleid"
:
comment
}
tags
:=
map
[
string
]
string
{
"table"
:
ipt
.
Table
,
"chain"
:
mchain
[
1
],
"ruleid"
:
comment
}
fields
:=
make
(
map
[
string
]
interface
{})
fields
:=
make
(
map
[
string
]
interface
{})
...
...
This diff is collapsed.
Click to expand it.
plugins/inputs/iptables/iptables_test.go
+
69
−
52
View file @
d5f48e3e
...
@@ -154,68 +154,85 @@ func TestIptables_Gather(t *testing.T) {
...
@@ -154,68 +154,85 @@ func TestIptables_Gather(t *testing.T) {
tags
:
[]
map
[
string
]
string
{},
tags
:
[]
map
[
string
]
string
{},
fields
:
[][]
map
[
string
]
interface
{}{},
fields
:
[][]
map
[
string
]
interface
{}{},
},
},
{
// 11 - all target and ports
table
:
"all_recv"
,
chains
:
[]
string
{
"accountfwd"
},
values
:
[]
string
{
`Chain accountfwd (1 references)
pkts bytes target prot opt in out source destination
123 456 all -- eth0 * 0.0.0.0/0 0.0.0.0/0 /* all_recv */
`
},
tags
:
[]
map
[
string
]
string
{
map
[
string
]
string
{
"table"
:
"all_recv"
,
"chain"
:
"accountfwd"
,
"ruleid"
:
"all_recv"
},
},
fields
:
[][]
map
[
string
]
interface
{}{
{
map
[
string
]
interface
{}{
"pkts"
:
uint64
(
123
),
"bytes"
:
uint64
(
456
)}},
},
},
}
}
for
i
,
tt
:=
range
tests
{
for
i
,
tt
:=
range
tests
{
i
++
t
.
Run
(
tt
.
table
,
func
(
t
*
testing
.
T
)
{
ipt
:=
&
Iptables
{
i
++
Table
:
tt
.
table
,
ipt
:=
&
Iptables
{
Chains
:
tt
.
chains
,
Table
:
tt
.
table
,
lister
:
func
(
table
,
chain
string
)
(
string
,
error
)
{
Chains
:
tt
.
chains
,
if
len
(
tt
.
values
)
>
0
{
lister
:
func
(
table
,
chain
string
)
(
string
,
error
)
{
v
:=
tt
.
values
[
0
]
if
len
(
tt
.
values
)
>
0
{
tt
.
values
=
tt
.
values
[
1
:
]
v
:=
tt
.
values
[
0
]
return
v
,
nil
tt
.
values
=
tt
.
values
[
1
:
]
}
return
v
,
nil
return
""
,
nil
}
},
return
""
,
nil
}
},
acc
:=
new
(
testutil
.
Accumulator
)
err
:=
acc
.
GatherError
(
ipt
.
Gather
)
if
!
reflect
.
DeepEqual
(
tt
.
err
,
err
)
{
t
.
Errorf
(
"%d: expected error '%#v' got '%#v'"
,
i
,
tt
.
err
,
err
)
}
if
tt
.
table
==
""
{
n
:=
acc
.
NFields
()
if
n
!=
0
{
t
.
Errorf
(
"%d: expected 0 fields if empty table got %d"
,
i
,
n
)
}
continue
}
if
len
(
tt
.
chains
)
==
0
{
n
:=
acc
.
NFields
()
if
n
!=
0
{
t
.
Errorf
(
"%d: expected 0 fields if empty chains got %d"
,
i
,
n
)
}
}
continue
acc
:=
new
(
testutil
.
Accumulator
)
}
err
:=
acc
.
GatherError
(
ipt
.
Gather
)
if
len
(
tt
.
tags
)
==
0
{
if
!
reflect
.
DeepEqual
(
tt
.
err
,
err
)
{
n
:=
acc
.
NFields
()
t
.
Errorf
(
"%d: expected error '%#v' got '%#v'"
,
i
,
tt
.
err
,
err
)
if
n
!=
0
{
t
.
Errorf
(
"%d: expected 0 values got %d"
,
i
,
n
)
}
}
continue
if
tt
.
table
==
""
{
}
n
:=
acc
.
NFields
()
n
:=
0
if
n
!=
0
{
for
j
,
tags
:=
range
tt
.
tags
{
t
.
Errorf
(
"%d: expected 0 fields if empty table got %d"
,
i
,
n
)
for
k
,
fields
:=
range
tt
.
fields
[
j
]
{
if
len
(
acc
.
Metrics
)
<
n
+
1
{
t
.
Errorf
(
"%d: expected at least %d values got %d"
,
i
,
n
+
1
,
len
(
acc
.
Metrics
))
break
}
}
m
:=
acc
.
Metrics
[
n
]
return
if
!
reflect
.
DeepEqual
(
m
.
Measurement
,
measurement
)
{
}
t
.
Errorf
(
"%d %d %d: expected measurement '%#v' got '%#v'
\n
"
,
i
,
j
,
k
,
measurement
,
m
.
Measurement
)
if
len
(
tt
.
chains
)
==
0
{
n
:=
acc
.
NFields
()
if
n
!=
0
{
t
.
Errorf
(
"%d: expected 0 fields if empty chains got %d"
,
i
,
n
)
}
}
if
!
reflect
.
DeepEqual
(
m
.
Tags
,
tags
)
{
return
t
.
Errorf
(
"%d %d %d: expected tags
\n
%#v got
\n
%#v
\n
"
,
i
,
j
,
k
,
tags
,
m
.
Tags
)
}
if
len
(
tt
.
tags
)
==
0
{
n
:=
acc
.
NFields
()
if
n
!=
0
{
t
.
Errorf
(
"%d: expected 0 values got %d"
,
i
,
n
)
}
}
if
!
reflect
.
DeepEqual
(
m
.
Fields
,
fields
)
{
return
t
.
Errorf
(
"%d %d %d: expected fields
\n
%#v got
\n
%#v
\n
"
,
i
,
j
,
k
,
fields
,
m
.
Fields
)
}
n
:=
0
for
j
,
tags
:=
range
tt
.
tags
{
for
k
,
fields
:=
range
tt
.
fields
[
j
]
{
if
len
(
acc
.
Metrics
)
<
n
+
1
{
t
.
Errorf
(
"%d: expected at least %d values got %d"
,
i
,
n
+
1
,
len
(
acc
.
Metrics
))
break
}
m
:=
acc
.
Metrics
[
n
]
if
!
reflect
.
DeepEqual
(
m
.
Measurement
,
measurement
)
{
t
.
Errorf
(
"%d %d %d: expected measurement '%#v' got '%#v'
\n
"
,
i
,
j
,
k
,
measurement
,
m
.
Measurement
)
}
if
!
reflect
.
DeepEqual
(
m
.
Tags
,
tags
)
{
t
.
Errorf
(
"%d %d %d: expected tags
\n
%#v got
\n
%#v
\n
"
,
i
,
j
,
k
,
tags
,
m
.
Tags
)
}
if
!
reflect
.
DeepEqual
(
m
.
Fields
,
fields
)
{
t
.
Errorf
(
"%d %d %d: expected fields
\n
%#v got
\n
%#v
\n
"
,
i
,
j
,
k
,
fields
,
m
.
Fields
)
}
n
++
}
}
n
++
}
}
}
}
)
}
}
}
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment