munin.sh 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #!/bin/bash
  2. NODES=${NODES:-}
  3. SNMP_NODES=${SNMP_NODES:-}
  4. SSH_NODES=${SSH_NODES:-}
  5. MUNIN_USERS=${MUNIN_USERS:-${MUNIN_USER:-user}}
  6. MUNIN_PASSWORDS=${MUNIN_PASSWORDS:-${MUNIN_PASSWORD:-password}}
  7. MAIL_CONF_PATH='/munin/db/.mailrc'
  8. SMTP_USE_TLS=${SMTP_USE_TLS:-false}
  9. SMTP_ALWAYS_SEND=${SMTP_ALWAYS_SEND:-true}
  10. SMTP_MESSAGE_DEFAULT='[${var:group};${var:host}] -> ${var:graph_title} -> warnings: ${loop<,>:wfields ${var:label}=${var:value}} / criticals: ${loop<,>:cfields ${var:label}=${var:value}}'
  11. SMTP_MESSAGE="${SMTP_MESSAGE:-$SMTP_MESSAGE_DEFAULT}"
  12. mkdir -p /munin/{db,log,www/cache}
  13. [ -a /munin/db/cgi-tmp ] || ln -s ../www/cache /munin/db/cgi-tmp
  14. [ -a /var/lib/munin/cgi-tmp ] || ln -s /munin/www/cache /var/lib/munin/cgi-tmp
  15. [ -a /var/log/nginx ] || ln -s /tmp /var/log/nginx
  16. truncate -s 0 "${MAIL_CONF_PATH}"
  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. # generate node list
  44. for NODE in $NODES
  45. do
  46. NAME=`echo $NODE | cut -d ":" -f1`
  47. HOST=`echo $NODE | cut -d ":" -f2`
  48. PORT=`echo $NODE | cut -d ":" -f3`
  49. if [ ${#PORT} -eq 0 ]; then
  50. PORT=4949
  51. fi
  52. if ! grep -q "'^$HOST$'" /etc/munin/munin.conf ; then
  53. cat << EOF >> /etc/munin/munin.conf
  54. [$NAME]
  55. address $HOST
  56. use_node_name yes
  57. port $PORT
  58. EOF
  59. echo "Added node '$NAME' '$HOST'"
  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. echo "Added SNMP node '$NAME' '$HOST'"
  79. fi
  80. done
  81. for SSH_NODE in $SSH_NODES
  82. do
  83. NAME=`echo $SSH_NODE | cut -d ":" -f1`
  84. HOST=`echo $SSH_NODE | cut -d ":" -f2`
  85. PORT=`echo $SSH_NODE | cut -d ":" -f3`
  86. if [ ${#PORT} -eq 0 ]; then
  87. PORT=4949
  88. fi
  89. if ! grep -q "'^$HOST$'" /etc/munin/munin.conf ; then
  90. cat << EOF >> /etc/munin/munin.conf
  91. [$NAME]
  92. address ssh://$HOST/usr/bin/nc localhost 4949
  93. use_node_name yes
  94. port $PORT
  95. EOF
  96. echo "Added SSH node '$NAME' '$HOST'"
  97. fi
  98. done
  99. # placeholder html to prevent permission error
  100. if [ ! -e /munin/www/index.html ]; then
  101. cat << EOF > /munin/www/index.html
  102. <html>
  103. <head>
  104. <title>Munin</title>
  105. </head>
  106. <body>
  107. Munin has not run yet. Please try again in a few moments.
  108. </body>
  109. </html>
  110. EOF
  111. fi
  112. # start rsyslogd
  113. truncate -s 0 /tmp/rsyslogd.pid
  114. #/usr/sbin/rsyslogd -i /tmp/rsyslogd.pid
  115. # start munin updates
  116. truncate -s 0 /munin/log/munin-update.log
  117. /usr/local/bin/supercronic -split-logs /etc/munin/munin.cron & cron_pid=$!
  118. echo "Using the following munin nodes:"
  119. echo $NODES
  120. echo "(ssh) $SSH_NODES"
  121. echo "(snmp) $SNMP_NODES"
  122. # Pregenerate data
  123. echo "First run"
  124. munin-cron
  125. echo "Start data servers"
  126. # start spawn-cgi to enable CGI interface with munin (dynamix graph generation)
  127. spawn-fcgi -n -p 9000 /usr/lib/munin/cgi/munin-cgi-graph & graph_pid=$!
  128. spawn-fcgi -n -p 9001 /usr/lib/munin/cgi/munin-cgi-html & html_pid=$1
  129. # start nginx
  130. /usr/sbin/nginx
  131. # show logs
  132. echo "Tailing syslog and munin-update log..."
  133. tail -F /munin/log/munin-update.log & pid=$!
  134. echo "tail -F running in $pid"
  135. sleep 1
  136. trap "echo 'stopping processes' ; kill $pid $cron_pid $graph_pid $html_pid $(cat /tmp/nginx.pid) $(cat /tmp/rsyslogd.pid)" SIGTERM SIGINT
  137. echo "Waiting for signal SIGINT/SIGTERM"
  138. wait