# $Id: Portfile,v 1.11 2006/07/19 05:38:51 markd Exp $
PortSystem      1.0

name                    monarch
version                 1.2
revision		1
categories              net
maintainers             markd@opendarwin.org
description             A GUI configuration tool for Nagios written in perl
long_description        ${description}
homepage                http://sourceforge.net/projects/monarch
platforms               darwin
master_sites            sourceforge
distfiles		${name}-${version}.tar.bz2 groundwork-monitor-monarch-1.2-patch.tar.bz2
checksums               ${name}-${version}.tar.bz2 md5 5a0a292495bbea4c570fc68d59c0db95 \
			groundwork-monitor-monarch-1.2-patch.tar.bz2 md5 6d0959c4e1475b363e13c8726f3e2c55
use_bzip2		yes
patchfiles		patch-nmap_scan-nmap_scan.c \
			patch-monarch_setup.pl
depends_lib             port:mysql5 \
			port:p5-libwww-perl \
			port:p5-xml-libxml-common \
			port:p5-xml-namespacesupport \
			port:p5-xml-sax \
			port:p5-xml-libxml \
			port:p5-nmap-scanner \
			port:p5-cgi.pm \
			port:p5-dbi \
			port:p5-dbd-mysql \
			port:p5-cgi-ajax \
			port:p5-class-accessor

use_configure		no
build {}

pre-fetch {
        ui_msg "\n

                   **************************************************
                   **** If MySQL is being installed during the   ****
		   **** Monarch install (first-time MySQL        ****
		   **** install, is recommended that you pre-    ****
		   ****	install MySQL5 with the +server variant  ****
		   **** so MySQL will start system boot.  To do  ****
		   **** so:					 ****

                      1) Cancel this install now with Cntrl-C.
                      2) Type \"port install mysql5 +server\"
                      3) Re-run the Monarch port install
                   ************************************************
                \n"
}

set monarchdir ${prefix}/groundwork/monarch

