[Git][NTPsec/ntpsec][master] 4 commits: Removed old debugging print()s.
Ian Bruene
gitlab at mg.gitlab.com
Wed Feb 28 18:14:53 UTC 2018
Ian Bruene pushed to branch master at NTPsec / ntpsec
Commits:
f64ceafb by Ian Bruene at 2018-02-28T12:14:29-06:00
Removed old debugging print()s.
- - - - -
4a4afb6d by Ian Bruene at 2018-02-28T12:14:29-06:00
Made existing logging consistent.
- - - - -
ad7aa2a4 by Ian Bruene at 2018-02-28T12:14:29-06:00
Removed duplicated logging calls.
- - - - -
b5c369a5 by Ian Bruene at 2018-02-28T12:14:29-06:00
Changed spin timings.
Changed PacketControl default spinGap back to 0.001s
Added spinGap to DataSource notification handler with 0.1s gap.
- - - - -
2 changed files:
- ntpclients/ntpsnmpd.py
- pylib/agentx.py
Changes:
=====================================
ntpclients/ntpsnmpd.py
=====================================
--- a/ntpclients/ntpsnmpd.py
+++ b/ntpclients/ntpsnmpd.py
@@ -33,6 +33,8 @@ nofork = False
debug = 0
defaultTimeout = 30
+log = (lambda msg, msgdbg: ntp.util.dolog(logfp, msg, debug, msgdbg))
+
ntpRootOID = (1, 3, 6, 1, 2, 1, 197) # mib-2 . 197, aka: NTPv4-MIB
snmpTrapOID = (1, 3, 6, 1, 6, 3, 1, 1, 4, 1, 0)
@@ -43,7 +45,7 @@ DEFLOG = "ntpsnmpd.log"
class DataSource(ntp.agentx.MIBControl):
- def __init__(self, hostname=DEFHOST, settingsFile=None):
+ def __init__(self, hostname=DEFHOST, settingsFile=None, notifySpin=0.1):
# This is defined as a dict tree because it is simpler, and avoids
# certain edge cases
# OIDs are relative from ntp root
@@ -232,9 +234,12 @@ class DataSource(ntp.agentx.MIBControl):
self.settingsFilename = settingsFile
# Cache so we don't hammer ntpd, default 1 second timeout
# Timeout default pulled from a hat: we don't want it to last for
- # long, just not flood ntpd when we don't need to.
+ # long, just not flood ntpd with duplicatte requests during a walk.
self.cache = ntp.util.Cache(1)
self.oldValues = {} # Used by notifications to detect changes
+ # spinGap so we don't spam ntpd with requests during notify checks
+ self.notifySpinTime = notifySpin
+ self.lastNotifyCheck = 0
self.lastHeartbeat = 0 # Timestamp used for heartbeat notifications
self.heartbeatInterval = 0 # should save to disk
self.sentNotifications = 0
@@ -697,6 +702,10 @@ class DataSource(ntp.agentx.MIBControl):
# =====================================
def checkNotifications(self, control):
+ currentTime = time.time()
+ if (currentTime - self.lastNotifyCheck) < self.notifySpinTime:
+ return
+ self.lastNotifyCheck = currentTime
if self.notifyModeChange is True:
self.doNotifyModeChange(control)
@@ -807,9 +816,7 @@ class DataSource(ntp.agentx.MIBControl):
else:
datetime = ntp.util.deformatNTPTime(datetime["reftime"])
adds, rms = changes
- print("Notify Change assoc:", changes)
if which in ("add", "both"):
- print("adding", adds)
for name in adds:
vl = [ax.Varbind(ax.VALUE_OID, snmpTrapOID,
ax.OID(ntpRootOID + (0, 4))), # Add
@@ -823,7 +830,6 @@ class DataSource(ntp.agentx.MIBControl):
control.sendNotify(vl)
self.sentNotifications += 1
if which in ("rm", "both"):
- print("removing", rms)
for name in rms:
vl = [ax.Varbind(ax.VALUE_OID, snmpTrapOID,
ax.OID(ntpRootOID + (0, 5))), # Remove
@@ -1076,14 +1082,6 @@ class DataSource(ntp.agentx.MIBControl):
return peerdata
-def dolog(text, level):
- if debug >= level:
- try:
- logfp.write(text)
- except Exception:
- pass
-
-
def connect(address):
try:
if type(address) is str:
@@ -1095,13 +1093,14 @@ def connect(address):
sock = socket.socket(af, socket.SOCK_STREAM)
sock.connect((host, port))
except socket.error as msg:
- dolog(repr(msg) + "\n", 2)
+ log(repr(msg) + "\n", 1)
sys.exit(1)
+ log("connected to master agent at " + address, 3)
return sock
def mainloop(snmpSocket, reconnectionAddr, host=None):
- dolog("initing loop\n", 1)
+ log("initing loop\n", 3)
dbase = DataSource(host, "/var/ntpsntpd/notify.conf")
while True: # Loop reconnection attempts
control = PacketControl(snmpSocket, dbase, logfp=logfp, debug=debug)
@@ -1110,6 +1109,7 @@ def mainloop(snmpSocket, reconnectionAddr, host=None):
if control.mainloop(True) is False: # disconnected
snmpSocket.close()
snmpSocket = connect(reconnectionAddr)
+ log("disconnected from master, attempting reconnect\n", 2)
else: # Something else happened
break
@@ -1117,10 +1117,10 @@ def mainloop(snmpSocket, reconnectionAddr, host=None):
def daemonize(runfunc, *runArgs):
pid = os.fork()
if pid < 0:
- print("Forking error", pid)
+ log("Forking error " + str(pid) + "\n", 1)
sys.exit(pid)
elif pid > 0: # We are the parent
- print("Daemonization success, child pid:", pid)
+ log("Daemonization success, child pid: " + str(pid) + "\n", 3)
sys.exit(0)
# We must be the child
=====================================
pylib/agentx.py
=====================================
--- a/pylib/agentx.py
+++ b/pylib/agentx.py
@@ -122,7 +122,7 @@ class MIBControl:
class PacketControl:
- def __init__(self, sock, dbase, spinGap=0.01, timeout=defaultTimeout,
+ def __init__(self, sock, dbase, spinGap=0.001, timeout=defaultTimeout,
logfp=None, debug=10000):
self.log = (lambda txt, dbg : ntp.util.dolog(logfp, txt, debug, dbg))
# take a pre-made socket instead of making our own so that
@@ -181,7 +181,7 @@ class PacketControl:
self.pduHandlers[ptype](packet)
else:
self.log("dropping packet type %i, not implemented\n" % ptype,
- 1)
+ 2)
self.checkResponses()
if self.lastReception is not None:
currentTime = time.time()
@@ -189,12 +189,11 @@ class PacketControl:
self.sendPing()
def initNewSession(self):
- self.log("init new session...\n", 1)
+ self.log("init new session...\n", 3)
# We already have a connection, need to open a session.
openpkt = ax.OpenPDU(True, 23, 0, 0, self.timeout, (),
"NTPsec SNMP subagent")
self.sendPacket(openpkt, False)
- self.log("Sent open packet\n", 1)
response = self.waitForResponse(openpkt, True)
self.sessionID = response.sessionID
# Register the tree
@@ -204,7 +203,6 @@ class PacketControl:
self.database.mib_upperBound(),
self.database.mib_context())
self.sendPacket(register, False)
- self.log("Sent registration\n", 1)
response = self.waitForResponse(register, True)
self.stillConnected = True
@@ -214,7 +212,6 @@ class PacketControl:
self.packetEater()
while len(self.recievedPackets) > 0:
packet = self.recievedPackets.pop(0)
- self.log("Waiting, got packet: " + repr(packet) + "\n\n", 3)
if packet.__class__ != ax.ResponsePDU:
continue
haveit = (opkt.transactionID == packet.transactionID) and \
@@ -222,6 +219,7 @@ class PacketControl:
if ignoreSID is False:
haveit = haveit and (opkt.sessionID == packet.sessionID)
if haveit is True:
+ self.log("Recieved waited for response", 4)
return packet
time.sleep(self.spinGap)
@@ -248,7 +246,7 @@ class PacketControl:
self.recievedPackets.append(pkt)
if pkt.transactionID > self.highestTransactionID:
self.highestTransactionID = pkt.transactionID
- self.log("\npacketEater got a full packet: %s\n" % repr(pkt), 3)
+ self.log("Received a full packet: %s\n" % repr(pkt), 4)
except ax.ParseDataLengthError:
return None # this happens if we don't have all of a packet
except (ax.ParseVersionError, ax.ParsePDUTypeError,
@@ -265,8 +263,7 @@ class PacketControl:
def sendPacket(self, packet, expectsReply, replyTimeout=defaultTimeout,
callback=None):
encoded = packet.encode()
- self.log("\nsending packet: %s\n%s \n" % (repr(packet), repr(encoded)),
- 4)
+ self.log("Sending packet: %s\n" % repr(packet), 4)
self.socket.sendall(encoded)
if expectsReply is True:
index = (packet.sessionID,
@@ -275,7 +272,6 @@ class PacketControl:
self.packetLog[index] = (replyTimeout, packet, callback)
def sendPing(self):
- # DUMMY transactionID, does this count for Pings?
# DUMMY packetID, does this need to change? or does the pktID only
# count relative to a given transaction ID?
tid = self.highestTransactionID + 5 # +5 to avoid collisions
@@ -316,7 +312,7 @@ class PacketControl:
tmp = tmp[0]
newdata = tmp.recv(4096) # Arbitrary value
if len(newdata) > 0:
- self.log("Received data: " + repr(newdata) + "\n", 4)
+ self.log("Received data: %s\n" % repr(newdata), 5)
data += newdata
self.lastReception = time.time()
else:
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/compare/8b19b80f8b99c3977a02018b085ab5f590f2e033...b5c369a5d01f9932d34c50d2f8ff9042b6afbeb4
---
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/compare/8b19b80f8b99c3977a02018b085ab5f590f2e033...b5c369a5d01f9932d34c50d2f8ff9042b6afbeb4
You're receiving this email because of your account on gitlab.com.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ntpsec.org/pipermail/vc/attachments/20180228/ca05bbcd/attachment.html>
More information about the vc
mailing list