Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions example-agent.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import (
absolute_import,
division,
print_function,
)

'''

Expand Down Expand Up @@ -61,7 +66,7 @@ def test(self, oid, data):
raise pyagentx.SetHandlerError()

def commit(self, oid, data):
print "COMMIT CALLED: %s = %s" % (oid, data)
print("COMMIT CALLED: %s = %s" % (oid, data))


class MyAgent(pyagentx.Agent):
Expand All @@ -78,7 +83,7 @@ def main():
a = MyAgent()
a.start()
except Exception as e:
print "Unhandled exception:", e
print ("Unhandled exception:", e)
a.stop()
except KeyboardInterrupt:
a.stop()
Expand Down
8 changes: 7 additions & 1 deletion minimal-agent.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import (
absolute_import,
division,
print_function,
)

import pyagentx

# Updater class that set OID values
Expand All @@ -22,7 +28,7 @@ def setup(self):
a = MyAgent()
a.start()
except Exception as e:
print "Unhandled exception:", e
print("Unhandled exception:", e)
a.stop()
except KeyboardInterrupt:
a.stop()
5 changes: 5 additions & 0 deletions pyagentx/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import (
absolute_import,
division,
print_function,
)

import logging

Expand Down
16 changes: 12 additions & 4 deletions pyagentx/agent.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import (
absolute_import,
division,
print_function,
)

# --------------------------------------------
import logging
Expand All @@ -11,8 +16,11 @@ def emit(self, record):
# --------------------------------------------

import time
import Queue
import inspect
try:
import queue
except ImportError:
import Queue as queue

import pyagentx
from pyagentx.updater import Updater
Expand Down Expand Up @@ -57,18 +65,18 @@ def setup(self):
pass

def start(self):
queue = Queue.Queue(maxsize=20)
update_queue = queue.Queue(maxsize=20)
self.setup()
# Start Updaters
for u in self._updater_list:
logger.debug('Starting updater [%s]' % u['oid'])
t = u['class']()
t.agent_setup(queue, u['oid'], u['freq'])
t.agent_setup(update_queue, u['oid'], u['freq'])
t.start()
self._threads.append(t)
# Start Network
oid_list = [u['oid'] for u in self._updater_list]
t = Network(queue, oid_list, self._sethandlers)
t = Network(update_queue, oid_list, self._sethandlers)
t.start()
self._threads.append(t)
# Do nothing ... just wait for someone to stop you
Expand Down
18 changes: 13 additions & 5 deletions pyagentx/network.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import (
absolute_import,
division,
print_function,
)

# --------------------------------------------
import logging
Expand All @@ -13,18 +18,21 @@ def emit(self, record):
import socket
import time
import threading
import Queue
try:
import queue
except ImportError:
import Queue as queue

import pyagentx
from pyagentx.pdu import PDU


class Network(threading.Thread):

def __init__(self, queue, oid_list, sethandlers):
def __init__(self, update_queue, oid_list, sethandlers):
threading.Thread.__init__(self)
self.stop = threading.Event()
self._queue = queue
self._queue = update_queue
self._oid_list = oid_list
self._sethandlers = sethandlers

Expand Down Expand Up @@ -84,7 +92,7 @@ def _get_updates(self):
update_oid = item['oid']
update_data = item['data']
# clear values with prefix oid
for oid in self.data.keys():
for oid in list(self.data.keys()):
if oid.startswith(update_oid):
del(self.data[oid])
# insert updated value
Expand All @@ -94,7 +102,7 @@ def _get_updates(self):
'value':row['value']}
# recalculate reverse index if data changed
self.data_idx = sorted(self.data.keys(), key=lambda k: tuple(int(part) for part in k.split('.')))
except Queue.Empty:
except queue.Empty:
break


Expand Down
20 changes: 13 additions & 7 deletions pyagentx/pdu.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import (
absolute_import,
division,
print_function,
)

# --------------------------------------------
import logging
Expand Down Expand Up @@ -68,10 +73,11 @@ def encode_oid(self, oid, include=0):


def encode_octet(self, octet):
octet = octet.encode("utf-8")
buf = struct.pack('!L', len(octet))
buf += str(octet)
buf += octet
padding = ( 4 - ( len(octet) % 4 ) ) % 4
buf += chr(0)* padding
buf += chr(0).encode() * padding
return buf


Expand Down Expand Up @@ -107,7 +113,7 @@ def encode_header(self, pdu_type, payload_length=0, flags=0):


def encode(self):
buf = ''
buf = b''
if self.type == pyagentx.AGENTX_OPEN_PDU:
# timeout
buf += struct.pack('!BBBB', 5, 0, 0, 0)
Expand Down Expand Up @@ -169,7 +175,7 @@ def decode_oid(self):
sub_ids.append(t[0])
oid = '.'.join(str(i) for i in sub_ids)
return oid, ret['include']
except Exception, e:
except Exception as e:
logger.exception('Invalid packing OID header')
logger.debug('%s' % pprint.pformat(self.decode_buf))

Expand All @@ -196,15 +202,15 @@ def decode_octet(self):
buf = self.decode_buf[:l]
self.decode_buf = self.decode_buf[l+padding:]
return buf
except Exception, e:
except Exception as e:
logger.exception('Invalid packing octet header')


def decode_value(self):
try:
vtype,_ = struct.unpack('!HH', self.decode_buf[:4])
self.decode_buf = self.decode_buf[4:]
except Exception, e:
except Exception as e:
logger.exception('Invalid packing value header')
oid,_ = self.decode_oid()
if vtype in [pyagentx.TYPE_INTEGER, pyagentx.TYPE_COUNTER32, pyagentx.TYPE_GAUGE32, pyagentx.TYPE_TIMETICKS]:
Expand Down Expand Up @@ -252,7 +258,7 @@ def decode_header(self):
context = self.decode_octet()
logger.debug('Context: %s' % context)
return ret
except Exception, e:
except Exception as e:
logger.exception('Invalid packing: %d' % len(self.decode_buf))
logger.debug('%s' % pprint.pformat(self.decode_buf))

Expand Down
5 changes: 5 additions & 0 deletions pyagentx/sethandler.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import (
absolute_import,
division,
print_function,
)

# --------------------------------------------
import logging
Expand Down
13 changes: 11 additions & 2 deletions pyagentx/updater.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import (
absolute_import,
division,
print_function,
)

# --------------------------------------------
import logging
Expand All @@ -12,7 +17,11 @@ def emit(self, record):

import time
import threading
import Queue
try:
import queue
except ImportError:
import Queue as queue


import pyagentx

Expand All @@ -39,7 +48,7 @@ def run(self):
self.update()
self._queue.put_nowait({'oid': self._oid,
'data':self._data})
except Queue.Full:
except queue.Full:
logger.error('Queue full')
except:
logger.exception('Unhandled update exception')
Expand Down