#!/bin/bash
#
# Univention Maintenance
#  maintenance script
#
# Copyright 2004-2014 Univention GmbH
#
# http://www.univention.de/
#
# All rights reserved.
#
# The source code of this program is made available
# under the terms of the GNU Affero General Public License version 3
# (GNU AGPL V3) as published by the Free Software Foundation.
#
# Binary versions of this program provided by Univention to you as
# well as other copyrighted, protected or trademarked materials like
# Logos, graphics, fonts, specific documentations and configurations,
# cryptographic keys etc. are subject to a license agreement between
# you and Univention and not subject to the GNU AGPL V3.
#
# In the case you use this program under the terms of the GNU AGPL V3,
# the program is provided in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public
# License with the Debian GNU/Linux or Univention distribution in file
# /usr/share/common-licenses/AGPL-3; if not, see
# <http://www.gnu.org/licenses/>.

# Mrtg will most likely not work properly when the environment variable
# LANG is set to UTF-8.
export LANG=C

# Name of config file
configfile="/etc/univention/univention-maintenance.conf"

# Search config file and check values
test -e $configfile || {
    echo
    echo "ERROR: Config file \"$configfile\" was not found!"
    echo
    exit 2;
}

. $configfile

test -z "$mrtg_config" && touch "$mrtg_config" 2>/dev/null && {
    echo
    echo "ERROR: \"\$mrtg_config\" is not valid."
    echo
    exit 2;
}
test -z "$wwwdir" && mkdir -p "$wwwdir" 2>/dev/null && {
    echo
    echo "ERROR: Missing \"\$wwwdir\" in $configfile"
    echo
    exit 2;
}
test -z $indexhtm && {
    echo
    echo "ERROR: Missing \"\$indexhtm\" in $configfile"
    echo
    exit 2;
}

# Delete old mrtg files
rm -f $wwwdir/ucs_*.png $wwwdir/ucs_*.txt
rm -f $wwwdir/uds_*.png $wwwdir/uds_*.txt
for i in $(ls $wwwdir/uds_*.log 2>/dev/null); do
	new=$(echo $i | sed 's/\/uds/\/ucs/')
	mv $i $new
done

# memory
freemem=$(more /proc/meminfo | grep ^MemFree: | awk {'print $2'})
buffers=$(more /proc/meminfo | grep ^Buffers: | awk {'print $2'})
cached=$(more /proc/meminfo | grep ^Cached: | awk {'print $2'})
memtotal=$(more /proc/meminfo | grep ^MemTotal: | awk {'print $2'})
free=$(($buffers + $cached + $freemem))
memused=$(($memtotal - $free))
prctmem=$((100 * $memused / $memtotal))
mrtgname="ucs_2mem"
echo "WorkDir: $wwwdir" > $mrtg_config
echo "Language: german" >> $mrtg_config
echo "Interval: 15" >> $mrtg_config
echo "Target[$mrtgname]: \`echo -e \"$prctmem\n0\nunused\nunused\"\`" >> $mrtg_config
echo "Options[$mrtgname]: growright,noo,gauge,nobanner,noborder,nolegend,noinfo" >> $mrtg_config
echo "Unscaled[$mrtgname]: dwmy" >> $mrtg_config
echo "MaxBytes[$mrtgname]: 100" >> $mrtg_config
echo "AbsMax[$mrtgname]: 99999" >> $mrtg_config
echo "Title[$mrtgname]: " >> $mrtg_config
echo "Ylegend[$mrtgname]: % " >> $mrtg_config
echo "Colours[$mrtgname]: blau#6666ff,unused#000000,unused#000000,unused#000000" >> $mrtg_config
echo "Background[$mrtgname]: #ffffff" >> $mrtg_config
echo "PageTop[$mrtgname]: <H1>Speicherauslastung</H1>" >> $mrtg_config
mrtg "$mrtg_config" 2>/dev/null
rm -f $mrtg_config $wwwdir/ucs_*.htm*
echo "Auslastung des Hauptspeichers: $prctmem%" >$wwwdir/$mrtgname.txt

# swap
swaptotal=$(more /proc/meminfo | grep ^SwapTotal: | awk {'print $2'})
swapfree=$(more /proc/meminfo | grep ^SwapFree: | awk {'print $2'})
swapused=$(($swaptotal - $swapfree))
if [ "$swaptotal" = "0" ]; then
	prctswap=0
