@@ -26,17 +26,7 @@ import (
2626)
2727
2828type conntrackCollector struct {
29- current * prometheus.Desc
30- limit * prometheus.Desc
31- found * prometheus.Desc
32- invalid * prometheus.Desc
33- ignore * prometheus.Desc
34- insert * prometheus.Desc
35- insertFailed * prometheus.Desc
36- drop * prometheus.Desc
37- earlyDrop * prometheus.Desc
38- searchRestart * prometheus.Desc
39- logger * slog.Logger
29+ logger * slog.Logger
4030}
4131
4232type conntrackStatistics struct {
@@ -54,59 +44,62 @@ func init() {
5444 registerCollector ("conntrack" , defaultEnabled , NewConntrackCollector )
5545}
5646
47+ var (
48+ conntrackCurrent = prometheus .NewDesc (
49+ prometheus .BuildFQName (namespace , "" , "nf_conntrack_entries" ),
50+ "Number of currently allocated flow entries for connection tracking." ,
51+ nil , nil ,
52+ )
53+ conntrackLimit = prometheus .NewDesc (
54+ prometheus .BuildFQName (namespace , "" , "nf_conntrack_entries_limit" ),
55+ "Maximum size of connection tracking table." ,
56+ nil , nil ,
57+ )
58+ conntrackFound = prometheus .NewDesc (
59+ prometheus .BuildFQName (namespace , "" , "nf_conntrack_stat_found" ),
60+ "Number of searched entries which were successful." ,
61+ nil , nil ,
62+ )
63+ conntrackInvalid = prometheus .NewDesc (
64+ prometheus .BuildFQName (namespace , "" , "nf_conntrack_stat_invalid" ),
65+ "Number of packets seen which can not be tracked." ,
66+ nil , nil ,
67+ )
68+ conntrackIgnore = prometheus .NewDesc (
69+ prometheus .BuildFQName (namespace , "" , "nf_conntrack_stat_ignore" ),
70+ "Number of packets seen which are already connected to a conntrack entry." ,
71+ nil , nil ,
72+ )
73+ conntrackInsert = prometheus .NewDesc (
74+ prometheus .BuildFQName (namespace , "" , "nf_conntrack_stat_insert" ),
75+ "Number of entries inserted into the list." ,
76+ nil , nil ,
77+ )
78+ conntrackInsertFailed = prometheus .NewDesc (
79+ prometheus .BuildFQName (namespace , "" , "nf_conntrack_stat_insert_failed" ),
80+ "Number of entries for which list insertion was attempted but failed." ,
81+ nil , nil ,
82+ )
83+ conntrackDrop = prometheus .NewDesc (
84+ prometheus .BuildFQName (namespace , "" , "nf_conntrack_stat_drop" ),
85+ "Number of packets dropped due to conntrack failure." ,
86+ nil , nil ,
87+ )
88+ conntrackEarlyDrop = prometheus .NewDesc (
89+ prometheus .BuildFQName (namespace , "" , "nf_conntrack_stat_early_drop" ),
90+ "Number of dropped conntrack entries to make room for new ones, if maximum table size was reached." ,
91+ nil , nil ,
92+ )
93+ conntrackSearchRestart = prometheus .NewDesc (
94+ prometheus .BuildFQName (namespace , "" , "nf_conntrack_stat_search_restart" ),
95+ "Number of conntrack table lookups which had to be restarted due to hashtable resizes." ,
96+ nil , nil ,
97+ )
98+ )
99+
57100// NewConntrackCollector returns a new Collector exposing conntrack stats.
58101func NewConntrackCollector (logger * slog.Logger ) (Collector , error ) {
59102 return & conntrackCollector {
60- current : prometheus .NewDesc (
61- prometheus .BuildFQName (namespace , "" , "nf_conntrack_entries" ),
62- "Number of currently allocated flow entries for connection tracking." ,
63- nil , nil ,
64- ),
65- limit : prometheus .NewDesc (
66- prometheus .BuildFQName (namespace , "" , "nf_conntrack_entries_limit" ),
67- "Maximum size of connection tracking table." ,
68- nil , nil ,
69- ),
70- found : prometheus .NewDesc (
71- prometheus .BuildFQName (namespace , "" , "nf_conntrack_stat_found" ),
72- "Number of searched entries which were successful." ,
73- nil , nil ,
74- ),
75- invalid : prometheus .NewDesc (
76- prometheus .BuildFQName (namespace , "" , "nf_conntrack_stat_invalid" ),
77- "Number of packets seen which can not be tracked." ,
78- nil , nil ,
79- ),
80- ignore : prometheus .NewDesc (
81- prometheus .BuildFQName (namespace , "" , "nf_conntrack_stat_ignore" ),
82- "Number of packets seen which are already connected to a conntrack entry." ,
83- nil , nil ,
84- ),
85- insert : prometheus .NewDesc (
86- prometheus .BuildFQName (namespace , "" , "nf_conntrack_stat_insert" ),
87- "Number of entries inserted into the list." ,
88- nil , nil ,
89- ),
90- insertFailed : prometheus .NewDesc (
91- prometheus .BuildFQName (namespace , "" , "nf_conntrack_stat_insert_failed" ),
92- "Number of entries for which list insertion was attempted but failed." ,
93- nil , nil ,
94- ),
95- drop : prometheus .NewDesc (
96- prometheus .BuildFQName (namespace , "" , "nf_conntrack_stat_drop" ),
97- "Number of packets dropped due to conntrack failure." ,
98- nil , nil ,
99- ),
100- earlyDrop : prometheus .NewDesc (
101- prometheus .BuildFQName (namespace , "" , "nf_conntrack_stat_early_drop" ),
102- "Number of dropped conntrack entries to make room for new ones, if maximum table size was reached." ,
103- nil , nil ,
104- ),
105- searchRestart : prometheus .NewDesc (
106- prometheus .BuildFQName (namespace , "" , "nf_conntrack_stat_search_restart" ),
107- "Number of conntrack table lookups which had to be restarted due to hashtable resizes." ,
108- nil , nil ,
109- ),
110103 logger : logger ,
111104 }, nil
112105}
@@ -117,36 +110,36 @@ func (c *conntrackCollector) Update(ch chan<- prometheus.Metric) error {
117110 return c .handleErr (err )
118111 }
119112 ch <- prometheus .MustNewConstMetric (
120- c . current , prometheus .GaugeValue , float64 (value ))
113+ conntrackCurrent , prometheus .GaugeValue , float64 (value ))
121114
122115 value , err = readUintFromFile (procFilePath ("sys/net/netfilter/nf_conntrack_max" ))
123116 if err != nil {
124117 return c .handleErr (err )
125118 }
126119 ch <- prometheus .MustNewConstMetric (
127- c . limit , prometheus .GaugeValue , float64 (value ))
120+ conntrackLimit , prometheus .GaugeValue , float64 (value ))
128121
129122 conntrackStats , err := getConntrackStatistics ()
130123 if err != nil {
131124 return c .handleErr (err )
132125 }
133126
134127 ch <- prometheus .MustNewConstMetric (
135- c . found , prometheus .GaugeValue , float64 (conntrackStats .found ))
128+ conntrackFound , prometheus .GaugeValue , float64 (conntrackStats .found ))
136129 ch <- prometheus .MustNewConstMetric (
137- c . invalid , prometheus .GaugeValue , float64 (conntrackStats .invalid ))
130+ conntrackInvalid , prometheus .GaugeValue , float64 (conntrackStats .invalid ))
138131 ch <- prometheus .MustNewConstMetric (
139- c . ignore , prometheus .GaugeValue , float64 (conntrackStats .ignore ))
132+ conntrackIgnore , prometheus .GaugeValue , float64 (conntrackStats .ignore ))
140133 ch <- prometheus .MustNewConstMetric (
141- c . insert , prometheus .GaugeValue , float64 (conntrackStats .insert ))
134+ conntrackInsert , prometheus .GaugeValue , float64 (conntrackStats .insert ))
142135 ch <- prometheus .MustNewConstMetric (
143- c . insertFailed , prometheus .GaugeValue , float64 (conntrackStats .insertFailed ))
136+ conntrackInsertFailed , prometheus .GaugeValue , float64 (conntrackStats .insertFailed ))
144137 ch <- prometheus .MustNewConstMetric (
145- c . drop , prometheus .GaugeValue , float64 (conntrackStats .drop ))
138+ conntrackDrop , prometheus .GaugeValue , float64 (conntrackStats .drop ))
146139 ch <- prometheus .MustNewConstMetric (
147- c . earlyDrop , prometheus .GaugeValue , float64 (conntrackStats .earlyDrop ))
140+ conntrackEarlyDrop , prometheus .GaugeValue , float64 (conntrackStats .earlyDrop ))
148141 ch <- prometheus .MustNewConstMetric (
149- c . searchRestart , prometheus .GaugeValue , float64 (conntrackStats .searchRestart ))
142+ conntrackSearchRestart , prometheus .GaugeValue , float64 (conntrackStats .searchRestart ))
150143 return nil
151144}
152145
@@ -159,7 +152,7 @@ func (c *conntrackCollector) handleErr(err error) error {
159152}
160153
161154func getConntrackStatistics () (* conntrackStatistics , error ) {
162- c := conntrackStatistics {}
155+ s := conntrackStatistics {}
163156
164157 fs , err := procfs .NewFS (* procPath )
165158 if err != nil {
@@ -172,15 +165,15 @@ func getConntrackStatistics() (*conntrackStatistics, error) {
172165 }
173166
174167 for _ , connStat := range connStats {
175- c .found += connStat .Found
176- c .invalid += connStat .Invalid
177- c .ignore += connStat .Ignore
178- c .insert += connStat .Insert
179- c .insertFailed += connStat .InsertFailed
180- c .drop += connStat .Drop
181- c .earlyDrop += connStat .EarlyDrop
182- c .searchRestart += connStat .SearchRestart
168+ s .found += connStat .Found
169+ s .invalid += connStat .Invalid
170+ s .ignore += connStat .Ignore
171+ s .insert += connStat .Insert
172+ s .insertFailed += connStat .InsertFailed
173+ s .drop += connStat .Drop
174+ s .earlyDrop += connStat .EarlyDrop
175+ s .searchRestart += connStat .SearchRestart
183176 }
184177
185- return & c , nil
178+ return & s , nil
186179}
0 commit comments