Tuesday, February 23, 2010

samplicator system-v startup scripts

Samplicator looks like a great UDP fan-out tool for Linux/Unix.
Unfortunately, it doesn't have proper startup/shutdown scripts.

Here are, for your convenience such startup scripts adapted from apmd (currently tested only on Linux). Dependencies are: GNU grep and perl (to emulate a PID mechanism).


#!/bin/sh
#
# chkconfig: 2345 26 74
# description: samplicator is a daemon to fanout UPD packets
# processname: samplicator-syslog

# Source function library.
. /etc/init.d/functions

RETVAL=0
SERVICE="syslog"
UDPLISTENPORT=514
SOURCEIP="10.0.250.131"
DESTINATIONS="10.0.250.132/514 10.0.250.61/514";
PIDFILE="samplicator-$SERVICE.pid"

start() {
  echo -n $"Starting up samplicator-$SERVICE daemon: "
  daemon --check samplicator-$SERVICE /usr/bin/samplicate -f -p $UDPLISTENPORT -s $SOURCEIP -b 8388608 -S $DESTINATIONS; netstat -upan | grep -P "$SOURCEIP:$UDPLISTENPORT.*samplicate" | perl -e 'my $line=; $line=~/([0-9]+)\//; print "$1\n";' >/var/run/$PIDFILE
  RETVAL=$?
  [ $RETVAL -eq 0 ] && touch /var/lock/subsys/samplicator-$SERVICE
  echo
  return $RETVAL
}

stop() {
  echo -n $"Shutting down samplicator-$SERVICE daemon: "
  killproc samplicator-$SERVICE
  RETVAL=$?
  [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/samplicator-$SERVICE
  echo
  return $RETVAL
}

dostatus() {
  #determine if the program is running based on the UDP port
  STATUS=`netstat -upan | grep -P "$SOURCEIP:$UDPLISTENPORT.*samplicate"|wc -l`;
  if [ $STATUS -eq 1 ]; then
    echo "samplicator-$SERVICE is running, bound on UDP port $UDPLISTENPORT";
  else
  #           echo "samplicator-$SERVICE is not running.";
    status samplicator-$SERVICE
  fi
}

restart() {
  stop
  start
}

condrestart() {
  [ -e /var/lock/subsys/samplicator-$SERVICE ] && restart || :
}

# See how we were called.
  case "$1" in
  start)
    start
  ;;
  stop)
    stop
  ;;
  status)
    dostatus
  ;;
  restart|reload)
    restart
  ;;
  condrestart)
    condrestart
  ;;
  *)
    echo $"Usage: samplicator-$SERVICE {start|stop|status|restart|reload|condrestart}"
    exit 1
  esac

exit $RETVAL 
 
The above example is for a syslog fan-out daemon. It can be easily changed to a snmp-trap or netflow fanout startup script by changing the variables at the top. Multiple concurrent daemons can run on different ports.

Good luck