Skip to content

Commit

Permalink
Add option to set flags via env variables
Browse files Browse the repository at this point in the history
  • Loading branch information
martialblog committed Sep 9, 2024
1 parent 1cafa1f commit 90309e7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
12 changes: 11 additions & 1 deletion check_bareos.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import argparse
import sys
import re
import os
import psycopg2
import psycopg2.extras

Expand Down Expand Up @@ -562,10 +563,19 @@ def printNagiosOutput(checkResult):


def commandline(args):
"""
Parse commandline arguments.
"""
def environ_or_required(key):
return ({'default': os.environ.get(key)} if os.environ.get(key) else {})

parser = argparse.ArgumentParser(description='Check Plugin for Bareos Backup Status')
group = parser.add_argument_group()
group.add_argument('-U', '--user', dest='user', action='store', required=True, help='user name for the database connections')
group.add_argument('-p', '--password', dest='password', action='store', help='password for the database connections', default="")
group.add_argument('-p', '--password', dest='password', action='store',
**environ_or_required('CHECK_BAREOS_DATABASE_PASSWORD'),
help='password for the database connections')

group.add_argument('-H', '--Host', dest='host', action='store', help='database host', default="127.0.0.1")
group.add_argument('-P', '--port', dest='port', action='store', help='database port', default=5432, type=int)
group.add_argument('-d', '--database', dest='database', default='bareos', help='database name')
Expand Down
11 changes: 11 additions & 0 deletions test_check_bareos.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import unittest
import unittest.mock as mock
import os
import sys

sys.path.append('..')
Expand Down Expand Up @@ -38,6 +39,16 @@ def test_commandline(self):
self.assertEqual(actual.host, 'localhost')
self.assertEqual(actual.user, 'bareos')


def test_commandline_fromenv(self):
os.environ['CHECK_BAREOS_DATABASE_PASSWORD'] = 'secret'

actual = commandline(['-H', 'localhost', '-U', 'bareos'])
self.assertEqual(actual.user, 'bareos')
self.assertEqual(actual.password, 'secret')

os.unsetenv('CHECK_BAREOS_DATABASE_PASSWORD')

class ThresholdTesting(unittest.TestCase):

def test_thresholds(self):
Expand Down

0 comments on commit 90309e7

Please sign in to comment.