-
Notifications
You must be signed in to change notification settings - Fork 24
/
add_metrics_to_readme.sh
executable file
·81 lines (60 loc) · 2.52 KB
/
add_metrics_to_readme.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
#! /usr/bin/env bash
function getMetrics() {
curl -Ss http://localhost:9090/api/v1/targets/metadata | jq -c '.data[] | select(.metric | startswith("logstash"))' | while read -r line; do
metric=$(echo "$line" | jq -r '.metric')
type=$(echo "$line" | jq -r '.type')
help=$(echo "$line" | jq -r '.help')
echo "| $metric | $type | $help |"
done
}
function configChangeReload() {
local logstashCID
logstashCID=$( docker ps -a | grep 'logstash-exporter-logstash-1' | awk '{print $1}' )
local logstashConf
logstashConf='/usr/share/logstash/pipeline/logstash.conf'
# "logstash pid should be always 1 AFAIK" ~ Kuskoman, 2024
local logstashPID
logstashPID=1
# *** create failing config ***
docker exec -it "$logstashCID" sh -c "echo 'Wrong Config' >> $logstashConf"
# reload logstash
docker exec -it "$logstashCID" sh -c "kill -1 $logstashPID"
# backup original config
# walk around with 'cp' to avoid replacing config inside container
docker exec "$logstashCID" sh -c "sed '\$d' $logstashConf > /tmp/prev_logstash.conf"
docker exec "$logstashCID" sh -c "cp /tmp/prev_logstash.conf $logstashConf"
# reload logstash
docker exec -it "$logstashCID" sh -c "kill -1 $logstashPID"
# *****************************
# *** create successful config ***
# delete lines of 'generator' config
docker exec "$logstashCID" sh -c "sed '2,7d' $logstashConf > /tmp/new_logstash.conf"
docker exec "$logstashCID" sh -c "cp /tmp/new_logstash.conf $logstashConf"
# reload logstash
docker exec -it "$logstashCID" sh -c "kill -1 $logstashPID"
# restore backup
docker exec "$logstashCID" sh -c "cp /tmp/prev_logstash.conf $logstashConf"
# reload logstash
docker exec -it "$logstashCID" sh -c "kill -1 $logstashPID"
# ********************************
}
configChangeReload
FILE=README.md
while IFS= read -r line; do LINES+=("$line"); done < $FILE
# remove number of lines from the beginning
LINES=("${LINES[@]:1}")
startLine=$(grep -n "^<!-- METRICS_TABLE_START -->" $FILE | awk -F: '{print $1}')
endLine=$(grep -n "^<!-- METRICS_TABLE_END -->" $FILE | awk -F: '{print $1}')
metricsTable="| Name | Type | Description |
| ----------- | ----------- | ----------- |
$(getMetrics | sort --version-sort)"
for ((i=0; i<${#LINES[@]}; i++)); do
if [ $i -eq "$startLine" ]; then
echo -e "${LINES[i]}"
echo "$metricsTable"
elif [ $i -gt "$startLine" ] && [ $i -lt $((endLine-2)) ]; then # -2 because of the empty line before the end marker
continue
else
echo -e "${LINES[i]}"
fi
done > $FILE