lldpd.sh 3.23 KB
Newer Older
1 2 3 4 5
#!/bin/sh
#
# Controls lldpd.
#

6 7
LLDPD_CONFIG=/etc/lldpd.conf
LLDPD=/usr/sbin/lldpd
8
LLDPD_CLI=/usr/sbin/lldpcli
9 10
# -x -- Enable SNMP subagent.
LLDPD_OPT=-x
11
MONIT=/usr/bin/monit
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

dotconfig=/wr/etc/dot-config


start_counter() {
    # increase start counter
    COUNTER_FILE="/tmp/start_cnt_lldpd"
    START_COUNTER=1
    if [ -f "$COUNTER_FILE" ];
    then
	read -r START_COUNTER < $COUNTER_FILE
	START_COUNTER=$((START_COUNTER+1))
    fi
    echo "$START_COUNTER" > $COUNTER_FILE
}

start() {


    echo -n "Starting lldpd: "
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48

    if [ -f $dotconfig ]; then
	. $dotconfig
    else
	echo "$0 unable to source dot-config ($dotconfig)!"
    fi

    if [ "$CONFIG_LLDPD_DISABLE" = "y" ]; then
	echo "lldpd disabled in dot-config!"
	if [ "$1" != "force" ]; then
	    # Unmonitor lldpd, ignore all printouts from monit.
	    # Run in background since monit may wait for a timeout.
	    $MONIT unmonitor lldpd &>/dev/null &
	    exit 0
	fi
	echo -n "Force start of lldpd: "
    fi
49

50 51 52 53
    # Creating lldpd config
    echo "# This configuration file was generated by $0 at startup" > $LLDPD_CONFIG
    echo "# If you would like to change a lldpd configuration in runtime please consider using lldpcli" >> $LLDPD_CONFIG
    echo "configure system hostname '$(hostname)'" >> $LLDPD_CONFIG
54 55 56 57 58 59 60 61 62 63 64 65

    tx_interval=5
    if [ ! -z "$CONFIG_LLDPD_TX_INTERVAL" ]; then
	tx_int=$CONFIG_LLDPD_TX_INTERVAL
	# check if given value is an int
	case $tx_int in
	    ''|*[!0-9]*) echo -n "wrong CONFIG_LLDPD_TX_INTERVAL "$tx_int". Using 5 as the default interval. " ;;
	    *) echo tx_interval=$tx_int ;;
	esac
    fi
    echo "configure lldp tx-interval $tx_interval" >> $LLDPD_CONFIG

66 67
    if [ "$CONFIG_LLDPD_MINIMUM_FRAME_SIZE" = "y" ]; then 
	echo "configure system description  'WR-SWITCH'" >> $LLDPD_CONFIG
68 69 70 71
	# disable capabilities-advertisements
	echo "unconfigure ports all lldp capabilities-advertisements" >> $LLDPD_CONFIG
	# disable management ip advertisements
	echo "unconfigure ports all lldp management-addresses-advertisements" >> $LLDPD_CONFIG
72 73 74
    else
	echo "configure system description  'WR-SWITCH: $(/wr/bin/wrsw_version)'" >> $LLDPD_CONFIG
    fi
75 76 77 78 79
    if [ "$CONFIG_LLDPD_MANAGEMENT_PORT_DISABLE" = "y" ]; then
	echo "configure system interface pattern '!eth*'" >> $LLDPD_CONFIG
    fi
    echo "resume" >> $LLDPD_CONFIG

80 81 82 83 84 85 86 87 88 89
    start-stop-daemon -S -q -p /var/run/lldpd.pid --exec $LLDPD -- $LLDPD_OPT
    ret=$?
    if [ $ret -eq 0 ]; then
	start_counter
	echo "OK"
    elif [ $ret -eq 1 ]; then
	echo "Failed (already running?)"
    else
	echo "Failed"
    fi
90 91 92 93 94 95 96 97 98

    # check whether the process was monitored
    $MONIT summary 2>&1 | grep lldpd | grep "Not monitored" &> /dev/null
    if [ $? -eq 0 ]; then
	echo "lldpd was not monitored, enabling monitoring"
	# the process was not monitored, enable monitoring
	# this will generate extra log entries from monit
	$MONIT monitor lldpd
    fi
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
}

stop() {
    echo -n "Stopping lldpd: "
    start-stop-daemon -K -q -p /var/run/lldpd.pid
    if [ $? -eq 0 ]; then
	echo "OK"
    else
	echo "Failed"
    fi
}

restart() {
    stop
    start
}

case "$1" in
  start)
118
	start "$2"
119
	;;
120 121
  stop)
	stop
122
	;;
123 124
  restart|reload)
	restart
125
	;;
126
  *)
127 128
	echo "Usage: $0 {start <force>|stop|restart}"
	echo "    start force -- enable lldpd even it is disabled in the dot-config"
129 130 131
	exit 1
	;;
esac