1
0
Fork 0
promscripts/printer/epson/et-2720/usage.awk

87 lines
2.6 KiB
Awk

# Parse Epson ET-2720 Advanced Usage Status page 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 a `printer_pages_count` metric for each available color
# and source.
#
# 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.
# Rather than parsing start and end tags, we'll set the field separator to
# any opening angled bracket and the record separator to any closing. This
# leaves us with the node names, attributes, and cdata.
BEGIN {
FS = "<"
RS = ">"
count = 0
expected_count = 8
print "# HELP printer_pages_count Number of pages."
print "# TYPE printer_pages_count counter"
}
# Simplify matching.
{
gsub(/B&(amp;)?W/, "BW")
gsub(/&nbsp;/, " ")
}
# Lines that mention BW or Color indicate a metric.
/BW|Color/ {
color = "CMYK"
source = ""
if ($1 ~ /BW/) color="K"
# Whitelist metrics.
switch ($1) {
case /Copy :$/:
source = "copy"
break
case /Scan :$/:
source = "scan"
break
case /Print from Computer or Mobile Device :$/:
source = "print"
break
case /Print from Memory Device or Other Functions :$/:
source = "internal"
break
default:
next
}
# Assume that the next line beginning with a number is the value.
while (!($1 ~ /^[0-9]/)) getline
printf "printer_pages_count{color=\"%s\", source=\"%s\"} %d\n", \
color, source, $1
count++
}
END {
print "# HELP printer_pages_count_success Whether scraping was successful."
print "# TYPE printer_pages_count_success gauge"
printf "printer_pages_count_success{} %d\n", (count == expected_count)
}