start-munin.sh 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #!/bin/bash
  2. NODES=${NODES:-}
  3. SNMP_NODES=${SNMP_NODES:-}
  4. MUNIN_USER=${MUNIN_USER:-user}
  5. MUNIN_PASSWORD=${MUNIN_PASSWORD:-password}
  6. MAIL_CONF_PATH='/var/lib/munin/.mailrc'
  7. SMTP_USE_TLS=${SMTP_USE_TLS:-false}
  8. SMTP_ALWAYS_SEND=${SMTP_ALWAYS_SEND:-true}
  9. SMTP_MESSAGE_DEFAULT='[${var:group};${var:host}] -> ${var:graph_title} -> warnings: ${loop<,>:wfields ${var:label}=${var:value}} / criticals: ${loop<,>:cfields ${var:label}=${var:value}}'
  10. SMTP_MESSAGE="${SMTP_MESSAGE:-$SMTP_MESSAGE_DEFAULT}"
  11. truncate -s 0 "${MAIL_CONF_PATH}"
  12. # set volume ownerships
  13. chown -R munin:munin /var/log/munin
  14. chown -R munin:munin /var/lib/munin
  15. chown -R munin:munin /var/run/munin
  16. chown -R munin:munin /var/cache/munin
  17. if [ "${SMTP_USE_TLS}" = true ] ; then
  18. cat >> "${MAIL_CONF_PATH}" <<EOF
  19. set smtp-use-starttls
  20. set ssl-verify=ignore
  21. EOF
  22. fi
  23. if [ -n "${SMTP_HOST}" -a -n "${SMTP_PORT}" ] ; then
  24. cat >> "${MAIL_CONF_PATH}" <<EOF
  25. set smtp=smtp://${SMTP_HOST}:${SMTP_PORT}
  26. EOF
  27. fi
  28. if [ -n "${SMTP_USERNAME}" -a -n "${SMTP_PASSWORD}" ] ; then
  29. cat >> "${MAIL_CONF_PATH}" <<EOF
  30. set smtp-auth=login
  31. set smtp-auth-user=${SMTP_USERNAME}
  32. set smtp-auth-password=${SMTP_PASSWORD}
  33. EOF
  34. fi
  35. grep -q 'contact.mail' /etc/munin/munin.conf; rc=$?
  36. if [ $rc -ne 0 -a -n "${ALERT_RECIPIENT}" -a -n "${ALERT_SENDER}" ] ; then
  37. echo "Setup alert email from ${ALERT_SENDER} to ${ALERT_RECIPIENT}"
  38. echo "contact.mail.command mail -r ${ALERT_SENDER} -s '${SMTP_MESSAGE}' ${ALERT_RECIPIENT}" >> /etc/munin/munin.conf
  39. if [ "${SMTP_ALWAYS_SEND}" = true ] ; then
  40. echo 'contact.mail.always_send warning critical' >> /etc/munin/munin.conf
  41. fi
  42. fi
  43. [ -e /etc/munin/htpasswd.users ] || htpasswd -b -c /etc/munin/htpasswd.users "$MUNIN_USER" "$MUNIN_PASSWORD"
  44. # generate node list
  45. for NODE in $NODES
  46. do
  47. NAME=`echo $NODE | cut -d ":" -f1`
  48. HOST=`echo $NODE | cut -d ":" -f2`
  49. PORT=`echo $NODE | cut -d ":" -f3`
  50. if [ ${#PORT} -eq 0 ]; then
  51. PORT=4949
  52. fi
  53. if ! grep -q $HOST /etc/munin/munin.conf ; then
  54. cat << EOF >> /etc/munin/munin.conf
  55. [$NAME]
  56. address $HOST
  57. use_node_name yes
  58. port $PORT
  59. EOF
  60. fi
  61. done
  62. # generate node list
  63. for NODE in $SNMP_NODES
  64. do
  65. NAME=`echo $NODE | cut -d ":" -f1`
  66. HOST=`echo $NODE | cut -d ":" -f2`
  67. PORT=`echo $NODE | cut -d ":" -f3`
  68. if [ ${#PORT} -eq 0 ]; then
  69. PORT=4949
  70. fi
  71. if ! grep -q $HOST /etc/munin/munin.conf ; then
  72. cat << EOF >> /etc/munin/munin.conf
  73. [$NAME]
  74. address $HOST
  75. use_node_name no
  76. port $PORT
  77. EOF
  78. fi
  79. done
  80. [ -d /var/cache/munin/www ] || mkdir /var/cache/munin/www
  81. # placeholder html to prevent permission error
  82. if [ ! -e /var/cache/munin/www/index.html ]; then
  83. cat << EOF > /var/cache/munin/www/index.html
  84. <html>
  85. <head>
  86. <title>Munin</title>
  87. </head>
  88. <body>
  89. Munin has not run yet. Please try again in a few moments.
  90. </body>
  91. </html>
  92. EOF
  93. chown munin:munin -R /var/cache/munin/www
  94. chmod g+w /var/cache/munin/www/index.html
  95. fi
  96. # start rsyslogd
  97. /usr/sbin/rsyslogd
  98. # start cron
  99. /usr/sbin/cron
  100. # start local munin-node
  101. /usr/sbin/munin-node
  102. echo "Using the following munin nodes:"
  103. echo $NODES
  104. # start spawn-cgi to enable CGI interface with munin (dynamix graph generation)
  105. spawn-fcgi -s /var/run/munin/fcgi-graph.sock -U munin -u munin -g munin /usr/lib/munin/cgi/munin-cgi-graph
  106. # start nginx
  107. /usr/sbin/nginx
  108. # show logs
  109. echo "Tailing /var/log/syslog..."
  110. tail -F /var/log/syslog /var/log/munin/munin-update.log & pid=$!
  111. echo "tail -F running in $pid"
  112. sleep 1
  113. trap "echo 'stopping processes' ; kill $pid $(cat /var/run/munin/munin-node.pid) $(cat /var/run/nginx.pid) $(cat /var/run/crond.pid) $(cat /var/run/rsyslogd.pid)" SIGTERM SIGINT
  114. echo "Waiting for signal SIGINT/SIGTERM"
  115. wait