Exemplo n.º 1
0
class NagiosSNMPPlugin(NagiosPlugin):
    """Nagios SNMP check plugin wrapper

    Class to implement plugins which use SNMP to check check_plugin_status

    Instance of seine.snmp.client.SNMPClient is available to check_plugin_status with
    provided credentials.
    """
    def __init__(self, description=None):
        NagiosPlugin.__init__(self, description=description)
        self.client = None

        self.add_argument('-H', '--host', required=True, help='SNMP host to contact')
        self.add_argument('-1', '--v1', action='store_true', help='Use SNMP V1')
        self.add_argument('-2', '--v2c', action='store_true', help='Use SNMP V2')
        self.add_argument('-3', '--v3', action='store_true', help='Use SNMP V3')
        self.add_argument('-C', '--community', help='SNMP community for SNMP v1 / v2')
        self.add_argument('--username', help='SNMP username for SNMP v3')
        self.add_argument('--auth-pass', help='SNMP authentication password for SNMP v3')
        self.add_argument('--priv-pass', help='SNMP privacy password for SNMP v3')
        self.add_argument('--auth-protocol', help='SNMP v3 authentication protocol')
        self.add_argument('--priv-protocol', help='SNMP v3 privacy protocol')

    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

    def check_plugin_status_example(self):
        """Example SNMP GET plugin test

        Example SNMP GET test: receives sysDescr (1.3.6.1.2.1.1.1.0) from
        host, splits result and returns second field

        """

        oid = '.1.3.6.1.2.1.1.1.0'
        try:
            res = self.client.get(oid)
        except SNMPError, emsg:
            raise NagiosPluginError('ERROR reading OID {0}: {1}'.format(oid, emsg))

        try:
            value = '{0}'.format(res[1])
            self.message += value.split()[2]
            self.state = 'OK'
        except IndexError:
            raise NagiosPluginError('Error splitting SNMP GET result {0}'.format(res[1]))
Exemplo n.º 2
0
class NagiosSNMPPlugin(NagiosPlugin):
    """Nagios SNMP check plugin wrapper

    Class to implement plugins which use SNMP to check check_plugin_status

    Instance of seine.snmp.client.SNMPClient is available to check_plugin_status with
    provided credentials.
    """
    def __init__(self, description=None):
        NagiosPlugin.__init__(self, description=description)
        self.client = None

        self.add_argument('-H',
                          '--host',
                          required=True,
                          help='SNMP host to contact')
        self.add_argument('-1',
                          '--v1',
                          action='store_true',
                          help='Use SNMP V1')
        self.add_argument('-2',
                          '--v2c',
                          action='store_true',
                          help='Use SNMP V2')
        self.add_argument('-3',
                          '--v3',
                          action='store_true',
                          help='Use SNMP V3')
        self.add_argument('-C',
                          '--community',
                          help='SNMP community for SNMP v1 / v2')
        self.add_argument('--username', help='SNMP username for SNMP v3')
        self.add_argument('--auth-pass',
                          help='SNMP authentication password for SNMP v3')
        self.add_argument('--priv-pass',
                          help='SNMP privacy password for SNMP v3')
        self.add_argument('--auth-protocol',
                          help='SNMP v3 authentication protocol')
        self.add_argument('--priv-protocol', help='SNMP v3 privacy protocol')

    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

    def check_plugin_status_example(self):
        """Example SNMP GET plugin test

        Example SNMP GET test: receives sysDescr (1.3.6.1.2.1.1.1.0) from
        host, splits result and returns second field

        """

        oid = '.1.3.6.1.2.1.1.1.0'
        try:
            res = self.client.get(oid)
        except SNMPError, emsg:
            raise NagiosPluginError('ERROR reading OID {0}: {1}'.format(
                oid, emsg))

        try:
            value = '{0}'.format(res[1])
            self.message += value.split()[2]
            self.state = 'OK'
        except IndexError:
            raise NagiosPluginError(
                'Error splitting SNMP GET result {0}'.format(res[1]))