Merge pull request #942 from jimmidyson/net-stats-from-proc

Drop regexp for net stats parsing
This commit is contained in:
Jimmi Dyson 2015-10-28 19:02:45 +00:00
commit c56af85fdb
3 changed files with 82 additions and 19 deletions

View File

@ -18,6 +18,7 @@ import (
"bufio"
"fmt"
"io/ioutil"
"os"
"path"
"regexp"
"strconv"
@ -135,7 +136,6 @@ func networkStatsFromProc(rootFs string, pid int) ([]info.InterfaceStats, error)
var (
ignoredDevicePrefixes = []string{"lo", "veth", "docker"}
netStatLineRE = regexp.MustCompile("[ ]*(.+):([ ]+[0-9]+){16}")
)
func isIgnoredDevice(ifName string) bool {
@ -147,6 +147,8 @@ func isIgnoredDevice(ifName string) bool {
return false
}
const netstatsLine = `%s %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d`
func scanInterfaceStats(netStatsFile string) ([]info.InterfaceStats, error) {
var (
bkt uint64
@ -154,34 +156,26 @@ func scanInterfaceStats(netStatsFile string) ([]info.InterfaceStats, error) {
stats := []info.InterfaceStats{}
data, err := ioutil.ReadFile(netStatsFile)
file, err := os.Open(netStatsFile)
if err != nil {
return stats, fmt.Errorf("failure opening %s: %v", netStatsFile, err)
}
defer file.Close()
reader := strings.NewReader(string(data))
scanner := bufio.NewScanner(reader)
scanner.Split(bufio.ScanLines)
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
if netStatLineRE.MatchString(line) {
line = strings.Replace(line, ":", "", -1)
line = strings.Replace(line, ":", "", -1)
i := info.InterfaceStats{}
i := info.InterfaceStats{}
_, err := fmt.Sscanf(line, "%s %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d",
&i.Name, &i.RxBytes, &i.RxPackets, &i.RxErrors, &i.RxDropped, &bkt, &bkt, &bkt,
&bkt, &i.TxBytes, &i.TxPackets, &i.TxErrors, &i.TxDropped, &bkt, &bkt, &bkt, &bkt)
_, err := fmt.Sscanf(line, netstatsLine,
&i.Name, &i.RxBytes, &i.RxPackets, &i.RxErrors, &i.RxDropped, &bkt, &bkt, &bkt,
&bkt, &i.TxBytes, &i.TxPackets, &i.TxErrors, &i.TxDropped, &bkt, &bkt, &bkt, &bkt)
if err != nil {
return stats, fmt.Errorf("failure opening %s: %v", netStatsFile, err)
}
if !isIgnoredDevice(i.Name) {
stats = append(stats, i)
}
if err == nil && !isIgnoredDevice(i.Name) {
stats = append(stats, i)
}
}

View File

@ -0,0 +1,63 @@
// Copyright 2014 Google Inc. All Rights Reserved.
//
// 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 libcontainer
import (
"testing"
info "github.com/google/cadvisor/info/v1"
)
func TestScanInterfaceStats(t *testing.T) {
stats, err := scanInterfaceStats("testdata/procnetdev")
if err != nil {
t.Error(err)
}
var netdevstats = []info.InterfaceStats{
{
Name: "wlp4s0",
RxBytes: 1,
RxPackets: 2,
RxErrors: 3,
RxDropped: 4,
TxBytes: 9,
TxPackets: 10,
TxErrors: 11,
TxDropped: 12,
},
{
Name: "em1",
RxBytes: 315849,
RxPackets: 1172,
RxErrors: 0,
RxDropped: 0,
TxBytes: 315850,
TxPackets: 1173,
TxErrors: 0,
TxDropped: 0,
},
}
if len(stats) != len(netdevstats) {
t.Errorf("Expected 2 net stats, got %d", len(stats))
}
for i, v := range netdevstats {
if v != stats[i] {
t.Errorf("Expected %#v, got %#v", v, stats[i])
}
}
}

View File

@ -0,0 +1,6 @@
Inter-| Receive | Transmit
face |bytes packets errs drop fifo frame compressed multicast|bytes packets errs drop fifo colls carrier compressed
wlp4s0: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 0
docker0: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
lo: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
em1: 315849 1172 0 0 0 0 0 0 315850 1173 0 0 0 0 0 0