summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--VERSION.txt1
-rwxr-xr-xlib/monitor/notificaton-service-master193
-rwxr-xr-xlib/monitor/services/service-notification181
3 files changed, 375 insertions, 0 deletions
diff --git a/VERSION.txt b/VERSION.txt
new file mode 100644
index 0000000..c3e1766
--- /dev/null
+++ b/VERSION.txt
@@ -0,0 +1 @@
+20190710
diff --git a/lib/monitor/notificaton-service-master b/lib/monitor/notificaton-service-master
new file mode 100755
index 0000000..1c9d3f3
--- /dev/null
+++ b/lib/monitor/notificaton-service-master
@@ -0,0 +1,193 @@
+#!/bin/sh
+
+# Copyright (C) 2020 Daniel Baumann <daniel.baumann@open-infrastructure.net>
+# 2020 David Kunz <david.kunz@dknet.ch>
+#
+# SPDX-License-Identifier: GPL-3.0+
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+set -e
+
+PROJECT="open-infrastructure"
+PROGRAM="monitor"
+COMMAND="$(basename ${0})"
+
+IP_ADDRESS="$(echo ${SSH_CLIENT} | awk '{print $1}')"
+SYSTEM="$(kdig -x ${IP_ADDRESS} +short | sed -e 's|.$||')"
+IPV4_ADDRESS="$(kdig +short A ${SYSTEM})"
+IPV6_ADDRESS="$(kdig +short AAAA ${SYSTEM})"
+
+CONFIG="/etc/icinga2/objects.d"
+
+OPTIONS="$(echo ${1} | awk '{print $1}')"
+SERVICES="$(echo ${1} | cut -d' ' -f2-)"
+
+if sudo sh -c "[ ! -d ${CONFIG}/${SYSTEM} ]"
+then
+ echo "Host '${SYSTEM}' exist not."
+ echo "Do 'monitor system --add'"
+ exit 1
+fi
+
+if [ -z "${1}" ]
+then
+ echo "Usage: ${PROGRAM} COMMAND [OPTIONS]" >&2
+ echo "Usage: ${PROGRAM} COMMAND1,COMMAND2,... [COMMON_OPTIONS]" >&2
+ exit 1
+fi
+
+Parameters ()
+{
+ GETOPT_LONGOPTIONS="add,remove,list,show,list-available,"
+ GETOPT_OPTIONS="a,r,l,s,v,"
+
+ PARAMETERS="$(getopt --longoptions ${GETOPT_LONGOPTIONS} --name=${COMMAND} --options ${GETOPT_OPTIONS} --shell sh -- ${@})"
+
+ if [ "${?}" != "0" ]
+ then
+ echo "'${COMMAND}': getopt exit" >&2
+ exit 1
+ fi
+
+ eval set -- "${PARAMETERS}"
+
+ while true
+ do
+ case "${1}" in
+ -a|--add)
+ ACTION="add"
+ shift 1
+ ;;
+
+ -r|--remove)
+ ACTION="remove"
+ shift 1
+ ;;
+
+ -l|--list)
+ ACTION="list"
+ shift 1
+ ;;
+
+ -s|--show)
+ ACTION="show"
+ shift 1
+ ;;
+
+
+ -v|--list-available)
+ ACTION="list-available"
+ shift 1
+ ;;
+
+ --)
+ shift 1
+ break
+ ;;
+
+ *)
+ echo "'${COMMAND}': getopt error" >&2
+ exit 1
+ ;;
+ esac
+ done
+}
+
+Parameters "${@}"
+
+Service ()
+{
+for SERVICE in $(echo ${SERVICES} | sed -e 's|,| |g')
+do
+
+ if sudo sh -c "[ ! -e /usr/lib/${PROJECT}/${PROGRAM}/services/${SERVICE} ]"
+ then
+ echo "'${SERVICE}': no such ${PROGRAM} command" >&2
+ exit 1
+ fi
+
+ # Notification
+ if echo "${OPTIONS}" | grep -qs "\--no-notification"
+ then
+ OPTIONS="$(echo "${OPTIONS}" | sed -e 's|--no-notification||')"
+ else
+ case "${SERVICE}" in
+ $(find /usr/lib/${PROJECT}/${PROGRAM}/services -maxdepth 1 -type f | awk -F/ '{ print $NF }'))
+ USER="${SUDO_USER:-${USER}}"
+ DATE="$(date +%Y-%m-%d\ %H:%M:%S)"
+ HOST="$(hostname -f 2> /dev/null || hostname)"
+
+ # logfile
+ echo "${DATE} ${HOST} ${USER} ${PROGRAM} ${SERVICE} ${OPTIONS}" >> "/var/log/${PROJECT}/${PROGRAM}.log"
+
+ # irc
+ if [ -e /usr/bin/irk ] && [ -e "/etc/${PROJECT}/${PROGRAM}.conf" ]
+ then
+ . "/etc/${PROJECT}/${PROGRAM}.conf"
+
+ if [ -n "${IRK_TARGETS}" ]
+ then
+ for TARGET in ${IRK_TARGETS}
+ do
+ irk ${TARGET} "\x0300${USER}\x03@\x0312${HOST}:\x03 \x0303${PROGRAM}\x03 \x0307${SERVICE}\x03 ${OPTIONS}"
+ done
+ fi
+ fi
+ ;;
+ esac
+ fi
+
+ # Run
+ "/usr/lib/${PROJECT}/${PROGRAM}/services/${SERVICE}" "${OPTIONS}"
+done
+}
+
+Notification_list ()
+{
+ SERVICE_LIST="$(sudo sh -c "find ${CONFIG}/${SYSTEM}/*.conf" | awk -F/ '{ print $NF }' | sed -e 's|.conf||')"
+ if [ ! ${SERVICE_LIST} ]
+ then
+ echo "No service available"
+ exit 1
+ fi
+ sudo sh -c "find ${CONFIG}/${SYSTEM}/*.conf" | awk -F/ '{ print $NF }' | sed -e 's|.conf$||'
+}
+
+Notification_list_available ()
+{
+ sudo sh -c "find /usr/lib/${PROJECT}/${PROGRAM}/services/*.conf.in" | awk -F/ '{ print $NF }' | sed -e 's|.conf.in$||'
+}
+
+case "${ACTION}" in
+ add)
+ Service
+ ;;
+
+ remove)
+ Service
+ ;;
+
+ show)
+ Service
+ ;;
+
+ list)
+ Service_list
+ ;;
+
+ list-available)
+ Service_list_available
+ ;;
+esac
diff --git a/lib/monitor/services/service-notification b/lib/monitor/services/service-notification
new file mode 100755
index 0000000..529f741
--- /dev/null
+++ b/lib/monitor/services/service-notification
@@ -0,0 +1,181 @@
+#!/bin/sh
+
+# Copyright (C) 2014-2019 David Kunz <david.kunz@dknet.ch>
+#
+# SPDX-License-Identifier: GPL-3.0+
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+set -e
+
+PROJECT="open-infrastructure"
+PROGRAM="monitor"
+COMMAND="$(basename ${0})"
+
+IP_ADDRESS="$(echo ${SSH_CLIENT} | awk '{print $1}')"
+SYSTEM="$(kdig -x ${IP_ADDRESS} +short | sed -e 's|.$||')"
+IPV4_ADDRESS="$(kdig +short A ${SYSTEM})"
+
+CONFIG="/etc/icinga2/objects.d"
+SERVICE="$(basename ${0})"
+
+Parameters ()
+{
+ GETOPT_LONGOPTIONS="add,remove,show,"
+ GETOPT_OPTIONS="a,r,s,"
+
+ PARAMETERS="$(getopt --longoptions ${GETOPT_LONGOPTIONS} --name=${COMMAND} --options ${GETOPT_OPTIONS} --shell sh -- ${@})"
+
+ if [ "${?}" != "0" ]
+ then
+ echo "'${COMMAND}': getopt exit" >&2
+ exit 1
+ fi
+
+ eval set -- "${PARAMETERS}"
+
+ while true
+ do
+ case "${1}" in
+ -a|--add)
+ ACTION="add"
+ shift 1
+ ;;
+
+ -r|--remove)
+ ACTION="remove"
+ shift 1
+ ;;
+
+ -s|--show)
+ ACTION="show"
+ shift 1
+ ;;
+
+ --)
+ shift 1
+ break
+ ;;
+
+ *)
+ echo "'${COMMAND}': getopt error" >&2
+ exit 1
+ ;;
+ esac
+ done
+}
+
+Parameters "${@}"
+
+Check_service_exist ()
+{
+ if sudo sh -c "[ ! -e "${CONFIG}/${SYSTEM}/${SERVICE}.conf" ]"
+ then
+ echo "Service do not exist."
+ exit 1
+ fi
+}
+
+Service_add ()
+{
+if [ -z "${IPV4_ADDRESS}" ]
+then
+ echo "Error: No IPv4 Address found."
+ exit 1
+fi
+
+if sudo sh -c "[ -e "${CONFIG}/${SYSTEM}/${SERVICE}.conf" ]"
+then
+ echo "Service exist."
+ exit 1
+fi
+
+echo "#### Creating ${SERVICE}.conf for host ${SYSTEM} ####"
+
+sudo sh -c "sed -e 's|@SYSTEM@|${SYSTEM}|g' \
+ /usr/lib/${PROJECT}/${PROGRAM}/services/${SERVICE}.conf.in > ${CONFIG}/${SYSTEM}/${SERVICE}.conf"
+}
+
+Service_rm ()
+{
+echo "Remove ${SERVICE}.conf from ${SYSTEM}."
+
+sudo sh -c "rm -f ${CONFIG}/${SYSTEM}/${SERVICE}.conf"
+}
+
+Service_show ()
+{
+ if sudo -sh -c "[ ! -e ${CONFIG}/${SYSTEM}/${SERVICE}.conf ]"
+ then
+ echo "Service '${SERVICE}' do not exist."
+ exit 1
+ fi
+
+ HOST_NAME="$(sudo sh -c "grep 'host_name' ${CONFIG}/${SYSTEM}/${SERVICE}.conf" | awk '{ print $3 }')"
+ CHECK_COMMAND="$(sudo sh -c "grep 'check_command' ${CONFIG}/${SYSTEM}/${SERVICE}.conf" | awk '{ print $3 }')"
+ MAX_CHECK_ATTEMPTS="$(sudo sh -c "grep 'max_check_attempts' ${CONFIG}/${SYSTEM}/${SERVICE}.conf" | awk '{ print $3 }')"
+ CHECK_INTERVAL="$(sudo sh -c "grep 'check_interval' ${CONFIG}/${SYSTEM}/${SERVICE}.conf" | awk '{ print $3 }')"
+ RETRY_INTERVAL="$(sudo sh -c "grep 'retry_interval' ${CONFIG}/${SYSTEM}/${SERVICE}.conf" | awk '{ print $3 }')"
+ ENABLE_PERFDATA="$(sudo sh -c "grep 'enable_perfdata' ${CONFIG}/${SYSTEM}/${SERVICE}.conf" | awk '{ print $3 }')"
+
+cat << EOF
+Host: ${HOST_NAME}
+Check command: ${CHECK_COMMAND}
+Max check attempts: ${MAX_CHECK_ATTEMPTS}
+Check interval: ${CHECK_INTERVAL}
+Retry interval: ${RETRY_INTERVAL}
+Enable Perfdata: ${ENABLE_PERFDATA}
+EOF
+}
+
+Config_check ()
+{
+echo -n "Checking icinga2 configuration..."
+
+set +e
+sudo icinga2 daemon -C > /dev/null 2>&1
+ERROR="${?}"
+set -e
+
+case "${ERROR}" in
+ 0)
+ echo " successfull."
+
+ echo -n "Reloading icinga2 service..."
+ sudo service icinga2 reload
+ echo " done."
+ ;;
+
+ *)
+ echo " failed."
+ ;;
+esac
+}
+
+case "${ACTION}" in
+ add)
+ Service_add
+ Config_check
+ ;;
+ remove)
+ Service_rm
+ Config_check
+ ;;
+ show)
+ Service_show
+ ;;
+ *)
+ echo "Error"
+ ;;
+esac