cubefs
205 строк · 6.4 Кб
1// Copyright 2018 The Prometheus Authors
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6// http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
14package procfs15
16import (17"bufio"18"errors"19"os"20"sort"21"strconv"22"strings"23)
24
25// NetDevLine is single line parsed from /proc/net/dev or /proc/[pid]/net/dev.
26type NetDevLine struct {27Name string `json:"name"` // The name of the interface.28RxBytes uint64 `json:"rx_bytes"` // Cumulative count of bytes received.29RxPackets uint64 `json:"rx_packets"` // Cumulative count of packets received.30RxErrors uint64 `json:"rx_errors"` // Cumulative count of receive errors encountered.31RxDropped uint64 `json:"rx_dropped"` // Cumulative count of packets dropped while receiving.32RxFIFO uint64 `json:"rx_fifo"` // Cumulative count of FIFO buffer errors.33RxFrame uint64 `json:"rx_frame"` // Cumulative count of packet framing errors.34RxCompressed uint64 `json:"rx_compressed"` // Cumulative count of compressed packets received by the device driver.35RxMulticast uint64 `json:"rx_multicast"` // Cumulative count of multicast frames received by the device driver.36TxBytes uint64 `json:"tx_bytes"` // Cumulative count of bytes transmitted.37TxPackets uint64 `json:"tx_packets"` // Cumulative count of packets transmitted.38TxErrors uint64 `json:"tx_errors"` // Cumulative count of transmit errors encountered.39TxDropped uint64 `json:"tx_dropped"` // Cumulative count of packets dropped while transmitting.40TxFIFO uint64 `json:"tx_fifo"` // Cumulative count of FIFO buffer errors.41TxCollisions uint64 `json:"tx_collisions"` // Cumulative count of collisions detected on the interface.42TxCarrier uint64 `json:"tx_carrier"` // Cumulative count of carrier losses detected by the device driver.43TxCompressed uint64 `json:"tx_compressed"` // Cumulative count of compressed packets transmitted by the device driver.44}
45
46// NetDev is parsed from /proc/net/dev or /proc/[pid]/net/dev. The map keys
47// are interface names.
48type NetDev map[string]NetDevLine49
50// NetDev returns kernel/system statistics read from /proc/net/dev.
51func (fs FS) NetDev() (NetDev, error) {52return newNetDev(fs.proc.Path("net/dev"))53}
54
55// NetDev returns kernel/system statistics read from /proc/[pid]/net/dev.
56func (p Proc) NetDev() (NetDev, error) {57return newNetDev(p.path("net/dev"))58}
59
60// newNetDev creates a new NetDev from the contents of the given file.
61func newNetDev(file string) (NetDev, error) {62f, err := os.Open(file)63if err != nil {64return NetDev{}, err65}66defer f.Close()67
68netDev := NetDev{}69s := bufio.NewScanner(f)70for n := 0; s.Scan(); n++ {71// Skip the 2 header lines.72if n < 2 {73continue74}75
76line, err := netDev.parseLine(s.Text())77if err != nil {78return netDev, err79}80
81netDev[line.Name] = *line82}83
84return netDev, s.Err()85}
86
87// parseLine parses a single line from the /proc/net/dev file. Header lines
88// must be filtered prior to calling this method.
89func (netDev NetDev) parseLine(rawLine string) (*NetDevLine, error) {90idx := strings.LastIndex(rawLine, ":")91if idx == -1 {92return nil, errors.New("invalid net/dev line, missing colon")93}94fields := strings.Fields(strings.TrimSpace(rawLine[idx+1:]))95
96var err error97line := &NetDevLine{}98
99// Interface Name100line.Name = strings.TrimSpace(rawLine[:idx])101if line.Name == "" {102return nil, errors.New("invalid net/dev line, empty interface name")103}104
105// RX106line.RxBytes, err = strconv.ParseUint(fields[0], 10, 64)107if err != nil {108return nil, err109}110line.RxPackets, err = strconv.ParseUint(fields[1], 10, 64)111if err != nil {112return nil, err113}114line.RxErrors, err = strconv.ParseUint(fields[2], 10, 64)115if err != nil {116return nil, err117}118line.RxDropped, err = strconv.ParseUint(fields[3], 10, 64)119if err != nil {120return nil, err121}122line.RxFIFO, err = strconv.ParseUint(fields[4], 10, 64)123if err != nil {124return nil, err125}126line.RxFrame, err = strconv.ParseUint(fields[5], 10, 64)127if err != nil {128return nil, err129}130line.RxCompressed, err = strconv.ParseUint(fields[6], 10, 64)131if err != nil {132return nil, err133}134line.RxMulticast, err = strconv.ParseUint(fields[7], 10, 64)135if err != nil {136return nil, err137}138
139// TX140line.TxBytes, err = strconv.ParseUint(fields[8], 10, 64)141if err != nil {142return nil, err143}144line.TxPackets, err = strconv.ParseUint(fields[9], 10, 64)145if err != nil {146return nil, err147}148line.TxErrors, err = strconv.ParseUint(fields[10], 10, 64)149if err != nil {150return nil, err151}152line.TxDropped, err = strconv.ParseUint(fields[11], 10, 64)153if err != nil {154return nil, err155}156line.TxFIFO, err = strconv.ParseUint(fields[12], 10, 64)157if err != nil {158return nil, err159}160line.TxCollisions, err = strconv.ParseUint(fields[13], 10, 64)161if err != nil {162return nil, err163}164line.TxCarrier, err = strconv.ParseUint(fields[14], 10, 64)165if err != nil {166return nil, err167}168line.TxCompressed, err = strconv.ParseUint(fields[15], 10, 64)169if err != nil {170return nil, err171}172
173return line, nil174}
175
176// Total aggregates the values across interfaces and returns a new NetDevLine.
177// The Name field will be a sorted comma separated list of interface names.
178func (netDev NetDev) Total() NetDevLine {179total := NetDevLine{}180
181names := make([]string, 0, len(netDev))182for _, ifc := range netDev {183names = append(names, ifc.Name)184total.RxBytes += ifc.RxBytes185total.RxPackets += ifc.RxPackets186total.RxErrors += ifc.RxErrors187total.RxDropped += ifc.RxDropped188total.RxFIFO += ifc.RxFIFO189total.RxFrame += ifc.RxFrame190total.RxCompressed += ifc.RxCompressed191total.RxMulticast += ifc.RxMulticast192total.TxBytes += ifc.TxBytes193total.TxPackets += ifc.TxPackets194total.TxErrors += ifc.TxErrors195total.TxDropped += ifc.TxDropped196total.TxFIFO += ifc.TxFIFO197total.TxCollisions += ifc.TxCollisions198total.TxCarrier += ifc.TxCarrier199total.TxCompressed += ifc.TxCompressed200}201sort.Strings(names)202total.Name = strings.Join(names, ", ")203
204return total205}
206