#!/bin/bash
######系統配合
#下載ISO檔
#https://ftp.ubuntu-tw.org/ubuntu-releases/26.04.1/ 安裝
######鄭亮星 jls@mail.jls.idv.tw , jls550225@gmail.com , jls@acfs.tw
####設定 Monitorix
ETH0=$(ls /proc/net/dev_snmp6 |awk '{ print $1 }' |sed -n '1p')
IP=$(ifconfig $ETH0 |grep 'inet ' |awk '{print $2}')
DOMAINNAME=$(/bin/hostname -d)
DATE=$(date "+%Y%m%d%H")
cp -arf /etc/bind/$DOMAINNAME /etc/bind/$DOMAINNAME-$DATE
cat >> /etc/bind/$DOMAINNAME << EOF
monitorix IN A $IP
EOF
find /etc/monitorix/monitorix.conf -type f -exec perl -i -pe s/"base_url = \/monitorix"/"base_url = \/"/g {} \;
cp -arf /etc/apache2/sites-available/http_80.conf /etc/apache2/sites-available/http_80.conf-$DATE
cat >> /etc/apache2/sites-available/http_80.conf << EOF
#------------------------------------------------------------------------------------------------------------------------
ServerAdmin root@$DOMAINNAME
ServerName monitorix.$DOMAINNAME
Order deny,allow
Allow from all
ProxyPreserveHost On
ProxyPass http://localhost:8080/
ProxyPassReverse http://localhost:8080/
#
#### From the Apache version 2.4.10 and above, use the SetHandler to run PHP as a fastCGI process server
# SetHandler "proxy:unix:/run/php/php7.4-fpm.sock|fcgi://localhost"
#
# CBandSpeed 40960 200 200
# CBandRemoteSpeed 40960kb/s 100 100
# CBandLimit 10000Mb
# CBandPeriod 4W
# CBandScoreboard /var/log/scoreboard/monitorix.$DOMAINNAME
# CBandScoreFlushPeriod /var/log/scoreboard/monitorix.$DOMAINNAME
# CBandExceededURL http://$DOMAINNAME/cband_error.html
EOF
cp -arf /etc/apache2/sites-available/https_443.conf /etc/apache2/sites-available/https_443.conf-$DATE
cat >> /etc/apache2/sites-available/https_443.conf << EOF
#------------------------------------------------------------------------------------------------------------------------
ServerAdmin root@$DOMAINNAME
ServerName monitorix.$DOMAINNAME
Order deny,allow
Allow from all
ProxyPreserveHost On
ProxyPass http://localhost:8080/
ProxyPassReverse http://localhost:8080/
#
#### From the Apache version 2.4.10 and above, use the SetHandler to run PHP as a fastCGI process server
# SetHandler "proxy:unix:/run/php/php7.4-fpm.sock|fcgi://localhost"
#
# CBandSpeed 40960 200 200
# CBandRemoteSpeed 40960kb/s 100 100
# CBandLimit 10000Mb
# CBandPeriod 4W
# CBandScoreboard /var/log/scoreboard/motioneye.$DOMAINNAME
# CBandScoreFlushPeriod /var/log/scoreboard/motioneye.$DOMAINNAME
# CBandExceededURL https://$DOMAINNAME/cband_error.html
EOF
a2enmod proxy
a2enmod proxy_http
systemctl daemon-reload
systemctl restart monitorix.service
systemctl restart named.service
systemctl restart apache2.service