Returns the network statistics for the network interfaces represented by the NetworkRuntimeInfo.
(interfaceName string)
| 43 | |
| 44 | // Returns the network statistics for the network interfaces represented by the NetworkRuntimeInfo. |
| 45 | func getNetworkInterfaceStats(interfaceName string) (*types.NetworkInterface, error) { |
| 46 | out := &types.NetworkInterface{Name: interfaceName} |
| 47 | // This can happen if the network runtime information is missing - possible if the |
| 48 | // container was created by an old version of libcontainer. |
| 49 | if interfaceName == "" { |
| 50 | return out, nil |
| 51 | } |
| 52 | type netStatsPair struct { |
| 53 | // Where to write the output. |
| 54 | Out *uint64 |
| 55 | // The network stats file to read. |
| 56 | File string |
| 57 | } |
| 58 | // Ingress for host veth is from the container. Hence tx_bytes stat on the host veth is actually number of bytes received by the container. |
| 59 | netStats := []netStatsPair{ |
| 60 | {Out: &out.RxBytes, File: "tx_bytes"}, |
| 61 | {Out: &out.RxPackets, File: "tx_packets"}, |
| 62 | {Out: &out.RxErrors, File: "tx_errors"}, |
| 63 | {Out: &out.RxDropped, File: "tx_dropped"}, |
| 64 | |
| 65 | {Out: &out.TxBytes, File: "rx_bytes"}, |
| 66 | {Out: &out.TxPackets, File: "rx_packets"}, |
| 67 | {Out: &out.TxErrors, File: "rx_errors"}, |
| 68 | {Out: &out.TxDropped, File: "rx_dropped"}, |
| 69 | } |
| 70 | for _, netStat := range netStats { |
| 71 | data, err := readSysfsNetworkStats(interfaceName, netStat.File) |
| 72 | if err != nil { |
| 73 | return nil, err |
| 74 | } |
| 75 | *(netStat.Out) = data |
| 76 | } |
| 77 | return out, nil |
| 78 | } |
| 79 | |
| 80 | // Reads the specified statistics available under /sys/class/net/<EthInterface>/statistics |
| 81 | func readSysfsNetworkStats(ethInterface, statsFile string) (uint64, error) { |
no test coverage detected
searching dependent graphs…