Skip to content
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

Draft: Add support for pfctl ruleset with anchors #177

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
23 changes: 10 additions & 13 deletions igcollect/artfiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,7 @@
import base64
from argparse import ArgumentParser
from time import time

try:
# Try importing the Python3 packages
from urllib.request import Request, urlopen
from urllib.parse import urlencode
except ImportError:
# On failure, import the Python2
from urllib2 import Request, urlopen
from urllib import urlencode
from urllib.request import Request, urlopen


def parse_args():
Expand Down Expand Up @@ -70,19 +62,24 @@ def parse_and_print(template, csv):
elif maxcur == '16A':
print(template.format(dc=dc, rack=rack, pdu_nr=pdu_nr, unit='max',
value='16.00'))
elif maxcur == '32A':
print(template.format(dc=dc, rack=rack, pdu_nr=pdu_nr, unit='max',
value='32.00'))
elif maxcur == 'redundant':
print(template.format(dc=dc, rack=rack, pdu_nr=pdu_nr, unit='max',
value='20.00'))
if measurement_type == 'ampere':
ampere = curval.replace(' A', '')
print(template.format(dc=dc, rack=rack, pdu_nr=pdu_nr, unit='ampere',
value=ampere))

# only for kwh racks we get the total
if measurement_type == 'kwh':
kwh = maxval_watt.replace(' kWh', '')
print(template.format(dc=dc, rack=rack, pdu_nr=pdu_nr, unit='kwh',
value=kwh))

# we now always get the current
ampere = curval.replace(' A', '')
print(template.format(dc=dc, rack=rack, pdu_nr=pdu_nr, unit='ampere',
value=ampere))


if __name__ == '__main__':
main()
9 changes: 4 additions & 5 deletions igcollect/pf.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from __future__ import print_function
from argparse import ArgumentParser
from subprocess import check_output
from subprocess import run
from time import time

import re
Expand Down Expand Up @@ -58,11 +58,10 @@ def parse_args():


def parse_pf_info():
pf_info_raw = check_output(
pf_info_raw = run(
['/sbin/pfctl', '-qvsi'],
universal_newlines=True,
close_fds=False,
).splitlines()
capture_output=True, text=True,
).stdout.splitlines()

pf_info = {}
for pf_info_graphite, (pf_info_section, pf_info_key) in PF_INFOS.items():
Expand Down
31 changes: 24 additions & 7 deletions igcollect/pf_labels.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
from __future__ import print_function
from argparse import ArgumentParser
from socket import gethostname
from subprocess import check_output
from subprocess import run
import json
import re
import time

POOL_RE = re.compile('(pool_[0-9]+)_([46]).*')
ANCHOR_RE = re.compile('anchor "(pool_[0-9]+_(sg|acl)_IPv[46]_if_[a-z0-9]+)"')

def parse_args():
parser = ArgumentParser()
Expand All @@ -23,12 +24,28 @@ def parse_args():


def parse_pf_labels():
# Get pfctl result of "show all labels"
pfctl_result = check_output(
pfctl_result = []

pfctl_result += run(
['/sbin/pfctl', '-q', '-sl'],
universal_newlines=True,
close_fds=False,
)
capture_output=True, text=True,
).stdout.splitlines()

# To obtain labels from a ruleset with anchors it is necessary to dive
# into each anchor.
pfctl_rules = run(
['/sbin/pfctl', '-q', '-sr'],
capture_output=True, text=True,
).stdout.splitlines()
for line in pfctl_rules:
anchor_re = ANCHOR_RE.match(line)
if not anchor_re:
continue
else:
pfctl_result += run(
['/sbin/pfctl', '-q', '-sl', '-a', anchor_re.group(1)],
capture_output=True, text=True,
).stdout.splitlines()

label_counters = {}

Expand All @@ -43,7 +60,7 @@ def parse_pf_labels():
reverse_pools[kpv['pf_name']] = kpk

# Read all lines
for line in pfctl_result.splitlines():
for line in pfctl_result:

# Split each line by ' ', this gives is the label name and values
line_tab = line.split(' ')
Expand Down