1
0
Fork 0

printer/epson/et-2720/metrics: Add serial number

Distinguish between two printers of the same model without having to
rely on URL, which can change over time.
master
Mike Gerwitz 2021-06-24 21:52:32 -04:00
parent 0ec4219d91
commit 16108f0c2e
Signed by: mikegerwitz
GPG Key ID: 8C917B7F5DC51BA2
1 changed files with 25 additions and 11 deletions

View File

@ -38,14 +38,27 @@ et-usage-status()
}
# Add host and URL to metric.
#
# WARNING: This does _not_ sanitize the URL; do not include double quotes.
add-host()
# Printer serial number.
et-serial()
{
local -r url="${1?Missing URL}"
sed 's|}|, url="'"$url"'", model="ET-2720"}|; s/{, /{/'
et-product-status "$url" \
| grep -oP 'Serial Number.*?</div>' \
| awk -F'[<>]' '{print $9}'
}
# Add URL and serial number to metric.
#
# WARNING: This does _not_ sanitize the URL; do not include double quotes.
add-labels()
{
local -r url="${1?Missing URL}"
local -r serial="${2?Missing serial number}"
sed 's|}|, url="'"$url"'", serial="'"$serial"'", model="ET-2720"}|;
s/{, /{/'
}
@ -56,11 +69,10 @@ main()
local -ri start=$( date +%s%3N )
# printer_ink_level
et-product-status "$url" | awk -f ink.awk | add-host "$url"
local -r serial=$( et-serial "$url" )
# printer_pages_count
et-usage-status "$url" | awk -f usage.awk | add-host "$url"
et-product-status "$url" | awk -f ink.awk | add-labels "$url" "$serial"
et-usage-status "$url" | awk -f usage.awk | add-labels "$url" "$serial"
local -ri end=$( date +%s%3N )
local -ri duration_s=$(( ( end - start ) / 1000 ))
@ -68,11 +80,13 @@ main()
echo '# HELP printer_scrape_time_seconds Timestamp of last scrape.'
echo '# TYPE printer_scrape_time_seconds counter'
echo "printer_scrape_time_seconds $(date +%s)"
echo "printer_scrape_time_seconds{} $(date +%s)" | add-labels "$url" "$serial"
echo '# HELP printer_scrape_duration_seconds Number of seconds spent scraping data from web interface.'
echo '# TYPE printer_scrape_duration_seconds gauge'
printf 'printer_scrape_duration_seconds %0.3f\n' "$duration_s.$duration_ms"
printf 'printer_scrape_duration_seconds{} %0.3f\n' \
"$duration_s.$duration_ms" \
| add-labels "$url" "$serial"
}
main "$@"