Module: check_mk
Branch: master
Commit: bff8c88142c5059ef1a92caaa618cee6925ec087
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=bff8c88142c505…
Author: Tom Baerwinkel <tb(a)mathias-kettner.de>
Date: Fri Sep 29 09:59:06 2017 +0200
5338 fileinfo: date variables can be defined in fileinfo.cfg
This werk adds the possibility to define date variables in the file
fileinfo.cfg either directly or via the corresponding rule Count,
size and age of files. Date variables can be used on linux and solaris
hosts and have the form $DATE:format-spec$ where format-spec is a list
of time format directives of the unix date command. Example:
$DATE:%Y%m%d$ is todays date in the format YYYYMMDD.
Using this option the agent will only send files containing the
current date. Date variables can be used together with the rules
File Grouping Patterns and Size, age count of file groups to monitor
if files are created in a directory on a dayly basis. Note that the
option Only check during the folling times of the day should be set.
Otherwise a warning may be displayed if the file of the current day
is not present yet.
Change-Id: I6b8487fba3a59c704d667ade8840106165e559cd
---
.werks/5338 | 22 ++++++++++++++++++++++
agents/check_mk_agent.linux | 17 +++++++++++++++++
agents/check_mk_agent.solaris | 18 ++++++++++++++++++
3 files changed, 57 insertions(+)
diff --git a/.werks/5338 b/.werks/5338
new file mode 100644
index 0000000..6927dad
--- /dev/null
+++ b/.werks/5338
@@ -0,0 +1,22 @@
+Title: fileinfo: date variables can be defined in fileinfo.cfg
+Level: 1
+Component: checks
+Compatible: compat
+Edition: cre
+Version: 1.5.0i1
+Date: 1506670399
+Class: feature
+
+This werk adds the possibility to define date variables in the file
+fileinfo.cfg either directly or via the corresponding rule Count,
+size and age of files. Date variables can be used on linux and solaris
+hosts and have the form $DATE:format-spec$ where format-spec is a list
+of time format directives of the unix date command. Example:
+$DATE:%Y%m%d$ is todays date in the format YYYYMMDD.
+Using this option the agent will only send files containing the
+current date. Date variables can be used together with the rules
+File Grouping Patterns and Size, age count of file groups to monitor
+if files are created in a directory on a dayly basis. Note that the
+option Only check during the folling times of the day should be set.
+Otherwise a warning may be displayed if the file of the current day
+is not present yet.
diff --git a/agents/check_mk_agent.linux b/agents/check_mk_agent.linux
index a3b3518..ec7af26 100755
--- a/agents/check_mk_agent.linux
+++ b/agents/check_mk_agent.linux
@@ -745,6 +745,22 @@ fi
# Fileinfo-Check: put patterns for files into /etc/check_mk/fileinfo.cfg
+function replace_datevariable()
+{
+ # Replace the date variable of the input, e.g. $DATE:%Y%m%d$, by
+ # the current date. If there's no match just return the input.
+ local file_name="$1"
+ local pattern='(\$DATE:(.*)\$)'
+
+ if [[ ! $file_name =~ $pattern ]]; then
+ echo "$file_name"
+ else
+ date_variable="${BASH_REMATCH[1]}"
+ format_string="${BASH_REMATCH[2]}"
+ echo "${file_name/$date_variable/$(date +$format_string)}"
+ fi
+}
+
if [ -r "$MK_CONFDIR/fileinfo.cfg" ] ; then
echo '<<<fileinfo:sep(124)>>>'
date +%s
@@ -752,6 +768,7 @@ if [ -r "$MK_CONFDIR/fileinfo.cfg" ] ; then
do
# only work on lines containing files, skip e.g. comments and blank lines
if [ ${line:0:1} = "/" ]; then
+ line="$(replace_datevariable $line)"
stat -c "%n|%s|%Y" "${line}" 2>/dev/null
if [ $? -ne 0 ]; then
diff --git a/agents/check_mk_agent.solaris b/agents/check_mk_agent.solaris
index 57bd19c..05134d1 100755
--- a/agents/check_mk_agent.solaris
+++ b/agents/check_mk_agent.solaris
@@ -348,6 +348,22 @@ fi
# Fileinfo-Check: put patterns for files into $MK_CONFDIR/fileinfo.cfg
+function replace_datevariable()
+{
+ # Replace the date variable of the input, e.g. $DATE:%Y%m%d$, by
+ # the current date. If there's no match just return the input.
+ local file_name="$1"
+ local pattern='(\$DATE:(.*)\$)'
+
+ if [[ ! $file_name =~ $pattern ]]; then
+ echo "$file_name"
+ else
+ date_variable="${BASH_REMATCH[1]}"
+ format_string="${BASH_REMATCH[2]}"
+ echo "${file_name/$date_variable/$(date +$format_string)}"
+ fi
+}
+
if [ -f "$MK_CONFDIR/fileinfo.cfg" ]
then
echo '<<<fileinfo:sep(124)>>>'
@@ -357,7 +373,9 @@ then
do
# only work on lines containing files, skip e.g. comments and blank lines
if [ ${file:0:1} = "/" ]; then
+ file="$(replace_datevariable $file)"
ls $file > /dev/null 2>&1
+
if [ $? -eq 0 ]; then
echo $file | \
/usr/bin/perl -e '