blob: f1eac1471967af74f2dc2d93463fac6b236f03b9 [file] [log] [blame]
danielhanrahan1b65fa52023-02-11 13:25:22 +00001#!/bin/bash
2#
3# Copyright 2023 Nordix Foundation.
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
18set -o errexit # Exit on most errors
19set -o nounset # Disallow expansion of unset variables
20set -o pipefail # Use last non-zero exit code in a pipeline
21#set -o xtrace # Uncomment for debugging
22
23function script_usage() {
24 cat <<EOF
25Usage:
26 -h|--help Displays this help
27 -u|--metrics-url=URL URL to Prometheus metrics
28 Default: http://localhost:8887/manage/prometheus
29 -o|--output=FILE Path to output file
30 Default: metrics-reports/metrics-[timestamp].tsv
31EOF
32}
33
34function parse_params() {
35 # Set parameter defaults
36 METRICS_URL="http://localhost:8887/manage/prometheus"
37 OUTFILE="metrics-reports/metrics-$(date --iso-8601=seconds).tsv"
38 TEMP_DIR="/tmp/cps-metrics"
39
40 # Parse parameters
41 local param
42 while [[ $# -gt 0 ]]; do
43 param="$1"
44 shift
45 case $param in
46 -h | --help)
47 script_usage
48 exit 0
49 ;;
50 -u | --metrics-url)
51 METRICS_URL=$1
52 shift
53 ;;
54 -o | --output)
55 OUTFILE=$1
56 shift
57 ;;
58 *)
59 echo "Invalid parameter was provided: $param" >&2
60 script_usage
61 exit 1
62 ;;
63 esac
64 done
65}
66
67function generate_report() {
68 # Create needed directories.
69 mkdir -p $TEMP_DIR "$(dirname "$OUTFILE")"
70
71 # Scrape raw metrics (suppress progress meter).
72 curl --fail --silent --show-error --output $TEMP_DIR/metrics-raw.txt "$METRICS_URL"
73
74 # Remove comments, sort by name, and separate by tabs.
75 grep --invert-match "^#" $TEMP_DIR/metrics-raw.txt | sort | sed 's/,[}]/}\t/' >$TEMP_DIR/metrics-all.txt
76
77 # Extract useful metrics.
78 grep -E "^cps_|^spring_data_" $TEMP_DIR/metrics-all.txt >$TEMP_DIR/metrics-cps.txt
79
80 # Extract into columns.
81 grep "_count" $TEMP_DIR/metrics-cps.txt | sed 's/_count//' | cut -f 1 >$TEMP_DIR/column1.txt
82 grep "_count" $TEMP_DIR/metrics-cps.txt | cut -f 2 >$TEMP_DIR/column2.txt
83 grep "_sum" $TEMP_DIR/metrics-cps.txt | cut -f 2 >$TEMP_DIR/column3.txt
84 grep "_max" $TEMP_DIR/metrics-cps.txt | cut -f 2 >$TEMP_DIR/column4.txt
85
86 # Combine columns into report.
87 paste $TEMP_DIR/column{1,2,3,4}.txt >$TEMP_DIR/report.txt
88
89 # Sort by Sum (column 3), descending.
90 sort --general-numeric-sort --reverse --field-separator=$'\t' --key=3 $TEMP_DIR/report.txt >$TEMP_DIR/report-sorted.txt
91
92 # Compile final report, with column headers.
93 echo -e "Method\tCount\tSum\tMax" >"$OUTFILE"
94 cat $TEMP_DIR/report-sorted.txt >>"$OUTFILE"
95
96 # Output path to generated file
97 echo "$OUTFILE"
98}
99
100function cleanup() {
101 rm -f $TEMP_DIR/* && rmdir $TEMP_DIR 2>/dev/null
102}
103# Set up the cleanup function to be triggered upon script exit
104trap cleanup EXIT
105
106# Main script logic
107parse_params "$@"
108generate_report
109exit 0