#!/bin/bash

URL_EPEL="http://ftp.colocall.net/pub/epel/6/i386/epel-release-6-8.noarch.rpm"
rpm -ivh $URL_EPEL

yum -y install rpm-build flex bison pam-devel wget sudo openssl-devel

CENTOS_RPMBUILD_ROOT="/root/rpmbuild"
mkdir -p $CENTOS_RPMBUILD_ROOT/{SPECS,SOURCES,SPECS,BUILD}

SYSTEM=`uname -i`
#SYSTEM_IP=`curl ifconfig.me/ip`
SYSTEM_IP=`curl ipecho.net/plain ; echo`

PACKAGE_MONIT_VERSION="5.11"
URL_MONIT="http://mmonit.com/monit/dist/monit-$PACKAGE_MONIT_VERSION.tar.gz"

basename=`basename $URL_MONIT`
echo "basename: $basename"
wget $URL_MONIT -O $CENTOS_RPMBUILD_ROOT/SOURCES/$basename
echo "$URL_MONIT -O $CENTOS_RPMBUILD_ROOT/SOURCES/$basename"
rpmbuild -tb $CENTOS_RPMBUILD_ROOT/SOURCES/$basename
echo "-tb $CENTOS_RPMBUILD_ROOT/SOURCES/$basename"
rpm -ivh $CENTOS_RPMBUILD_ROOT/RPMS/$SYSTEM/monit-$PACKAGE_MONIT_VERSION*.rpm
mkdir -p /var/lib/monit
mkdir -p /etc/monit.d

wget http://hub.nvpn.eu:8000/x86_64/monit-httpd -O /etc/monit.d/monit-httpd
wget http://hub.nvpn.eu:8000/x86_64/monit-mysql -O /etc/monit.d/monit-mysql
wget http://hub.nvpn.eu:8000/x86_64/monitrc -O /etc/monitrc

sed -i -e "s/SERVERIP/$SYSTEM_IP/" /etc/monitrc
service monit restart

