// 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 procfs

import (
	
	
	
	
	
	
)

// NetDevLine is single line parsed from /proc/net/dev or /proc/[pid]/net/dev.
type NetDevLine struct {
	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.
type NetDev map[string]NetDevLine

// NetDev returns kernel/system statistics read from /proc/net/dev.
func ( FS) () (NetDev, error) {
	return newNetDev(.proc.Path("net/dev"))
}

// NetDev returns kernel/system statistics read from /proc/[pid]/net/dev.
func ( Proc) () (NetDev, error) {
	return newNetDev(.path("net/dev"))
}

// newNetDev creates a new NetDev from the contents of the given file.
func newNetDev( string) (NetDev, error) {
	,  := os.Open()
	if  != nil {
		return NetDev{}, 
	}
	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 {
		return nil, errors.New("invalid net/dev line, missing colon")
	}
	 := strings.Fields(strings.TrimSpace([+1:]))

	var  error
	 := &NetDevLine{}

	// Interface Name
	.Name = strings.TrimSpace([:])
	if .Name == "" {
		return nil, errors.New("invalid net/dev line, empty interface name")
	}

	// RX
	.RxBytes,  = strconv.ParseUint([0], 10, 64)
	if  != nil {
		return nil, 
	}
	.RxPackets,  = strconv.ParseUint([1], 10, 64)
	if  != nil {
		return nil, 
	}
	.RxErrors,  = strconv.ParseUint([2], 10, 64)
	if  != nil {
		return nil, 
	}
	.RxDropped,  = strconv.ParseUint([3], 10, 64)
	if  != nil {
		return nil, 
	}
	.RxFIFO,  = strconv.ParseUint([4], 10, 64)
	if  != nil {
		return nil, 
	}
	.RxFrame,  = strconv.ParseUint([5], 10, 64)
	if  != nil {
		return nil, 
	}
	.RxCompressed,  = strconv.ParseUint([6], 10, 64)
	if  != nil {
		return nil, 
	}
	.RxMulticast,  = strconv.ParseUint([7], 10, 64)
	if  != nil {
		return nil, 
	}

	// TX
	.TxBytes,  = strconv.ParseUint([8], 10, 64)
	if  != nil {
		return nil, 
	}
	.TxPackets,  = strconv.ParseUint([9], 10, 64)
	if  != nil {
		return nil, 
	}
	.TxErrors,  = strconv.ParseUint([10], 10, 64)
	if  != nil {
		return nil, 
	}
	.TxDropped,  = strconv.ParseUint([11], 10, 64)
	if  != nil {
		return nil, 
	}
	.TxFIFO,  = strconv.ParseUint([12], 10, 64)
	if  != nil {
		return nil, 
	}
	.TxCollisions,  = strconv.ParseUint([13], 10, 64)
	if  != nil {
		return nil, 
	}
	.TxCarrier,  = strconv.ParseUint([14], 10, 64)
	if  != nil {
		return nil, 
	}
	.TxCompressed,  = strconv.ParseUint([15], 10, 64)
	if  != nil {
		return nil, 
	}

	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 
}