p4dstate.sh #1

  • //
  • guest/
  • perforce_software/
  • sdp/
  • dev/
  • Server/
  • Unix/
  • p4/
  • common/
  • bin/
  • p4dstate.sh
  • View
  • Commits
  • Open Download .zip Download (4 KB)
#!/bin/sh

#==============================================================================
# Copyright and license info is available in the LICENSE file included with
# the Server Deployment Package (SDP), and also available online:
# https://swarm.workshop.perforce.com/projects/perforce-software-sdp/view/main/LICENSE
#------------------------------------------------------------------------------

# The RCSCHANGE and RCSDATE values are copied verbatim from the originating
# Helix Server, and read-only elsewhere.  Do not change these and do not
# apply the '+k' file type modifer to this file.
RCSCHANGE="$Change: 1262283 $"
RCSDATE="$Date: 2015/11/05 $"

outfile=/<absolute-path-to->/p4dstate-`date +%Y%m%d%H%M%S`.out
p4d=/<absolute-path-to->/p4d
p4=/<absolute-path-to->/p4
P4ROOT=/<absolute-path-to-P4ROOT>
P4LOG=/<absolute-path-to->/log
P4JOURNAL=/<absolute-path-to->/journal
P4PORT=<p4d-listener>
P4USER=<perforce-superuser>

TIMEOUT=120

export P4PORT
export P4USER

SIGKILL=9
SIGTERM=15

