def authenticated(self, xs): logger.info('Authenticated %s' % str(self)) self.xmlstream.addObserver('/message', self.message) self.xmlstream.addObserver('/presence', self.presence) self.xmlstream.addObserver('/iq', self.iq) self.xmlstream.addObserver('/*', self.all) presence = domish.Element((None, 'presence')) self.xmlstream.send(presence) msg = domish.Element(('jabber:client', 'message')) msg['from'] = str(self) msg['to'] = '*****@*****.**' msg['type'] = 'chat' msg.addElement('body', 'jabber:client', 'testing') html = domish.Element((None, 'html')) html['xmlns'] = 'http://jabber.org/protocol/xhtml-im' body = domish.Element((None, 'body')) body['xmlns'] = 'http://www.w3.org/1999/xhtml' img = domish.Element((None, 'img')) # The hash should be of the data you send across data = open("seomoz.png", "rb").read().encode('base64') key = '*****@*****.**' % hashlib.sha1(data.replace('\n', '')).hexdigest() self.cids[key] = data img['src'] = 'cid:%s' % key img['alt'] = 'seomoz' body.addChild(img) html.addChild(body) msg.addChild(html) logger.warn(self.msgToString(msg)) self.xmlstream.send(msg)
def reconfig(self, path, **kwargs): self.patterns = dict([(k, re.compile(v)) for k,v in kwargs.items()]) self.path = path try: os.mkfifo(self.path) except OSError: logger.warn('Path "%s" already exists. Treating like fifo...' % self.path) self.f = os.open(self.path, os.O_RDONLY | os.O_NONBLOCK) self.stat = os.fstat(self.f)
def reconfig(self, path, **kwargs): self.patterns = dict([(k, re.compile(v)) for k, v in kwargs.items()]) self.path = path try: os.mkfifo(self.path) except OSError: logger.warn('Path "%s" already exists. Treating like fifo...' % self.path) self.f = os.open(self.path, os.O_RDONLY | os.O_NONBLOCK) self.stat = os.fstat(self.f)
def setInstanceId(self): try: # This looks a little weird, but the idea is that if you would # to have the InstanceId automatically filled in, then simply # add the key in the yaml file, but not the value. If you'd like # to override it, then you can override it by providing a value. # So, this covers the case that the key is provided, but no value if not self.dims.get('InstanceId'): self.dims['InstanceId'] = urllib2.urlopen('http://169.254.169.254/1.0/meta-data/instance-id', timeout=1.0).read().strip() except urllib2.URLError: logger.warn('Failed to get an instance ID for this node from Amazon')
def setInstanceId(self): try: # This looks a little weird, but the idea is that if you would # to have the InstanceId automatically filled in, then simply # add the key in the yaml file, but not the value. If you'd like # to override it, then you can override it by providing a value. # So, this covers the case that the key is provided, but no value if not self.dims: self.dims['InstanceId'] = get_instance_metadata()['instance-id'] except: logger.warn('Failed to get an instance ID for this node from Amazon')
def values(self): # Alright, first get new stats on the file s = os.fstat(self.f) # The lines we've read lines = [] # Now, see if the file was nuked # I'm not sure how this works. Checking inode might not really capture # what we're talking about. It certainly happens when the file is replaced, # but there /may/ be other times when it changes if s.st_ino != self.stat.st_ino: logger.warn('Inode for %s has changed' % self.path) os.close(self.f) self.f = os.open(self.path, os.O_RDONLY | os.O_NONBLOCK) elif s.st_mtime > self.stat.st_mtime: # If it's been modified since we last checked... r, w, e = select.select([self.f], [], [], 0) # And it's not read-ready, then we have to actually re-open it if len(r) == 0: os.close(self.f) self.f = os.open(self.path, os.O_RDONLY | os.O_NONBLOCK) # Now, remember the current stats self.stat = s # Now, let's check to see if it's ready for some reading content = '' r, w, e = select.select([self.f], [], [], 0) while len(r): content += os.read(self.f, 1024) r, w, e = select.select([self.f], [], [], 0) # Now, split it into lines lines = content.strip().split('\n') # Now that we have all our lines, go ahead and try to match the regex to each line counts = dict([(k, 0) for k in self.patterns]) for line in lines: for k, r in self.patterns.items(): m = r.search(line) if m: try: # Use the last matching group if found counts[k] += int(m.groups()[-1]) except ValueError: logger.warn('Could not parse int from %s. Using 1' % m.gorups()[-1]) counts[k] += 1 except IndexError: logger.info('No groups in regular expression. Using 1') counts[k] += 1 return { 'results': dict([(k, (v, 'Count')) for k, v in counts.items()]) }
def values(self): # Alright, first get new stats on the file s = os.fstat(self.f) # The lines we've read lines = [] # Now, see if the file was nuked # I'm not sure how this works. Checking inode might not really capture # what we're talking about. It certainly happens when the file is replaced, # but there /may/ be other times when it changes if s.st_ino != self.stat.st_ino: logger.warn('Inode for %s has changed' % self.path) os.close(self.f) self.f = os.open(self.path, os.O_RDONLY | os.O_NONBLOCK) elif s.st_mtime > self.stat.st_mtime: # If it's been modified since we last checked... r, w, e = select.select([self.f], [], [], 0) # And it's not read-ready, then we have to actually re-open it if len(r) == 0: os.close(self.f) self.f = os.open(self.path, os.O_RDONLY | os.O_NONBLOCK) # Now, remember the current stats self.stat = s # Now, let's check to see if it's ready for some reading content = '' r, w, e = select.select([self.f], [], [], 0) while len(r): content += os.read(self.f, 1024) r, w, e = select.select([self.f], [], [], 0) # Now, split it into lines lines = content.strip().split('\n') # Now that we have all our lines, go ahead and try to match the regex to each line counts = dict([(k, 0) for k in self.patterns]) for line in lines: for k, r in self.patterns.items(): m = r.search(line) if m: try: # Use the last matching group if found counts[k] += int(m.groups()[-1]) except ValueError: logger.warn('Could not parse int from %s. Using 1' % m.gorups()[-1]) counts[k] += 1 except IndexError: logger.info('No groups in regular expression. Using 1') counts[k] += 1 return { 'results' : dict([(k, (v, 'Count')) for k, v in counts.items()]) }
def getValues(self): if self.keys: results = self.values() pruned = {} for k in self.keys: try: pruned[k] = results['results'][k] except KeyError: logger.warn('Key %s unavailable' % k) results['results'] = pruned return results else: return self.values()
def setInstanceId(self): try: # This looks a little weird, but the idea is that if you would # to have the InstanceId automatically filled in, then simply # add the key in the yaml file, but not the value. If you'd like # to override it, then you can override it by providing a value. # So, this covers the case that the key is provided, but no value if not self.dims: self.dims['InstanceId'] = get_instance_metadata( )['instance-id'] except: logger.warn( 'Failed to get an instance ID for this node from Amazon')
def setInstanceId(self): try: # This looks a little weird, but the idea is that if you would # to have the InstanceId automatically filled in, then simply # add the key in the yaml file, but not the value. If you'd like # to override it, then you can override it by providing a value. # So, this covers the case that the key is provided, but no value if not self.dims.get('InstanceId'): self.dims['InstanceId'] = urllib2.urlopen( 'http://169.254.169.254/1.0/meta-data/instance-id', timeout=1.0).read().strip() except urllib2.URLError: logger.warn( 'Failed to get an instance ID for this node from Amazon')
def updateActions(self, actions): '''Update the actions on this account based on the supplied actions. Actions should a dictionary of Amazon Simple Notification Service topic names, and their associated subscriptions.''' # First, we need a SNS Connection to make this changes conn = SNSConnection(**self.kwargs) # Now make sure each subscription is registered to the topic for name, subscriptions in actions.items(): logger.info('Creating topic %s' % name) # Try to make a topic try: arn = conn.create_topic(name)['CreateTopicResponse'][ 'CreateTopicResult']['TopicArn'] self.actions[name] = arn except KeyError: raise EmitterException('Bad response creating topic %s' % name) if len(subscriptions) == 0: raise EmitterException('No subscriptions for action %s' % name) # Now try to arrange for subscriptions # Oddly enough, calling create_topic doesn't have any effect # if the topic already exists, but calling subscribe() for an # existing subscription causes a second subscription to be added # So, we have to get a list of current subscriptions, and then # make sure to only add the subscription if it's currently there logger.info('Getting a list of current subscriptions...') current = conn.get_all_subscriptions_by_topic(arn) current = current['ListSubscriptionsByTopicResponse'] current = current['ListSubscriptionsByTopicResult'] current = current['Subscriptions'] current = set(s['Endpoint'] for s in current) # For all desired subscriptions not present, subscribe for s in subscriptions: if s['endpoint'] not in current: logger.info('Adding %s to action %s' % (s['endpoint'], name)) conn.subscribe(arn, s.get('protocol', 'email'), s['endpoint']) else: logger.info('%s already subscribed to action' % s['endpoint']) # Check for subscriptions that are active, but not listed... activeUnlisted = set(current) - set( [s['endpoint'] for s in subscriptions]) for s in activeUnlisted: logger.warn('Subscript "%s" active, but not listed in config' % s)
def __init__(self, consumer_key, consumer_secret, access_token=None, access_secret=None): super(Twitter,self).__init__() # https://github.com/tweepy/tweepy/blob/master/ self.auth = tweepy.auth.OAuthHandler(consumer_key, consumer_secret) if not access_token or not access_secret: try: logger.warn('To authenticate, visit : %s' % self.auth.get_authorization_url()) verifier = raw_input('Verifier: ') except tweepy.error.TweepError: raise EmitterException('Failed to request token.') try: logger.info(repr(self.auth.get_access_token(verifier))) except tweepy.error.TweepError: raise EmitterException('Error! Failed to get access token.') else: self.auth.set_access_token(access_token, access_secret) self.api = tweepy.API(self.auth)
def updateActions(self, actions): '''Update the actions on this account based on the supplied actions. Actions should a dictionary of Amazon Simple Notification Service topic names, and their associated subscriptions.''' # First, we need a SNS Connection to make this changes conn = SNSConnection(**self.kwargs) # Now make sure each subscription is registered to the topic for name, subscriptions in actions.items(): logger.info('Creating topic %s' % name) # Try to make a topic try: arn = conn.create_topic(name)['CreateTopicResponse']['CreateTopicResult']['TopicArn'] self.actions[name] = arn except KeyError: raise EmitterException('Bad response creating topic %s' % name) if len(subscriptions) == 0: raise EmitterException('No subscriptions for action %s' % name) # Now try to arrange for subscriptions # Oddly enough, calling create_topic doesn't have any effect # if the topic already exists, but calling subscribe() for an # existing subscription causes a second subscription to be added # So, we have to get a list of current subscriptions, and then # make sure to only add the subscription if it's currently there logger.info('Getting a list of current subscriptions...') current = conn.get_all_subscriptions_by_topic(arn) current = current['ListSubscriptionsByTopicResponse'] current = current['ListSubscriptionsByTopicResult'] current = current['Subscriptions'] current = set(s['Endpoint'] for s in current) # For all desired subscriptions not present, subscribe for s in subscriptions: if s['endpoint'] not in current: logger.info('Adding %s to action %s' % (s['endpoint'], name)) conn.subscribe(arn, s.get('protocol', 'email'), s['endpoint']) else: logger.info('%s already subscribed to action' % s['endpoint']) # Check for subscriptions that are active, but not listed... activeUnlisted = set(current) - set([s['endpoint'] for s in subscriptions]) for s in activeUnlisted: logger.warn('Subscript "%s" active, but not listed in config' % s)
def __init__(self, consumer_key, consumer_secret, access_token=None, access_secret=None): super(Twitter, self).__init__() # https://github.com/tweepy/tweepy/blob/master/ self.auth = tweepy.auth.OAuthHandler(consumer_key, consumer_secret) if not access_token or not access_secret: try: logger.warn('To authenticate, visit : %s' % self.auth.get_authorization_url()) verifier = raw_input('Verifier: ') except tweepy.error.TweepError: raise EmitterException('Failed to request token.') try: logger.info(repr(self.auth.get_access_token(verifier))) except tweepy.error.TweepError: raise EmitterException('Error! Failed to get access token.') else: self.auth.set_access_token(access_token, access_secret) self.api = tweepy.API(self.auth)
def values(self): try: # Alright, first get new stats on the file s = os.lstat(self.path) # The lines we've read lines = [] # Now, see if the file was nuked # I'm not sure how this works. Checking inode might not really capture # what we're talking about. It certainly happens when the file is replaced, # but there /may/ be other times when it changes if s.st_ino != self.stat.st_ino: logger.warn('Inode for %s has changed' % self.path) self.f.close() self.f = file(self.path) lines = self.f.read(s.st_size).strip().split('\n') elif s.st_size < self.stat.st_size: logger.warn( 'File %s has shrunk since last read! Reading from beginning...' % self.path) self.f.seek(0) lines = self.f.read(s.st_size).strip().split('\n') elif s.st_mtime > self.stat.st_mtime: # If the file has been changed since last we looked self.f.seek(self.stat.st_size) lines = self.f.read(s.st_size - self.stat.st_size).strip().split('\n') # Now, remember the current stats self.stat = s # Now that we have all our lines, go ahead and try to match the regex to each line counts = dict([(k, 0) for k in self.patterns]) for line in lines: for k, r in self.patterns.items(): m = r.search(line) if m: try: # Use the last matching group if found counts[k] += int(m.groups()[-1]) except ValueError: logger.warn( 'Could not parse int from %s. Using 1' % m.gorups()[-1]) counts[k] += 1 except IndexError: logger.info( 'No groups in regular expression. Using 1') counts[k] += 1 return { 'results': dict([(k, (v, 'Count')) for k, v in counts.items()]) } except Exception as e: raise MetricException(e)
def values(self): try: # Alright, first get new stats on the file s = os.lstat(self.path) # The lines we've read lines = [] # Now, see if the file was nuked # I'm not sure how this works. Checking inode might not really capture # what we're talking about. It certainly happens when the file is replaced, # but there /may/ be other times when it changes if s.st_ino != self.stat.st_ino: logger.warn('Inode for %s has changed' % self.path) self.f.close() self.f = file(self.path) lines = self.f.read(s.st_size).strip().split('\n') elif s.st_size < self.stat.st_size: logger.warn('File %s has shrunk since last read! Reading from beginning...' % self.path) self.f.seek(0) lines = self.f.read(s.st_size).strip().split('\n') elif s.st_mtime > self.stat.st_mtime: # If the file has been changed since last we looked self.f.seek(self.stat.st_size) lines = self.f.read(s.st_size - self.stat.st_size).strip().split('\n') # Now, remember the current stats self.stat = s # Now that we have all our lines, go ahead and try to match the regex to each line counts = dict([(k, 0) for k in self.patterns]) for line in lines: for k, r in self.patterns.items(): m = r.search(line) if m: try: # Use the last matching group if found counts[k] += int(m.groups()[-1]) except ValueError: logger.warn('Could not parse int from %s. Using 1' % m.gorups()[-1]) counts[k] += 1 except IndexError: logger.info('No groups in regular expression. Using 1') counts[k] += 1 return { 'results' : dict([(k, (v, 'Count')) for k, v in counts.items()]) } except Exception as e: raise MetricException(e)
def init_failed(self, failure): logger.warn('Initialization failed %s : %s' % (str(self), failure.getErrorMessage()))