#!/usr/bin/env bash

echo 
echo "*** Tune Munin for Asterisk ***"
echo

yum -y install munin munin-nginx munin-cgi PyMunin --enablerepo=epel

cd /
sed -i "s/x86-07.phx2.fedoraproject.org/localhost/" /etc/munin/munin.conf 
sed -i "s/user root/user munin/" /etc/munin/munin-node.conf
sed -i "s/group root/group munin/" /etc/munin/munin-node.conf
sed -i "s/^host \*.*$/#host \*/" /etc/munin/munin-node.conf
sed -i "s/^#host 127.0.0.1/host 127.0.0.1/" /etc/munin/munin-node.conf

cat >> /etc/munin/plugin-conf.d/munin-node <<-EOF

[*]
env.LD_LIBRARY_PATH /usr/lib64
env.HOME /var/run/munin
env.PYTHONPATH /usr/lib64/python3.12:/usr/lib/python3.12
EOF

mkdir -p /var/www/html/maint/
ln -s /var/www/html/munin /var/www/html/maint/munin

# remove unworking plugins
cd /etc/munin/plugins
list="selinux_avcstat iostat_ios postfix_mailqueue yum lpstat"
for s in $list; do rm -f $s; done

ln -s /usr/share/munin/plugins/memcachedstats /etc/munin/plugins

# asterisk plugins
ln -s /usr/share/munin/plugins/asteriskstats /etc/munin/plugins
cat >> /etc/munin/plugin-conf.d/munin-node <<-EOF

[asteriskstats]
env.amihost localhost
env.amiport 5038
env.amiuser internal
env.amipass insecure
env.list_codecs alaw,ulaw,gsm,ilbc,g729
env.list_trunks PSTN=Zap\/(?P<num>\d+)=1-3,VoIP=SIP\/(net2phone|skype)

EOF

# mysql plugins
MYSQL='commands connections files_tables innodb_bpool innodb_bpool_act innodb_insert_buf innodb_io innodb_io_pend innodb_log innodb_rows innodb_semaphores innodb_tnx myisam_indexes network_traffic qcache qcache_mem select_types slow sorts table_locks tmp_tables'
cd /etc/munin/plugins/
for p in `echo $MYSQL`; do
ln -s /usr/share/munin/plugins/mysql_ mysql_$p
done

# DB access for MySQL plugins
cat >> /etc/munin/plugin-conf.d/munin-node <<-EOF
[mysql_*]
env.mysqlconnection DBI:mysql:mysql;host=127.0.0.1;port=3306
env.mysqluser root
env.mysqlpassword passw0rd

EOF

# fix access rights to munin dir
chown munin. /var/lib/munin/ -R 

#sed -i "s|^#cgiurl_graph .*$|cgiurl_graph /tunnel/link.cgi/http://127.0.0.1/munin-cgi/munin-cgi-graph|" /etc/munin/munin.conf
sed -i "s|^#cgiurl_graph .*$|cgiurl_graph /asterisk/extlink.cgi/http://127.0.0.1/munin-cgi/munin-cgi-graph|" /etc/munin/munin.conf


#cat > /etc/nginx/conf.d/https/service/munin/munin-cgi.conf <<-EOF
cat > /dev/null <<-EOF
location ^~ /munin-cgi/munin-cgi-graph/ {
    allow 127.0.0.1;
    deny all;
    fastcgi_split_path_info ^(/munin-cgi/munin-cgi-graph)(.*);
    fastcgi_param PATH_INFO \$fastcgi_path_info;
    fastcgi_pass unix:/var/run/munin/munin-cgi-graph.sock;
    include fastcgi_params;
}

location /munin/static/ {
    allow 127.0.0.1;
    deny all;
    alias /etc/munin/static/;
}
EOF

# add autorestart for munin services
for serv in `echo munin-node munin munin-cgi-graph`; do
mkdir -p /etc/systemd/system/$serv.service.d/
cat > /etc/systemd/system/$serv.service.d/override.conf <<-EOF
[Install]
WantedBy=multi-user.target

[Service]
Environment="LD_LIBRARY_PATH=/usr/lib64:/lib64"
Environment="HOME=/var/run/munin"
UnsetEnvironment=PERL5LIB

Environment=HOME=/var/lib/munin
Restart=always
RestartSec=60
EOF
done

systemctl daemon-reload
systemctl enable  munin munin-node munin.timer munin-cgi-graph.socket munin-cgi-graph.service
systemctl restart munin munin-node munin.timer munin-cgi-graph.socket munin-cgi-graph.service

yum -y install thirdlane-munin
