Transcription

python-netsnmpagentWriting net-snmp AgentX subagentsin PythonPieter [email protected] / @pfhllntsLinux System Engineer /„Hardware Competence Center“ DudeDFS Deutsche Flugsicherung GmbH

This work sponsored by.DFS Deutsche FlugsicherungGmbH German Air Traffic Control 100% government owned (yet) 5000 employees, 20 locations We produce safety

Why the fuzz? REST, node.js et. al – SNMP not particularly sexy? But: employer uses monitoring that speaks SNMP Need to integrate centralized hardware monitoring(detect fan failures etc.) among a hardware zooServer vendors with own MIBs, clients need extrawork (coretemp, lm sensors, smartctl) Define and implement our own Hardware-MIB(for better or worse)

SNMP / net-snmp SNMP: Simple Network Monitoring Protocol– Versions 1 (RFC 1157), 2c (1901), 3 (2571)net-snmp: dominant toolkit to implement SNMP–applications (snmpwalk etc.) and libraries–snmpd: master agent–extensible: dlopen() modules, smux, AgentX–C API, mib2c template generator for ownagents–Agent architecture beyond scope of talk

Python and SNMP Why Python? exboss told me so. net-snmp comes with a Python module „netsnmp“ –73KB C code that abstracts C api–Synchronous client code onlyIdea: access C API from Python directly with ctypesmodule, imitating AgentX subagents written in CExisting python-agentx module on Sourceforge–Design issues–Orphaned?

Hello python-netsnmpagent Two source files–netsnmpapi.py (ctypes stuff), 13KB–netsnmpagent.py (classes), 33KB (21KB) Extensively commented Example MIBs/agents included Whaddya mean, „coding style“? Tested with net-snmp 5.4.2 (SLES11 SP2), 5.4.3(Ubuntu 12.04 LTS), 5.7.1 (openSUSE 12.x)

SIMPLE-MIB.txtsimpleInteger OBJECT TYPESYNTAXInteger32MAX ACCESSread writeSTATUScurrentA simple scalar 32-bit signed value.Tables are more complex.DESCRIPTION"A read write, unsigned, 32 bits integer value.":: { simpleScalars 1 }translates to something like.1.3.6.1.2.1.74.1.30187.1.1.1Yes, tables also possible.

netsnmpagent module initimport netsnmpagentnetsmpagent will importnetsnmpapi itselftry:agent netsnmpagent.netsnmpAgent(net-snmp needs iteg. to translate OIDsAgentName "SimpleAgent",MIBFiles "[.]/SIMPLE MIB.txt" ])except netsnmpagent.netsnmpAgentException as e:# handle exception

SNMP object registrationsimpleInteger agent.Integer32(Sort of a class factory.Returns a Python object handlinga SNMP object of type „Integer32“.oidstr "SIMPLE MIB::simpleInteger")Having objects declared in the MIB aloneis not enough – two subagents might use oneMIB. So explicitly register what OID this objecthandles.try:agent.start()Registrations done, connect to snmpd.except netsnmpagent.netsnmpAgentException as e:# handle exception

Agent lifecyclewhile (loop):Net-snmp API call: block until we have workagent.check and ) * 2)print „val: {0}“.simpleInteger.value() More complete examples (eg. with tables) in thesource distributionNaturally, a real life agent would be more complex.(DFS HW-Agent: 120KB, 3000 lines)

To do Notifications/traps API documentation (doh.) Unit tests–After all „we produce safety“, right?

Thank you! PyPI ry packages for ge python-netsnmpagent&project home%3ApfhllntsNet-SNMP: http://www.net-snmp.org

SNMP / net-snmp SNMP: Simple Network Monitoring Protocol – Versions 1 (RFC 1157), 2c (1901), 3 (2571) net-snmp: dominant toolkit to implement SNMP – applications (snmpwalk etc.) and libraries – snmpd: master agent – extensible: dlopen() modules, smux