Guest User

Untitled

a guest
Mar 26th, 2012
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 5.22 KB | None | 0 0
  1. #! /bin/bash
  2.  
  3. ### BEGIN INIT INFO
  4. # Provides:          munin-node
  5. # Required-Start:    $local_fs $remote_fs
  6. # Required-Stop:     $local_fs $remote_fs
  7. # Should-Start:      $all
  8. # Should-Stop:       $all
  9. # Default-Start:     2 3 4 5
  10. # Default-Stop:      0 1 6
  11. # Short-Description: Start/stop Munin-Node
  12. # Description:       Start/stop Munin-Node
  13. ### END INIT INFO
  14.  
  15. PATH=/sbin:/bin:/usr/sbin:/usr/bin
  16. DAEMON=/usr/sbin/munin-node
  17. PIDFILE=/var/run/munin/munin-node.pid
  18. CONFFILE=/etc/munin/munin-node.conf
  19.  
  20. # log_daemon_msg() and log_progress_msg() isn't present in present in Sarge.
  21. # Below is a copy of them from lsb-base 3.0-5, for the convenience of back-
  22. # porters.  If the installed version of lsb-base provides these functions,
  23. # they will be used instead.
  24.  
  25. log_daemon_msg () {
  26.     if [ -z "$1" ]; then
  27.         return 1
  28.     fi
  29.  
  30.     if [ -z "$2" ]; then
  31.         echo -n "$1:"
  32.         return
  33.     fi
  34.    
  35.     echo -n "$1: $2"
  36. }
  37.  
  38. log_progress_msg () {
  39.     if [ -z "$1" ]; then
  40.         return 1
  41.     fi
  42.     echo -n " $@"
  43. }
  44.  
  45. . /lib/lsb/init-functions
  46. [ -r /etc/default/munin-node ] && . /etc/default/munin-node
  47.  
  48. if [ ! -x $DAEMON ]; then
  49.     log_failure_msg "Munin-Node appears to be uninstalled."
  50.     exit 5
  51. elif [ ! -e $CONFFILE ]; then
  52.     log_failure_msg "Munin-Node appears to be unconfigured."
  53.     exit 6
  54. fi
  55.  
  56. # Figure out if the pid file is in a non-standard location
  57. while read line; do
  58.     line=${line%%\#*} # get rid of comments
  59.     set -f
  60.     line=$(echo $line) # get rid of extraneous blanks
  61.     set +f
  62.     if [ "$line" != "${line#pid_file }" ]; then
  63.         PIDFILE=${line#pid_file }
  64.     fi
  65. done < $CONFFILE
  66.  
  67. verify_superuser() {
  68.     action=$1
  69.     [ $EUID -eq 0 ] && return
  70.     log_failure_msg "Superuser privileges required for the" \
  71.             "\"$action\" action."
  72.     exit 4
  73. }
  74.  
  75. start() {
  76.     log_daemon_msg "Starting Munin-Node"
  77.     mkdir -p /var/run/munin
  78.     chown munin:root /var/run/munin
  79.     chmod 0755 /var/run/munin
  80.     if pidofproc -p $PIDFILE $DAEMON >/dev/null; then
  81.         log_progress_msg "started beforehand"
  82.         log_end_msg 0
  83.         exit 0
  84.     fi
  85.     start_daemon -p $PIDFILE $DAEMON $DAEMON_ARGS
  86.     ret=$?
  87.     # start_daemon() isn't thorough enough, ensure the daemon has been
  88.     # started manually
  89.     attempts=0
  90.     until pidofproc -p $PIDFILE $DAEMON >/dev/null; do
  91.         attempts=$(( $attempts + 1 ))
  92.         sleep 0.05
  93.         [ $attempts -lt 20 ] && continue
  94.         log_end_msg 1
  95.         return 1
  96.     done
  97.     [ $ret -eq 0 ] && log_progress_msg "done"
  98.     log_end_msg $ret
  99.     return $ret
  100. }
  101.  
  102. stop() {
  103.     log_daemon_msg "Stopping Munin-Node"
  104.     # killproc() doesn't try hard enough if the pid file is missing,
  105.     # so create it is gone and the daemon is still running
  106.     if [ ! -r $PIDFILE ]; then
  107.         pid=$(pidofproc -p $PIDFILE $DAEMON)
  108.         if [ -z "$pid" ]; then
  109.             log_progress_msg "stopped beforehand"
  110.             log_end_msg 0
  111.             return 0
  112.         fi
  113.         echo $pid 2>/dev/null > $PIDFILE
  114.         if [ $? -ne 0 ]; then
  115.             log_end_msg 1
  116.             return 1
  117.         fi
  118.     fi
  119.     killproc -p $PIDFILE /usr/bin/munin-node
  120.     ret=$?
  121.     # killproc() isn't thorough enough, ensure the daemon has been
  122.     # stopped manually
  123.     attempts=0
  124.     until ! pidofproc -p $PIDFILE $DAEMON >/dev/null; do
  125.         attempts=$(( $attempts + 1 ))
  126.         sleep 0.05
  127.         [ $attempts -lt 20 ] && continue
  128.         log_end_msg 1
  129.         return 1
  130.     done
  131.     [ $ret -eq 0 ] && log_progress_msg "done"
  132.     log_end_msg $ret
  133.     return $ret
  134. }
  135.  
  136. if [ "$#" -ne 1 ]; then
  137.     log_failure_msg "Usage: /etc/init.d/munin-node" \
  138.             "{start|stop|restart|force-reload|try-restart}"
  139.     exit 2
  140. fi
  141.  
  142. case "$1" in
  143.   start)
  144.     verify_superuser $1
  145.     start
  146.     exit $?
  147.     ;;
  148.   stop)
  149.     verify_superuser $1
  150.     stop
  151.     exit $?
  152.     ;;
  153.   restart|force-reload)
  154.     verify_superuser $1
  155.     stop || exit $?
  156.     start
  157.     exit $?
  158.     ;;
  159.   try-restart)
  160.     verify_superuser $1
  161.     pidofproc -p $PIDFILE $DAEMON >/dev/null
  162.     if [ $? -eq 0 ]; then
  163.         stop || exit $?
  164.         start
  165.         exit $?
  166.     fi
  167.     log_success_msg "Munin-Node was stopped beforehand and thus not" \
  168.             "restarted."
  169.     exit 0
  170.     ;;
  171.   reload)
  172.     log_failure_msg "The \"reload\" action is not implemented."
  173.     exit 3
  174.     ;;
  175.   status)
  176.     pid=$(pidofproc -p $PIDFILE $DAEMON)
  177.     ret=$?
  178.     pid=${pid% } # pidofproc() supplies a trailing space, strip it
  179.     if [ $ret -eq 0 ]; then
  180.         log_success_msg "Munin-Node is running (PID: $pid)"
  181.         exit 0
  182.     # the LSB specifies that I in this case (daemon dead + pid file exists)
  183.     # should return 1, however lsb-base returned 2 in this case up to and
  184.     # including version 3.1-10 (cf. #381684).  Since that bug is present
  185.     # in Sarge, Ubuntu Dapper, and (at the time of writing) Ubuntu Etch,
  186.     # and taking into account that later versions of pidofproc() do not
  187.     # under any circumstance return 2, I'll keep understanding invalid
  188.     # return code for the time being, even though the LSB specifies it is
  189.     # to be used for the situation where the "program is dead and /var/lock
  190.     # lock file exists".  
  191.     elif [ $ret -eq 1 ] || [ $ret -eq 2 ]; then
  192.         log_failure_msg "Munin-Node is dead, although $PIDFILE exists."
  193.         exit 1
  194.     elif [ $ret -eq 3 ]; then
  195.         log_warning_msg "Munin-Node is not running."
  196.         exit 3
  197.     fi
  198.     log_warning_msg "Munin-Node status unknown."
  199.     exit 4
  200.         ;;
  201.   *)
  202.     log_failure_msg "Usage: /etc/init.d/munin-node" \
  203.             "{start|stop|restart|force-reload|try-restart}"
  204.     exit 2
  205.     ;;
  206. esac
  207.  
  208. log_failure_msg "Unexpected failure, please file a bug."
  209. exit 1
Advertisement
Add Comment
Please, Sign In to add comment