44# ^^^ the above line is purely for shellcheck to treat this as a bash-like script
55# (OpenWRT's ash from busybox is kinda similar but there still could be issues)
66
7- _TMPDIR=$( mktemp -d -p /tmp loki_exporter.XXXXXX)
7+ _TMPDIR=" $( mktemp -d -p /tmp loki_exporter.XXXXXX) "
88PIPE_NAME=" /${_TMPDIR} /loki_exporter.pipe"
99BULK_DATA=" /${_TMPDIR} /loki_exporter.boot"
1010
@@ -16,17 +16,48 @@ LOKI_BULK_TEMPLATE_FOOTER="]}]}"
1616DATETIME_STR_FORMAT=" %a %b %d %H:%M:%S %Y"
1717OS=$( uname -s | tr " [:upper:]" " [:lower:]" )
1818
19+ USER_AGENT=" openwrt-loki-exporter/%% VERSION %%"
20+ BUILD_ID=" %% BUILD_ID %%"
21+
22+ _curl_bulk_cmd () {
1923if [ " ${AUTOTEST-0} " -eq 1 ]; then
20- _CURL_BULK_CMD=(curl --no-progress-meter -fv -H " Content-Type: application/json" -H " Content-Encoding: gzip" -H " Connection: close" )
21- _CURL_CMD=(curl --no-progress-meter -fv -H " Content-Type: application/json" -H " Connection: close" )
24+ curl --no-progress-meter -fv \
25+ -H " Content-Type: application/json" \
26+ -H " Content-Encoding: gzip" \
27+ -H " User-Agent: ${USER_AGENT} " \
28+ -H " Connection: close" \
29+ " $@ "
2230else
23- _CURL_BULK_CMD=(curl -fsS -H " Content-Type: application/json" -H " Content-Encoding: gzip" -H " Authorization: Basic ${LOKI_AUTH_HEADER} " -H " Connection: close" )
24- _CURL_CMD=(curl -fsS -H " Content-Type: application/json" -H " Authorization: Basic ${LOKI_AUTH_HEADER} " -H " Connection: close" )
31+ curl -fsS \
32+ -H " Content-Type: application/json" \
33+ -H " Content-Encoding: gzip" \
34+ -H " User-Agent: ${USER_AGENT} " \
35+ -H " Authorization: Basic ${LOKI_AUTH_HEADER} " \
36+ -H " Connection: close" \
37+ " $@ "
2538fi
39+ }
40+
41+ _curl_cmd () {
42+ if [ " ${AUTOTEST-0} " -eq 1 ]; then
43+ curl --no-progress-meter -fv \
44+ -H " Content-Type: application/json" \
45+ -H " User-Agent: ${USER_AGENT} " \
46+ -H " Connection: close" \
47+ " $@ "
48+ else
49+ curl -fsS \
50+ -H " Content-Type: application/json" \
51+ -H " User-Agent: ${USER_AGENT} " \
52+ -H " Authorization: Basic ${LOKI_AUTH_HEADER} " \
53+ -H " Connection: close" \
54+ " $@ "
55+ fi
56+ }
2657
2758_setup () {
2859 mkfifo " ${PIPE_NAME} "
29- echo " started with BOOT=${BOOT} " >&2
60+ echo " openwrt-loki-exporter/ ${BUILD_ID} started with BOOT=${BOOT} " >&2
3061}
3162
3263_teardown () {
@@ -73,7 +104,7 @@ _do_bulk_post() {
73104 echo " ${post_body} " | gzip > " ${_log_file} .payload.gz"
74105 rm -f " ${_log_file} "
75106
76- if ! " ${_CURL_BULK_CMD[@]} " --data-binary " @${_log_file} .payload.gz" " ${LOKI_PUSH_URL} " > " ${_log_file} .payload.gz-response" 2>&1 ; then
107+ if ! _curl_bulk_cmd --data-binary " @${_log_file} .payload.gz" " ${LOKI_PUSH_URL} " > " ${_log_file} .payload.gz-response" 2>&1 ; then
77108 echo " BULK POST FAILED: leaving ${_log_file} .payload.gz for now"
78109 fi
79110}
@@ -211,7 +242,7 @@ _main_loop() {
211242 post_body=" ${post_body/ TIMESTAMP/ $ts_ns } "
212243 post_body=" ${post_body/ MESSAGE/ $msg } "
213244
214- if ! " ${_CURL_CMD[@]} " -d " ${post_body} " " ${LOKI_PUSH_URL} " ; then
245+ if ! _curl_cmd -d " ${post_body} " " ${LOKI_PUSH_URL} " ; then
215246 echo " POST FAILED: '${post_body} '"
216247 fi
217248
0 commit comments