summaryrefslogtreecommitdiff
path: root/src/pmns/Rebuild
blob: 1d69e048255758c7d5ed9e50c9c9e11e9a5a8ac3 (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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
#!/bin/sh
#
# Copyright (c) 2000-2001,2003 Silicon Graphics, Inc.  All Rights Reserved.
# 
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version.
# 
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
# 
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#
# Rebuild the PMNS, handling assorted errors
#

# Note. has to be run from where the PMNS files are installed as local
#       file names (not full paths) are used
#

# source the PCP configuration environment variables
. $PCP_DIR/etc/pcp.env

tmp=`mktemp -d ./pcp.XXXXXXXXX` || exit 1
status=1

$PCP_SHARE_DIR/lib/lockpmns root
trap "$PCP_SHARE_DIR/lib/unlockpmns root; rm -rf $tmp; exit \$status" 0 1 2 3 15

_trace()
{
    if $silent
    then
	:
    else
	if $nochanges
	then
	    echo "$*"
	else
	    echo "$*" >>$tmp/trace
	fi
    fi
}

_trace_file()
{
    if $silent
    then
	:
    else
	if $nochanges
	then
	    cat $1
	else
	    cat $1 >>$tmp/trace
	fi
    fi
}

_syslog()
{
    $PCP_SYSLOG_PROG -p user.alert -t PCP "$*"
    _trace "$*"
}

_die()
{
    [ -f $tmp/trace ] && cat $tmp/trace
    rm -f root.new
    exit
}

prog=`basename $0`
debug=false
nochanges=false
root=root
root_updated=false
update=false
verbose=""
dupok=false
silent=false

_usage()
{
    _trace "Usage: Rebuild [-dnsuv]"
    _trace "Options:"
    _trace "  -d    allow duplicate PMIDs in the PMNS"
    _trace "  -n    dry run, show me what would be done"
    _trace "  -s    silent, exit status says it all"
    _trace "  -u    once only upgrade processing for a new PCP version"
    _trace "  -v    verbose, for the paranoid"
}

# Fixup "root" after PCP upgrade
#
_upgrade_root()
{
    [ ! -f root ] && return
    _trace "Rebuild: PCP upgrade processing for \"root\" PMNS changes ..."
    $nochanges && _trace "+ cull root_* names from PMNS ... <root >$tmp/root"

    # Cull root to remove all metrics from root_* files, so only metrics
    # for optional PMDAs remain

    # If there are deprecated top-level names (below "root") that are
    # no longer in a root_* file, add them here ...
    EXCLUDE="pagebuf origin"
    if [ "$PCP_PLATFORM" = linux ]
    then
	# If we're on Linux and the proc PMDA is _not_ included in
	# the pmcd configuration file, add the top-level metrics
	# that migrated from the linux PMDA to the proc PMDA
	#
	if [ -f $PCP_PMCDCONF_PATH ]
	then
	    if grep '^proc[ 	]' $PCP_PMCDCONF_PATH >/dev/null
	    then
		# proc PMDA is installed
		#
		:
	    else
		EXCLUDE="$EXCLUDE proc cgroup"
	    fi
	else
	    EXCLUDE="$EXCLUDE proc cgroup"
	fi
    else
	EXCLUDE="$EXCLUDE proc cgroup"
    fi

    # now gather top-level names from root_* files
    #
    if [ "`echo root_*`" != "root_*" ]
    then
	EXCLUDE_TMP=`for file in root_*
	do
	    $PCP_AWK_PROG <$file '
$1 == "}"			{ exit }
in_root==1			{ printf "%s ",$1 }
$1 == "root" && $2 == "{"	{ in_root = 1 }'
done`
	EXCLUDE="$EXCLUDE $EXCLUDE_TMP"
    fi

    if [ ! -z "$verbose" -a ! -z "$EXCLUDE" ]
    then
	_trace "Exclude these top-level names ..."
	_trace "`echo $EXCLUDE | fmt | sed -e 's/^/    /'`"
    fi

    $PCP_AWK_PROG <root >$tmp/root '
BEGIN	{   # exclude these top-level names and all their descendents
	    #
	    n = split("'"$EXCLUDE"'", e)
	    for (i=1; i <= n; i++) {
		not_in_root[e[i]] = 1
	    }

	    in_root = 0
	    skip = 0
	}
$1 == "root" && $2 == "{"	{ in_root = 1 }
$1 == "}"			{ in_root = 0 }
in_root				{ if (!($1 in not_in_root)) print
				  next
				}
$2 == "{"			{ n = split($1, name, ".")
			          if (n > 0 && name[1] in not_in_root)
				    skip = 1
				}
skip && $1 == "}"		{ skip = 0; next }
skip				{ next }
				{ print }'
    if cmp -s root $tmp/root >/dev/null 2>&1
    then
	# no changes ... already been here?
	:
    else
	# we will usually end up here
	root=$tmp/root
	root_updated=true
    fi
}

while getopts dnusv\? c
do
    case $c
    in
	d)	dupok=true
		;;
	n)	nochanges=true
		echo "$prog: Warning: dry run, no changes will be made"
		;;
	u)	update=true
		;;
	s)	silent=true
		;;
	v)	verbose="-v"
		;;
	\?)	_usage
		status=0
		_die
		;;
    esac
