LogoopenSUSE Build Service > Projects
Sign Up | Log In

View File rc.snmpd of Package net-snmp (Project openSUSE:Factory)

#! /bin/sh
# Copyright (c) 1996-2011 SuSE GmbH Nuernberg, Germany.  All rights reserved.
#
# Author: Christopher Mahmood, Remo Behn
#
# /etc/init.d/snmpd
#
### BEGIN INIT INFO
# Provides:            net-snmp snmp
# Required-Start:      $remote_fs
# Should-Start:        $syslog $network
# Required-Stop:       $remote_fs
# Should-Stop:         $syslog $network
# Default-Start:       2 3 5
# Default-Stop:        0 1 6
# Description:         Net-SNMP agent
# Short-Description:   Net-SNMP agent
### END INIT INFO

SNMPD=/usr/sbin/snmpd
AGENTDIR=/usr/lib/net-snmp/agents
SNMPDCONF=/etc/snmp/snmpd.conf
SNMPD_PID=/var/run/snmpd.pid

test -x $SNMPD || exit 5

# Check for existence of needed config file and read it
SNMPD_CONFIG=/etc/sysconfig/net-snmp
test -r $SNMPD_CONFIG || { echo "$SNMPD_CONFIG not existing";
        if [ "$1" = "stop" ]; then exit 0;
        else exit 6; fi; }

# Read config
. $SNMPD_CONFIG

# Shell functions sourced from /etc/rc.status:
#      rc_check         check and set local and overall rc status
#      rc_status        check and set local and overall rc status
#      rc_status -v     ditto but be verbose in local rc status
#      rc_status -v -r  ditto and clear the local rc status
#      rc_failed        set local and overall rc status to failed
#      rc_failed <num>  set local and overall rc status to <num><num>
#      rc_reset         clear local rc status (overall remains)
#      rc_exit          exit appropriate to overall rc status
. /etc/rc.status

# First reset status of this service
rc_reset

# Return values acc. to LSB for all commands but status:
# 0 - success
# 1 - generic or unspecified error
# 2 - invalid or excess argument(s)
# 3 - unimplemented feature (e.g. "reload")
# 4 - insufficient privilege
# 5 - program is not installed
# 6 - program is not configured
# 7 - program is not running
#
# Note that starting an already running service, stopping
# or restarting a not-running service as well as the restart
# with force-reload (in case signalling is not supported) are
# considered a success.

# check whether to enable agentx support and get list of installed
# agents
get_agents()
{
  agents=
  agentargs=''
  for agent in $AGENTDIR/*; do
    test -x $agent || continue
    agents="$agents $agent"
    agentargs="-x /var/run/agentx/master"
  done
}

case "$1" in
    start)
        echo -n "Starting snmpd"
        #
        # do not even try to start if the log file is (2GB-1MB) big.
        # the snmpd doesnt handle LFS properly
        #
        SNMPD_LOGFILE="${SNMPD_LOGFILE:-/var/log/net-snmpd.log}"
        if [ -e "$SNMPD_LOGFILE" ] ; then
        SNMPD_SIZE_LOGFILE="$(stat -c "%s" $SNMPD_LOGFILE)"
        SNMPD_SIZE_MAX="1073741824"
        if [ $SNMPD_SIZE_LOGFILE -gt $SNMPD_SIZE_MAX ] ; then
            echo -e "\n\"$SNMPD_LOGFILE\" is too large. Running 'logrotate' for net-snmp." >&2
            /usr/sbin/logrotate /etc/logrotate.d/net-snmp
        fi
        fi
        get_agents
        # Don't add -c /etc/snmp/snmpd.conf, as it is loaded by default.
        if [ $SNMPDCONF = "/etc/snmp/snmpd.conf" ]; then
          SNMPDCONF=""
        else
          SNMPDCONF="-c $SNMPDCONF"
        fi
        case "${SNMPD_USE_SMUX:-yes}" in
          [Nn][Oo])
            SNMPDNOSMUX="-I -smux"
          ;;
        esac
        case "${SNMPD_LOG_SUCCESSFUL_CONNECTIONS:-yes}" in
          [Nn][Oo])
            SNMPDNOTCPWRAPPERLOG="--dontLogTCPWrappersConnects"
          ;;
        esac
        # /var/run can now be mounted as tmpfs
        if [ ! -d /var/run/agentx ]; then
          mkdir -m 0700 /var/run/agentx
        fi
        startproc $SNMPD $SNMPDCONF -r -A -LF ${SNMPD_LOGLEVEL:-n} $SNMPD_LOGFILE -p $SNMPD_PID $agentargs $SNMPDNOSMUX $SNMPDNOTCPWRAPPERLOG
        rc_status -v
        rc_reset

        # start all agents
        if test -n "$agents"; then
          usleep 300000
          for agent in $agents; do
            echo -en "\tStarting `basename $agent`"
            startproc -t1 $agent
            rc_status -v ; rc_reset
          done
        fi
        ;;
    stop)
        echo -n "Shutting down snmpd:"
        killproc -TERM $SNMPD
        rc_status -v ; rc_reset
        # we also need to make sure all agents die
        if test -n "$agents"; then
          for agent in $AGENTDIR/*; do
           test -x $agent || continue
           echo -ne "\tShutting down `basename $agent`:"
           killproc $agent
           rc_status -v ; rc_reset
        done
        fi
        ;;
    try-restart)
        $0 status >/dev/null &&  $0 restart
        rc_status
        ;;
    restart)
        $0 stop
        $0 start
        rc_status
        ;;
    force-reload)
        echo -n "Reload service snmpd:"
        killproc -HUP $SNMPD
        rc_status -v
        ;;
    reload)
        echo -n "Reload service snmpd:"
        killproc -HUP $SNMPD
        rc_status -v
        ;;
    status)
        echo -n "Checking for service snmpd:"
        checkproc $SNMPD
        rc_status -v
        get_agents
        if test -n "$agents"; then
          echo -e "Checking for agents:"

          for agent in $agents; do
            echo -en "\t`basename $agent`"
            checkproc $agent
            rc_status -v
      done
        fi
        ;;
    *)
        echo "Usage: $0 {start|stop|try-restart|restart|force-reload|reload|status}"
        exit 1
        ;;
esac
rc_exit