#!/bin/bash

SCRIPT_VER="1.0"

APP="Monitorix"
APP_CATEGORY="Monitoring"
APP_SCOPE="system"
APP_ENABLED="no"
SUPPORTED_ON="Ubuntu16.04,Ubuntu18.04,Ubuntu20.04"
LIBRARY="functions4.sh"

if [ "${SUPPORT-}" = '' ]; then
    SUPPORT="https://installers.rapidseedbox.com/scripts/whmcs_client_installers/support"
fi

LOGFILE="/var/log/whmcs_client_installers/${0}.log"
[ ! -d "/var/log/whmcs_client_installers" ] && mkdir -p "/var/log/whmcs_client_installers"

wget -4 -nc -q --no-check-certificate --timeout=20 --tries=5 ${SUPPORT}/${LIBRARY} >> "${LOGFILE}" 2>&1
if [ ! -f "./${LIBRARY}" ] ; then
    echo "55"
    echo "Error downloading \"${SUPPORT}/${LIBRARY}\"" >> "${LOGFILE}"
    exit 55
fi

# shellcheck source=support/functions4.sh
. "./${LIBRARY}"
SCRIPTENTRY


while : ; do

    main_rc=1

    CHECK_APP ; _rc=$?
    if [ ${_rc} -ne 0 ] ; then
        main_rc=${_rc}
        break
    fi

    #
    # http://www.monitorix.org/doc-debian.html
    #
    if ! echo "deb http://apt.izzysoft.de/ubuntu generic universe" \
            > "/etc/apt/sources.list.d/izzysoft.list"
    then
        ERROR "Error adding IzzySoft repository!"
        break
    fi
    if ! ADD_APT_KEY_URL "https://apt.izzysoft.de/izzysoft.asc" ; then
        ERROR "Error adding IzzySoft repository key!"
        RM_FILES "/etc/apt/sources.list.d/izzysoft.list"
        break
    fi

    APT_UPDATE || break

    ${PKG_INSTALL} "monitorix" || break
    cat <<-EOF > "/etc/monitorix/conf.d/00-debian.conf"
	# This is the Debian/Ubuntu configuration file to be added in the directory
	# /etc/monitorix/conf.d. It will override some default options without having
	# to change any line of the main configuration file.

	title           = Seedbox statistics
	secure_log      = /var/log/auth.log
	ftp_log         = /var/log/syslog

	<httpd_builtin>
	    enabled = n
	</httpd_builtin>

	# Graphs (de)activation
	# -----------------------------------------------------------------------------
	<graph_enable>
	        fs              = y
	        du              = y
	        process         = y
	        kern            = n
	        proc            = n
	        port            = n
	        ftp             = y
	        apache          = y
	        int             = n
	        serv            = n
	</graph_enable>

	# FS graph
	# -----------------------------------------------------------------------------
	<fs>
	        <list>
	                0 = /
	        </list>
	</fs>

	# DU graph
	# -----------------------------------------------------------------------------
	<du>
	        list = System, Users
	        <desc>
	                0 = /var/log, /etc, /tmp
	                1 = /home, /home/user
	        </desc>
	</du>

	# NET graph
	# -----------------------------------------------------------------------------
	<net>
	        max = 10
	        list = ${DEV}
	        <desc>
	                ${DEV} = FastEthernet LAN, 0, 10000000
	        </desc>
	        gateway = ${DEV}
	</net>

	# PORT graph
	# -----------------------------------------------------------------------------
	<port>
	        max = 4
	        rule = 24000
	        list = 20, 21, 443, 2222
	        <desc>
	                20      = FTP-DATA, tcp, in, 0, 1000, L
	                21      = FTP,      tcp, in, 0, 1000, L
	                443     = HTTPS,    tcp, in, 0, 1000, L
	                2222    = SSH,      tcp, in, 0, 1000, L
	        </desc>
	</port>

	# FTP graph
	# -----------------------------------------------------------------------------
	<ftp>
	        server = pure-ftpd
	</ftp>

	# PROCESS graph
	# -----------------------------------------------------------------------------
	<process>
	        <list>
	                0 = apache2, sshd, pure-ftpd, rtorrent, deluged, utserver
	        </list>
	        <desc>
	                apache2 = Apache
	                pure-ftpd = FTP
	                rtorrent = rTorrent
	                deluged = Deluge
	                utserver = uTorrent
	        </desc>
	</process>
	EOF
    SVC_RESTART "monitorix"

    # Configure Apache2 for `monitorix`
    if [ -d "/etc/apache2/userconf-enabled" ] ; then
        RUN_LOG "a2enmod cgid"
        cat <<-EOF > "/etc/apache2/userconf-enabled/monitorix.conf"
		Alias /monitorix /var/lib/monitorix/www
		ScriptAlias /monitorix-cgi /var/lib/monitorix/www/cgi

		<Directory /var/lib/monitorix/www/cgi/>
		    DirectoryIndex monitorix.cgi
		    Options +ExecCGI
		    Require all granted
		</Directory>

		# Apache rules to restrict access to Monitorix:
		#
		<Directory "/var/lib/monitorix/www">
		    Options Indexes Includes FollowSymLinks
		    <IfModule mod_authz_core.c>
		        # Apache 2.4
		        Require all denied
		        Require ip 127.0.0.1
		    </IfModule>
		    <IfModule !mod_authz_core.c>
		        # Apache 2.2
		        Order deny,allow
		        Deny from all
		        Allow from 127.0.0.1
		    </IfModule>
		    AllowOverride  None
		    AuthUserFile   /etc/apache2/users.password
		    AuthName       "apache2"
		    AuthType       Digest
		    Require        valid-user
		#    Satisfy        Any
		</Directory>
		EOF
        SVC_RESTART "${APACHE_SVC}"
    fi


    main_rc=0
    break

done

SCRIPTEXIT
