#!/usr/bin/env python
# -*- coding: utf-8 -*

import sys, os
sys.path.append("/usr/scripts/gestion")

import hptools

dico = {
  "READY": 1,
  "PrÁt": 1,
  "Pr menus, appuy \x1e": 0,
  "Powersave activÅ": 3,
  "Verification": 2,
  "imprimante": 0,
  "PrÅchauffage": 1,
  "Traitement de la": 0,
  "tÀche du bac 4": 0,
  "tÀche du bac 3": 0,
  "COMMANDER CARTOUCHE": 0,
  "COMMANDER KIT NETTOY": 0,
  "COMMANDER FOURNIT.": 0,
  "CYAN": 0,
  "MAGENTA": 0,
  "JAUNE": 0,
  "NOIR": 0,
  'COMM TAMB. NOIR': 0,
  'PAGES REST.': 0,
  "": 0,
}

def etat():
    """ Renvoie une liste des differents ecrans actuels du display de l'imprimante """
    liste_oid = ["mib-2.43.16.5.1.2.1.1","mib-2.43.16.5.1.2.1.2","mib-2.43.16.5.1.2.1.3","mib-2.43.16.5.1.2.1.4","mib-2.43.16.5.1.2.1.5"]
    total = 0
    try:
        comm = hptools.snmp(host="laserjet.adm.crans.org", version="1", community="public")
        for oid in liste_oid:
            msg = comm.get_string(oid)
            diff = dico.get(msg, None)
            if diff == None:
                for k in dico.keys():
                    if k in msg and k != "": diff = dico[k]
            if diff == None:
                diff = 4
            total += diff
    except Exception, err:
        total += 24
    return total

if len(sys.argv) > 1:
    arg = sys.argv[1]
else:
    arg = ''

if arg == "config":
    # Ecrit sur la sortie standard la configuration
    print u'host_name laserjet'
    print u"graph_category État"
    print u"graph_args --lower-limit 0 --upper-limit 10"
    print u"graph_title Etat de l'imprimante"
    print u"graph_vlabel nb"
    print u"etat.label Estimation des embrouilles (>3=problemes)"
    print u"etat.warning 4"
    print u"etat.critical 8"
    print u"lpq.label Taille de la file d'attente"
else:
    # Ecrit les valeurs actuelles sur la sortie standard
    print "etat.value %s" % etat()
    print "lpq.value %d" % (int(os.popen("lpq|wc -l").read())-2)
