aboutsummaryrefslogtreecommitdiffstats
path: root/rc.d/init.d/netconsole
blob: 172a82b996c6c857a490c85f19c64056060b4913 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/bin/bash
# 
# netconsole 	This loads the netconsole module with the configured parameters.
#
# chkconfig: - 50 50
# 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, 2001

PATH=/sbin:/usr/sbin:$PATH
RETVAL=0
SERVER_ADDRESS_RESOLUTION=

# Check that networking is up.
. /etc/sysconfig/network

# Source function library.
. /etc/rc.d/init.d/functions

# Default values
LOCALPORT=6666
DEV=

SYSLOGADDR=
SYSLOGPORT=514
SYSLOGMACADDR=

kernel=$(uname -r | cut -d. -f1-2)

usage ()
{
	echo $"Usage: $0 {start|stop|status|restart|condrestart}" 1>&2
	RETVAL=3
}

print_address_info ()
{
	local host=$1
	local route via target

	route=$(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 echo $route | grep -q " via " ; then
		via=$(echo $route | sed "s|.* via \([^ ]*\).*|\1|")
		target=$via
	else
		target=$host
	fi
	if [ -z "$SYSLOGMACADDR" ]; then
		arp=$(LANG=C /sbin/arping -c 1 -I $DEV $target 2>/dev/null | awk '/ reply from .*[.*]/ { print gensub(".* reply from .* \\[(.*)\\].*","\\1","G") }')
		[ -n "$arp" ] && echo "SYSLOGMACADDR=$arp"
	fi	
}	
	
start ()
{
	[ -f /etc/sysconfig/netconsole ] || exit 6
	. /etc/sysconfig/netconsole

	SYSLOGOPTS=
	# syslogd server, if any
	if [ -n "$SYSLOGADDR" ]; then
		MATCH="^([0-9]{1,3}\.){3}[0-9]{1,3}$"
		if ! [[ "$SYSLOGADDR" =~ $MATCH ]]; then
			SYSLOGADDR=$(LANG=C host $SYSLOGADDR 2>/dev/null | awk '/has address / { print $NF }')
		fi
	fi
	if [ -z "$SYSLOGADDR" ] ; then
		echo $"Server address not specified in /etc/sysconfig/netconsole" 1>&2
		exit 6
	fi
	eval $(print_address_info $SYSLOGADDR)
	
	if [ -z "$SYSLOGMACADDR" ]; then
		echo $"netconsole: can't resolve MAC address of $SYSLOGADDR" 1>&2
		exit 1
	fi

	SYSLOGOPTS="netconsole=$LOCALPORT@$LOCALADDR/$DEV,$SYSLOGPORT@$SYSLOGADDR/$SYSLOGMACADDR "

	/usr/bin/logger -p daemon.info -t netconsole: inserting netconsole module with arguments \
	$SYSLOGOPTS
	if [ -n "$SYSLOGOPTS" ]; then
		action $"Initializing netconsole" modprobe netconsole \
			$SYSLOGOPTS
	fi
	touch /var/lock/subsys/netconsole
}

stop ()
{
	if /sbin/lsmod | grep netconsole >/dev/null 2>&1 ; then
	    action $"Disabling netconsole" rmmod netconsole;
	fi

	rm -f /var/lock/subsys/netconsole
}

status ()
{
	if /sbin/lsmod | grep netconsole >/dev/null 2>&1 ; then
		echo $"netconsole module loaded"
		RETVAL=0
	else
		echo $"netconsole module not loaded"
		RETVAL=3
	fi
}


restart ()
{
	stop
	start
}

condrestart ()
{
	[ -e /var/lock/subsys/netconsole ] && restart
}


case "$1" in
    stop) stop ;;
    status) status ;;
    start|restart|reload) restart ;;
    condrestart) condrestart ;;
    *) usage ;;
esac

exit $RETVAL