Skip to content

Commit 1794f03

Browse files
m3v113evanugarte
authored andcommitted
added parsing html for printer info
1 parent 0d1a21a commit 1794f03

2 files changed

Lines changed: 35 additions & 0 deletions

File tree

printer/modules/collector.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55

66
from pysnmp.hlapi import *
77

8+
# imports for parsing html
9+
import requests
10+
import bs4
11+
from bs4 import BeautifulSoup
12+
813
from modules.metrics import MetricsHandler
914

1015
metrics_handler = MetricsHandler.instance()
@@ -62,11 +67,40 @@ def fetch_ips_from_config(config_file_path):
6267
except Exception:
6368
logging.exception(f"error opening config file")
6469

70+
def scrape_html(ip):
71+
72+
url = "http://" + ip + "/"
73+
74+
try:
75+
page = requests.get(url, timeout=5)
76+
page.raise_for_status()
77+
except Exception:
78+
logging.exception("failed to fetch printer html page")
79+
return
80+
81+
soup = BeautifulSoup(page.content, 'html.parser')
82+
83+
content = soup.find_all('td')
84+
text = "%"
85+
ink_level = ""
86+
for element in content:
87+
if text in str(element.string):
88+
ink_level = float((element.text.strip()).rstrip('%'))
89+
metrics_handler.snmp_metric.labels(name="ink_level", ip=ip).set(ink_level)
90+
content = soup.find_all('td', class_='tableDataCellStand width30')
91+
pages_remaining = 0
92+
for element in content:
93+
try:
94+
pages_remaining = int(element.text.strip())
95+
metrics_handler.snmp_metric.labels(name="pages_remaining", ip=ip).set(pages_remaining)
96+
except Exception:
97+
pass
6598

6699
def scrape_snmp(ip_list, sleep_duration_minutes=5):
67100
while True:
68101
for ip in ip_list:
69102
get_snmp_data(ip)
103+
scrape_html(ip)
70104
time.sleep(sleep_duration_minutes * 60)
71105

72106

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)