2015-09-26 08:36:40 -07: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.
|
|
|
|
|
2021-10-03 04:35:24 -07:00
|
|
|
//go:build !notcpstat
|
2015-05-12 04:06:41 -07:00
|
|
|
// +build !notcpstat
|
2015-03-24 04:34:48 -07:00
|
|
|
|
|
|
|
package collector
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2022-03-21 03:38:05 -07:00
|
|
|
"syscall"
|
|
|
|
"unsafe"
|
2015-03-24 04:34:48 -07:00
|
|
|
|
2020-11-14 02:53:51 -08:00
|
|
|
"github.com/go-kit/log"
|
2022-03-21 03:38:05 -07:00
|
|
|
"github.com/mdlayher/netlink"
|
2015-03-24 04:34:48 -07:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
)
|
|
|
|
|
2017-02-28 08:44:53 -08:00
|
|
|
type tcpConnectionState int
|
2015-03-24 04:34:48 -07:00
|
|
|
|
|
|
|
const (
|
2017-02-28 08:44:53 -08:00
|
|
|
// TCP_ESTABLISHED
|
|
|
|
tcpEstablished tcpConnectionState = iota + 1
|
|
|
|
// TCP_SYN_SENT
|
|
|
|
tcpSynSent
|
|
|
|
// TCP_SYN_RECV
|
|
|
|
tcpSynRecv
|
|
|
|
// TCP_FIN_WAIT1
|
|
|
|
tcpFinWait1
|
|
|
|
// TCP_FIN_WAIT2
|
|
|
|
tcpFinWait2
|
|
|
|
// TCP_TIME_WAIT
|
|
|
|
tcpTimeWait
|
|
|
|
// TCP_CLOSE
|
|
|
|
tcpClose
|
|
|
|
// TCP_CLOSE_WAIT
|
|
|
|
tcpCloseWait
|
|
|
|
// TCP_LAST_ACK
|
|
|
|
tcpLastAck
|
|
|
|
// TCP_LISTEN
|
|
|
|
tcpListen
|
|
|
|
// TCP_CLOSING
|
|
|
|
tcpClosing
|
2020-03-31 01:46:32 -07:00
|
|
|
// TCP_RX_BUFFER
|
|
|
|
tcpRxQueuedBytes
|
|
|
|
// TCP_TX_BUFFER
|
|
|
|
tcpTxQueuedBytes
|
2015-03-24 04:34:48 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
type tcpStatCollector struct {
|
2019-12-31 08:19:37 -08:00
|
|
|
desc typedDesc
|
|
|
|
logger log.Logger
|
2015-03-24 04:34:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2017-09-28 06:06:26 -07:00
|
|
|
registerCollector("tcpstat", defaultDisabled, NewTCPStatCollector)
|
2015-03-24 04:34:48 -07:00
|
|
|
}
|
|
|
|
|
2017-05-14 04:23:57 -07:00
|
|
|
// NewTCPStatCollector returns a new Collector exposing network stats.
|
2019-12-31 08:19:37 -08:00
|
|
|
func NewTCPStatCollector(logger log.Logger) (Collector, error) {
|
2015-03-24 04:34:48 -07:00
|
|
|
return &tcpStatCollector{
|
2016-12-28 06:21:31 -08:00
|
|
|
desc: typedDesc{prometheus.NewDesc(
|
2017-09-28 06:06:26 -07:00
|
|
|
prometheus.BuildFQName(namespace, "tcp", "connection_states"),
|
2016-12-28 06:21:31 -08:00
|
|
|
"Number of connection states.",
|
|
|
|
[]string{"state"}, nil,
|
|
|
|
), prometheus.GaugeValue},
|
2019-12-31 08:19:37 -08:00
|
|
|
logger: logger,
|
2015-03-24 04:34:48 -07:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2022-03-21 03:38:05 -07:00
|
|
|
// InetDiagSockID (inet_diag_sockid) contains the socket identity.
|
|
|
|
// https://github.com/torvalds/linux/blob/v4.0/include/uapi/linux/inet_diag.h#L13
|
|
|
|
type InetDiagSockID struct {
|
|
|
|
SourcePort [2]byte
|
|
|
|
DestPort [2]byte
|
|
|
|
SourceIP [4][4]byte
|
|
|
|
DestIP [4][4]byte
|
|
|
|
Interface uint32
|
|
|
|
Cookie [2]uint32
|
|
|
|
}
|
|
|
|
|
|
|
|
// InetDiagReqV2 (inet_diag_req_v2) is used to request diagnostic data.
|
|
|
|
// https://github.com/torvalds/linux/blob/v4.0/include/uapi/linux/inet_diag.h#L37
|
|
|
|
type InetDiagReqV2 struct {
|
|
|
|
Family uint8
|
|
|
|
Protocol uint8
|
|
|
|
Ext uint8
|
|
|
|
Pad uint8
|
|
|
|
States uint32
|
|
|
|
ID InetDiagSockID
|
|
|
|
}
|
|
|
|
|
|
|
|
const sizeOfDiagRequest = 0x38
|
|
|
|
|
|
|
|
func (req *InetDiagReqV2) Serialize() []byte {
|
|
|
|
return (*(*[sizeOfDiagRequest]byte)(unsafe.Pointer(req)))[:]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (req *InetDiagReqV2) Len() int {
|
|
|
|
return sizeOfDiagRequest
|
|
|
|
}
|
|
|
|
|
|
|
|
type InetDiagMsg struct {
|
|
|
|
Family uint8
|
|
|
|
State uint8
|
|
|
|
Timer uint8
|
|
|
|
Retrans uint8
|
|
|
|
ID InetDiagSockID
|
|
|
|
Expires uint32
|
|
|
|
RQueue uint32
|
|
|
|
WQueue uint32
|
|
|
|
UID uint32
|
|
|
|
Inode uint32
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseInetDiagMsg(b []byte) *InetDiagMsg {
|
|
|
|
return (*InetDiagMsg)(unsafe.Pointer(&b[0]))
|
|
|
|
}
|
|
|
|
|
2017-02-28 10:47:20 -08:00
|
|
|
func (c *tcpStatCollector) Update(ch chan<- prometheus.Metric) error {
|
2022-03-21 03:38:05 -07:00
|
|
|
tcpStats, err := getTCPStats(syscall.AF_INET)
|
2015-03-24 04:34:48 -07:00
|
|
|
if err != nil {
|
2020-06-15 13:27:14 -07:00
|
|
|
return fmt.Errorf("couldn't get tcpstats: %w", err)
|
2015-03-24 04:34:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// if enabled ipv6 system
|
2022-03-21 03:38:05 -07:00
|
|
|
if _, hasIPv6 := os.Stat(procFilePath("net/tcp6")); hasIPv6 == nil {
|
|
|
|
tcp6Stats, err := getTCPStats(syscall.AF_INET6)
|
2015-03-24 04:34:48 -07:00
|
|
|
if err != nil {
|
2020-06-15 13:27:14 -07:00
|
|
|
return fmt.Errorf("couldn't get tcp6stats: %w", err)
|
2015-03-24 04:34:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
for st, value := range tcp6Stats {
|
|
|
|
tcpStats[st] += value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for st, value := range tcpStats {
|
2016-12-28 06:21:31 -08:00
|
|
|
ch <- c.desc.mustNewConstMetric(value, st.String())
|
2015-03-24 04:34:48 -07:00
|
|
|
}
|
2022-03-21 03:38:05 -07:00
|
|
|
|
2017-02-28 08:44:53 -08:00
|
|
|
return nil
|
2015-03-24 04:34:48 -07:00
|
|
|
}
|
|
|
|
|
2022-03-21 03:38:05 -07:00
|
|
|
func getTCPStats(family uint8) (map[tcpConnectionState]float64, error) {
|
|
|
|
const TCPFAll = 0xFFF
|
|
|
|
const InetDiagInfo = 2
|
|
|
|
const SockDiagByFamily = 20
|
|
|
|
|
|
|
|
conn, err := netlink.Dial(syscall.NETLINK_INET_DIAG, nil)
|
2015-03-24 04:34:48 -07:00
|
|
|
if err != nil {
|
2022-03-21 03:38:05 -07:00
|
|
|
return nil, fmt.Errorf("couldn't connect netlink: %w", err)
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
msg := netlink.Message{
|
|
|
|
Header: netlink.Header{
|
|
|
|
Type: SockDiagByFamily,
|
|
|
|
Flags: syscall.NLM_F_REQUEST | syscall.NLM_F_DUMP,
|
|
|
|
},
|
|
|
|
Data: (&InetDiagReqV2{
|
|
|
|
Family: family,
|
|
|
|
Protocol: syscall.IPPROTO_TCP,
|
|
|
|
States: TCPFAll,
|
|
|
|
Ext: 0 | 1<<(InetDiagInfo-1),
|
|
|
|
}).Serialize(),
|
2015-03-24 04:34:48 -07:00
|
|
|
}
|
|
|
|
|
2022-03-21 03:38:05 -07:00
|
|
|
messages, err := conn.Execute(msg)
|
2019-07-08 06:53:14 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-03-24 04:34:48 -07:00
|
|
|
|
2022-03-21 03:38:05 -07:00
|
|
|
return parseTCPStats(messages)
|
|
|
|
}
|
2020-03-31 01:46:32 -07:00
|
|
|
|
2022-03-21 03:38:05 -07:00
|
|
|
func parseTCPStats(msgs []netlink.Message) (map[tcpConnectionState]float64, error) {
|
|
|
|
tcpStats := map[tcpConnectionState]float64{}
|
2015-03-24 04:34:48 -07:00
|
|
|
|
2022-03-21 03:38:05 -07:00
|
|
|
for _, m := range msgs {
|
|
|
|
msg := parseInetDiagMsg(m.Data)
|
2020-03-31 01:46:32 -07:00
|
|
|
|
2022-03-21 03:38:05 -07:00
|
|
|
tcpStats[tcpTxQueuedBytes] += float64(msg.WQueue)
|
|
|
|
tcpStats[tcpRxQueuedBytes] += float64(msg.RQueue)
|
|
|
|
tcpStats[tcpConnectionState(msg.State)]++
|
2015-03-24 04:34:48 -07:00
|
|
|
}
|
|
|
|
|
2019-07-08 06:53:14 -07:00
|
|
|
return tcpStats, nil
|
2015-03-24 04:34:48 -07:00
|
|
|
}
|
|
|
|
|
2017-02-28 08:44:53 -08:00
|
|
|
func (st tcpConnectionState) String() string {
|
2015-03-24 04:34:48 -07:00
|
|
|
switch st {
|
2017-02-28 08:44:53 -08:00
|
|
|
case tcpEstablished:
|
2015-03-24 04:34:48 -07:00
|
|
|
return "established"
|
2017-02-28 08:44:53 -08:00
|
|
|
case tcpSynSent:
|
2015-03-24 04:34:48 -07:00
|
|
|
return "syn_sent"
|
2017-02-28 08:44:53 -08:00
|
|
|
case tcpSynRecv:
|
2015-03-24 04:34:48 -07:00
|
|
|
return "syn_recv"
|
2017-02-28 08:44:53 -08:00
|
|
|
case tcpFinWait1:
|
2015-03-24 04:34:48 -07:00
|
|
|
return "fin_wait1"
|
2017-02-28 08:44:53 -08:00
|
|
|
case tcpFinWait2:
|
2015-03-24 04:34:48 -07:00
|
|
|
return "fin_wait2"
|
2017-02-28 08:44:53 -08:00
|
|
|
case tcpTimeWait:
|
2015-03-24 04:34:48 -07:00
|
|
|
return "time_wait"
|
2017-02-28 08:44:53 -08:00
|
|
|
case tcpClose:
|
2015-03-24 04:34:48 -07:00
|
|
|
return "close"
|
2017-02-28 08:44:53 -08:00
|
|
|
case tcpCloseWait:
|
2015-03-24 04:34:48 -07:00
|
|
|
return "close_wait"
|
2017-02-28 08:44:53 -08:00
|
|
|
case tcpLastAck:
|
2015-03-24 04:34:48 -07:00
|
|
|
return "last_ack"
|
2017-02-28 08:44:53 -08:00
|
|
|
case tcpListen:
|
2015-03-24 04:34:48 -07:00
|
|
|
return "listen"
|
2017-02-28 08:44:53 -08:00
|
|
|
case tcpClosing:
|
2015-03-24 04:34:48 -07:00
|
|
|
return "closing"
|
2020-03-31 01:46:32 -07:00
|
|
|
case tcpRxQueuedBytes:
|
|
|
|
return "rx_queued_bytes"
|
|
|
|
case tcpTxQueuedBytes:
|
|
|
|
return "tx_queued_bytes"
|
2015-03-24 04:34:48 -07:00
|
|
|
default:
|
|
|
|
return "unknown"
|
|
|
|
}
|
|
|
|
}
|