2015-09-26 17:36:40 +02:00
|
|
|
// 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.
|
|
|
|
|
2015-09-09 18:37:48 +02:00
|
|
|
// +build !nosockstat
|
2015-09-04 21:34:10 +02:00
|
|
|
|
|
|
|
package collector
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2016-08-12 01:36:42 +02:00
|
|
|
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2015-09-04 21:34:10 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
sockStatSubsystem = "sockstat"
|
|
|
|
)
|
|
|
|
|
2015-09-09 20:05:16 +02:00
|
|
|
// Used for calculating the total memory bytes on TCP and UDP.
|
2015-09-04 21:34:10 +02:00
|
|
|
var pageSize = os.Getpagesize()
|
|
|
|
|
2016-08-12 01:36:42 +02:00
|
|
|
type sockStatCollector struct{}
|
2015-09-04 21:34:10 +02:00
|
|
|
|
|
|
|
func init() {
|
2015-09-09 20:05:16 +02:00
|
|
|
Factories[sockStatSubsystem] = NewSockStatCollector
|
2015-09-04 21:34:10 +02:00
|
|
|
}
|
|
|
|
|
2015-09-09 20:05:16 +02:00
|
|
|
// NewSockStatCollector returns a new Collector exposing socket stats.
|
2015-09-04 21:34:10 +02:00
|
|
|
func NewSockStatCollector() (Collector, error) {
|
2016-08-12 01:36:42 +02:00
|
|
|
return &sockStatCollector{}, nil
|
2015-09-04 21:34:10 +02:00
|
|
|
}
|
|
|
|
|
2017-02-28 19:47:20 +01:00
|
|
|
func (c *sockStatCollector) Update(ch chan<- prometheus.Metric) error {
|
2015-09-26 14:53:46 +02:00
|
|
|
sockStats, err := getSockStats(procFilePath("net/sockstat"))
|
2015-09-04 21:34:10 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("couldn't get sockstats: %s", 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)
|
|
|
|
}
|
2016-08-12 01:36:42 +02:00
|
|
|
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.GaugeValue, v,
|
|
|
|
)
|
2015-09-04 21:34:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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() {
|
2017-02-28 18:42:43 +01:00
|
|
|
line := strings.Split(scanner.Text(), " ")
|
2015-09-10 12:21:25 +02:00
|
|
|
// Remove trailing ':'.
|
2015-09-04 21:34:10 +02: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++
|
|
|
|
}
|
|
|
|
}
|
2017-02-28 19:31:35 +01:00
|
|
|
if err := scanner.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-09-04 21:34:10 +02:00
|
|
|
|
2015-09-10 12:21:25 +02:00
|
|
|
// 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.
|
2015-09-04 21:34:10 +02: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 20:05:16 +02:00
|
|
|
sockStat["TCP"]["mem_bytes"] = strconv.Itoa(pageCount * pageSize)
|
2015-09-04 21:34:10 +02:00
|
|
|
|
2015-09-09 20:05:16 +02:00
|
|
|
// Update the UDP mem from page count to bytes.
|
2016-07-05 23:22:26 +02:00
|
|
|
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)
|
2015-09-04 21:34:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return sockStat, nil
|
|
|
|
}
|