done
shift `expr $OPTIND - 1`

# some preliminary checks
#
for file in $PCP_BINADM_DIR/pmnsmerge
do
    if [ ! -x $file ]
    then
	_syslog "$prog: $file is missing. Cannot proceed."
	_die
    fi
done

# remove all trace of old binary pmns (not used in PCP 3.6 or later)
#
rm -f root.bin

here=`pwd`
_trace "Rebuilding the Performance Metrics Name Space (PMNS) in $here ..."

if [ $# -ne 0 ]
then
    _usage
    _die
fi

if $nochanges
then
    CP="_trace + cp"
    MV="_trace + mv"
    LN="_trace + ln"
    RM="_trace + rm"
    PMNSMERGE="_trace + pmnsmerge ..."
else
    CP=cp
    MV=mv
    LN=ln
    RM=rm
    PMNSMERGE=

    if [ ! -w `pwd` ]
    then
	_syslog "$prog: cannot write in directory `pwd`, script should be run as \"root\"?"
	_die
    fi

    if [ ! -f root ]
    then
    	echo "root {" >root
    	echo "}" >>root
	chmod 644 root
    fi

    if [ ! -w root ]
    then
	_syslog "$prog: cannot write file \"root\" in directory `pwd`, script should be run as \"root\"?"
	_die
    fi
fi

if $update
then
    # PCP upgrade fix ups
    #
    _upgrade_root
fi

if [ -f $root ]
then
    haveroot=true
else
    haveroot=false
    if $nochanges
    then
	_trace "+ create empty root PMNS ..."
    else
	root=$tmp/root
	cat <<EOFEOF >$root
root {
}
EOFEOF
    fi
fi

# Merge $root and root_* to produce the new root.
# Each root_* file should be a complete namespace,
# i.e. it should include an entry for root.
#
mergelist=""
if [ "`echo root_*`" != "root_*" ]
then
    mergelist=`ls -1 root_* | $PCP_AWK_PROG '
    /root_web/		{next}
			{print}'`
fi

_trace "$prog: merging the following PMNS files: "
_trace $root $mergelist | fmt | sed -e 's/^/    /'

rm -f root.new
eval $PMNSMERGE
if $dupok
then
    pmnsmerge $verbose -d $root $mergelist root.new >$tmp/out 2>&1
else
    pmnsmerge $verbose $root $mergelist root.new >$tmp/out 2>&1
fi

if [ $? != 0 ]
then
    cat $tmp/out
    _syslog "$prog: pmnsmerge failed"
    _trace "         \"root\" has not been changed."
    _die
fi

# Multiple Rebuilds in succession should be a no-op.
#
if [ -f root ]
then
    if $dupok
    then
	pminfo -m -N root 2>/dev/null | sort >$tmp/list.old
    else
	pminfo -m -n root 2>/dev/null | sort >$tmp/list.old
    fi
fi
if [ ! -s $tmp/list.old ]
then
    if $dupok
    then
	pminfo -m -N $root 2>/dev/null | sort >$tmp/list.old
    else
	pminfo -m -n $root 2>/dev/null | sort >$tmp/list.old
    fi
fi
if $dupok
then
    pminfo -m -N root.new | sort >$tmp/list.new
else
    pminfo -m -n root.new | sort >$tmp/list.new
fi
if cmp -s $tmp/list.old $tmp/list.new > /dev/null 2>&1
then
    [ ! -f root ] && eval $MV root.new root
    _trace "$prog: PMNS is unchanged."
else
    # Install the new root
    #
    [ ! -z "$verbose" ] && _trace_file $tmp/out
    if $haveroot
    then
	_trace "$prog: PMNS \"$here/root\" updated."
	_trace "... previous version saved as \"$here/root.prev\""
	eval $MV root root.prev
    else
	_trace "$prog: new PMNS \"$here/root\" created."
    fi
    eval $MV root.new root

    # signal pmcd if it is running
    #
    pminfo -v pmcd.version >/dev/null 2>&1 && pmsignal -a -s HUP pmcd

    if [ ! -z "$verbose" ] && $haveroot
    then
	_trace "+ PMNS differences ..."
	diff -c $tmp/list.old $tmp/list.new >$tmp/diff
	_trace_file $tmp/diff
	_trace
	_trace "+ root differences ..."
	diff -c root.prev root >$tmp/diff
	_trace_file $tmp/diff
    fi
fi
rm -f root.new

# remake stdpmid
#
[ -f Make.stdpmid ] && ./Make.stdpmid

[ X"$verbose" = X-v -a -f $tmp/trace ] && cat $tmp/trace

status=0
exit