示例#1
0
 def generate_log_file_name(self):
     name = ''
     if self.bucket_prefix:
         name = self.bucket_prefix+'/'
     # {Bucket}/{Prefix}/AWSLogs/{AWS AccountID}/elasticloadbalancing/{Region}/{Year}/{Month}/{Day}/{AWS Account ID}_elasticloadbalancing_{Region}_{Load Balancer Name}_{End Time}_{Load Balancer IP}_{Random String}.log
     # S3://mylogsbucket/myapp/prod/AWSLogs/123456789012/elasticloadbalancing/us-east-1/2014/02/15/123456789012_elasticloadbalancing_us-east-1_my-test-loadbalancer_20140215T2340Z_172.160.001.192_20sg8hgm.log
     now = dt.utcnow()
     ip_addr = self.get_accesslog_ip()
     if not ip_addr:
         ip_addr = '127.0.0.1'
     name = name + 'AWSLogs/' + config.get_owner_account_id() + '/elasticloadbalancing/eucalyptus/' + str(now.year)\
         + '/' + str(now.month) + '/' + str(now.day) + '/' + config.get_owner_account_id()\
         + '_elasticloadbalancing_eucalyptus_' + self.loadbalancer + '_' + now.strftime('%Y%m%dT%H%MZ') + '_'\
         + ip_addr + '_' + ''.join(random.choice(string.ascii_lowercase + string.digits) for _ in range(8)) + '.log'
     return name
    def do_emit(self):
        servo.log.debug('Trying to emit access log of %d entries' % len(self._logs))
        print 'Trying to emit access log of %d entries' % len(self._logs)
        aws_access_key_id = config.get_access_key_id()
        aws_secret_access_key = config.get_secret_access_key()
        security_token = config.get_security_token()
        conn = boto.connect_s3(aws_access_key_id=aws_access_key_id, aws_secret_access_key=aws_secret_access_key, security_token=security_token, is_secure=False, port=8773, path='/services/objectstorage', host= config.get_objectstorage_service_host(), calling_format='boto.s3.connection.OrdinaryCallingFormat')
        if not conn:
            raise Exception('Could not connect to object storage (S3) service') 

        key_name = self.generate_log_file_name()
        tmpfile = tempfile.mkstemp()
        fd = os.fdopen(tmpfile[0],'w')
        tmpfile_path = tmpfile[1]
	for line in self._logs:
            fd.write(line+'\n')
        fd.close()
        
        bucket = conn.get_bucket(self.bucket_name)
        k = Key(bucket)
        k.key = key_name
        k.set_contents_from_filename(tmpfile_path)
        k.add_user_grant('FULL_CONTROL', config.get_owner_account_id())

        os.unlink(tmpfile_path)
        servo.log.debug('Access logs were emitted successfully: s3://%s/%s'  % (self.bucket_name,key_name))
 def generate_log_file_name(self):
     name = ''
     if self.bucket_prefix:
         name = self.bucket_prefix+'/'
     #{Bucket}/{Prefix}/AWSLogs/{AWS AccountID}/elasticloadbalancing/{Region}/{Year}/{Month}/{Day}/{AWS Account ID}_elasticloadbalancing_{Region}_{Load Balancer Name}_{End Time}_{Load Balancer IP}_{Random String}.log
     #S3://mylogsbucket/myapp/prod/AWSLogs/123456789012/elasticloadbalancing/us-east-1/2014/02/15/123456789012_elasticloadbalancing_us-east-1_my-test-loadbalancer_20140215T2340Z_172.160.001.192_20sg8hgm.log
     now = dt.utcnow()
     name = name + 'AWSLogs/' + config.get_owner_account_id() + '/elasticloadbalancing/eucalyptus/'+str(now.year)+'/'+str(now.month)+'/'+str(now.day)+'/'+config.get_owner_account_id()+'_elasticloadbalancing_eucalyptus_'+self.loadbalancer+'_'+now.strftime('%Y%m%dT%H%MZ')+'_'+socket.gethostbyname(socket.gethostname())+'_'+''.join(random.choice(string.ascii_lowercase + string.digits) for _ in range(8)) +'.log'
     return name