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-05-05 14:49:35 +02:00
|
|
|
// +build !noipvs
|
|
|
|
|
|
|
|
package collector
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-07-26 15:20:28 +02:00
|
|
|
"os"
|
2015-05-05 14:49:35 +02:00
|
|
|
"strconv"
|
|
|
|
|
2019-12-31 17:19:37 +01:00
|
|
|
"github.com/go-kit/kit/log"
|
|
|
|
"github.com/go-kit/kit/log/level"
|
2015-05-05 14:49:35 +02:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
"github.com/prometheus/procfs"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ipvsCollector struct {
|
|
|
|
Collector
|
|
|
|
fs procfs.FS
|
2016-12-28 15:21:31 +01:00
|
|
|
backendConnectionsActive, backendConnectionsInact, backendWeight typedDesc
|
|
|
|
connections, incomingPackets, outgoingPackets, incomingBytes, outgoingBytes typedDesc
|
2019-12-31 17:19:37 +01:00
|
|
|
logger log.Logger
|
2015-05-05 14:49:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2017-09-28 15:06:26 +02:00
|
|
|
registerCollector("ipvs", defaultEnabled, NewIPVSCollector)
|
2015-05-05 14:49:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewIPVSCollector sets up a new collector for IPVS metrics. It accepts the
|
|
|
|
// "procfs" config parameter to override the default proc location (/proc).
|
2019-12-31 17:19:37 +01:00
|
|
|
func NewIPVSCollector(logger log.Logger) (Collector, error) {
|
|
|
|
return newIPVSCollector(logger)
|
2015-05-05 14:49:35 +02:00
|
|
|
}
|
|
|
|
|
2019-12-31 17:19:37 +01:00
|
|
|
func newIPVSCollector(logger log.Logger) (*ipvsCollector, error) {
|
2015-05-05 14:49:35 +02:00
|
|
|
var (
|
|
|
|
ipvsBackendLabelNames = []string{
|
|
|
|
"local_address",
|
|
|
|
"local_port",
|
|
|
|
"remote_address",
|
|
|
|
"remote_port",
|
|
|
|
"proto",
|
2019-08-27 14:24:11 +02:00
|
|
|
"local_mark",
|
2015-05-05 14:49:35 +02:00
|
|
|
}
|
|
|
|
c ipvsCollector
|
|
|
|
err error
|
2015-05-20 20:04:49 +02:00
|
|
|
subsystem = "ipvs"
|
2015-05-05 14:49:35 +02:00
|
|
|
)
|
|
|
|
|
2019-12-31 17:19:37 +01:00
|
|
|
c.logger = logger
|
2015-09-26 14:53:46 +02:00
|
|
|
c.fs, err = procfs.NewFS(*procPath)
|
2015-05-05 14:49:35 +02:00
|
|
|
if err != nil {
|
2019-11-29 14:51:31 +01:00
|
|
|
return nil, fmt.Errorf("failed to open procfs: %w", err)
|
2015-05-05 14:49:35 +02:00
|
|
|
}
|
|
|
|
|
2016-12-28 15:21:31 +01:00
|
|
|
c.connections = typedDesc{prometheus.NewDesc(
|
2017-09-28 15:06:26 +02:00
|
|
|
prometheus.BuildFQName(namespace, subsystem, "connections_total"),
|
2016-12-28 15:21:31 +01:00
|
|
|
"The total number of connections made.",
|
|
|
|
nil, nil,
|
|
|
|
), prometheus.CounterValue}
|
|
|
|
c.incomingPackets = typedDesc{prometheus.NewDesc(
|
2017-09-28 15:06:26 +02:00
|
|
|
prometheus.BuildFQName(namespace, subsystem, "incoming_packets_total"),
|
2016-12-28 15:21:31 +01:00
|
|
|
"The total number of incoming packets.",
|
|
|
|
nil, nil,
|
|
|
|
), prometheus.CounterValue}
|
|
|
|
c.outgoingPackets = typedDesc{prometheus.NewDesc(
|
2017-09-28 15:06:26 +02:00
|
|
|
prometheus.BuildFQName(namespace, subsystem, "outgoing_packets_total"),
|
2016-12-28 15:21:31 +01:00
|
|
|
"The total number of outgoing packets.",
|
|
|
|
nil, nil,
|
|
|
|
), prometheus.CounterValue}
|
|
|
|
c.incomingBytes = typedDesc{prometheus.NewDesc(
|
2017-09-28 15:06:26 +02:00
|
|
|
prometheus.BuildFQName(namespace, subsystem, "incoming_bytes_total"),
|
2016-12-28 15:21:31 +01:00
|
|
|
"The total amount of incoming data.",
|
|
|
|
nil, nil,
|
|
|
|
), prometheus.CounterValue}
|
|
|
|
c.outgoingBytes = typedDesc{prometheus.NewDesc(
|
2017-09-28 15:06:26 +02:00
|
|
|
prometheus.BuildFQName(namespace, subsystem, "outgoing_bytes_total"),
|
2016-12-28 15:21:31 +01:00
|
|
|
"The total amount of outgoing data.",
|
|
|
|
nil, nil,
|
|
|
|
), prometheus.CounterValue}
|
|
|
|
c.backendConnectionsActive = typedDesc{prometheus.NewDesc(
|
2017-09-28 15:06:26 +02:00
|
|
|
prometheus.BuildFQName(namespace, subsystem, "backend_connections_active"),
|
2016-12-28 15:21:31 +01:00
|
|
|
"The current active connections by local and remote address.",
|
|
|
|
ipvsBackendLabelNames, nil,
|
|
|
|
), prometheus.GaugeValue}
|
|
|
|
c.backendConnectionsInact = typedDesc{prometheus.NewDesc(
|
2017-09-28 15:06:26 +02:00
|
|
|
prometheus.BuildFQName(namespace, subsystem, "backend_connections_inactive"),
|
2016-12-28 15:21:31 +01:00
|
|
|
"The current inactive connections by local and remote address.",
|
|
|
|
ipvsBackendLabelNames, nil,
|
|
|
|
), prometheus.GaugeValue}
|
|
|
|
c.backendWeight = typedDesc{prometheus.NewDesc(
|
2017-09-28 15:06:26 +02:00
|
|
|
prometheus.BuildFQName(namespace, subsystem, "backend_weight"),
|
2016-12-28 15:21:31 +01:00
|
|
|
"The current backend weight by local and remote address.",
|
|
|
|
ipvsBackendLabelNames, nil,
|
|
|
|
), prometheus.GaugeValue}
|
2015-05-05 14:49:35 +02:00
|
|
|
|
|
|
|
return &c, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ipvsCollector) Update(ch chan<- prometheus.Metric) error {
|
2019-06-12 20:47:16 +02:00
|
|
|
ipvsStats, err := c.fs.IPVSStats()
|
2015-05-05 14:49:35 +02:00
|
|
|
if err != nil {
|
2017-07-26 15:20:28 +02:00
|
|
|
// Cannot access ipvs metrics, report no error.
|
|
|
|
if os.IsNotExist(err) {
|
2019-12-31 17:19:37 +01:00
|
|
|
level.Debug(c.logger).Log("msg", "ipvs collector metrics are not available for this system")
|
2020-02-19 16:11:29 +01:00
|
|
|
return ErrNoData
|
2017-07-26 15:20:28 +02:00
|
|
|
}
|
2015-05-05 14:49:35 +02:00
|
|
|
return fmt.Errorf("could not get IPVS stats: %s", err)
|
|
|
|
}
|
2016-12-28 15:21:31 +01:00
|
|
|
ch <- c.connections.mustNewConstMetric(float64(ipvsStats.Connections))
|
|
|
|
ch <- c.incomingPackets.mustNewConstMetric(float64(ipvsStats.IncomingPackets))
|
|
|
|
ch <- c.outgoingPackets.mustNewConstMetric(float64(ipvsStats.OutgoingPackets))
|
|
|
|
ch <- c.incomingBytes.mustNewConstMetric(float64(ipvsStats.IncomingBytes))
|
|
|
|
ch <- c.outgoingBytes.mustNewConstMetric(float64(ipvsStats.OutgoingBytes))
|
2015-05-05 14:49:35 +02:00
|
|
|
|
2019-06-12 20:47:16 +02:00
|
|
|
backendStats, err := c.fs.IPVSBackendStatus()
|
2015-05-05 14:49:35 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not get backend status: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, backend := range backendStats {
|
2019-08-27 14:24:11 +02:00
|
|
|
localAddress := ""
|
|
|
|
if backend.LocalAddress.String() != "<nil>" {
|
|
|
|
localAddress = backend.LocalAddress.String()
|
|
|
|
}
|
2015-05-05 14:49:35 +02:00
|
|
|
labelValues := []string{
|
2019-08-27 14:24:11 +02:00
|
|
|
localAddress,
|
2015-05-05 14:49:35 +02:00
|
|
|
strconv.FormatUint(uint64(backend.LocalPort), 10),
|
|
|
|
backend.RemoteAddress.String(),
|
|
|
|
strconv.FormatUint(uint64(backend.RemotePort), 10),
|
|
|
|
backend.Proto,
|
2019-08-27 14:24:11 +02:00
|
|
|
backend.LocalMark,
|
2015-05-05 14:49:35 +02:00
|
|
|
}
|
2016-12-28 15:21:31 +01:00
|
|
|
ch <- c.backendConnectionsActive.mustNewConstMetric(float64(backend.ActiveConn), labelValues...)
|
|
|
|
ch <- c.backendConnectionsInact.mustNewConstMetric(float64(backend.InactConn), labelValues...)
|
|
|
|
ch <- c.backendWeight.mustNewConstMetric(float64(backend.Weight), labelValues...)
|
2015-05-05 14:49:35 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|