Search This Blog

Tuesday, January 26, 2021

Check wget output to trigger check_mk

I need to check wget output for status then let check_mk send alert.
#!/bin/bash

mkdir -p /temp_folder/wget_output/log

# define the search string and file
STR='200 OK'
FILE=/temp_folder/wget_output/check_api.wget

# clean the file everytime runs it.
rm $FILE
# output stdout & stderr to $FILE, note the wget running out goes to stderr by default. That is why I put '2>&1'
/usr/bin/wget https://milliondollarserver.com/api/api1234 > $FILE 2>&1

if grep -q "$STR" "$FILE"; then
  echo "Return: $STR" >> /temp_folder/wget_output/logg/`basename $0 .sh`.log
else
  echo 'Error!' >> /temp_folder/wget_output/log/`basename $0 .sh`.err
  echo '----------------------------------' >> /temp_folder/wget_output/log/`basename $0 .sh`.err
  cat $FILE >> /temp_folder/wget_output/log/`basename $0 .sh`.err
  echo '----------------------------------' >> /temp_folder/wget_output/log/`basename $0 .sh`.err
fi
And my check_mk configuration /etc/check_mk/logwatch.cfg:
# Check API status
/temp_folder/wget_output/log/*err
 W Error *
Update Version 2: switch temp file to variable.
#!/bin/bash

# define the search string
STR='200 OK'
# output stdout & stderr to $RESULT, note the wget running out goes to stderr by default. That is why I put '2>&1'
# wget -O - will put download file to stdout then goes to $RESULT as well.
RESULT=$( /usr/bin/wget -O - https://milliondollarserver.com/api/api1234 -nc 2>&1 )

if grep -q "$STR" <<< "$RESULT"; then
  echo `date` "Return: $STR" >> /temp_folder/wget_output/logg/`basename $0 .sh`.log
else
  # log Error in the stand log file as well.
  echo 'Error!' `date` >> /temp_folder/wget_output/logg/`basename $0 .sh`.log
  echo 'Error!' `date` >> /temp_folder/wget_output/log/`basename $0 .sh`.err
  echo '----------------------------------' >> /temp_folder/wget_output/log/`basename $0 .sh`.err
  echo $RESULT >> /temp_folder/wget_output/log/`basename $0 .sh`.err
  echo '----------------------------------' >> /temp_folder/wget_output/log/`basename $0 .sh`.err
fi

No comments:

Post a Comment