-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
Socket summary module #6782
Socket summary module #6782
Changes from all commits
50e7963
101b550
7439d3a
1534a55
7c9a11a
abdc848
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
//// | ||
This file is generated! See scripts/docs_collector.py | ||
//// | ||
|
||
[[metricbeat-metricset-system-socket_summary]] | ||
=== System socket_summary metricset | ||
|
||
experimental[] | ||
|
||
include::../../../module/system/socket_summary/_meta/docs.asciidoc[] | ||
|
||
|
||
==== Fields | ||
|
||
For a description of each field in the metricset, see the | ||
<<exported-fields-system,exported fields>> section. | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
The System `socket_summary` metricset provides the summary of open network sockets in the host system. | ||
|
||
The following metrics are exported: | ||
* TCP | ||
* all: All connections | ||
* listening: All listening connections | ||
* UDP: | ||
* all: All connections | ||
|
||
This metricset is available on: | ||
|
||
- FreeBSD | ||
- Linux | ||
- macOS | ||
- Windows | ||
|
||
[float] | ||
=== Configuration | ||
|
||
There are no configuration options for this metricset. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
- name: socket.summary | ||
title: Socket summary | ||
type: group | ||
description: > | ||
Summary metrics of open sockets in the host system | ||
fields: | ||
- name: all | ||
type: group | ||
description: > | ||
All connections | ||
fields: | ||
- name: count | ||
type: integer | ||
description: > | ||
All open connections | ||
- name: listening | ||
type: integer | ||
description: > | ||
All listening ports | ||
- name: tcp | ||
type: group | ||
description: > | ||
All TCP connections | ||
fields: | ||
- name: all | ||
type: group | ||
description: > | ||
All TCP connections | ||
fields: | ||
- name: count | ||
type: integer | ||
description: > | ||
All open TCP connections | ||
- name: listening | ||
type: integer | ||
description: > | ||
All TCP listening ports | ||
- name: udp | ||
type: group | ||
description: > | ||
All UDP connections | ||
fields: | ||
- name: all | ||
type: group | ||
description: > | ||
All UDP connections | ||
fields: | ||
- name: count | ||
type: integer | ||
description: > | ||
All open TCP connections | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
// Licensed to Elasticsearch B.V. under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. Elasticsearch B.V. licenses this file to you 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. | ||
|
||
package socket_summary | ||
|
||
import ( | ||
"syscall" | ||
|
||
"github.com/elastic/beats/libbeat/common" | ||
|
||
"github.com/elastic/beats/libbeat/common/cfgwarn" | ||
"github.com/elastic/beats/metricbeat/mb" | ||
|
||
"github.com/shirou/gopsutil/net" | ||
) | ||
|
||
// init registers the MetricSet with the central registry as soon as the program | ||
// starts. The New function will be called later to instantiate an instance of | ||
// the MetricSet for each host defined in the module's configuration. After the | ||
// MetricSet has been created then Fetch will begin to be called periodically. | ||
func init() { | ||
mb.Registry.MustAddMetricSet("system", "socket_summary", New, | ||
mb.WithNamespace("system.socket.summary"), | ||
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. @ruflin can this cause some kind of collision with the socket metricset or with its fields definition? 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. In case we would also use Should we create a collision on day by accident, CI should detect multiple definitions of the same field. |
||
) | ||
} | ||
|
||
// MetricSet holds any configuration or state information. It must implement | ||
// the mb.MetricSet interface. And this is best achieved by embedding | ||
// mb.BaseMetricSet because it implements all of the required mb.MetricSet | ||
// interface methods except for Fetch. | ||
type MetricSet struct { | ||
mb.BaseMetricSet | ||
} | ||
|
||
// New creates a new instance of the MetricSet. New is responsible for unpacking | ||
// any MetricSet specific configuration options if there are any. | ||
func New(base mb.BaseMetricSet) (mb.MetricSet, error) { | ||
cfgwarn.Experimental("The socket_summary metricset is experimental.") | ||
|
||
return &MetricSet{ | ||
BaseMetricSet: base, | ||
}, nil | ||
} | ||
|
||
func calculateConnStats(conns []net.ConnectionStat) common.MapStr { | ||
var ( | ||
allConns = len(conns) | ||
allListening = 0 | ||
tcpConns = 0 | ||
tcpListening = 0 | ||
udpConns = 0 | ||
) | ||
|
||
for _, conn := range conns { | ||
if conn.Status == "LISTEN" { | ||
allListening++ | ||
} | ||
switch conn.Type { | ||
case syscall.SOCK_STREAM: | ||
tcpConns++ | ||
|
||
if conn.Status == "LISTEN" { | ||
tcpListening++ | ||
} | ||
case syscall.SOCK_DGRAM: | ||
udpConns++ | ||
} | ||
} | ||
|
||
return common.MapStr{ | ||
"all": common.MapStr{ | ||
"count": allConns, | ||
"listening": allListening, | ||
}, | ||
"tcp": common.MapStr{ | ||
"all": common.MapStr{ | ||
"count": tcpConns, | ||
"listening": tcpListening, | ||
}, | ||
}, | ||
"udp": common.MapStr{ | ||
"all": common.MapStr{ | ||
"count": udpConns, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
// Fetch methods implements the data gathering and data conversion to the right | ||
// format. It publishes the event which is then forwarded to the output. In case | ||
// of an error set the Error field of mb.Event or simply call report.Error(). | ||
func (m *MetricSet) Fetch(report mb.ReporterV2) { | ||
|
||
// all network connections | ||
conns, err := net.Connections("inet") | ||
|
||
if err != nil { | ||
report.Error(err) | ||
return | ||
} | ||
|
||
report.Event(mb.Event{ | ||
MetricSetFields: calculateConnStats(conns), | ||
}) | ||
} |
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.
don't use an underscore in package name