-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Add TCP state collector #56
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+181
−0
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| sl local_address rem_address st tx_queue rx_queue tr tm->when retrnsmt uid timeout inode | ||
| 0: 00000000:0016 00000000:0000 0A 00000000:00000000 00:00000000 00000000 0 0 2740 1 ffff88003d3af3c0 100 0 0 10 0 | ||
| 1: 0F02000A:0016 0202000A:8B6B 01 00000000:00000000 02:000AC99B 00000000 0 0 3652 4 ffff88003d3ae040 21 4 31 47 46 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,150 @@ | ||
| // +build !notcpstat | ||
|
|
||
| package collector | ||
|
|
||
| import ( | ||
| "bufio" | ||
| "fmt" | ||
| "io" | ||
| "os" | ||
| "strconv" | ||
| "strings" | ||
|
|
||
| "github.com/prometheus/client_golang/prometheus" | ||
| ) | ||
|
|
||
| const ( | ||
| procTCPStat = "/proc/net/tcp" | ||
| procTCP6Stat = "/proc/net/tcp6" | ||
| ) | ||
|
|
||
| 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 { | ||
| config Config | ||
| metric *prometheus.GaugeVec | ||
| } | ||
|
|
||
| func init() { | ||
| Factories["tcpstat"] = NewTCPStatCollector | ||
| } | ||
|
|
||
| // NewTCPStatCollector takes a config struct and returns | ||
| // a new Collector exposing network stats. | ||
| func NewTCPStatCollector(config Config) (Collector, error) { | ||
| return &tcpStatCollector{ | ||
| config: config, | ||
| 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) { | ||
| tcpStats, err := getTCPStats(procTCPStat) | ||
| if err != nil { | ||
| return fmt.Errorf("couldn't get tcpstats: %s", err) | ||
| } | ||
|
|
||
| // if enabled ipv6 system | ||
| if _, hasIPv6 := os.Stat(procTCP6Stat); hasIPv6 == nil { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, I have added modification. (93d21b5) |
||
| tcp6Stats, err := getTCPStats(procTCP6Stat) | ||
| 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" | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| package collector | ||
|
|
||
| import ( | ||
| "os" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestTCPStat(t *testing.T) { | ||
| file, err := os.Open("fixtures/tcpstat") | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| defer file.Close() | ||
|
|
||
| tcpStats, err := parseTCPStats(file) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| if want, got := 1, int(tcpStats[TCP_ESTABLISHED]); want != got { | ||
| t.Errorf("want tcpstat number of established state %d, got %d", want, got) | ||
| } | ||
|
|
||
| if want, got := 1, int(tcpStats[TCP_LISTEN]); want != got { | ||
| t.Errorf("want tcpstat number of listen state %d, got %d", want, got) | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if it'd also make sense to differentiate IPv4 / IPv6 via a label, but not sure about the practical use. @brian-brazil any opinion on this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it does make sense
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can imagine uses for it if say you were debugging an issue with a buggy v6 client/server. I'm not too bothered either way, I think I'd tend to keep it simple for now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do not need IPv4 / IPv6 labels, but I think good to label as an option.