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

PortSystem          1.0
PortGroup           perl5 1.0

name                nedi
version             1.0.9
revision            4
license             GPL-3
categories          net
maintainers         nomaintainer
platforms           darwin

description         Network Discovery Suite

long_description    A low noise network discovery, management, and inventory \
                    system for Cisco networks with a user friendly web interface.

homepage            http://www.nedi.ch/
master_sites        http://www.nedi.ch/pub

extract.mkdir       yes
extract.suffix      .tgz

checksums           rmd160  776b178b4fd3fe139796dbc367ae0e626f6ccaa1 \
                    sha256  cf004dafb781f1ac638b2291ea565d3c3972eb5dd049f9119b46dbc66f722f13

perl5.branches      5.26

depends_lib         port:perl${perl5.major} \
                    path:bin/mysql_config5:mysql5 \
                    port:p${perl5.major}-net-snmp \
                    port:p${perl5.major}-net-ssh-perl \
                    port:p${perl5.major}-net-telnet \
                    port:p${perl5.major}-net-telnet-cisco \
                    port:p${perl5.major}-libwww-perl \
                    port:p${perl5.major}-algorithm-diff \
                    port:p${perl5.major}-dbi \
                    port:p${perl5.major}-dbd-mysql \
                    port:rrdtool \
                    port:fping


# Set some useful variables
set nedidir         ${prefix}/share/${name}
set rrdbin          ${prefix}/bin/rrdtool
set rrdfiles        ${nedidir}/rrd
set fpingpath       ${prefix}/sbin
set cacticlidir     ${prefix}/share/cacti/cli

startupitem.create  yes
startupitem.name    nedimonitor
startupitem.executable  ${nedidir}/moni.pl

use_configure       no
build {}