else
	prctswap=$((100 * $swapused / $swaptotal)) 
fi
mrtgname="ucs_3swap"
echo "WorkDir: $wwwdir" > $mrtg_config
echo "Language: german" >> $mrtg_config
echo "Interval: 15" >> $mrtg_config
echo "Target[$mrtgname]: \`echo -e \"$prctswap\n0\nunused\nunused\"\`" >> $mrtg_config
echo "Options[$mrtgname]: growright,noo,gauge,nobanner,noborder,nolegend,noinfo" >> $mrtg_config
echo "Unscaled[$mrtgname]: dwmy" >> $mrtg_config
echo "MaxBytes[$mrtgname]: 100" >> $mrtg_config
echo "AbsMax[$mrtgname]: 99999" >> $mrtg_config
echo "Title[$mrtgname]: " >> $mrtg_config
echo "Ylegend[$mrtgname]: %" >> $mrtg_config
echo "Colours[$mrtgname]: blau#6666ff,unused#000000,unused#000000,unused#000000" >> $mrtg_config
echo "Background[$mrtgname]: #ffffff" >> $mrtg_config
echo "PageTop[$mrtgname]: <H1>Swapauslastung</H1>" >> $mrtg_config
mrtg "$mrtg_config" 2>/dev/null
rm -f $mrtg_config $wwwdir/ucs_*.htm*
echo "Auslastung des Auslagerungsspeicher: $prctswap%" >$wwwdir/$mrtgname.txt

# CPU usage of last 15 minutes
loadavg="`echo \`cat /proc/loadavg |awk '{print $3}'\`*100 | bc | cut -f1 -d"."`"
mrtgname="ucs_0load"
echo "WorkDir: $wwwdir" > $mrtg_config
echo "Language: german" >> $mrtg_config
echo "Interval: 15" >> $mrtg_config
echo "Target[$mrtgname]: \`echo -e \"$loadavg\n0\nunused\nunused\"\`" >> $mrtg_config
echo "Options[$mrtgname]: growright,noo,gauge,nobanner,noborder,nolegend,noinfo" >> $mrtg_config
echo "Unscaled[$mrtgname]: dwmy" >> $mrtg_config
echo "MaxBytes[$mrtgname]: 100" >> $mrtg_config
echo "AbsMax[$mrtgname]: 99999" >> $mrtg_config
echo "Title[$mrtgname]: " >> $mrtg_config
echo "Ylegend[$mrtgname]: %" >> $mrtg_config
echo "Colours[$mrtgname]: blau#6666ff,unused#000000,unused#000000,unused#000000" >> $mrtg_config
echo "Background[$mrtgname]: #ffffff" >> $mrtg_config
echo "PageTop[$mrtgname]: <H1>Systemlast</H1>" >> $mrtg_config
mrtg "$mrtg_config" 2>/dev/null
rm -f $mrtg_config $wwwdir/ucs_*.htm*
echo "Systemlast: $loadavg%" >$wwwdir/$mrtgname.txt

# Write HTML file
cat <<__EOT__ >$wwwdir/$indexhtm
<HTML>
<HEAD>
</HEAD>
<BODY BGCOLOR="#F0F0F0">
__EOT__
for file in $wwwdir/*-day.png; do
    echo "<P ALIGN=\"center\"><FONT FACE=\"ARIAL\">" >>$wwwdir/$indexhtm
    cat "`echo \"$file\" | sed \"s/-day.png/.txt/\"`" >>$wwwdir/$indexhtm
    echo "<BR>" >>$wwwdir/$indexhtm
    echo "<IMG SRC=\"`basename $file`\"><BR>" >>$wwwdir/$indexhtm
    echo "<IMG SRC=\"`basename $file -day.png`-week.png\"><BR>" >>$wwwdir/$indexhtm
    echo "<IMG SRC=\"`basename $file -day.png`-month.png\"><BR>" >>$wwwdir/$indexhtm
    echo "<IMG SRC=\"`basename $file -day.png`-year.png\"><BR>" >>$wwwdir/$indexhtm
    echo "</FONT></P>" >>$wwwdir/$indexhtm
done
cat <<__EOT__ >>$wwwdir/$indexhtm
</BODY>
</HTML>
__EOT__

