Skip to content

Commit a93b74b

Browse files
committed
Merge pull request #1 from DataDog/refactor_for_library_use
Refactor for library use
2 parents 2d9bcef + fb88012 commit a93b74b

File tree

2 files changed

+20
-13
lines changed

2 files changed

+20
-13
lines changed

agent.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
# Override the generic daemon class to run our checks
3737
class agent(Daemon):
3838

39-
def run(self):
39+
def run(self, agentConfig=None, run_forever=True):
4040
agentLogger = logging.getLogger('agent')
4141

4242
agentLogger.debug('Collecting basic system stats')
@@ -46,7 +46,10 @@ def run(self):
4646

4747
agentLogger.debug('Creating checks instance')
4848

49-
agentConfig, rawConfig = get_config()
49+
if agentConfig is None:
50+
agentConfig, rawConfig = get_config()
51+
else:
52+
rawConfig = {}
5053
emitter = http_emitter
5154

5255
# Checks instance
@@ -56,7 +59,8 @@ def run(self):
5659
agentLogger.debug('Scheduling checks every ' + str(agentConfig['checkFreq']) + ' seconds')
5760
s = sched.scheduler(time.time, time.sleep)
5861
c.doChecks(s, True, systemStats) # start immediately (case 28315)
59-
s.run()
62+
if run_forever:
63+
s.run()
6064

6165
def setupLogging(agentConfig):
6266
"""Used by ddagent.py as well"""

checks/common.py

+13-10
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,17 @@
3939

4040
from resources.processes import Processes as ResProcesses
4141

42+
def getUuid():
43+
# Generate a unique name that will stay constant between
44+
# invocations, such as platform.node() + uuid.getnode()
45+
# Use uuid5, which does not depend on the clock and is
46+
# recommended over uuid3.
47+
# This is important to be able to identify a server even if
48+
# its drives have been wiped clean.
49+
# Note that this is not foolproof but we can reconcile servers
50+
# on the back-end if need be, based on mac addresses.
51+
return uuid.uuid5(uuid.NAMESPACE_DNS, platform.node() + str(uuid.getnode())).hex
52+
4253
def recordsize(func):
4354
"Record the size of the response"
4455
def wrapper(*args, **kwargs):
@@ -102,7 +113,7 @@ def __init__(self, agentConfig, rawConfig, emitter):
102113

103114
self._event_checks = [Hudson(), Nagios(socket.gethostname())]
104115
self._resources_checks = [ResProcesses(self.checksLogger,self.agentConfig)]
105-
116+
106117
#
107118
# Checks - FIXME migrating to the new Check interface is a WIP
108119
#
@@ -334,15 +345,7 @@ def _doChecks(self, firstRun, systemStats=False):
334345
except socket.error:
335346
self.checksLogger.exception('Unable to get hostname')
336347

337-
# Generate a unique name that will stay constant between
338-
# invocations, such as platform.node() + uuid.getnode()
339-
# Use uuid5, which does not depend on the clock and is
340-
# recommended over uuid3.
341-
# This is important to be able to identify a server even if
342-
# its drives have been wiped clean.
343-
# Note that this is not foolproof but we can reconcile servers
344-
# on the back-end if need be, based on mac addresses.
345-
checksData['uuid'] = uuid.uuid5(uuid.NAMESPACE_DNS, platform.node() + str(uuid.getnode())).hex
348+
checksData['uuid'] = getUuid()
346349
self.checksLogger.debug('doChecks: added uuid %s' % checksData['uuid'])
347350

348351
# Process the event checks.

0 commit comments

Comments
 (0)