Rev 34 | Blame | Compare with Previous | Last modification | View Log | RSS feed
#!/bin/bash## netconsole This loads the netconsole module with the configured parameters.## chkconfig: - 11 89# description: Initializes network console logging# config: /etc/sysconfig/netconsole## Copyright 2002 Red Hat, Inc.## Based in part on a shell script by# Andreas Dilger <adilger@turbolinux.com> Sep 26, 2001PATH=/sbin:/usr/sbin:$PATHRETVAL=0SERVER_ADDRESS_RESOLUTION=# Check that networking is up.. /etc/sysconfig/network# Source function library.. /etc/rc.d/init.d/functions# Default valuesLOCALPORT=6666DEV=SYSLOGADDR=SYSLOGPORT=514SYSLOGMACADDR=kernel=$(uname -r | cut -d. -f1-2)usage (){echo $"Usage: $0 {start|stop|status|restart|condrestart}" 1>&2RETVAL=2}print_address_info (){local host=$1local route via targetroute=$(LANG=C ip -o route get to $host/32)[ -z "$DEV" ] && DEV=$(echo $route | sed "s|.* dev \([^ ]*\).*|\1|")echo "DEV=$DEV"echo "LOCALADDR=$(echo $route | sed "s|.* src \([^ ]*\).*|\1|")"if [[ $route == *" via "* ]] ; thenvia=$(echo $route | sed "s|.* via \([^ ]*\).*|\1|")target=$viaelsetarget=$hostfiif [ -z "$SYSLOGMACADDR" ]; thenarp=$(LANG=C /sbin/arping -f -c 1 -I $DEV $target 2>/dev/null | awk '/ reply from .*[.*]/ { print gensub(".* reply from .* \\[(.*)\\].*","\\1","G"); exit }')[ -n "$arp" ] && echo "SYSLOGMACADDR=$arp"fi}start (){[ -f /etc/sysconfig/netconsole ] || exit 6. /etc/sysconfig/netconsoleSYSLOGOPTS=# syslogd server, if anyif [ -n "$SYSLOGADDR" ]; then# IPv6 regex also covers 4to6, zero-compressed, and link-local addresses with zone-index addresses.# It should also cover IPv4-embedded, IPv4-mapped, and IPv4-translated IPv6 addresses.# Taken from: http://stackoverflow.com/a/17871737/3481531IPv4_regex="^([0-9]{1,3}\.){3}[0-9]{1,3}$"IPv6_regex="^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$"if ! [[ "$SYSLOGADDR" =~ $IPv4_regex ]] && ! [[ "$SYSLOGADDR" =~ $IPv6_regex ]]; then# Use IPv4 by default:SYSLOGADDR="$(LANG=C getent ahostsv4 $SYSLOGADDR 2> /dev/null)"# Try IPv6 in case IPv4 resolution has failed:if [[ $? -eq 2 ]]; thenSYSLOGADDR="$(LANG=C getent ahostsv6 $SYSLOGADDR 2> /dev/null)"fiif [[ $? -ne 0 ]]; thenecho $"Unable to resolve IP address specified in /etc/sysconfig/netconsole" 1>&2exit 6fiSYSLOGADDR="$(echo "$SYSLOGADDR" | head -1 | cut --delimiter=' ' --fields=1)"fifiif [ -z "$SYSLOGADDR" ] ; thenecho $"Server address not specified in /etc/sysconfig/netconsole" 1>&2exit 6fieval $(print_address_info $SYSLOGADDR)if [ -z "$SYSLOGMACADDR" ]; thenecho $"netconsole: can't resolve MAC address of $SYSLOGADDR" 1>&2exit 1fiSYSLOGOPTS="netconsole=$LOCALPORT@$LOCALADDR/$DEV,$SYSLOGPORT@$SYSLOGADDR/$SYSLOGMACADDR "/usr/bin/logger -p daemon.info -t netconsole: inserting netconsole module with arguments \$SYSLOGOPTSif [ -n "$SYSLOGOPTS" ]; thenaction $"Initializing netconsole" modprobe netconsole \$SYSLOGOPTS[ "$?" != "0" ] && RETVAL=1fitouch /var/lock/subsys/netconsole}stop (){if /sbin/lsmod | grep netconsole >/dev/null 2>&1 ; thenaction $"Disabling netconsole" rmmod netconsole;[ "$?" != "0" ] && RETVAL=1firm -f /var/lock/subsys/netconsole}status (){if /sbin/lsmod | grep netconsole >/dev/null 2>&1 ; thenecho $"netconsole module loaded"RETVAL=0elseecho $"netconsole module not loaded"RETVAL=3fi}restart (){stopstart}condrestart (){[ -e /var/lock/subsys/netconsole ] && restart}case "$1" instop) stop ;;status) status ;;start|restart|reload|force-reload) restart ;;condrestart) condrestart ;;*) usage ;;esacexit $RETVAL