mox/vendor/github.com/prometheus/procfs/proc_snmp.go

354 lines
9.2 KiB
Go
Raw Normal View History

2023-01-30 16:27:06 +03:00
// Copyright 2022 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package procfs
import (
"bufio"
"bytes"
"fmt"
"io"
"strconv"
"strings"
"github.com/prometheus/procfs/internal/util"
)
// ProcSnmp models the content of /proc/<pid>/net/snmp.
type ProcSnmp struct {
// The process ID.
PID int
Ip
Icmp
IcmpMsg
Tcp
Udp
UdpLite
}
type Ip struct { // nolint:revive
Forwarding *float64
DefaultTTL *float64
InReceives *float64
InHdrErrors *float64
InAddrErrors *float64
ForwDatagrams *float64
InUnknownProtos *float64
InDiscards *float64
InDelivers *float64
OutRequests *float64
OutDiscards *float64
OutNoRoutes *float64
ReasmTimeout *float64
ReasmReqds *float64
ReasmOKs *float64
ReasmFails *float64
FragOKs *float64
FragFails *float64
FragCreates *float64
2023-01-30 16:27:06 +03:00
}
type Icmp struct { // nolint:revive
InMsgs *float64
InErrors *float64
InCsumErrors *float64
InDestUnreachs *float64
InTimeExcds *float64
InParmProbs *float64
InSrcQuenchs *float64
InRedirects *float64
InEchos *float64
InEchoReps *float64
InTimestamps *float64
InTimestampReps *float64
InAddrMasks *float64
InAddrMaskReps *float64
OutMsgs *float64
OutErrors *float64
OutDestUnreachs *float64
OutTimeExcds *float64
OutParmProbs *float64
OutSrcQuenchs *float64
OutRedirects *float64
OutEchos *float64
OutEchoReps *float64
OutTimestamps *float64
OutTimestampReps *float64
OutAddrMasks *float64
OutAddrMaskReps *float64
2023-01-30 16:27:06 +03:00
}
type IcmpMsg struct {
InType3 *float64
OutType3 *float64
2023-01-30 16:27:06 +03:00
}
type Tcp struct { // nolint:revive
RtoAlgorithm *float64
RtoMin *float64
RtoMax *float64
MaxConn *float64
ActiveOpens *float64
PassiveOpens *float64
AttemptFails *float64
EstabResets *float64
CurrEstab *float64
InSegs *float64
OutSegs *float64
RetransSegs *float64
InErrs *float64
OutRsts *float64
InCsumErrors *float64
2023-01-30 16:27:06 +03:00
}
type Udp struct { // nolint:revive
InDatagrams *float64
NoPorts *float64
InErrors *float64
OutDatagrams *float64
RcvbufErrors *float64
SndbufErrors *float64
InCsumErrors *float64
IgnoredMulti *float64
2023-01-30 16:27:06 +03:00
}
type UdpLite struct { // nolint:revive
InDatagrams *float64
NoPorts *float64
InErrors *float64
OutDatagrams *float64
RcvbufErrors *float64
SndbufErrors *float64
InCsumErrors *float64
IgnoredMulti *float64
2023-01-30 16:27:06 +03:00
}
func (p Proc) Snmp() (ProcSnmp, error) {
filename := p.path("net/snmp")
data, err := util.ReadFileNoStat(filename)
if err != nil {
return ProcSnmp{PID: p.PID}, err
}
procSnmp, err := parseSnmp(bytes.NewReader(data), filename)
procSnmp.PID = p.PID
return procSnmp, err
}
// parseSnmp parses the metrics from proc/<pid>/net/snmp file
// and returns a map contains those metrics (e.g. {"Ip": {"Forwarding": 2}}).
func parseSnmp(r io.Reader, fileName string) (ProcSnmp, error) {
var (
scanner = bufio.NewScanner(r)
procSnmp = ProcSnmp{}
)
for scanner.Scan() {
nameParts := strings.Split(scanner.Text(), " ")
scanner.Scan()
valueParts := strings.Split(scanner.Text(), " ")
// Remove trailing :.
protocol := strings.TrimSuffix(nameParts[0], ":")
if len(nameParts) != len(valueParts) {
return procSnmp, fmt.Errorf("%w: mismatch field count mismatch in %s: %s",
ErrFileParse, fileName, protocol)
2023-01-30 16:27:06 +03:00
}
for i := 1; i < len(nameParts); i++ {
value, err := strconv.ParseFloat(valueParts[i], 64)
if err != nil {
return procSnmp, err
}
key := nameParts[i]
switch protocol {
case "Ip":
switch key {
case "Forwarding":
procSnmp.Ip.Forwarding = &value
2023-01-30 16:27:06 +03:00
case "DefaultTTL":
procSnmp.Ip.DefaultTTL = &value
2023-01-30 16:27:06 +03:00
case "InReceives":
procSnmp.Ip.InReceives = &value
2023-01-30 16:27:06 +03:00
case "InHdrErrors":
procSnmp.Ip.InHdrErrors = &value
2023-01-30 16:27:06 +03:00
case "InAddrErrors":
procSnmp.Ip.InAddrErrors = &value
2023-01-30 16:27:06 +03:00
case "ForwDatagrams":
procSnmp.Ip.ForwDatagrams = &value
2023-01-30 16:27:06 +03:00
case "InUnknownProtos":
procSnmp.Ip.InUnknownProtos = &value
2023-01-30 16:27:06 +03:00
case "InDiscards":
procSnmp.Ip.InDiscards = &value
2023-01-30 16:27:06 +03:00
case "InDelivers":
procSnmp.Ip.InDelivers = &value
2023-01-30 16:27:06 +03:00
case "OutRequests":
procSnmp.Ip.OutRequests = &value
2023-01-30 16:27:06 +03:00
case "OutDiscards":
procSnmp.Ip.OutDiscards = &value
2023-01-30 16:27:06 +03:00
case "OutNoRoutes":
procSnmp.Ip.OutNoRoutes = &value
2023-01-30 16:27:06 +03:00
case "ReasmTimeout":
procSnmp.Ip.ReasmTimeout = &value
2023-01-30 16:27:06 +03:00
case "ReasmReqds":
procSnmp.Ip.ReasmReqds = &value
2023-01-30 16:27:06 +03:00
case "ReasmOKs":
procSnmp.Ip.ReasmOKs = &value
2023-01-30 16:27:06 +03:00
case "ReasmFails":
procSnmp.Ip.ReasmFails = &value
2023-01-30 16:27:06 +03:00
case "FragOKs":
procSnmp.Ip.FragOKs = &value
2023-01-30 16:27:06 +03:00
case "FragFails":
procSnmp.Ip.FragFails = &value
2023-01-30 16:27:06 +03:00
case "FragCreates":
procSnmp.Ip.FragCreates = &value
2023-01-30 16:27:06 +03:00
}
case "Icmp":
switch key {
case "InMsgs":
procSnmp.Icmp.InMsgs = &value
2023-01-30 16:27:06 +03:00
case "InErrors":
procSnmp.Icmp.InErrors = &value
2023-01-30 16:27:06 +03:00
case "InCsumErrors":
procSnmp.Icmp.InCsumErrors = &value
2023-01-30 16:27:06 +03:00
case "InDestUnreachs":
procSnmp.Icmp.InDestUnreachs = &value
2023-01-30 16:27:06 +03:00
case "InTimeExcds":
procSnmp.Icmp.InTimeExcds = &value
2023-01-30 16:27:06 +03:00
case "InParmProbs":
procSnmp.Icmp.InParmProbs = &value
2023-01-30 16:27:06 +03:00
case "InSrcQuenchs":
procSnmp.Icmp.InSrcQuenchs = &value
2023-01-30 16:27:06 +03:00
case "InRedirects":
procSnmp.Icmp.InRedirects = &value
2023-01-30 16:27:06 +03:00
case "InEchos":
procSnmp.Icmp.InEchos = &value
2023-01-30 16:27:06 +03:00
case "InEchoReps":
procSnmp.Icmp.InEchoReps = &value
2023-01-30 16:27:06 +03:00
case "InTimestamps":
procSnmp.Icmp.InTimestamps = &value
2023-01-30 16:27:06 +03:00
case "InTimestampReps":
procSnmp.Icmp.InTimestampReps = &value
2023-01-30 16:27:06 +03:00
case "InAddrMasks":
procSnmp.Icmp.InAddrMasks = &value
2023-01-30 16:27:06 +03:00
case "InAddrMaskReps":
procSnmp.Icmp.InAddrMaskReps = &value
2023-01-30 16:27:06 +03:00
case "OutMsgs":
procSnmp.Icmp.OutMsgs = &value
2023-01-30 16:27:06 +03:00
case "OutErrors":
procSnmp.Icmp.OutErrors = &value
2023-01-30 16:27:06 +03:00
case "OutDestUnreachs":
procSnmp.Icmp.OutDestUnreachs = &value
2023-01-30 16:27:06 +03:00
case "OutTimeExcds":
procSnmp.Icmp.OutTimeExcds = &value
2023-01-30 16:27:06 +03:00
case "OutParmProbs":
procSnmp.Icmp.OutParmProbs = &value
2023-01-30 16:27:06 +03:00
case "OutSrcQuenchs":
procSnmp.Icmp.OutSrcQuenchs = &value
2023-01-30 16:27:06 +03:00
case "OutRedirects":
procSnmp.Icmp.OutRedirects = &value
2023-01-30 16:27:06 +03:00
case "OutEchos":
procSnmp.Icmp.OutEchos = &value
2023-01-30 16:27:06 +03:00
case "OutEchoReps":
procSnmp.Icmp.OutEchoReps = &value
2023-01-30 16:27:06 +03:00
case "OutTimestamps":
procSnmp.Icmp.OutTimestamps = &value
2023-01-30 16:27:06 +03:00
case "OutTimestampReps":
procSnmp.Icmp.OutTimestampReps = &value
2023-01-30 16:27:06 +03:00
case "OutAddrMasks":
procSnmp.Icmp.OutAddrMasks = &value
2023-01-30 16:27:06 +03:00
case "OutAddrMaskReps":
procSnmp.Icmp.OutAddrMaskReps = &value
2023-01-30 16:27:06 +03:00
}
case "IcmpMsg":
switch key {
case "InType3":
procSnmp.IcmpMsg.InType3 = &value
2023-01-30 16:27:06 +03:00
case "OutType3":
procSnmp.IcmpMsg.OutType3 = &value
2023-01-30 16:27:06 +03:00
}
case "Tcp":
switch key {
case "RtoAlgorithm":
procSnmp.Tcp.RtoAlgorithm = &value
2023-01-30 16:27:06 +03:00
case "RtoMin":
procSnmp.Tcp.RtoMin = &value
2023-01-30 16:27:06 +03:00
case "RtoMax":
procSnmp.Tcp.RtoMax = &value
2023-01-30 16:27:06 +03:00
case "MaxConn":
procSnmp.Tcp.MaxConn = &value
2023-01-30 16:27:06 +03:00
case "ActiveOpens":
procSnmp.Tcp.ActiveOpens = &value
2023-01-30 16:27:06 +03:00
case "PassiveOpens":
procSnmp.Tcp.PassiveOpens = &value
2023-01-30 16:27:06 +03:00
case "AttemptFails":
procSnmp.Tcp.AttemptFails = &value
2023-01-30 16:27:06 +03:00
case "EstabResets":
procSnmp.Tcp.EstabResets = &value
2023-01-30 16:27:06 +03:00
case "CurrEstab":
procSnmp.Tcp.CurrEstab = &value
2023-01-30 16:27:06 +03:00
case "InSegs":
procSnmp.Tcp.InSegs = &value
2023-01-30 16:27:06 +03:00
case "OutSegs":
procSnmp.Tcp.OutSegs = &value
2023-01-30 16:27:06 +03:00
case "RetransSegs":
procSnmp.Tcp.RetransSegs = &value
2023-01-30 16:27:06 +03:00
case "InErrs":
procSnmp.Tcp.InErrs = &value
2023-01-30 16:27:06 +03:00
case "OutRsts":
procSnmp.Tcp.OutRsts = &value
2023-01-30 16:27:06 +03:00
case "InCsumErrors":
procSnmp.Tcp.InCsumErrors = &value
2023-01-30 16:27:06 +03:00
}
case "Udp":
switch key {
case "InDatagrams":
procSnmp.Udp.InDatagrams = &value
2023-01-30 16:27:06 +03:00
case "NoPorts":
procSnmp.Udp.NoPorts = &value
2023-01-30 16:27:06 +03:00
case "InErrors":
procSnmp.Udp.InErrors = &value
2023-01-30 16:27:06 +03:00
case "OutDatagrams":
procSnmp.Udp.OutDatagrams = &value
2023-01-30 16:27:06 +03:00
case "RcvbufErrors":
procSnmp.Udp.RcvbufErrors = &value
2023-01-30 16:27:06 +03:00
case "SndbufErrors":
procSnmp.Udp.SndbufErrors = &value
2023-01-30 16:27:06 +03:00
case "InCsumErrors":
procSnmp.Udp.InCsumErrors = &value
2023-01-30 16:27:06 +03:00
case "IgnoredMulti":
procSnmp.Udp.IgnoredMulti = &value
2023-01-30 16:27:06 +03:00
}
case "UdpLite":
switch key {
case "InDatagrams":
procSnmp.UdpLite.InDatagrams = &value
2023-01-30 16:27:06 +03:00
case "NoPorts":
procSnmp.UdpLite.NoPorts = &value
2023-01-30 16:27:06 +03:00
case "InErrors":
procSnmp.UdpLite.InErrors = &value
2023-01-30 16:27:06 +03:00
case "OutDatagrams":
procSnmp.UdpLite.OutDatagrams = &value
2023-01-30 16:27:06 +03:00
case "RcvbufErrors":
procSnmp.UdpLite.RcvbufErrors = &value
2023-01-30 16:27:06 +03:00
case "SndbufErrors":
procSnmp.UdpLite.SndbufErrors = &value
2023-01-30 16:27:06 +03:00
case "InCsumErrors":
procSnmp.UdpLite.InCsumErrors = &value
2023-01-30 16:27:06 +03:00
case "IgnoredMulti":
procSnmp.UdpLite.IgnoredMulti = &value
2023-01-30 16:27:06 +03:00
}
}
}
}
return procSnmp, scanner.Err()
}