Skip to content

Commit 5c12586

Browse files
committed
remove mapstructure
Signed-off-by: Nikos Kakavas <[email protected]>
1 parent 3d753b0 commit 5c12586

7 files changed

+650
-89
lines changed

go.mod

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ go 1.13
44

55
require (
66
github.com/google/go-cmp v0.5.4
7-
github.com/mitchellh/mapstructure v1.4.3
87
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a
98
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c
109
)

go.sum

-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
github.com/google/go-cmp v0.5.4 h1:L8R9j+yAqZuZjsqh/z+F1NCffTKKLShY6zXTItVIZ8M=
22
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
3-
github.com/mitchellh/mapstructure v1.4.3 h1:OVowDSCllw/YjdLkam3/sm7wEtOy59d8ndGgCcyj8cs=
4-
github.com/mitchellh/mapstructure v1.4.3/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
53
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a h1:DcqTD9SDLc+1P/r1EmRBwnVsrOwW+kk2vWf9n+1sGhs=
64
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
75
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c h1:VwygUrnw9jn88c4u8GD3rZQbqrP/tgas88tPUbBxQrk=

proc_netstat.go

+243-26
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"strconv"
2222
"strings"
2323

24-
"github.com/mitchellh/mapstructure"
2524
"github.com/prometheus/procfs/internal/util"
2625
)
2726

