Node_Exporter/collector/tcpstat_linux.go

168 lines
3.5 KiB
Go
Raw Normal View History

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.
// +build !notcpstat
2015-03-24 12:34:48 +01:00
package collector
import (
"fmt"
"io"
"io/ioutil"
2015-03-24 12:34:48 +01:00
"os"
"strconv"
"strings"
"github.com/go-kit/kit/log"
2015-03-24 12:34:48 +01:00
"github.com/prometheus/client_golang/prometheus"
)
2017-02-28 17:44:53 +01:00
type tcpConnectionState int
2015-03-24 12:34:48 +01:00
const (
2017-02-28 17:44:53 +01: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
2015-03-24 12:34:48 +01:00
)
type tcpStatCollector struct {
desc typedDesc
logger log.Logger
2015-03-24 12:34:48 +01:00
}
func init() {
registerCollector("tcpstat", defaultDisabled, NewTCPStatCollector)
2015-03-24 12:34:48 +01:00
}
2017-05-14 13:23:57 +02:00
// NewTCPStatCollector returns a new Collector exposing network stats.
func NewTCPStatCollector(logger log.Logger) (Collector, error) {
2015-03-24 12:34:48 +01:00
return &tcpStatCollector{
desc: typedDesc{prometheus.NewDesc(
prometheus.BuildFQName(namespace, "tcp", "connection_states"),
"Number of connection states.",
[]string{"state"}, nil,
), prometheus.GaugeValue},
logger: logger,
2015-03-24 12:34:48 +01:00
}, nil
}
func (c *tcpStatCollector) Update(ch chan<- prometheus.Metric) error {
tcpStats, err := getTCPStats(procFilePath("net/tcp"))
2015-03-24 12:34:48 +01:00
if err != nil {
return fmt.Errorf("couldn't get tcpstats: %s", err)
}
// if enabled ipv6 system
tcp6File := procFilePath("net/tcp6")
if _, hasIPv6 := os.Stat(tcp6File); hasIPv6 == nil {
tcp6Stats, err := getTCPStats(tcp6File)
2015-03-24 12:34:48 +01: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 {
ch <- c.desc.mustNewConstMetric(value, st.String())
2015-03-24 12:34:48 +01:00
}
2017-02-28 17:44:53 +01:00
return nil
2015-03-24 12:34:48 +01:00
}
2017-02-28 17:44:53 +01:00
func getTCPStats(statsFile string) (map[tcpConnectionState]float64, error) {
2015-03-24 12:34:48 +01:00
file, err := os.Open(statsFile)
if err != nil {
return nil, err
}
defer file.Close()
return parseTCPStats(file)
}
2017-02-28 17:44:53 +01:00
func parseTCPStats(r io.Reader) (map[tcpConnectionState]float64, error) {
tcpStats := map[tcpConnectionState]float64{}
contents, err := ioutil.ReadAll(r)
if err != nil {
return nil, err
}
2015-03-24 12:34:48 +01:00
for _, line := range strings.Split(string(contents), "\n")[1:] {
parts := strings.Fields(line)
2015-03-24 12:34:48 +01:00
if len(parts) == 0 {
continue
}
if len(parts) < 4 {
return nil, fmt.Errorf("invalid TCP stats line: %q", line)
}
2015-03-24 12:34:48 +01:00
st, err := strconv.ParseInt(parts[3], 16, 8)
if err != nil {
return nil, err
}
2017-02-28 17:44:53 +01:00
tcpStats[tcpConnectionState(st)]++
2015-03-24 12:34:48 +01:00
}
return tcpStats, nil
2015-03-24 12:34:48 +01:00
}
2017-02-28 17:44:53 +01:00
func (st tcpConnectionState) String() string {
2015-03-24 12:34:48 +01:00
switch st {
2017-02-28 17:44:53 +01:00
case tcpEstablished:
2015-03-24 12:34:48 +01:00
return "established"
2017-02-28 17:44:53 +01:00
case tcpSynSent:
2015-03-24 12:34:48 +01:00
return "syn_sent"
2017-02-28 17:44:53 +01:00
case tcpSynRecv:
2015-03-24 12:34:48 +01:00
return "syn_recv"
2017-02-28 17:44:53 +01:00
case tcpFinWait1:
2015-03-24 12:34:48 +01:00
return "fin_wait1"
2017-02-28 17:44:53 +01:00
case tcpFinWait2:
2015-03-24 12:34:48 +01:00
return "fin_wait2"
2017-02-28 17:44:53 +01:00
case tcpTimeWait:
2015-03-24 12:34:48 +01:00
return "time_wait"
2017-02-28 17:44:53 +01:00
case tcpClose:
2015-03-24 12:34:48 +01:00
return "close"
2017-02-28 17:44:53 +01:00
case tcpCloseWait:
2015-03-24 12:34:48 +01:00
return "close_wait"
2017-02-28 17:44:53 +01:00
case tcpLastAck:
2015-03-24 12:34:48 +01:00
return "last_ack"
2017-02-28 17:44:53 +01:00
case tcpListen:
2015-03-24 12:34:48 +01:00
return "listen"
2017-02-28 17:44:53 +01:00
case tcpClosing:
2015-03-24 12:34:48 +01:00
return "closing"
default:
return "unknown"
}
}