Skip to content

Commit 806915d

Browse files
m3v113evanugarte
andauthored
added parsing html for printer info (#143)
* added parsing html for printer info * metric line replaced/removed * changed exception Co-authored-by: Evan Ugarte <36345325+evanugarte@users.noreply.github.com> * cleaning up * imagine this works --------- Co-authored-by: Evan Ugarte <36345325+evanugarte@users.noreply.github.com> Co-authored-by: evan <evanuxd@gmail.com>
1 parent 05d7048 commit 806915d

2 files changed

Lines changed: 32 additions & 1 deletion

File tree

printer/modules/collector.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
import logging
44
import json
55

6+
from bs4 import BeautifulSoup
67
from pysnmp.hlapi import *
8+
import requests
79

810
from modules.metrics import MetricsHandler
911

@@ -19,7 +21,6 @@
1921

2022

2123
class SnmpOid(enum.Enum):
22-
INK_LEVEL = ("ink_level", "1.3.6.1.2.1.43.11.1.1.9.1.1")
2324
INK_CAPACITY = ("ink_capacity", "1.3.6.1.2.1.43.11.1.1.8.1.1")
2425
PAGE_COUNT = ("page_count", "1.3.6.1.2.1.43.10.2.1.4.1.1")
2526
TRAY_EMPTY = ("tray_empty", "1.3.6.1.2.1.43.18.1.1.8.1.13", True)
@@ -62,11 +63,40 @@ def fetch_ips_from_config(config_file_path):
6263
except Exception:
6364
logging.exception(f"error opening config file")
6465

66+
def scrape_html(ip: str):
67+
url = f"http://{ip}/"
68+
69+
try:
70+
response = requests.get(url, timeout=5)
71+
response.raise_for_status()
72+
except requests.RequestException:
73+
logging.exception("failed to fetch printer html page")
74+
return
75+
76+
soup = BeautifulSoup(response.content, 'html.parser')
77+
78+
ink_td = soup.find('td', string=lambda s: s and '%' in s)
79+
if ink_td:
80+
try:
81+
# Clean: strip whitespace, remove %, convert to float
82+
level = float(ink_td.text.strip().rstrip('%'))
83+
metrics_handler.snmp_metric.labels(name="ink_level", ip=ip).set(level)
84+
except ValueError:
85+
logging.warning(f"Could not parse ink level from: {ink_td.text}")
86+
87+
page_cells = soup.select('td.tableDataCellStand.width30')
88+
for cell in page_cells:
89+
try:
90+
count = int(cell.text.strip())
91+
metrics_handler.snmp_metric.labels(name="pages_remaining", ip=ip).set(count)
92+
except ValueError:
93+
continue
6594

6695
def scrape_snmp(ip_list, sleep_duration_minutes=5):
6796
while True:
6897
for ip in ip_list:
6998
get_snmp_data(ip)
99+
scrape_html(ip)
70100
time.sleep(sleep_duration_minutes * 60)
71101

72102

printer/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ httpx==0.28.1
66
requests==2.32.3
77
pysnmp==4.4.12
88
pyasn1==0.4.8
9+
bs4==0.0.2

0 commit comments

Comments
 (0)