Skip to content

Instantly share code, notes, and snippets.

@fr34k8
Forked from anonymous/cmk-add-host
Last active August 27, 2015 09:57
Show Gist options
  • Save fr34k8/9e0e69ccf52ce93da39f to your computer and use it in GitHub Desktop.
Save fr34k8/9e0e69ccf52ce93da39f to your computer and use it in GitHub Desktop.
Check MK add host script in bash
#!/bin/bash
site=$1; shift
hostname=$1; shift
mk_file="/opt/omd/sites/$site/etc/check_mk/conf.d/wato/hosts.mk"
wato_file="$(dirname $mk_file)/.wato"
## exit with code 1 if host exists
(
cat <<EOT
GET hosts
Columns: host_name
EOT
) | su - $site -c "lq" \
| grep -q $hostname
if [[ $? == 0 ]]; then
echo "$hostname already added"
exit 1
fi
host $hostname &> /dev/null
if [[ $? != 0 ]]; then
echo "$hostname failed to resolve"
exit 1
fi
## add host to base wato hosts.mk
cat <<EOT >> $mk_file
all_hosts += [ "$hostname|wato|/" + FOLDER_PATH + "/" ]
host_attributes.update({'$hostname': {}})
EOT
## update num_hosts var for wato
current_num_hosts=$( cat $wato_file | sed -e "s/^.*'num_hosts': //" -e "s/,.*$//")
new_num_hosts=$(( $current_num_hosts + 1 ))
sed -i $wato_file -e "s/'num_hosts': [0-9]\+/'num_hosts': $new_num_hosts/"
su - $site -c "cmk -II $hostname"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment