Skip to content

Commit

Permalink
remove mapstructure
Browse files Browse the repository at this point in the history
Signed-off-by: Nikos Kakavas <nikakis@protonmail.com>
  • Loading branch information
nikakis committed Mar 9, 2022
1 parent 3d753b0 commit 5c12586
Show file tree
Hide file tree
Showing 7 changed files with 650 additions and 89 deletions.
1 change: 0 additions & 1 deletion go.mod
Expand Up @@ -4,7 +4,6 @@ go 1.13

require (
github.com/google/go-cmp v0.5.4
github.com/mitchellh/mapstructure v1.4.3
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c
)
2 changes: 0 additions & 2 deletions go.sum
@@ -1,7 +1,5 @@
github.com/google/go-cmp v0.5.4 h1:L8R9j+yAqZuZjsqh/z+F1NCffTKKLShY6zXTItVIZ8M=
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/mitchellh/mapstructure v1.4.3 h1:OVowDSCllw/YjdLkam3/sm7wEtOy59d8ndGgCcyj8cs=
github.com/mitchellh/mapstructure v1.4.3/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a h1:DcqTD9SDLc+1P/r1EmRBwnVsrOwW+kk2vWf9n+1sGhs=
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c h1:VwygUrnw9jn88c4u8GD3rZQbqrP/tgas88tPUbBxQrk=
Expand Down
269 changes: 243 additions & 26 deletions proc_netstat.go
Expand Up @@ -21,7 +21,6 @@ import (
"strconv"
"strings"

"github.com/mitchellh/mapstructure"
"github.com/prometheus/procfs/internal/util"
)

Expand Down Expand Up @@ -171,32 +170,21 @@ type IpExt struct {

func (p Proc) Netstat() (ProcNetstat, error) {
filename := p.path("net/netstat")
procNetstat := ProcNetstat{PID: p.PID}

data, err := util.ReadFileNoStat(filename)
if err != nil {
return procNetstat, err
}

netStats, err := parseNetstat(bytes.NewReader(data), filename)
if err != nil {
return procNetstat, err
}

mapStructureErr := mapstructure.Decode(netStats, &procNetstat)
if mapStructureErr != nil {
return procNetstat, mapStructureErr
return ProcNetstat{PID: p.PID}, err
}

return procNetstat, nil
procNetstat, err := parseNetstat(bytes.NewReader(data), filename)
procNetstat.PID = p.PID
return procNetstat, err
}

// parseNetstat parses the metrics from proc/<pid>/net/netstat file
// and returns a map contains those metrics (e.g. {"TcpExt": {"SyncookiesSent": 0}}).
func parseNetstat(r io.Reader, fileName string) (map[string]map[string]float64, error) {
// and returns a ProcNetstat structure.
func parseNetstat(r io.Reader, fileName string) (ProcNetstat, error) {
var (
netStats = map[string]map[string]float64{}
scanner = bufio.NewScanner(r)
procNetstat = ProcNetstat{}
)

for scanner.Scan() {
Expand All @@ -205,19 +193,248 @@ func parseNetstat(r io.Reader, fileName string) (map[string]map[string]float64,
valueParts := strings.Split(scanner.Text(), " ")
// Remove trailing :.
protocol := nameParts[0][:len(nameParts[0])-1]
netStats[protocol] = map[string]float64{}
if len(nameParts) != len(valueParts) {
return nil, fmt.Errorf("mismatch field count mismatch in %s: %s",
return procNetstat, fmt.Errorf("mismatch field count mismatch in %s: %s",
fileName, protocol)
}
for i := 1; i < len(nameParts); i++ {
var err error
netStats[protocol][nameParts[i]], err = strconv.ParseFloat(valueParts[i], 64)
value, err := strconv.ParseFloat(valueParts[i], 64)
if err != nil {
return nil, err
return procNetstat, err
}
key := nameParts[i]

switch protocol {
case "TcpExt":
switch key {
case "SyncookiesSent":
procNetstat.TcpExt.SyncookiesSent = value
case "SyncookiesRecv":
procNetstat.TcpExt.SyncookiesRecv = value
case "SyncookiesFailed":
procNetstat.TcpExt.SyncookiesFailed = value
case "EmbryonicRsts":
procNetstat.TcpExt.EmbryonicRsts = value
case "PruneCalled":
procNetstat.TcpExt.PruneCalled = value
case "RcvPruned":
procNetstat.TcpExt.RcvPruned = value
case "OfoPruned":
procNetstat.TcpExt.OfoPruned = value
case "OutOfWindowIcmps":
procNetstat.TcpExt.OutOfWindowIcmps = value
case "LockDroppedIcmps":
procNetstat.TcpExt.LockDroppedIcmps = value
case "ArpFilter":
procNetstat.TcpExt.ArpFilter = value
case "TW":
procNetstat.TcpExt.TW = value
case "TWRecycled":
procNetstat.TcpExt.TWRecycled = value
case "TWKilled":
procNetstat.TcpExt.TWKilled = value
case "PAWSActive":
procNetstat.TcpExt.PAWSActive = value
case "PAWSEstab":
procNetstat.TcpExt.PAWSEstab = value
case "DelayedACKs":
procNetstat.TcpExt.DelayedACKs = value
case "DelayedACKLocked":
procNetstat.TcpExt.DelayedACKLocked = value
case "DelayedACKLost":
procNetstat.TcpExt.DelayedACKLost = value
case "ListenOverflows":
procNetstat.TcpExt.ListenOverflows = value
case "ListenDrops":
procNetstat.TcpExt.ListenDrops = value
case "TCPHPHits":
procNetstat.TcpExt.TCPHPHits = value
case "TCPPureAcks":
procNetstat.TcpExt.TCPPureAcks = value
case "TCPHPAcks":
procNetstat.TcpExt.TCPHPAcks = value
case "TCPRenoRecovery":
procNetstat.TcpExt.TCPRenoRecovery = value
case "TCPSackRecovery":
procNetstat.TcpExt.TCPSackRecovery = value
case "TCPSACKReneging":
procNetstat.TcpExt.TCPSACKReneging = value
case "TCPSACKReorder":
procNetstat.TcpExt.TCPSACKReorder = value
case "TCPRenoReorder":
procNetstat.TcpExt.TCPRenoReorder = value
case "TCPTSReorder":
procNetstat.TcpExt.TCPTSReorder = value
case "TCPFullUndo":
procNetstat.TcpExt.TCPFullUndo = value
case "TCPPartialUndo":
procNetstat.TcpExt.TCPPartialUndo = value
case "TCPDSACKUndo":
procNetstat.TcpExt.TCPDSACKUndo = value
case "TCPLossUndo":
procNetstat.TcpExt.TCPLossUndo = value
case "TCPLostRetransmit":
procNetstat.TcpExt.TCPLostRetransmit = value
case "TCPRenoFailures":
procNetstat.TcpExt.TCPRenoFailures = value
case "TCPSackFailures":
procNetstat.TcpExt.TCPSackFailures = value
case "TCPLossFailures":
procNetstat.TcpExt.TCPLossFailures = value
case "TCPFastRetrans":
procNetstat.TcpExt.TCPFastRetrans = value
case "TCPSlowStartRetrans":
procNetstat.TcpExt.TCPSlowStartRetrans = value
case "TCPTimeouts":
procNetstat.TcpExt.TCPTimeouts = value
case "TCPLossProbes":
procNetstat.TcpExt.TCPLossProbes = value
case "TCPLossProbeRecovery":
procNetstat.TcpExt.TCPLossProbeRecovery = value
case "TCPRenoRecoveryFail":
procNetstat.TcpExt.TCPRenoRecoveryFail = value
case "TCPSackRecoveryFail":
procNetstat.TcpExt.TCPSackRecoveryFail = value
case "TCPRcvCollapsed":
procNetstat.TcpExt.TCPRcvCollapsed = value
case "TCPDSACKOldSent":
procNetstat.TcpExt.TCPDSACKOldSent = value
case "TCPDSACKOfoSent":
procNetstat.TcpExt.TCPDSACKOfoSent = value
case "TCPDSACKRecv":
procNetstat.TcpExt.TCPDSACKRecv = value
case "TCPDSACKOfoRecv":
procNetstat.TcpExt.TCPDSACKOfoRecv = value
case "TCPAbortOnData":
procNetstat.TcpExt.TCPAbortOnData = value
case "TCPAbortOnClose":
procNetstat.TcpExt.TCPAbortOnClose = value
case "TCPDeferAcceptDrop":
procNetstat.TcpExt.TCPDeferAcceptDrop = value
case "IPReversePathFilter":
procNetstat.TcpExt.IPReversePathFilter = value
case "TCPTimeWaitOverflow":
procNetstat.TcpExt.TCPTimeWaitOverflow = value
case "TCPReqQFullDoCookies":
procNetstat.TcpExt.TCPReqQFullDoCookies = value
case "TCPReqQFullDrop":
procNetstat.TcpExt.TCPReqQFullDrop = value
case "TCPRetransFail":
procNetstat.TcpExt.TCPRetransFail = value
case "TCPRcvCoalesce":
procNetstat.TcpExt.TCPRcvCoalesce = value
case "TCPOFOQueue":
procNetstat.TcpExt.TCPOFOQueue = value
case "TCPOFODrop":
procNetstat.TcpExt.TCPOFODrop = value
case "TCPOFOMerge":
procNetstat.TcpExt.TCPOFOMerge = value
case "TCPChallengeACK":
procNetstat.TcpExt.TCPChallengeACK = value
case "TCPSYNChallenge":
procNetstat.TcpExt.TCPSYNChallenge = value
case "TCPFastOpenActive":
procNetstat.TcpExt.TCPFastOpenActive = value
case "TCPFastOpenActiveFail":
procNetstat.TcpExt.TCPFastOpenActiveFail = value
case "TCPFastOpenPassive":
procNetstat.TcpExt.TCPFastOpenPassive = value
case "TCPFastOpenPassiveFail":
procNetstat.TcpExt.TCPFastOpenPassiveFail = value
case "TCPFastOpenListenOverflow":
procNetstat.TcpExt.TCPFastOpenListenOverflow = value
case "TCPFastOpenCookieReqd":
procNetstat.TcpExt.TCPFastOpenCookieReqd = value
case "TCPFastOpenBlackhole":
procNetstat.TcpExt.TCPFastOpenBlackhole = value
case "TCPSpuriousRtxHostQueues":
procNetstat.TcpExt.TCPSpuriousRtxHostQueues = value
case "BusyPollRxPackets":
procNetstat.TcpExt.BusyPollRxPackets = value
case "TCPAutoCorking":
procNetstat.TcpExt.TCPAutoCorking = value
case "TCPFromZeroWindowAdv":
procNetstat.TcpExt.TCPFromZeroWindowAdv = value
case "TCPToZeroWindowAdv":
procNetstat.TcpExt.TCPToZeroWindowAdv = value
case "TCPWantZeroWindowAdv":
procNetstat.TcpExt.TCPWantZeroWindowAdv = value
case "TCPSynRetrans":
procNetstat.TcpExt.TCPSynRetrans = value
case "TCPOrigDataSent":
procNetstat.TcpExt.TCPOrigDataSent = value
case "TCPHystartTrainDetect":
procNetstat.TcpExt.TCPHystartTrainDetect = value
case "TCPHystartTrainCwnd":
procNetstat.TcpExt.TCPHystartTrainCwnd = value
case "TCPHystartDelayDetect":
procNetstat.TcpExt.TCPHystartDelayDetect = value
case "TCPHystartDelayCwnd":
procNetstat.TcpExt.TCPHystartDelayCwnd = value
case "TCPACKSkippedSynRecv":
procNetstat.TcpExt.TCPACKSkippedSynRecv = value
case "TCPACKSkippedPAWS":
procNetstat.TcpExt.TCPACKSkippedPAWS = value
case "TCPACKSkippedSeq":
procNetstat.TcpExt.TCPACKSkippedSeq = value
case "TCPACKSkippedFinWait2":
procNetstat.TcpExt.TCPACKSkippedFinWait2 = value
case "TCPACKSkippedTimeWait":
procNetstat.TcpExt.TCPACKSkippedTimeWait = value
case "TCPACKSkippedChallenge":
procNetstat.TcpExt.TCPACKSkippedChallenge = value
case "TCPWinProbe":
procNetstat.TcpExt.TCPWinProbe = value
case "TCPKeepAlive":
procNetstat.TcpExt.TCPKeepAlive = value
case "TCPMTUPFail":
procNetstat.TcpExt.TCPMTUPFail = value
case "TCPMTUPSuccess":
procNetstat.TcpExt.TCPMTUPSuccess = value
case "TCPWqueueTooBig":
procNetstat.TcpExt.TCPWqueueTooBig = value
}
case "IpExt":
switch key {
case "InNoRoutes":
procNetstat.IpExt.InNoRoutes = value
case "InTruncatedPkts":
procNetstat.IpExt.InTruncatedPkts = value
case "InMcastPkts":
procNetstat.IpExt.InMcastPkts = value
case "OutMcastPkts":
procNetstat.IpExt.OutMcastPkts = value
case "InBcastPkts":
procNetstat.IpExt.InBcastPkts = value
case "OutBcastPkts":
procNetstat.IpExt.OutBcastPkts = value
case "InOctets":
procNetstat.IpExt.InOctets = value
case "OutOctets":
procNetstat.IpExt.OutOctets = value
case "InMcastOctets":
procNetstat.IpExt.InMcastOctets = value
case "OutMcastOctets":
procNetstat.IpExt.OutMcastOctets = value
case "InBcastOctets":
procNetstat.IpExt.InBcastOctets = value
case "OutBcastOctets":
procNetstat.IpExt.OutBcastOctets = value
case "InCsumErrors":
procNetstat.IpExt.InCsumErrors = value
case "InNoECTPkts":
procNetstat.IpExt.InNoECTPkts = value
case "InECT1Pkts":
procNetstat.IpExt.InECT1Pkts = value
case "InECT0Pkts":
procNetstat.IpExt.InECT0Pkts = value
case "InCEPkts":
procNetstat.IpExt.InCEPkts = value
case "ReasmOverlaps":
procNetstat.IpExt.ReasmOverlaps = value
}
}
}
}

return netStats, scanner.Err()
return procNetstat, scanner.Err()
}
9 changes: 5 additions & 4 deletions proc_netstat_test.go
@@ -1,6 +1,8 @@
package procfs

import "testing"
import (
"testing"
)

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

{name: "IpExt:InNoRoutes", want: 0, have: procNetstat.IpExt.InNoRoutes},
{name: "TcpExt:InMcastPkts", want: 208, have: procNetstat.IpExt.InMcastPkts},
{name: "TcpExt:OutMcastPkts", want: 214, have: procNetstat.IpExt.OutMcastPkts},
{name: "IpExt:InMcastPkts", want: 208, have: procNetstat.IpExt.InMcastPkts},
{name: "IpExt:OutMcastPkts", want: 214, have: procNetstat.IpExt.OutMcastPkts},
} {
if test.want != test.have {
t.Errorf("want %s %f, have %f", test.name, test.want, test.have)
}
}

}

0 comments on commit 5c12586

Please sign in to comment.