-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathupdate_p4prom.sh
executable file
·285 lines (223 loc) · 8.03 KB
/
update_p4prom.sh
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
#!/bin/bash
# Updates the following: p4prometheus, node_exporter.
#
# First version assumes SDP environment.
#
# shellcheck disable=SC2128
if [[ -z "${BASH_VERSINFO}" ]] || [[ -z "${BASH_VERSINFO[0]}" ]] || [[ ${BASH_VERSINFO[0]} -lt 4 ]]; then
echo "This script requires Bash version >= 4";
exit 1;
fi
# ============================================================
# Configuration section
# This might also be /hxlogs/metrics or passed as a parameter (with -m flag)
metrics_root=/hxlogs/metrics
metrics_link=/p4/metrics
local_bin_dir=/usr/local/bin
VER_NODE_EXPORTER="1.3.1"
VER_P4PROMETHEUS="0.8.8"
# Default to amd but allow arm architecture
arch="amd64"
[[ $(uname -p) == 'aarch64' ]] && arch="arm64"
# ============================================================
function msg () { echo -e "$*"; }
function bail () { msg "\nError: ${1:-Unknown Error}\n"; exit "${2:-1}"; }
function usage
{
declare errorMessage=${2:-Unset}
if [[ "$errorMessage" != Unset ]]; then
echo -e "\\n\\nUsage Error:\\n\\n$errorMessage\\n\\n" >&2
fi
echo "USAGE for update_p4prom.sh:
update_p4prom.sh [<instance> | -nosdp] [-m <metrics_root>] [-l <metrics_link>] [-u <osuser>]
or
update_p4prom.sh -h
<metrics_root> is the directory where metrics will be written - default: $metrics_root
<metrics_link> is an alternative link to metrics_root where metrics will be written - default: $metrics_link
Typically only used for SDP installations.
<osuser> Operating system user, e.g. perforce, under which p4d process is running
Specify either the SDP instance (e.g. 1), or -nosdp
WARNING: If using -nosdp, then please ensure P4PORT and P4USER are appropriately set and that you can connect
to your server (e.g. you have done a 'p4 trust' if required, and logged in already)
Examples:
./update_p4prom.sh 1
./update_p4prom.sh -nosdp -m /p4metrics -u perforce
"
}
# Command Line Processing
declare -i shiftArgs=0
declare -i UseSDP=1
declare -i SELinuxEnabled=0
declare OsUser=""
declare P4LOG=""
set +u
while [[ $# -gt 0 ]]; do
case $1 in
(-h) usage -h && exit 1;;
# (-man) usage -man;;
(-m) metrics_root=$2; shiftArgs=1;;
(-u) OsUser="$2"; shiftArgs=1;;
(-nosdp) UseSDP=0;;
(-l) P4LOG="$2"; shiftArgs=1;;
(-*) 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
if [[ $(id -u) -ne 0 ]]; then
echo "$0 can only be run as root or via sudo"
exit 1
fi
wget=$(which wget)
[[ $? -eq 0 ]] || bail "Failed to find wget in path"
if command -v getenforce > /dev/null; then
selinux=$(getenforce)
[[ "$selinux" == "Enforcing" ]] && SELinuxEnabled=1
fi
[[ -d "$metrics_root" ]] || bail "Specified metrics directory '$metrics_root' does not exist!"
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. E.g."
echo " update_p4prom.sh 1"
exit 1
fi
# Find OSGROUP for ownership permissions - group of /p4 dir itself
# shellcheck disable=SC2010
OSGROUP=$(ls -al /p4/ | grep -E '\.$' | head -1 | awk '{print $4}')
# Load SDP controlled shell environment.
# shellcheck disable=SC1091
source /p4/common/bin/p4_vars "$SDP_INSTANCE" ||\
{ echo -e "\\nError: Failed to load SDP environment.\\n"; exit 1; }
p4="$P4BIN -u $P4USER -p $P4PORT"
$p4 info -s || bail "Can't connect to P4PORT: $P4PORT"
p4prom_config_dir="/p4/common/config"
p4prom_bin_dir="/p4/common/site/bin"
else
SDP_INSTANCE=""
p4port=${Port:-$P4PORT}
p4user=${User:-$P4USER}
OSUSER="$OsUser"
OSGROUP=$(id -gn "$OSUSER")
p4="p4 -u $p4user -p $p4port"
$p4 info -s || bail "Can't connect to P4PORT: $p4port"
p4prom_config_dir="/etc/p4prometheus"
p4prom_bin_dir="$p4prom_config_dir"
fi
p4prom_config_file="$p4prom_config_dir/p4prometheus.yaml"
download_and_untar () {
fname=$1
url=$2
[[ -f "$fname" ]] && rm -f "$fname"
msg "downloading and extracting $url"
wget -q "$url"
tar zxvf "$fname"
}
update_node_exporter () {
userid="node_exporter"
if ! grep -q "^$userid:" /etc/passwd ;then
useradd --no-create-home --shell /bin/false "$userid" || bail "Failed to create user"
msg "Created user $userid"
fi
curr_ver=$(node_exporter --version | grep ' version ' | awk '{print $3}')
if [[ "$curr_ver" == "$VER_NODE_EXPORTER" ]]; then
msg "Current version $curr_ver of node_exporter is up-to-date"
return
fi
sudo systemctl stop node_exporter
cd /tmp || bail "Failed to cd to /tmp"
PVER="$VER_NODE_EXPORTER"
fname="node_exporter-$PVER.linux-${arch}.tar.gz"
[[ -d node_exporter-$PVER.linux-${arch} ]] && rm -rf node_exporter-$PVER.linux-${arch}
download_and_untar "$fname" "https://github.com/prometheus/node_exporter/releases/download/v$PVER/$fname"
msg "Installing node_exporter"
mv node_exporter-$PVER.linux-${arch}/node_exporter ${local_bin_dir}/
if [[ $SELinuxEnabled -eq 1 ]]; then
bin_file=${local_bin_dir}/node_exporter
semanage fcontext -a -t bin_t $bin_file
restorecon -vF $bin_file
fi
mkdir -p "$metrics_root"
chown "$OSUSER:$OSGROUP" "$metrics_root"
chmod 755 "$metrics_root"
f=$(readlink -f "$metrics_root")
while [[ $f != / ]]; do chmod 755 "$f"; f=$(dirname "$f"); done;
if [[ $UseSDP -eq 1 ]]; then
ln -s "$metrics_root" "$metrics_link"
chown -h "$OSUSER:$OSGROUP" "$metrics_link"
fi
msg "Creating service file for node_exporter"
cat << EOF > /etc/systemd/system/node_exporter.service
[Unit]
Description=Node Exporter
Wants=network-online.target
After=network-online.target
[Service]
User=$userid
Group=$userid
Type=simple
ExecStart=${local_bin_dir}/node_exporter --collector.systemd \
--collector.systemd.unit-include=(p4.*|node_exporter).service \
--collector.textfile.directory=$metrics_root
[Install]
WantedBy=multi-user.target
EOF
sudo systemctl daemon-reload
sudo systemctl enable node_exporter
sudo systemctl start node_exporter
sudo systemctl status node_exporter --no-pager
}
update_p4prometheus () {
curr_ver=$(p4prometheus --version | grep 'p4prometheus, version ' | awk '{print $3}')
if [[ "$curr_ver" == "v$VER_P4PROMETHEUS" ]]; then
msg "Current version $curr_ver of p4prometheus is up-to-date"
return
fi
systemctl stop p4prometheus
PVER="$VER_P4PROMETHEUS"
fname="p4prometheus.linux-${arch}.gz"
url="https://github.com/perforce/p4prometheus/releases/download/v$PVER/$fname"
msg "downloading and extracting $url"
[[ -e p4prometheus.linux-${arch} ]] && rm -f p4prometheus.linux-${arch}
wget -q "$url"
gunzip "$fname"
chmod +x p4prometheus.linux-${arch}
mv p4prometheus.linux-${arch} ${local_bin_dir}/p4prometheus
if [[ $SELinuxEnabled -eq 1 ]]; then
bin_file=${local_bin_dir}/p4prometheus
semanage fcontext -a -t bin_t $bin_file
restorecon -vF $bin_file
fi
msg "Creating service file for p4prometheus"
cat << EOF > /etc/systemd/system/p4prometheus.service
[Unit]
Description=P4prometheus
Documentation=https://github.com/perforce/p4prometheus/blob/master/README.md
Wants=network-online.target
After=network-online.target
[Service]
User=$OSUSER
Group=$OSGROUP
Type=simple
ExecStart=${local_bin_dir}/p4prometheus --config=$p4prom_config_file
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable p4prometheus
systemctl start p4prometheus
systemctl status p4prometheus --no-pager
}
update_node_exporter
update_p4prometheus
echo "
Should have updated node_exporter and p4prometheus.
"