Skip to content
This repository was archived by the owner on Jun 17, 2023. It is now read-only.

Commit 07e1778

Browse files
committed
Initial version
0 parents  commit 07e1778

File tree

24 files changed

+2167
-0
lines changed

24 files changed

+2167
-0
lines changed

.github/workflows/goreleaser.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
on:
2+
push:
3+
tags:
4+
- '*'
5+
name: goreleaser
6+
jobs:
7+
GoReleaser:
8+
runs-on: ubuntu-latest
9+
steps:
10+
- name: Checkout code
11+
uses: actions/checkout@v2
12+
with:
13+
fetch-depth: 0
14+
- name: Install Go
15+
uses: actions/setup-go@v2
16+
with:
17+
go-version: 1.16
18+
- name: Set build info
19+
run: |
20+
echo "USER=${whoami}" >> $GITHUB_ENV
21+
echo "HOST=${hostname}" >> $GITHUB_ENV
22+
- name: Run GoReleaser
23+
uses: goreleaser/goreleaser-action@v2
24+
with:
25+
distribution: goreleaser
26+
version: latest
27+
args: release --rm-dist
28+
env:
29+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
30+
USER: ${{ env.USER }}
31+
HOST: ${{ env.HOST }}

.github/workflows/tests.yml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
on:
2+
push:
3+
pull_request:
4+
schedule:
5+
- cron: '0 12 1 * *'
6+
name: tests
7+
jobs:
8+
Test:
9+
strategy:
10+
fail-fast: false
11+
matrix:
12+
go-version:
13+
- 1.14.x
14+
- 1.15.x
15+
- 1.16.x
16+
- 1.x
17+
os:
18+
- ubuntu-latest
19+
- windows-latest
20+
- macos-latest
21+
runs-on: ${{ matrix.os }}
22+
steps:
23+
- name: Set git to turn off auto-converting line endings
24+
run: git config --global core.autocrlf false
25+
- name: Checkout code
26+
uses: actions/checkout@v2
27+
- name: Start containers
28+
run: docker-compose -f testdata/docker/docker-compose.yml up -d
29+
if: runner.os == 'Linux'
30+
- name: Install Go
31+
uses: actions/setup-go@v2
32+
with:
33+
go-version: ${{ matrix.go-version }}
34+
- name: Test (on Ubuntu)
35+
run: go test -coverprofile=coverage.txt -covermode=atomic ./...
36+
if: runner.os == 'Linux'
37+
- name: Test (on macOS)
38+
run: go test -short -coverprofile=coverage.txt -covermode=atomic ./...
39+
if: runner.os == 'macOS'
40+
- name: Test (on Windows)
41+
run: go test -short -coverprofile=coverage.txt -covermode=atomic ./...
42+
shell: cmd
43+
if: runner.os == 'Windows'
44+
- name: Upload coverage to Codecov
45+
uses: codecov/codecov-action@v1

.goreleaser.yml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
builds:
2+
- id: ipsec_exporter
3+
main: ./cmd/ipsec_exporter
4+
ldflags: |
5+
-s
6+
-X github.com/prometheus/common/version.Version={{.Version}}
7+
-X github.com/prometheus/common/version.Revision={{.FullCommit}}
8+
-X github.com/prometheus/common/version.Branch={{.Branch}}
9+
-X github.com/prometheus/common/version.BuildUser={{.Env.USER}}@{{.Env.HOST}}
10+
-X github.com/prometheus/common/version.BuildDate={{time "20060102-15:04:05"}}
11+
tags:
12+
- netgo
13+
env:
14+
- CGO_ENABLED=0
15+
targets:
16+
- linux_amd64
17+
- linux_386
18+
- darwin_amd64
19+
- windows_amd64
20+
- windows_386
21+
- freebsd_amd64
22+
- freebsd_386
23+
- openbsd_amd64
24+
- openbsd_386
25+
- netbsd_amd64
26+
- netbsd_386
27+
- dragonfly_amd64
28+
- linux_arm
29+
- linux_arm64
30+
- freebsd_arm
31+
- openbsd_arm
32+
- netbsd_arm
33+
- aix_ppc64
34+
- linux_ppc64
35+
- linux_ppc64le
36+
- linux_mips64
37+
- linux_mips64le
38+
- linux_s390x
39+
40+
archives:
41+
- id: ipsec_exporter
42+
builds:
43+
- ipsec_exporter
44+
format_overrides:
45+
- goos: windows
46+
format: zip

LICENSE

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
BSD 3-Clause License
2+
3+
Copyright (c) 2021, Sergey Makinen
4+
All rights reserved.
5+
6+
Redistribution and use in source and binary forms, with or without
7+
modification, are permitted provided that the following conditions are met:
8+
9+
1. Redistributions of source code must retain the above copyright notice, this
10+
list of conditions and the following disclaimer.
11+
12+
2. Redistributions in binary form must reproduce the above copyright notice,
13+
this list of conditions and the following disclaimer in the documentation
14+
and/or other materials provided with the distribution.
15+
16+
3. Neither the name of the copyright holder nor the names of its
17+
contributors may be used to endorse or promote products derived from
18+
this software without specific prior written permission.
19+
20+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
build:
2+
go build -o ipsec_exporter ./cmd/ipsec_exporter

