From 85f3fffe50e24d279e916f83b4a074c0d699f340 Mon Sep 17 00:00:00 2001 From: Stefan Majer Date: Wed, 14 Feb 2024 09:49:05 +0100 Subject: [PATCH] Update to go-1.22, refresh --- .github/workflows/release.yaml | 8 +- .github/workflows/test.yaml | 6 +- Dockerfile | 4 +- README.md | 6 +- go.mod | 18 ++-- go.sum | 152 ++++----------------------------- main.go | 124 +++++++++++++++------------ main_test.go | 94 +++++++++++++------- 8 files changed, 167 insertions(+), 245 deletions(-) diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index 91aed69..1879a49 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -11,7 +11,7 @@ jobs: goreleaser: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 with: fetch-depth: 0 @@ -19,11 +19,11 @@ jobs: run: git fetch --force --tags - name: Set up Go - uses: actions/setup-go@v2 + uses: actions/setup-go@v5 with: - go-version: 1.18 + go-version: 1.22 - name: Run GoReleaser - uses: goreleaser/goreleaser-action@v2 + uses: goreleaser/goreleaser-action@v5 with: distribution: goreleaser version: latest diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 878deb6..d4e90e0 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -11,12 +11,12 @@ jobs: test: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 - name: Set up Go - uses: actions/setup-go@v2 + uses: actions/setup-go@v5 with: - go-version: 1.18 + go-version: 1.22 - name: Check formatting run: if [ "$(gofmt -s -l . | wc -l)" -gt 0 ]; then exit 1; fi diff --git a/Dockerfile b/Dockerfile index 67f2aba..c98ae45 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM golang:1.16-alpine AS builder +FROM golang:1.22-alpine AS builder ENV CGO_ENABLED=0 @@ -14,7 +14,7 @@ RUN go test RUN go build -ldflags "-X main.version=${VERSION}" -FROM gcr.io/distroless/static-debian11 +FROM gcr.io/distroless/static-debian12 COPY --from=builder /app/suricata_exporter . EXPOSE 9917 ENTRYPOINT ["/suricata_exporter"] diff --git a/README.md b/README.md index 39cb3ff..b811638 100644 --- a/README.md +++ b/README.md @@ -4,10 +4,9 @@ This is a Prometheus Exporter for Suricata using `dump-counters` via the [unix socket](https://suricata.readthedocs.io/en/suricata-6.0.0/unix-socket.html#interacting-via-unix-socket) to query metrics. - ## Usage -``` +```bash $ ./suricata_exporter -h Usage of ./suricata_exporter: -suricata.socket-path string @@ -21,7 +20,7 @@ Usage of ./suricata_exporter: To verify the exporter is working with your Suricata setup, use the following command to view a subset of the capture metrics. -``` +```bash $ curl -s localhost:9917/metrics | grep kernel_packets_total # HELP suricata_capture_kernel_packets_total # TYPE suricata_capture_kernel_packets_total counter @@ -35,7 +34,6 @@ You can now configure a [Prometheus server](https://prometheus.io/docs/prometheu to scrape the Suricata exporter in regular intervals for later visualization and analysis. - ## Metrics Currently produces per-thread metrics for `decoder`, `flow`, `flow_bypassed`, diff --git a/go.mod b/go.mod index 1e0019b..c319ae3 100644 --- a/go.mod +++ b/go.mod @@ -1,19 +1,17 @@ module github.com/corelight/suricata_exporter -go 1.17 +go 1.22 require ( - github.com/prometheus/client_golang v1.11.0 - github.com/prometheus/client_model v0.2.0 + github.com/prometheus/client_golang v1.18.0 + github.com/prometheus/client_model v0.5.0 ) require ( github.com/beorn7/perks v1.0.1 // indirect - github.com/cespare/xxhash/v2 v2.1.1 // indirect - github.com/golang/protobuf v1.4.3 // indirect - github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect - github.com/prometheus/common v0.26.0 // indirect - github.com/prometheus/procfs v0.6.0 // indirect - golang.org/x/sys v0.0.0-20210603081109-ebe580a85c40 // indirect - google.golang.org/protobuf v1.26.0-rc.1 // indirect + github.com/cespare/xxhash/v2 v2.2.0 // indirect + github.com/prometheus/common v0.46.0 // indirect + github.com/prometheus/procfs v0.12.0 // indirect + golang.org/x/sys v0.17.0 // indirect + google.golang.org/protobuf v1.32.0 // indirect ) diff --git a/go.sum b/go.sum index 6a42e5c..9b35f94 100644 --- a/go.sum +++ b/go.sum @@ -1,138 +1,20 @@ -cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= -github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= -github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= -github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= -github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= -github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk541a8SKzHPHnH3zbiI+7dagKZ0cgpgrD7Fyho= -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= github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= -github.com/cespare/xxhash/v2 v2.1.1 h1:6MnRN8NT7+YBpUIWxHtefFZOKTAPgGjpQSxqLNn0+qY= -github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= -github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44= +github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= -github.com/go-kit/kit v0.9.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= -github.com/go-kit/log v0.1.0/go.mod h1:zbhenjAZHb184qTLMA9ZjW7ThYL0H2mk7Q6pNt4vbaY= -github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE= -github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk= -github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A= -github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= -github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= -github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= -github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= -github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= -github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8= -github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA= -github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs= -github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w= -github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0= -github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= -github.com/golang/protobuf v1.4.3 h1:JjCZWpVbqXDqFVmTfYWEVTMIYrL/NPdPSCHPJ0T/raM= -github.com/golang/protobuf v1.4.3/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= -github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= -github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= -github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= -github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= -github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4= -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/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w= -github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM= -github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= -github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= -github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc= -github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= -github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= -github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= -github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU= -github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= -github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= -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/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= -github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= -github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= -github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw= -github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo= -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_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.2.0 h1:uq5h0d+GuxiXLJLNABMgp2qUWDPiLvgCzz2dUR+/W/M= -github.com/prometheus/client_model v0.2.0/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= -github.com/prometheus/common v0.4.1/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4= -github.com/prometheus/common v0.10.0/go.mod h1:Tlit/dnDKsSWFlCLTWaA1cyBgKHSMdTB80sz/V91rCo= -github.com/prometheus/common v0.26.0 h1:iMAkS2TDoNWnKM+Kopnx/8tnEStIfpYA0ur0xQzzhMQ= -github.com/prometheus/common v0.26.0/go.mod h1:M7rCNAaPfAosfx8veZJCuw84e35h3Cfd9VFqTh1DIvc= -github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk= -github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA= -github.com/prometheus/procfs v0.1.3/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4OA4YeYWdaU= -github.com/prometheus/procfs v0.6.0 h1:mxy4L2jP6qMonqmq+aTtOx1ifVWUgG/TAmntgbh3xv4= -github.com/prometheus/procfs v0.6.0/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA= -github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= -github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= -github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88= -github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= -github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= -github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= -golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= -golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= -golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= -golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= -golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= -golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= -golang.org/x/net v0.0.0-20190613194153-d28f0bde5980/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/net v0.0.0-20200625001655-4c5254603344/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA= -golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= -golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200106162015-b016eb3dc98e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200615200032-f1bc736245b1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210603081109-ebe580a85c40 h1:JWgyZ1qgdTaF3N3oxC+MdTV7qvEEgHo3otj+HB5CM7Q= -golang.org/x/sys v0.0.0-20210603081109-ebe580a85c40/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= -golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= -golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= -golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= -google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= -google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= -google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM= -google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE= -google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo= -google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= -google.golang.org/protobuf v1.26.0-rc.1 h1:7QnIQpGRHE5RnLKnESfDoxm2dTapTZua5a0kS0A+VXQ= -google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= -gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -gopkg.in/yaml.v2 v2.2.5/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= +github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/prometheus/client_golang v1.18.0 h1:HzFfmkOzH5Q8L8G+kSJKUx5dtG87sewO+FoDDqP5Tbk= +github.com/prometheus/client_golang v1.18.0/go.mod h1:T+GXkCk5wSJyOqMIzVgvvjFDlkOQntgjkJWKrN5txjA= +github.com/prometheus/client_model v0.5.0 h1:VQw1hfvPvk3Uv6Qf29VrPF32JB6rtbgI6cYPYQjL0Qw= +github.com/prometheus/client_model v0.5.0/go.mod h1:dTiFglRmd66nLR9Pv9f0mZi7B7fk5Pm3gvsjB5tr+kI= +github.com/prometheus/common v0.46.0 h1:doXzt5ybi1HBKpsZOL0sSkaNHJJqkyfEWZGGqqScV0Y= +github.com/prometheus/common v0.46.0/go.mod h1:Tp0qkxpb9Jsg54QMe+EAmqXkSV7Evdy1BTn+g2pa/hQ= +github.com/prometheus/procfs v0.12.0 h1:jluTpSng7V9hY0O2R9DzzJHYb2xULk9VTR1V1R/k6Bo= +github.com/prometheus/procfs v0.12.0/go.mod h1:pcuDEFsWDnvcgNzo4EEweacyhjeA9Zk3cnaOZAZEfOo= +golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y= +golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +google.golang.org/protobuf v1.32.0 h1:pPC6BG5ex8PDFnkbrGU3EixyhKcQ2aDuBS36lqK/C7I= +google.golang.org/protobuf v1.32.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= diff --git a/main.go b/main.go index 2d77bbd..60c8be2 100644 --- a/main.go +++ b/main.go @@ -376,72 +376,85 @@ func (c *SuricataClient) EnsureConnection() error { // Do the version handshake. Returns nil or the error. func (c *SuricataClient) Handshake() error { // Send the version as hand-shake. - cmdData, _ := json.Marshal(map[string]string{ + cmdData, err := json.Marshal(map[string]string{ "version": "0.2", }) + if err != nil { + c.Close() + return err + } + fmt.Fprintf(c.conn, "%s\n", string(cmdData)) reader := bufio.NewReader(c.conn) line, err := reader.ReadBytes('\n') if err != nil { c.Close() - return fmt.Errorf("Failed read response from Suricata: %v", err) + return fmt.Errorf("failed read response from Suricata: %w", err) } - var parsed map[string]interface{} + var parsed map[string]any err = json.Unmarshal(line, &parsed) if err != nil { c.Close() - return fmt.Errorf("Failed to parse version response from Suricata: %v", err) + return fmt.Errorf("failed to parse version response from Suricata: %w", err) } if parsed["return"] != "OK" { c.Close() - return fmt.Errorf("No \"OK\" response from Suricata: %v", parsed) + return fmt.Errorf("no \"OK\" response from Suricata: %v", parsed) } return nil } func (c *SuricataClient) Uptime() (uint64, error) { - cmdData, _ := json.Marshal(map[string]string{ + cmdData, err := json.Marshal(map[string]string{ "command": "uptime", }) + if err != nil { + c.Close() + return 0, err + } fmt.Fprintf(c.conn, "%s\n", string(cmdData)) reader := bufio.NewReader(c.conn) line, err := reader.ReadBytes('\n') if err != nil { c.Close() - return 0, fmt.Errorf("Failed read response from Suricata: %v", err) + return 0, fmt.Errorf("failed read response from Suricata: %w", err) } - var parsed map[string]interface{} + var parsed map[string]any err = json.Unmarshal(line, &parsed) if err != nil { c.Close() - return 0, fmt.Errorf("Failed to parse version response from Suricata: %v", err) + return 0, fmt.Errorf("failed to parse version response from Suricata: %w", err) } if parsed["return"] != "OK" { c.Close() - return 0, fmt.Errorf("No \"OK\" response from Suricata: %v", parsed) + return 0, fmt.Errorf("no \"OK\" response from Suricata: %v", parsed) } uptime, ok := parsed["message"].(float64) if !ok { - return 0, fmt.Errorf("Could get uptime from response: %v", parsed) + return 0, fmt.Errorf("could get uptime from response: %v", parsed) } return uint64(uptime), nil } -// Send dump-counters command and return JSON as parsed map[string]interface{} -func (c *SuricataClient) DumpCounters() (map[string]interface{}, error) { - cmdData, _ := json.Marshal(map[string]string{ +// Send dump-counters command and return JSON as parsed map[string]any +func (c *SuricataClient) DumpCounters() (map[string]any, error) { + cmdData, err := json.Marshal(map[string]string{ "command": "dump-counters", }) + if err != nil { + c.Close() + return nil, err + } fmt.Fprintf(c.conn, "%s\n", string(cmdData)) // Read until '\n' shows up or there was an error. A lot of data @@ -461,7 +474,7 @@ func (c *SuricataClient) DumpCounters() (map[string]interface{}, error) { } } - var parsed map[string]interface{} + var parsed map[string]any if err := json.Unmarshal(response, &parsed); err != nil { c.Close() return nil, err @@ -475,7 +488,7 @@ func (c *SuricataClient) DumpCounters() (map[string]interface{}, error) { } // Produce a new for the metricInfo -func newConstMetric(m metricInfo, data map[string]interface{}, labelValues ...string) prometheus.Metric { +func newConstMetric(m metricInfo, data map[string]any, labelValues ...string) prometheus.Metric { field_value, ok := data[m.field] if !ok { @@ -496,22 +509,22 @@ func newConstMetric(m metricInfo, data map[string]interface{}, labelValues ...st } // Extract Napatech related metrics from message -func handleNapatechMetrics(ch chan<- prometheus.Metric, message map[string]interface{}) { - if napaTotal, ok := message["napa_total"].(map[string]interface{}); ok { +func handleNapatechMetrics(ch chan<- prometheus.Metric, message map[string]any) { + if napaTotal, ok := message["napa_total"].(map[string]any); ok { for _, m := range napaTotalMetrics { if cm := newConstMetric(m, napaTotal); cm != nil { ch <- cm } } } - if napaTotal, ok := message["napa_dispatch_host"].(map[string]interface{}); ok { + if napaTotal, ok := message["napa_dispatch_host"].(map[string]any); ok { for _, m := range napaDispatchHost { if cm := newConstMetric(m, napaTotal); cm != nil { ch <- cm } } } - if napaTotal, ok := message["napa_dispatch_drop"].(map[string]interface{}); ok { + if napaTotal, ok := message["napa_dispatch_drop"].(map[string]any); ok { for _, m := range napaDispatchDrop { if cm := newConstMetric(m, napaTotal); cm != nil { ch <- cm @@ -520,15 +533,15 @@ func handleNapatechMetrics(ch chan<- prometheus.Metric, message map[string]inter } } -func handleWorkerThread(ch chan<- prometheus.Metric, threadName string, thread map[string]interface{}) { - if capture, ok := thread["capture"].(map[string]interface{}); ok { +func handleWorkerThread(ch chan<- prometheus.Metric, threadName string, thread map[string]any) { + if capture, ok := thread["capture"].(map[string]any); ok { for _, m := range perThreadCaptureMetrics { if cm := newConstMetric(m, capture, threadName); cm != nil { ch <- cm } } - if afpacket, ok := capture["afpacket"].(map[string]interface{}); ok { + if afpacket, ok := capture["afpacket"].(map[string]any); ok { for _, m := range perThreadCaptureAFPacketMetrics { if cm := newConstMetric(m, afpacket, threadName); cm != nil { ch <- cm @@ -551,30 +564,30 @@ func handleWorkerThread(ch chan<- prometheus.Metric, threadName string, thread m } } - tcp := thread["tcp"].(map[string]interface{}) + tcp := thread["tcp"].(map[string]any) for _, m := range perThreadTcpMetrics { if cm := newConstMetric(m, tcp, threadName); cm != nil { ch <- cm } } - flow := thread["flow"].(map[string]interface{}) + flow := thread["flow"].(map[string]any) for _, m := range perThreadFlowMetrics { if cm := newConstMetric(m, flow, threadName); cm != nil { ch <- cm } } - wrk := flow["wrk"].(map[string]interface{}) + wrk := flow["wrk"].(map[string]any) for _, m := range perThreadFlowWrkMetrics { if cm := newConstMetric(m, wrk, threadName); cm != nil { ch <- cm } } - defrag := thread["defrag"].(map[string]interface{}) - defragIpv4 := defrag["ipv4"].(map[string]interface{}) - defragIpv6 := defrag["ipv6"].(map[string]interface{}) + defrag := thread["defrag"].(map[string]any) + defragIpv4 := defrag["ipv4"].(map[string]any) + defragIpv6 := defrag["ipv6"].(map[string]any) for _, m := range perThreadDefragIpv4Metrics { if cm := newConstMetric(m, defragIpv4, threadName); cm != nil { ch <- cm @@ -591,7 +604,7 @@ func handleWorkerThread(ch chan<- prometheus.Metric, threadName string, thread m } } - detect := thread["detect"].(map[string]interface{}) + detect := thread["detect"].(map[string]any) for _, m := range perThreadDetectMetrics { if cm := newConstMetric(m, detect, threadName); cm != nil { ch <- cm @@ -600,14 +613,14 @@ func handleWorkerThread(ch chan<- prometheus.Metric, threadName string, thread m // Convert all decoder entries that look like numbers // as perThreadDecoder metric with a "kind" label. - decoder := thread["decoder"].(map[string]interface{}) + decoder := thread["decoder"].(map[string]any) for _, m := range perThreadDecoderMetrics { if cm := newConstMetric(m, decoder, threadName); cm != nil { ch <- cm } } - bypassed := thread["flow_bypassed"].(map[string]interface{}) + bypassed := thread["flow_bypassed"].(map[string]any) for _, m := range perThreadFlowBypassedMetrics { if cm := newConstMetric(m, bypassed, threadName); cm != nil { ch <- cm @@ -619,8 +632,8 @@ func handleWorkerThread(ch chan<- prometheus.Metric, threadName string, thread m // // suricata_app_layer_flows_total{app="ntp",thread="W#08-wlp0s20f3"} 87 // suricata_app_layer_flows_total{app="tls",thread="W#04-wlp0s20f3"} 204 - appLayer := thread["app_layer"].(map[string]interface{}) - appLayerFlow := appLayer["flow"].(map[string]interface{}) + appLayer := thread["app_layer"].(map[string]any) + appLayerFlow := appLayer["flow"].(map[string]any) for k, v := range appLayerFlow { value, ok := v.(float64) if !ok { @@ -632,16 +645,16 @@ func handleWorkerThread(ch chan<- prometheus.Metric, threadName string, thread m } } -func handleFlowManagerThread(ch chan<- prometheus.Metric, threadName string, thread map[string]interface{}) { - flow := thread["flow"].(map[string]interface{}) - mgr := flow["mgr"].(map[string]interface{}) +func handleFlowManagerThread(ch chan<- prometheus.Metric, threadName string, thread map[string]any) { + flow := thread["flow"].(map[string]any) + mgr := flow["mgr"].(map[string]any) for _, m := range perThreadFlowMgrMetrics { if cm := newConstMetric(m, mgr, threadName); cm != nil { ch <- cm } } - flowBypassed := thread["flow_bypassed"].(map[string]interface{}) + flowBypassed := thread["flow_bypassed"].(map[string]any) for _, m := range perThreadFlowMgrBypassedMetrics { if cm := newConstMetric(m, flowBypassed, threadName); cm != nil { ch <- cm @@ -650,9 +663,9 @@ func handleFlowManagerThread(ch chan<- prometheus.Metric, threadName string, thr } // Handle flow recycler metrics -func handleFlowRecyclerThread(ch chan<- prometheus.Metric, threadName string, thread map[string]interface{}) { - flow := thread["flow"].(map[string]interface{}) - recycler := flow["recycler"].(map[string]interface{}) +func handleFlowRecyclerThread(ch chan<- prometheus.Metric, threadName string, thread map[string]any) { + flow := thread["flow"].(map[string]any) + recycler := flow["recycler"].(map[string]any) for _, m := range perThreadFlowRecyclerMetrics { if cm := newConstMetric(m, recycler, threadName); cm != nil { ch <- cm @@ -663,8 +676,8 @@ func handleFlowRecyclerThread(ch chan<- prometheus.Metric, threadName string, th } // Handle global metrics. -func handleGlobal(ch chan<- prometheus.Metric, message map[string]interface{}) { - if globalTcp, ok := message["tcp"].(map[string]interface{}); ok { +func handleGlobal(ch chan<- prometheus.Metric, message map[string]any) { + if globalTcp, ok := message["tcp"].(map[string]any); ok { for _, m := range globalTcpMetrics { if cm := newConstMetric(m, globalTcp); cm != nil { ch <- cm @@ -674,7 +687,7 @@ func handleGlobal(ch chan<- prometheus.Metric, message map[string]interface{}) { log.Printf("WARN: No top-level tcp entry in message") } - if globalFlow, ok := message["flow"].(map[string]interface{}); ok { + if globalFlow, ok := message["flow"].(map[string]any); ok { for _, m := range globalFlowMetrics { if cm := newConstMetric(m, globalFlow); cm != nil { ch <- cm @@ -684,24 +697,24 @@ func handleGlobal(ch chan<- prometheus.Metric, message map[string]interface{}) { log.Printf("WARN: No top-level flow entry message") } - if globalHttp, ok := message["http"].(map[string]interface{}); ok { + if globalHttp, ok := message["http"].(map[string]any); ok { ch <- prometheus.MustNewConstMetric(httpMemuseMetric.desc, httpMemuseMetric.t, globalHttp["memuse"].(float64)) } else { log.Printf("WARN: No top-level http entry message") } - if globalFtp, ok := message["ftp"].(map[string]interface{}); ok { + if globalFtp, ok := message["ftp"].(map[string]any); ok { ch <- prometheus.MustNewConstMetric(ftpMemuseMetric.desc, httpMemuseMetric.t, globalFtp["memuse"].(float64)) } else { log.Printf("WARN: No top-level ftp entry message") } - if globalDetect, ok := message["detect"].(map[string]interface{}); ok { - if engines, ok := globalDetect["engines"].([]interface{}); ok { + if globalDetect, ok := message["detect"].(map[string]any); ok { + if engines, ok := globalDetect["engines"].([]any); ok { for _, e := range engines { - em := e.(map[string]interface{}) + em := e.(map[string]any) engine_id := strconv.Itoa(int(em["id"].(float64))) rules_loaded := em["rules_loaded"].(float64) rules_failed := em["rules_failed"].(float64) @@ -730,16 +743,16 @@ func handleGlobal(ch chan<- prometheus.Metric, message map[string]interface{}) { } -func produceMetrics(ch chan<- prometheus.Metric, counters map[string]interface{}) { +func produceMetrics(ch chan<- prometheus.Metric, counters map[string]any) { - message := counters["message"].(map[string]interface{}) + message := counters["message"].(map[string]any) // Uptime metric ch <- newConstMetric(metricUptime, message) // Produce per thread metrics - for threadName, thread_ := range message["threads"].(map[string]interface{}) { - if thread, ok := thread_.(map[string]interface{}); ok { + for threadName, thread_ := range message["threads"].(map[string]any) { + if thread, ok := thread_.(map[string]any); ok { if strings.HasPrefix(threadName, "W#") { handleWorkerThread(ch, threadName, thread) } else if strings.HasPrefix(threadName, "FM") { @@ -817,13 +830,16 @@ func main() { http.Handle(*path, promhttp.HandlerFor(r, promhttp.HandlerOpts{})) http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - w.Write([]byte(` + _, err := w.Write([]byte(` Suricata Exporter

Suricata Exporter

Metrics

`)) + if err != nil { + http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) + } }) if err := http.ListenAndServe(*addr, nil); err != nil { diff --git a/main_test.go b/main_test.go index 4c56fa3..4c96634 100644 --- a/main_test.go +++ b/main_test.go @@ -3,8 +3,8 @@ package main import ( "encoding/json" "fmt" - "io/ioutil" "log" + "os" "regexp" "sort" "strings" @@ -16,13 +16,13 @@ import ( var ( descStringRe = regexp.MustCompile("fqName: \"([^\"]+)\"") - sampleCounters = map[string]interface{}{ - "message": map[string]interface{}{ + sampleCounters = map[string]any{ + "message": map[string]any{ "uptime": 123.0, - "threads": map[string]interface{}{}, - "detect": map[string]interface{}{ - "engines": []interface{}{ - map[string]interface{}{ + "threads": map[string]any{}, + "detect": map[string]any{ + "engines": []any{ + map[string]any{ "id": 0.0, "last_reload": "2021-12-08T11:28:38.980499+0100", "rules_loaded": 42.0, @@ -45,7 +45,10 @@ func aggregateMetrics(metrics []prometheus.Metric) map[string][]testMetric { result := make(map[string][]testMetric) for _, m := range metrics { dm := &dto.Metric{} - m.Write(dm) + err := m.Write(dm) + if err != nil { + return nil + } tm := testMetricFromMetric(m) // fmt.Printf("%+v\n", tm) @@ -58,8 +61,10 @@ func aggregateMetrics(metrics []prometheus.Metric) map[string][]testMetric { func testMetricFromMetric(m prometheus.Metric) testMetric { desc := m.Desc() dm := &dto.Metric{} - m.Write(dm) - + err := m.Write(dm) + if err != nil { + return testMetric{} + } var type_ string var value float64 if dm.Counter != nil { @@ -90,7 +95,7 @@ func testMetricFromMetric(m prometheus.Metric) testMetric { } // Call produceMetrics with the given data and collect all produced metrics. -func produceMetricsHelper(data map[string]interface{}) []prometheus.Metric { +func produceMetricsHelper(data map[string]any) []prometheus.Metric { ch := make(chan prometheus.Metric) finished := make(chan bool) @@ -125,17 +130,23 @@ func TestProduceMetricsRules(t *testing.T) { if strings.Contains(m.Desc().String(), "suricata_detect_engine_rules_loaded") { found_rules_loaded = true dm := &dto.Metric{} - m.Write(dm) + err := m.Write(dm) + if err != nil { + t.Error(err) + } - expected := `label: gauge: ` + expected := `label:{name:"id" value:"0"} gauge:{value:42}` if dm.String() != expected { t.Errorf("Unexpected rules_loaded metric: expected=%q have=%q", expected, dm.String()) } } else if strings.Contains(m.Desc().String(), "suricata_detect_engine_rules_failed") { dm := &dto.Metric{} - m.Write(dm) + err := m.Write(dm) + if err != nil { + t.Error(err) + } found_rules_failed = true - expected := `label: gauge: ` + expected := `label:{name:"id" value:"0"} gauge:{value:18}` if dm.String() != expected { t.Errorf("Unexpected rules_loaded metric: expected=%q have=%q", expected, dm.String()) } @@ -161,9 +172,11 @@ func TestProduceMetricsLastReload(t *testing.T) { if strings.Contains(m.Desc().String(), "suricata_detect_engine_last_reload") { found_last_reload = true dm := &dto.Metric{} - m.Write(dm) - - expected := `label: gauge: ` + err := m.Write(dm) + if err != nil { + t.Error(err) + } + expected := `label:{name:"id" value:"0"} gauge:{value:1.638959318e+09}` if dm.String() != expected { t.Errorf("Unexpected rules_loaded metric: expected=%q have=%q", expected, dm.String()) } @@ -176,13 +189,16 @@ func TestProduceMetricsLastReload(t *testing.T) { } func TestDump604AFPacket(t *testing.T) { - data, err := ioutil.ReadFile("./testdata/dump-counters-6.0.4-afpacket.json") + data, err := os.ReadFile("./testdata/dump-counters-6.0.4-afpacket.json") if err != nil { log.Panicf("Unable to open file: %s", err) } - var counters map[string]interface{} - json.Unmarshal(data, &counters) + var counters map[string]any + err = json.Unmarshal(data, &counters) + if err != nil { + t.Error(err) + } metrics := produceMetricsHelper(counters) agged := aggregateMetrics(metrics) @@ -209,13 +225,16 @@ func TestDump604AFPacket(t *testing.T) { } func TestDump604Netmap(t *testing.T) { - data, err := ioutil.ReadFile("./testdata/dump-counters-6.0.4-netmap.json") + data, err := os.ReadFile("./testdata/dump-counters-6.0.4-netmap.json") if err != nil { log.Panicf("Unable to open file: %s", err) } - var counters map[string]interface{} - json.Unmarshal(data, &counters) + var counters map[string]any + err = json.Unmarshal(data, &counters) + if err != nil { + t.Error(err) + } metrics := produceMetricsHelper(counters) // This is a bit dumb because once more metrics are added this isn't @@ -226,13 +245,16 @@ func TestDump604Netmap(t *testing.T) { } func TestDump604Napatech(t *testing.T) { - data, err := ioutil.ReadFile("./testdata/dump-counters-6.0.4-napatech.json") + data, err := os.ReadFile("./testdata/dump-counters-6.0.4-napatech.json") if err != nil { log.Panicf("Unable to open file: %s", err) } - var counters map[string]interface{} - json.Unmarshal(data, &counters) + var counters map[string]any + err = json.Unmarshal(data, &counters) + if err != nil { + t.Error(err) + } metrics := produceMetricsHelper(counters) agged := aggregateMetrics(metrics) @@ -264,13 +286,16 @@ func TestDump604Napatech(t *testing.T) { } func TestDump700AFPacket(t *testing.T) { - data, err := ioutil.ReadFile("./testdata/dump-counters-7.0.0-afpacket.json") + data, err := os.ReadFile("./testdata/dump-counters-7.0.0-afpacket.json") if err != nil { log.Panicf("Unable to open file: %s", err) } - var counters map[string]interface{} - json.Unmarshal(data, &counters) + var counters map[string]any + err = json.Unmarshal(data, &counters) + if err != nil { + t.Error(err) + } metrics := produceMetricsHelper(counters) agged := aggregateMetrics(metrics) @@ -305,13 +330,16 @@ func TestDump700AFPacket(t *testing.T) { } func TestDump701(t *testing.T) { - data, err := ioutil.ReadFile("./testdata/dump-counters-7.0.1.json") + data, err := os.ReadFile("./testdata/dump-counters-7.0.1.json") if err != nil { log.Panicf("Unable to open file: %s", err) } - var counters map[string]interface{} - json.Unmarshal(data, &counters) + var counters map[string]any + err = json.Unmarshal(data, &counters) + if err != nil { + t.Error(err) + } metrics := produceMetricsHelper(counters) agged := aggregateMetrics(metrics)