From 87954bf1b20e84551b5ba00b6ddcd6e9af72c04b Mon Sep 17 00:00:00 2001 From: Peter Haag Date: Wed, 25 Aug 2021 12:11:27 +0200 Subject: [PATCH] first commit --- README.md | 86 ++++++++++++++++++++++ dataSocket.go | 197 ++++++++++++++++++++++++++++++++++++++++++++++++++ go.mod | 5 ++ go.sum | 136 ++++++++++++++++++++++++++++++++++ main.go | 187 +++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 611 insertions(+) create mode 100644 README.md create mode 100644 dataSocket.go create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go diff --git a/README.md b/README.md new file mode 100644 index 0000000..163e8b6 --- /dev/null +++ b/README.md @@ -0,0 +1,86 @@ +# Nfdump Exporter + +This is a prototype exporter for nfdump. It exposes metrics processed by the Proetheus monitoring system. + +It's purpose is to to play and experiment with nfdump netflow data and Promtheus/Grafana to build a new graphical UI as a repacement for aging NfSen. + +This experimental exporter exposes counters for flows/packets and bytes per protocol (tcp/udp/icmp/other) and the source identifier from the nfcapd collector. (currently hardwired "live") + +## Metrics: + +``` + namespace = "nfsen" + uptime = prometheus.NewDesc( + prometheus.BuildFQName(namespace, "collector", "uptime"), + "nfsen uptime.", + []string{"version"}, nil, + ) + flowsReceived = prometheus.NewDesc( + prometheus.BuildFQName(namespace, "collector", "flows"), + "How many flows have been received (per ident and protocol (tcp/udp/icmp/other)).", + []string{"ident", "proto"}, nil, + ) + packetsReceived = prometheus.NewDesc( + prometheus.BuildFQName(namespace, "collector", "packets"), + "How many packets have been received (per ident and protocol) (tcp/udp/icmp/other).", + []string{"ident", "proto"}, nil, + ) + bytesReceived = prometheus.NewDesc( + prometheus.BuildFQName(namespace, "collector", "bytes"), + "How many bytes have been received (per ident and protocol) (tcp/udp/icmp/other).", + []string{"ident", "proto"}, nil, + ) +``` + + + +## Usage: + +``` +Usage of ./nfsen_exporter: + -UNIX socket string + Path for nfcapd collectors to connect (default "/tmp/nfsen.sock") + -listen string + Address to listen on for telemetry (default ":9141") + -metrics URI string + Path under which to expose metrics (default "/metrics") + +``` + +The nfsen_exporter listens on a UNIX socket for statistics sent by the nfcapd collector. + +Add this to prometheus.yml: + +``` + - job_name: "nfsen" + + # metrics_path defaults to '/metrics' + # scheme defaults to 'http'. + + static_configs: + - targets: ["localhost:9141"] +``` + + + +## Nfdump + +The metric export is integrated in nfdump 1.7-beta + +In order not to pollute an existing nfdump netflow installation, forward the traffic from an existing collector. Add: `-R 127.0.0.1/9999` to the argument list and setup the new collector. You may also send it to another host, which runs also Prometheus for example. + +Build nfdump 1.7-beta: + +`git clone -b unicorn https://github.com/phaag/nfdump.git nfdump.unicorn` + +Build nfdump with `sh bootstrap.sh; ./configure` but do not run make install, as it would replace your existing installation. Create a tmp flow dir and run the collector from the src directory. For example: + +`./nfcapd -l -S2 -y -p 9999 -m ` + +When adding `-m ` nfcapd exports the internal statistics every 5s the the exporter. + + + +## Note: + +Only the statistics is exposed and not the netflow recods itself. \ No newline at end of file diff --git a/dataSocket.go b/dataSocket.go new file mode 100644 index 0000000..f794bde --- /dev/null +++ b/dataSocket.go @@ -0,0 +1,197 @@ +/* + * Copyright (c) 2021, Peter Haag + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of the author nor the names of its contributors may be + * used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + */ + +/* + * dataSocket implements a UNIX socket server to receive data from nfcapd + * Up to now the exporter implements flows/packets/bytes counters per + * protocol(tcp/udp/icmp/other and the source identifier from the collector + * + */ + +package main + +/* + +#include + +typedef struct metric_record_s { + // Ident + char ident[128]; + // uptime + uint64_t uptime; + // flow stat + uint64_t numflows_tcp; + uint64_t numflows_udp; + uint64_t numflows_icmp; + uint64_t numflows_other; + // bytes stat + uint64_t numbytes_tcp; + uint64_t numbytes_udp; + uint64_t numbytes_icmp; + uint64_t numbytes_other; + // packet stat + uint64_t numpackets_tcp; + uint64_t numpackets_udp; + uint64_t numpackets_icmp; + uint64_t numpackets_other; +} metric_record_t; +*/ +import "C" + +import ( + // "encoding/binary" + "fmt" + "os" + "log" + "net" + "unsafe" +) + +const packetPrefix byte = '@' + +type nfsenMetric struct { + // Ident + ident string + // uptime + uptime uint64 + // flow stat + numFlows_tcp uint64 + numFlows_udp uint64 + numFlows_icmp uint64 + numFlows_other uint64 + // bytes stat + numBytes_tcp uint64 + numBytes_udp uint64 + numBytes_icmp uint64 + numBytes_other uint64 + // packet stat + numPackets_tcp uint64 + numPackets_udp uint64 + numPackets_icmp uint64 + numPackets_other uint64 +} + +var metric nfsenMetric + +type socketConf struct { + socketPath string + listener net.Listener +} + +func New(socketPath string) *socketConf { + conf := new(socketConf) + conf.socketPath = socketPath + return conf +} + +func (socket *socketConf) Open() error { + + if err := os.RemoveAll(socket.socketPath); err != nil { + return err + } + listener, err := net.Listen("unix", socket.socketPath) + if err != nil { + return err + } + socket.listener = listener + return nil + +} // End of Open + +func (socket *socketConf) Close() error { + + return socket.listener.Close() + +} // End of Close + +func processStat(conn net.Conn) { + + defer conn.Close() + + // storage for reading from socket. + readBuf := make([]byte, 10240) + + dataLen, err := conn.Read(readBuf) + if err != nil || dataLen == 0{ + fmt.Printf("Socket read error: %v\n", err) + return + } + if readBuf[0] != packetPrefix { + fmt.Printf("Message prefix error - got %u\n", readBuf[0]) + return + } + + /* + version := readBuf[1] + payloadSize := int(binary.LittleEndian.Uint16(readBuf[2:4])) + + fmt.Printf("Message size: %d, payload size: %d version: %d\n", + dataLen, payloadSize, version); + */ + + var s *C.metric_record_t = (*C.metric_record_t)(unsafe.Pointer(&readBuf[4])) + mutex.Lock() + metric.ident = C.GoString(&s.ident[0]) + metric.uptime = uint64(s.uptime) + metric.numFlows_tcp = uint64(s.numflows_tcp) + metric.numFlows_udp = uint64(s.numflows_udp) + metric.numFlows_icmp = uint64(s.numflows_icmp) + metric.numFlows_other = uint64(s.numflows_other) + + metric.numBytes_tcp = uint64(s.numbytes_tcp) + metric.numBytes_udp = uint64(s.numbytes_udp) + metric.numBytes_icmp = uint64(s.numbytes_icmp) + metric.numBytes_other = uint64(s.numbytes_other) + + metric.numPackets_tcp = uint64(s.numpackets_tcp) + metric.numPackets_udp = uint64(s.numpackets_udp) + metric.numPackets_icmp = uint64(s.numpackets_icmp) + metric.numPackets_other = uint64(s.numpackets_other) + mutex.Unlock() + + +} // end of processStat + +func (socket *socketConf) Run() { + + go func() { + for { + // Accept new connections from nfcapd collectors and + // dispatching them to goroutine processStat + conn, err := socket.listener.Accept() + if err != nil { + log.Fatal("accept error:", err) + } + // fmt.Printf("New connection\n") + go processStat(conn) + } + }() + +} // End of Run diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..8ad160a --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module nfsen_exporter + +go 1.14 + +require github.com/prometheus/client_golang v1.11.0 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..b198df8 --- /dev/null +++ b/go.sum @@ -0,0 +1,136 @@ +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/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/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/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= diff --git a/main.go b/main.go new file mode 100644 index 0000000..5948cb8 --- /dev/null +++ b/main.go @@ -0,0 +1,187 @@ +/* + * Copyright (c) 2021, Peter Haag + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * * Neither the name of the author nor the names of its contributors may be + * used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. +*/ + +/* + * Poc to implement a metric exporter for nfcapd collectors to Prometheus + */ + +package main + +import ( + "fmt" + "flag" + "log" + "net/http" + "os" + "os/signal" + "syscall" + "sync" + + "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promhttp" +) + +const namespace = "nfsen" + +var mutex *sync.Mutex + +var ( + listenAddress = flag.String("listen", ":9141", + "Address to listen on for telemetry") + metricsURI = flag.String("metrics URI", "/metrics", + "Path under which to expose metrics") + socketPath = flag.String("UNIX socket", "/tmp/nfsen.sock", + "Path for nfcapd collectors to connect") +) + +var ( + + // Metrics + uptime = prometheus.NewDesc( + prometheus.BuildFQName(namespace, "collector", "uptime"), + "nfsen uptime.", + []string{"version"}, nil, + ) + flowsReceived = prometheus.NewDesc( + prometheus.BuildFQName(namespace, "collector", "flows"), + "How many flows have been received (per ident and protocol (tcp/udp/icmp/other)).", + []string{"ident", "proto"}, nil, + ) + packetsReceived = prometheus.NewDesc( + prometheus.BuildFQName(namespace, "collector", "packets"), + "How many packets have been received (per ident and protocol) (tcp/udp/icmp/other).", + []string{"ident", "proto"}, nil, + ) + bytesReceived = prometheus.NewDesc( + prometheus.BuildFQName(namespace, "collector", "bytes"), + "How many bytes have been received (per ident and protocol) (tcp/udp/icmp/other).", + []string{"ident", "proto"}, nil, + ) +) + +type Exporter struct { + +} + +func NewExporter() *Exporter { + return &Exporter{ + + } +} // End of NewExporter + +func (e *Exporter) Describe(ch chan<- *prometheus.Desc) { + ch <- uptime + ch <- flowsReceived + ch <- packetsReceived + ch <- bytesReceived +} // End of Describe + +func (e *Exporter) Collect(ch chan<- prometheus.Metric) { + /* + fmt.Printf("Ident : %s\n", metric.ident) + fmt.Printf("Uptime : %d\n", metric.uptime) + fmt.Printf("Flows tcp : %d\n", metric.numFlows_tcp) + fmt.Printf("Flows udp : %d\n", metric.numFlows_udp) + fmt.Printf("Flows icmp : %d\n", metric.numFlows_icmp) + fmt.Printf("Flows other : %d\n", metric.numFlows_other) + fmt.Printf("Bytes tcp : %d\n", metric.numBytes_tcp) + fmt.Printf("Bytes udp : %d\n", metric.numBytes_udp) + fmt.Printf("Bytes icmp : %d\n", metric.numBytes_icmp) + fmt.Printf("Bytes other : %d\n", metric.numBytes_other) + fmt.Printf("Packets tcp : %d\n", metric.numPackets_tcp) + fmt.Printf("Packets udp : %d\n", metric.numPackets_udp) + fmt.Printf("Packets icmp : %d\n", metric.numPackets_icmp) + fmt.Printf("Packets other : %d\n", metric.numPackets_other) + */ + + mutex.Lock() + ch <- prometheus.MustNewConstMetric(uptime, prometheus.CounterValue, float64(metric.uptime), "v1.7-beta") + ch <- prometheus.MustNewConstMetric(flowsReceived, prometheus.CounterValue, float64(metric.numFlows_tcp), metric.ident, "tcp") + ch <- prometheus.MustNewConstMetric(flowsReceived, prometheus.CounterValue, float64(metric.numFlows_udp), metric.ident, "udp") + ch <- prometheus.MustNewConstMetric(flowsReceived, prometheus.CounterValue, float64(metric.numFlows_icmp), metric.ident, "icmp") + ch <- prometheus.MustNewConstMetric(flowsReceived, prometheus.CounterValue, float64(metric.numFlows_other), metric.ident, "other") + + // packets + ch <- prometheus.MustNewConstMetric(packetsReceived, prometheus.CounterValue, float64(metric.numPackets_tcp), metric.ident, "tcp") + ch <- prometheus.MustNewConstMetric(packetsReceived, prometheus.CounterValue, float64(metric.numPackets_udp), metric.ident, "udp") + ch <- prometheus.MustNewConstMetric(packetsReceived, prometheus.CounterValue, float64(metric.numPackets_icmp), metric.ident, "icmp") + ch <- prometheus.MustNewConstMetric(packetsReceived, prometheus.CounterValue, float64(metric.numPackets_other), metric.ident, "other") + + // bytes + ch <- prometheus.MustNewConstMetric(bytesReceived, prometheus.CounterValue, float64(metric.numBytes_tcp), metric.ident, "tcp") + ch <- prometheus.MustNewConstMetric(bytesReceived, prometheus.CounterValue, float64(metric.numBytes_udp), metric.ident, "udp") + ch <- prometheus.MustNewConstMetric(bytesReceived, prometheus.CounterValue, float64(metric.numPackets_icmp), metric.ident, "icmp") + ch <- prometheus.MustNewConstMetric(bytesReceived, prometheus.CounterValue, float64(metric.numPackets_other), metric.ident, "other") + metric = nfsenMetric{} + mutex.Unlock() + +} // End of Collect + +// cleanup on signal TERM/cntrl-C +func SetupCloseHandler(socketHandler *socketConf) { + c := make(chan os.Signal) + signal.Notify(c, os.Interrupt, syscall.SIGTERM) + go func() { + <-c + fmt.Printf("Exit exporter\n") + socketHandler.Close() + os.Remove(*socketPath) + os.Exit(0) + }() +} + +func main() { + + flag.Parse() + + exporter := NewExporter() + prometheus.MustRegister(exporter) + + mutex = new(sync.Mutex) + + socketHandler := New(*socketPath) + if err := socketHandler.Open(); err != nil { + log.Fatal("Socket handler failed: ", err) + } + SetupCloseHandler(socketHandler) + + socketHandler.Run() + + http.Handle(*metricsURI, promhttp.Handler()) + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + w.Write([]byte(` + NfSen Metric Exporter + +

NfSen Metric Exporter

+

Metrics

+ + `)) + }) + log.Fatal(http.ListenAndServe(*listenAddress, nil)) +}