Esempio n. 1
0
    def parse_args(self, *args, **kwargs):
        """Parse SNMP plugin arguments

        Initialize plugin arguments, requiring valid SNMP session settings

        As a side effect, self.client() is set to SNMP session.

        """
        args = NagiosPlugin.parse_args(self, *args, **kwargs)

        if not args.v1 and not args.v2c and not args.v3:
            self.error('SNMP version not specified')

        if args.v1 or args.v2c:
            if args.username or args.auth_pass or args.priv_pass:
                self.error('Incompatible SNMP arguments')
        elif args.v3 and args.community:
            self.error('Incompatible SNMP arguments')

        if args.v1:
            if args.community is None:
                self.error('SNMP v1 requires community')
            auth = SNMPv1Auth(args.community)

        elif args.v2c:
            if args.community is None:
                self.error('SNMP v2c requires community')
            auth = SNMPv2cAuth(args.community)

        elif args.v3:
            if not args.username and not args.auth_pass:
                self.error(
                    'SNMP v3 requires username and authentication password')

            auth = SNMPv3Auth(args.username, args.auth_pass, args.priv_pass,
                              args.auth_protocol, args.priv_protocol)

        self.client = SNMPClient(address=args.host, auth=auth)

        return args
Esempio n. 2
0
            int(float(details[i]['level']) / float(details[i]['max']) * 100),
        }) for i in details.keys())

    def reset(self, resettype=DEFAULT_RESET_TYPE):
        try:
            resettype = rfc1902.Integer(RESET_TYPE_MAP[resettype])
        except KeyError:
            raise ValueError('Invalid reset type: %s' % resettype)
        oid = PRINTER_MIB_OID + '.5.1.1.3.1'
        return self.client.set(oid, resettype)

    def testpage(self, pagetype=DEFAULT_TESTPAGE):
        try:
            pagetype = rfc1902.Integer(TESTPAGE_MAP[pagetype])
        except KeyError:
            raise ValueError('Invalid testpage type: %s' % pagetype)
        oid = HP_LASERJET_MIB_OID + '.4.2.1.1.5.2.0'
        return self.client.set(oid, pagetype)


if __name__ == '__main__':
    import sys

    ljclient = LaserjetSNMPControl(
        SNMPClient(sys.argv[1], SNMPv1Auth(community=sys.argv[2])))
    #ljclient.client.logger.set_level('DEBUG')
    #for k in sorted(HP_PRINTER_OID_MAP.keys()): print k, getattr(ljclient,k)

    for color, details in ljclient.supply_level_details().items():
        print '%12s %s %%' % (color, details['percent'])