Munin Automated Script for Ubuntu


#!/bin/bash

Variables & Paths
DIR_NAME_BASE=my_base_dir
TGTENV=dev
MUNIN_HTTP_USERNAME=administrator
MUNIN_HTTP_PASSWORD=mypassword
DOMAIN_NAME=somecompany.com
MUNIN_SERVER=${TGTENV}-munin.${DOMAIN_NAME}
SERVER_ADMIN=”administrator@$DOMAIN_NAME”

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

Make Directories
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

Database Dir
mkdir -p $dbdir
mv /var/lib/munin/* $dbdir
rm -Rf /var/lib/munin/
ln -s $dbdir /var/lib/munin
chown -R munin.munin $dbdir

HTML Dir
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

LOG Dir
mkdir -p $logdir
mv /var/log/munin/* $logdir
rm -Rf /var/log/munin
ln -s $logdir /var/log/munin
chown -R munin.munin $logdir

RUN Dir
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

Re-Starting Apache Service
/etc/init.d/apache2 restart

Setup Cron Jobs(Daily @ 11PM)
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 –

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s