1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
|
#
# python-netsnmpagent module
# Copyright (c) 2013-2016 Pieter Hollants <pieter@hollants.com>
# Licensed under the GNU Lesser Public License (LGPL) version 3
#
# net-snmp C API abstraction module
#
import ctypes, ctypes.util
c_sizet_p = ctypes.POINTER(ctypes.c_size_t)
# Make libnetsnmpagent available via Python's ctypes module. We do this globally
# so we can define C function prototypes
# Workaround for net-snmp 5.4.x that has a bug with unresolved dependencies
# in its libraries (http://sf.net/p/net-snmp/bugs/2107): load netsnmphelpers
# first
try:
libnsh = ctypes.cdll.LoadLibrary(ctypes.util.find_library("netsnmphelpers"))
except:
raise Exception("Could not load libnetsnmphelpers! Is net-snmp installed?")
try:
libnsa = ctypes.cdll.LoadLibrary(ctypes.util.find_library("netsnmpagent"))
except:
raise Exception("Could not load libnetsnmpagent! Is net-snmp installed?")
# net-snmp <5.6.x had various functions in libnetsnmphelpers.so that were moved
# to libnetsnmpagent.so in later versions. Use netsnmp_create_watcher_info as
# a test and define a libnsX handle to abstract from the actually used library
# version.
try:
libnsa.netsnmp_create_watcher_info
libnsX = libnsa
except AttributeError:
libnsX = libnsh
# include/net-snmp/library/callback.h
# Callback major types
SNMP_CALLBACK_LIBRARY = 0
SNMP_CALLBACK_APPLICATION = 1
# SNMP_CALLBACK_LIBRARY minor types
SNMP_CALLBACK_LOGGING = 4
SNMPCallback = ctypes.CFUNCTYPE(
ctypes.c_int, # result type
ctypes.c_int, # int majorID
ctypes.c_int, # int minorID
ctypes.c_void_p, # void *serverarg
ctypes.c_void_p # void *clientarg
)
for f in [ libnsa.snmp_register_callback ]:
f.argtypes = [
ctypes.c_int, # int major
ctypes.c_int, # int minor
SNMPCallback, # SNMPCallback *new_callback
ctypes.c_void_p # void *arg
]
f.restype = int
# include/net-snmp/agent/agent_callbacks.h
SNMPD_CALLBACK_INDEX_STOP = 11
# include/net-snmp/library/snmp_logging.h
LOG_EMERG = 0 # system is unusable
LOG_ALERT = 1 # action must be taken immediately
LOG_CRIT = 2 # critical conditions
LOG_ERR = 3 # error conditions
LOG_WARNING = 4 # warning conditions
LOG_NOTICE = 5 # normal but significant condition
LOG_INFO = 6 # informational
LOG_DEBUG = 7 # debug-level messages
class snmp_log_message(ctypes.Structure): pass
snmp_log_message_p = ctypes.POINTER(snmp_log_message)
snmp_log_message._fields_ = [
("priority", ctypes.c_int),
("msg", ctypes.c_char_p)
]
# include/net-snmp/library/snmp_api.h
SNMPERR_SUCCESS = 0
# include/net-snmp/library/default_store.h
NETSNMP_DS_LIBRARY_ID = 0
NETSNMP_DS_APPLICATION_ID = 1
NETSNMP_DS_LIB_PERSISTENT_DIR = 8
for f in [ libnsa.netsnmp_ds_set_boolean ]:
f.argtypes = [
ctypes.c_int, # int storeid
ctypes.c_int, # int which
ctypes.c_int # int value
]
f.restype = ctypes.c_int
for f in [ libnsa.netsnmp_ds_set_string ]:
f.argtypes = [
ctypes.c_int, # int storeid
ctypes.c_int, # int which
ctypes.c_char_p # const char *value
]
f.restype = ctypes.c_int
# include/net-snmp/agent/ds_agent.h
NETSNMP_DS_AGENT_ROLE = 1
NETSNMP_DS_AGENT_X_SOCKET = 1
# include/net-snmp/library/snmp.h
SNMP_ERR_NOERROR = 0
for f in [ libnsa.init_snmp ]:
f.argtypes = [
ctypes.c_char_p # const char *type
]
f.restype = None
for f in [ libnsa.snmp_shutdown ]:
f.argtypes = [
ctypes.c_char_p # const char *type
]
f.restype = None
# include/net-snmp/library/oid.h
c_oid = ctypes.c_ulong
c_oid_p = ctypes.POINTER(c_oid)
# include/net-snmp/types.h
MAX_OID_LEN = 128
# include/net-snmp/agent/snmp_vars.h
for f in [ libnsa.init_agent ]:
f.argtypes = [
ctypes.c_char_p # const char *app
]
f.restype = ctypes.c_int
for f in [ libnsa.shutdown_agent ]:
f.argtypes = None
f.restype = ctypes.c_int
# include/net-snmp/library/parse.h
class tree(ctypes.Structure): pass
# include/net-snmp/mib_api.h
for f in [ libnsa.netsnmp_init_mib ]:
f.argtypes = None
f.restype = None
for f in [ libnsa.read_mib ]:
f.argtypes = [
ctypes.c_char_p # const char *filename
]
f.restype = ctypes.POINTER(tree)
for f in [ libnsa.read_objid ]:
f.argtypes = [
ctypes.c_char_p, # const char *input
c_oid_p, # oid *output
c_sizet_p # size_t *out_len
]
f.restype = ctypes.c_int
# include/net-snmp/agent/agent_handler.h
HANDLER_CAN_GETANDGETNEXT = 0x01
HANDLER_CAN_SET = 0x02
HANDLER_CAN_RONLY = HANDLER_CAN_GETANDGETNEXT
HANDLER_CAN_RWRITE = (HANDLER_CAN_GETANDGETNEXT | \
HANDLER_CAN_SET)
class netsnmp_mib_handler(ctypes.Structure): pass
netsnmp_mib_handler_p = ctypes.POINTER(netsnmp_mib_handler)
class netsnmp_handler_registration(ctypes.Structure): pass
netsnmp_handler_registration_p = ctypes.POINTER(netsnmp_handler_registration)
netsnmp_handler_registration._fields_ = [
("handlerName", ctypes.c_char_p),
("contextName", ctypes.c_char_p),
("rootoid", c_oid_p),
("rootoid_len", ctypes.c_size_t),
("handler", netsnmp_mib_handler_p),
("modes", ctypes.c_int),
("priority", ctypes.c_int),
("range_subid", ctypes.c_int),
("range_ubound", c_oid),
("timeout", ctypes.c_int),
("global_cacheid", ctypes.c_int),
("my_reg_void", ctypes.c_void_p)
]
for f in [ libnsa.netsnmp_create_handler_registration ]:
f.argtypes = [
ctypes.c_char_p, # const char *name
ctypes.c_void_p, # Netsnmp_Node_Handler *handler_access_method
c_oid_p, # const oid *reg_oid
ctypes.c_size_t, # size_t reg_oid_len
ctypes.c_int # int modes
]
f.restype = netsnmp_handler_registration_p
# include/net-snmp/library/asn1.h
ASN_INTEGER = 0x02
ASN_OCTET_STR = 0x04
ASN_APPLICATION = 0x40
# counter64 requires some extra work because it can't be reliably represented
# by a single C data type
class counter64(ctypes.Structure):
@property
def value(self):
return self.high << 32 | self.low
@value.setter
def value(self, val):
self.high = val >> 32
self.low = val & 0xFFFFFFFF
def __init__(self, initval=0):
ctypes.Structure.__init__(self, 0, 0)
self.value = initval
counter64_p = ctypes.POINTER(counter64)
counter64._fields_ = [
("high", ctypes.c_ulong),
("low", ctypes.c_ulong)
]
# include/net-snmp/library/snmp_impl.h
ASN_IPADDRESS = ASN_APPLICATION | 0
ASN_COUNTER = ASN_APPLICATION | 1
ASN_UNSIGNED = ASN_APPLICATION | 2
ASN_TIMETICKS = ASN_APPLICATION | 3
ASN_COUNTER64 = ASN_APPLICATION | 6
# include/net-snmp/agent/watcher.h
WATCHER_FIXED_SIZE = 0x01
WATCHER_MAX_SIZE = 0x02
class netsnmp_watcher_info(ctypes.Structure): pass
netsnmp_watcher_info_p = ctypes.POINTER(netsnmp_watcher_info)
netsnmp_watcher_info._fields_ = [
("data", ctypes.c_void_p),
("data_size", ctypes.c_size_t),
("max_size", ctypes.c_size_t),
("type", ctypes.c_ubyte),
("flags", ctypes.c_int)
# net-snmp 5.7.x knows data_size_p here as well but we ignore it for
# backwards compatibility with net-snmp 5.4.x.
]
for f in [ libnsX.netsnmp_create_watcher_info ]:
f.argtypes = [
ctypes.c_void_p, # void *data
ctypes.c_size_t, # size_t size
ctypes.c_ubyte, # u_char type
ctypes.c_int # int flags
]
f.restype = netsnmp_watcher_info_p
for f in [ libnsX.netsnmp_register_watched_instance ]:
f.argtypes = [
netsnmp_handler_registration_p, # netsnmp_handler_registration *reginfo
netsnmp_watcher_info_p # netsnmp_watcher_info *winfo
]
f.restype = ctypes.c_int
for f in [ libnsX.netsnmp_register_watched_scalar ]:
f.argtypes = [
netsnmp_handler_registration_p, # netsnmp_handler_registration *reginfo
netsnmp_watcher_info_p # netsnmp_watcher_info *winfo
]
f.restype = ctypes.c_int
# include/net-snmp/types.h
class netsnmp_variable_list(ctypes.Structure): pass
netsnmp_variable_list_p = ctypes.POINTER(netsnmp_variable_list)
netsnmp_variable_list_p_p = ctypes.POINTER(netsnmp_variable_list_p)
# include/net-snmp/varbind_api.h
for f in [ libnsa.snmp_varlist_add_variable ]:
f.argtypes = [
netsnmp_variable_list_p_p, # netsnmp_variable_list **varlist
c_oid_p, # const oid *name
ctypes.c_size_t, # size_t name_length
ctypes.c_ubyte, # u_char type
ctypes.c_void_p, # const void *value
ctypes.c_size_t # size_t len
]
f.restype = netsnmp_variable_list_p
# include/net-snmp/agent/table_data.h
class netsnmp_table_row(ctypes.Structure): pass
netsnmp_table_row_p = ctypes.POINTER(netsnmp_table_row)
netsnmp_table_row._fields_ = [
("indexes", netsnmp_variable_list_p),
("index_oid", c_oid_p),
("index_oid_len", ctypes.c_size_t),
("data", ctypes.c_void_p),
("next", netsnmp_table_row_p),
("prev", netsnmp_table_row_p)
]
class netsnmp_table_data(ctypes.Structure): pass
netsnmp_table_data_p = ctypes.POINTER(netsnmp_table_data)
netsnmp_table_data._fields_ = [
("indexes_template", netsnmp_variable_list_p),
("name", ctypes.c_char_p),
("flags", ctypes.c_int),
("store_indexes", ctypes.c_int),
("first_row", netsnmp_table_row_p),
("last_row", netsnmp_table_row_p)
]
# include/net-snmp/agent/table_dataset.h
class netsnmp_table_data_set_storage_udata(ctypes.Union): pass
netsnmp_table_data_set_storage_udata._fields_ = [
("voidp", ctypes.c_void_p),
("integer", ctypes.POINTER(ctypes.c_long)),
("string", ctypes.c_char_p),
("objid", c_oid_p),
("bitstring", ctypes.POINTER(ctypes.c_ubyte)),
("counter64", ctypes.POINTER(counter64)),
("floatVal", ctypes.POINTER(ctypes.c_float)),
("doubleVal", ctypes.POINTER(ctypes.c_double))
]
class netsnmp_table_data_set_storage(ctypes.Structure): pass
netsnmp_table_data_set_storage_p = ctypes.POINTER(netsnmp_table_data_set_storage)
netsnmp_table_data_set_storage._fields_ = [
("column", ctypes.c_uint),
("writable", ctypes.c_byte),
("change_ok_fn", ctypes.c_void_p),
("my_change_data", ctypes.c_void_p),
("type", ctypes.c_ubyte),
("data", netsnmp_table_data_set_storage_udata),
("data_len", ctypes.c_ulong),
("next", netsnmp_table_data_set_storage_p)
]
class netsnmp_table_data_set(ctypes.Structure): pass
netsnmp_table_data_set_p = ctypes.POINTER(netsnmp_table_data_set)
netsnmp_table_data_set._fields_ = [
("table", netsnmp_table_data_p),
("default_row", netsnmp_table_data_set_storage_p),
("allow_creation", ctypes.c_int),
("rowstatus_column", ctypes.c_uint)
]
for f in [ libnsX.netsnmp_create_table_data_set ]:
f.argtypes = [
ctypes.c_char_p # const char *table_name
]
f.restype = netsnmp_table_data_set_p
for f in [ libnsX.netsnmp_table_dataset_add_row ]:
f.argtypes = [
netsnmp_table_data_set_p, # netsnmp_table_data_set *table
netsnmp_table_row_p, # netsnmp_table_row *row
]
f.restype = None
for f in [ libnsX.netsnmp_table_data_set_create_row_from_defaults ]:
f.argtypes = [
netsnmp_table_data_set_storage_p # netsnmp_table_data_set_storage *defrow
]
f.restype = netsnmp_table_row_p
for f in [ libnsX.netsnmp_table_set_add_default_row ]:
f.argtypes = [
netsnmp_table_data_set_p, # netsnmp_table_data_set *table_set
ctypes.c_uint, # unsigned int column
ctypes.c_int, # int type
ctypes.c_int, # int writable
ctypes.c_void_p, # void *default_value
ctypes.c_size_t # size_t default_value_len
]
f.restype = ctypes.c_int
for f in [ libnsX.netsnmp_register_table_data_set ]:
f.argtypes = [
netsnmp_handler_registration_p, # netsnmp_handler_registration *reginfo
netsnmp_table_data_set_p, # netsnmp_table_data_set *data_set
ctypes.c_void_p # netsnmp_table_registration_info *table_info
]
f.restype = ctypes.c_int
for f in [ libnsX.netsnmp_set_row_column ]:
f.argtypes = [
netsnmp_table_row_p, # netsnmp_table_row *row
ctypes.c_uint, # unsigned int column
ctypes.c_int, # int type
ctypes.c_void_p, # const void *value
ctypes.c_size_t # size_t value_len
]
f.restype = ctypes.c_int
for f in [ libnsX.netsnmp_table_dataset_add_index ]:
f.argtypes = [
netsnmp_table_data_set_p, # netsnmp_table_data_set *table
ctypes.c_ubyte # u_char type
]
f.restype = None
for f in [ libnsX.netsnmp_table_dataset_remove_and_delete_row ]:
f.argtypes = [
netsnmp_table_data_set_p, # netsnmp_table_data_set *table
netsnmp_table_row_p # netsnmp_table_row *row
]
# include/net-snmp/agent/snmp_agent.h
for f in [ libnsa.agent_check_and_process ]:
f.argtypes = [
ctypes.c_int # int block
]
f.restype = ctypes.c_int
|