# -*- coding: utf-8; mode: tcl; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:filetype=tcl:et:sw=4:ts=4:sts=4
# $Id$

PortSystem  1.0

name            nagios
version         4.2.1
categories      net
license         GPL-2
maintainers     nomaintainer
platforms       darwin

description     Nagios is a host and service monitoring program

long_description    A network service and host monitor that can notify you \
            when servers go down by monitoring for external services \
            such as HTTP, FTP, SMTP, SNMP, as well as internal host \
            checks for things such as CPU and disk usage and many \
            other things.

homepage        http://nagios.org

livecheck.regex     nagios-(\[0-9.\]+)${extract.suffix}

master_sites        sourceforge:project/nagios/nagios-4.x/nagios-${version}
checksums       rmd160 ae6eed1681bfff139dc3197d37c4aa5b46ef09b5 \
                sha256 cb62c5c16964cfc143b126a576a7f8130e634cb9dda4dd3bd3632598d7855a87

depends_build       port:gd2
depends_lib         path:lib/libssl.dylib:openssl \
                    port:nagios-plugins

set sysconfdir      ${prefix}/etc/${name}
destroot.destdir    prefix=${prefix} DESTDIR=${destroot}

destroot.target     install install-config install-commandmode install-init

configure.args      --with-cgiurl=/nagios/cgi-bin \
            --with-htmurl=/nagios \
            --with-nagios-user=nagios \
            --with-nagios-grp=nagios \
            --with-command-grp=nagios \
            --with-gd-lib=${prefix}/lib \
            --with-gd-inc=${prefix}/include \
            --sbindir=${prefix}/sbin/nagios \
            --libexecdir=${prefix}/libexec/nagios \
            --datadir=${prefix}/share/nagios \
            --with-lockfile=${prefix}/var/nagios/nagios.lock \
            --with-init-dir=${prefix}/var/nagios \
            --sysconfdir=${sysconfdir} \
            --localstatedir=${prefix}/var/nagios \
            --with-httpd-conf=/etc/apache2/httpd.conf

platform darwin 8 {
    configure.args-delete --with-httpd-conf=/etc/apache2/httpd.conf
    configure.args-append --with-httpd-conf=/etc/httpd/httpd.conf
}

startupitem.create      yes
startupitem.start       "${prefix}/var/nagios/nagios.init start"
startupitem.stop        "${prefix}/var/nagios/nagios.init stop"

post-patch {
        reinplace "s|INIT_OPTS=-o root -g root|INIT_OPTS=-o nagios -g nagios|g" \
                "${worksrcpath}/Makefile.in"

    reinplace "s|daemon-init \$(DESTDIR)\$(INIT_DIR)/nagios|daemon-init \$(DESTDIR)\$(INIT_DIR)/nagios.init|g" \
        "${worksrcpath}/Makefile.in"

    reinplace "s|NagiosLockDir=/var/lock/subsys|NagiosLockDir=${prefix}/var/nagios|g" \
        ${worksrcpath}/daemon-init.in
    reinplace "s|su -|su -m|g" \
        ${worksrcpath}/daemon-init.in
}

add_users nagios group=nagios

post-destroot {
# Keep the empty command file directory created by 'make install-commandmode'
        destroot.keepdirs ${destroot}${prefix}/var/nagios/rw

# Nagios sample files no longer install with -sample suffixes, so do this manually for safety
    foreach cfgfile [glob ${destroot}${sysconfdir}/*.cfg] {
        file rename ${cfgfile} ${cfgfile}-sample
    }
    foreach cfgfile [glob ${destroot}${sysconfdir}/objects/*.cfg] {
        file rename ${cfgfile} ${cfgfile}-sample
    }
}


notes "
************************************************************
The basic steps to complete the installation are below.  But Nagios is\
a complex program so you should read the documentation online at\
http://www.nagios.org or locally at ${prefix}/share/nagios/docs/index.html\
to fully understand Nagios.
************************************************************

IMPORTANT: A user and group called 'nagios' has been created and you must run\
       nagios as that user.



1) Setup Apache's httpd.conf file for Nagios (restart Apache when done)

   Insert the \"Nagios stuff\" text block somewhere *above* your Apache cgi-bin\
   ScriptAlias statement:

    ScriptAlias /cgi-bin/ \"/Library/WebServer/CGI-Executables/\"

#
# Nagios stuff

ScriptAlias /nagios/cgi-bin/ \"${prefix}/sbin/nagios/\"
<Directory \"${prefix}/sbin/nagios\">
 AllowOverride None
    Options ExecCGI
    Order allow,deny
    Allow from all
    AuthName \"Nagios Access\"
    AuthType Basic
    AuthUserFile ${prefix}/etc/nagios/htpasswd.users
    require valid-user
</Directory>

Alias /nagios \"${prefix}/share/nagios\"
<Directory \"${prefix}/share/nagios\">
    Options None
    AllowOverride AuthConfig
    Order allow,deny
    Allow from all
</Directory>

# End Nagios stuff
#


2) Configure the Nagios sample files in ${prefix}/etc/nagios.

a) First, backup the Nagios sample files.

   cd ${prefix}/etc/nagios
   sudo mkdir sample
   sudo cp *.cfg-sample sample/

b) Then rename the sample files for use.

   sudo -s
   for i in *cfg-sample; do mv \$i `echo \$i | sed -e s/cfg-sample/cfg/`; done;
   exit

c) Configure the default commands.cfg file to your requirements.  Then test\
   the Nagios configuration with this command until you get no errors:

   sudo nagios -v ${prefix}/etc/nagios/nagios.cfg


3) Set user 'nagiosadmin' as a \"superuser\" to browse all hosts/services in
   ${prefix}/etc/nagios/cgi.cfg

   authorized_for_system_information= nagiosadmin
   authorized_for_configuration_information= nagiosadmin
   authorized_for_system_commands= nagiosadmin
   authorized_for_all_services= nagiosadmin
   authorized_for_all_hosts= nagiosadmin
   authorized_for_all_service_commands= nagiosadmin
   authorized_for_all_host_commands= nagiosadmin


4) Create a 'nagiosadmin' Apache user/password to correspond with the Nagios Apache\
   user created above

    htpasswd -c ${prefix}/etc/nagios/htpasswd.users nagiosadmin

   Create other users as desired, omit '-c' since it creates the password file.  Make\
   additional users login names match Nagios contact names to restrict web viewing to your\
   host's by contact.


5) Set Nagios to start at system boot, and start Nagios

   Run launchctl so Nagios will start at system boot
    sudo launchctl load -w /Library/LaunchDaemons/org.macports.nagios.plist

   You may now start Nagios manually with this command (or a reboot):
    sudo ${prefix}/var/nagios/nagios.init start


6) You may now login as 'nagiosadmin' to the Nagios web interface at http://localhost/nagios


NOTE: The SMTP server Postfix is required to enable Nagios email notifications


File Locations:

    Configuration files: ${prefix}/etc/nagios
    Nagios plugins: ${prefix}/libexec/nagios
    Lock file:  ${prefix}/var/nagios/nagios.lock
    Init file:  ${prefix}/var/nagios/nagios.init
    HTML URL:  http://localhost/nagios/
    CGI URL:  http://localhost/nagios/cgi-bin/
"