|
5 | 5 |
|
6 | 6 | from pysnmp.hlapi import * |
7 | 7 |
|
| 8 | +# imports for parsing html |
| 9 | +import requests |
| 10 | +import bs4 |
| 11 | +from bs4 import BeautifulSoup |
| 12 | + |
8 | 13 | from modules.metrics import MetricsHandler |
9 | 14 |
|
10 | 15 | metrics_handler = MetricsHandler.instance() |
@@ -62,11 +67,40 @@ def fetch_ips_from_config(config_file_path): |
62 | 67 | except Exception: |
63 | 68 | logging.exception(f"error opening config file") |
64 | 69 |
|
| 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 |
65 | 98 |
|
66 | 99 | def scrape_snmp(ip_list, sleep_duration_minutes=5): |
67 | 100 | while True: |
68 | 101 | for ip in ip_list: |
69 | 102 | get_snmp_data(ip) |
| 103 | + scrape_html(ip) |
70 | 104 | time.sleep(sleep_duration_minutes * 60) |
71 | 105 |
|
72 | 106 |
|
|
0 commit comments