summaryrefslogtreecommitdiff
path: root/src/pmlogger/pmnewlog.sh
blob: ff68936e227f4228f1fe34f2c05b662e20198407 (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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
#! /bin/sh
#
# Copyright (c) 2014 Red Hat.
# Copyright (c) 1995-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.
# 
# stop and restart a pmlogger instance
#

# Get standard environment
. $PCP_DIR/etc/pcp.env


# error messages should go to stderr, not the GUI notifiers
#
unset PCP_STDERR

tmp=`mktemp -d /tmp/pcp.XXXXXXXXX` || exit 1
status=0
trap "rm -rf $tmp; exit \$status" 0 1 2 3 15
prog=`basename $0`

VERBOSE=false
SHOWME=false
CP=cp
MV=mv
RM=rm
KILL=pmsignal
primary=true
myname="primary pmlogger"
connect=primary
access=""
config=""
saveconfig=""
logfile="pmlogger.log"
namespace=""
args=""
sock_me=""
proxyhost="$PMPROXY_HOST"
proxyport="$PMPROXY_PORT"

cat > $tmp/usage << EOF
# Usage: [options] archive

pmnewlog options:
  -a=FILE,--access=FILE  specify access controls for the new pmlogger
  -C=FILE,--save=FILE    save the configuration of new pmlogger in FILE
  -c=FILE,--config=FILE  file to load configuration from
  -N,--showme            perform a dry run, showing what would be done
  --namespace
  -P,--primary           execute as primary logger instance
  -p=PID,--pid=PID       restart non-primary logger with pid
  -s,--socks             use pmsocks
  -V,--verbose           turn on verbose reporting of pmnewlog progress
  --help

pmlogger options:
  --debug
  -c=FILE,--config=FILE  file to load configuration from
  -l=FILE, --log=FILE    redirect diagnostics and trace output
  -L, --linger           run even if not primary logger instance and nothing to log
  -m=MSG, --note=MSG     descriptive note to be added to the port map file
  --namespace
  -P, --primary          execute as primary logger instance
  -r, --report           report record sizes and archive growth rate
  -t=DELTA, --interval=DELTA  default logging interval
  -T=TIME, --finish=TIME end of the time window
  -v=SIZE, --volsize=SIZE  switch log volumes after size has been accumulated
  -y                     set timezone for times to local time rather than from PMCD host
EOF

_abandon()
{
    echo
    echo "Sorry, but this is fatal.  No new pmlogger instance has been started."
    status=1
    exit
}

_check_pid()
{
    if $SHOWME
    then
	:
    else
	_get_pids_by_name pmlogger | grep "^$1\$"
    fi
}

_check_logfile()
{
    if [ ! -f $logfile ]
    then
	echo "Cannot find pmlogger output file at \"$logfile\""
    else
	echo "Contents of pmlogger output file \"$logfile\" ..."
	cat $logfile
    fi
}

_check_logger()
{
    # wait until pmlogger process starts, or exits
    #
    delay=5
    [ ! -z "$PMCD_CONNECT_TIMEOUT" ] && delay=$PMCD_CONNECT_TIMEOUT
    x=5
    [ ! -z "$PMCD_REQUEST_TIMEOUT" ] && x=$PMCD_REQUEST_TIMEOUT

    # wait for maximum time of a connection and 20 requests
    #
    delay=`expr $delay + 20 \* $x`
    i=0
    while [ $i -lt $delay ]
    do
	$VERBOSE && $PCP_ECHO_PROG $PCP_ECHO_N ".""$PCP_ECHO_C"
	if $SHOWME
	then
	    echo "+ echo 'connect $1' | $pmlc_prefix pmlc ..."
	    $VERBOSE && echo " done"
	    return 0
	elif echo "connect $1" | eval $pmlc_prefix pmlc 2>&1 | grep "Unable to connect" >/dev/null
	then
            :
        else
            sleep 5
            $VERBOSE && echo " done"
            return 0
        fi

	if _get_pids_by_name pmlogger | grep "^$1\$" >/dev/null
	then
            :
        else
	    $VERBOSE || _message restart
	    echo " process exited!"
	    _check_logfile
	    return 1
	fi
	sleep 5
	i=`expr $i + 5`
    done
    $VERBOSE || _message restart
    echo " timed out waiting!"
    sed -e 's/^/	/' $tmp/out
    _check_logfile
    return 1
}

_message()
{
    case $1
    in
	looking)
	    $PCP_ECHO_PROG $PCP_ECHO_N "Looking for $myname ...""$PCP_ECHO_C"
	    ;;
	get_host)
	    $PCP_ECHO_PROG $PCP_ECHO_N "Getting logged host name from $myname ...""$PCP_ECHO_C"
	    ;;
	get_state)
	    $PCP_ECHO_PROG $PCP_ECHO_N "Contacting $myname to get logging state ...""$PCP_ECHO_C"
	    ;;
	restart)
	    $PCP_ECHO_PROG $PCP_ECHO_N "Waiting for new pmlogger to start ..""$PCP_ECHO_C"
	    ;;
    esac
}

_do_cmd()
{
    if $SHOWME
    then
	echo "+ $1"
    else
	eval $1
    fi
}

# option parsing
#
# pmlogger, without -V version which is redefined as -V (verbose)
# and -s exit_size which is redefined as -s (pmsocks), and ignore
# [ -h host ] and [ -x fd ] as they make no sense in the argument
# part of the pmlogger control file for a long-running pmlogger.
#

ARGS=`pmgetopt --progname=$prog --config=$tmp/usage -- "$@"`
[ $? != 0 ] && exit 1

eval set -- "$ARGS"
while [ $# -gt 0 ]
do
    case "$1"
    in

# pmnewlog options and flags
#

	-a)	access="$2"
		shift
		if [ ! -f "$access" ]
		then
		    echo "$prog: Error: cannot find accessfile ($access)"
		    _abandon
		fi
		;;

	-C)	saveconfig="$2"
		shift
		;;

	-N)	SHOWME=true
		CP="echo + cp"
		MV="echo + mv"
		RM="echo + rm"
		KILL="echo + kill"
		;;

	-p)	pid=$2
		shift
		primary=false
		myname="pmlogger (process $pid)"
		connect=$pid
		;;

	-s)	if which pmsocks >/dev/null 2>&1
                then
                    sock_me="pmsocks "
                else
                    echo "$prog: Warning: no pmsocks available, would run without"
                    sock_me=""
                fi
		;;

	-V)	VERBOSE=true
		;;

# pmlogger options and flags that need special handling
#

	-c)	config="$2"
		shift
		if [ ! -f "$config" ]
		then
		    if [ -f "$PCP_SYSCONF_DIR/pmlogger/$config" ]
		    then
			config="$PCP_SYSCONF_DIR/pmlogger/$config"
		    else
			echo "$prog: Error: cannot find configfile ($config)"
			_abandon
		    fi
		fi
		;;

	-l)	logfile="$2"
		shift
		;;

	-n)	namespace="-n $2"
		args="${args}$1 $2 "
		shift
		;;

	-P)	primary=true
		myname="primary pmlogger"
		;;

# pmlogger flags passed through
#

	-L|-r|-y)
		args="${args}$1 "
		;;

	-D|-m|-t|-T|-v)
		args="${args}$1 $2 "
		shift
		;;

	--)	# end of options, start of arguments
		shift
		break
		;;

	-\?)	pmgetopt --usage --progname=$prog --config=$tmp/usage
		_abandon
		;;
    esac
    shift
done

