Repository navigation
Expand file tree
/
Copy pathp4monitor_locks.sh
More file actions
executable file
·125 lines (105 loc) · 4.36 KB
/
Copy pathp4monitor_locks.sh
File metadata and controls
executable file
·125 lines (105 loc) · 4.36 KB
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
#!/bin/bash
# Generate lock monitoring metrics and log file for use with Prometheus (collected via node_exporter)
# Calls the underlying script monitor_locks.py
# Note that the Python script requires the 'lslocks' utility to be installed.
#
# If used, put this job into perforce user crontab, for SDP, e.g. where INSTANCE=1:
#
# */1 * * * * /p4/common/site/bin/p4monitor_locks.sh $INSTANCE > /dev/null 2>&1 ||:
#
# For non-SDP installation, either specify port/user or ensure P4PORT and P4USER are set in environment:
#
# */1 * * * * /p4/common/site/bin/p4monitor_locks.sh -nosdp -p server:1666 -u p4admin > /dev/null 2>&1 ||:
#
# If not using SDP then please ensure that appropriate LONG TERM TICKET is setup in the environment
# that this script is running.
#
# You can specify metrics root director (for use with node_exporter) with: -m /my_metrics
#
# Please note you need to make sure that the specified directory below (which may be linked)
# can be read by the node_exporter user (and is setup via --collector.textfile.directory parameter)
#
# This might also be /hxlogs/metrics or /var/metrics, and can be set via the "-m" parameter to script.
metrics_root=/p4/metrics
function msg () { echo -e "$*"; }
function bail () { msg "\nError: ${1:-Unknown Error}\n"; exit ${2:-1}; }
function usage
{
declare style=${1:--h}
declare errorMessage=${2:-Unset}
if [[ "$errorMessage" != Unset ]]; then
echo -e "\\n\\nUsage Error:\\n\\n$errorMessage\\n\\n" >&2
fi
echo "USAGE for p4monitor_locks.sh:
p4monitor_locks.sh [<instance> | -nosdp] [-p <port>] | [-u <user>] | [-m <metrics_dir>] [-L <log_file>] [-c <config_file>]
or
p4monitor_locks.sh -h
E.g.
p4monitor_locks.sh 1
p4monitor_locks.sh 1 -c /p4/common/config/p4monitor_locks.yaml
p4monitor_locks.sh -nosdp -p server:1666 -u p4admin -m /var/metrics -L /var/metrics/p4monitor_locks.log -c /etc/p4/p4monitor_locks.yaml
"
}
# Command Line Processing
declare -i shiftArgs=0
declare -i UseSDP=1
declare logfile=""
declare configfile=""
set +u
while [[ $# -gt 0 ]]; do
case $1 in
(-h) usage -h;;
# (-man) usage -man;;
(-p) Port=$2; shiftArgs=1;;
(-u) User=$2; shiftArgs=1;;
(-m) metrics_root=$2; shiftArgs=1;;
(-L) logfile=$2; shiftArgs=1;;
(-c) configfile=$2; shiftArgs=1;;
(-nosdp) UseSDP=0;;
(-*) usage -h "Unknown command line option ($1)." && exit 1;;
(*) export SDP_INSTANCE=$1;;
esac
# Shift (modify $#) the appropriate number of times.
shift; while [[ "$shiftArgs" -gt 0 ]]; do
[[ $# -eq 0 ]] && usage -h "Incorrect number of arguments."
shiftArgs=$shiftArgs-1
shift
done
done
set -u
[[ -d "$metrics_root" ]] || bail "Specified metrics directory '$metrics_root' does not exist!"
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
if [[ $UseSDP -eq 1 ]]; then
SDP_INSTANCE=${SDP_INSTANCE:-Unset}
SDP_INSTANCE=${1:-$SDP_INSTANCE}
if [[ $SDP_INSTANCE == Unset ]]; then
echo -e "\\nError: Instance parameter not supplied.\\n"
echo "You must supply the Perforce SDP instance as a parameter to this script or use flag: -nosdp."
exit 1
fi
[[ -d "/p4/$SDP_INSTANCE" ]] || bail "Specified SDP instance '/p4/$SDP_INSTANCE' does not exist!"
source /p4/common/bin/p4_vars "$SDP_INSTANCE"
else
p4port=${Port:-$P4PORT}
p4user=${User:-$P4USER}
export P4PORT=${p4port}
export P4USER=${p4user}
export P4BIN=${P4BIN:-p4}
[[ -z "$logfile" ]] && logfile="$metrics_root/p4monitor_locks.log"
fi
# Build optional arguments
declare config_arg=""
[[ -n "$configfile" ]] && config_arg="-c $configfile"
# Adjust to your script location if required
# Use venv if you want to run in a virtual environment, otherwise just run the script directly.
# Reccommended to use a virtual environment to avoid dependency issues with other Python packages
# and use Astral's uv to manage python and dependencies. See https://astral.sh/docs/uv/ for more information.
# This script imports "pyyaml"
[[ -f "$SCRIPT_DIR/.venv/bin/activate" ]] && source "$SCRIPT_DIR/.venv/bin/activate"
if [[ $UseSDP -eq 1 ]]; then
# shellcheck disable=SC2086
"$SCRIPT_DIR"/monitor_locks.py -i "$SDP_INSTANCE" -m "$metrics_root" $config_arg
else
# shellcheck disable=SC2086
"$SCRIPT_DIR"/monitor_locks.py -m "$metrics_root" -p "$p4port" -u "$p4user" -L "$logfile" $config_arg
fi