Skip to content
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

Add checked_total metric to tcp checker #16

Merged
merged 2 commits into from
Feb 12, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,25 @@ The application exposes Prometheus metrics on `/metrics` for general insight int
| ---------------------- | ------------------------------ | --------------------------------------------------------- |
| `strong_duckling_info` | `version`,`strongswan_version` | Metadata such as version info of the application it self. |

## TCP checker

Enable TCP checker metrics by setting `--tcp-checker` to continually try to establish TCP connections to a remote and report the results in logs and metrics.

| Name | Type | Description |
| ------------------------------------------------ | ------- | ----------------------------------------------- |
| `strong_duckling_tcp_checker_checked_total` | Counter | Total number of checks performed on the address |
| `strong_duckling_tcp_checker_connected_total` | Counter | Total number of changes to connected state |
| `strong_duckling_tcp_checker_disconnected_total` | Counter | Total number of changes to disconnected state |
| `strong_duckling_tcp_checker_open_info` | Gauge | Connection is open if value 1 otherwise 0 |

All metrics contains the labels based on the configured `address`, `port` and optionally `name`.

```
# strong-duckling --listen=:9100 --tcp-checker partner1:1.2.3.4:4500

strong_duckling_tcp_checker_open_info{name="partner1", address="1.2.3.4", port="4500"} 1
```

## IKE SA metrics

Enable Strongswan metrics by setting `--vici-socket` to a charon socket of a running strongswan process.
Expand All @@ -36,6 +55,7 @@ Usually this is `/var/run/charon.vici`.
| `strong_duckling_ike_sa_child_state_info` | Gauge | | Metadata on the state of the child SA |

## Local development setup

To use the test setup start a linux build watcher (requires nodemon) like this:

```bash
Expand All @@ -50,8 +70,8 @@ docker-compose up -d

This will start 2 linked docker containers each running:

* StrongSwan VPN
* A small nodejs HTTP server on :8080
* strong-duckling
- StrongSwan VPN
- A small nodejs HTTP server on :8080
- strong-duckling

The setup is configured to automatically connect the 2 containers using StrongSwan through an IKE v2 tunnel. The machines have added internal IPs `10.101.0.1` and `10.102.0.1`.
16 changes: 13 additions & 3 deletions internal/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func (pr *PrometheusReporter) TcpChecker() tcpchecker.Reporter {
}

type tcpChecker struct {
checks *prometheus.CounterVec
open *prometheus.GaugeVec
connectedTotal *prometheus.CounterVec
disconectedTotal *prometheus.CounterVec
Expand Down Expand Up @@ -113,6 +114,12 @@ func NewPrometheusReporter(reg prometheus.Registerer, logger Logger) (*Prometheu
Help: "Version info of strong_duckling",
}, []string{"version"}),
tcpChecker: &tcpChecker{
checks: prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: namespace,
Subsystem: subSystemTcpChecker,
Name: "checked_total",
Help: "Total number of times the connection has been checked",
}, []string{"name", "address", "port"}),
Crevil marked this conversation as resolved.
Show resolved Hide resolved
open: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: subSystemTcpChecker,
Expand Down Expand Up @@ -282,14 +289,17 @@ func (p *PrometheusReporter) Info(strongDucklingVersion string) {
}

func (r *tcpChecker) ReportPortCheck(report tcpchecker.Report) {
labelValues := []string{report.Name, report.Address, fmt.Sprintf("%d", report.Port)}
Crevil marked this conversation as resolved.
Show resolved Hide resolved
r.checks.WithLabelValues(labelValues...).Inc()
if report.Open {
r.open.WithLabelValues(report.Name, report.Address, fmt.Sprintf("%d", report.Port)).Set(1)
r.open.WithLabelValues(labelValues...).Set(1)
if r.previousOpenState == nil || *r.previousOpenState != report.Open {
r.connectedTotal.WithLabelValues(report.Name, report.Address, fmt.Sprintf("%d", report.Port)).Add(1)
r.connectedTotal.WithLabelValues(labelValues...).Add(1)
}
} else {
r.open.WithLabelValues(labelValues...).Set(0)
if r.previousOpenState == nil || *r.previousOpenState != report.Open {
r.disconectedTotal.WithLabelValues(report.Name, report.Address, fmt.Sprintf("%d", report.Port)).Add(0)
r.disconectedTotal.WithLabelValues(labelValues...).Add(0)
}
}
r.previousOpenState = &report.Open
Expand Down