From a90f0127d407fb70943b226f744d7396b3415363 Mon Sep 17 00:00:00 2001 From: Andrei Belov Date: Sat, 21 Dec 2024 18:29:52 +0400 Subject: [PATCH 1/2] Add User-Agent header --- Makefile | 6 +++++- loki_exporter.sh | 15 +++++++++------ 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index ce1844d..7476600 100644 --- a/Makefile +++ b/Makefile @@ -244,7 +244,11 @@ loki-exporter: loki_exporter.sh loki_exporter.init loki_exporter.conf for f in loki_exporter.init loki_exporter.conf; do \ install -m 644 $(TOPDIR)/$${f} $(TOPDIR)/$@/files/ ; \ done - install -m 755 $(TOPDIR)/loki_exporter.sh $(TOPDIR)/$@/files/loki_exporter.sh + sed \ + -e "s,%% VERSION %%,$(VERSION),g" \ + -e "s,%% BUILD_ID %%,$(BUILD),g" \ + < $(TOPDIR)/loki_exporter.sh > $(TOPDIR)/$@/files/loki_exporter.sh + chmod 755 $(TOPDIR)/$@/files/loki_exporter.sh .PHONY: package package: loki-exporter ## Build OpenWRT package diff --git a/loki_exporter.sh b/loki_exporter.sh index f464c5d..de400b9 100755 --- a/loki_exporter.sh +++ b/loki_exporter.sh @@ -4,7 +4,7 @@ # ^^^ the above line is purely for shellcheck to treat this as a bash-like script # (OpenWRT's ash from busybox is kinda similar but there still could be issues) -_TMPDIR=$(mktemp -d -p /tmp loki_exporter.XXXXXX) +_TMPDIR="$(mktemp -d -p /tmp loki_exporter.XXXXXX)" PIPE_NAME="/${_TMPDIR}/loki_exporter.pipe" BULK_DATA="/${_TMPDIR}/loki_exporter.boot" @@ -16,17 +16,20 @@ LOKI_BULK_TEMPLATE_FOOTER="]}]}" DATETIME_STR_FORMAT="%a %b %d %H:%M:%S %Y" OS=$(uname -s | tr "[:upper:]" "[:lower:]") +USER_AGENT="openwrt-loki-exporter/%% VERSION %%" +BUILD_ID="%% BUILD_ID %%" + if [ "${AUTOTEST-0}" -eq 1 ]; then - _CURL_BULK_CMD=(curl --no-progress-meter -fv -H "Content-Type: application/json" -H "Content-Encoding: gzip" -H "Connection: close") - _CURL_CMD=(curl --no-progress-meter -fv -H "Content-Type: application/json" -H "Connection: close") + _CURL_BULK_CMD=(curl --no-progress-meter -fv -H "Content-Type: application/json" -H "Content-Encoding: gzip" -H "Connection: close" -H "User-Agent: ${USER_AGENT}") + _CURL_CMD=(curl --no-progress-meter -fv -H "Content-Type: application/json" -H "Connection: close" -H "User-Agent: ${USER_AGENT}") else - _CURL_BULK_CMD=(curl -fsS -H "Content-Type: application/json" -H "Content-Encoding: gzip" -H "Authorization: Basic ${LOKI_AUTH_HEADER}" -H "Connection: close") - _CURL_CMD=(curl -fsS -H "Content-Type: application/json" -H "Authorization: Basic ${LOKI_AUTH_HEADER}" -H "Connection: close") + _CURL_BULK_CMD=(curl -fsS -H "Content-Type: application/json" -H "Content-Encoding: gzip" -H "Authorization: Basic ${LOKI_AUTH_HEADER}" -H "Connection: close" -H "User-Agent: ${USER_AGENT}") + _CURL_CMD=(curl -fsS -H "Content-Type: application/json" -H "Authorization: Basic ${LOKI_AUTH_HEADER}" -H "Connection: close" -H "User-Agent: ${USER_AGENT}") fi _setup() { mkfifo "${PIPE_NAME}" - echo "started with BOOT=${BOOT}" >&2 + echo "${USER_AGENT} (${BUILD_ID}) started with BOOT=${BOOT}" >&2 } _teardown() { From f56c5ec3efdd3fac8a9179d90bd1420d55fd13b5 Mon Sep 17 00:00:00 2001 From: Andrei Belov Date: Sat, 21 Dec 2024 19:28:51 +0400 Subject: [PATCH 2/2] Use functions instead of array expansion to run curl It turned out ash does not have bash-like array support at all. --- loki_exporter.sh | 42 +++++++++++++++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 7 deletions(-) diff --git a/loki_exporter.sh b/loki_exporter.sh index de400b9..066fcd8 100755 --- a/loki_exporter.sh +++ b/loki_exporter.sh @@ -19,17 +19,45 @@ OS=$(uname -s | tr "[:upper:]" "[:lower:]") USER_AGENT="openwrt-loki-exporter/%% VERSION %%" BUILD_ID="%% BUILD_ID %%" +_curl_bulk_cmd() { if [ "${AUTOTEST-0}" -eq 1 ]; then - _CURL_BULK_CMD=(curl --no-progress-meter -fv -H "Content-Type: application/json" -H "Content-Encoding: gzip" -H "Connection: close" -H "User-Agent: ${USER_AGENT}") - _CURL_CMD=(curl --no-progress-meter -fv -H "Content-Type: application/json" -H "Connection: close" -H "User-Agent: ${USER_AGENT}") + curl --no-progress-meter -fv \ + -H "Content-Type: application/json" \ + -H "Content-Encoding: gzip" \ + -H "User-Agent: ${USER_AGENT}" \ + -H "Connection: close" \ + "$@" else - _CURL_BULK_CMD=(curl -fsS -H "Content-Type: application/json" -H "Content-Encoding: gzip" -H "Authorization: Basic ${LOKI_AUTH_HEADER}" -H "Connection: close" -H "User-Agent: ${USER_AGENT}") - _CURL_CMD=(curl -fsS -H "Content-Type: application/json" -H "Authorization: Basic ${LOKI_AUTH_HEADER}" -H "Connection: close" -H "User-Agent: ${USER_AGENT}") + curl -fsS \ + -H "Content-Type: application/json" \ + -H "Content-Encoding: gzip" \ + -H "User-Agent: ${USER_AGENT}" \ + -H "Authorization: Basic ${LOKI_AUTH_HEADER}" \ + -H "Connection: close" \ + "$@" fi +} + +_curl_cmd() { +if [ "${AUTOTEST-0}" -eq 1 ]; then + curl --no-progress-meter -fv \ + -H "Content-Type: application/json" \ + -H "User-Agent: ${USER_AGENT}" \ + -H "Connection: close" \ + "$@" +else + curl -fsS \ + -H "Content-Type: application/json" \ + -H "User-Agent: ${USER_AGENT}" \ + -H "Authorization: Basic ${LOKI_AUTH_HEADER}" \ + -H "Connection: close" \ + "$@" +fi +} _setup() { mkfifo "${PIPE_NAME}" - echo "${USER_AGENT} (${BUILD_ID}) started with BOOT=${BOOT}" >&2 + echo "openwrt-loki-exporter/${BUILD_ID} started with BOOT=${BOOT}" >&2 } _teardown() { @@ -76,7 +104,7 @@ _do_bulk_post() { echo "${post_body}" | gzip >"${_log_file}.payload.gz" rm -f "${_log_file}" - if ! "${_CURL_BULK_CMD[@]}" --data-binary "@${_log_file}.payload.gz" "${LOKI_PUSH_URL}" >"${_log_file}.payload.gz-response" 2>&1; then + if ! _curl_bulk_cmd --data-binary "@${_log_file}.payload.gz" "${LOKI_PUSH_URL}" >"${_log_file}.payload.gz-response" 2>&1; then echo "BULK POST FAILED: leaving ${_log_file}.payload.gz for now" fi } @@ -214,7 +242,7 @@ _main_loop() { post_body="${post_body/TIMESTAMP/$ts_ns}" post_body="${post_body/MESSAGE/$msg}" - if ! "${_CURL_CMD[@]}" -d "${post_body}" "${LOKI_PUSH_URL}"; then + if ! _curl_cmd -d "${post_body}" "${LOKI_PUSH_URL}"; then echo "POST FAILED: '${post_body}'" fi