Skip to content

Instantly share code, notes, and snippets.

@jeanfrancis
Created July 17, 2016 21:41
Show Gist options
  • Save jeanfrancis/cee39d6613fdd3e3c726b9e170c19011 to your computer and use it in GitHub Desktop.
Save jeanfrancis/cee39d6613fdd3e3c726b9e170c19011 to your computer and use it in GitHub Desktop.
Unattended Installation / Bootstrap Script for Installing Munin Server on Ubuntu 12.04. You can simply pass this into an AWS EC2 box and you're 99% of the way done with setting up a Munin Server and an Apache/HTTP WebUI
#!/bin/bash
################################################################################################
################################################################################################
################################################################################################
##
## Basic Ubuntu 12.04 Unattended Bootstrap for Munin
##
## Author: Allan Parsons ([email protected])
##
## Adapted from: https://library.linode.com/server-monitoring/munin/ubuntu-12.04-precise-pangolin
##
################################################################################################
################################################################################################
################################################################################################
################################################################################################
## Variables
################################################################################################
DIR_NAME_BASE=my_base_dir ## /my_base_dir/
TGTENV=dev ## used to prefix some stuff
MUNIN_HTTP_USERNAME=administrator ## used for htpasswd
MUNIN_HTTP_PASSWORD=mypassword ## used for htpasswd
DOMAIN_NAME=somecompany.com ## used for apache setup
MUNIN_SERVER=${TGTENV}-munin.${DOMAIN_NAME} ## used for apache setup
SERVER_ADMIN="administrator@$DOMAIN_NAME" ## used for apache setup
################################################################################################
## Install Dependencies
################################################################################################
apt-get -y update
apt-get -y upgrade
apt-get -y install build-essential make
apt-get -y install htop
apt-get -y install munin munin-node
apt-get -y install apache2 apache2-doc apache2-utils
################################################################################################
## MUNIN: Make Directories
##
## link the default dirs back to what we just created so we don't have to mess with much
## this makes it easier to pickup and move (just pickup and move /BASEDIR and /etc/munin)
################################################################################################
dbdir=/${DIR_NAME_BASE}/munin/db
htmldir=/${DIR_NAME_BASE}/munin/www
logdir=/${DIR_NAME_BASE}/munin/log
rundir=/${DIR_NAME_BASE}/munin/run
templdir=/${DIR_NAME_BASE}/munin/templates
## dbdir
mkdir -p $dbdir
mv /var/lib/munin/* $dbdir
rm -Rf /var/lib/munin/
ln -s $dbdir /var/lib/munin
chown -R munin.munin $dbdir
## htmldir
mkdir -p $htmldir
mv /var/cache/munin/www/* $htmldir
rm -Rf /var/cache/munin/www
ln -s $htmldir /var/cache/munin/www
chown -R munin.munin $htmldir
## logdir
mkdir -p $logdir
mv /var/log/munin/* $logdir
rm -Rf /var/log/munin
ln -s $logdir /var/log/munin
chown -R munin.munin $logdir
## rundir
mkdir -p $rundir
mv /var/run/munin/* $rundir
rm -Rf /var/run/munin
ln -s $rundir /var/run/munin
chown -R munin.munin $rundir
## templates
mkdir -p $templdir
mv /etc/munin/templates/* $templdir
rm -Rf /etc/munin/templates
ln -s $templdir /etc/munin/templates
chown munin.munin $templdir
################################################################################################
## Configure Apache for Munin
################################################################################################
touch /etc/apache2/sites-available/${MUNIN_SERVER}
cat>/etc/apache2/sites-available/${MUNIN_SERVER}<<EOF
<VirtualHost *:80>
ServerAdmin ${SERVER_ADMIN}
ServerName ${MUNIN_SERVER}
Alias /munin /var/cache/munin/www
<Directory /var/cache/munin/www>
Order allow,deny
Allow from all
Options FollowSymLinks
AllowOverride None
AuthUserFile /etc/munin/munin-htpasswd
AuthName ${MUNIN_HTTP_USERNAME}
AuthType Basic
require valid-user
RewriteEngine On
RewriteBase /munin/
<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault M310
</IfModule>
</Directory>
LogLevel notice
CustomLog /var/log/apache2/access.log combined
ErrorLog /var/log/apache2/error.log
ServerSignature On
</VirtualHost>
EOF
if [ -f /etc/munin/munin-htpasswd ]; then
rm -f /etc/munin/munin-htpasswd
fi
htpasswd -b -c /etc/munin/munin-htpasswd ${MUNIN_HTTP_USERNAME} ${MUNIN_HTTP_PASSWORD}
a2enmod rewrite
a2ensite ${MUNIN_SERVER}
a2dissite default
/etc/init.d/apache2 restart
################################################################################################
## Setup Cron Jobs to Take Backups of your HTTP Config and Munin Config (daily @ 11p UTC)
################################################################################################
mkdir -p /${DIR_NAME_BASE}/munin/backup/
crontab -l | { cat; echo "0 23 * * * cp /etc/munin/munin.conf /${DIR_NAME_BASE}/munin/backup/munin.conf.\`date --date=\"-1 days\" +%Y%m%d\`.bak > /dev/null 2>&1 &"; } | crontab -
crontab -l | { cat; echo "0 23 * * * cp /etc/apache2/sites-available/${MUNIN_SERVER} /${DIR_NAME_BASE}/munin/backup/${MUNIN_SERVER}.\`date --date=\"-1 days\" +%Y%m%d\`.bak > /dev/null 2>&1 &"; } | crontab -
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment