Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixing up example code

  • Loading branch information...
commit 6f5d945882e4e93fb7a27f97aa7f91c4c76493fe 1 parent c4786fc
@andrewgross andrewgross authored
Showing with 59 additions and 11 deletions.
  1. +7 −4 plugins/parsers/nginx.py
  2. +52 −7 plugins/processors/url.py
View
11 plugins/parsers/nginx.py
@@ -1,24 +1,27 @@
import re
def parse_nginx(line):
- regex = re.compile("(?P<ip_address>\S*)\s-\s(?P<requesting_user>\S*)\s\[(?P<timestamp>.*?)\]\s\s\"(?P<method>\S*)\s*(?P<request>\S*)\s*(HTTP\/)*(?P<http_version>.*?)\"\s(?P<response_code>\d{3})\s(?P<size>\S*)\s\"(?P<referrer>[^\"]*)\"\s\"(?P<client>[^\"]*)\"\s(?P<service_time>\S*)\s(?P<application_time>\S*)\s(?P<pipe>\S*)")
+ regex = re.compile("(?P<ip_address>\S*)\s-\s(?P<requesting_user>\S*)\s\[(?P<timestamp>.*?)\]\s\s\"(?P<method>\S*)\s*(?P<request>\S*)\s*(HTTP\/)*(?P<http_version>.*?)\"\s(?P<response_code>\d{3})\s(?P<size>\S*)\s\"(?P<referrer>[^\"]*)\"\s\"(?P<client>[^\"]*)\"\s(?P<service_time>\S*)\s(?P<application_time>\S*)\s(?P<pipe>\S*)")
r = regex.search(line)
result_set = {}
if r:
for k, v in r.groupdict().iteritems():
- if v is None or v == "-":
+ if v is None or v is "-":
continue
if "request" in k:
if "?" in v:
request = v.partition("?")
path = request[0]
query = request[2]
- result_set["path"] = path
+ result_set["path"] = path
result_set["query"] = query
r.groupdict().pop(k)
continue
+ else:
+ result_set["path"] = r.groupdict().pop(k)
+ continue
result_set[k] = r.groupdict().pop(k)
- return result_set
+ return result_set
View
59 plugins/processors/url.py
@@ -1,13 +1,58 @@
from plugins.outputs.statsd import output_statsd_count
def process_ajax(line):
- if line['request'].startswith('/ajax/'):
- output_statsd_count('ajax')
+ if 'path' in line.keys():
+ if line['path'].startswith('/ajax/'):
+ output_statsd_count('call.ajax')
def process_api(line):
- if line['request'].startswith('/api/'):
- output_statsd_count('api')
+ if 'path' in line.keys():
+ if line['path'].startswith('/api/'):
+ output_statsd_count('call.api')
-def process_business(line):
- if line['request'].startswith('/business/'):
- output_statsd_count('business')
+def process_os_and_user_agent_request(line):
+ if 'client' in line.keys():
+ user_agent, os = _get_platform(line['client'])
+ metric_name = 'browser_request.{0}.{1}'.format(user_agent, os)
+ output_statsd_count(metric_name)
+
+def _get_platform(user_agent_string):
+ user_agent_string = user_agent_string.lower()
+
+ # Dumb parser to attempt to get a simple UA and OS
+ user_agent = 'unknown'
+ os = 'unknown'
+
+ # Check IE and get version
+ if 'msie' in user_agent_string:
+ for ie_version in ['4','5','6', '7', '8', '9', '10']:
+ if "msie {}".format(ie_version) in user_agent_string:
+ user_agent = 'ie{}'.format(ie_version)
+
+ # Check Webkit
+ if 'safari' in user_agent_string:
+ # Determine if Chrome or Safari
+ if 'chrome' in user_agent_string:
+ user_agent = 'chrome'
+ else:
+ user_agent = 'safari'
+
+ # Check Firefox
+ if 'firefox' in user_agent_string:
+ user_agent = 'firefox'
+
+ # Figure out OS
+ if 'windows' in user_agent_string:
+ os = 'windows'
+ elif 'mac' in user_agent_string:
+ os = 'mac'
+ elif 'linux' in user_agent_string:
+ os = 'linux'
+
+ # Keep track of our bots
+ for bot in ['bot', 'spider', 'symfony', 'grabber']:
+ if bot in user_agent_string:
+ user_agent = 'bot'
+ os = 'bot'
+
+ return (user_agent, os)
Please sign in to comment.
Something went wrong with that request. Please try again.