Added new metric mem_bytes that reports the number of bytes used by TCP/UDP memory

This commit is contained in:
Ken Herner 2015-09-09 14:05:16 -04:00
parent 60a57a15d5
commit d4adef9881
2 changed files with 15 additions and 11 deletions

View File

@ -17,7 +17,7 @@ const (
sockStatSubsystem = "sockstat" 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() var pageSize = os.Getpagesize()
type sockStatCollector struct { type sockStatCollector struct {
@ -25,10 +25,10 @@ type sockStatCollector struct {
} }
func init() { 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) { func NewSockStatCollector() (Collector, error) {
return &sockStatCollector{ return &sockStatCollector{
metrics: map[string]prometheus.Gauge{}, metrics: map[string]prometheus.Gauge{},
@ -84,7 +84,7 @@ func parseSockStats(r io.Reader, fileName string) (map[string]map[string]string,
for scanner.Scan() { for scanner.Scan() {
line := strings.Split(string(scanner.Text()), " ") line := strings.Split(string(scanner.Text()), " ")
// Remove trailing : // Remove trailing :.
protocol := line[0][:len(line[0])-1] protocol := line[0][:len(line[0])-1]
sockStat[protocol] = map[string]string{} 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 The mem metrics is the count of pages used.
// Update TCP Mem 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"]) pageCount, err := strconv.Atoi(sockStat["TCP"]["mem"])
if err != nil { if err != nil {
return nil, fmt.Errorf("invalid value %s in sockstats: %s", sockStat["TCP"]["mem"], err) 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"]) pageCount, err = strconv.Atoi(sockStat["UDP"]["mem"])
if err != nil { if err != nil {
return nil, fmt.Errorf("invalid value %s in sockstats: %s", sockStat["UDP"]["mem"], err) 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 return sockStat, nil
} }

View File

@ -35,7 +35,7 @@ func TestSockStats(t *testing.T) {
// page size in bytes back from sockstat_linux. We get the page size from // 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 // os here because this value can change from system to system. The value is
// 4096 by default from linux 2.4 onward. // 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) t.Errorf("want sockstat sockets used %s, got %s", want, got)
} }
} }