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

354 lines
9.2 KiB
Go
Raw Normal View History

2022-08-02 06:19:38 +00: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
2022-12-28 23:00:02 +00:00
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
2022-08-02 06:19:38 +00:00
}
2022-12-28 23:00:02 +00: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
2022-08-02 06:19:38 +00:00
}
type IcmpMsg struct {
2022-12-28 23:00:02 +00:00
InType3 *float64
OutType3 *float64
2022-08-02 06:19:38 +00:00
}
type Tcp struct { // nolint:revive
2022-12-28 23:00:02 +00:00
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
2022-08-02 06:19:38 +00:00
}
type Udp struct { // nolint:revive
2022-12-28 23:00:02 +00:00
InDatagrams *float64
NoPorts *float64
InErrors *float64
OutDatagrams *float64
RcvbufErrors *float64
SndbufErrors *float64
InCsumErrors *float64
IgnoredMulti *float64
2022-08-02 06:19:38 +00:00
}
type UdpLite struct { // nolint:revive
2022-12-28 23:00:02 +00:00
InDatagrams *float64
NoPorts *float64
InErrors *float64
OutDatagrams *float64
RcvbufErrors *float64
SndbufErrors *float64
InCsumErrors *float64
IgnoredMulti *float64
2022-08-02 06:19:38 +00: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("mismatch field count mismatch in %s: %s",
fileName, protocol)
}
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":
2022-12-28 23:00:02 +00:00
procSnmp.Ip.Forwarding = &value
2022-08-02 06:19:38 +00:00
case "DefaultTTL":
2022-12-28 23:00:02 +00:00
procSnmp.Ip.DefaultTTL = &value
2022-08-02 06:19:38 +00:00
case "InReceives":
2022-12-28 23:00:02 +00:00
procSnmp.Ip.InReceives = &value
2022-08-02 06:19:38 +00:00
case "InHdrErrors":
2022-12-28 23:00:02 +00:00
procSnmp.Ip.InHdrErrors = &value
2022-08-02 06:19:38 +00:00
case "InAddrErrors":
2022-12-28 23:00:02 +00:00
procSnmp.Ip.InAddrErrors = &value
2022-08-02 06:19:38 +00:00
case "ForwDatagrams":
2022-12-28 23:00:02 +00:00
procSnmp.Ip.ForwDatagrams = &value
2022-08-02 06:19:38 +00:00
case "InUnknownProtos":
2022-12-28 23:00:02 +00:00
procSnmp.Ip.InUnknownProtos = &value
2022-08-02 06:19:38 +00:00
case "InDiscards":
2022-12-28 23:00:02 +00:00
procSnmp.Ip.InDiscards = &value
2022-08-02 06:19:38 +00:00
case "InDelivers":
2022-12-28 23:00:02 +00:00
procSnmp.Ip.InDelivers = &value
2022-08-02 06:19:38 +00:00
case "OutRequests":
2022-12-28 23:00:02 +00:00
procSnmp.Ip.OutRequests = &value
2022-08-02 06:19:38 +00:00
case "OutDiscards":
2022-12-28 23:00:02 +00:00
procSnmp.Ip.OutDiscards = &value
2022-08-02 06:19:38 +00:00
case "OutNoRoutes":
2022-12-28 23:00:02 +00:00
procSnmp.Ip.OutNoRoutes = &value
2022-08-02 06:19:38 +00:00
case "ReasmTimeout":
2022-12-28 23:00:02 +00:00
procSnmp.Ip.ReasmTimeout = &value
2022-08-02 06:19:38 +00:00
case "ReasmReqds":
2022-12-28 23:00:02 +00:00
procSnmp.Ip.ReasmReqds = &value
2022-08-02 06:19:38 +00:00
case "ReasmOKs":
2022-12-28 23:00:02 +00:00
procSnmp.Ip.ReasmOKs = &value
2022-08-02 06:19:38 +00:00
case "ReasmFails":
2022-12-28 23:00:02 +00:00
procSnmp.Ip.ReasmFails = &value
2022-08-02 06:19:38 +00:00
case "FragOKs":
2022-12-28 23:00:02 +00:00
procSnmp.Ip.FragOKs = &value
2022-08-02 06:19:38 +00:00
case "FragFails":
2022-12-28 23:00:02 +00:00
procSnmp.Ip.FragFails = &value
2022-08-02 06:19:38 +00:00
case "FragCreates":
2022-12-28 23:00:02 +00:00
procSnmp.Ip.FragCreates = &value
2022-08-02 06:19:38 +00:00
}
case "Icmp":
switch key {
case "InMsgs":
2022-12-28 23:00:02 +00:00
procSnmp.Icmp.InMsgs = &value
2022-08-02 06:19:38 +00:00
case "InErrors":
2022-12-28 23:00:02 +00:00
procSnmp.Icmp.InErrors = &value
2022-08-02 06:19:38 +00:00
case "InCsumErrors":
2022-12-28 23:00:02 +00:00
procSnmp.Icmp.InCsumErrors = &value
2022-08-02 06:19:38 +00:00
case "InDestUnreachs":
2022-12-28 23:00:02 +00:00
procSnmp.Icmp.InDestUnreachs = &value
2022-08-02 06:19:38 +00:00
case "InTimeExcds":
2022-12-28 23:00:02 +00:00
procSnmp.Icmp.InTimeExcds = &value
2022-08-02 06:19:38 +00:00
case "InParmProbs":
2022-12-28 23:00:02 +00:00
procSnmp.Icmp.InParmProbs = &value
2022-08-02 06:19:38 +00:00
case "InSrcQuenchs":
2022-12-28 23:00:02 +00:00
procSnmp.Icmp.InSrcQuenchs = &value
2022-08-02 06:19:38 +00:00
case "InRedirects":
2022-12-28 23:00:02 +00:00
procSnmp.Icmp.InRedirects = &value
2022-08-02 06:19:38 +00:00
case "InEchos":
2022-12-28 23:00:02 +00:00
procSnmp.Icmp.InEchos = &value
2022-08-02 06:19:38 +00:00
case "InEchoReps":
2022-12-28 23:00:02 +00:00
procSnmp.Icmp.InEchoReps = &value
2022-08-02 06:19:38 +00:00
case "InTimestamps":
2022-12-28 23:00:02 +00:00
procSnmp.Icmp.InTimestamps = &value
2022-08-02 06:19:38 +00:00
case "InTimestampReps":
2022-12-28 23:00:02 +00:00
procSnmp.Icmp.InTimestampReps = &value
2022-08-02 06:19:38 +00:00
case "InAddrMasks":
2022-12-28 23:00:02 +00:00
procSnmp.Icmp.InAddrMasks = &value
2022-08-02 06:19:38 +00:00
case "InAddrMaskReps":
2022-12-28 23:00:02 +00:00
procSnmp.Icmp.InAddrMaskReps = &value
2022-08-02 06:19:38 +00:00
case "OutMsgs":
2022-12-28 23:00:02 +00:00
procSnmp.Icmp.OutMsgs = &value
2022-08-02 06:19:38 +00:00
case "OutErrors":
2022-12-28 23:00:02 +00:00
procSnmp.Icmp.OutErrors = &value
2022-08-02 06:19:38 +00:00
case "OutDestUnreachs":
2022-12-28 23:00:02 +00:00
procSnmp.Icmp.OutDestUnreachs = &value
2022-08-02 06:19:38 +00:00
case "OutTimeExcds":
2022-12-28 23:00:02 +00:00
procSnmp.Icmp.OutTimeExcds = &value
2022-08-02 06:19:38 +00:00
case "OutParmProbs":
2022-12-28 23:00:02 +00:00
procSnmp.Icmp.OutParmProbs = &value
2022-08-02 06:19:38 +00:00
case "OutSrcQuenchs":
2022-12-28 23:00:02 +00:00
procSnmp.Icmp.OutSrcQuenchs = &value
2022-08-02 06:19:38 +00:00
case "OutRedirects":
2022-12-28 23:00:02 +00:00
procSnmp.Icmp.OutRedirects = &value
2022-08-02 06:19:38 +00:00
case "OutEchos":
2022-12-28 23:00:02 +00:00
procSnmp.Icmp.OutEchos = &value
2022-08-02 06:19:38 +00:00
case "OutEchoReps":
2022-12-28 23:00:02 +00:00
procSnmp.Icmp.OutEchoReps = &value
2022-08-02 06:19:38 +00:00
case "OutTimestamps":
2022-12-28 23:00:02 +00:00
procSnmp.Icmp.OutTimestamps = &value
2022-08-02 06:19:38 +00:00
case "OutTimestampReps":
2022-12-28 23:00:02 +00:00
procSnmp.Icmp.OutTimestampReps = &value
2022-08-02 06:19:38 +00:00
case "OutAddrMasks":
2022-12-28 23:00:02 +00:00
procSnmp.Icmp.OutAddrMasks = &value
2022-08-02 06:19:38 +00:00
case "OutAddrMaskReps":
2022-12-28 23:00:02 +00:00
procSnmp.Icmp.OutAddrMaskReps = &value
2022-08-02 06:19:38 +00:00
}
case "IcmpMsg":
switch key {
case "InType3":
2022-12-28 23:00:02 +00:00
procSnmp.IcmpMsg.InType3 = &value
2022-08-02 06:19:38 +00:00
case "OutType3":
2022-12-28 23:00:02 +00:00
procSnmp.IcmpMsg.OutType3 = &value
2022-08-02 06:19:38 +00:00
}
case "Tcp":
switch key {
case "RtoAlgorithm":
2022-12-28 23:00:02 +00:00
procSnmp.Tcp.RtoAlgorithm = &value
2022-08-02 06:19:38 +00:00
case "RtoMin":
2022-12-28 23:00:02 +00:00
procSnmp.Tcp.RtoMin = &value
2022-08-02 06:19:38 +00:00
case "RtoMax":
2022-12-28 23:00:02 +00:00
procSnmp.Tcp.RtoMax = &value
2022-08-02 06:19:38 +00:00
case "MaxConn":
2022-12-28 23:00:02 +00:00
procSnmp.Tcp.MaxConn = &value
2022-08-02 06:19:38 +00:00
case "ActiveOpens":
2022-12-28 23:00:02 +00:00
procSnmp.Tcp.ActiveOpens = &value
2022-08-02 06:19:38 +00:00
case "PassiveOpens":
2022-12-28 23:00:02 +00:00
procSnmp.Tcp.PassiveOpens = &value
2022-08-02 06:19:38 +00:00
case "AttemptFails":
2022-12-28 23:00:02 +00:00
procSnmp.Tcp.AttemptFails = &value
2022-08-02 06:19:38 +00:00
case "EstabResets":
2022-12-28 23:00:02 +00:00
procSnmp.Tcp.EstabResets = &value
2022-08-02 06:19:38 +00:00
case "CurrEstab":
2022-12-28 23:00:02 +00:00
procSnmp.Tcp.CurrEstab = &value
2022-08-02 06:19:38 +00:00
case "InSegs":
2022-12-28 23:00:02 +00:00
procSnmp.Tcp.InSegs = &value
2022-08-02 06:19:38 +00:00
case "OutSegs":
2022-12-28 23:00:02 +00:00
procSnmp.Tcp.OutSegs = &value
2022-08-02 06:19:38 +00:00
case "RetransSegs":
2022-12-28 23:00:02 +00:00
procSnmp.Tcp.RetransSegs = &value
2022-08-02 06:19:38 +00:00
case "InErrs":
2022-12-28 23:00:02 +00:00
procSnmp.Tcp.InErrs = &value
2022-08-02 06:19:38 +00:00
case "OutRsts":
2022-12-28 23:00:02 +00:00
procSnmp.Tcp.OutRsts = &value
2022-08-02 06:19:38 +00:00
case "InCsumErrors":
2022-12-28 23:00:02 +00:00
procSnmp.Tcp.InCsumErrors = &value
2022-08-02 06:19:38 +00:00
}
case "Udp":
switch key {
case "InDatagrams":
2022-12-28 23:00:02 +00:00
procSnmp.Udp.InDatagrams = &value
2022-08-02 06:19:38 +00:00
case "NoPorts":
2022-12-28 23:00:02 +00:00
procSnmp.Udp.NoPorts = &value
2022-08-02 06:19:38 +00:00
case "InErrors":
2022-12-28 23:00:02 +00:00
procSnmp.Udp.InErrors = &value
2022-08-02 06:19:38 +00:00
case "OutDatagrams":
2022-12-28 23:00:02 +00:00
procSnmp.Udp.OutDatagrams = &value
2022-08-02 06:19:38 +00:00
case "RcvbufErrors":
2022-12-28 23:00:02 +00:00
procSnmp.Udp.RcvbufErrors = &value
2022-08-02 06:19:38 +00:00
case "SndbufErrors":
2022-12-28 23:00:02 +00:00
procSnmp.Udp.SndbufErrors = &value
2022-08-02 06:19:38 +00:00
case "InCsumErrors":
2022-12-28 23:00:02 +00:00
procSnmp.Udp.InCsumErrors = &value
2022-08-02 06:19:38 +00:00
case "IgnoredMulti":
2022-12-28 23:00:02 +00:00
procSnmp.Udp.IgnoredMulti = &value
2022-08-02 06:19:38 +00:00
}
case "UdpLite":
switch key {
case "InDatagrams":
2022-12-28 23:00:02 +00:00
procSnmp.UdpLite.InDatagrams = &value
2022-08-02 06:19:38 +00:00
case "NoPorts":
2022-12-28 23:00:02 +00:00
procSnmp.UdpLite.NoPorts = &value
2022-08-02 06:19:38 +00:00
case "InErrors":
2022-12-28 23:00:02 +00:00
procSnmp.UdpLite.InErrors = &value
2022-08-02 06:19:38 +00:00
case "OutDatagrams":
2022-12-28 23:00:02 +00:00
procSnmp.UdpLite.OutDatagrams = &value
2022-08-02 06:19:38 +00:00
case "RcvbufErrors":
2022-12-28 23:00:02 +00:00
procSnmp.UdpLite.RcvbufErrors = &value
2022-08-02 06:19:38 +00:00
case "SndbufErrors":
2022-12-28 23:00:02 +00:00
procSnmp.UdpLite.SndbufErrors = &value
2022-08-02 06:19:38 +00:00
case "InCsumErrors":
2022-12-28 23:00:02 +00:00
procSnmp.UdpLite.InCsumErrors = &value
2022-08-02 06:19:38 +00:00
case "IgnoredMulti":
2022-12-28 23:00:02 +00:00
procSnmp.UdpLite.IgnoredMulti = &value
2022-08-02 06:19:38 +00:00
}
}
}
}
return procSnmp, scanner.Err()
}