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

add ignore option, to ignore check on jails, and/or host #11

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
29 changes: 21 additions & 8 deletions src/checkpkgaudit/checkpkgaudit.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def _popen(cmd): # pragma: no cover
raise nagiosplugin.CheckError(message)


def _get_jails():
def _get_jails(ignored_jails=[]):
"""Provides running jails."""
jailargs = []
jls = subprocess.check_output('jls')
Expand All @@ -42,7 +42,7 @@ def _get_jails():
jailargs = list()
for jail in jails:
host_idx = 1 if len(jail.split()) == 3 else 2
if not jail.split()[host_idx].startswith('hastd:'):
if not jail.split()[host_idx].startswith('hastd:') and jail.split()[host_idx] not in ignored_jails:
jailargs.append({'jid': jail.split()[0],
'hostname': jail.split()[host_idx]})
return jailargs
Expand All @@ -53,6 +53,15 @@ class CheckPkgAudit(nagiosplugin.Resource):

hostname = platform.node()


def __init__(self, ignored_jails=[]):
"""Create CheckPkgAudit Ressource.

Store ignored jails in ignored_jails list
"""
self.ignored_jails = ignored_jails


def pkg_audit(self, jail=None):
"""Run pkg audit.

Expand Down Expand Up @@ -91,11 +100,11 @@ def pkg_audit(self, jail=None):

def probe(self):
"""Runs pkg audit over host and running jails."""

yield nagiosplugin.Metric(self.hostname, self.pkg_audit(),
min=0, context="pkg_audit")
if not self.hostname in self.ignored_jails:
yield nagiosplugin.Metric(self.hostname, self.pkg_audit(),
min=0, context="pkg_audit")
# yield running jails
jails = _get_jails()
jails = _get_jails(self.ignored_jails)
if jails:
for jail in jails:
yield nagiosplugin.Metric(jail['hostname'],
Expand Down Expand Up @@ -136,6 +145,10 @@ def problem(self, results):
def parse_args(): # pragma: no cover
"""Arguments parser."""
argp = argparse.ArgumentParser(description=__doc__)
argp.add_argument('-i', '--ignore', action="append",
metavar='ignored jails', dest='ignored_jails',
help='ignored jail name or host hostname \n \
ex : -i ns0 -i host')
argp.add_argument('-v', '--verbose', action='count', default=0,
help='increase output verbosity (use up to 3 times)')

Expand All @@ -154,11 +167,11 @@ def main(): # pragma: no cover
"""

args = parse_args()
check = nagiosplugin.Check(CheckPkgAudit(),
check = nagiosplugin.Check(CheckPkgAudit(args.ignored_jails),
nagiosplugin.ScalarContext('pkg_audit', None,
'@1:'),
AuditSummary())
check.main(verbose=args.verbose)
check.main(verbose=args.verbose, timeout=0)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check.main(verbose=args.verbose)
timeout=0 is only for debugging stuff



if __name__ == '__main__': # pragma: no cover
Expand Down
19 changes: 16 additions & 3 deletions src/checkpkgaudit/tests/test_checkauditpkg.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ def test__get_jls_no_running_jails(self):
mocked = "checkpkgaudit.checkpkgaudit.subprocess"
with mock.patch(mocked) as subprocess:
subprocess.check_output.return_value = no_jails
self.assertEqual(meth(), [])
self.assertEqual(meth(ignored_jails=[]), [])

def test__get_jls_running_jails(self):
def test__get_jls_running_jails_without_ignored(self):
meth = checkpkgaudit._get_jails
mocked = "checkpkgaudit.checkpkgaudit.subprocess"
jls = [{'hostname': 'masterdns', 'jid': '50'},
Expand All @@ -46,7 +46,20 @@ def test__get_jls_running_jails(self):
{'hostname': 'formationpy', 'jid': '61'}]
with mock.patch(mocked) as subprocess:
subprocess.check_output.return_value = ''.join(jails)
self.assertEqual(meth(), jls)
self.assertEqual(meth(ignored_jails=[]), jls)

def test__get_jls_running_jails_with_ignored(self):
meth = checkpkgaudit._get_jails
mocked = "checkpkgaudit.checkpkgaudit.subprocess"
jls = [{'hostname': 'masterdns', 'jid': '50'},
{'hostname': 'smtp', 'jid': '52'},
{'hostname': 'ns1', 'jid': '55'},
{'hostname': 'http', 'jid': '57'},
{'hostname': 'supervision', 'jid': '59'},
{'hostname': 'formationpy', 'jid': '61'}]
with mock.patch(mocked) as subprocess:
subprocess.check_output.return_value = ''.join(jails)
self.assertEqual(meth(ignored_jails=['ns0']), jls)


class Test_CheckPkgAudit(unittest.TestCase):
Expand Down