/rc6.d/K20ntop

http://github.com/brinkman83/bashrc · Shell · 146 lines · 112 code · 11 blank · 23 comment · 21 complexity · 2e1befcc986c105d30faf23b01377724 MD5 · raw file

  1. #! /bin/sh
  2. ### BEGIN INIT INFO
  3. # Provides: ntop
  4. # Required-Start: $remote_fs $syslog
  5. # Required-Stop: $remote_fs $syslog
  6. # Default-Start: 2 3 4 5
  7. # Default-Stop: 0 1 6
  8. ### END INIT INFO
  9. DAEMON="/usr/sbin/ntop"
  10. NAME="ntop"
  11. DESC="network top daemon"
  12. INIT="/etc/default/ntop"
  13. HOMEDIR="/var/lib/ntop"
  14. LOGDIR="/var/log/ntop"
  15. # Workaround for a rrd problem, see #471862.
  16. export LANG=C
  17. # end of workaround
  18. test -f $DAEMON || exit 0
  19. test -f $INIT || exit 0
  20. . $INIT
  21. sanity_check() {
  22. # Sanity check, we expect USER And INTERFACES to be defined
  23. # (we could also set defaults for them before calling INIT...)
  24. if [ -z "$USER" ] ; then
  25. echo -n "ERROR: Cannot start ntop since USER is not defined, check the configuration file $INIT" >&2
  26. return 1
  27. fi
  28. if [ -z "$INTERFACES" ] ; then
  29. echo "ERROR: Cannot start ntop since INTERFACES is not defined, check the configuration file $INIT" >&2
  30. return 1
  31. fi
  32. return 0
  33. }
  34. check_log_dir() {
  35. # Does the logging directory belong to the User running the application
  36. # If we cannot determine the logdir return without error
  37. # (we will not check it)
  38. [ -n "$LOGDIR" ] || return 0
  39. [ -n "$USER" ] || return 0
  40. if [ ! -e "$LOGDIR" ] ; then
  41. echo -n "ERR: logging directory $LOGDIR does not exist"
  42. return 1
  43. elif [ ! -d "$LOGDIR" ] ; then
  44. echo -n "ERR: logging directory $LOGDIR does not exist"
  45. return 1
  46. else
  47. real_log_user=`stat -c %U $LOGDIR`
  48. # An alternative way is to check if the the user can create
  49. # a file there...
  50. if [ "$real_log_user" != "$USER" ] ; then
  51. echo -n "ERR: logging directory $LOGDIR does not belong
  52. to the user $USER"
  53. return 1
  54. fi
  55. fi
  56. return 0
  57. }
  58. check_interfaces() {
  59. # Check the interface status, abort with error if a configured one is not
  60. # available
  61. [ -z "$INTERFACES" ] && return 0
  62. { echo $INTERFACES | awk -F , '{ for(i=1;i<=NF;i++) print $i }' |
  63. while read iface ; do
  64. if ! ifconfig "$iface" | grep -w UP >/dev/null; then
  65. echo "ERR: interface $iface is DOWN..."
  66. return 1
  67. fi
  68. done
  69. return 0
  70. }
  71. return $?
  72. }
  73. case "$1" in
  74. start)
  75. if pidof $DAEMON > /dev/null ; then
  76. echo "Not starting $DESC, it has already been started."
  77. exit 0
  78. fi
  79. echo -n "Starting $DESC: "
  80. if ! sanity_check || ! check_log_dir || ! check_interfaces; then
  81. echo " will not start $DESC!"
  82. exit 1
  83. fi
  84. start-stop-daemon --start --quiet --name $NAME --exec $DAEMON -- \
  85. -d -L -u $USER -P $HOMEDIR \
  86. --access-log-file=$LOGDIR/access.log -i "$INTERFACES" \
  87. -p /etc/ntop/protocol.list \
  88. -O $LOGDIR $GETOPT
  89. if pidof $DAEMON > /dev/null ; then
  90. echo ntop
  91. else
  92. # WARNING: This might introduce a race condition in some (fast) systems
  93. # Wait for a sec an try again
  94. sleep 1
  95. if ps xa | grep -v grep | grep $DAEMON > /dev/null ; then
  96. echo ntop
  97. else
  98. echo "ntop not started. Read /usr/share/doc/ntop/README.Debian."
  99. exit 1
  100. fi
  101. fi
  102. ;;
  103. stop)
  104. echo -n "Stopping $DESC: "
  105. start-stop-daemon --stop --oknodo --name $NAME --user $USER --retry 9
  106. if pidof $DAEMON > /dev/null ; then
  107. # WARNING: This might introduce a race condition in some (fast) systems
  108. # Wait for a sec an try again
  109. sleep 1
  110. if ps xa | grep -v grep | grep $DAEMON > /dev/null ; then
  111. echo "Ntop not stopped. Need to kill manually."
  112. exit 1
  113. else
  114. echo ntop
  115. fi
  116. else
  117. echo ntop
  118. fi
  119. ;;
  120. restart | force-reload)
  121. $0 stop
  122. sleep 2
  123. $0 start
  124. ;;
  125. reload)
  126. echo "reload option not implemented"
  127. exit 3
  128. ;;
  129. *)
  130. N=/etc/init.d/$NAME
  131. echo "Usage: $N {start|stop|restart|force-reload|reload}" >&2
  132. exit 1
  133. ;;
  134. esac
  135. exit 0