Skip to content

Commit

Permalink
Add target label support
Browse files Browse the repository at this point in the history
  • Loading branch information
syepes committed Mar 4, 2022
1 parent 4be0d2a commit 2faa635
Show file tree
Hide file tree
Showing 19 changed files with 254 additions and 75 deletions.
2 changes: 1 addition & 1 deletion .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ archives:
- README.md
- LICENSE.md
- network_exporter.yml
wrap_in_directory: true
wrap_in_directory: "true"
snapshot:
name_template: SNAPSHOT-{{ .Commit }}
nfpms:
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
BUILDX_VER=v0.7.0
BUILDX_VER=v0.8.0-rc1
IMAGE_NAME=syepes/network_exporter
VERSION?=latest

Expand Down
29 changes: 20 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ This exporter gathers either ICMP, MTR, TCP Port or HTTP Get stats and exports t
- `http_get_seconds{type=ContentTransfer}`: ContentTransfer connection drill down time in seconds
- `http_get_seconds{type=Total}`: Total connection time in seconds

Each metric contains the labels:
Each metric contains the below labels and additionally the ones added in the configuration file.

- `name` (ALL: The target name)
- `target` (ALL: the target IP Address)
Expand All @@ -82,12 +82,14 @@ Each metric contains the labels:
## Building and running the software

### Prerequisites for Linux
```

```console
apt update
apt install docker
apt install docker.io
touch network_exporter.yml
```

### Local Build

```console
Expand Down Expand Up @@ -117,14 +119,16 @@ To see all available configuration flags:
./network_exporter -h
```

Most of the configuration is set in the YAML based config file:
- network_exporter.yml should be edited before building the docker container
The configuration (YAML) is mainly separated into three sections Main, Protocols and Targets.
The file `network_exporter.yml` can be either edited before building the docker container or changed it runtime.

```yaml
# Main Config
conf:
refresh: 15m
nameserver: 192.168.0.1:53

# Specific Protocol settings
icmp:
interval: 3s
timeout: 1s
Expand All @@ -144,13 +148,17 @@ http_get:
interval: 15m
timeout: 5s

# Target list and settings
targets:
- name: internal
host: 192.168.0.1
type: ICMP
probe:
- hostname1
- hostname2
labels:
dc: home
rack: a1
- name: google-dns1
host: 8.8.8.8
type: ICMP
Expand Down Expand Up @@ -181,20 +189,24 @@ If the host field of a target contains a SRV record with the format `_<service>.
Every field of the parent target with a SRV record will be inherited by sub targets except `name` and `host`

SRV record
```

```console
_connectivity-check._icmp.example.com. 86400 IN SRV 10 5 8 server.example.com.
_connectivity-check._icmp.example.com. 86400 IN SRV 10 5 8 server2.example.com.
_connectivity-check._icmp.example.com. 86400 IN SRV 10 5 8 server3.example.com.
```

Configuration reference
```

```yaml
- name: test-srv-record
host: _connectivity-check._icmp.example.com
type: ICMP
```

Will be resolved to 3 separate targets:
```

```yaml
- name: server.example.com
host: server.example.com
type: ICMP
Expand All @@ -206,7 +218,6 @@ Will be resolved to 3 separate targets:
type: ICMP
```

## Deployment

This deployment example will permit you to have as many Ping Stations as you need (LAN or WIFI) devices but at the same time decoupling the data collection from the storage and visualization.
Expand All @@ -223,4 +234,4 @@ If you have any idea for an improvement or find a bug do not hesitate in opening
## License