plural()
{
	if [ $1 != "1" ]
	then
	    printf "%s %ss" $1 $2
	else
	    if [ $# -lt 3 ]
	    then
		printf "%s %s" $1 $2
	    else
		printf "%s" $3
	    fi
	fi
}

header()
{
	printf "Output from "
	if [ $# -gt 1 ]
	then
	    plural $2 second
	    printf " of "
	fi
	printf "\"%s\"" "$1"
	if [ $# -gt 2 ]
	then
	    printf " repeated every "
	    plural $3 second second
	fi
	printf ", starting at %s\n" "`date "+%Y/%m/%d %H:%M:%S"`"
}

initmask()
{
	printf "%.$1d" 0
}

setbit()
{
	masklen=`expr length $1`

	if [ $2 -gt 1 ]
	then
	    printf "%s" `expr substr $1 1 \`expr $2 - 1\``
	fi

	printf "1"

	if [ $2 -lt $masklen ]
	then
	    printf "%s" `expr substr $1 \`expr $2 + 1\` \`expr $masklen - $2\``
	fi
}

isset()
{
	test `expr substr $1 $2 1` = "1"
}

sleepkill()
{
	sleep $1 > /dev/null	# redirection required so as to not stall pipe
	kill -$2 $3 2> /dev/null
}

run()
{
	header "$1" $2 $3

	if [ $# -lt 3 ]
	then
	    $1 2>&1 &
	else
	    {
		while [ 1 ]
		do
		    date +%H:%M:%S
		    $1 2>&1
		    printf "=======\n"
		    sleep $3
		done
	    } &
	fi
	runpid=$!

	reportkills=`initmask 64`
	if [ $# -gt 1 ]
	then
	    reportkills=`setbit $reportkills $SIGKILL`

	    sleepkill $2 $SIGTERM $runpid

	    {
		sleepkill $TIMEOUT $SIGKILL $runpid
	    } &
	else
	    reportkills=`setbit $reportkills $SIGKILL`
	    reportkills=`setbit $reportkills $SIGTERM`

	    {
		sleepkill $TIMEOUT $SIGTERM $runpid
		sleepkill $TIMEOUT $SIGKILL $runpid
	    } &
	fi
	waitpid=$!
	wait $runpid 2> /dev/null
	signal=`expr $? - 128`
	test $signal -gt 0 && isset $reportkills $signal &&
	{
	    printf "Command timed out; terminated using \"kill %s\".\n" -$signal
	}
	kill $waitpid 2> /dev/null

	wait 2> /dev/null

	printf "\n"
}

lsofrun()
{
	run "lsof -r1 $P4ROOT/* $P4ROOT/server.locks/*/* $P4JOURNAL $P4LOG" 15 \
	    | grep -v '^tail'
}

getpids()
{
	printf "ps -e | grep '%s$'" `echo $p4d | sed 's/.*\/\([^\/]*\)$/\1/'` \
	    | sh \
	    | sed 's/^[^0-9]*\([0-9]*\)[^0-9]*.*/\1/'
}

pidruns()
{
	getpids | while read pid
	do
	    run "lsof -p $pid"
	    run "strace -q -p $pid" 0.50
	done
}

collectfile()
{
	echo $outfile.`printf "%.2d" $1`
}

collect()
{
	ncollects=`expr $ncollects + 1`

	"$@" > `collectfile $ncollects` &
}

ncollects=0

printf "Collecting p4d state..."

collect run "$p4d -V"
collect run "$p4 -V"

collect run "uname -a"

collect run "tail -f -n10000 $P4LOG" 15
collect run "tail -f -n10000 $P4JOURNAL" 15

collect lsofrun

collect pidruns

collect run "ps -elfjH" 15 1
collect run "netstat -antp" 15 1

collect run "tail -f -n10000 /var/log/messages" 15

#
# Depending upon the state of the server, the following commands
# might not behave as expected.
#

collect run "$p4 monitor show -el" 15 1

#
# All commands have been started; wait for them to finish.
#
wait

icollect=0
while [ $icollect -lt $ncollects ]
do
	icollect=`expr $icollect + 1`

	collected=`collectfile $icollect`
	cat $collected
	rm -f $collected
done \
	> $outfile

printf " done.\n"

exit 0
# Change User Description Committed
#14 30154 Robert Cowham Fix incorrect logic to call check_config, and fix getpids when SDP case-insensitive p4d is running.
#13 30148 Robert Cowham Add usage comments for SDP version of script
#12 30147 Robert Cowham Changed filetype to +k p4dstate.sh
#11 29923 C. Thomas Tyler Updated HTML hyperlinks to use 'portal.perforce.com'.

This replaces currently broken links to 'answers.perforce.com' and
currently redirected links to 'community.perforce.com'.

#review-29924
#10 29709 Robert Cowham Customize lightly for SDP (sourcing variables for an instance) - requires use of /bin/bash rather than /bin/sh
#9 28979 Robert Cowham Collect lslocks output even if older version of lslocks with no JSON option.
#8 28731 C. Thomas Tyler Reset p4dstate.sh to latest version from FTP server:
http://ftp.perforce.com/perforce/tools/p4dstate/p4dstate.sh

Also added p4pstate.sh and p4brokerstate.sh with URLs:
http://ftp.perforce.com/perforce/tools/p4dstate/p4pstate.sh
http://ftp.perforce.com/perforce/tools/p4dstate/p4brokerstate.sh

These versions are not SDP-ified; these are the unmodified stock
versions from the FTP server.

TO DO Later: Enhance the stock SDP versions to be SDP-ready
but not SDP-dependent.
#7 27722 C. Thomas Tyler Refinements to @27712:
* Resolved one out-of-date file (verify_sdp.sh).
* Added missing adoc file for which HTML file had a change (WorkflowEnforcementTriggers.adoc).
* Updated revdate/revnumber in *.adoc files.
* Additional content updates in Server/Unix/p4/common/etc/cron.d/ReadMe.md.
* Bumped version numbers on scripts with Version= def'n.
* Generated HTML, PDF, and doc/gen files:
  - Most HTML and all PDF are generated using Makefiles that call an AsciiDoc utility.
  - HTML for Perl scripts is generated with pod2html.
  - doc/gen/*.man.txt files are generated with .../tools/gen_script_man_pages.sh.

#review-27712
#6 26368 Robert Cowham Shift lslocks earlier
#5 23435 Robert Cowham Convert tabs to spaces - whitespace only no other changes
#4 23434 Robert Cowham Require bash and fix problem with unspecified parameters to run().
#3 23424 Robert Cowham Update with latest changes from the original script (merged in).
//guest/perforce_software/admin_toolkit/p4dstate.sh
#2 19059 Russell C. Jackson (Rusty) Updated to use SDP variables
#1 16652 C. Thomas Tyler Added p4dstate.sh to SDP.
Changes from originating server (no functionality changes):
* Adjusted Copyright message to SDP standard, which references
the same basic content.
* Added comment clarifying that RCS values are verbatim from
the originating Helix Server, and read-only elsewhere.

To Do:
* Comments and usage instructions needed.
* SDP-ifiy it; i.e. have it take an instance parameter, and
which point all other settings (path to p4d and p4 executables,
P4ROOT and other environment variables) are known.

#review @michael_shields @robert_cowham