// Copyright 2018 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 procfsimport ()// NetDevLine is single line parsed from /proc/net/dev or /proc/[pid]/net/dev.typeNetDevLinestruct { Name string`json:"name"`// The name of the interface. RxBytes uint64`json:"rx_bytes"`// Cumulative count of bytes received. RxPackets uint64`json:"rx_packets"`// Cumulative count of packets received. RxErrors uint64`json:"rx_errors"`// Cumulative count of receive errors encountered. RxDropped uint64`json:"rx_dropped"`// Cumulative count of packets dropped while receiving. RxFIFO uint64`json:"rx_fifo"`// Cumulative count of FIFO buffer errors. RxFrame uint64`json:"rx_frame"`// Cumulative count of packet framing errors. RxCompressed uint64`json:"rx_compressed"`// Cumulative count of compressed packets received by the device driver. RxMulticast uint64`json:"rx_multicast"`// Cumulative count of multicast frames received by the device driver. TxBytes uint64`json:"tx_bytes"`// Cumulative count of bytes transmitted. TxPackets uint64`json:"tx_packets"`// Cumulative count of packets transmitted. TxErrors uint64`json:"tx_errors"`// Cumulative count of transmit errors encountered. TxDropped uint64`json:"tx_dropped"`// Cumulative count of packets dropped while transmitting. TxFIFO uint64`json:"tx_fifo"`// Cumulative count of FIFO buffer errors. TxCollisions uint64`json:"tx_collisions"`// Cumulative count of collisions detected on the interface. TxCarrier uint64`json:"tx_carrier"`// Cumulative count of carrier losses detected by the device driver. TxCompressed uint64`json:"tx_compressed"`// Cumulative count of compressed packets transmitted by the device driver.}// NetDev is parsed from /proc/net/dev or /proc/[pid]/net/dev. The map keys// are interface names.typeNetDevmap[string]NetDevLine// NetDev returns kernel/system statistics read from /proc/net/dev.func ( FS) () (NetDev, error) {returnnewNetDev(.proc.Path("net/dev"))}// NetDev returns kernel/system statistics read from /proc/[pid]/net/dev.func ( Proc) () (NetDev, error) {returnnewNetDev(.path("net/dev"))}// newNetDev creates a new NetDev from the contents of the given file.func newNetDev( string) (NetDev, error) { , := os.Open()if != nil {returnNetDev{}, }defer .Close() := NetDev{} := bufio.NewScanner()for := 0; .Scan(); ++ {// Skip the 2 header lines.if < 2 {continue } , := .parseLine(.Text())if != nil {return , } [.Name] = * }return , .Err()}// parseLine parses a single line from the /proc/net/dev file. Header lines// must be filtered prior to calling this method.func ( NetDev) ( string) (*NetDevLine, error) { := strings.LastIndex(, ":")if == -1 {returnnil, errors.New("invalid net/dev line, missing colon") } := strings.Fields(strings.TrimSpace([+1:]))varerror := &NetDevLine{}// Interface Name .Name = strings.TrimSpace([:])if .Name == "" {returnnil, errors.New("invalid net/dev line, empty interface name") }// RX .RxBytes, = strconv.ParseUint([0], 10, 64)if != nil {returnnil, } .RxPackets, = strconv.ParseUint([1], 10, 64)if != nil {returnnil, } .RxErrors, = strconv.ParseUint([2], 10, 64)if != nil {returnnil, } .RxDropped, = strconv.ParseUint([3], 10, 64)if != nil {returnnil, } .RxFIFO, = strconv.ParseUint([4], 10, 64)if != nil {returnnil, } .RxFrame, = strconv.ParseUint([5], 10, 64)if != nil {returnnil, } .RxCompressed, = strconv.ParseUint([6], 10, 64)if != nil {returnnil, } .RxMulticast, = strconv.ParseUint([7], 10, 64)if != nil {returnnil, }// TX .TxBytes, = strconv.ParseUint([8], 10, 64)if != nil {returnnil, } .TxPackets, = strconv.ParseUint([9], 10, 64)if != nil {returnnil, } .TxErrors, = strconv.ParseUint([10], 10, 64)if != nil {returnnil, } .TxDropped, = strconv.ParseUint([11], 10, 64)if != nil {returnnil, } .TxFIFO, = strconv.ParseUint([12], 10, 64)if != nil {returnnil, } .TxCollisions, = strconv.ParseUint([13], 10, 64)if != nil {returnnil, } .TxCarrier, = strconv.ParseUint([14], 10, 64)if != nil {returnnil, } .TxCompressed, = strconv.ParseUint([15], 10, 64)if != nil {returnnil, }return , nil}// Total aggregates the values across interfaces and returns a new NetDevLine.// The Name field will be a sorted comma separated list of interface names.func ( NetDev) () NetDevLine { := NetDevLine{} := make([]string, 0, len())for , := range { = append(, .Name) .RxBytes += .RxBytes .RxPackets += .RxPackets .RxErrors += .RxErrors .RxDropped += .RxDropped .RxFIFO += .RxFIFO .RxFrame += .RxFrame .RxCompressed += .RxCompressed .RxMulticast += .RxMulticast .TxBytes += .TxBytes .TxPackets += .TxPackets .TxErrors += .TxErrors .TxDropped += .TxDropped .TxFIFO += .TxFIFO .TxCollisions += .TxCollisions .TxCarrier += .TxCarrier .TxCompressed += .TxCompressed }sort.Strings() .Name = strings.Join(, ", ")return}
The pages are generated with Goldsv0.8.2. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.