Skip to content

Commit 9e6ffaf

Browse files
committed
Introduce egress metrics to count invalid packets and bytes
Signed-off-by: terashima <tomoya-terashima@cybozu.co.jp>
1 parent 314fc3a commit 9e6ffaf

File tree

1 file changed

+63
-14
lines changed

1 file changed

+63
-14
lines changed

v2/pkg/metrics/egress.go

Lines changed: 63 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,33 +45,53 @@ var (
4545
Name: "nftables_masqueraded_bytes_total",
4646
Help: "the number of bytes that are masqueraded by nftables",
4747
}, []string{"namespace", "pod", "egress"})
48+
49+
NfTableInvalidPackets = prometheus.NewGaugeVec(prometheus.GaugeOpts{
50+
Namespace: constants.MetricsNS,
51+
Subsystem: "egress",
52+
Name: "nftables_invalid_packets_total",
53+
Help: "the number of packets that are dropped as invalid packets by nftables",
54+
}, []string{"namespace", "pod", "egress"})
55+
56+
NfTableInvalidBytes = prometheus.NewGaugeVec(prometheus.GaugeOpts{
57+
Namespace: constants.MetricsNS,
58+
Subsystem: "egress",
59+
Name: "nftables_invalid_bytes_total",
60+
Help: "the number of bytes that are dropped as invalid packets by nftables",
61+
}, []string{"namespace", "pod", "egress"})
4862
)
4963

5064
type egressCollector struct {
51-
conn *nftables.Conn
52-
nfConnctackCount prometheus.Gauge
53-
nfConnctackLimit prometheus.Gauge
54-
nfTablesPackets prometheus.Gauge
55-
nfTablesBytes prometheus.Gauge
65+
conn *nftables.Conn
66+
nfConnctackCount prometheus.Gauge
67+
nfConnctackLimit prometheus.Gauge
68+
nfTablesNATPackets prometheus.Gauge
69+
nfTablesNATBytes prometheus.Gauge
70+
nfTablesInvalidPackets prometheus.Gauge
71+
nfTablesInvalidBytes prometheus.Gauge
5672
}
5773

5874
func NewEgressCollector(ns, pod, egress string) (Collector, error) {
5975
NfConnctackCount.Reset()
6076
NfConnctackLimit.Reset()
6177
NfTableMasqueradeBytes.Reset()
6278
NfTableMasqueradePackets.Reset()
79+
NfTableInvalidPackets.Reset()
80+
NfTableInvalidBytes.Reset()
6381

6482
c, err := nftables.New()
6583
if err != nil {
6684
return nil, err
6785
}
6886

6987
return &egressCollector{
70-
conn: c,
71-
nfConnctackCount: NfConnctackCount.WithLabelValues(ns, pod, egress),
72-
nfConnctackLimit: NfConnctackLimit.WithLabelValues(ns, pod, egress),
73-
nfTablesPackets: NfTableMasqueradePackets.WithLabelValues(ns, pod, egress),
74-
nfTablesBytes: NfTableMasqueradeBytes.WithLabelValues(ns, pod, egress),
88+
conn: c,
89+
nfConnctackCount: NfConnctackCount.WithLabelValues(ns, pod, egress),
90+
nfConnctackLimit: NfConnctackLimit.WithLabelValues(ns, pod, egress),
91+
nfTablesNATPackets: NfTableMasqueradePackets.WithLabelValues(ns, pod, egress),
92+
nfTablesNATBytes: NfTableMasqueradeBytes.WithLabelValues(ns, pod, egress),
93+
nfTablesInvalidPackets: NfTableInvalidPackets.WithLabelValues(ns, pod, egress),
94+
nfTablesInvalidBytes: NfTableInvalidBytes.WithLabelValues(ns, pod, egress),
7595
}, nil
7696
}
7797

@@ -93,17 +113,24 @@ func (c *egressCollector) Update() error {
93113
}
94114
c.nfConnctackLimit.Set(float64(val))
95115

96-
packets, bytes, err := c.getNfTablesCounter()
116+
natPackets, natBytes, err := c.getNfTablesNATCounter()
97117
if err != nil {
98118
return nil
99119
}
100-
c.nfTablesPackets.Set(float64(packets))
101-
c.nfTablesBytes.Set(float64(bytes))
120+
c.nfTablesNATPackets.Set(float64(natPackets))
121+
c.nfTablesNATBytes.Set(float64(natBytes))
122+
123+
invalidPackets, invalidBytes, err := c.getNfTablesInvalidCounter()
124+
if err != nil {
125+
return nil
126+
}
127+
c.nfTablesInvalidPackets.Set(float64(invalidPackets))
128+
c.nfTablesInvalidBytes.Set(float64(invalidBytes))
102129

103130
return nil
104131
}
105132

106-
func (c *egressCollector) getNfTablesCounter() (uint64, uint64, error) {
133+
func (c *egressCollector) getNfTablesNATCounter() (uint64, uint64, error) {
107134
table := &nftables.Table{Family: nftables.TableFamilyIPv4, Name: "nat"}
108135
rules, err := c.conn.GetRules(table, &nftables.Chain{
109136
Name: "POSTROUTING",
@@ -125,6 +152,28 @@ func (c *egressCollector) getNfTablesCounter() (uint64, uint64, error) {
125152
return 0, 0, errors.New("a masquerade rule is not found")
126153
}
127154

155+
func (c *egressCollector) getNfTablesInvalidCounter() (uint64, uint64, error) {
156+
table := &nftables.Table{Family: nftables.TableFamilyIPv4, Name: "filter"}
157+
rules, err := c.conn.GetRules(table, &nftables.Chain{
158+
Name: "FORWARD",
159+
Type: nftables.ChainTypeFilter,
160+
Table: table,
161+
Hooknum: nftables.ChainHookForward,
162+
})
163+
if err != nil {
164+
return 0, 0, err
165+
}
166+
for _, rule := range rules {
167+
for _, e := range rule.Exprs {
168+
if counter, ok := e.(*expr.Counter); ok {
169+
// A rule in the egress pod must be only one, so we can return by finding a first one.
170+
return counter.Packets, counter.Bytes, nil
171+
}
172+
}
173+
}
174+
return 0, 0, errors.New("a rule for invalid packets is not found")
175+
}
176+
128177
func readUintFromFile(path string) (uint64, error) {
129178
data, err := os.ReadFile(path)
130179
if err != nil {

0 commit comments

Comments
 (0)