Skip to content

Commit

Permalink
csv-values: header included in data results despite setting "--skip-…
Browse files Browse the repository at this point in the history
…header" (fix #706)
  • Loading branch information
markuslf committed Aug 22, 2023
1 parent 929d73f commit 0b40df7
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 31 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ Monitoring Plugins:

Monitoring Plugins:

* csv-values: header included in data results despite setting "--skip-header" (fix [#706](https://github.com/Linuxfabrik/monitoring-plugins/issues/706))
* journald-query: Rename perfdata from "sudo journald-query" to "journald-query"
* swap-usage: Fix Traceback `PdhAddEnglishCounterW failed`

Expand Down
10 changes: 5 additions & 5 deletions check-plugins/csv-values/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ Help
[--critical-query CRITICAL_QUERY] [--delimiter DELIMITER]
[--filename FILENAME] [--insecure] [--newline NEWLINE]
[--no-proxy] [--password PASSWORD] [--quotechar QUOTECHAR]
[--skip-header] [--test TEST] [--timeout TIMEOUT] [-u URL]
[--skip-header] [--timeout TIMEOUT] [-u URL]
[--username USERNAME] [-w WARN]
[--warning-query WARNING_QUERY]
Expand Down Expand Up @@ -147,9 +147,8 @@ Help
--password PASSWORD SMB or HTTP Basic Auth Password.
--quotechar QUOTECHAR
CSV quotechar. Default: `"`
--skip-header Treat the first row as header names. Default: True
--test TEST For unit tests. Needs "path-to-stdout-file,path-to-
stderr-file,expected-retc".
--skip-header Treat the first row as header names, and skip this
row. Default: False
--timeout TIMEOUT Network timeout in seconds. Default: 3 (seconds)
-u URL, --url URL Set the url of the CSV file, either starting with
"http://", "https://" or "smb://". This is mutually
Expand Down Expand Up @@ -210,7 +209,8 @@ Checking this local CSV file: WARN if more than 6 hosts in network A have more t
--warning-query='select * from data where network = "A" and WaitingUpdates > 3' \
--warning=6 \
--critical-query='select * from data where network <> "A" and WaitingUpdates > 4' \
--critical=2
--critical=2 \
--skip-header
Output:
Expand Down
8 changes: 4 additions & 4 deletions check-plugins/csv-values/csv-values
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ from lib.globals import (STATE_CRIT, STATE_OK, # pylint: disable=C0413
STATE_UNKNOWN, STATE_WARN)

__author__ = 'Linuxfabrik GmbH, Zurich/Switzerland'
__version__ = '2023071201'
__version__ = '2023082201'

DESCRIPTION = """This check imports a CSV file into an SQLite database and can then run a separate
warning query and/or a critical query against it. The result - the number of
Expand All @@ -45,7 +45,7 @@ DEFAULT_INSECURE = False
DEFAULT_NEWLINE = None
DEFAULT_NO_PROXY = False
DEFAULT_QUOTECHAR = '"'
DEFAULT_SKIP_HEADER = True
DEFAULT_SKIP_HEADER = False
DEFAULT_TIMEOUT = 3


Expand Down Expand Up @@ -158,9 +158,9 @@ def parse_args():

parser.add_argument(
'--skip-header',
help='Treat the first row as header names. Default: %(default)s',
help='Treat the first row as header names, and skip this row. Default: %(default)s',
dest='SKIP_HEADER',
action='store_false',
action='store_true',
default=DEFAULT_SKIP_HEADER,
)

Expand Down
51 changes: 29 additions & 22 deletions check-plugins/csv-values/unit-test/run
Original file line number Diff line number Diff line change
Expand Up @@ -28,72 +28,79 @@ class TestCheck(unittest.TestCase):
#self.assertIn('Waiting for more data (1).', stdout)
#self.assertRegex(stdout, r'1 error in Kernel Ring Buffer.')

def test_if_check_runs_EXAMPLE01(self):
stdout, stderr, retc = lib.base.coe(lib.shell.shell_exec(self.check + ' --filename=stdout/EXAMPLE01 --columns-query="date TEXT, network TEXT, hostname TEXT, waitingupdates INTEGER"'))
def test_if_check_runs_EXAMPLE01_01(self):
stdout, stderr, retc = lib.base.coe(lib.shell.shell_exec(self.check + ' --filename=stdout/EXAMPLE01 --columns-query="date TEXT, network TEXT, hostname TEXT, waitingupdates INTEGER" --skip-header'))
self.assertIn('Nothing to check, no queries provided', stdout)
self.assertEqual(stderr, '')
self.assertEqual(retc, STATE_UNKNOWN)

def test_if_check_runs_EXAMPLE02(self):
stdout, stderr, retc = lib.base.coe(lib.shell.shell_exec(self.check + ' --filename=stdout/EXAMPLE01.csv --columns-query="date TEXT, network TEXT, hostname TEXT, waitingupdates INTEGER"'))
def test_if_check_runs_EXAMPLE01_02(self):
stdout, stderr, retc = lib.base.coe(lib.shell.shell_exec(self.check + ' --filename=stdout/EXAMPLE01.csv --columns-query="date TEXT, network TEXT, hostname TEXT, waitingupdates INTEGER" --skip-header'))
self.assertIn('Nothing to check, no queries provided', stdout)
self.assertEqual(stderr, '')
self.assertEqual(retc, STATE_UNKNOWN)

def test_if_check_runs_EXAMPLE03(self):
stdout, stderr, retc = lib.base.coe(lib.shell.shell_exec(self.check + ' --filename=stdout/EXAMPLE01 --columns-query="date TEXT, network TEXT, hostname TEXT, waitingupdates INTEGER" --warning-query="test"'))
def test_if_check_runs_EXAMPLE01_03(self):
stdout, stderr, retc = lib.base.coe(lib.shell.shell_exec(self.check + ' --filename=stdout/EXAMPLE01 --columns-query="date TEXT, network TEXT, hostname TEXT, waitingupdates INTEGER" --warning-query="test" --skip-header'))
self.assertIn('I/O error "No such file or directory" while opening or reading stdout/EXAMPLE01', stdout)
self.assertEqual(stderr, '')
self.assertEqual(retc, STATE_UNKNOWN)

def test_if_check_runs_EXAMPLE04(self):
stdout, stderr, retc = lib.base.coe(lib.shell.shell_exec(self.check + ' --filename=stdout/EXAMPLE01.csv --columns-query="date TEXT, network TEXT, hostname TEXT, waitingupdates INTEGER" --warning-query="test"'))
def test_if_check_runs_EXAMPLE01_04(self):
stdout, stderr, retc = lib.base.coe(lib.shell.shell_exec(self.check + ' --filename=stdout/EXAMPLE01.csv --columns-query="date TEXT, network TEXT, hostname TEXT, waitingupdates INTEGER" --warning-query="test" --skip-header'))
self.assertIn('Query failed: test, Error: near "test": syntax error, Data: {}', stdout)
self.assertEqual(stderr, '')
self.assertEqual(retc, STATE_UNKNOWN)

def test_if_check_runs_EXAMPLE05(self):
stdout, stderr, retc = lib.base.coe(lib.shell.shell_exec(self.check + ' --filename=stdout/EXAMPLE01.csv --columns-query="date TEXT, network TEXT, hostname TEXT, waitingupdates INTEGER" --warning-query="select * from data where 1"'))
def test_if_check_runs_EXAMPLE01_05(self):
stdout, stderr, retc = lib.base.coe(lib.shell.shell_exec(self.check + ' --filename=stdout/EXAMPLE01.csv --columns-query="date TEXT, network TEXT, hostname TEXT, waitingupdates INTEGER" --warning-query="select * from data where 1" --skip-header'))
self.assertIn('24 results from warning query', stdout)
self.assertEqual(stderr, '')
self.assertEqual(retc, STATE_OK)

def test_if_check_runs_EXAMPLE06(self):
stdout, stderr, retc = lib.base.coe(lib.shell.shell_exec(self.check + ' --filename=stdout/EXAMPLE01.csv --columns-query="date TEXT, network TEXT, hostname TEXT, waitingupdates INTEGER" --warning-query="select * from x where 1"'))
def test_if_check_runs_EXAMPLE01_06(self):
stdout, stderr, retc = lib.base.coe(lib.shell.shell_exec(self.check + ' --filename=stdout/EXAMPLE01.csv --columns-query="date TEXT, network TEXT, hostname TEXT, waitingupdates INTEGER" --warning-query="select * from x where 1" --skip-header'))
self.assertIn('Query failed: select * from x where 1, Error: no such table: x, Data: {}', stdout)
self.assertEqual(stderr, '')
self.assertEqual(retc, STATE_UNKNOWN)

def test_if_check_runs_EXAMPLE07(self):
stdout, stderr, retc = lib.base.coe(lib.shell.shell_exec(self.check + ' --filename=stdout/EXAMPLE01.csv --columns-query="date TEXT, network TEXT, hostname TEXT, waitingupdates INTEGER" --warning-query="select * from data where network = \'A\' and WaitingUpdates > 3"'))
def test_if_check_runs_EXAMPLE01_07(self):
stdout, stderr, retc = lib.base.coe(lib.shell.shell_exec(self.check + ' --filename=stdout/EXAMPLE01.csv --columns-query="date TEXT, network TEXT, hostname TEXT, waitingupdates INTEGER" --warning-query="select * from data where network = \'A\' and WaitingUpdates > 3" --skip-header'))
self.assertIn('7 results from warning query', stdout)
self.assertEqual(stderr, '')
self.assertEqual(retc, STATE_OK)

def test_if_check_runs_EXAMPLE08(self):
stdout, stderr, retc = lib.base.coe(lib.shell.shell_exec(self.check + ' --filename=stdout/EXAMPLE01.csv --columns-query="date TEXT, network TEXT, hostname TEXT, waitingupdates INTEGER" --warning-query="select * from data where network = \'A\' and WaitingUpdates > 3" --warning=7'))
def test_if_check_runs_EXAMPLE01_08(self):
stdout, stderr, retc = lib.base.coe(lib.shell.shell_exec(self.check + ' --filename=stdout/EXAMPLE01.csv --columns-query="date TEXT, network TEXT, hostname TEXT, waitingupdates INTEGER" --warning-query="select * from data where network = \'A\' and WaitingUpdates > 3" --warning=7 --skip-header'))
self.assertIn('7 results from warning query', stdout)
self.assertEqual(stderr, '')
self.assertEqual(retc, STATE_OK)

def test_if_check_runs_EXAMPLE09(self):
stdout, stderr, retc = lib.base.coe(lib.shell.shell_exec(self.check + ' --filename=stdout/EXAMPLE01.csv --columns-query="date TEXT, network TEXT, hostname TEXT, waitingupdates INTEGER" --warning-query="select * from data where network = \'A\' and WaitingUpdates > 3" --warning=6'))
def test_if_check_runs_EXAMPLE01_09(self):
stdout, stderr, retc = lib.base.coe(lib.shell.shell_exec(self.check + ' --filename=stdout/EXAMPLE01.csv --columns-query="date TEXT, network TEXT, hostname TEXT, waitingupdates INTEGER" --warning-query="select * from data where network = \'A\' and WaitingUpdates > 3" --warning=6 --skip-header'))
self.assertIn('7 results from warning query', stdout)
self.assertEqual(stderr, '')
self.assertEqual(retc, STATE_WARN)

def test_if_check_runs_EXAMPLE10(self):
stdout, stderr, retc = lib.base.coe(lib.shell.shell_exec(self.check + ' --filename=stdout/EXAMPLE01.csv --columns-query="date TEXT, network TEXT, hostname TEXT, waitingupdates INTEGER" --warning-query="select * from data where network = \'A\' and WaitingUpdates > 3" --warning=6 --critical-query="select * from data where network <> \'A\' and WaitingUpdates > 4" --critical=4'))
def test_if_check_runs_EXAMPLE01_10(self):
stdout, stderr, retc = lib.base.coe(lib.shell.shell_exec(self.check + ' --filename=stdout/EXAMPLE01.csv --columns-query="date TEXT, network TEXT, hostname TEXT, waitingupdates INTEGER" --warning-query="select * from data where network = \'A\' and WaitingUpdates > 3" --warning=6 --critical-query="select * from data where network <> \'A\' and WaitingUpdates > 4" --critical=4 --skip-header'))
self.assertIn('7 results from warning query', stdout)
self.assertEqual(stderr, '')
self.assertEqual(retc, STATE_WARN)

def test_if_check_runs_EXAMPLE11(self):
stdout, stderr, retc = lib.base.coe(lib.shell.shell_exec(self.check + ' --filename=stdout/EXAMPLE01.csv --columns-query="date TEXT, network TEXT, hostname TEXT, waitingupdates INTEGER" --warning-query="select * from data where network = \'A\' and WaitingUpdates > 3" --warning=6 --critical-query="select * from data where network <> \'A\' and WaitingUpdates > 4" --critical=2'))
def test_if_check_runs_EXAMPLE01_11(self):
stdout, stderr, retc = lib.base.coe(lib.shell.shell_exec(self.check + ' --filename=stdout/EXAMPLE01.csv --columns-query="date TEXT, network TEXT, hostname TEXT, waitingupdates INTEGER" --warning-query="select * from data where network = \'A\' and WaitingUpdates > 3" --warning=6 --critical-query="select * from data where network <> \'A\' and WaitingUpdates > 4" --critical=2 --skip-header'))
self.assertIn('7 results from warning query', stdout)
self.assertEqual(stderr, '')
self.assertEqual(retc, STATE_CRIT)

def test_if_check_runs_EXAMPLE02_01(self):
stdout, stderr, retc = lib.base.coe(lib.shell.shell_exec(self.check + ' --filename=stdout/EXAMPLE02.csv --chunksize 1000 --columns-query="username TEXT, failedlogin INTEGER" --critical 1 --critical-query "SELECT * FROM data WHERE failedlogin > 10;" --delimiter "," --skip-header --timeout 3 --warning 1 --warning-query "SELECT * FROM data WHERE failedlogin > 5 and failedlogin <= 10;"'))
self.assertIn('0 results from warning query', stdout)
self.assertIn('and 1 result from critical query', stdout)
self.assertEqual(stderr, '')
self.assertEqual(retc, STATE_OK)


if __name__ == '__main__':
unittest.main()
3 changes: 3 additions & 0 deletions check-plugins/csv-values/unit-test/stdout/EXAMPLE02.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
username,failedlogin
myfirstuser,15
myseconduser,1

0 comments on commit 0b40df7

Please sign in to comment.