destroot {
        xinstall -m 755 -d ${destroot}${monarchdir}
        system "cp -R ${worksrcpath}/* ${destroot}${monarchdir}"

# Apply patches
	eval xinstall -m 755 [glob ${workpath}/groundwork-monitor-monarch-1.2-patch/*.cgi] \
		${destroot}${monarchdir}
	eval xinstall -m 755 [glob ${workpath}/groundwork-monitor-monarch-1.2-patch/*.pm] \
		${destroot}${monarchdir}
}

post-destroot {
        eval reinplace "s|/usr/bin/perl|${prefix}/bin/perl|g" \
                [glob ${destroot}${monarchdir}/*.pl]

	eval reinplace "s|/usr/local/groundwork/bin/perl|${prefix}/bin/perl|g" \
		[glob ${destroot}${monarchdir}/*.cgi]

        eval reinplace "s|/usr/local/groundwork/bin/perl|${prefix}/bin/perl|g" \
                [glob ${destroot}${monarchdir}/*.p*]  

        eval reinplace "s|/usr/local/groundwork/monarch|${monarchdir}|g" \
		[glob ${destroot}${monarchdir}/*.*]

# Fix mysqldump location
	reinplace "s|/usr/bin/mysqldump|${prefix}/bin/mysqldump5|g" \
                ${destroot}${monarchdir}/MonarchFile.pm

# Enable Nmap Scanner
	xinstall -m 755 -d ${destroot}${monarchdir}/bin

	reinplace "s|/usr/local/groundwork/bin|${monarchdir}/bin|g" \
		${destroot}${monarchdir}/nmap_scan_one.pl

	reinplace "s|__PREFIX__|${prefix}|g" \
		${worksrcpath}/nmap_scan/nmap_scan.c

	xinstall -m 755 ${destroot}${monarchdir}/nmap_scan_one.pl \
                ${destroot}${monarchdir}/bin

	system "cd ${worksrcpath}/nmap_scan && gcc -g -O2 -Wall -o nmap_scan_one nmap_scan.c"

# Install nmap_scan_one with mode 4750 and chown it to root (below) to allow normal user to run nmap with root privs
	xinstall -m 4750 ${worksrcpath}/nmap_scan/nmap_scan_one \
		${destroot}${monarchdir}/bin

# Delete precompiled Linux version of nmap_scan_one and leftover nmap_scan_one.pl
	file delete ${destroot}${monarchdir}/nmap_scan_one
	file delete ${destroot}${monarchdir}/nmap_scan_one.pl

# End enable Nmap-Scanner

# Set permissions & modes
	system "chown -R nagios:nagios ${destroot}${monarchdir}/*"
	system "chmod +x ${destroot}${monarchdir}/*.cgi"
	system "chown root ${destroot}${monarchdir}/bin/nmap_scan_one"
}

post-activate {

ui_msg "\n **** To complete the Monarch installation ****
        
Read the README at ${prefix}/share/groundwork/monarch/ for full information.

1) Configure Nagios with the default sample files or begin with a Nagios configuration you created manually.


2) Setup MySQL and create the Monarch user
   Configure MySQL (new MySQL installs)
        sudo -u mysql ${prefix}/lib/mysql5/bin/mysql_install_db
 
   Start MySQL.
        sudo ${prefix}/share/mysql5/mysql/mysql.server start

   Set MySQL to start at system boot (optional)
        sudo launchctl load -w /Library/LaunchDaemons/org.darwinports.mysql5.plist

   Set a root MySQL password.
        Follow the instructions that were given after you executed 'mysql_install_db' above.

   Create the Monarch MySQL user and password and set the account's permissions
	mysql5 -u root -p (login with new root password when prompted)
        mysql> grant CREATE,INSERT,SELECT,DELETE,UPDATE on monarch.* to monarch@localhost;
        mysql> grant CREATE,INSERT,SELECT,DELETE,UPDATE on monarch.* to monarch;
	mysql> SET PASSWORD FOR monarch@localhost = PASSWORD('my-password');
        mysql> exit


3) Create the Monarch MySQL database and initialize it using monarch_setup.pl.
   Login to MySQL and the create Monarch database
	mysql5 -u root -p (login with new root password when prompted)
        mysql> create database monarch;

   Run monarch_setup.pl
	cd ${monarchdir}
	sudo ./monarch_setup.pl

   Responses for the monarch_setup.pl program
	What is the host name of your MySQL database server? - localhost
	What is the name of the user that will have access to the database? - monarch
	What is the password for user monarch to access the database monarch - <my-password>
	What is the name of the database you will use for your Monarch installation? - monarch
	Enter web server's user account - nagios
	Enter web server's user group - nagios
	Please enter your web server's relative path to cgi-bin - /cgi-bin
	What is your web server's document root - /Library/WebServer/Documents
	Enter the full path of your cgi-bin directory - /Library/WebServer/CGI-Executables/
	Enter the full installation path for Monarch - ${monarchdir}
	Enter the full path to your nagios.cfg file - ${prefix}/etc/nagios
	Enter the full path to your nagios binary file - ${prefix}/bin
	
   Verify Monarch DB:
        mysql5 -u root -p
        mysql> use monarch;
        mysql> show tables;
        mysql> exit

NOTE: If you wish to delete the monarch database to start over: a) log into MySQL as
      root; b) execute 'drop database monarch'; c) repeat Step 2.  There is no need
      to repeat step 1 because the Monarch user/pass and permissions are not deleted
      by dropping (deleting) the database.


4) Set your Apache user and group to the Nagios user
        sudo pico /etc/httpd/httpd.conf
        
	user nagios
	group nagios


5) Create a ParserDetails.ini for module XML::SAX
   Execute this command
        perl -MXML::SAX -e \"XML::SAX->add_parser(q(XML::SAX::PurePerl))->save_parsers()\"


6) Initial Monarch setup at http://localhost/cgi-bin/monarch.cgi

        Login with the default user/pass \"super_user\" \"password\"

Control menu
   Setup -> Nagios Version -> 2.x
   Nagios Main Configuration -> Load from nagios.cfg (to import existing Nagios configuration)
   Nagios CGI Configuration -> Load from cgi.cfg (to import existing CGI configuration)
   Load (to populate the Monarch database with sample Nagios object files or manually created ones)

Now setup your hosts, services, etc using the Monarch GUI.


7) Optionally use the simplified Monarch interface

   After the initial Monarch setup you may setup your hosts, services, etc with the optional
   Monarch EZ GUI if you intend to use default settings for the advanced options and/or use
   nmap host discovery to add hosts to Nagios, use the simplified Monarch interface.

   To do this, first copy monarch_ez.cgi and monarch_scan.cgi to your cgi-bin/ directory
        cd ${monarchdir}
        sudo cp -p monarch_ez.cgi /Library/WebServer/CGI-Executables/
        sudo cp -p monarch_scan.cgi /Library/WebServer/CGI-Executables/

   Now go to http://localhost/cgi-bin/monarch_ez.cgi

   You may revert to using the full Monarch interface at any time.

NOTE: No login is required for the monarch_ez.cgi interface, so you will need to use Apache
      username(s)/password(s) just like you use for the Nagios monitor interface to maintain
      secure access.
\n"
}
