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

PortSystem	1.0

name			nedi
version			1.0.5
revision		3
categories		net
maintainers		markd
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/_media/files:

extract.suffix		.tgz
worksrcdir		${name}
checksums		md5 6ed980228483addfdc5caf14430d7570

depends_lib		port:perl5.12 \
			path:bin/mysql_config5:mysql5 \
			port:p5.12-net-snmp \
			port:p5.12-net-ssh-perl \
			port:p5.12-net-telnet \
			port:p5.12-net-telnet-cisco \
			port:p5.12-libwww-perl \
			port:p5.12-algorithm-diff \
			port:p5.12-dbi \
			port:p5.12-dbd-mysql \
			port:rrdtool \
			port:fping

variant cacti description {NeDi supports the Cacti API to selectively add graphs of NeDi discovered devices to a Cacti installation on the same host} {
	depends_lib-append port:cacti
}

# 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 {
	file mkdir ${destroot}${nedidir}
	system "cp -R ${worksrcpath}/* ${destroot}${nedidir}"
}

post-destroot {
# Fix general paths
       eval reinplace "s|/usr/bin/perl|${prefix}/bin/perl5.12|g" \
                [glob ${destroot}${nedidir}/*.pl] \
		[glob ${destroot}${nedidir}/inc/*.pl] \
                [glob ${destroot}${nedidir}/html/inc/*.pl] \
		[glob ${destroot}${nedidir}/contrib/*.pl]

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

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

pre-install {
# MacPorts can create only one startupitem per portfile.  So copy nedisyslog.plist and
# snmptrapd.plist from filespath and link them 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/
	system "cd /Library/LaunchDaemons && ln -sf \
		${prefix}/etc/LaunchDaemons/org.macports.nedisyslog/org.macports.nedisyslog.plist"

	xinstall -m 755 -d ${destroot}${prefix}/etc/LaunchDaemons/org.macports.snmptrapd/
	xinstall -m 755 ${filespath}/org.macports.snmptrapd.plist \
		${destroot}${prefix}/etc/LaunchDaemons/org.macports.snmptrapd/
	system "cd /Library/LaunchDaemons && ln -sf \
		${prefix}/etc/LaunchDaemons/org.macports.snmptrapd/org.macports.snmptrapd.plist"
}

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 Apache 2
------------------------------------------------
	sudo port install php5 +apache2 +mysql5 +snmp

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

-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 Apache 2
------------------------------------------------
-Turn off Apple's Apache 1.3 (Personal Web Sharing in System Preferences).  Then execute
 these commands:

	cd ${prefix}/apache2/conf
	sudo cp httpd.conf.sample httpd.conf

   Edit the user/group variables in the Apache 2 httpd.conf to be your selected NeDi user/group.

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


4) Configure NeDi, initialize database, and Login to NeDi
-----------------------------------------------
-Set NeDi owner: sudo chown -R <nedi-user>:<nedi-group> ${nedidir}
-Apache symlink: ln -s ${nedidir}/html/  ${prefix}/apache2/htdocs/nedi
-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-commnity-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.

-Set Nedi MySQL user to use old style passwords
	mysql -u root -p
	mysql> SET PASSWORD FOR nedi@localhost = OLD_PASSWORD('<nedidb-pwd>');

-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/index.php
	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>

Optionally uncomment the 'cacticli' variable to create Cacti graphs within NeDi

-Make any other desired changes in nedi.conf
-Start NeDi data collection:
	cd ${nedidir}
	sudo -u <nedi-user> nedi.pl -c -d (debug)
-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 -c >> /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

-To enable the NeDi SNMP trap receiver, first use the snmpconf Unix utility to create an
 snmptrapd.conf file with the entry:

	traphandle      default ${nedidir}/trap.pl

 Then load the startup script to run the Unix snmptrapd daemon:

	sudo load lauchctl load -w /Library/LaunchDaemons/org.macports.snmptrapd.plist"
