From d4adef9881f72ad128c115120c5a9076d0ccd20e Mon Sep 17 00:00:00 2001 From: Ken Herner Date: Wed, 9 Sep 2015 14:05:16 -0400 Subject: [PATCH] Added new metric mem_bytes that reports the number of bytes used by TCP/UDP memory --- collector/sockstat_linux.go | 24 ++++++++++++++---------- collector/sockstat_linux_test.go | 2 +- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/collector/sockstat_linux.go b/collector/sockstat_linux.go index a70ad3fd..0d471f8d 100644 --- a/collector/sockstat_linux.go +++ b/collector/sockstat_linux.go @@ -17,7 +17,7 @@ const ( sockStatSubsystem = "sockstat" ) -// Used for calculating the total memory bytes on TCP and UDP +// Used for calculating the total memory bytes on TCP and UDP. var pageSize = os.Getpagesize() type sockStatCollector struct { @@ -25,10 +25,10 @@ type sockStatCollector struct { } func init() { - Factories["sockstat"] = NewSockStatCollector + Factories[sockStatSubsystem] = NewSockStatCollector } -// NewSockStatCollector returns a new Collector exposing socket stats +// NewSockStatCollector returns a new Collector exposing socket stats. func NewSockStatCollector() (Collector, error) { return &sockStatCollector{ metrics: map[string]prometheus.Gauge{}, @@ -84,7 +84,7 @@ func parseSockStats(r io.Reader, fileName string) (map[string]map[string]string, for scanner.Scan() { line := strings.Split(string(scanner.Text()), " ") - // Remove trailing : + // Remove trailing :. protocol := line[0][:len(line[0])-1] sockStat[protocol] = map[string]string{} @@ -94,21 +94,25 @@ func parseSockStats(r io.Reader, fileName string) (map[string]map[string]string, } } - // The mem options are reported in pages - // Multiply them by the pagesize to get bytes - // Update TCP Mem + /* + The mem metrics is the count of pages used. + Multiply the mem metrics by the page size from the kernal to get the number + of bytes used. + + Update the TCP mem from page count to bytes. + */ pageCount, err := strconv.Atoi(sockStat["TCP"]["mem"]) if err != nil { return nil, fmt.Errorf("invalid value %s in sockstats: %s", sockStat["TCP"]["mem"], err) } - sockStat["TCP"]["mem"] = strconv.Itoa(pageCount * pageSize) + sockStat["TCP"]["mem_bytes"] = strconv.Itoa(pageCount * pageSize) - // Update UDP Mem + // Update the UDP mem from page count to bytes. pageCount, err = strconv.Atoi(sockStat["UDP"]["mem"]) if err != nil { return nil, fmt.Errorf("invalid value %s in sockstats: %s", sockStat["UDP"]["mem"], err) } - sockStat["UDP"]["mem"] = strconv.Itoa(pageCount * pageSize) + sockStat["UDP"]["mem_bytes"] = strconv.Itoa(pageCount * pageSize) return sockStat, nil } diff --git a/collector/sockstat_linux_test.go b/collector/sockstat_linux_test.go index 36b1aec9..4096536e 100644 --- a/collector/sockstat_linux_test.go +++ b/collector/sockstat_linux_test.go @@ -35,7 +35,7 @@ func TestSockStats(t *testing.T) { // page size in bytes back from sockstat_linux. We get the page size from // os here because this value can change from system to system. The value is // 4096 by default from linux 2.4 onward. - if want, got := strconv.Itoa(os.Getpagesize()), sockStats["TCP"]["mem"]; want != got { + if want, got := strconv.Itoa(os.Getpagesize()), sockStats["TCP"]["mem_bytes"]; want != got { t.Errorf("want sockstat sockets used %s, got %s", want, got) } }