Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_http_status(self):
line = '127.0.0.1 - - [02/Jul/2015:14:49:48 +0000] "GET /basic_status HTTP/1.1" 200 110 "-" ' + \
'"python-requests/2.2.1 CPython/2.7.6 Linux/3.13.0-48-generic"'
# run single method
collector = NginxAccessLogsCollector(object=self.fake_object, tail=[])
collector.http_status(NginxAccessLogParser().parse(line))
# check
metrics = self.fake_object.statsd.current
assert_that(metrics, has_item('counter'))
counters = metrics['counter']
assert_that(counters, has_item('nginx.http.status.2xx'))
assert_that(counters['nginx.http.status.2xx'][0][1], equal_to(1))
'127.0.0.1 - - [02/Jul/2015:14:49:48 +0000] "GET /basic_status HTTP/1.1" %d 110 "-" '
'"python-requests/2.2.1 CPython/2.7.6 Linux/3.13.0-48-generic"'
)
# collect requests with $status 400 to 498
lines = [line_template % x for x in xrange(400, 499)]
NginxAccessLogsCollector(object=self.fake_object, tail=lines).collect()
counter = self.fake_object.statsd.flush()['metrics']['counter']
assert_that(counter, has_entries(
'C|nginx.http.status.4xx', collected_metric(99),
'C|nginx.http.status.discarded', collected_metric(0)
))
# collect single request with $status 499
tail = [line_template % 499]
NginxAccessLogsCollector(object=self.fake_object, tail=tail).collect()
counter = self.fake_object.statsd.flush()['metrics']['counter']
assert_that(counter, has_entries(
'C|nginx.http.status.4xx', collected_metric(1),
'C|nginx.http.status.discarded', collected_metric(1)
))
Account for nginx variables with numbers in them
"""
log_format = '$remote_addr [$time_local] $status $geoip_country_code ' + \
'$geoip_country_code3 "$geoip_country_name"'
lines = [
'10.10.10.102 [29/Jun/2016:23:31:07 +0000] 200 US USA "United States"',
'10.10.10.46 [29/Jun/2016:23:34:33 +0000] 200 CA CAN "Canada"',
'10.10.10.189 [29/Jun/2016:23:34:42 +0000] 200 IE IRL "Ireland"',
'10.10.10.194 [29/Jun/2016:23:35:02 +0000] 200 NL NLD "Netherlands"',
'10.10.10.198 [29/Jun/2016:23:37:08 +0000] 200 FR FRA "France"',
'10.10.10.232 [29/Jun/2016:23:37:49 +0000] 200 SG SGP "Singapore"',
'10.10.10.100 [29/Jun/2016:23:38:19 +0000] 200 ID IDN "Indonesia"'
]
collector = NginxAccessLogsCollector(object=self.fake_object, log_format=log_format, tail=lines)
collector.collect()
# Make sure that variable name with number is properly formatted...
keys = collector.parser.keys
assert_that(keys, has_item('geoip_country_code'))
assert_that(keys, has_item('geoip_country_code3'))
# check
metrics = self.fake_object.statsd.flush()['metrics']
assert_that(metrics, has_item('counter'))
# check some values
counter = metrics['counter']
assert_that(counter['C|nginx.http.status.2xx'][0][1], equal_to(7))
def test_upstream_status_and_length(self):
log_format = '$remote_addr - $remote_user [$time_local] ' + \
'"$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" ' + \
'rt=$request_time ut="$upstream_response_time" cs=$upstream_cache_status ' + \
'us=$upstream_status $upstream_response_length'
line = \
'1.2.3.4 - - [22/Jan/2010:19:34:21 +0300] "GET /foo/ HTTP/1.1" 200 11078 ' + \
'"http://www.rambler.ru/" "Mozilla/5.0 (Windows; U; Windows NT 5.1" rt=0.010 ut="2.001, 0.345" cs=MISS ' + \
'us=200 20'
# run single method
collector = NginxAccessLogsCollector(object=self.fake_object, tail=[])
collector.upstreams(NginxAccessLogParser(log_format).parse(line))
# check
metrics = self.fake_object.statsd.current
assert_that(metrics, has_item('counter'))
assert_that(metrics, has_item('average'))
assert_that(metrics, has_item('timer'))
# counters
counters = metrics['counter']
assert_that(counters, has_item('nginx.upstream.request.count'))
assert_that(counters, has_item('nginx.upstream.next.count'))
assert_that(counters, has_item('nginx.cache.miss'))
assert_that(counters, has_item('nginx.upstream.status.2xx'))
assert_that(counters['nginx.upstream.request.count'][0][1], equal_to(1))
assert_that(counters['nginx.upstream.next.count'][0][1], equal_to(1))
def test_upstream_status_multi_value(self):
log_format = '$remote_addr - $remote_user [$time_local] ' + \
'"$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" ' + \
'rt=$request_time us="$upstream_status" ut="$upstream_response_time" cs=$upstream_cache_status'
lines = [
'1.2.3.4 - - [22/Jan/2010:19:34:21 +0300] "GET /foo/ HTTP/1.1" 200 11078 ' +
'"http://www.rambler.ru/" "Mozilla/5.0 (Windows; U; Windows NT 5.1" rt=0.010 us="200" ut="2.001, 0.345" cs=MISS',
'1.2.3.4 - - [22/Jan/2010:20:34:21 +0300] "GET /foo/ HTTP/1.1" 300 1078 ' +
'"http://www.rambler.ru/" "Mozilla/5.0 (Windows; U; Windows NT 5.1" rt=0.010 us="502, 504" ut="2.002" cs=HIT',
]
collector = NginxAccessLogsCollector(object=self.fake_object,
log_format=log_format, tail=lines)
collector.collect()
# check
metrics = self.fake_object.statsd.flush()['metrics']
assert_that(metrics, has_item('counter'))
assert_that(metrics, has_item('timer'))
# counter keys
counter = metrics['counter']
for key in ['C|nginx.http.method.get',
'C|nginx.http.v1_1',
'C|nginx.upstream.next.count',
'C|nginx.upstream.request.count',
'C|nginx.http.status.3xx',
'C|nginx.cache.miss',
def test_server_name(self):
self.fake_object.filters = [
Filter(
filter_rule_id=2,
metric='nginx.http.status.2xx',
data=[
['$server_name', '~', 'differentsimgirls.com']
]
)
]
collector = NginxAccessLogsCollector(
object=self.fake_object,
log_format='$remote_addr - $remote_user [$time_local] \"$request\" $status $body_bytes_sent ' +
'\"$http_referer\" \"$http_user_agent\" \"$http_x_forwarded_for\" ' +
'rt=$request_time ua=\"$upstream_addr\" us=\"$upstream_status\" ' +
'ut=\"$upstream_response_time\" ul=\"$upstream_response_length\" ' +
'cs=$upstream_cache_status sn=$server_name',
tail=[
'104.236.93.23 - - [05/May/2016:12:52:50 +0200] "GET / HTTP/1.1" 200 28275 "-" ' +
'"curl/7.35.0" "-" rt=0.082 ua="-" us="-" ut="-" ul="-" cs=- sn=differentsimgirls.com'
]
)
collector.collect()
# check
metrics = self.fake_object.statsd.flush()['metrics']
assert_that(metrics, has_item('counter'))
def test_regex_filter(self):
self.fake_object.filters = [
Filter(
filter_rule_id=2,
metric='nginx.http.request.body_bytes_sent',
data=[
['$request_uri', '~', '/img.*'],
['$server_protocol', '~', 'HTTP/1.2']
]
)
]
collector = NginxAccessLogsCollector(object=self.fake_object, tail=self.lines)
collector.collect()
# check
metrics = self.fake_object.statsd.flush()['metrics']
assert_that(metrics, has_item('counter'))
# counters
counter = metrics['counter']
for key in ('C|nginx.http.method.get', 'C|nginx.http.request.body_bytes_sent', 'C|nginx.http.status.3xx',
'C|nginx.http.status.2xx', 'C|nginx.http.method.post', 'C|nginx.http.v1_1',
'C|nginx.http.status.4xx', 'C|nginx.http.request.body_bytes_sent||2', 'C|nginx.http.method.post'):
assert_that(counter, has_key(key))
# values
assert_that(counter['C|nginx.http.method.get'][0][1], equal_to(5))
assert_that(counter['C|nginx.http.method.post'][0][1], equal_to(2))
def test_collector_multi_line_log_format(self):
log_format = '''$remote_addr - $remote_user [$time_local]
"$request" $status $body_bytes_sent "$http_referer" "$http_user_agent"
rt=$request_time us="$upstream_status" ut="$upstream_response_time" cs=$upstream_cache_status'''
lines = [
'1.2.3.4 - - [22/Jan/2010:19:34:21 +0300]',
' "GET /foo/ HTTP/1.1" 200 11078 "http://www.rambler.ru/" "Mozilla/5.0 (Windows; U; Windows NT 5.1"',
' rt=0.010 us="200" ut="2.001, 0.345" cs=MISS',
'1.2.3.4 - - [22/Jan/2010:20:34:21 +0300]',
' "GET /foo/ HTTP/1.1" 300 1078 "http://www.rambler.ru/" "Mozilla/5.0 (Windows; U; Windows NT 5.1"',
' rt=0.010 us="502, 504" ut="2.002" cs=HIT'
]
collector = NginxAccessLogsCollector(object=self.fake_object,
log_format=log_format, tail=lines)
collector.collect()
# check
metrics = self.fake_object.statsd.flush()['metrics']
assert_that(metrics, has_item('counter'))
assert_that(metrics, has_item('timer'))
# counter keys
counter = metrics['counter']
for key in ['C|nginx.http.method.get',
'C|nginx.http.v1_1',
'C|nginx.upstream.next.count',
'C|nginx.upstream.request.count',
'C|nginx.http.status.3xx',
'C|nginx.cache.miss',
def _setup_access_logs(self):
# access logs
for log_description, log_data in self.config.access_logs.iteritems():
format_name = log_data['log_format']
log_format = self.config.log_formats.get(format_name)
tail = self.__setup_pipeline(log_description)
if tail:
self.collectors.append(
NginxAccessLogsCollector(
object=self,
interval=self.intervals['logs'],
log_format=log_format,
tail=tail
)
)
# Send access log discovery event.
self.eventd.event(level=INFO, message='nginx access log %s found' % log_description)
try:
parsed = self.parser.parse(line)
except:
context.log.debug('could not parse line %r' % line, exc_info=True)
parsed = None
if not parsed:
continue
if parsed['malformed']:
self.request_malformed()
else:
# try to match custom filters and collect log metrics with them
matched_filters = [filter for filter in self.filters if filter.match(parsed)]
super(NginxAccessLogsCollector, self).collect(parsed, matched_filters)
tail_name = self.tail.name if isinstance(self.tail, Pipeline) else 'list'
context.log.debug('%s processed %s lines from %s' % (self.object.definition_hash, count, tail_name))