mirror of
https://github.com/akvorado/akvorado.git
synced 2025-12-12 06:24:10 +01:00
This change split the inlet component into a simpler inlet and a new outlet component. The new inlet component receive flows and put them in Kafka, unparsed. The outlet component takes them from Kafka and resume the processing from here (flow parsing, enrichment) and puts them in ClickHouse. The main goal is to ensure the inlet does a minimal work to not be late when processing packets (and restart faster). It also brings some simplification as the number of knobs to tune everything is reduced: for inlet, we only need to tune the queue size for UDP, the number of workers and a few Kafka parameters; for outlet, we need to tune a few Kafka parameters, the number of workers and a few ClickHouse parameters. The outlet component features a simple Kafka input component. The core component becomes just a callback function. There is also a new ClickHouse component to push data to ClickHouse using the low-level ch-go library with batch inserts. This processing has an impact on the internal representation of a FlowMessage. Previously, it was tailored to dynamically build the protobuf message to be put in Kafka. Now, it builds the batch request to be sent to ClickHouse. This makes the FlowMessage structure hides the content of the next batch request and therefore, it should be reused. This also changes the way we decode flows as they don't output FlowMessage anymore, they reuse one that is provided to each worker. The ClickHouse tables are slightly updated. Instead of using Kafka engine, the Null engine is used instead. Fix #1122
134 lines
3.9 KiB
Go
134 lines
3.9 KiB
Go
// SPDX-FileCopyrightText: 2022 Tchadel Icard
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
// Package sflow handles sFlow v5 decoding.
|
|
package sflow
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"net"
|
|
"time"
|
|
|
|
"github.com/netsampler/goflow2/v2/decoders/sflow"
|
|
|
|
"akvorado/common/reporter"
|
|
"akvorado/common/schema"
|
|
"akvorado/outlet/flow/decoder"
|
|
)
|
|
|
|
const (
|
|
// interfaceLocal is used for InIf and OutIf when the traffic is
|
|
// locally originated or terminated. We need to translate it to 0.
|
|
interfaceLocal = 0x3fffffff
|
|
// interfaceOutMask is the mask to interpret output interface type
|
|
interfaceOutMask = 0xc0000000
|
|
// interfaceOutDiscard is used for OutIf when the traffic is discarded
|
|
interfaceOutDiscard = 0x40000000
|
|
// interfaceOutMultiple is used when there are multiple output interfaces
|
|
interfaceOutMultiple = 0x80000000
|
|
)
|
|
|
|
// Decoder contains the state for the sFlow v5 decoder.
|
|
type Decoder struct {
|
|
r *reporter.Reporter
|
|
d decoder.Dependencies
|
|
errLogger reporter.Logger
|
|
|
|
metrics struct {
|
|
errors *reporter.CounterVec
|
|
stats *reporter.CounterVec
|
|
sampleRecordsStatsSum *reporter.CounterVec
|
|
sampleStatsSum *reporter.CounterVec
|
|
}
|
|
}
|
|
|
|
// New instantiates a new sFlow decoder.
|
|
func New(r *reporter.Reporter, dependencies decoder.Dependencies) decoder.Decoder {
|
|
nd := &Decoder{
|
|
r: r,
|
|
d: dependencies,
|
|
errLogger: r.Sample(reporter.BurstSampler(30*time.Second, 3)),
|
|
}
|
|
|
|
nd.metrics.errors = nd.r.CounterVec(
|
|
reporter.CounterOpts{
|
|
Name: "errors_total",
|
|
Help: "sFlows processed errors.",
|
|
},
|
|
[]string{"exporter", "error"},
|
|
)
|
|
nd.metrics.stats = nd.r.CounterVec(
|
|
reporter.CounterOpts{
|
|
Name: "flows_total",
|
|
Help: "sFlows processed.",
|
|
},
|
|
[]string{"exporter", "agent", "version"},
|
|
)
|
|
nd.metrics.sampleRecordsStatsSum = nd.r.CounterVec(
|
|
reporter.CounterOpts{
|
|
Name: "sample_records_sum",
|
|
Help: "sFlows samples sum of records.",
|
|
},
|
|
[]string{"exporter", "agent", "version", "type"},
|
|
)
|
|
nd.metrics.sampleStatsSum = nd.r.CounterVec(
|
|
reporter.CounterOpts{
|
|
Name: "sample_sum",
|
|
Help: "sFlows samples sum.",
|
|
},
|
|
[]string{"exporter", "agent", "version", "type"},
|
|
)
|
|
|
|
return nd
|
|
}
|
|
|
|
// Decode decodes an sFlow payload.
|
|
func (nd *Decoder) Decode(in decoder.RawFlow, _ decoder.Option, bf *schema.FlowMessage, finalize decoder.FinalizeFlowFunc) (int, error) {
|
|
buf := bytes.NewBuffer(in.Payload)
|
|
key := in.Source.String()
|
|
ts := uint64(in.TimeReceived.UTC().Unix())
|
|
|
|
var packet sflow.Packet
|
|
if err := sflow.DecodeMessageVersion(buf, &packet); err != nil {
|
|
nd.metrics.errors.WithLabelValues(key, "sFlow decoding error").Inc()
|
|
nd.errLogger.Err(err).Str("exporter", key).Msg("error while decoding sFlow")
|
|
return 0, fmt.Errorf("error while decoding sFlow: %w", err)
|
|
}
|
|
|
|
// Update some stats
|
|
agent := net.IP(packet.AgentIP).String()
|
|
version := "5"
|
|
samples := packet.Samples
|
|
nd.metrics.stats.WithLabelValues(key, agent, version).Inc()
|
|
for _, s := range samples {
|
|
switch sConv := s.(type) {
|
|
case sflow.FlowSample:
|
|
nd.metrics.sampleStatsSum.WithLabelValues(key, agent, version, "FlowSample").
|
|
Inc()
|
|
nd.metrics.sampleRecordsStatsSum.WithLabelValues(key, agent, version, "FlowSample").
|
|
Add(float64(len(sConv.Records)))
|
|
case sflow.ExpandedFlowSample:
|
|
nd.metrics.sampleStatsSum.WithLabelValues(key, agent, version, "ExpandedFlowSample").
|
|
Inc()
|
|
nd.metrics.sampleRecordsStatsSum.WithLabelValues(key, agent, version, "ExpandedFlowSample").
|
|
Add(float64(len(sConv.Records)))
|
|
case sflow.CounterSample:
|
|
nd.metrics.sampleStatsSum.WithLabelValues(key, agent, version, "CounterSample").
|
|
Inc()
|
|
nd.metrics.sampleRecordsStatsSum.WithLabelValues(key, agent, version, "CounterSample").
|
|
Add(float64(len(sConv.Records)))
|
|
}
|
|
}
|
|
|
|
return len(samples), nd.decode(packet, bf, func() {
|
|
bf.TimeReceived = uint32(ts)
|
|
finalize()
|
|
})
|
|
}
|
|
|
|
// Name returns the name of the decoder.
|
|
func (nd *Decoder) Name() string {
|
|
return "sflow"
|
|
}
|