#!/bin/bash /usr/scripts/python.sh # -*- coding: utf-8 -*- from __future__ import print_function import BeautifulSoup import re import requests import sys URL = 'https://imprimante.adm.crans.org/hp/device/DeviceStatus/Index' CA = '/etc/ssl/certs/cacert.org.pem' COLOR_INFOS = { 'black': ('noir', '000000'), 'cyan': ('cyan', '00ffff'), 'magenta': ('magenta', 'ff00ff'), 'yellow': ('jaune', 'ffff00'), } COLORS = COLOR_INFOS.keys() CONS_CLASS_BLOCK = { color: re.compile(".*gauge "+color.capitalize() +".*") for color in COLORS } def ffindconsomable(color, raw): """Retrouve la couleur `color` dans raw""" couleur=raw.findAll('div', attrs={"class":CONS_CLASS_BLOCK[color]}) couleur_toner=couleur[0].findAll('span')[0].string couleur_toner=couleur_toner.split('%')[0] couleur_tambour=couleur[1].findAll('span')[0].string couleur_tambour=couleur_tambour.split('%')[0] try: couleur_tambour=re.sub('[^\.0-9]', '', couleur_tambour.decode()) couleur_toner=re.sub('[^\.0-9]', '', couleur_toner.decode()) except: return None, None return couleur_toner, couleur_tambour if __name__ == '__main__': if 'config' in sys.argv: print("host_name hp") print("graph_category consommables") print("graph_args --lower-limit 0 --upper-limit 100 --rigid") print("graph_title Etat des consommables (pourcentage)") print("graph_vlabel Pourcentage restant") for color, (pr_name, hex) in COLOR_INFOS.iteritems(): print("%s_toner.label Toner %s" % (color, pr_name)) print("%s_tambour.label Tambour %s" % (color, pr_name)) print("%s_toner.draw LINE1" % color) print("%s_tambour.draw LINE2" % color) print("%s_toner.colour %s" % (color, hex)) print("%s_tambour.colour %s" % (color, hex)) else: page = requests.get(URL, verify=CA) raw=BeautifulSoup.BeautifulSoup(page.text) for color in COLORS: toner, tambour = ffindconsomable(color, raw) print('{}_toner.value {}'.format(color, toner)) print('{}_tambour.value {}'.format(color, tambour))