if [ $# -ne 1 ]
then
    echo "$prog: Insufficient arguments" >&2
    echo >&2
    pmgetopt --usage --progname=$prog --config=$tmp/usage
    _abandon
fi

# initial sanity checking for new archive name
#
archive="$1"

# check that designated pmlogger is really running
#
$VERBOSE && _message looking
$PCP_PS_PROG $PCP_PS_ALL_FLAGS \
| if $primary
then
    grep 'pmlogger .*-P' | grep -v grep
else
    $PCP_AWK_PROG '$2 == '"$pid"' && /pmlogger/ { print }'
fi >$tmp/out

if [ -s $tmp/out ]
then
    $VERBOSE && echo " found"
    $VERBOSE && cat $tmp/out
    # expecting something like
    # pcp      30019   ... pmlogger ...args... -h hostname ...args...
    # pick pid and hostname (safer to do it here if possible because it
    # captures the possible -h hostname@proxy construct which is lost
    # if one gets the hostname from pmlogger via pmlc)
    #
    pid=`$PCP_AWK_PROG '{ print $2 }' <$tmp/out`
    hostname=`sed -n <$tmp/out -e '/ -h /{
s/.* -h //
s/ .*//
p
}'`
    # may have @proxyhost or @proxyhost:proxyport appended to hostname
    #
    proxyhost=`echo "$hostname" | sed -n -e '/@/{
s/.*@//
s/:.*//
p
}'`
    proxyport=`echo "$hostname" | sed -n -e '/@.*:/s/.*;//p'`
else
    if $VERBOSE
    then
	:
    else
	_message looking
	echo
    fi
    echo "$prog: Error: process not found"
    _abandon
fi

if [ -n "$proxyhost" ]
then
    if [ -n "$proxyport" ]
    then
	pmlc_prefix="PMPROXY_HOST=$proxyhost PMPROXY_PORT=$proxyport"
    else
	pmlc_prefix="PMPROXY_HOST=$proxyhost"
    fi
else
    pmlc_prefix=''
fi

# pass primary/not primary down
#
$primary && args="$args-P "

# pass logfile option down
#
args="$args-l $logfile "

# if not a primary pmlogger, get name of pmcd host pmlogger is connected to
#
if $primary
then
    hostname=localhost
elif [ -z "$hostname" ]
then
    # did not get pmcd hostname from ps output above, talk to pmlogger
    # via pmlc
    #
    # start critical section ... no interrupts due to pmlogger SIGPIPE
    # bug in PCP 1.1
    #
    trap "echo; echo $prog:' Interrupt! ... I am talking to pmlogger, please wait ...'" 1 2 3 15
    $VERBOSE && _message get_host

    _do_cmd "( echo 'connect $connect' ; echo status ) | eval $pmlc_prefix pmlc 2>$tmp/err >$tmp/out"

    # end critical section
    #
    trap "rm -rf $tmp; exit \$status" 0 1 2 3 15

    if $SHOWME || [ ! -s $tmp/err ]
    then
	$VERBOSE && echo " done"
    else
	if grep "Unable to connect" $tmp/err >/dev/null
	then
	    $VERBOSE || _message get_host
	    echo " failed to connect"
	    echo
	    sed -e 's/^/	/' $tmp/err
	    _abandon
	else
	    $VERBOSE || _message get_host
	    echo
	    echo "$prog: Warning: errors from talking to $myname via pmlc"
	    sed -e 's/^/	/' $tmp/err
	    echo
	    echo "continuing ..."
	fi
    fi

    if $SHOWME
    then
	hostname=somehost
    else
	hostname=`sed -n -e '/^pmlogger/s/.* from host //p' <$tmp/out`
	if [ -z "$hostname" ]
	then
	    echo "$prog: Error: failed to get host name from $myname"
	    echo "This is what was collected from $myname."
	    echo
	    sed -e 's/^/	/' $tmp/out
	    _abandon
	fi
	args="$args-h $hostname "
    fi
else
    # got hostname from ps output
    #
    args="$args-h $hostname "
fi

# extract/construct config file if required
#
if [ -z "$config" ]
then
    # start critical section ... no interrupts due to pmlogger SIGPIPE
    # bug in PCP 1.1
    #
    trap "echo; echo $prog:' Interrupt! ... I am talking to pmlogger, please wait ...'" 1 2 3 15
    $VERBOSE && _message get_state

    # iterate over top-level names in pmns, and query pmlc for
    # current configuration ... note exclusion of "proc" metrics
    # ... others may be excluded in a similar fashion
    #
    if $SHOWME
    then
	echo "+ ( echo 'connect $connect'; echo 'query ...'; ... ) | eval $pmlc_prefix pmlc $namespace | $PCP_AWK_PROG ..."
    else
	echo "connect $connect" >$tmp/pmlc.cmd
	for top in `pminfo -h $hostname $namespace \
		    | sed -e 's/\..*//' -e '/^proc$/d' \
		    | sort -u`
	do
	    echo "query $top" >>$tmp/pmlc.cmd
	done
	eval $pmlc_prefix pmlc $namespace <$tmp/pmlc.cmd 2>$tmp/err \
	| $PCP_AWK_PROG >$tmp/out '
/^[^ ]/						{ metric = $1; next }
$1 == "mand" || ( $1 == "adv" && $2 == "on" ) 	{ print $0 " " metric }'
    fi

    # end critical section
    #
    trap "rm -rf $tmp; exit \$status" 0 1 2 3 15

    if $SHOWME || [ ! -s $tmp/err ]
    then
	$VERBOSE && echo " done"
    else
	if grep "Unable to connect" $tmp/err >/dev/null
	then
	    $VERBOSE || _message get_state
	    echo " failed to connect"
	    echo
	    sed -e 's/^/	/' $tmp/err
	    _abandon
	else
	    $VERBOSE || _message get_state
	    echo
	    echo "$prog: Warning: errors from talking to $myname via pmlc"
	    sed -e 's/^/	/' $tmp/err
	    echo
	    echo "continuing ..."
	fi
    fi

    if [ ! -s $tmp/out ]
    then
	if $SHOWME
	then
	    :
	else
	    echo "$prog: Error: failed to collect configuration info from $myname"
	    echo "Most likely this pmlogger instance is inactive."
	    _abandon
	fi
    fi

    # convert to a pmlogger config file
    #
    if $SHOWME
    then
	echo "+ create new pmlogger config file ..."
    else
	sed <$tmp/out >$tmp/config \
	    -e 's/ on  nl/ on/' \
	    -e 's/ off nl/ off/'\
	    -e 's/  *mand  *\(o[nf]*\) /log mandatory \1 /' \
	    -e 's/  *adv  *on /log advisory on/' \
	    -e 's/\[[0-9][0-9]* or /[/' \
	    -e 's/\(\[[^]]*]\) \([^ ]*\)/\2 \1/' \
	    -e 's/   */ /g'

	if [ ! -s $tmp/config ]
	then
	    echo "$prog: Error: failed to generate a pmlogger configuration file for pmlogger"
	    echo "This is what was collected from $myname."
	    echo
	    sed -e 's/^/	/' $tmp/out
	    _abandon
	fi
    fi
    config=$tmp/config
fi

# optionally append access control specifications
#
if [ -n "$access" ]
then
    if grep '\[access]' $config >/dev/null
    then
	echo "$prog: Error: pmlogger configuration file already contains an"
	echo "	access control section, specifications from \"$access\" cannot"
	echo "	be applied."
	_abandon
    fi
    cat $access >>$config
fi

# add config file to the args, save config file if -C
#
args="$args-c $config "
if [ -n "$saveconfig" ]
then
    if eval $CP $config $saveconfig
    then
	echo "New pmlogger configuration file saved as $saveconfig"
    else
	echo "$prog: Warning: unable to save configuration file as $saveconfig"
    fi
fi

# kill off existing pmlogger
#
$VERBOSE && $PCP_ECHO_PROG $PCP_ECHO_N "Terminating $myname ...""$PCP_ECHO_C"
for sig in USR1 TERM KILL
do
    $VERBOSE && $PCP_ECHO_PROG $PCP_ECHO_N " SIG$sig ...""$PCP_ECHO_C"
    eval $KILL -s $sig $pid
    sleep 5
    [ "`_check_pid $pid`" = "" ] && break
done

if [ "`_check_pid $pid`" = "" ]
then
    $VERBOSE && echo " done"
else
    echo " failed!"
    _abandon
fi

# the archive folio Latest is for the most recent archive in this directory
#
dir=`dirname $archive`
eval $RM -f $dir/Latest

# clean up port-map, just in case
#
PM_LOG_PORT_DIR="$PCP_TMP_DIR/pmlogger"
eval $RM -f "$PM_LOG_PORT_DIR"/$pid
$primary && eval $RM -f $PM_LOG_PORT_DIR/primary

# finally do it, ...
#
cd $dir
$SHOWME && echo "+ cd $dir"
[ "$dir" = . ] && dir=`pwd`
archive=`basename $archive`

# handle duplicates/aliases (happens when pmlogger is restarted
# within a minute and basename is the same)
#
suff=''
for file in $archive.*
do
    [ "$file" = "$archive"'.*' ] && continue
    # we have a clash! ... find a new -number suffix for the
    # existing files ... we are going to keep $archive for the
    # new pmlogger below
    #
    if [ -z "$suff" ]
    then
	for xx in 0 1 2 3 4 5 6 7 8 9
	do
	    for yy in 0 1 2 3 4 5 6 7 8 9
	    do
		[ "`echo $archive-${xx}${yy}.*`" != "$archive-${xx}${yy}.*" ] && continue
		suff=${xx}$yy
		break
	    done
	    [ ! -z "$suff" ] && break
	done
	if [ -z "$suff" ]
	then
	    echo "$prog: Error: unable to break duplicate clash for archive basename \"$archive\""
	    _abandon
	fi
	$VERBOSE && echo "Duplicate archive basename ... rename $archive.* files to $archive-$suff.*"
    fi
    eval $MV -f $file `echo $file | sed -e "s/$archive/&-$suff/"`
done

$VERBOSE && echo "Launching new pmlogger in directory \"$dir\" as ..."
[ -f $logfile ] && eval $MV -f $logfile $logfile.prior
$VERBOSE && echo "${sock_me}pmlogger $args$archive"

if $SHOWME
then
    echo "+ ${sock_me}pmlogger $args$archive &"
    echo "+ ... assume pid is 12345"
    new_pid=12345
else
    ${sock_me}pmlogger $args$archive &
    new_pid=$!
fi

# stall a bit ...
#
STALL_TIME=10
sleep $STALL_TIME

$VERBOSE && _message restart
if _check_logger $new_pid || $SHOWME
then
    $VERBOSE && echo "New pmlogger status ..."
    $VERBOSE && _do_cmd "( echo 'connect $new_pid'; echo status ) | $pmlc_prefix pmlc"

    # make the "Latest" archive folio
    #
    i=0
    failed=true
    WAIT_TIME=10
    while [ $i -lt $WAIT_TIME ]
    do
	if $SHOWME || [ -f $archive.0 -a -f $archive.meta -a $archive.index ]
	then
	    _do_cmd "mkaf $archive.0 >Latest" 2>$tmp/err
	    if [ -s $tmp/err ]
	    then
		# errors from mkaf typically result from race conditions
		# at the start of pmlogger, e.g.
		# Warning: cannot extract hostname from archive "..." ...
		#
		# simply keep trying
		:
	    else
		failed=false
		break
	    fi
	fi
	sleep 1
	i=`expr $i + 1`
    done

    if $failed
    then
	ELAPSED=`expr $STALL_TIME + $WAIT_TIME`
	echo "Warning: pmlogger [pid=$new_pid host=$hostname] failed to create archive files within $ELAPSED seconds"
	if [ -f $tmp/err ]
	then
	    echo "Warnings/errors from mkaf ..."
	    cat $tmp/err
	fi
    fi
else
    _abandon
fi

exit