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 (
|
2020-06-15 22:27:14 +02:00
|
|
|
"errors"
|
2015-09-04 21:34:10 +02:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2016-08-12 01:36:42 +02:00
|
|
|
|
2020-11-14 11:53:51 +01:00
|
|
|
"github.com/go-kit/log"
|
|
|
|
"github.com/go-kit/log/level"
|
2016-08-12 01:36:42 +02:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2019-11-25 20:41:38 +01:00
|
|
|
"github.com/prometheus/procfs"
|
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()
|
|
|
|
|
2019-12-31 17:19:37 +01:00
|
|
|
type sockStatCollector struct {
|
|
|
|
logger log.Logger
|
|
|
|
}
|
2015-09-04 21:34:10 +02:00
|
|
|
|
|
|
|
func init() {
|
2017-09-28 15:06:26 +02:00
|
|
|
registerCollector(sockStatSubsystem, defaultEnabled, 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.
|
2019-12-31 17:19:37 +01:00
|
|
|
func NewSockStatCollector(logger log.Logger) (Collector, error) {
|
|
|
|
return &sockStatCollector{logger}, 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 {
|
2019-11-25 20:41:38 +01:00
|
|
|
fs, err := procfs.NewFS(*procPath)
|
2015-09-04 21:34:10 +02:00
|
|
|
if err != nil {
|
2019-11-29 14:51:31 +01:00
|
|
|
return fmt.Errorf("failed to open procfs: %w", err)
|
2015-09-04 21:34:10 +02:00
|
|
|
}
|
2019-11-25 20:41:38 +01:00
|
|
|
|
|
|
|
// If IPv4 and/or IPv6 are disabled on this kernel, handle it gracefully.
|
|
|
|
stat4, err := fs.NetSockstat()
|
|
|
|
switch {
|
|
|
|
case err == nil:
|
2020-06-15 22:27:14 +02:00
|
|
|
case errors.Is(err, os.ErrNotExist):
|
2019-12-31 17:19:37 +01:00
|
|
|
level.Debug(c.logger).Log("msg", "IPv4 sockstat statistics not found, skipping")
|
2019-11-25 20:41:38 +01:00
|
|
|
default:
|
2019-11-29 14:51:31 +01:00
|
|
|
return fmt.Errorf("failed to get IPv4 sockstat data: %w", err)
|
2015-09-04 21:34:10 +02:00
|
|
|
}
|
|
|
|
|
2019-11-25 20:41:38 +01:00
|
|
|
stat6, err := fs.NetSockstat6()
|
|
|
|
switch {
|
|
|
|
case err == nil:
|
2020-06-15 22:27:14 +02:00
|
|
|
case errors.Is(err, os.ErrNotExist):
|
2019-12-31 17:19:37 +01:00
|
|
|
level.Debug(c.logger).Log("msg", "IPv6 sockstat statistics not found, skipping")
|
2019-11-25 20:41:38 +01:00
|
|
|
default:
|
2019-11-29 14:51:31 +01:00
|
|
|
return fmt.Errorf("failed to get IPv6 sockstat data: %w", err)
|
2019-11-25 20:41:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
stats := []struct {
|
|
|
|
isIPv6 bool
|
|
|
|
stat *procfs.NetSockstat
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
stat: stat4,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
isIPv6: true,
|
|
|
|
stat: stat6,
|
|
|
|
},
|
2015-09-04 21:34:10 +02:00
|
|
|
}
|
|
|
|
|
2019-11-25 20:41:38 +01:00
|
|
|
for _, s := range stats {
|
|
|
|
c.update(ch, s.isIPv6, s.stat)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2015-09-04 21:34:10 +02:00
|
|
|
}
|
|
|
|
|
2019-11-25 20:41:38 +01:00
|
|
|
func (c *sockStatCollector) update(ch chan<- prometheus.Metric, isIPv6 bool, s *procfs.NetSockstat) {
|
|
|
|
if s == nil {
|
|
|
|
// IPv6 disabled or similar; nothing to do.
|
|
|
|
return
|
2015-09-04 21:34:10 +02:00
|
|
|
}
|
2019-11-25 20:41:38 +01:00
|
|
|
|
|
|
|
// 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),
|
|
|
|
)
|
2017-02-28 19:31:35 +01:00
|
|
|
}
|
2015-09-04 21:34:10 +02:00
|
|
|
|
2019-11-25 20:41:38 +01:00
|
|
|
// A name and optional value for a sockstat metric.
|
|
|
|
type ssPair struct {
|
|
|
|
name string
|
|
|
|
v *int
|
2015-09-04 21:34:10 +02:00
|
|
|
}
|
|
|
|
|
2019-11-25 20:41:38 +01:00
|
|
|
// 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,
|
|
|
|
},
|
2016-07-05 23:22:26 +02:00
|
|
|
}
|
2015-09-04 21:34:10 +02:00
|
|
|
|
2019-11-25 20:41:38 +01:00
|
|
|
// 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,
|
|
|
|
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,
|
|
|
|
float64(*pair.v),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2015-09-04 21:34:10 +02:00
|
|
|
}
|