]> sourceware.org Git - systemtap.git/blob - stap-find-servers
Configuration problems.
[systemtap.git] / stap-find-servers
1 #!/bin/bash
2
3 # Find compile servers for systemtap
4 #
5 # Copyright (C) 2008, 2009 Red Hat Inc.
6 #
7 # This file is part of systemtap, and is free software. You can
8 # redistribute it and/or modify it under the terms of the GNU General
9 # Public License (GPL); either version 2, or (at your option) any
10 # later version.
11
12 # This script uses avahi to find systemtap compile servers on the local
13 # network. Information about each server found is printed to stdout.
14
15 # Deprecation warning.
16 echo "WARNING: stap-find-servers is deprecated and will be removed in release 1.5" >&2
17 echo "Please use 'stap --list-servers'. See stap(1) for more information" >&2
18
19 # Initialize the environment
20 . ${PKGLIBEXECDIR}stap-env
21
22 #-----------------------------------------------------------------------------
23 # Helper functions.
24 #-----------------------------------------------------------------------------
25 # function: initialization
26 function initialization {
27 rc=1 # not found yet
28 if test "X$1" = "X--all"; then
29 find_all=1
30 else
31 find_all=0
32 fi
33 timeout=20 # seconds
34 }
35
36 # function: find_servers
37 #
38 # Find and establish connection with a compatible stap server.
39 function find_servers {
40 # Create a temp file for the list of servers. We do this instead
41 # of using a pipe so that we can kill avahi-browse if it
42 # takes too long.
43 tmpfile=`mktemp -t stap-serversXXXXXX` || \
44 fatal "Cannot create temporary file " $tmpfile
45
46 # Find servers
47 avahi-browse $stap_avahi_service_tag --terminate -r > $tmpfile &
48
49 for ((attempt=0; $attempt < $timeout; ++attempt))
50 do
51 if ! jobs '%avahi-browse' >/dev/null 2>&1; then
52 break
53 fi
54 sleep 1
55 done
56
57 # Kill avahi-browse, if it's still running
58 test $attempt = $timeout && kill -s SIGTERM '%avahi-browse' 2>/dev/null
59
60 match_server < $tmpfile
61 rm -fr $tmpfile
62 }
63
64 # function: match_server
65 #
66 # Find a suitable server using the avahi-browse output provided on stdin.
67 function match_server {
68 local server_ip
69 local server_name
70 local server_sysinfo
71 local server_port
72
73 rc=1 # not found yet
74
75 # Loop over the avahi service descriptors.
76 read -t $timeout || return
77 while test "X$REPLY" != "X"
78 do
79 server_name=
80 server_ip=
81 server_port=
82 server_sysinfo=
83
84 # Examine the next service descriptor
85 # Is it a stap server?
86 if ! echo $REPLY | grep -q "=.* .* IPv4 .*_stap"; then
87 read -t $timeout || return
88 continue
89 fi
90 REPLY=
91
92 # Get the details of the service
93 local service_tag equal service_data
94 while read -t $timeout service_tag equal service_data
95 do
96 case $service_tag in
97 hostname )
98 server_name=`expr "$service_data" : '\[\([^]]*\)\]'`
99 ;;
100 address )
101 # Sometimes (seems random), avahi-resolve-host-name resolves a local server to its
102 # hardware address rather than its ip address. Keep trying until we get
103 # an ip address.
104 server_ip=`expr "$service_data" : '\[\([^]]*\)\]'`
105 local attempt
106 for ((attempt=0; $attempt < 5; ++attempt))
107 do
108 server_ip=`expr "$server_ip" : '\([0-9]*\.[0-9]*\.[0-9]*\.[0-9]*\)$'`
109 if test "X$server_ip" != "X"; then
110 break
111 fi
112 # Resolve the server.domain to an ip address.
113 server_ip=`avahi-resolve-host-name $server_name`
114 server_ip=`expr "$server_ip" : '.* \(.*\)$'`
115 done
116 ;;
117 port )
118 server_port=`expr "$service_data" : '\[\([^]]*\)\]'`
119 ;;
120 txt )
121 server_sysinfo=`expr "$service_data" : '\[.*\"\(sysinfo=[^]]*\)\"\]'`
122 ;;
123 * )
124 REPLY="$service_tag $equal $service_data"
125 break
126 ;;
127 esac
128 done
129
130 # It's an stap server, but is it compatible?
131 if test $find_all = 0 -a "$server_sysinfo" != "`client_sysinfo`"; then
132 continue
133 fi
134
135 # It's compatible, or we're finding all servers. Print a summary line
136 echo "$server_name $server_ip $server_port '$server_sysinfo'"
137 rc=0
138 done
139 }
140
141 # function client_sysinfo
142 #
143 # Generate the client's sysinfo and echo it to stdout
144 function client_sysinfo {
145 if test "X$sysinfo_client" = "X"; then
146 # Add some info from uname
147 sysinfo_client="`uname -r` `stap_get_arch`"
148 fi
149 echo sysinfo=$sysinfo_client
150 }
151
152 # function: fatal [ MESSAGE ]
153 #
154 # Fatal error
155 # Prints its arguments to stderr and exits
156 function fatal {
157 echo "$0: ERROR:" "$@" >&2
158 exit 1
159 }
160
161 #-----------------------------------------------------------------------------
162 # Beginning of main line execution.
163 #-----------------------------------------------------------------------------
164 initialization "$@"
165 find_servers
166
167 exit $rc
This page took 0.104079 seconds and 5 git commands to generate.