All content is distributed under the [Apache 2.0 License](http://www.apache.org/licenses/LICENSE-2.0)
Copyright &copy; 2020, [Sebastian YEPES](mailto:syepes@gmail.com)
Copyright &copy; 2020-2022, [Sebastian YEPES](mailto:syepes@gmail.com)
12 changes: 11 additions & 1 deletion collector/collector_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ var (
type HTTPGet struct {
Monitor *monitor.HTTPGet
metrics map[string]*http.HTTPReturn
labels map[string]map[string]string
}

// Describe prom
Expand All @@ -39,10 +40,14 @@ func (p *HTTPGet) Collect(ch chan<- prometheus.Metric) {
httpMutex.Lock()
defer httpMutex.Unlock()

if m := p.Monitor.Export(); len(m) > 0 {
if m := p.Monitor.ExportMetrics(); len(m) > 0 {
p.metrics = m
}

if l := p.Monitor.ExportLabels(); len(l) > 0 {
p.labels = l
}

if len(p.metrics) > 0 {
ch <- prometheus.MustNewConstMetric(httpStateDesc, prometheus.GaugeValue, 1)
} else {
Expand All @@ -54,6 +59,11 @@ func (p *HTTPGet) Collect(ch chan<- prometheus.Metric) {
targets = append(targets, target)
l := strings.SplitN(target, " ", 2)
l = append(l, metric.DestAddr)
l2 := prometheus.Labels(p.labels[target])

httpTimeDesc = prometheus.NewDesc("http_get_seconds", "HTTP Get Drill Down time in seconds", append(httpLabelNames, "type"), l2)
httpSizeDesc = prometheus.NewDesc("http_get_content_bytes", "HTTP Get Content Size in bytes", httpLabelNames, l2)
httpStatusDesc = prometheus.NewDesc("http_get_status", "HTTP Get Status", httpLabelNames, l2)

if metric.Success {
ch <- prometheus.MustNewConstMetric(httpStatusDesc, prometheus.GaugeValue, float64(metric.Status), l...)
Expand Down
11 changes: 10 additions & 1 deletion collector/collector_mtr.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ var (
type MTR struct {
Monitor *monitor.MTR
metrics map[string]*mtr.MtrResult
labels map[string]map[string]string
}

// Describe prom
Expand All @@ -37,10 +38,14 @@ func (p *MTR) Collect(ch chan<- prometheus.Metric) {
mtrMutex.Lock()
defer mtrMutex.Unlock()

if m := p.Monitor.Export(); len(m) > 0 {
if m := p.Monitor.ExportMetrics(); len(m) > 0 {
p.metrics = m
}

if l := p.Monitor.ExportLabels(); len(l) > 0 {
p.labels = l
}

if len(p.metrics) > 0 {
ch <- prometheus.MustNewConstMetric(mtrStateDesc, prometheus.GaugeValue, 1)
} else {
Expand All @@ -51,6 +56,10 @@ func (p *MTR) Collect(ch chan<- prometheus.Metric) {
for target, metric := range p.metrics {
targets = append(targets, target)
l := []string{target, metric.DestAddr}
l2 := prometheus.Labels(p.labels[target])

mtrDesc = prometheus.NewDesc("mtr_rtt_seconds", "Round Trip Time in seconds", append(mtrLabelNames, "type"), l2)
mtrHopsDesc = prometheus.NewDesc("mtr_hops", "Number of route hops", []string{"name", "target"}, l2)

ch <- prometheus.MustNewConstMetric(mtrHopsDesc, prometheus.GaugeValue, float64(len(metric.Hops)), l...)
for _, hop := range metric.Hops {
Expand Down
12 changes: 11 additions & 1 deletion collector/collector_ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ var (
type PING struct {
Monitor *monitor.PING
metrics map[string]*ping.PingResult
labels map[string]map[string]string
}

// Describe prom
Expand All @@ -38,10 +39,14 @@ func (p *PING) Collect(ch chan<- prometheus.Metric) {
icmpMutex.Lock()
defer icmpMutex.Unlock()

if m := p.Monitor.Export(); len(m) > 0 {
if m := p.Monitor.ExportMetrics(); len(m) > 0 {
p.metrics = m
}

if l := p.Monitor.ExportLabels(); len(l) > 0 {
p.labels = l
}

if len(p.metrics) > 0 {
ch <- prometheus.MustNewConstMetric(icmpStateDesc, prometheus.GaugeValue, 1)
} else {
Expand All @@ -52,6 +57,11 @@ func (p *PING) Collect(ch chan<- prometheus.Metric) {
for target, metric := range p.metrics {
targets = append(targets, target)
l := []string{target, metric.DestAddr}
l2 := prometheus.Labels(p.labels[target])

icmpStatusDesc = prometheus.NewDesc("ping_status", "Ping Status", icmpLabelNames, l2)
icmpRttDesc = prometheus.NewDesc("ping_rtt_seconds", "Round Trip Time in seconds", append(icmpLabelNames, "type"), l2)
icmpLossDesc = prometheus.NewDesc("ping_loss_percent", "Packet loss in percent", icmpLabelNames, l2)

if metric.Success {
ch <- prometheus.MustNewConstMetric(icmpStatusDesc, prometheus.GaugeValue, 1, l...)
Expand Down
11 changes: 10 additions & 1 deletion collector/collector_tcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ var (
type TCP struct {
Monitor *monitor.TCPPort
metrics map[string]*tcp.TCPPortReturn
labels map[string]map[string]string
}

// Describe prom
Expand All @@ -37,10 +38,14 @@ func (p *TCP) Collect(ch chan<- prometheus.Metric) {
tcpMutex.Lock()
defer tcpMutex.Unlock()

if m := p.Monitor.Export(); len(m) > 0 {
if m := p.Monitor.ExportMetrics(); len(m) > 0 {
p.metrics = m
}

if l := p.Monitor.ExportLabels(); len(l) > 0 {
p.labels = l
}

if len(p.metrics) > 0 {
ch <- prometheus.MustNewConstMetric(tcpStateDesc, prometheus.GaugeValue, 1)
} else {
Expand All @@ -53,6 +58,10 @@ func (p *TCP) Collect(ch chan<- prometheus.Metric) {
l := strings.SplitN(target, " ", 2)
l = append(l, metric.DestAddr)
l = append(l, metric.DestPort)
l2 := prometheus.Labels(p.labels[target])

tcpTimeDesc = prometheus.NewDesc("tcp_connection_seconds", "Connection time in seconds", tcpLabelNames, l2)
tcpStatusDesc = prometheus.NewDesc("tcp_connection_status", "Connection Status", tcpLabelNames, l2)

ch <- prometheus.MustNewConstMetric(tcpTimeDesc, prometheus.GaugeValue, metric.ConTime.Seconds(), l...)

Expand Down
12 changes: 5 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,19 @@ go 1.17
module github.com/syepes/network_exporter

require (
github.com/alecthomas/units v0.0.0-20210927113745-59d0afb8317a // indirect
github.com/go-kit/kit v0.12.0
github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137 // indirect
github.com/go-kit/log v0.2.0
github.com/golang/protobuf v1.5.2 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/client_golang v1.11.0
github.com/prometheus/client_golang v1.12.1
github.com/prometheus/common v0.32.1
github.com/prometheus/procfs v0.7.3 // indirect
golang.org/x/net v0.0.0-20211206223403-eba003a116a9
golang.org/x/sys v0.0.0-20211205182925-97ca703d548d // indirect
golang.org/x/net v0.0.0-20220225172249-27dd8689420f
golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9 // indirect
gopkg.in/alecthomas/kingpin.v2 v2.2.6
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
)

require github.com/go-kit/log v0.2.0

require (
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 // indirect
github.com/beorn7/perks v1.0.1 // indirect
Expand Down
19 changes: 19 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRF
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk541a8SKzHPHnH3zbiI+7dagKZ0cgpgrD7Fyho=
github.com/alecthomas/units v0.0.0-20210927113745-59d0afb8317a h1:E/8AP5dFtMhl5KPJz66Kt9G0n+7Sn41Fy1wv9/jHOrc=
github.com/alecthomas/units v0.0.0-20210927113745-59d0afb8317a/go.mod h1:OMCwj8VM1Kc9e19TLln2VL61YJF0x1XFtfdL4JdbSyE=
github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137 h1:s6gZFSlWYmbqAuRjVTiNNhvNRfY2Wxp9nhfyel4rklc=
github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137/go.mod h1:OMCwj8VM1Kc9e19TLln2VL61YJF0x1XFtfdL4JdbSyE=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
Expand Down Expand Up @@ -138,6 +140,7 @@ github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/json-iterator/go v1.1.11/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
Expand All @@ -157,6 +160,7 @@ github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJ
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
Expand All @@ -170,6 +174,8 @@ github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5Fsn
github.com/prometheus/client_golang v1.7.1/go.mod h1:PY5Wy2awLA44sXw4AOSfFBetzPP4j5+D6mVACh+pe2M=
github.com/prometheus/client_golang v1.11.0 h1:HNkLOAEQMIDv/K+04rukrLx6ch7msSRwf3/SASFAGtQ=
github.com/prometheus/client_golang v1.11.0/go.mod h1:Z6t4BnS23TR94PD6BsDNk8yVqroYurpAkEiz0P2BEV0=
github.com/prometheus/client_golang v1.12.1 h1:ZiaPsmm9uiBeaSMRznKsCDNtPCS0T3JVDGF+06gjBzk=
github.com/prometheus/client_golang v1.12.1/go.mod h1:3Z9XVyYiZYEO+YQWt3RD2R3jrbd179Rt297l4aS6nDY=
github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo=
github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
Expand Down Expand Up @@ -275,6 +281,12 @@ golang.org/x/net v0.0.0-20211201190559-0a0e4e1bb54c h1:WtYZ93XtWSO5KlOMgPZu7hXY9
golang.org/x/net v0.0.0-20211201190559-0a0e4e1bb54c/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20211206223403-eba003a116a9 h1:HhGRSJWlxVO54+s9MeOVrZrbnwv+6oZQIvsUrMUte7U=
golang.org/x/net v0.0.0-20211206223403-eba003a116a9/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20220105145211-5b0dc2dfae98 h1:+6WJMRLHlD7X7frgp7TUZ36RnQzSf9wVVTNakEp+nqY=
golang.org/x/net v0.0.0-20220105145211-5b0dc2dfae98/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd h1:O7DYs+zxREGLKzKoMQrtrEacpb0ZVXA5rIwylE2Xchk=
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
golang.org/x/net v0.0.0-20220225172249-27dd8689420f h1:oA4XRj0qtSt8Yo1Zms0CUlsT3KG69V2UGQWPBxujDmc=
golang.org/x/net v0.0.0-20220225172249-27dd8689420f/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
Expand Down Expand Up @@ -331,6 +343,13 @@ golang.org/x/sys v0.0.0-20211124211545-fe61309f8881 h1:TyHqChC80pFkXWraUUf6RuB5I
golang.org/x/sys v0.0.0-20211124211545-fe61309f8881/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211205182925-97ca703d548d h1:FjkYO/PPp4Wi0EAUOVLxePm7qVW4r4ctbWpURyuOD0E=
golang.org/x/sys v0.0.0-20211205182925-97ca703d548d/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e h1:fLOSk5Q00efkSvAm+4xcoXD+RRmLmmulPn5I3Y9F2EM=
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220128215802-99c3d69c2c27 h1:XDXtA5hveEEV8JB2l7nhMTp3t3cHp9ZpwcdjqyEWLlo=
golang.org/x/sys v0.0.0-20220128215802-99c3d69c2c27/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9 h1:nhht2DYV/Sn3qOayu8lM+cU1ii9sTLUeBQwQQfUHtrs=
golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"gopkg.in/alecthomas/kingpin.v2"
)

const version string = "1.4.0"
const version string = "1.5.0"

var (
listenAddress = kingpin.Flag("web.listen-address", "The address to listen on for HTTP requests").Default(":9427").String()
Expand Down
Loading

0 comments on commit 2faa635

Please sign in to comment.