Skip to content

Created an ipset textfile exporter #144

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
85 changes: 85 additions & 0 deletions ipsets
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#!/usr/bin/env bash
#
# Expose some useful ipset related metrics
#
# Usage: add this to crontab:
#
# */5 * * * * root sh -c 'path_to_/ipsets | sponge /var/lib/node_exporter/ipset.prom'

# Author: Clément Bourgeois <[email protected]>
set -eu

if [ "$(id -u)" != "0" ]; then
echo 1>&2 "This script must be run with super-user privileges."
exit 1
fi

# We first check if the ipset tooling is installed
if ! [ -x "$(command -v /usr/sbin/ipset)" ]; then
echo 1>&2 "Ipset tooling not available"
exit 1
fi

# Gathering available ipsets
ipset_list=$(/usr/sbin/ipset list -n)

if [ -e "$ipset_list" ]; then
exit 0
fi

counts=()
sizes=()
references=()
types=()

for ipset_name in $ipset_list; do
ipset_info=$(/usr/sbin/ipset list "$ipset_name")

entry_count=$(echo "$ipset_info" | grep -oE 'entries:[[:space:]]*[0-9]+' | awk '{print $2}')
entry_size=$(echo "$ipset_info" | grep -oE 'memory:[[:space:]]*[0-9]+' | awk '{print $2}')
entry_references=$(echo "$ipset_info" | grep -oE 'References:[[:space:]]*[0-9]+' | awk '{print $2}')
entry_type=$(echo "$ipset_info" | grep -oE 'Type:[[:space:]]*[[:print:]]+' | awk '{print $2}')

counts+=("ipset_entry_total{name=\"$ipset_name\"} $entry_count")
sizes+=("ipset_size_bytes{name=\"$ipset_name\"} $entry_size")
references+=("ipset_references_total{name=\"$ipset_name\"} $entry_references")
types+=("ipset_type{name=\"$ipset_name\", type=\"$entry_type\"} 1")
done

# Final output for collector

if [ ${#counts[@]} -gt 0 ]; then
echo "# HELP ipset_entry_total Number of entries inside the ipset"
echo "# TYPE ipset_entry_total gauge"

for line in "${counts[@]}"; do
echo "$line"
done
fi

if [ ${#sizes[@]} -gt 0 ]; then
echo "# HELP ipset_size_bytes In-memory size of the ipset"
echo "# TYPE ipset_size_bytes gauge"

for line in "${sizes[@]}"; do
echo "$line"
done
fi

if [ ${#references[@]} -gt 0 ]; then
echo "# HELP ipset_references_total Number of references of this ipset"
echo "# TYPE ipset_references_total gauge"

for line in "${references[@]}"; do
echo "$line"
done
fi

if [ ${#types[@]} -gt 0 ]; then
echo "# HELP ipset_type Type information about this ipset"
echo "# TYPE ipset_type gauge"

for line in "${types[@]}"; do
echo "$line"
done
fi