diff options
Diffstat (limited to 'files/munin.sh')
-rwxr-xr-x | files/munin.sh | 164 |
1 files changed, 164 insertions, 0 deletions
diff --git a/files/munin.sh b/files/munin.sh new file mode 100755 index 0000000..63f5732 --- /dev/null +++ b/files/munin.sh @@ -0,0 +1,164 @@ +#!/bin/bash + +NODES=${NODES:-} +SNMP_NODES=${SNMP_NODES:-} +SSH_NODES=${SSH_NODES:-} +MUNIN_USERS=${MUNIN_USERS:-${MUNIN_USER:-user}} +MUNIN_PASSWORDS=${MUNIN_PASSWORDS:-${MUNIN_PASSWORD:-password}} +MAIL_CONF_PATH='/munin/db/.mailrc' +SMTP_USE_TLS=${SMTP_USE_TLS:-false} +SMTP_ALWAYS_SEND=${SMTP_ALWAYS_SEND:-true} +SMTP_MESSAGE_DEFAULT='[${var:group};${var:host}] -> ${var:graph_title} -> warnings: ${loop<,>:wfields ${var:label}=${var:value}} / criticals: ${loop<,>:cfields ${var:label}=${var:value}}' +SMTP_MESSAGE="${SMTP_MESSAGE:-$SMTP_MESSAGE_DEFAULT}" + + +mkdir -p /munin/{db,log,www/cache} +[ -a /munin/db/cgi-tmp ] || ln -s ../www/cache /munin/db/cgi-tmp +[ -a /var/lib/munin/cgi-tmp ] || ln -s /munin/www/cache /var/lib/munin/cgi-tmp +[ -a /var/log/nginx ] || ln -s /tmp /var/log/nginx + + +truncate -s 0 "${MAIL_CONF_PATH}" + +if [ "${SMTP_USE_TLS}" = true ] ; then + cat >> "${MAIL_CONF_PATH}" <<EOF +set smtp-use-starttls +set ssl-verify=ignore +EOF +fi + +if [ -n "${SMTP_HOST}" -a -n "${SMTP_PORT}" ] ; then + cat >> "${MAIL_CONF_PATH}" <<EOF +set smtp=smtp://${SMTP_HOST}:${SMTP_PORT} +EOF +fi + +if [ -n "${SMTP_USERNAME}" -a -n "${SMTP_PASSWORD}" ] ; then + cat >> "${MAIL_CONF_PATH}" <<EOF +set smtp-auth=login +set smtp-auth-user=${SMTP_USERNAME} +set smtp-auth-password=${SMTP_PASSWORD} +EOF +fi + +grep -q 'contact.mail' /etc/munin/munin.conf; rc=$? +if [ $rc -ne 0 -a -n "${ALERT_RECIPIENT}" -a -n "${ALERT_SENDER}" ] ; then + echo "Setup alert email from ${ALERT_SENDER} to ${ALERT_RECIPIENT}" + echo "contact.mail.command mail -r ${ALERT_SENDER} -s '${SMTP_MESSAGE}' ${ALERT_RECIPIENT}" >> /etc/munin/munin.conf + if [ "${SMTP_ALWAYS_SEND}" = true ] ; then + echo 'contact.mail.always_send warning critical' >> /etc/munin/munin.conf + fi +fi + +# generate node list +for NODE in $NODES +do + NAME=`echo $NODE | cut -d ":" -f1` + HOST=`echo $NODE | cut -d ":" -f2` + PORT=`echo $NODE | cut -d ":" -f3` + if [ ${#PORT} -eq 0 ]; then + PORT=4949 + fi + if ! grep -q "'^$HOST$'" /etc/munin/munin.conf ; then + cat << EOF >> /etc/munin/munin.conf +[$NAME] + address $HOST + use_node_name yes + port $PORT + +EOF + echo "Added node '$NAME' '$HOST'" + fi +done + +# generate node list +for NODE in $SNMP_NODES +do + NAME=`echo $NODE | cut -d ":" -f1` + HOST=`echo $NODE | cut -d ":" -f2` + PORT=`echo $NODE | cut -d ":" -f3` + if [ ${#PORT} -eq 0 ]; then + PORT=4949 + fi + if ! grep -q "'^$HOST$'" /etc/munin/munin.conf ; then + cat << EOF >> /etc/munin/munin.conf +[$NAME] + address $HOST + use_node_name no + port $PORT + +EOF + echo "Added SNMP node '$NAME' '$HOST'" + fi +done + +for SSH_NODE in $SSH_NODES +do + NAME=`echo $SSH_NODE | cut -d ":" -f1` + HOST=`echo $SSH_NODE | cut -d ":" -f2` + PORT=`echo $SSH_NODE | cut -d ":" -f3` + if [ ${#PORT} -eq 0 ]; then + PORT=4949 + fi + if ! grep -q "'^$HOST$'" /etc/munin/munin.conf ; then + cat << EOF >> /etc/munin/munin.conf +[$NAME] + address ssh://$HOST/usr/bin/nc localhost 4949 + use_node_name yes + port $PORT + +EOF + echo "Added SSH node '$NAME' '$HOST'" + fi +done + + +# placeholder html to prevent permission error +if [ ! -e /munin/www/index.html ]; then +cat << EOF > /munin/www/index.html +<html> +<head> + <title>Munin</title> +</head> +<body> +Munin has not run yet. Please try again in a few moments. +</body> +</html> +EOF +fi + +# start rsyslogd +truncate -s 0 /tmp/rsyslogd.pid +#/usr/sbin/rsyslogd -i /tmp/rsyslogd.pid + +# start munin updates +truncate -s 0 /munin/log/munin-update.log +/usr/local/bin/supercronic -split-logs /etc/munin/munin.cron & cron_pid=$! + + +echo "Using the following munin nodes:" +echo $NODES +echo "(ssh) $SSH_NODES" +echo "(snmp) $SNMP_NODES" + +# Pregenerate data +echo "First run" +munin-cron + +echo "Start data servers" +# start spawn-cgi to enable CGI interface with munin (dynamix graph generation) +spawn-fcgi -n -p 9000 /usr/lib/munin/cgi/munin-cgi-graph & graph_pid=$! +spawn-fcgi -n -p 9001 /usr/lib/munin/cgi/munin-cgi-html & html_pid=$1 +# start nginx +/usr/sbin/nginx +# show logs +echo "Tailing syslog and munin-update log..." +tail -F /munin/log/munin-update.log & pid=$! +echo "tail -F running in $pid" + +sleep 1 + +trap "echo 'stopping processes' ; kill $pid $cron_pid $graph_pid $html_pid $(cat /tmp/nginx.pid) $(cat /tmp/rsyslogd.pid)" SIGTERM SIGINT + +echo "Waiting for signal SIGINT/SIGTERM" +wait |