1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
|
// 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
package collector
import (
"bufio"
"fmt"
"io"
"os"
"strconv"
"strings"
"github.com/prometheus/client_golang/prometheus"
)
type tcpConnectionState int
const (
// 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
)
type tcpStatCollector struct {
desc typedDesc
}
func init() {
registerCollector("tcpstat", defaultDisabled, NewTCPStatCollector)
}
// NewTCPStatCollector returns a new Collector exposing network stats.
func NewTCPStatCollector() (Collector, error) {
return &tcpStatCollector{
desc: typedDesc{prometheus.NewDesc(
prometheus.BuildFQName(namespace, "tcp", "connection_states"),
"Number of connection states.",
[]string{"state"}, nil,
), prometheus.GaugeValue},
}, nil
}
func (c *tcpStatCollector) Update(ch chan<- prometheus.Metric) error {
tcpStats, err := getTCPStats(procFilePath("net/tcp"))
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)
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())
}
return nil
}
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 len(parts) < 4 {
return nil, fmt.Errorf("invalid TCP stats line: %q", scanner.Text())
}
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, scanner.Err()
}
func (st tcpConnectionState) String() string {
switch st {
case tcpEstablished:
return "established"
case tcpSynSent:
return "syn_sent"
case tcpSynRecv:
return "syn_recv"
case tcpFinWait1:
return "fin_wait1"
case tcpFinWait2:
return "fin_wait2"
case tcpTimeWait:
return "time_wait"
case tcpClose:
return "close"
case tcpCloseWait:
return "close_wait"
case tcpLastAck:
return "last_ack"
case tcpListen:
return "listen"
case tcpClosing:
return "closing"
default:
return "unknown"
}
}
|