#!/bin/bash
source /root/envsetup/envsetup.conf

# nginx
python3 /root/envsetup/envsetup.py 7
sed -i "s@server_name .*@server_name ${MONITOR};@" /etc/nginx/sites-available/msmonitor.conf
sed -i "s@server_name .*@server_name ${MS};@" /etc/nginx/sites-available/mediaserver-msuser.conf
# munin
# uwsgi
cp uwsgi_ /usr/share/munin/plugins/uwsgi_
chmod +x /usr/share/munin/plugins/uwsgi_
ln -s /usr/share/munin/plugins/uwsgi_ /etc/munin/plugins/uwsgi_average
ln -s /usr/share/munin/plugins/uwsgi_ /etc/munin/plugins/uwsgi_memory
ln -s /usr/share/munin/plugins/uwsgi_ /etc/munin/plugins/uwsgi_processes
# nginx
aptitude install -y libwww-perl
echo '' >> /etc/munin/plugin-conf.d/munin-node
echo '[nginx*]' >> /etc/munin/plugin-conf.d/munin-node
echo 'env.url http://127.0.0.1/nginx_status' >> /etc/munin/plugin-conf.d/munin-node
echo 'env.ua nginx-status-verifier/0.1' >> /etc/munin/plugin-conf.d/munin-node
cp nginx* /usr/share/munin/plugins/
ln -s /usr/share/munin/plugins/nginx-combined /etc/munin/plugins/
ln -s /usr/share/munin/plugins/nginx_connection_request /etc/munin/plugins/
ln -s /usr/share/munin/plugins/nginx_error /etc/munin/plugins/
ln -s /usr/share/munin/plugins/nginx_memory /etc/munin/plugins/
ln -s /usr/share/munin/plugins/nginx_vhost_traffic /etc/munin/plugins/
ln -s /usr/share/munin/plugins/nginx_working_set /etc/munin/plugins/
ln -s /usr/share/munin/plugins/nginx_request /etc/munin/plugins/nginx_request
ln -s /usr/share/munin/plugins/nginx_status /etc/munin/plugins/nginx_status
# http_response_time
cp http_response_time_ /usr/share/munin/plugins/
chmod +x /usr/share/munin/plugins/http_response_time_
ln -s /usr/share/munin/plugins/http_response_time_ /etc/munin/plugins/http_response_time_ms
cat << EOF >> /etc/munin/plugin-conf.d/munin-node

[http_response_time_ms]
env.url1_url https://${FTP}/crossdomain.xml
env.url1_name videos
env.url1_label ${FTP}/crossdomain.xml
EOF
# configure alerts
# detect where to insert
LIG=$(grep -n contact.nagios.command /etc/munin/munin.conf | awk -F ":" '{print$1}')
LIG=$(( $LIG + 1 ))

head -n ${LIG} /etc/munin/munin.conf > /etc/munin/munin.conf.tmp

cat << EOF >> /etc/munin/munin.conf.tmp
contacts alert
contact.alert.command mail -a "From:sysadmin <sysadmin@ubicast.eu>" -s "[munin] Alert on ${var:host}" sysadmin@ubicast.eu
contact.alert.always_send warning critical

[${MS}]
    address 127.0.0.1
    use_node_name yes
    memory.free.warning 512000000:
EOF

mv /etc/munin/munin.conf.tmp /etc/munin/munin.conf
# modify df function https://redmine.ubicast.net/issues/17912
sed -i "s@-P -l @-P@" /usr/share/munin/plugins/df

# new template
cd /etc/munin
git clone https://git.ubicast.net/mediaserver/munin-monitoring-theme.git
mv /etc/munin/static /etc/munin/static.orig
mv /etc/munin/templates /etc/munin/templates.orig
cp -pr munin-monitoring-theme/templates/munstrap/static /etc/munin/
cp -pr munin-monitoring-theme/templates/munstrap/templates /etc/munin/
# service restart
munin-node-configure --suggest
service munin restart
service munin-node restart
