Module: check_mk
Branch: master
Commit: 4d1e5f097fafaa6790c0e83b09670726925d029a
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=4d1e5f097fafaa…
Author: Moritz Kiemer <mo(a)mathias-kettner.de>
Date: Wed Sep 26 07:46:06 2018 +0200
fileinfo: Fix bug in agents and make parse_function more robust
Change-Id: I558e0da2db7694f24421118f1713965807640788
---
agents/check_mk_agent.aix | 2 +-
agents/check_mk_agent.freebsd | 2 +-
agents/check_mk_agent.linux | 2 +-
agents/check_mk_agent.macosx | 2 +-
agents/check_mk_agent.openwrt | 2 +-
agents/check_mk_agent.solaris | 2 +-
checks/fileinfo | 61 +++++++++-------------
.../generictests/datasets/fileinfo_new_empty.py | 15 ++++++
.../datasets/fileinfo_old_empty_regression.py | 13 +++++
9 files changed, 59 insertions(+), 42 deletions(-)
diff --git a/agents/check_mk_agent.aix b/agents/check_mk_agent.aix
index 084ec1e..810060f 100755
--- a/agents/check_mk_agent.aix
+++ b/agents/check_mk_agent.aix
@@ -304,7 +304,7 @@ if [ -r "$MK_CONFDIR/fileinfo.cfg" ]; then
done | perl -e '
print time."\n";
print "[[[header]]]\n";
- print "name|status|size|date\n";
+ print "name|status|size|time\n";
print "[[[content]]]\n";
while (<>) {
chomp $_;
diff --git a/agents/check_mk_agent.freebsd b/agents/check_mk_agent.freebsd
index 95ea306..33e3d8d 100755
--- a/agents/check_mk_agent.freebsd
+++ b/agents/check_mk_agent.freebsd
@@ -396,7 +396,7 @@ if [ -r "$MK_CONFDIR/fileinfo.cfg" ]; then
done | perl -e '
print time."\n";
print "[[[header]]]\n";
- print "name|status|size|date\n";
+ print "name|status|size|time\n";
print "[[[content]]]\n";
while (<>) {
chomp $_;
diff --git a/agents/check_mk_agent.linux b/agents/check_mk_agent.linux
index 3dd5a3f..93407d0 100755
--- a/agents/check_mk_agent.linux
+++ b/agents/check_mk_agent.linux
@@ -895,7 +895,7 @@ if [ -r "$MK_CONFDIR/fileinfo.cfg" ]; then
done | perl -e '
print time."\n";
print "[[[header]]]\n";
- print "name|status|size|date\n";
+ print "name|status|size|time\n";
print "[[[content]]]\n";
while (<>) {
chomp $_;
diff --git a/agents/check_mk_agent.macosx b/agents/check_mk_agent.macosx
index af2bb7d..95e5580 100755
--- a/agents/check_mk_agent.macosx
+++ b/agents/check_mk_agent.macosx
@@ -112,7 +112,7 @@ if [ -r "$MK_CONFDIR/fileinfo.cfg" ]; then
done | perl -e '
print time."\n";
print "[[[header]]]\n";
- print "name|status|size|date\n";
+ print "name|status|size|time\n";
print "[[[content]]]\n";
while (<>) {
chomp $_;
diff --git a/agents/check_mk_agent.openwrt b/agents/check_mk_agent.openwrt
index 91e2f01..8bba808 100755
--- a/agents/check_mk_agent.openwrt
+++ b/agents/check_mk_agent.openwrt
@@ -684,7 +684,7 @@ if [ -r "$MK_CONFDIR/fileinfo.cfg" ] ; then
IFS='
'
echo "[[[header]]]"
- echo " name | status | size | date "
+ echo " name | status | size | time "
echo "[[[content]]]"
# name | status (missing/stat failed/ok)) | size | date
# filename|missing||
diff --git a/agents/check_mk_agent.solaris b/agents/check_mk_agent.solaris
index 69876b9..53062c5 100755
--- a/agents/check_mk_agent.solaris
+++ b/agents/check_mk_agent.solaris
@@ -373,7 +373,7 @@ if [ -r "$MK_CONFDIR/fileinfo.cfg" ]; then
done | perl -e '
print time."\n";
print "[[[header]]]\n";
- print "name|status|size|date\n";
+ print "name|status|size|time\n";
print "[[[content]]]\n";
while (<>) {
chomp $_;
diff --git a/checks/fileinfo b/checks/fileinfo
index 2e1d3b3..a1998f4 100644
--- a/checks/fileinfo
+++ b/checks/fileinfo
@@ -50,31 +50,26 @@ import collections
fileinfo_groups = []
-def _get_field(row, index):
+def _get_field(row, index, data_type):
try:
- return row[index]
- except IndexError:
- return ''
+ return data_type(row[index])
+ except (IndexError, ValueError, TypeError):
+ return None
def _parse_backwards_compatible(info):
for row in info[1:]:
- name = _get_field(row, 0)
+ name = _get_field(row, 0, str)
# endswith("No such file...") is needed to
# support the very old solaris perl based version of fileinfo
if not name or name.endswith("No such file or directory"):
continue
- ambiguous = _get_field(row, 1)
+ ambiguous = _get_field(row, 1, str)
missing = ambiguous == 'missing'
failed = ambiguous in ('not readable', '')
- try:
- size = int(ambiguous)
- except ValueError:
- size = None
- try:
- time = int(_get_field(row, 2))
- except ValueError:
- time = None
+ size = _get_field(row, 1, int)
+ time = _get_field(row, 2, int)
+
yield name, missing, failed, size, time
@@ -82,45 +77,39 @@ def parse_fileinfo(info):
if not info:
return {}
parsed = {'files': {}}
- try:
- parsed['reftime'] = int(info[0][0])
- except (IndexError, ValueError):
- pass
+
+ parsed['reftime'] = _get_field(info[0], 0, int)
FileinfoItem = collections.namedtuple(
"FileinfoItem", "name missing failed size time")
# deal with old agents' output:
- if _get_field(_get_field(info, 1), 0) != '[[[header]]]':
+ if len(info) > 1 and _get_field(info[1], 0, str) != '[[[header]]]':
for args in _parse_backwards_compatible(info):
fi = FileinfoItem(*args)
parsed['files'][fi.name] = fi
return parsed
- header = _get_field(info, 2)
+ if len(info) < 3:
+ return parsed
+
+ header = info[2]
name_i = header.index('name') if 'name' in header else None
size_i = header.index('size') if 'size' in header else None
time_i = header.index('time') if 'time' in header else None
- status_i = header.index('status') if 'status' in header else None
+ stat_i = header.index('status') if 'status' in header else None
for row in info[4:]:
- name = _get_field(row, name_i)
- if not name:
- continue
- status = _get_field(row, status_i)
- if not status:
+ name = _get_field(row, name_i, str)
+ stat = _get_field(row, stat_i, str)
+ size = _get_field(row, size_i, int)
+ time = _get_field(row, time_i, int)
+
+ if name is None or stat is None:
continue
- try:
- size = int(row[size_i])
- except (IndexError, ValueError):
- size = None
- try:
- time = int(row[time_i])
- except (IndexError, ValueError):
- time = None
- fi = FileinfoItem(name, ('missing' in status),
- ('stat failed' in status), size, time)
+ fi = FileinfoItem(name, ('missing' in stat),
+ ('stat failed' in stat), size, time)
parsed['files'][fi.name] = fi
return parsed
diff --git a/tests/unit/checks/generictests/datasets/fileinfo_new_empty.py
b/tests/unit/checks/generictests/datasets/fileinfo_new_empty.py
new file mode 100644
index 0000000..1f40c00
--- /dev/null
+++ b/tests/unit/checks/generictests/datasets/fileinfo_new_empty.py
@@ -0,0 +1,15 @@
+
+
+checkname = 'fileinfo'
+
+
+info = [
+ ['1536557964'],
+ ['[[[header]]]'],
+ ['name', 'status', 'size', 'time'],
+ ['[[[content]]]'],
+]
+
+
+discovery = {'': [], 'groups': []}
+
diff --git a/tests/unit/checks/generictests/datasets/fileinfo_old_empty_regression.py
b/tests/unit/checks/generictests/datasets/fileinfo_old_empty_regression.py
new file mode 100644
index 0000000..e4ee53c
--- /dev/null
+++ b/tests/unit/checks/generictests/datasets/fileinfo_old_empty_regression.py
@@ -0,0 +1,13 @@
+
+
+checkname = 'fileinfo'
+
+
+info = [
+ ['1536557964'],
+]
+
+
+discovery = {'': [], 'groups': []}
+
+