2015-09-09 16:37:48 +00:00
|
|
|
// +build !nosockstat
|
2015-09-04 19:34:10 +00:00
|
|
|
|
|
|
|
package collector
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
procSockStat = "/proc/net/sockstat"
|
|
|
|
sockStatSubsystem = "sockstat"
|
|
|
|
)
|
|
|
|
|
2015-09-09 18:05:16 +00:00
|
|
|
// Used for calculating the total memory bytes on TCP and UDP.
|
2015-09-04 19:34:10 +00:00
|
|
|
var pageSize = os.Getpagesize()
|
|
|
|
|
|
|
|
type sockStatCollector struct {
|
|
|
|
metrics map[string]prometheus.Gauge
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2015-09-09 18:05:16 +00:00
|
|
|
Factories[sockStatSubsystem] = NewSockStatCollector
|
2015-09-04 19:34:10 +00:00
|
|
|
}
|
|
|
|
|
2015-09-09 18:05:16 +00:00
|
|
|
// NewSockStatCollector returns a new Collector exposing socket stats.
|
2015-09-04 19:34:10 +00:00
|
|
|
func NewSockStatCollector() (Collector, error) {
|
|
|
|
return &sockStatCollector{
|
|
|
|
metrics: map[string]prometheus.Gauge{},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *sockStatCollector) Update(ch chan<- prometheus.Metric) (err error) {
|
|
|
|
sockStats, err := getSockStats(procSockStat)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("couldn't get sockstats: %s", err)
|
|
|
|
}
|
|
|
|
for protocol, protocolStats := range sockStats {
|
|
|
|
for name, value := range protocolStats {
|
|
|
|
key := protocol + "_" + name
|
|
|
|
if _, ok := c.metrics[key]; !ok {
|
|
|
|
c.metrics[key] = prometheus.NewGauge(
|
|
|
|
prometheus.GaugeOpts{
|
|
|
|
Namespace: Namespace,
|
|
|
|
Subsystem: sockStatSubsystem,
|
|
|
|
Name: key,
|
|
|
|
Help: fmt.Sprintf("%s %s from /proc/net/sockstat.", protocol, name),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
v, err := strconv.ParseFloat(value, 64)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("invalid value %s in sockstats: %s", value, err)
|
|
|
|
}
|
|
|
|
c.metrics[key].Set(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, m := range c.metrics {
|
|
|
|
m.Collect(ch)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func getSockStats(fileName string) (map[string]map[string]string, error) {
|
|
|
|
file, err := os.Open(fileName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
return parseSockStats(file, fileName)
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseSockStats(r io.Reader, fileName string) (map[string]map[string]string, error) {
|
|
|
|
var (
|
|
|
|
sockStat = map[string]map[string]string{}
|
|
|
|
scanner = bufio.NewScanner(r)
|
|
|
|
)
|
|
|
|
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := strings.Split(string(scanner.Text()), " ")
|
2015-09-09 18:05:16 +00:00
|
|
|
// Remove trailing :.
|
2015-09-04 19:34:10 +00:00
|
|
|
protocol := line[0][:len(line[0])-1]
|
|
|
|
sockStat[protocol] = map[string]string{}
|
|
|
|
|
|
|
|
for i := 1; i < len(line) && i+1 < len(line); i++ {
|
|
|
|
sockStat[protocol][line[i]] = line[i+1]
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-09 18:05:16 +00:00
|
|
|
/*
|
|
|
|
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.
|
|
|
|
*/
|
2015-09-04 19:34:10 +00:00
|
|
|
pageCount, err := strconv.Atoi(sockStat["TCP"]["mem"])
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("invalid value %s in sockstats: %s", sockStat["TCP"]["mem"], err)
|
|
|
|
}
|
2015-09-09 18:05:16 +00:00
|
|
|
sockStat["TCP"]["mem_bytes"] = strconv.Itoa(pageCount * pageSize)
|
2015-09-04 19:34:10 +00:00
|
|
|
|
2015-09-09 18:05:16 +00:00
|
|
|
// Update the UDP mem from page count to bytes.
|
2015-09-04 19:34:10 +00:00
|
|
|
pageCount, err = strconv.Atoi(sockStat["UDP"]["mem"])
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("invalid value %s in sockstats: %s", sockStat["UDP"]["mem"], err)
|
|
|
|
}
|
2015-09-09 18:05:16 +00:00
|
|
|
sockStat["UDP"]["mem_bytes"] = strconv.Itoa(pageCount * pageSize)
|
2015-09-04 19:34:10 +00:00
|
|
|
|
|
|
|
return sockStat, nil
|
|
|
|
}
|