2015-09-26 15:36:40 +00: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-12 11:06:41 +00:00
|
|
|
// +build !notcpstat
|
2015-03-24 11:34:48 +00:00
|
|
|
|
|
|
|
package collector
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
)
|
|
|
|
|
|
|
|
type TCPConnectionState int
|
|
|
|
|
|
|
|
const (
|
|
|
|
TCP_ESTABLISHED TCPConnectionState = iota + 1
|
|
|
|
TCP_SYN_SENT
|
|
|
|
TCP_SYN_RECV
|
|
|
|
TCP_FIN_WAIT1
|
|
|
|
TCP_FIN_WAIT2
|
|
|
|
TCP_TIME_WAIT
|
|
|
|
TCP_CLOSE
|
|
|
|
TCP_CLOSE_WAIT
|
|
|
|
TCP_LAST_ACK
|
|
|
|
TCP_LISTEN
|
|
|
|
TCP_CLOSING
|
|
|
|
)
|
|
|
|
|
|
|
|
type tcpStatCollector struct {
|
|
|
|
metric *prometheus.GaugeVec
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
Factories["tcpstat"] = NewTCPStatCollector
|
|
|
|
}
|
|
|
|
|
2015-05-20 18:04:49 +00:00
|
|
|
// NewTCPStatCollector takes a returns
|
2015-03-24 11:34:48 +00:00
|
|
|
// a new Collector exposing network stats.
|
2015-05-20 18:04:49 +00:00
|
|
|
func NewTCPStatCollector() (Collector, error) {
|
2015-03-24 11:34:48 +00:00
|
|
|
return &tcpStatCollector{
|
|
|
|
metric: prometheus.NewGaugeVec(
|
|
|
|
prometheus.GaugeOpts{
|
|
|
|
Namespace: Namespace,
|
|
|
|
Name: "tcp_connection_states",
|
|
|
|
Help: "Number of connection states.",
|
|
|
|
},
|
|
|
|
[]string{"state"},
|
|
|
|
),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *tcpStatCollector) Update(ch chan<- prometheus.Metric) (err error) {
|
2015-09-26 12:53:46 +00:00
|
|
|
tcpStats, err := getTCPStats(procFilePath("net/tcp"))
|
2015-03-24 11:34:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("couldn't get tcpstats: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// if enabled ipv6 system
|
2015-09-26 12:53:46 +00:00
|
|
|
tcp6File := procFilePath("net/tcp6")
|
|
|
|
if _, hasIPv6 := os.Stat(tcp6File); hasIPv6 == nil {
|
|
|
|
tcp6Stats, err := getTCPStats(tcp6File)
|
2015-03-24 11:34:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("couldn't get tcp6stats: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for st, value := range tcp6Stats {
|
|
|
|
tcpStats[st] += value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for st, value := range tcpStats {
|
|
|
|
c.metric.WithLabelValues(st.String()).Set(value)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.metric.Collect(ch)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func getTCPStats(statsFile string) (map[TCPConnectionState]float64, error) {
|
|
|
|
file, err := os.Open(statsFile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
return parseTCPStats(file)
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseTCPStats(r io.Reader) (map[TCPConnectionState]float64, error) {
|
|
|
|
var (
|
|
|
|
tcpStats = map[TCPConnectionState]float64{}
|
|
|
|
scanner = bufio.NewScanner(r)
|
|
|
|
)
|
|
|
|
|
|
|
|
for scanner.Scan() {
|
|
|
|
parts := strings.Fields(scanner.Text())
|
|
|
|
if len(parts) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(parts[0], "sl") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
st, err := strconv.ParseInt(parts[3], 16, 8)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
tcpStats[TCPConnectionState(st)]++
|
|
|
|
}
|
|
|
|
|
|
|
|
return tcpStats, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (st TCPConnectionState) String() string {
|
|
|
|
switch st {
|
|
|
|
case TCP_ESTABLISHED:
|
|
|
|
return "established"
|
|
|
|
case TCP_SYN_SENT:
|
|
|
|
return "syn_sent"
|
|
|
|
case TCP_SYN_RECV:
|
|
|
|
return "syn_recv"
|
|
|
|
case TCP_FIN_WAIT1:
|
|
|
|
return "fin_wait1"
|
|
|
|
case TCP_FIN_WAIT2:
|
|
|
|
return "fin_wait2"
|
|
|
|
case TCP_TIME_WAIT:
|
|
|
|
return "time_wait"
|
|
|
|
case TCP_CLOSE:
|
|
|
|
return "close"
|
|
|
|
case TCP_CLOSE_WAIT:
|
|
|
|
return "close_wait"
|
|
|
|
case TCP_LAST_ACK:
|
|
|
|
return "last_ack"
|
|
|
|
case TCP_LISTEN:
|
|
|
|
return "listen"
|
|
|
|
case TCP_CLOSING:
|
|
|
|
return "closing"
|
|
|
|
default:
|
|
|
|
return "unknown"
|
|
|
|
}
|
|
|
|
}
|