-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsend.sh
119 lines (94 loc) · 2.63 KB
/
send.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
#!/bin/bash
### SEND METRICS TO THE API ###
### DO NOT MODIFY THIS FILE ###
# Configuration
source config.env
source params.env
# Common code
source common.sh
METRICS_ENDPOINT=${BASE_API}/metrics
# Function to filter the output of 'ldvs' command and return the filtered lines
get_params() {
local filter="grep -E \"^("
local idx=0
for param in "$@"; do
filter+="$param"
if [ $idx -lt $(($#-1)) ]; then
filter+="|"
fi
idx=$((idx+1))
done
filter+="),\""
printf "%s\n" "$($ldvs | eval "$filter")"
}
# Function to convert a line of output from 'ldvs' command to json format
line2json() {
key="$1"
value="$2"
res=""
# null value
if [[ "$value" == "@invalid" ]] || [[ "$value" == "--" ]] || [[ "$value" == "none" ]]; then
res="null"
# boolean values
elif [[ "$value" == "true" ]] || [[ "$value" == "false" ]]; then
res="$value"
elif [[ "$value" == "ON" ]]; then
res="true"
elif [[ "$value" == "OFF" ]]; then
res="false"
# numeric values
elif [[ "$value" =~ ^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$ ]]; then
res="$value"
# string values
else
res="\"$value\""
fi
echo "\"$key\": $res"
}
# Determine the parameters to be sent to the API based on the current time
params_output=""
hour=$(date +%H)
minute=$(date +%M)
# Check if the current time is 23:59, or if we ran with -f flag.
# if so, send all parameters (force update all)
if ( [ $hour == "23" ] && [ $minute == "59" ] ) || [ "$1" == "-f" ]; then
params_output+=$(get_params "${fast_params[@]}" "${slow_params[@]}" "${eventual_params[@]}")
# Check if launched with some argument, then send for only that parameter
elif [ $# -eq 1 ]; then
params_output+=$(get_params "$1")
# Send partial parameters based every 5 minutes
elif [ $((10#$minute % 5)) -eq 0 ]; then
params_output+=$(get_params "${fast_params[@]}" "${slow_params[@]}")
# Send only frequently changed parameters every minute
else
params_output+=$(get_params "${fast_params[@]}")
fi
# Check if ldvs/grep command exited successfully before proceeding
if [ $? -ne 0 ]; then
echo "Error: ldvs command failed."
exit 1
fi
# Generate the JSON document
json="{"
first=true
while IFS=',' read -ra line; do
key="${line[0]}"
value="${line[1]}"
if [ "$first" = true ]; then
first=false
else
json+=", "
fi
json+=$(line2json "$key" "$value")
done <<< "$params_output"
json+="}"
# Debug print JSON
if [ "$LOCAL_DEBUG" = 1 ]; then
echo $json
fi
# Send the JSON document as a POST request, to the metrics endpoint
curl $CURL_OPTS \
-H "Content-Type: application/json" \
-H "Authorization: Basic $BASIC_AUTH" \
-d "$json" \
$METRICS_ENDPOINT