Subversion Repositories configs

Rev

Rev 34 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
4 - 1
#!/bin/bash
2
#
3
# netconsole 	This loads the netconsole module with the configured parameters.
4
#
34 - 5
# chkconfig: - 11 89
4 - 6
# description: Initializes network console logging
7
# config: /etc/sysconfig/netconsole
8
#
9
# Copyright 2002 Red Hat, Inc.
10
#
11
# Based in part on a shell script by
12
# Andreas Dilger <adilger@turbolinux.com>  Sep 26, 2001
13
 
14
PATH=/sbin:/usr/sbin:$PATH
15
RETVAL=0
16
SERVER_ADDRESS_RESOLUTION=
17
 
18
# Check that networking is up.
19
. /etc/sysconfig/network
20
 
21
# Source function library.
22
. /etc/rc.d/init.d/functions
23
 
24
# Default values
25
LOCALPORT=6666
26
DEV=
27
 
28
SYSLOGADDR=
29
SYSLOGPORT=514
30
SYSLOGMACADDR=
31
 
32
kernel=$(uname -r | cut -d. -f1-2)
33
 
34
usage ()
35
{
36
	echo $"Usage: $0 {start|stop|status|restart|condrestart}" 1>&2
37
	RETVAL=2
38
}
39
 
40
print_address_info ()
41
{
42
	local host=$1
43
	local route via target
44
 
45
	route=$(LANG=C ip -o route get to $host/32)
46
 
47
	[ -z "$DEV" ] && DEV=$(echo $route | sed "s|.* dev \([^ ]*\).*|\1|")
48
	echo "DEV=$DEV"
49
	echo "LOCALADDR=$(echo $route | sed "s|.* src \([^ ]*\).*|\1|")"
50
	if [[ $route == *" via "* ]] ; then
51
		via=$(echo $route | sed "s|.* via \([^ ]*\).*|\1|")
52
		target=$via
53
	else
54
		target=$host
55
	fi
56
	if [ -z "$SYSLOGMACADDR" ]; then
57
		arp=$(LANG=C /sbin/arping -f -c 1 -I $DEV $target 2>/dev/null | awk '/ reply from .*[.*]/ { print gensub(".* reply from .* \\[(.*)\\].*","\\1","G"); exit }')
58
		[ -n "$arp" ] && echo "SYSLOGMACADDR=$arp"
59
	fi
60
}
61
 
62
start ()
63
{
64
	[ -f /etc/sysconfig/netconsole ] || exit 6
65
	. /etc/sysconfig/netconsole
66
 
67
	SYSLOGOPTS=
68
	# syslogd server, if any
69
	if [ -n "$SYSLOGADDR" ]; then
97 - 70
		# IPv6 regex also covers 4to6, zero-compressed, and link-local addresses with zone-index addresses.
71
		# It should also cover IPv4-embedded, IPv4-mapped, and IPv4-translated IPv6 addresses.
72
		# Taken from: http://stackoverflow.com/a/17871737/3481531
73
		IPv4_regex="^([0-9]{1,3}\.){3}[0-9]{1,3}$"
74
		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]))$"
75
		if ! [[ "$SYSLOGADDR" =~ $IPv4_regex ]] && ! [[ "$SYSLOGADDR" =~ $IPv6_regex ]]; then
76
			# Use IPv4 by default:
77
			SYSLOGADDR="$(LANG=C getent ahostsv4 $SYSLOGADDR 2> /dev/null)"
78
 
79
			# Try IPv6 in case IPv4 resolution has failed:
80
			if [[ $? -eq 2 ]]; then
81
				SYSLOGADDR="$(LANG=C getent ahostsv6 $SYSLOGADDR 2> /dev/null)"
82
			fi
83
 
84
			if [[ $? -ne 0 ]]; then
85
				echo $"Unable to resolve IP address specified in /etc/sysconfig/netconsole" 1>&2
86
				exit 6
87
			fi
88
 
89
			SYSLOGADDR="$(echo "$SYSLOGADDR" | head -1 | cut --delimiter=' ' --fields=1)"
4 - 90
		fi
91
	fi
92
	if [ -z "$SYSLOGADDR" ] ; then
93
		echo $"Server address not specified in /etc/sysconfig/netconsole" 1>&2
94
		exit 6
95
	fi
96
	eval $(print_address_info $SYSLOGADDR)
97
 
98
	if [ -z "$SYSLOGMACADDR" ]; then
99
		echo $"netconsole: can't resolve MAC address of $SYSLOGADDR" 1>&2
100
		exit 1
101
	fi
102
 
103
	SYSLOGOPTS="netconsole=$LOCALPORT@$LOCALADDR/$DEV,$SYSLOGPORT@$SYSLOGADDR/$SYSLOGMACADDR "
104
 
105
	/usr/bin/logger -p daemon.info -t netconsole: inserting netconsole module with arguments \
106
	$SYSLOGOPTS
107
	if [ -n "$SYSLOGOPTS" ]; then
108
		action $"Initializing netconsole" modprobe netconsole \
109
			$SYSLOGOPTS
110
		[ "$?" != "0" ] && RETVAL=1
111
	fi
112
	touch /var/lock/subsys/netconsole
113
}
114
 
115
stop ()
116
{
117
	if /sbin/lsmod | grep netconsole >/dev/null 2>&1 ; then
118
	    action $"Disabling netconsole" rmmod netconsole;
119
	    [ "$?" != "0" ] && RETVAL=1
120
	fi
121
 
122
	rm -f /var/lock/subsys/netconsole
123
}
124
 
125
status ()
126
{
127
	if /sbin/lsmod | grep netconsole >/dev/null 2>&1 ; then
128
		echo $"netconsole module loaded"
129
		RETVAL=0
130
	else
131
		echo $"netconsole module not loaded"
132
		RETVAL=3
133
	fi
134
}
135
 
136
 
137
restart ()
138
{
139
	stop
140
	start
141
}
142
 
143
condrestart ()
144
{
145
	[ -e /var/lock/subsys/netconsole ] && restart
146
}
147
 
148
 
149
case "$1" in
150
    stop) stop ;;
151
    status) status ;;
152
    start|restart|reload|force-reload) restart ;;
153
    condrestart) condrestart ;;
154
    *) usage ;;
155
esac
156
 
157
exit $RETVAL