Skip to content

Instantly share code, notes, and snippets.

@jpylypiw
Last active July 2, 2024 18:01
Show Gist options
  • Save jpylypiw/f55352de920b75cccbe6e50c6ad4b4cd to your computer and use it in GitHub Desktop.
Save jpylypiw/f55352de920b75cccbe6e50c6ad4b4cd to your computer and use it in GitHub Desktop.
Check_MK Agent Update Script
#!/bin/bash
# this script works with the latest version of Check_MK. Tested using Raw 1.6.0b7 on Debian and RedHat.
# be sure that curl is installed!
# apt install -y curl
# yum install -y curl
# use the script with these steps:
# curl -Os -H 'Cache-Control: no-cache' https://gist.githubusercontent.com/jpylypiw/f55352de920b75cccbe6e50c6ad4b4cd/raw/update_check_mk_agent.sh
# bash update_check_mk_agent.sh -h myservername -i/s myinstance/mysite [-p http/https] [-d /usr/lib/check_mk_agent/plugins]
# Example:
# (sudo) bash update_check_mk_agent.sh -h test.example.com -s test -p http -d /usr/local/lib/check_mk_agent/plugins
# (sudo) bash update_check_mk_agent.sh -h test.example.com -i test
CHECK_MK_HOST="myservername"
CHECK_MK_INSTANCE="myinstance"
CHECK_MK_PROTOCOL="https"
CHECK_MK_PLUGIN_DIR="/usr/lib/check_mk_agent/plugins"
if [ "$EUID" -ne 0 ]; then
echo "This script must be run as root. Try again using sudo or log in as root user."
exit 1
fi
while getopts "h:i:p:d:s:" opt; do
case ${opt} in
h)
CHECK_MK_HOST=$OPTARG
;;
i)
CHECK_MK_INSTANCE=$OPTARG
;;
s)
CHECK_MK_INSTANCE=$OPTARG
;;
p)
CHECK_MK_PROTOCOL=$OPTARG
;;
d)
CHECK_MK_PLUGIN_DIR=$OPTARG
;;
*)
echo "Usage: cmd -h -i [-p] [-d]"
;;
esac
done
OS=
if grep 'Debian' /etc/issue >/dev/null 2>&1; then
OS=debian
fi
if grep 'Debian' /etc/os-release >/dev/null 2>&1; then
OS=debian
fi
if grep 'Ubuntu' /etc/issue >/dev/null 2>&1; then
OS=debian
fi
if grep 'ubuntu' /etc/os-release >/dev/null 2>&1; then
OS=debian
fi
if grep 'CentOS' /etc/issue >/dev/null 2>&1; then
OS=rhel
fi
if grep 'CentOS' /etc/os-release >/dev/null 2>&1; then
OS=rhel
fi
if grep 'Red' /etc/issue >/dev/null 2>&1; then
OS=rhel
fi
if [ ! $OS ]; then
echo "Could not detect OS"
exit 1
fi
echo "OS: $OS"
if [ "$OS" == "debian" ]; then
echo "Downloading Agent Package"
FILENAME=$(curl -s "$CHECK_MK_PROTOCOL"://"$CHECK_MK_HOST"/"$CHECK_MK_INSTANCE"/check_mk/agents/ | grep -oP "check-mk-agent\w[a-z\-\_0-9\.]+" | head -1)
URL=$CHECK_MK_PROTOCOL"://$CHECK_MK_HOST/$CHECK_MK_INSTANCE/check_mk/agents/"$FILENAME
curl -Os "$URL"
dpkg -i "$FILENAME"
rm "$FILENAME"
cd "$CHECK_MK_PLUGIN_DIR" || exit 1
echo "updating plugins"
for filename in $(find . -type f | cut -c 3-); do
echo "downloading $filename from check_mk server"
curl -sO "$CHECK_MK_PROTOCOL"://"$CHECK_MK_HOST"/"$CHECK_MK_INSTANCE"/check_mk/agents/plugins/"$filename"
chmod +x "$filename"
done
fi
if [ "$OS" == "rhel" ]; then
echo "Downloading Agent Package"
FILENAME=$(curl -s "$CHECK_MK_PROTOCOL"://"$CHECK_MK_HOST"/"$CHECK_MK_INSTANCE"/check_mk/agents/ | grep -oP "check-mk-agent\-\w[a-z\-0-9\.]+" | head -1)
URL=$CHECK_MK_PROTOCOL"://$CHECK_MK_HOST/$CHECK_MK_INSTANCE/check_mk/agents/"$FILENAME
curl -Os "$URL"
rpm --install --quiet --replacefiles "$FILENAME"
rm "$FILENAME"
cd "$CHECK_MK_PLUGIN_DIR" || exit 1
echo "updating plugins"
for filename in $(find . -type f | cut -c 3-); do
echo "downloading $filename from check_mk server"
curl -sO "$CHECK_MK_PROTOCOL"://"$CHECK_MK_HOST"/"$CHECK_MK_INSTANCE"/check_mk/agents/plugins/"$filename"
chmod +x "$filename"
done
fi
echo 'Complete'
@tps27
Copy link

tps27 commented Jan 7, 2019

Hi,

I am using this script for a Red Hat distro, and it keeps erroring out with

:: Downloading Agent Package
curl: Remote file name has no length!
curl: try 'curl --help' or 'curl --manual' for more information
rpm: no packages given for install
rm: missing operand
Try 'rm --help' for more information.
chmod: cannot access ‘*’: No such file or directory
:: Complete

Any suggestions as to why this is the case?

@jpylypiw
Copy link
Author

Hi there! I changed the script so you won't have any issues any more.
You need to have the latest version of Check_MK installed to run the script. The latest version does not need xinetd or gdebi anymore.
For a command line arguments example look at the comment at the first lines.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment