k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/hack/run-prometheus-on-etcd-scrapes.sh (about) 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 38 if (( $# != 1 )); then 39 echo "Usage: $0 \$scrapes_tar_pathname" >&2 40 exit 1 41 fi 42 43 scrapes_file="$1" 44 45 if ! [[ -r "$scrapes_file" ]]; then 46 echo "$0: $scrapes_file is not a readable file" >&2 47 exit 2 48 fi 49 50 SCRIPT_ROOT=$(dirname "${BASH_SOURCE[0]}") 51 52 CONFIG="/tmp/$(cd /tmp && mktemp config.XXXXXX)" 53 UNPACKDIR="/tmp/$(cd /tmp && mktemp -d unpack.XXXXXX)" 54 SERVER_PID="" 55 56 cleanup_prom() { 57 rm -f "$CONFIG" 58 rm -rf "$UNPACKDIR" 59 if [[ -n "$SERVER_PID" ]]; then 60 kill "$SERVER_PID" 61 fi 62 } 63 64 trap cleanup_prom EXIT 65 66 chmod +r "$CONFIG" "$UNPACKDIR" 67 68 tar xzf "$scrapes_file" -C "$UNPACKDIR" 69 70 if 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 }') 74 else 75 IPADDR=$(ifconfig | grep -w inet | grep -Fv 127.0.0. | head -1 | 76 awk '{ print $2 }' | awk -F/ '{ print $1 }') 77 fi 78 79 echo 80 echo "Historic metrics will be at http://\${any_local_address}:9091/\${any_path}" 81 echo "Prometheus will listen on port 9090 and scrape historic metrics from http://${IPADDR}:9091/metrics" 82 sleep 1 83 echo 84 85 cat > "$CONFIG" <<EOF 86 global: 87 scrape_interval: 30s 88 89 scrape_configs: 90 91 - job_name: local 92 static_configs: 93 - targets: ['${IPADDR}:9091'] 94 EOF 95 96 "${SCRIPT_ROOT}/serve-prom-scrapes.sh" 9091 "$UNPACKDIR" & 97 SERVER_PID=$! 98 docker run -p 9090:9090 -v "${CONFIG}:/config.yaml" prom/prometheus --config.file=/config.yaml --storage.tsdb.retention.time=3650d