Module: check_mk
Branch: master
Commit: 2470f1811f83fd30818c7869a1bf83b2c06fbd0a
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=2470f1811f83fd…
Author: Simon Betz <si(a)mathias-kettner.de>
Date: Thu Jun 21 16:08:04 2018 +0200
6136 check_mk_agent.aix, check_mk_agent.linux, check_mk_agent.solaris: Now fileinfo
section is able to interpret extended globbing patterns
Change-Id: Ie296410cca4805a70b61af0e7eb1594b941dc3d3
---
.werks/6136 | 10 ++++++++
agents/check_mk_agent.aix | 47 +++++++++++++++++++++++++++++--------
agents/check_mk_agent.linux | 26 +++++++++++++++++----
agents/check_mk_agent.solaris | 54 ++++++++++++++++++++++++++++++-------------
checks/fileinfo | 36 ++++++++++++++++++-----------
5 files changed, 131 insertions(+), 42 deletions(-)
diff --git a/.werks/6136 b/.werks/6136
new file mode 100644
index 0000000..cf9a3a7
--- /dev/null
+++ b/.werks/6136
@@ -0,0 +1,10 @@
+Title: check_mk_agent.aix, check_mk_agent.linux, check_mk_agent.solaris: Now fileinfo
section is able to interpret extended globbing patterns
+Level: 1
+Component: checks
+Compatible: compat
+Edition: cre
+Version: 1.6.0i1
+Date: 1529590014
+Class: feature
+
+
diff --git a/agents/check_mk_agent.aix b/agents/check_mk_agent.aix
index e820990..2b6b2d1 100755
--- a/agents/check_mk_agent.aix
+++ b/agents/check_mk_agent.aix
@@ -287,27 +287,56 @@ then
done
fi
-# Fileinfo-Check: put patterns for files into /etc/check_mk/fileinfo.cfg
if [ -r "$MK_CONFDIR/fileinfo.cfg" ]; then
echo '<<<fileinfo:sep(124)>>>'
- date +%s
- # simulate output of the Linux "stat -c "%n|%s|%Y" command
- # TODO: feel free to improve this command. It opens the perl interpreter for every
file found...
+ /usr/bin/perl -e 'print time."\n"'
+
OLD_IFS=$IFS
IFS='
'
+
+ old_state=$(set +o)
+ set +f
while read -r pattern; do
case $pattern in
/*) for file in $pattern; do
- if [ -f "$file" ]; then
- /usr/bin/perl -e 'printf "$_|%s|%s\n", (stat) [7,9]
for @ARGV' "$file"
- else
- echo "$file|missing|`date +%s`"
+ if [ -d "$file" ]; then
+ continue
+ fi
+
+ if [ ! -f "$file" ]; then
+ # 'date' may be missing on AIX
+ echo "$file|missing|$(/usr/bin/perl -e 'print
time')"
+ continue
+ fi
+
+ if [ ! -r "$file" ]; then
+ # 'date' may be missing on AIX
+ echo "${file}|not readable|$(/usr/bin/perl -e 'print
time')"
+ continue
fi
- done ;;
+
+ FILELIST=${FILELIST:+"$FILELIST\n"}"${file}"
+ done
+ ;;
esac
done < "$MK_CONFDIR/fileinfo.cfg"
+ set +vx; eval "$old_state"
IFS=$OLD_IFS
+
+ # Collecting all files and starting perl in one call
+ # saves a lot of performance.
+ echo "$FILELIST" | perl -e '
+while (<>) {
+ my $files = $_;
+ chomp($files);
+ foreach my $file (split(/\\n/, $files)) {
+ ($device, $inode, $mode, $nlink, $uid, $gid, $rdev, $size,
+ $atime, $mtime, $ctime, $blksize, $blocks) = stat($file);
+ printf "$file|$size|$mtime|%04o\n", $mode & 07777;
+ }
+}
+'
fi
# powerHA
diff --git a/agents/check_mk_agent.linux b/agents/check_mk_agent.linux
index 4b14d9a..8b7d8c4 100755
--- a/agents/check_mk_agent.linux
+++ b/agents/check_mk_agent.linux
@@ -867,21 +867,39 @@ function replace_datevariable()
fi
}
-if [ -r "$MK_CONFDIR/fileinfo.cfg" ] ; then
+if [ -r "$MK_CONFDIR/fileinfo.cfg" ]; then
echo '<<<fileinfo:sep(124)>>>'
date +%s
OLD_IFS=$IFS
IFS='
'
- (cat $MK_CONFDIR/fileinfo.cfg $MK_CONFDIR/fileinfo.d/* 2>/dev/null) | while read
-r pattern; do
+ old_state=$(set +o)
+ set +f
+ (cat "$MK_CONFDIR/fileinfo.cfg" "$MK_CONFDIR/fileinfo.d/*"
2>/dev/null) | while read -r pattern; do
case $pattern in
/*) pattern=$(replace_datevariable "$pattern")
for file in $pattern; do
- stat -c "%n|%s|%Y" "$file" 2> /dev/null ||
echo "$file|missing|$(date +%s)"
- done ;;
+ if [ -d "$file" ]; then
+ continue
+ fi
+
+ if [ ! -f "$file" ]; then
+ echo "$file|missing|$(date +%s)"
+ continue
+ fi
+
+ if [ ! -r "$file" ]; then
+ echo "${file}|not readable|$(date +%s)"
+ continue
+ fi
+
+ stat -c "%n|%s|%Y" "$file"
+ done
+ ;;
esac
done
+ set +vx; eval "$old_state"
IFS=$OLD_IFS
fi
diff --git a/agents/check_mk_agent.solaris b/agents/check_mk_agent.solaris
index e5fa784..7afe5b5 100755
--- a/agents/check_mk_agent.solaris
+++ b/agents/check_mk_agent.solaris
@@ -373,34 +373,56 @@ function replace_datevariable()
fi
}
-if [ -f "$MK_CONFDIR/fileinfo.cfg" ]
-then
+if [ -r "$MK_CONFDIR/fileinfo.cfg" ]; then
echo '<<<fileinfo:sep(124)>>>'
/usr/bin/perl -e 'print time."\n"'
- # note: patterns in the fileinfo.cfg are resolved inside this loop
OLD_IFS=$IFS
IFS='
'
+
+ old_state=$(set +o)
+ set +f
while read -r pattern; do
case $pattern in
- /*) pattern=`replace_datevariable "$pattern"`
- for file in $pattern; do
- if [ -f "$file" ]; then
- echo "$file" | /usr/bin/perl -e '
- while(my $file = <>) {
- $file =~ s/\n$//;
- ($device, $inode, $mode, $nlink, $uid, $gid, $rdev,
$size,
- $atime, $mtime, $ctime, $blksize, $blocks) =
stat($file);
- print("$file|$size|$mtime\n");
- }'
- else
- echo "$file|missing|`/usr/bin/perl -e 'print
time'`"
+ /*) for file in $pattern; do
+ if [ -d "$file" ]; then
+ continue
+ fi
+
+ if [ ! -f "$file" ]; then
+ # 'date' may be missing on Solaris
+ echo "$file|missing|$(/usr/bin/perl -e 'print
time')"
+ continue
+ fi
+
+ if [ ! -r "$file" ]; then
+ # 'date' may be missing on Solaris
+ echo "${file}|not readable|$(/usr/bin/perl -e 'print
time')"
+ continue
fi
- done ;;
+
+ FILELIST=${FILELIST:+"$FILELIST\n"}"${file}"
+ done
+ ;;
esac
done < "$MK_CONFDIR/fileinfo.cfg"
+ set +vx; eval "$old_state"
IFS=$OLD_IFS
+
+ # Collecting all files and starting perl in one call
+ # saves a lot of performance.
+ echo "$FILELIST" | perl -e '
+while (<>) {
+ my $files = $_;
+ chomp($files);
+ foreach my $file (split(/\\n/, $files)) {
+ ($device, $inode, $mode, $nlink, $uid, $gid, $rdev, $size,
+ $atime, $mtime, $ctime, $blksize, $blocks) = stat($file);
+ printf "$file|$size|$mtime|%04o\n", $mode & 07777;
+ }
+}
+'
fi
# Libelle Business Shadow
diff --git a/checks/fileinfo b/checks/fileinfo
index 6b28477..f30055f 100644
--- a/checks/fileinfo
+++ b/checks/fileinfo
@@ -180,6 +180,8 @@ def check_fileinfo(item, params, info):
if item == line[0]:
if line[1] == "missing":
continue
+ elif line[1] == "not readable":
+ return 1, "File not readable"
state = 0
size = int(line[1])
age = reftime - int(line[2])
@@ -219,17 +221,18 @@ def check_fileinfo_groups(item, params, info):
yield 3, "No information sent by agent"
return
- outof_range_txt = fileinfo_check_timeranges(params)
- reftime = None
- count_all = 0
- age_oldest = None
- age_newest = 0
- size_all = 0
- size_smallest = None
- size_largest = 0
- date_inclusion = ""
- include_patterns = set([])
- exclude_patterns = set([])
+ outof_range_txt = fileinfo_check_timeranges(params)
+ reftime = None
+ count_all = 0
+ age_oldest = None
+ age_newest = 0
+ size_all = 0
+ size_smallest = None
+ size_largest = 0
+ date_inclusion = ""
+ include_patterns = set()
+ exclude_patterns = set()
+ files_not_readable = set()
# Start counting values on all files
for line in info:
@@ -278,8 +281,12 @@ def check_fileinfo_groups(item, params, info):
else:
excl_match = fnmatch.fnmatch(line[0], exclusion)
- if str(line[1]) not in ['missing', ''] and \
- incl_match and not excl_match:
+ if incl_match and not excl_match:
+ if str(line[1]) in ['missing', '']:
+ continue
+ elif str(line[1]) == "not readable":
+ files_not_readable.add(str(line[1]))
+
size = int(line[1])
size_all += size
if size_smallest is None:
@@ -317,6 +324,9 @@ def check_fileinfo_groups(item, params, info):
if date_inclusion:
check_definition.append(("Date pattern", "date pattern",
date_inclusion, str))
+ if files_not_readable:
+ yield 1, "Files not readable: %s" % ",
".join(files_not_readable)
+
yield fileinfo_check_function(check_definition, params, outof_range_txt)
if include_patterns:
yield 0, "\nInclude patterns: %s" % ",
".join(include_patterns)