Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/lib/libcs/genlocal.sh
1808 views
1
########################################################################
2
# #
3
# This software is part of the ast package #
4
# Copyright (c) 1990-2011 AT&T Intellectual Property #
5
# and is licensed under the #
6
# Eclipse Public License, Version 1.0 #
7
# by AT&T Intellectual Property #
8
# #
9
# A copy of the License is available at #
10
# http://www.eclipse.org/org/documents/epl-v10.html #
11
# (with md5 checksum b35adb5213ca9657e911e9befb180842) #
12
# #
13
# Information and Software Systems Research #
14
# AT&T Research #
15
# Florham Park NJ #
16
# #
17
# Glenn Fowler <[email protected]> #
18
# #
19
########################################################################
20
: generate local host info
21
#
22
# genlocal [options] [host ...]
23
#
24
# @(#)genlocal (AT&T Research) 2002-10-02
25
#
26
# NOTE: this only works for hosts the caller can reach by rsh/ssh
27
#
28
29
case $RANDOM in
30
$RANDOM) exec ksh $0 "$@"; echo "$0: ksh required" >&2; exit 1 ;;
31
esac
32
integer cluster=5
33
dir=.
34
file=
35
heading=1
36
list=
37
rsh=rsh
38
timeout=90
39
verbose=:
40
while :
41
do case $# in
42
0) break ;;
43
esac
44
case $1 in
45
-[frt])case $# in
46
1) set -- '-?'
47
break
48
;;
49
esac
50
case $1 in
51
-f) file=$2 ;;
52
-r) rsh=$2 ;;
53
-t) timeout=$2 ;;
54
esac
55
shift
56
;;
57
-h) heading=
58
;;
59
-n) list=1
60
;;
61
-v) verbose='print -u2'
62
;;
63
'-?') break
64
;;
65
[-+]*) print -u2 "$0: $1: invalid option"
66
set -- '-?'
67
break
68
;;
69
*) break
70
;;
71
esac
72
shift
73
done
74
case $1 in
75
'-?') print -u2 "Usage: $0 [-hnv] [-f share-file] [-r rsh-path] [-t timeout] [host ...]"; exit 1 ;;
76
esac
77
case $file in
78
"") ifs=${IFS-'
79
'}
80
IFS=:
81
set "" $PATH "--" "$@"
82
IFS=$ifs
83
f=lib/cs/share
84
while :
85
do shift
86
case $1 in
87
"") continue ;;
88
--) break ;;
89
esac
90
case $file in
91
"") test -f ${1%bin}$f && file=${1%bin}$f ;;
92
esac
93
done
94
shift
95
case $file in
96
"") print -u2 $0: $f: not found; exit 1 ;;
97
esac
98
;;
99
*) if test ! -f $file
100
then print -u2 "$0: $file: cannot read share file"; exit 1
101
fi
102
;;
103
esac
104
print -u2 "$0: warning: $rsh may hang on some hosts -- monitor the progress with the -v option or ps $$"
105
server=
106
for host in $(egrep -v '^#' $file)
107
do case $server in
108
"") server=$host ;;
109
*) server="$server|$host" ;;
110
esac
111
done
112
hostname=$(package host name)
113
hostname=${hostname%%.*}
114
case $# in
115
0) domain=$(
116
{
117
cat /etc/hosts
118
ypcat hosts
119
} 2>/dev/null |
120
egrep "[^#].*[ ]$hostname(\$|[. ])" |
121
sed -e '1!d' -e 's/^[^ ]*//' -e 's/[^.]*.//' -e 's/[ ].*//'
122
)
123
set -- $(
124
{
125
print "$hostname"
126
egrep -v '^#' $file
127
ruptime | fgrep -v 'no hosts'
128
{
129
for i in /etc/resolv.conf /usr/etc/resolv.conf
130
do if test -f $i
131
then echo "ls -t A $(sed -e '/^domain/!d' -e 's/.*[ ][ ]*//' $i)" | nslookup
132
break
133
fi
134
done
135
cat /etc/hosts
136
ypcat hosts
137
} |
138
fgrep ".$domain" |
139
awk '{print $2}'
140
} 2>/dev/null |
141
sed -e 's/[. ].*//' -e '/^[a-zA-Z].*[a-zA-Z0-9]$/!d' |
142
sort -u
143
)
144
case $list in
145
?*) print domain=$domain
146
;;
147
esac
148
;;
149
esac
150
case $list in
151
?*) print hosts='"'$*'"'; exit ;;
152
esac
153
hosts=
154
for host
155
do case $host in
156
local|localhost) host=$hostname ;;
157
esac
158
case " $hosts " in
159
*" $host "*) continue ;;
160
esac
161
hosts="$hosts $host"
162
$verbose -n "$host "
163
case $host in
164
$hostname)
165
package host name type cpu rating
166
;;
167
*) if ping -c 1 -w 4 $host >/dev/null 2>&1
168
then $rsh $host bin/package host name type cpu rating &
169
info=$!
170
{
171
sleep $timeout
172
kill -9 $info
173
} &
174
time=$!
175
wait $info
176
kill -9 $time
177
fi
178
;;
179
esac 2>/dev/null
180
done | while read host type cpu rating
181
do $verbose type=$type cpu=$cpu rating=$rating
182
host=${host%%.*}
183
eval "case '$host' in
184
$server) idle= ;;
185
*) idle=15m ;;
186
esac"
187
print $host" "type=$type" "${rating}${idle:+" idle=$idle"}${cpu:+" cpu=$cpu"}
188
done | sort -b +2n -3 | {
189
integer v c d i j k n
190
((n=0))
191
while read host type rate attr
192
do ((n=n+1))
193
h[n]=$host
194
t[n]=$type
195
v[n]=$rate
196
a[n]=$attr
197
done
198
c=1
199
while ((c))
200
do ((c=0))
201
((i=1))
202
while ((i<n))
203
do ((d=v[i+1]-v[i]))
204
if ((d>1&&d<cluster))
205
then ((d=d/2))
206
((v[i]=v[i]+d))
207
((v[i+1]=v[i+1]-d))
208
c=1
209
fi
210
((i=i+1))
211
done
212
done
213
((i=1))
214
while ((i<=n))
215
do ((c=v[i]))
216
((j=i+1))
217
((k=1))
218
while ((j<=n))
219
do if (((v[j]-v[j-1])>1||(v[j]-v[i])>cluster))
220
then break
221
fi
222
((c=c+v[j]))
223
((k=k+1))
224
((j=j+1))
225
done
226
((c=c/k))
227
((d=c%10))
228
((c=c-d))
229
if ((d>6))
230
then ((c=c+10))
231
elif ((d>3))
232
then ((c=c+5))
233
fi
234
while ((i<j))
235
do ((v[i]=c))
236
((i=i+1))
237
done
238
done
239
((i=1))
240
while ((i<=n))
241
do rate=${v[i]}
242
print ${h[i]} ${t[i]} rating=${v[i]}" ${a[i]}"
243
((i=i+1))
244
done
245
} | sort -b +1 -2 +2.7n -3 +0 -1 | while read host type attr
246
do case $heading in
247
1) print '#'
248
print '# local host attributes'
249
print '#'
250
print
251
print 'local busy=2m pool=9'
252
print
253
;;
254
''|$type)
255
;;
256
*) print
257
;;
258
esac
259
heading=$type
260
case $host in
261
????????*)
262
tab1=" "
263
;;
264
*) tab1=" "
265
;;
266
esac
267
case $type in
268
????????*)
269
tab2=" "
270
;;
271
*) tab2=" "
272
;;
273
esac
274
print $host"$tab1"$type"$tab2$attr"
275
done
276
277