destroot {
# create directories
    file mkdir ${destroot}/Library/LaunchDaemons
    file mkdir ${destroot}${nedidir}

# copy content to destroot
    copy {*}[glob ${worksrcpath}/*] ${destroot}${nedidir}
}

post-destroot {

# Fix general paths
    reinplace "s|^#!.*|#!${perl5.bin}|" \
        {*}[glob ${destroot}${nedidir}/*.pl] \
        {*}[glob ${destroot}${nedidir}/contrib/*.pl] \
        {*}[glob ${destroot}${nedidir}/inc/*.pl]

    reinplace "s|netstat|/usr/sbin/netstat|g" \
        ${destroot}${nedidir}/inc/libmisc.pm

    reinplace "s|/etc/nedi.conf|${nedidir}/nedi.conf|g" \
        ${destroot}${nedidir}/html/inc/libmisc.php

# Fix rrdcmd location in nedi.conf
    reinplace "s|rrdtool|${rrdbin}|g" \
        ${destroot}${nedidir}/nedi.conf

# Fix fping path
    reinplace "s|/usr/local/sbin|${fpingpath}|g" \
        ${destroot}${nedidir}/html/Nodes-Toolbox.php

# Fix Cacti paths in nedi.conf
    reinplace "s|/usr/bin/php /var/www/html/cacti/cli|${prefix}/bin/php ${cacticlidir}|g" \
        ${destroot}${nedidir}/nedi.conf

# Create directory for rrd files
        xinstall -m 755 -d ${destroot}${rrdfiles}

# Keep these empty directories
        destroot.keepdirs \
                ${destroot}${nedidir}/db/cfg \
        ${destroot}${nedidir}/html/log \
        ${destroot}${rrdfiles}

# Rename nedi.conf to nedi.conf.sample so port upgrades don't overwrite an installed nedi.conf
# Also set permissions on nedi.conf because it has passwords.
    file rename ${destroot}${nedidir}/nedi.conf \
        ${destroot}${nedidir}/nedi.conf.sample
        system "chmod 600 ${destroot}${nedidir}/nedi.conf.sample"

# MacPorts can create only one startupitem per portfile.  So copy nedisyslog.plist
# from filespath and link it into /Library/LaunchDaemons as startupitem's do.

    xinstall -m 755 -d ${destroot}${prefix}/etc/LaunchDaemons/org.macports.nedisyslog/
    xinstall -m 755 ${filespath}/org.macports.nedisyslog.plist \
        ${destroot}${prefix}/etc/LaunchDaemons/org.macports.nedisyslog/
    ln -s ${prefix}/etc/LaunchDaemons/org.macports.nedisyslog/org.macports.nedisyslog.plist \
        ${destroot}/Library/LaunchDaemons
}

pre-activate {
    # nedi < 1.0.9 installed this symlink directly, bypassing the destroot.
    # Remove it if found to prevent activation failure.
    set f /Library/LaunchDaemons/org.macports.nedisyslog.plist
    if {![catch {file type ${f}}] && [registry_file_registered ${f}] == "0"} {
        if {[catch {delete ${f}}]} {
            ui_warn "Cannot delete ${f}; please remove it manually"
        }
    }
}

notes \
  "**** To complete the NeDi OS X installation ****


1) Setup MySQL (for new installs)
-----------------------------------------------
-Configure MySQL:
        sudo -u mysql ${prefix}/lib/mysql5/bin/mysql_install_db

-Start MySQL and set it to run at system boot:
    sudo launchctl load -w /Library/LaunchDaemons/org.macports.mysql5.plist

-Set the root MySQL password (replace <mypassword> with your chosen password):
    sudo ${prefix}/lib/mysql5/bin/mysqladmin -u root password <mypassword>


2) Install PHP5 with SNMP support and MacPorts Apache
------------------------------------------------
    sudo port install php5 +apache2 +mysql5 +snmp

    sudo ${prefix}/apache2/bin/apxs -a -e -n \"php5\" libphp5.so

NOTE: Check to make sure the PHP variable \"mysql.default_socket\" is set to path
 ${prefix}/var/run/mysql5/mysqld.sock by viewing the output of this terminal command:
        php -i


3) Turn on MacPorts Apache
------------------------------------------------
-Turn off Apple's built-in Apache (Personal Web Sharing in System Preferences).  Then execute
   these commands:
    cd ${prefix}/apache2/conf
    sudo cp httpd.conf.sample httpd.conf

-Edit your httpd.conf
   Change the user/group variables in the Apache httpd.conf to be the Unix user/group that
     you'll use to run the NeDi executable

   Add an alias statement:
    Alias /nedi \"${prefix}/share/nedi/html\"

-Start Apache and set it to run at system boot:
    sudo launchctl load -w /Library/LaunchdDaemons/org.macports.apache2.plist


4) Configure NeDi, initialize NeDi database, and Login to NeDi
-----------------------------------------------
-Set NeDi owner: sudo chown -R <nedi-user>:<nedi-group> ${nedidir}

-Edit nedi.conf Backend/Authen/Device Access sections: sudo pico ${nedidir}/nedi.conf
    Leave nedi.conf permissions at 600 to protect your network passwords!

backend     MSQ
dbpass      <nedidb-password>
authuser    mysql

comm <my-community-string> (community string of your Cisco devices)
<usr> <pass> <enablepass> (user/pass of your Cisco devices)

-Initialize the NeDi database:
    cd ${nedidir}
    sudo ./nedi.pl -i
    When prompted for \"MySQL admin user:\" and enter 'root' and then MySQL root password.

-Verify the NeDi DB:
        mysql -u root -p
        mysql> use nedi;
        mysql> show tables;
        mysql> exit;

-Login to the NeDi web interface: http://localhost/nedi
    Initial user/pass is 'admin'/'admin'


5) Discover Your Network with NeDi
-----------------------------------------------
-Edit nedi.conf Device Acc. variables: sudo pico ${nedidir}/nedi.conf

comm <my-community-string>
<usr>  <pass>  <enablepass>

-Make any other desired changes in nedi.conf

-Start NeDi data collection:
    cd ${nedidir}
    sudo -u <nedi-user> nedi.pl -pr (options shown set CDP and routing tables for discovery)

-Put a command in the crontab to discover your network at regular intervals.
    A typical interval is 1 hour; to get accurate NeDi rrdtool graphs at
    NeDi's default settings you must run NeDi at 1 hour intervals.
    Otherwise, you must adjust NeDi's default rrdtool settings.  To run
    NeDi every hour, the cron entry is:

0 * * * * cd ${nedidir} ; ./nedi.pl -pr >> /dev/null 2>&1


6) Set NeDi to receive device alerts, syslogs, and SNMP traps.  (optional)
-----------------------------------------------
-To enable NeDi alerts

    sudo launchctl load -w /Library/LaunchDaemons/org.macports.nedimonitor.plist

-To enable the NeDi syslog daemon:

    sudo launchctl load -w /Library/LaunchDaemons/org.macports.nedisyslog.plist"

livecheck.type      regex
livecheck.url       http://www.nedi.ch/download/
livecheck.regex     "${name}-(\\d+(?:\\.\\d+)*\[A-Z\]?)${extract.suffix}"