Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions printer/modules/collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@

from pysnmp.hlapi import *

# imports for parsing html
import requests
import bs4
from bs4 import BeautifulSoup

from modules.metrics import MetricsHandler

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

def scrape_html(ip):

url = "http://" + ip + "/"

try:
page = requests.get(url, timeout=5)
page.raise_for_status()
except Exception:
logging.exception("failed to fetch printer html page")
return

soup = BeautifulSoup(page.content, 'html.parser')

content = soup.find_all('td')
text = "%"
ink_level = ""
for element in content:
if text in str(element.string):
ink_level = float((element.text.strip()).rstrip('%'))
metrics_handler.snmp_metric.labels(name="ink_level", ip=ip).set(ink_level)
content = soup.find_all('td', class_='tableDataCellStand width30')
pages_remaining = 0
for element in content:
try:
pages_remaining = int(element.text.strip())
metrics_handler.snmp_metric.labels(name="pages_remaining", ip=ip).set(pages_remaining)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what does pages_remaining show? about how many pages the printer could print before running otu of ink?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, ink_level shows percentage of ink left and pages_remaining shows about how many pages could still be printed

except Exception:
pass
Comment thread
m3v113 marked this conversation as resolved.
Outdated

def scrape_snmp(ip_list, sleep_duration_minutes=5):
while True:
for ip in ip_list:
get_snmp_data(ip)
scrape_html(ip)
time.sleep(sleep_duration_minutes * 60)


Expand Down
1 change: 1 addition & 0 deletions printer/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ httpx==0.28.1
requests==2.32.3
pysnmp==4.4.12
pyasn1==0.4.8
bs4==0.0.2