...
Text file
src/k8s.io/kubernetes/hack/run-prometheus-on-etcd-scrapes.sh
1#!/usr/bin/env bash
2
3# Copyright 2021 The Kubernetes Authors.
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17# Unpacks a tarfile of etcd scrapes and runs a simple web server exposing it
18# and a Prometheus server scraping that simple web server.
19# The simple web server listens on port 9091.
20# The Prometheus server is run in a container and looks for the
21# simple web server at the host's first global IPv4 address.
22
23# Usage: $0 scrapes_tar_pathname
24#
25# Where scrapes_tar_pathname is a gzipped tar archive containing
26# files whose name is of the form
27# <timestamp>.scrape
28# where <timestamp> is seconds since Jan 1, 1970 UTC.
29# Each such file is taken to be a scrape that lacks timestamps,
30# and the timestamp from the filename is multiplied by the necessary 1000
31# and added to the data in that file.
32
33# This requires a:
34# - `docker run` command
35# - an `ip` or `ifconfig` command that this script knows how to wrangle
36# - an `nc` command that serve-prom-scrapes.sh knows how to wrangle
37
38if (( $# != 1 )); then
39 echo "Usage: $0 \$scrapes_tar_pathname" >&2
40 exit 1
41fi
42
43scrapes_file="$1"
44
45if ! [[ -r "$scrapes_file" ]]; then
46 echo "$0: $scrapes_file is not a readable file" >&2
47 exit 2
48fi
49
50SCRIPT_ROOT=$(dirname "${BASH_SOURCE[0]}")
51
52CONFIG="/tmp/$(cd /tmp && mktemp config.XXXXXX)"
53UNPACKDIR="/tmp/$(cd /tmp && mktemp -d unpack.XXXXXX)"
54SERVER_PID=""
55
56cleanup_prom() {
57 rm -f "$CONFIG"
58 rm -rf "$UNPACKDIR"
59 if [[ -n "$SERVER_PID" ]]; then
60 kill "$SERVER_PID"
61 fi
62}
63
64trap cleanup_prom EXIT
65
66chmod +r "$CONFIG" "$UNPACKDIR"
67
68tar xzf "$scrapes_file" -C "$UNPACKDIR"
69
70if which ip > /dev/null; then
71 IPADDR=$(ip addr show scope global up |
72 grep -w inet | head -1 |
73 awk '{ print $2 }' | awk -F/ '{ print $1 }')
74else
75 IPADDR=$(ifconfig | grep -w inet | grep -Fv 127.0.0. | head -1 |
76 awk '{ print $2 }' | awk -F/ '{ print $1 }')
77fi
78
79echo
80echo "Historic metrics will be at http://\${any_local_address}:9091/\${any_path}"
81echo "Prometheus will listen on port 9090 and scrape historic metrics from http://${IPADDR}:9091/metrics"
82sleep 1
83echo
84
85cat > "$CONFIG" <<EOF
86global:
87 scrape_interval: 30s
88
89scrape_configs:
90
91- job_name: local
92 static_configs:
93 - targets: ['${IPADDR}:9091']
94EOF
95
96"${SCRIPT_ROOT}/serve-prom-scrapes.sh" 9091 "$UNPACKDIR" &
97SERVER_PID=$!
98docker run -p 9090:9090 -v "${CONFIG}:/config.yaml" prom/prometheus --config.file=/config.yaml --storage.tsdb.retention.time=3650d
View as plain text