collector: reimplement sockstat collector with procfs (#1552)

* collector: reimplement sockstat collector with procfs
* collector: handle sockstat IPv4 disabled, debug logging

Signed-off-by: Matt Layher <mdlayher@gmail.com>
This commit is contained in:
Matt Layher 2019-11-25 14:41:38 -05:00 committed by Ben Kochie
commit da6b66371f
15 changed files with 374 additions and 342 deletions

View file

@ -2575,15 +2575,27 @@ node_scrape_collector_success{collector="vmstat"} 1
node_scrape_collector_success{collector="wifi"} 1
node_scrape_collector_success{collector="xfs"} 1
node_scrape_collector_success{collector="zfs"} 1
# HELP node_sockstat_FRAG6_inuse Number of FRAG6 sockets in state inuse.
# TYPE node_sockstat_FRAG6_inuse gauge
node_sockstat_FRAG6_inuse 0
# HELP node_sockstat_FRAG6_memory Number of FRAG6 sockets in state memory.
# TYPE node_sockstat_FRAG6_memory gauge
node_sockstat_FRAG6_memory 0
# HELP node_sockstat_FRAG_inuse Number of FRAG sockets in state inuse.
# TYPE node_sockstat_FRAG_inuse gauge
node_sockstat_FRAG_inuse 0
# HELP node_sockstat_FRAG_memory Number of FRAG sockets in state memory.
# TYPE node_sockstat_FRAG_memory gauge
node_sockstat_FRAG_memory 0
# HELP node_sockstat_RAW6_inuse Number of RAW6 sockets in state inuse.
# TYPE node_sockstat_RAW6_inuse gauge
node_sockstat_RAW6_inuse 1
# HELP node_sockstat_RAW_inuse Number of RAW sockets in state inuse.
# TYPE node_sockstat_RAW_inuse gauge
node_sockstat_RAW_inuse 0
# HELP node_sockstat_TCP6_inuse Number of TCP6 sockets in state inuse.
# TYPE node_sockstat_TCP6_inuse gauge
node_sockstat_TCP6_inuse 17
# HELP node_sockstat_TCP_alloc Number of TCP sockets in state alloc.
# TYPE node_sockstat_TCP_alloc gauge
node_sockstat_TCP_alloc 17
@ -2602,6 +2614,12 @@ node_sockstat_TCP_orphan 0
# HELP node_sockstat_TCP_tw Number of TCP sockets in state tw.
# TYPE node_sockstat_TCP_tw gauge
node_sockstat_TCP_tw 4
# HELP node_sockstat_UDP6_inuse Number of UDP6 sockets in state inuse.
# TYPE node_sockstat_UDP6_inuse gauge
node_sockstat_UDP6_inuse 9
# HELP node_sockstat_UDPLITE6_inuse Number of UDPLITE6 sockets in state inuse.
# TYPE node_sockstat_UDPLITE6_inuse gauge
node_sockstat_UDPLITE6_inuse 0
# HELP node_sockstat_UDPLITE_inuse Number of UDPLITE sockets in state inuse.
# TYPE node_sockstat_UDPLITE_inuse gauge
node_sockstat_UDPLITE_inuse 0
@ -2614,7 +2632,7 @@ node_sockstat_UDP_mem 0
# HELP node_sockstat_UDP_mem_bytes Number of UDP sockets in state mem_bytes.
# TYPE node_sockstat_UDP_mem_bytes gauge
node_sockstat_UDP_mem_bytes 0
# HELP node_sockstat_sockets_used Number of sockets sockets in state used.
# HELP node_sockstat_sockets_used Number of IPv4 sockets in use.
# TYPE node_sockstat_sockets_used gauge
node_sockstat_sockets_used 229
# HELP node_textfile_mtime_seconds Unixtime mtime of textfiles successfully read.

View file

@ -0,0 +1,5 @@
TCP6: inuse 17
UDP6: inuse 9
UDPLITE6: inuse 0
RAW6: inuse 1
FRAG6: inuse 0 memory 0

View file

@ -1,5 +0,0 @@
sockets: used 229
TCP: inuse 4 orphan 0 tw 4 alloc 17 mem 1
UDP: inuse 0
RAW: inuse 0
FRAG: inuse 0 memory 0

View file

@ -16,14 +16,12 @@
package collector
import (
"bufio"
"fmt"
"io"
"os"
"strconv"
"strings"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/log"
"github.com/prometheus/procfs"
)
const (
@ -45,78 +43,138 @@ func NewSockStatCollector() (Collector, error) {
}
func (c *sockStatCollector) Update(ch chan<- prometheus.Metric) error {
sockStats, err := getSockStats(procFilePath("net/sockstat"))
fs, err := procfs.NewFS(*procPath)
if err != nil {
return fmt.Errorf("couldn't get sockstats: %s", err)
return fmt.Errorf("failed to open procfs: %v", err)
}
for protocol, protocolStats := range sockStats {
for name, value := range protocolStats {
v, err := strconv.ParseFloat(value, 64)
if err != nil {
return fmt.Errorf("invalid value %s in sockstats: %s", value, err)
// If IPv4 and/or IPv6 are disabled on this kernel, handle it gracefully.
stat4, err := fs.NetSockstat()
switch {
case err == nil:
case os.IsNotExist(err):
log.Debug("IPv4 sockstat statistics not found, skipping")
default:
return fmt.Errorf("failed to get IPv4 sockstat data: %v", err)
}
stat6, err := fs.NetSockstat6()
switch {
case err == nil:
case os.IsNotExist(err):
log.Debug("IPv6 sockstat statistics not found, skipping")
default:
return fmt.Errorf("failed to get IPv6 sockstat data: %v", err)
}
stats := []struct {
isIPv6 bool
stat *procfs.NetSockstat
}{
{
stat: stat4,
},
{
isIPv6: true,
stat: stat6,
},
}
for _, s := range stats {
c.update(ch, s.isIPv6, s.stat)
}
return nil
}
func (c *sockStatCollector) update(ch chan<- prometheus.Metric, isIPv6 bool, s *procfs.NetSockstat) {
if s == nil {
// IPv6 disabled or similar; nothing to do.
return
}
// If sockstat contains the number of used sockets, export it.
if !isIPv6 && s.Used != nil {
// TODO: this must be updated if sockstat6 ever exports this data.
ch <- prometheus.MustNewConstMetric(
prometheus.NewDesc(
prometheus.BuildFQName(namespace, sockStatSubsystem, "sockets_used"),
"Number of IPv4 sockets in use.",
nil,
nil,
),
prometheus.GaugeValue,
float64(*s.Used),
)
}
// A name and optional value for a sockstat metric.
type ssPair struct {
name string
v *int
}
// Previously these metric names were generated directly from the file output.
// In order to keep the same level of compatibility, we must map the fields
// to their correct names.
for _, p := range s.Protocols {
pairs := []ssPair{
{
name: "inuse",
v: &p.InUse,
},
{
name: "orphan",
v: p.Orphan,
},
{
name: "tw",
v: p.TW,
},
{
name: "alloc",
v: p.Alloc,
},
{
name: "mem",
v: p.Mem,
},
{
name: "memory",
v: p.Memory,
},
}
// Also export mem_bytes values for sockets which have a mem value
// stored in pages.
if p.Mem != nil {
v := *p.Mem * pageSize
pairs = append(pairs, ssPair{
name: "mem_bytes",
v: &v,
})
}
for _, pair := range pairs {
if pair.v == nil {
// This value is not set for this protocol; nothing to do.
continue
}
ch <- prometheus.MustNewConstMetric(
prometheus.NewDesc(
prometheus.BuildFQName(namespace, sockStatSubsystem, protocol+"_"+name),
fmt.Sprintf("Number of %s sockets in state %s.", protocol, name),
nil, nil,
prometheus.BuildFQName(
namespace,
sockStatSubsystem,
fmt.Sprintf("%s_%s", p.Protocol, pair.name),
),
fmt.Sprintf("Number of %s sockets in state %s.", p.Protocol, pair.name),
nil,
nil,
),
prometheus.GaugeValue, v,
prometheus.GaugeValue,
float64(*pair.v),
)
}
}
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(scanner.Text(), " ")
// Remove trailing ':'.
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++
}
}
if err := scanner.Err(); err != nil {
return nil, err
}
// The mem metrics is the count of pages used. Multiply the mem metrics by
// the page size from the kernel 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_bytes"] = strconv.Itoa(pageCount * pageSize)
// Update the UDP mem from page count to bytes.
if udpMem := sockStat["UDP"]["mem"]; udpMem != "" {
pageCount, err = strconv.Atoi(udpMem)
if err != nil {
return nil, fmt.Errorf("invalid value %s in sockstats: %s", sockStat["UDP"]["mem"], err)
}
sockStat["UDP"]["mem_bytes"] = strconv.Itoa(pageCount * pageSize)
}
return sockStat, nil
}

View file

@ -1,59 +0,0 @@
// Copyright 2015 The Prometheus Authors
// 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 collector
import (
"os"
"strconv"
"testing"
)
func TestSockStats(t *testing.T) {
testSockStats(t, "fixtures/proc/net/sockstat")
testSockStats(t, "fixtures/proc/net/sockstat_rhe4")
}
func testSockStats(t *testing.T, fixture string) {
file, err := os.Open(fixture)
if err != nil {
t.Fatal(err)
}
defer file.Close()
sockStats, err := parseSockStats(file, fixture)
if err != nil {
t.Fatal(err)
}
if want, got := "229", sockStats["sockets"]["used"]; want != got {
t.Errorf("want sockstat sockets used %s, got %s", want, got)
}
if want, got := "4", sockStats["TCP"]["tw"]; want != got {
t.Errorf("want sockstat TCP tw %s, got %s", want, got)
}
if want, got := "17", sockStats["TCP"]["alloc"]; want != got {
t.Errorf("want sockstat TCP alloc %s, got %s", want, got)
}
// The test file has 1 for TCP mem, which is one page. So we should get the
// 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_bytes"]; want != got {
t.Errorf("want sockstat TCP mem_bytes %s, got %s", want, got)
}
}