170 lines
5.4 KiB
Bash
Executable File
170 lines
5.4 KiB
Bash
Executable File
#! /bin/sh -e
|
|
#
|
|
# Copyright 2015 Mirantis, Inc.
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
# not use this file except in compliance with the License. You may obtain
|
|
# a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
# License for the specific language governing permissions and limitations
|
|
# under the License.
|
|
#
|
|
### BEGIN INIT INFO
|
|
# Provides: snmpd snmptrapd
|
|
# Required-Start: $network $remote_fs $syslog
|
|
# Required-Stop: $network $remote_fs $syslog
|
|
# Default-Start: 2 3 4 5
|
|
# Default-Stop: 0 1 6
|
|
# Short-Description: SNMP agents
|
|
# Description: NET SNMP (Simple Network Management Protocol) Agents
|
|
### END INIT INFO
|
|
#
|
|
# Author: Jochen Friedrich <jochen@scram.de>
|
|
# Modified by: Piotr Misiak <pmisiak@mirantis.com> (network namespaces support added)
|
|
#
|
|
set -e
|
|
|
|
. /lib/lsb/init-functions
|
|
|
|
export PATH=/sbin:/usr/sbin:/bin:/usr/bin
|
|
|
|
# Defaults
|
|
OLD_MIBS_DIR="/usr/share/mibs/site:/usr/share/snmp/mibs:/usr/share/mibs/iana:/usr/share/mibs/ietf:/usr/share/mibs/netsnmp"
|
|
MIBS_DIR="/usr/share/snmp/mibs:/usr/share/snmp/mibs/iana:/usr/share/snmp/mibs/ietf"
|
|
export MIBDIRS="$MIBS_DIR:$OLD_MIBS_DIR"
|
|
|
|
IP_BINARY="/sbin/ip"
|
|
|
|
SNMPD_BINARY="/usr/sbin/snmpd"
|
|
SNMPD_PID="/run/snmpd.pid"
|
|
#run snmpd process in the following namespace:
|
|
SNMPDNS="zabbix"
|
|
SNMPDRUN="yes"
|
|
SNMPDOPTS="-Lsd -Lf /dev/null -p $SNMPD_PID"
|
|
|
|
TRAPD_BINARY="/usr/sbin/snmptrapd"
|
|
TRAPD_PID="/run/snmptrapd.pid"
|
|
#run snmptrapd process in the following namespace:
|
|
TRAPDNS="zabbix"
|
|
TRAPDRUN="no"
|
|
TRAPDOPTS="-Lsd -p $TRAPD_PID"
|
|
|
|
# Reads config file (will override defaults above)
|
|
[ -r /etc/default/snmpd ] && . /etc/default/snmpd
|
|
|
|
test -x $SNMPD_BINARY || exit 0
|
|
test -x $TRAPD_BINARY || exit 0
|
|
|
|
create_ns_if_not_exists() {
|
|
[ -n "$1" ] || return 0
|
|
exists=`$IP_BINARY netns | egrep "^$1\$|^$1 " | wc -l`
|
|
[ $exists -le 0 ] || return 0
|
|
$IP_BINARY netns add "$1"
|
|
}
|
|
|
|
if [ "x$SNMPDNS" != "x" ]; then
|
|
create_ns_if_not_exists "$SNMPDNS"
|
|
SNMPDOPTS="netns exec $SNMPDNS $SNMPD_BINARY $SNMPDOPTS"
|
|
SNMPDEXEC=$IP_BINARY
|
|
else
|
|
SNMPDEXEC=$SNMPD_BINARY
|
|
fi
|
|
|
|
if [ "x$TRAPDNS" != "x" ]; then
|
|
create_ns_if_not_exists "$TRAPDNS"
|
|
TRAPDOPTS="netns exec $TRAPDNS $TRAPD_BINARY $TRAPDOPTS"
|
|
TRAPDEXEC=$IP_BINARY
|
|
else
|
|
TRAPDEXEC=$TRAPD_BINARY
|
|
fi
|
|
|
|
# Cd to / before starting any daemons.
|
|
cd /
|
|
|
|
case "$1" in
|
|
start)
|
|
log_daemon_msg "Starting network management services:"
|
|
# remove old symlink with previous version
|
|
if [ -L /var/run/agentx ]; then
|
|
rm -f /var/run/agentx
|
|
fi
|
|
if [ ! -d /var/run/agentx ]; then
|
|
mkdir -p /var/run/agentx
|
|
fi
|
|
if [ "$SNMPDRUN" = "yes" -a -f /etc/snmp/snmpd.conf ]; then
|
|
start-stop-daemon --quiet --start --oknodo --exec $SNMPDEXEC -- $SNMPDOPTS
|
|
log_progress_msg " snmpd"
|
|
fi
|
|
if [ "$TRAPDRUN" = "yes" -a -f /etc/snmp/snmptrapd.conf ]; then
|
|
start-stop-daemon --quiet --start --oknodo --exec $TRAPDEXEC -- $TRAPDOPTS
|
|
log_progress_msg " snmptrapd"
|
|
fi
|
|
;;
|
|
stop)
|
|
log_daemon_msg "Stopping network management services:"
|
|
start-stop-daemon --quiet --stop --oknodo --exec $SNMPD_BINARY && [ ! -f $SNMPD_PID ] || rm $SNMPD_PID
|
|
log_progress_msg " snmpd"
|
|
start-stop-daemon --quiet --stop --oknodo --exec $TRAPD_BINARY && [ ! -f $TRAPD_PID ] || rm $TRAPD_PID
|
|
log_progress_msg " snmptrapd"
|
|
;;
|
|
restart)
|
|
log_daemon_msg "Restarting network management services:"
|
|
start-stop-daemon --quiet --stop --oknodo --exec $SNMPD_BINARY && [ ! -f $SNMPD_PID ] || rm $SNMPD_PID
|
|
start-stop-daemon --quiet --stop --oknodo --exec $TRAPD_BINARY && [ ! -f $TRAPD_PID ] || rm $TRAPD_PID
|
|
# Allow the daemons time to exit completely.
|
|
set +e
|
|
for i in `seq 0 10`; do
|
|
pgrep -f $SNMPD_BINARY >/dev/null
|
|
SNMPDSTATUS=$?
|
|
pgrep -f $TRAPD_BINARY >/dev/null
|
|
TRAPDSTATUS=$?
|
|
if [ $SNMPDSTATUS -eq 1 -a $TRAPDSTATUS -eq 1 ]; then
|
|
# both processes are not running
|
|
break
|
|
fi
|
|
if [ $i -eq 10 ]; then
|
|
log_failure_msg "ERROR: Network management services didn't stop within 10 seconds, exiting..."
|
|
exit 1
|
|
fi
|
|
sleep 1
|
|
done
|
|
set -e
|
|
if [ "$SNMPDRUN" = "yes" -a -f /etc/snmp/snmpd.conf ]; then
|
|
start-stop-daemon --quiet --start --exec $SNMPDEXEC -- $SNMPDOPTS
|
|
log_progress_msg " snmpd"
|
|
fi
|
|
if [ "$TRAPDRUN" = "yes" -a -f /etc/snmp/snmptrapd.conf ]; then
|
|
start-stop-daemon --quiet --start --exec $TRAPDEXEC -- $TRAPDOPTS
|
|
log_progress_msg " snmptrapd"
|
|
fi
|
|
;;
|
|
reload|force-reload)
|
|
log_daemon_msg "Reloading network management services:"
|
|
if [ "$SNMPDRUN" = "yes" -a -f /etc/snmp/snmpd.conf ]; then
|
|
start-stop-daemon --quiet --stop --signal 1 \
|
|
--pidfile /var/run/snmpd.pid --exec $SNMPD_BINARY
|
|
log_progress_msg " snmpd"
|
|
fi
|
|
;;
|
|
status)
|
|
status=0
|
|
if [ "$SNMPDRUN" = "yes" -a -f /etc/snmp/snmpd.conf ]; then
|
|
status_of_proc $SNMPD_BINARY snmpd || status=$?
|
|
fi
|
|
if [ "$TRAPDRUN" = "yes" -a -f /etc/snmp/snmptrapd.conf ]; then
|
|
status_of_proc $TRAPD_BINARY snmptrapd || status=$?
|
|
fi
|
|
exit $status
|
|
;;
|
|
*)
|
|
echo "Usage: /etc/init.d/snmpd {start|stop|restart|reload|force-reload|status}"
|
|
exit 1
|
|
esac
|
|
|
|
exit 0
|