Date Tags linux

We needed a network analysis tool and decided to use ntop. Official ntop website provides binary packages for Ubuntu and CentOS.

Ubuntu has an official package for ntop but the package is version 3 and we preferred to use the latest version, so package repositories were no good for us.

We set up a CentOS 6 server and installed ntop rpm package. But the package did not include an init script. Here is an init script to start/stop ntop in CentOS 6:

#!/bin/bash
#
# ntop          Start up the ntop as a daemon
#
# chkconfig: 2345 55 25
# description: ntop is a network traffic probe that shows the network usage
#              This service starts up the ntop as a daemon.
#
# processname: ntop
# config: /etc/ntop.conf
# pidfile: /usr/local/var/ntop/ntop.pid

### BEGIN INIT INFO
# Provides: ntop
# Required-Start: $local_fs $network $syslog
# Required-Stop: $local_fs $syslog
# Should-Start: $syslog
# Should-Stop: $network $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start up the ntop as a daemon
# Description: ntop is a network traffic probe that shows the network usage
#                    This service starts up the ntop as a daemon.
### END INIT INFO

# source function library
. /etc/rc.d/init.d/functions

runlevel=$(set -- $(runlevel); eval "echo \$$#" )

RETVAL=0
prog="ntop"

NTOPD="/usr/local/bin/ntop"
NTOP_CONF_FILE="/etc/ntop.conf"
PID_FILE="/usr/local/var/ntop/ntop.pid"

if [ -f $NTOP_CONF_FILE ]
then
    OPTIONS="@$NTOP_CONF_FILE --daemon"
else
    OPTIONS="--daemon"
fi

start()
{
        if [ ! -x $NTOPD ]; then
        echo "Failed to find $prog exeuctable on $NTOPD!"
        exit 5
    fi
        echo -n $"Starting $prog: "
        $NTOPD $OPTIONS && success || failure
    RETVAL=$?
    PID=`pidof $prog`
    echo $PID > $PID_FILE
        return $RETVAL
}

stop()
{
        echo -n $"Stopping $prog: "
        if [ -f $PID_FILE ]; then
        killproc -p $PID_FILE $NTOPD
        else
        failure $"Stopping $prog"
        fi
        RETVAL=$?
        # if we are in halt or reboot runlevel kill all running sessions
        # so the TCP connections are closed cleanly
        if [ "x$runlevel" = x0 -o "x$runlevel" = x6 ] ; then
            trap '' TERM
            killall $prog 2>/dev/null
            trap TERM
        fi
        echo
}

restart() {
        stop
        start
}

rh_status() {
        status -p $PID_FILE $prog
}

rh_status_q() {
        rh_status >/dev/null 2>&1
}

case "$1" in
        start)
                rh_status && exit 0
                start
                ;;
        stop)
                if ! rh_status_q; then
                        exit 0
                fi
                stop
                ;;
        restart)
                restart
                ;;
        status)
                rh_status
                RETVAL=$?
                ;;
        *)
                echo $"Usage: $0 {start|stop|restart|status}"
                RETVAL=2
        ;;
esac
exit $RETVAL

place the script in /etc/init.d and make sure it has execute permissions.

sudo chmod 750 /etc/init.d/ntop

You may use chkconfig to add the init script to autostart on certain run levels, The defaults for this script is on run level 2,3,4,5.

sudo chkconfig --add ntop

The init script uses /etc/ntop.conf as the default configuration file. Run ntop executable with --help to see more information about command line options. You could use the same options in the configuration file.