Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
21 changes: 21 additions & 0 deletions cmd/goflow2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"encoding/json"
"errors"
"flag"
"fmt"
Expand Down Expand Up @@ -307,6 +308,26 @@ func main() {
os.Exit(1)
}

// Add optional HTTP handler for templates
if nfP, ok := p.(*utils.NetFlowPipe); ok && *TemplatePath != "" {
http.HandleFunc(*TemplatePath, func(wr http.ResponseWriter, r *http.Request) {
templates := nfP.GetTemplatesForAllSources()
if body, err := json.MarshalIndent(templates, "", " "); err != nil {
slog.Error("error writing JSON body for /templates", slog.String("error", err.Error()))
wr.WriteHeader(http.StatusInternalServerError)
if _, err := wr.Write([]byte("Internal Server Error\n")); err != nil {
slog.Error("error writing HTTP", slog.String("error", err.Error()))
}
} else {
wr.Header().Add("Content-Type", "application/json")
wr.WriteHeader(http.StatusOK)
if _, err := wr.Write(body); err != nil {
slog.Error("error writing HTTP", slog.String("error", err.Error()))
}
}
})
}

decodeFunc = p.DecodeFlow
// intercept panic and generate error
decodeFunc = debug.PanicDecoderWrapper(decodeFunc)
Expand Down
1 change: 1 addition & 0 deletions decoders/netflow/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type NetFlowTemplateSystem interface {
RemoveTemplate(version uint16, obsDomainId uint32, templateId uint16) (interface{}, error)
GetTemplate(version uint16, obsDomainId uint32, templateId uint16) (interface{}, error)
AddTemplate(version uint16, obsDomainId uint32, templateId uint16, template interface{}) error
GetTemplates() FlowBaseTemplateSet
}

func (ts *BasicTemplateSystem) GetTemplates() FlowBaseTemplateSet {
Expand Down
4 changes: 4 additions & 0 deletions metrics/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,7 @@ func (s *PromTemplateSystem) RemoveTemplate(version uint16, obsDomainId uint32,

return template, err
}

func (s *PromTemplateSystem) GetTemplates() netflow.FlowBaseTemplateSet {
return s.wrapped.GetTemplates()
}
18 changes: 18 additions & 0 deletions utils/pipe.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package utils
import (
"bytes"
"fmt"
"maps"
"sync"

"github.com/netsampler/goflow2/v2/decoders/netflow"
Expand Down Expand Up @@ -223,6 +224,23 @@ func (p *NetFlowPipe) DecodeFlow(msg interface{}) error {
func (p *NetFlowPipe) Close() {
}

// Return a copy of the template set for all known netflow sources. Useful for exporting the
// templates via an HTTP endpoint
func (p *NetFlowPipe) GetTemplatesForAllSources() map[string]map[uint64]interface{} {
p.templateslock.RLock()
defer p.templateslock.RUnlock()

ret := make(map[string]map[uint64]interface{})
for k, v := range p.templates {
templateRef := v.GetTemplates()
var templateCopy = make(netflow.FlowBaseTemplateSet)
maps.Copy(templateCopy, templateRef)
ret[k] = templateCopy
}

return ret
}

type AutoFlowPipe struct {
*SFlowPipe
*NetFlowPipe
Expand Down