Skip to content

Commit

Permalink
feat(inputs.ipset): address review comments - part 2
Browse files Browse the repository at this point in the history
* rename misnamed "initialized" parameter
* remove "reset" function
* remove "initSet" function
* return err from addLine instead of adding to acc
* rework switch block in addLine
  • Loading branch information
verybadsoldier committed Nov 18, 2024
1 parent 60e0d89 commit 7bb1170
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 37 deletions.
10 changes: 6 additions & 4 deletions plugins/inputs/ipset/ipset.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ func (i *Ipset) Gather(acc telegraf.Accumulator) error {
line := scanner.Text()

if i.CountPerIPEntries {
i.entriesParser.addLine(line, acc)
err := i.entriesParser.addLine(line, acc)
if err != nil {
acc.AddError(err)
}
}

// Ignore sets created without the "counters" option
Expand Down Expand Up @@ -143,9 +146,8 @@ func setList(timeout config.Duration, useSudo bool) (*bytes.Buffer, error) {
func init() {
inputs.Add("ipset", func() telegraf.Input {
return &Ipset{
lister: setList,
entriesParser: ipsetEntries{},
Timeout: defaultTimeout,
lister: setList,
Timeout: defaultTimeout,
}
})
}
55 changes: 22 additions & 33 deletions plugins/inputs/ipset/ipset_entries.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import (
)

type ipsetEntries struct {
initizalized bool
setName string
entries int
ips int
initialized bool
setName string
entries int
ips int
}

func getCountInCidr(cidr string) (int, error) {
Expand All @@ -41,50 +41,39 @@ func getCountInCidr(cidr string) (int, error) {
return numIps, nil
}

func (counter *ipsetEntries) reset() {
counter.initSet("")
counter.initizalized = false
}

func (counter *ipsetEntries) initSet(setName string) {
counter.initizalized = true
counter.setName = setName
counter.entries = 0
counter.ips = 0
}

func (counter *ipsetEntries) addLine(line string, acc telegraf.Accumulator) {
func (counter *ipsetEntries) addLine(line string, acc telegraf.Accumulator) error {
data := strings.Fields(line)
if len(data) < 3 {
acc.AddError(fmt.Errorf("error parsing line (expected at least 3 fields): %s", line))
return
return fmt.Errorf("error parsing line (expected at least 3 fields): %s", line)
}

operation := data[0]
if operation == "create" {
switch data[0] {
case "create":
counter.commit(acc)
counter.initSet(data[1])
} else if operation == "add" {
counter.initialized = true
counter.setName = data[1]
counter.entries = 0
counter.ips = 0
case "add":
counter.entries++

ip := data[2]
count, err := getCountInCidr(ip)
count, err := getCountInCidr(data[2])
if err != nil {
acc.AddError(err)
return
return err
}
counter.ips += count
}
return nil
}

func (counter *ipsetEntries) commit(acc telegraf.Accumulator) {
if !counter.initizalized {
if !counter.initialized {
return
}

fields := make(map[string]interface{}, 3)
fields["entries"] = counter.entries
fields["ips"] = counter.ips
fields := map[string]interface{}{
"entries": counter.entries,
"ips": counter.ips,
}

tags := map[string]string{
"set": counter.setName,
Expand All @@ -93,5 +82,5 @@ func (counter *ipsetEntries) commit(acc telegraf.Accumulator) {
acc.AddGauge(measurement, fields, tags)

// reset counter and prepare for next usage
counter.reset()
counter.initialized = false
}

0 comments on commit 7bb1170

Please sign in to comment.