1
0
Fork 0
promscripts/printer/epson/et-2720/metrics

94 lines
2.8 KiB
Bash
Executable File

#!/bin/bash
# Parse Epson ET-2720 metrics from web interface
#
# Copyright (C) 2021 Mike Gerwitz
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# This will produce `printer_ink_level` and `printer_pages_count` metrics.
#
# Note that this script is hardly robust and relies on many different
# assumptions about the strucutre of the sloppy HTML on the page, beacuse
# I do not permit the printer to access the Internet, and therefore the
# firmware can never upgrade and make changes to the layout of this page.
# Consequently, you may find that this script does not work for your
# printer, even if it is the same model.
cd "$(dirname "$0")"
et-product-status()
{
wget -qO- "$1/PRESENTATION/ADVANCED/INFO_PRTINFO/TOP"
}
et-usage-status()
{
wget -qO- "$1/PRESENTATION/ADVANCED/INFO_MENTINFO/TOP"
}
# Printer serial number.
et-serial()
{
local -r url="${1?Missing URL}"
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/{, /{/'
}
main()
{
local url="${1?Missing Epson ET-2720 URL}"
url="${url%%/}"
local -ri start=$( date +%s%3N )
local -r serial=$( et-serial "$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 ))
local -ri duration_ms=$(( ( end - start ) % 1000 ))
echo '# HELP printer_scrape_time_seconds Timestamp of last scrape.'
echo '# TYPE printer_scrape_time_seconds counter'
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" \
| add-labels "$url" "$serial"
}
main "$@"