# $Id: Portfile 82553 2011-08-15 19:22:39Z jmr@macports.org $
PortSystem      1.0

name                    monarch
version                 2.5.0
revision                1
categories              net
maintainers             markd
description             A GUI configuration tool for Nagios written in perl
long_description        ${description}
homepage                http://sourceforge.net/projects/monarch
platforms               darwin
master_sites            sourceforge
checksums               md5 68658989ff485ac58a42035c3a927362
patchfiles              patch-nmap_scan-nmap_scan.c
depends_build		port:nagios
depends_lib             path:bin/mysql_config5:mysql5 \
			port:p5.12-libwww-perl \
			port:p5.12-xml-namespacesupport \
			port:p5.12-xml-sax \
			port:p5.12-xml-libxml \
			port:p5.12-nmap-scanner \
			port:p5.12-dbi \
			port:p5.12-dbd-mysql \
			port:p5.12-cgi-ajax \
			port:p5.12-class-accessor
patch.post_args		-F 3
use_configure		no
build {}

variant server  {
# This variant doesn't do anything in this port, it only exists to trigger the server
# variant in the mysql5 port.
}

variant_set server

set monarchdir ${prefix}/groundwork/monarch

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

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

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

        eval reinplace "s|/usr/local/groundwork/bin/perl|${prefix}/bin/perl5.12|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}/MonarchStorProc.pm

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

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

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

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

	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.macports.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) 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.


6) 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"
}