README.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# IPsec Exporter
2+
3+
[![tests](https://github.com/sergeymakinen/ipsec_exporter/workflows/tests/badge.svg)](https://github.com/sergeymakinen/ipsec_exporter/actions?query=workflow%3Atests)
4+
[![Go Reference](https://pkg.go.dev/badge/github.com/sergeymakinen/ipsec_exporter.svg)](https://pkg.go.dev/github.com/sergeymakinen/ipsec_exporter)
5+
[![Go Report Card](https://goreportcard.com/badge/github.com/sergeymakinen/ipsec_exporter)](https://goreportcard.com/report/github.com/sergeymakinen/ipsec_exporter)
6+
[![codecov](https://codecov.io/gh/sergeymakinen/ipsec_exporter/branch/main/graph/badge.svg)](https://codecov.io/gh/sergeymakinen/ipsec_exporter)
7+
8+
Export strongswan/libreswan IPsec stats to Prometheus.
9+
10+
To run it:
11+
12+
```bash
13+
make
14+
./ipsec_exporter [flags]
15+
```
16+
17+
## Exported Metrics
18+
19+
| Metric | Meaning | Labels
20+
| --- | --- | ---
21+
| ipsec_up | Was the last scrape successful. |
22+
| ipsec_uptime_seconds | Number of seconds since the daemon started. |
23+
| ipsec_workers_total | Number of worker threads. |
24+
| ipsec_idle_workers | Number of idle worker threads. |
25+
| ipsec_active_workers | Number of threads processing jobs. |
26+
| ipsec_queues | Number of queued jobs. | priority
27+
| ipsec_ike_sas | Number of currently registered IKE SAs. |
28+
| ipsec_half_open_ike_sas | Number of IKE SAs in half-open state. |
29+
| ipsec_pool_ips_total | Number of addresses in the pool. | name, address
30+
| ipsec_online_pool_ips | Number of leases online. | name, address
31+
| ipsec_offline_pool_ips | Number of leases offline. | name, address
32+
| ipsec_ike_sa_state | IKE SA state. Created: 0, connecting: 1, established: 2, passive: 3, rekeying: 4, rekeyed: 5, deleting: 6, destroying: 7. | name, uid, version, local_host, local_id, remote_host, remote_id, remote_identity, vips
33+
| ipsec_ike_sa_established_seconds | Number of seconds since the IKE SA has been established. | name, uid, version, local_host, local_id, remote_host, remote_id, remote_identity, vips
34+
| ipsec_child_sa_state | Child SA state. Created: 0, routed: 1, installing: 2, installed: 3, updating: 4, rekeying: 5, rekeyed: 6, retrying: 7, deleting: 8, deleted: 9, destroying: 10. | ike_sa_name, ike_sa_uid, name, uid, mode, protocol, local_ts, remote_ts
35+
| ipsec_child_sa_bytes_in | Number of input bytes processed. | ike_sa_name, ike_sa_uid, name, uid, mode, protocol, local_ts, remote_ts
36+
| ipsec_child_sa_packets_in | Number of input packets processed. | ike_sa_name, ike_sa_uid, name, uid, mode, protocol, local_ts, remote_ts
37+
| ipsec_child_sa_bytes_out | Number of output bytes processed. | ike_sa_name, ike_sa_uid, name, uid, mode, protocol, local_ts, remote_ts
38+
| ipsec_child_sa_packets_out | Number of output packets processed. | ike_sa_name, ike_sa_uid, name, uid, mode, protocol, local_ts, remote_ts
39+
| ipsec_child_sa_installed_seconds | Number of seconds since the child SA has been installed. | ike_sa_name, ike_sa_uid, name, uid, mode, protocol, local_ts, remote_ts
40+
41+
### Flags
42+
43+
```bash
44+
./ipsec_exporter --help
45+
```
46+
47+
* __`vici.address`:__ VICI socket address. Example: `unix:///var/run/charon.vici` or `tcp://127.0.0.1:4502`.
48+
* __`vici.timeout`:__ VICI socket connect timeout.
49+
* __`collector`:__ Collector type to scrape metrics with. `vici` or `ipsec`.
50+
* __`ipsec.command`:__ Command to scrape IPsec metrics when the collector is configured to an `ipsec` binary.
51+
* __`web.listen-address`:__ Address to listen on for web interface and telemetry.
52+
* __`web.telemetry-path`:__ Path under which to expose metrics.
53+
* __`log.level`:__ Logging level. `info` by default.
54+
* __`log.format`:__ Set the log target and format. Example: `logger:syslog?appname=bob&local=7`
55+
or `logger:stdout?json=true`.
56+
57+
### TLS and basic authentication
58+
59+
The ipsec_exporter supports TLS and basic authentication.
60+
To use TLS and/or basic authentication, you need to pass a configuration file
61+
using the `--web.config.file` parameter. The format of the file is described
62+
[in the exporter-toolkit repository](https://github.com/prometheus/exporter-toolkit/blob/master/docs/web-configuration.md).

cmd/ipsec_exporter/main.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package main
2+
3+
import (
4+
"net/http"
5+
"os"
6+
"strings"
7+
8+
"github.com/go-kit/kit/log/level"
9+
"github.com/google/shlex"
10+
"github.com/prometheus/client_golang/prometheus"
11+
"github.com/prometheus/client_golang/prometheus/promhttp"
12+
"github.com/prometheus/common/promlog"
13+
"github.com/prometheus/common/promlog/flag"
14+
"github.com/prometheus/common/version"
15+
"github.com/prometheus/exporter-toolkit/web"
16+
webflag "github.com/prometheus/exporter-toolkit/web/kingpinflag"
17+
"github.com/sergeymakinen/ipsec_exporter/exporter"
18+
"gopkg.in/alecthomas/kingpin.v2"
19+
)
20+
21+
type cmdValue []string
22+
23+
func (c *cmdValue) Set(s string) (err error) {
24+
*c, err = shlex.Split(s)
25+
return
26+
}
27+
28+
func (c cmdValue) String() string { return strings.Join(c, " ") }
29+
30+
func newCmd(s kingpin.Settings) (target *[]string) {
31+
target = new([]string)
32+
s.SetValue((*cmdValue)(target))
33+
return
34+
}
35+
36+
func main() {
37+
var (
38+
address = kingpin.Flag("vici.address", "VICI socket address.").PlaceHolder(`"` + viciDefaultAddress + `"`).Default(viciDefaultAddress).URL()
39+
timeout = kingpin.Flag("vici.timeout", "VICI socket connect timeout.").Default("1s").Duration()
40+
collector = kingpin.Flag("collector", "Collector type to scrape metrics with. One of: [vici, ipsec]").Default("vici").Enum("vici", "ipsec")
41+
ipsecCmd = newCmd(kingpin.Flag("ipsec.command", "Command to scrape IPsec metrics from.").PlaceHolder(`"ipsec statusall"`).Default("ipsec statusall"))
42+
webConfig = webflag.AddFlags(kingpin.CommandLine)
43+
listenAddress = kingpin.Flag("web.listen-address", "Address to listen on for web interface and telemetry.").Default(":9844").String()
44+
metricsPath = kingpin.Flag("web.telemetry-path", "Path under which to expose metrics.").Default("/metrics").String()
45+
)
46+
promlogConfig := &promlog.Config{}
47+
flag.AddFlags(kingpin.CommandLine, promlogConfig)
48+
kingpin.HelpFlag.Short('h')
49+
kingpin.Version(version.Print("ipsec_exporter"))
50+
kingpin.Parse()
51+
logger := promlog.New(promlogConfig)
52+
53+
level.Info(logger).Log("msg", "Starting ipsec_exporter", "version", version.Info())
54+
level.Info(logger).Log("msg", "Build context", "context", version.BuildContext())
55+
56+
prometheus.MustRegister(version.NewCollector("ipsec_exporter"))
57+
collectorType := exporter.CollectorVICI
58+
if *collector == "ipsec" {
59+
collectorType = exporter.CollectorIpsec
60+
}
61+
exporter, err := exporter.New(collectorType, *address, *timeout, *ipsecCmd, logger)
62+
if err != nil {
63+
level.Error(logger).Log("msg", "Error creating the exporter", "err", err)
64+
os.Exit(1)
65+
}
66+
prometheus.MustRegister(exporter)
67+
68+
http.Handle(*metricsPath, promhttp.Handler())
69+
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
70+
w.Write([]byte(`<html>
71+
<head><title>IPsec Exporter</title></head>
72+
<body>
73+
<h1>IPsec Exporter</h1>
74+
<p><a href='` + *metricsPath + `'>Metrics</a></p>
75+
</body>
76+
</html>`))
77+
})
78+
79+
level.Info(logger).Log("msg", "Listening on address", "address", *listenAddress)
80+
srv := &http.Server{Addr: *listenAddress}
81+
if err := web.ListenAndServe(srv, *webConfig, logger); err != nil {
82+
level.Error(logger).Log("msg", "Error running HTTP server", "err", err)
83+
os.Exit(1)
84+
}
85+
}

cmd/ipsec_exporter/main_other.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// +build !windows
2+
3+
package main
4+
5+
const viciDefaultAddress = "unix:///var/run/charon.vici"

cmd/ipsec_exporter/main_windows.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package main
2+
3+
const viciDefaultAddress = "tcp://127.0.0.1:4502"

0 commit comments

Comments
 (0)