-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'influxdata:master' into master
- Loading branch information
Showing
41 changed files
with
296 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
//go:generate ../../../tools/readme_config_includer/generator | ||
package ipset | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"net" | ||
"strings" | ||
|
||
"github.com/influxdata/telegraf" | ||
) | ||
|
||
type ipsetEntries struct { | ||
initialized bool | ||
setName string | ||
entries int | ||
ips int | ||
} | ||
|
||
func getCountInCidr(cidr string) (int, error) { | ||
_, ipNet, err := net.ParseCIDR(cidr) | ||
if err != nil { | ||
// check if single IP | ||
if net.ParseIP(cidr) == nil { | ||
return 0, errors.New("invalid IP address format. Not CIDR format and not a single IP address") | ||
} | ||
return 1, nil // Single IP has only one address | ||
} | ||
|
||
ones, bits := ipNet.Mask.Size() | ||
if ones == 0 && bits == 0 { | ||
return 0, errors.New("invalid CIDR range") | ||
} | ||
numIps := 1 << (bits - ones) | ||
|
||
// exclude network and broadcast addresses if IPv4 and range > /31 | ||
if bits == 32 && numIps > 2 { | ||
numIps -= 2 | ||
} | ||
|
||
return numIps, nil | ||
} | ||
|
||
func (counter *ipsetEntries) addLine(line string, acc telegraf.Accumulator) error { | ||
data := strings.Fields(line) | ||
if len(data) < 3 { | ||
return fmt.Errorf("error parsing line (expected at least 3 fields): %s", line) | ||
} | ||
|
||
switch data[0] { | ||
case "create": | ||
counter.commit(acc) | ||
counter.initialized = true | ||
counter.setName = data[1] | ||
counter.entries = 0 | ||
counter.ips = 0 | ||
case "add": | ||
counter.entries++ | ||
count, err := getCountInCidr(data[2]) | ||
if err != nil { | ||
return err | ||
} | ||
counter.ips += count | ||
} | ||
return nil | ||
} | ||
|
||
func (counter *ipsetEntries) commit(acc telegraf.Accumulator) { | ||
if !counter.initialized { | ||
return | ||
} | ||
|
||
fields := map[string]interface{}{ | ||
"entries": counter.entries, | ||
"ips": counter.ips, | ||
} | ||
|
||
tags := map[string]string{ | ||
"set": counter.setName, | ||
} | ||
|
||
acc.AddGauge(measurement, fields, tags) | ||
|
||
// reset counter and prepare for next usage | ||
counter.initialized = false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package ipset | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/influxdata/telegraf" | ||
"github.com/influxdata/telegraf/testutil" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestIpsetEntries(t *testing.T) { | ||
var acc testutil.Accumulator | ||
|
||
lines := []string{ | ||
"create mylist hash:net family inet hashsize 16384 maxelem 131072 timeout 300 bucketsize 12 initval 0x4effa9ad", | ||
"add mylist 89.101.238.143 timeout 161558", | ||
"add mylist 122.224.15.166 timeout 186758", | ||
"add mylist 47.128.40.145 timeout 431559", | ||
} | ||
|
||
entries := ipsetEntries{} | ||
for _, line := range lines { | ||
require.NoError(t, entries.addLine(line, &acc)) | ||
} | ||
entries.commit(&acc) | ||
|
||
expected := []telegraf.Metric{ | ||
testutil.MustMetric( | ||
"ipset", | ||
map[string]string{ | ||
"set": "mylist", | ||
}, | ||
map[string]interface{}{ | ||
"entries": 3, | ||
"ips": 3, | ||
}, | ||
time.Unix(0, 0), | ||
telegraf.Gauge, | ||
), | ||
} | ||
|
||
testutil.RequireMetricsEqual(t, expected, acc.GetTelegrafMetrics(), testutil.IgnoreTime()) | ||
} | ||
|
||
func TestIpsetEntriesCidr(t *testing.T) { | ||
var acc testutil.Accumulator | ||
|
||
lines := []string{ | ||
"create mylist0 hash:net family inet hashsize 16384 maxelem 131072 timeout 300 bucketsize 12 initval 0x4effa9ad", | ||
"add mylist0 89.101.238.143 timeout 161558", | ||
"add mylist0 122.224.5.0/24 timeout 186758", | ||
"add mylist0 47.128.40.145 timeout 431559", | ||
|
||
"create mylist1 hash:net family inet hashsize 16384 maxelem 131072 timeout 300 bucketsize 12 initval 0x4effa9ad", | ||
"add mylist1 90.101.238.143 timeout 161558", | ||
"add mylist1 44.128.40.145 timeout 431559", | ||
"add mylist1 122.224.5.0/8 timeout 186758", | ||
"add mylist1 45.128.40.145 timeout 431560", | ||
} | ||
|
||
entries := ipsetEntries{} | ||
for _, line := range lines { | ||
require.NoError(t, entries.addLine(line, &acc)) | ||
} | ||
entries.commit(&acc) | ||
|
||
expected := []telegraf.Metric{ | ||
testutil.MustMetric( | ||
"ipset", | ||
map[string]string{ | ||
"set": "mylist0", | ||
}, | ||
map[string]interface{}{ | ||
"entries": 3, | ||
"ips": 256, | ||
}, | ||
time.Now().Add(time.Millisecond*0), | ||
telegraf.Gauge, | ||
), | ||
testutil.MustMetric( | ||
"ipset", | ||
map[string]string{ | ||
"set": "mylist1", | ||
}, | ||
map[string]interface{}{ | ||
"entries": 4, | ||
"ips": 16777217, | ||
}, | ||
time.Unix(0, 0), | ||
telegraf.Gauge, | ||
), | ||
} | ||
|
||
testutil.RequireMetricsEqual(t, expected, acc.GetTelegrafMetrics(), testutil.IgnoreTime()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.