Writing SNMP Based Checks

Hello Everyone!

I am trying to write a SNMP check for Juniper LSP’s, to display status of the LSP.
I have checked the documentation but there are missing articles and I was not able to do that.
At the moment this is my check :

Which with command - cmk -nv [host]
is outputing
LSP OK -AX102_AX101_LSP

Which is correct but the problem is that I have more than this LSP in the current device and I want to create LSP service for all the LSP - LSP 1, LSP 2, LSP3 etc.
I think it should be done with ‘service_description’: ‘LSP %s’ but i don’t know how to pass to %s the parameter and when i put %s in the description the check doesn’t work at all.

Also I want to add more OID’s (total 8 but don’t know how to pass them to the check function and make decisions based on them as an example :
snmp_info[“Juniper_LSP”]=( “.1.3.6.1.4.1.2636.3.2.5.1”, [ “1”, “2”, “5”, “6”, “7”, “9”, “12”, “14” ] )
OID 1- Name
OID 2 - State
OID 5 - Age
OID 6- Total operational time
OID 7 - Primary Path Operational time
OID 9 - time since last transition
OID 12 - Number of paths for this lsp
OID 14 - Number of operational paths for lsp

P.S- I saw in many tutorials yield but couldn’t understand what this is and how it’s working.

Please for your support!

Best Regards!

Please take a look at this check, maybe it helps you at least understand how yield can be used:

#!/usr/bin/env python
factory_settings['axing_default_levels'] = (60,70)

# inventory (check_mk will find these checks in the service discovery)
def inventory_axing_temp(info):
    for line in info:
        if line[0] == "TEMP":
            yield line[0], "axing_default_levels"


def check_axing_temp(name, params, info):
    warn, crit = params
    status = 0
    for line in info:
        if name == line[0]:
            item = line[0]
            value = int(line[1])
            infotext = "Temperature is %s°C" % (value)
            perfdata = [("temp",value,warn,crit)]
            if value >= warn:
                status = 1
            if value >= crit:
                status = 2
            yield status, infotext, perfdata


# some metadata for the check plugin, how the services are named, etc.
check_info["axing.temp"] = {
    "check_function"         : check_axing_temp,
    "inventory_function"     : inventory_axing_temp,
    "service_description"    : "Axing %s",
    "default_levels_variable" : "axing_default_levels",
    "has_perfdata"          : True,
}

Hope this helps.
Regards

PS : I found the following text for yield:

Return sends a specified value back to its caller whereas Yield can produce a sequence of values. We should use yield when we want to iterate over a sequence, but don’t want to store the entire sequence in memory.

Thanks a lot for the guidenance ! Just wanted to ask you in this case what is the service description it will be given to each axing and from where it’s taken?

In this particular case the discovered service is called Axing TEMP because my discovery function only does something when there is the string TEMP found in the agent output of this plugin. Thus %s resolves to the first returned parameter of the inventory function.

Thanks, I didn’t manage to make it work… Do you have other examples?

Try:

def  inventory_Juniper_LSP(info):
  if len(info) == 0:
    yield None, None
else:
  for line in info:
    if line.beginswith(2):
      name = "State"

  ...

 yield name

and then you can use %s for your service description. Suggested your output looks something like this:

<<<your-check>>>
1 foo
2 bar
3 baz

Hello,

I,ve tried to use your function and copied most of it for now my function is called 2try and it partially working… - Below is the function but the problem with it is that now it gives name LSP 0, LSP1 ,LSP2, etc but the text it’s always the first entry of the array (test or info).
When I print the array ( test) the output is the full list of LSP with different names.
When I print in the loop it always prints the first name. Don’t know where my mystake is …

#!/usr/bin/env python
factory_settings['axing_default_levels'] = (60,70)


def inventory_2try(info):
    counter=0
    **test=[]**
    for line in info:
        **test.append(line)**
        yield str(counter), "axing_default_levels"
        counter+=1


def check_2try(name, params, test):
    warn, crit = params
    status = 0
    **print test**
    for name,state in test:
        print "Name: " +name
        print "State: "+state
        if state=="2":
           status = 0
           infotext="LSP IS UP: " + name
        #    return 0 , name
        else:
           status = 2
           infotext="LSP IS DOWN: " +name
        #   return 3, name
        return 0, infotext



check_info["2try"] = {
    "check_function"         : check_2try,
    "inventory_function"     : inventory_2try,
    "service_description"    : "LSP %s",
    "default_levels_variable" : "axing_default_levels",
    "has_perfdata"          : True,
}
snmp_info["2try"]=( ".1.3.6.1.4.1.2636.3.2.5.1", [ "1","2" ]) ```

Good evening, can you please put your code in a codeblock?

You can do this with <code> your-code </code> or with ``` your-code ```.
In python indentation means syntax, so codeblocks are required to properly read it.

Good evening, thank you once again for the attention! I have edited my previous reply and the code is now in block.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.