Hello,
Here is a tool that is checking for some docker events: https://bitbucket.org/quaideman/dem/src/master/app/main.py
I can see how it’s working and it’s pretty straightforward.
However, I cannot understand how it’s continually performing the task.
In main.py
, the client.events()
(2nd line in the try
block) is called once, right?
if __name__ == '__main__': signal.signal(signal.SIGINT, shutdown) signal.signal(signal.SIGTERM, shutdown) logger = log.load() config = conf.load() try: client = docker.DockerClient(base_url='unix://var/run/docker.sock') stream = client.events(decode=True) thisHost = client.info()['Name'] except: logger.info('Failed to connect to Docker event stream') shutdown() logger.info('Starting up') main()
Then, main()
is called, which basically do nothing if stream
(from above snippet) is empty, right?
def main(): ''' Look for any event on the event stream that matches the defined event types ''' for event in stream: logger.debug('Event: {}'.format(event)) eventType = event['Type'] eventAction = event['Action'] timestamp = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(event['time'])) if 'name' in event['Actor']['Attributes']: eventActorName = event['Actor']['Attributes']['name'] else: # No name available, use ID instead eventActorName = event['Actor']['ID'] ## Should the event trigger an alert if eventType in config['events']: if eventAction in config['events'][eventType]: if 'exclusions' in config['settings']: if not includeExclude(eventActorName,config['settings']['exclusions']): logger.info('Included Event: {}'.format(event)) if not silenceWindow(event): sendAlert(event,timestamp) else: logger.info('Silenced Event: {}'.format(event)) else: logger.info('Excluded Event: {}'.format(event)) elif 'inclusions' in config['settings']: if includeExclude(eventActorName,config['settings']['inclusions']): logger.info('Included Event: {}'.format(event)) if not silenceWindow(event): sendAlert(event,timestamp) else: logger.info('Silenced Event: {}'.format(event)) else: logger.info('Excluded Event: {}'.format(event)) else: ## If include or exclude lists do not exist, default to include logger.info('Included Event: {}'.format(event)) if not silenceWindow(event): sendAlert(event,timestamp) else: logger.info('Silenced Event: {}'.format(event))
But it seems this tool works continuously..
So I was thinking there should be a mechanism to handle periodic execution. But I cannot see how it’s done.
I wondered if it’s not done outside of Python by running the program indefinitely (like using cron
or bash
) but it doesn’t seem to be the case either.
Thanks for any hints!
submitted by /u/sanjibukai
[link] [comments]
r/learnpython Hello, Here is a tool that is checking for some docker events: https://bitbucket.org/quaideman/dem/src/master/app/main.py I can see how it’s working and it’s pretty straightforward. However, I cannot understand how it’s continually performing the task. In main.py, the client.events() (2nd line in the try block) is called once, right? if __name__ == ‘__main__’: signal.signal(signal.SIGINT, shutdown) signal.signal(signal.SIGTERM, shutdown) logger = log.load() config = conf.load() try: client = docker.DockerClient(base_url=’unix://var/run/docker.sock’) stream = client.events(decode=True) thisHost = client.info()[‘Name’] except: logger.info(‘Failed to connect to Docker event stream’) shutdown() logger.info(‘Starting up’) main() Then, main() is called, which basically do nothing if stream (from above snippet) is empty, right? def main(): ”’ Look for any event on the event stream that matches the defined event types ”’ for event in stream: logger.debug(‘Event: {}’.format(event)) eventType = event[‘Type’] eventAction = event[‘Action’] timestamp = time.strftime(‘%Y-%m-%d %H:%M:%S’, time.localtime(event[‘time’])) if ‘name’ in event[‘Actor’][‘Attributes’]: eventActorName = event[‘Actor’][‘Attributes’][‘name’] else: # No name available, use ID instead eventActorName = event[‘Actor’][‘ID’] ## Should the event trigger an alert if eventType in config[‘events’]: if eventAction in config[‘events’][eventType]: if ‘exclusions’ in config[‘settings’]: if not includeExclude(eventActorName,config[‘settings’][‘exclusions’]): logger.info(‘Included Event: {}’.format(event)) if not silenceWindow(event): sendAlert(event,timestamp) else: logger.info(‘Silenced Event: {}’.format(event)) else: logger.info(‘Excluded Event: {}’.format(event)) elif ‘inclusions’ in config[‘settings’]: if includeExclude(eventActorName,config[‘settings’][‘inclusions’]): logger.info(‘Included Event: {}’.format(event)) if not silenceWindow(event): sendAlert(event,timestamp) else: logger.info(‘Silenced Event: {}’.format(event)) else: logger.info(‘Excluded Event: {}’.format(event)) else: ## If include or exclude lists do not exist, default to include logger.info(‘Included Event: {}’.format(event)) if not silenceWindow(event): sendAlert(event,timestamp) else: logger.info(‘Silenced Event: {}’.format(event)) But it seems this tool works continuously.. So I was thinking there should be a mechanism to handle periodic execution. But I cannot see how it’s done. I wondered if it’s not done outside of Python by running the program indefinitely (like using cron or bash) but it doesn’t seem to be the case either. Thanks for any hints! submitted by /u/sanjibukai [link] [comments]
Hello,
Here is a tool that is checking for some docker events: https://bitbucket.org/quaideman/dem/src/master/app/main.py
I can see how it’s working and it’s pretty straightforward.
However, I cannot understand how it’s continually performing the task.
In main.py
, the client.events()
(2nd line in the try
block) is called once, right?
if __name__ == '__main__': signal.signal(signal.SIGINT, shutdown) signal.signal(signal.SIGTERM, shutdown) logger = log.load() config = conf.load() try: client = docker.DockerClient(base_url='unix://var/run/docker.sock') stream = client.events(decode=True) thisHost = client.info()['Name'] except: logger.info('Failed to connect to Docker event stream') shutdown() logger.info('Starting up') main()
Then, main()
is called, which basically do nothing if stream
(from above snippet) is empty, right?
def main(): ''' Look for any event on the event stream that matches the defined event types ''' for event in stream: logger.debug('Event: {}'.format(event)) eventType = event['Type'] eventAction = event['Action'] timestamp = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(event['time'])) if 'name' in event['Actor']['Attributes']: eventActorName = event['Actor']['Attributes']['name'] else: # No name available, use ID instead eventActorName = event['Actor']['ID'] ## Should the event trigger an alert if eventType in config['events']: if eventAction in config['events'][eventType]: if 'exclusions' in config['settings']: if not includeExclude(eventActorName,config['settings']['exclusions']): logger.info('Included Event: {}'.format(event)) if not silenceWindow(event): sendAlert(event,timestamp) else: logger.info('Silenced Event: {}'.format(event)) else: logger.info('Excluded Event: {}'.format(event)) elif 'inclusions' in config['settings']: if includeExclude(eventActorName,config['settings']['inclusions']): logger.info('Included Event: {}'.format(event)) if not silenceWindow(event): sendAlert(event,timestamp) else: logger.info('Silenced Event: {}'.format(event)) else: logger.info('Excluded Event: {}'.format(event)) else: ## If include or exclude lists do not exist, default to include logger.info('Included Event: {}'.format(event)) if not silenceWindow(event): sendAlert(event,timestamp) else: logger.info('Silenced Event: {}'.format(event))
But it seems this tool works continuously..
So I was thinking there should be a mechanism to handle periodic execution. But I cannot see how it’s done.
I wondered if it’s not done outside of Python by running the program indefinitely (like using cron
or bash
) but it doesn’t seem to be the case either.
Thanks for any hints!
submitted by /u/sanjibukai
[link] [comments]