def __init__(self, domain=None): self.conn = SDBConnection(keys.aws.AWS_ACCESS_KEY_ID, keys.aws.AWS_SECRET_KEY) self.domain_name = None self.domains = {} if domain is None and is_ec2(): stack = get_stack() stack_name = str(stack['instance']['stack']) self.domain_name = stack_name elif domain is not None: self.domain_name = domain
def getS3Key(stack_name=None): stack_name = stack_name or ec2_utils.get_stack().instance.stack BUCKET_NAME = "stamped.com.static.images" FILE_NAME = "search/v2/autocomplete/" + stack_name conn = S3Connection(keys.aws.AWS_ACCESS_KEY_ID, keys.aws.AWS_SECRET_KEY) bucket = conn.create_bucket(BUCKET_NAME) key = bucket.get_key(FILE_NAME) if key is None: key = bucket.new_key(FILE_NAME) return key
def sendFailLogEmail(self): if len(self.__fails) == 0: return stack_info = get_stack() stack_name = 'localhost' node_name = 'localhost' if stack_info is not None: stack_name = stack_info.instance.stack node_name = stack_info.instance.name output = '<html>' output += "<h3>RateLimiter Fail Limit reached on %s</h3>" % stack_name output += "<p>On stack '%s' instance '%s'.</p>" % (stack_name, node_name) output += "<p><i>There were %s failed requests to service '%s' within the last %s seconds</p></i>" % \ (self.fail_limit, self.__service_name, self.fail_period) back_online = time.strftime('%m/%d/%Y %H:%M:%S', time.localtime(self.blackout_start + self.blackout_wait)) # Timestamp output += "<p>Waiting for %s seconds. Service will be active again at: %s</p>" % (self.blackout_wait, back_online) output += '<h3>Fail Log</h3>' output += '<table border=1 cellpadding=5>' output += '<tr>' labels = ['Timestamp', 'Url', 'Body', 'Headers', 'Code', 'Content'] for label in labels: output += '<td style="font-weight:bold">%s</td>' % label output += '</tr>' for fail in self.__fails: output += '<tr>' output += '<td valign=top>%s</td>' % time.strftime('%m/%d/%Y %H:%M:%S', time.localtime(fail.timestamp)) # Timestamp output += '<td valign=top>%s</td>' % fail.url output += '<td valign=top>%s</td>' % fail.body output += '<td valign=top>%s</td>' % fail.headers output += '<td valign=top>%s</td>' % fail.status_code output += '<td valign=top>%s</td>' % escape(fail.content) output += '</tr>' output += '</table>' output += '</html>' try: email = {} email['from'] = 'Stamped <*****@*****.**>' email['to'] = '*****@*****.**' email['subject'] = "%s RateLimiter '%s' fail limit reached" % (stack_name, self.__service_name) email['body'] = output utils.sendEmail(email, format='html') except Exception as e: print('UNABLE TO SEND EMAIL: %s' % e) return output
def __init__(self, service_name=None, limit=None, period=None, cpd=None, fail_limit=None, fail_period=None, blackout_wait=None): self.__service_name = service_name self.__requests = RLPriorityQueue() self.limit = limit self.period = period self.cpd = cpd self.__curr_timeblock_start = time.time() self.__fails = deque() self.fail_limit = fail_limit self.fail_period = fail_period self.blackout_wait = blackout_wait self.blackout_start = None self.__request_dur_log = deque() self.__request_dur_log_size = REQUEST_DUR_LOG_SIZE self.__request_dur_cap = REQUEST_DUR_CAP self.__calls = 0 self.day_calls = 0 self.__day_start = self._getDay() self.__low_count = 0 self.__high_count = 0 self.__just_finished = 0 self.__semaphore = Semaphore() self.__queue_sem = Semaphore() self.__is_ratelimiter_node = False stack_info = get_stack() if stack_info is not None: self.__is_ratelimiter_node = 'ratelimiter' in stack_info.instance.roles if self.limit is not None and self.period is not None: self.__worker = gevent.spawn(workerProcess, self)
def parseCommandLine(): usage = "Usage: %prog [options] command [args]" version = "%prog " + __version__ parser = OptionParser(usage=usage, version=version) parser.add_option( "-d", "--headers", action="store_true", dest="show_headers", default=False, help="Include HTTP headers in results", ) parser.add_option( "-f", "--form", action="store_true", dest="show_form", default=False, help="Include user-submitted input form in results", ) parser.add_option( "-o", "--output", action="store_true", dest="show_output", default=False, help="Include JSON result in results" ) parser.add_option( "-e", "--errors", action="store_true", dest="show_errors", default=False, help="Only display errors in results" ) parser.add_option( "-v", "--verbose", action="store_true", dest="verbose", default=False, help="Set verbosity of logs" ) parser.add_option( "-s", "--stack", default=None, type="string", action="store", dest="stack", help="stack to pull logs from" ) parser.add_option("-u", "--user-id", dest="user_id", default=None, type="string", help="Filter results on user id") parser.add_option("-i", "--request-id", dest="request_id", default=None, type="string", help="Filter on request id") parser.add_option( "-m", "--method", dest="method", default=None, type="string", help="Request method (POST, GET, or ASYNC)" ) parser.add_option("-l", "--limit", dest="limit", default=10, type="int", help="Limit number of results returned") parser.add_option("-p", "--path", dest="path", default=None, type="string", help="Filter results on url path") parser.add_option( "-t", "--severity", dest="severity", default=None, type="string", help="Filter results on severity (debug, info, warning, error, critical)", ) parser.add_option("-c", "--code", dest="code", default=None, type="int", help="Filter results on status code") parser.add_option( "-S", "--slow", dest="slow", action="store", default=None, type="int", help="Filter results that took longer than n seconds", ) (options, args) = parser.parse_args() if is_ec2() or options.stack: logger_nodes = get_db_nodes("logger") if len(logger_nodes) == 0: print ("Warning: No instances exist on stack 'logger'") return options instance_name = "%s.%s" % (logger_nodes[0]["stack"], logger_nodes[0]["name"]) utils.init_log_db_config(instance_name) if options.stack is None: options.stack = get_stack().instance.stack return options