@@ -171,32 +170,21 @@ type IpExt struct {
171170

172171
func (p Proc) Netstat() (ProcNetstat, error) {
173172
filename := p.path("net/netstat")
174-
procNetstat := ProcNetstat{PID: p.PID}
175-
176173
data, err := util.ReadFileNoStat(filename)
177174
if err != nil {
178-
return procNetstat, err
179-
}
180-
181-
netStats, err := parseNetstat(bytes.NewReader(data), filename)
182-
if err != nil {
183-
return procNetstat, err
184-
}
185-
186-
mapStructureErr := mapstructure.Decode(netStats, &procNetstat)
187-
if mapStructureErr != nil {
188-
return procNetstat, mapStructureErr
175+
return ProcNetstat{PID: p.PID}, err
189176
}
190-
191-
return procNetstat, nil
177+
procNetstat, err := parseNetstat(bytes.NewReader(data), filename)
178+
procNetstat.PID = p.PID
179+
return procNetstat, err
192180
}
193181

194182
// parseNetstat parses the metrics from proc/<pid>/net/netstat file
195-
// and returns a map contains those metrics (e.g. {"TcpExt": {"SyncookiesSent": 0}}).
196-
func parseNetstat(r io.Reader, fileName string) (map[string]map[string]float64, error) {
183+
// and returns a ProcNetstat structure.
184+
func parseNetstat(r io.Reader, fileName string) (ProcNetstat, error) {
197185
var (
198-
netStats = map[string]map[string]float64{}
199186
scanner = bufio.NewScanner(r)
187+
procNetstat = ProcNetstat{}
200188
)
201189

202190
for scanner.Scan() {
@@ -205,19 +193,248 @@ func parseNetstat(r io.Reader, fileName string) (map[string]map[string]float64,
205193
valueParts := strings.Split(scanner.Text(), " ")
206194
// Remove trailing :.
207195
protocol := nameParts[0][:len(nameParts[0])-1]
208-
netStats[protocol] = map[string]float64{}
209196
if len(nameParts) != len(valueParts) {
210-
return nil, fmt.Errorf("mismatch field count mismatch in %s: %s",
197+
return procNetstat, fmt.Errorf("mismatch field count mismatch in %s: %s",
211198
fileName, protocol)
212199
}
213200
for i := 1; i < len(nameParts); i++ {
214-
var err error
215-
netStats[protocol][nameParts[i]], err = strconv.ParseFloat(valueParts[i], 64)
201+
value, err := strconv.ParseFloat(valueParts[i], 64)
216202
if err != nil {
217-
return nil, err
203+
return procNetstat, err
204+
}
205+
key := nameParts[i]
206+
207+
switch protocol {
208+
case "TcpExt":
209+
switch key {
210+
case "SyncookiesSent":
211+
procNetstat.TcpExt.SyncookiesSent = value
212+
case "SyncookiesRecv":
213+
procNetstat.TcpExt.SyncookiesRecv = value
214+
case "SyncookiesFailed":
215+
procNetstat.TcpExt.SyncookiesFailed = value
216+
case "EmbryonicRsts":
217+
procNetstat.TcpExt.EmbryonicRsts = value
218+
case "PruneCalled":
219+
procNetstat.TcpExt.PruneCalled = value
220+
case "RcvPruned":
221+
procNetstat.TcpExt.RcvPruned = value
222+
case "OfoPruned":
223+
procNetstat.TcpExt.OfoPruned = value
224+
case "OutOfWindowIcmps":
225+
procNetstat.TcpExt.OutOfWindowIcmps = value
226+
case "LockDroppedIcmps":
227+
procNetstat.TcpExt.LockDroppedIcmps = value
228+
case "ArpFilter":
229+
procNetstat.TcpExt.ArpFilter = value
230+
case "TW":
231+
procNetstat.TcpExt.TW = value
232+
case "TWRecycled":
233+
procNetstat.TcpExt.TWRecycled = value
234+
case "TWKilled":
235+
procNetstat.TcpExt.TWKilled = value
236+
case "PAWSActive":
237+
procNetstat.TcpExt.PAWSActive = value
238+
case "PAWSEstab":
239+
procNetstat.TcpExt.PAWSEstab = value
240+
case "DelayedACKs":
241+
procNetstat.TcpExt.DelayedACKs = value
242+
case "DelayedACKLocked":
243+
procNetstat.TcpExt.DelayedACKLocked = value
244+
case "DelayedACKLost":
245+
procNetstat.TcpExt.DelayedACKLost = value
246+
case "ListenOverflows":
247+
procNetstat.TcpExt.ListenOverflows = value
248+
case "ListenDrops":
249+
procNetstat.TcpExt.ListenDrops = value
250+
case "TCPHPHits":
251+
procNetstat.TcpExt.TCPHPHits = value
252+
case "TCPPureAcks":
253+
procNetstat.TcpExt.TCPPureAcks = value
254+
case "TCPHPAcks":
255+
procNetstat.TcpExt.TCPHPAcks = value
256+
case "TCPRenoRecovery":
257+
procNetstat.TcpExt.TCPRenoRecovery = value
258+
case "TCPSackRecovery":
259+
procNetstat.TcpExt.TCPSackRecovery = value
260+
case "TCPSACKReneging":
261+
procNetstat.TcpExt.TCPSACKReneging = value
262+
case "TCPSACKReorder":
263+
procNetstat.TcpExt.TCPSACKReorder = value
264+
case "TCPRenoReorder":
265+
procNetstat.TcpExt.TCPRenoReorder = value
266+
case "TCPTSReorder":
267+
procNetstat.TcpExt.TCPTSReorder = value
268+
case "TCPFullUndo":
269+
procNetstat.TcpExt.TCPFullUndo = value
270+
case "TCPPartialUndo":
271+
procNetstat.TcpExt.TCPPartialUndo = value
272+
case "TCPDSACKUndo":
273+
procNetstat.TcpExt.TCPDSACKUndo = value
274+
case "TCPLossUndo":
275+
procNetstat.TcpExt.TCPLossUndo = value
276+
case "TCPLostRetransmit":
277+
procNetstat.TcpExt.TCPLostRetransmit = value
278+
case "TCPRenoFailures":
279+
procNetstat.TcpExt.TCPRenoFailures = value
280+
case "TCPSackFailures":
281+
procNetstat.TcpExt.TCPSackFailures = value
282+
case "TCPLossFailures":
283+
procNetstat.TcpExt.TCPLossFailures = value
284+
case "TCPFastRetrans":
285+
procNetstat.TcpExt.TCPFastRetrans = value
286+
case "TCPSlowStartRetrans":
287+
procNetstat.TcpExt.TCPSlowStartRetrans = value
288+
case "TCPTimeouts":
289+
procNetstat.TcpExt.TCPTimeouts = value
290+
case "TCPLossProbes":
291+
procNetstat.TcpExt.TCPLossProbes = value
292+
case "TCPLossProbeRecovery":
293+
procNetstat.TcpExt.TCPLossProbeRecovery = value
294+
case "TCPRenoRecoveryFail":
295+
procNetstat.TcpExt.TCPRenoRecoveryFail = value
296+
case "TCPSackRecoveryFail":
297+
procNetstat.TcpExt.TCPSackRecoveryFail = value
298+
case "TCPRcvCollapsed":
299+
procNetstat.TcpExt.TCPRcvCollapsed = value
300+
case "TCPDSACKOldSent":
301+
procNetstat.TcpExt.TCPDSACKOldSent = value
302+
case "TCPDSACKOfoSent":
303+
procNetstat.TcpExt.TCPDSACKOfoSent = value
304+
case "TCPDSACKRecv":
305+
procNetstat.TcpExt.TCPDSACKRecv = value
306+
case "TCPDSACKOfoRecv":
307+
procNetstat.TcpExt.TCPDSACKOfoRecv = value
308+
case "TCPAbortOnData":
309+
procNetstat.TcpExt.TCPAbortOnData = value
310+
case "TCPAbortOnClose":
311+
procNetstat.TcpExt.TCPAbortOnClose = value
312+
case "TCPDeferAcceptDrop":
313+
procNetstat.TcpExt.TCPDeferAcceptDrop = value
314+
case "IPReversePathFilter":
315+
procNetstat.TcpExt.IPReversePathFilter = value
316+
case "TCPTimeWaitOverflow":
317+
procNetstat.TcpExt.TCPTimeWaitOverflow = value
318+
case "TCPReqQFullDoCookies":
319+
procNetstat.TcpExt.TCPReqQFullDoCookies = value
320+
case "TCPReqQFullDrop":
321+
procNetstat.TcpExt.TCPReqQFullDrop = value
322+
case "TCPRetransFail":
323+
procNetstat.TcpExt.TCPRetransFail = value
324+
case "TCPRcvCoalesce":
325+
procNetstat.TcpExt.TCPRcvCoalesce = value
326+
case "TCPOFOQueue":
327+
procNetstat.TcpExt.TCPOFOQueue = value
328+
case "TCPOFODrop":
329+
procNetstat.TcpExt.TCPOFODrop = value
330+
case "TCPOFOMerge":
331+
procNetstat.TcpExt.TCPOFOMerge = value
332+
case "TCPChallengeACK":
333+
procNetstat.TcpExt.TCPChallengeACK = value
334+
case "TCPSYNChallenge":
335+
procNetstat.TcpExt.TCPSYNChallenge = value
336+
case "TCPFastOpenActive":
337+
procNetstat.TcpExt.TCPFastOpenActive = value
338+
case "TCPFastOpenActiveFail":
339+
procNetstat.TcpExt.TCPFastOpenActiveFail = value
340+
case "TCPFastOpenPassive":
341+
procNetstat.TcpExt.TCPFastOpenPassive = value
342+
case "TCPFastOpenPassiveFail":
343+
procNetstat.TcpExt.TCPFastOpenPassiveFail = value
344+
case "TCPFastOpenListenOverflow":
345+
procNetstat.TcpExt.TCPFastOpenListenOverflow = value
346+
case "TCPFastOpenCookieReqd":
347+
procNetstat.TcpExt.TCPFastOpenCookieReqd = value
348+
case "TCPFastOpenBlackhole":
349+
procNetstat.TcpExt.TCPFastOpenBlackhole = value
350+
case "TCPSpuriousRtxHostQueues":
351+
procNetstat.TcpExt.TCPSpuriousRtxHostQueues = value
352+
case "BusyPollRxPackets":
353+
procNetstat.TcpExt.BusyPollRxPackets = value
354+
case "TCPAutoCorking":
355+
procNetstat.TcpExt.TCPAutoCorking = value
356+
case "TCPFromZeroWindowAdv":
357+
procNetstat.TcpExt.TCPFromZeroWindowAdv = value
358+
case "TCPToZeroWindowAdv":
359+
procNetstat.TcpExt.TCPToZeroWindowAdv = value
360+
case "TCPWantZeroWindowAdv":
361+
procNetstat.TcpExt.TCPWantZeroWindowAdv = value
362+
case "TCPSynRetrans":
363+
procNetstat.TcpExt.TCPSynRetrans = value
364+
case "TCPOrigDataSent":
365+
procNetstat.TcpExt.TCPOrigDataSent = value
366+
case "TCPHystartTrainDetect":
367+
procNetstat.TcpExt.TCPHystartTrainDetect = value
368+
case "TCPHystartTrainCwnd":
369+
procNetstat.TcpExt.TCPHystartTrainCwnd = value
370+
case "TCPHystartDelayDetect":
371+
procNetstat.TcpExt.TCPHystartDelayDetect = value
372+
case "TCPHystartDelayCwnd":
373+
procNetstat.TcpExt.TCPHystartDelayCwnd = value
374+
case "TCPACKSkippedSynRecv":
375+
procNetstat.TcpExt.TCPACKSkippedSynRecv = value
376+
case "TCPACKSkippedPAWS":
377+
procNetstat.TcpExt.TCPACKSkippedPAWS = value
378+
case "TCPACKSkippedSeq":
379+
procNetstat.TcpExt.TCPACKSkippedSeq = value
380+
case "TCPACKSkippedFinWait2":
381+
procNetstat.TcpExt.TCPACKSkippedFinWait2 = value
382+
case "TCPACKSkippedTimeWait":
383+
procNetstat.TcpExt.TCPACKSkippedTimeWait = value
384+
case "TCPACKSkippedChallenge":
385+
procNetstat.TcpExt.TCPACKSkippedChallenge = value
386+
case "TCPWinProbe":
387+
procNetstat.TcpExt.TCPWinProbe = value
388+
case "TCPKeepAlive":
389+
procNetstat.TcpExt.TCPKeepAlive = value
390+
case "TCPMTUPFail":
391+
procNetstat.TcpExt.TCPMTUPFail = value
392+
case "TCPMTUPSuccess":
393+
procNetstat.TcpExt.TCPMTUPSuccess = value
394+
case "TCPWqueueTooBig":
395+
procNetstat.TcpExt.TCPWqueueTooBig = value
396+
}
397+
case "IpExt":
398+
switch key {
399+
case "InNoRoutes":
400+
procNetstat.IpExt.InNoRoutes = value
401+
case "InTruncatedPkts":
402+
procNetstat.IpExt.InTruncatedPkts = value
403+
case "InMcastPkts":
404+
procNetstat.IpExt.InMcastPkts = value
405+
case "OutMcastPkts":
406+
procNetstat.IpExt.OutMcastPkts = value
407+
case "InBcastPkts":
408+
procNetstat.IpExt.InBcastPkts = value
409+
case "OutBcastPkts":
410+
procNetstat.IpExt.OutBcastPkts = value
411+
case "InOctets":
412+
procNetstat.IpExt.InOctets = value
413+
case "OutOctets":
414+
procNetstat.IpExt.OutOctets = value
415+
case "InMcastOctets":
416+
procNetstat.IpExt.InMcastOctets = value
417+
case "OutMcastOctets":
418+
procNetstat.IpExt.OutMcastOctets = value
419+
case "InBcastOctets":
420+
procNetstat.IpExt.InBcastOctets = value
421+
case "OutBcastOctets":
422+
procNetstat.IpExt.OutBcastOctets = value
423+
case "InCsumErrors":
424+
procNetstat.IpExt.InCsumErrors = value
425+
case "InNoECTPkts":
426+
procNetstat.IpExt.InNoECTPkts = value
427+
case "InECT1Pkts":
428+
procNetstat.IpExt.InECT1Pkts = value
429+
case "InECT0Pkts":
430+
procNetstat.IpExt.InECT0Pkts = value
431+
case "InCEPkts":
432+
procNetstat.IpExt.InCEPkts = value
433+
case "ReasmOverlaps":
434+
procNetstat.IpExt.ReasmOverlaps = value
435+
}
218436
}
219437
}
220438
}
221-
222-
return netStats, scanner.Err()
439+
return procNetstat, scanner.Err()
223440
}

proc_netstat_test.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package procfs
22

3-
import "testing"
3+
import (
4+
"testing"
5+
)
46

57
func TestProcNetstat(t *testing.T) {
68
p, err := getProcFixtures(t).Proc(26231)
@@ -25,12 +27,11 @@ func TestProcNetstat(t *testing.T) {
2527
{name: "TcpExt:PAWSEstab", want: 3640, have: procNetstat.TcpExt.PAWSEstab},
2628

2729
{name: "IpExt:InNoRoutes", want: 0, have: procNetstat.IpExt.InNoRoutes},
28-
{name: "TcpExt:InMcastPkts", want: 208, have: procNetstat.IpExt.InMcastPkts},
29-
{name: "TcpExt:OutMcastPkts", want: 214, have: procNetstat.IpExt.OutMcastPkts},
30+
{name: "IpExt:InMcastPkts", want: 208, have: procNetstat.IpExt.InMcastPkts},
31+
{name: "IpExt:OutMcastPkts", want: 214, have: procNetstat.IpExt.OutMcastPkts},
3032
} {
3133
if test.want != test.have {
3234
t.Errorf("want %s %f, have %f", test.name, test.want, test.have)
3335
}
3436
}
35-
3637
}

0 commit comments

Comments
 (0)