Paul Hobbs | ef4e070 | 2016-06-27 17:01:42 -0700 | [diff] [blame] | 1 | #!/usr/bin/python2 |
| 2 | |
| 3 | # Copyright 2016 The Chromium OS Authors. All rights reserved. |
| 4 | # Use of this source code is governed by a BSD-style license that can be |
| 5 | # found in the LICENSE file. |
| 6 | |
| 7 | """Script to upload metrics from apache logs to Monarch. |
| 8 | |
| 9 | We are interested in static file bandwidth, so it parses out GET requests to |
| 10 | /static and uploads the sizes to a cumulative metric. |
| 11 | """ |
| 12 | from __future__ import print_function |
| 13 | |
| 14 | import argparse |
| 15 | import logging |
| 16 | import re |
| 17 | import sys |
| 18 | |
| 19 | from devserver import MakeLogHandler |
| 20 | |
| 21 | from chromite.lib import ts_mon_config |
| 22 | from chromite.lib import metrics |
| 23 | from infra_libs import ts_mon |
| 24 | |
| 25 | |
| 26 | STATIC_GET_MATCHER = re.compile( |
| 27 | r'^(?P<ip_addr>\d+\.\d+\.\d+\.\d+) ' |
| 28 | r'.*GET /static/\S*[^"]*" ' |
| 29 | r'200 (?P<size>\S+) .*') |
| 30 | |
| 31 | STATIC_GET_METRIC_NAME = 'chromeos/devserver/apache/static_response_size' |
| 32 | |
| 33 | |
| 34 | LAB_SUBNETS = ( |
| 35 | ("172.17.40.0", 22), |
| 36 | ("100.107.160.0", 19), |
| 37 | ("100.115.128.0", 17), |
| 38 | ("100.115.254.126", 25), |
| 39 | ("100.107.141.128", 25), |
| 40 | ("172.27.212.0", 22), |
| 41 | ("100.107.156.192", 26), |
| 42 | ("172.22.29.0", 25), |
| 43 | ("172.22.38.0", 23), |
| 44 | ("100.107.224.0", 23), |
| 45 | ("100.107.226.0", 25), |
| 46 | ("100.107.126.0", 25), |
| 47 | ) |
| 48 | |
| 49 | def IPToNum(ip): |
| 50 | return reduce(lambda seed, x: seed * 2**8 + int(x), ip.split('.'), 0) |
| 51 | |
| 52 | |
| 53 | def MatchesSubnet(ip, base, mask): |
| 54 | ip_value = IPToNum(ip) |
| 55 | base_value = IPToNum(base) |
| 56 | mask = (2**mask - 1) << (32 - mask) |
| 57 | return (ip_value & mask) == (base_value & mask) |
| 58 | |
| 59 | |
| 60 | def InLab(ip): |
| 61 | return any(MatchesSubnet(ip, base, mask) |
| 62 | for (base, mask) in LAB_SUBNETS) |
| 63 | |
| 64 | |
| 65 | def EmitStaticRequestMetric(m): |
| 66 | """Emits a Counter metric for sucessful GETs to /static endpoints.""" |
| 67 | ipaddr, size = m.groups() |
| 68 | try: |
| 69 | size = int(size) |
| 70 | except ValueError: # Zero is represented by "-" |
| 71 | size = 0 |
| 72 | |
| 73 | metrics.Counter(STATIC_GET_METRIC_NAME).increment_by( |
| 74 | size, fields={ |
| 75 | 'builder': '', |
| 76 | 'in_lab': InLab(ipaddr), |
| 77 | 'endpoint': ''}) |
| 78 | |
| 79 | |
| 80 | def RunMatchers(stream, matchers): |
| 81 | """Parses lines of |stream| using patterns and emitters from |matchers|""" |
| 82 | for line in stream: |
| 83 | for matcher, emitter in matchers: |
| 84 | m = matcher.match(line) |
| 85 | if m: |
| 86 | emitter(m) |
| 87 | # The input might terminate if the log gets rotated. Make sure that Monarch |
| 88 | # flushes any pending metrics before quitting. |
| 89 | ts_mon.close() |
| 90 | |
| 91 | |
| 92 | # TODO(phobbs) add a matcher for all requests, not just static files. |
| 93 | MATCHERS = [ |
| 94 | (STATIC_GET_MATCHER, EmitStaticRequestMetric), |
| 95 | ] |
| 96 | |
| 97 | |
| 98 | def ParseArgs(): |
| 99 | """Parses command line arguments.""" |
| 100 | p = argparse.ArgumentParser( |
| 101 | description='Parses apache logs and emits metrics to Monarch') |
| 102 | p.add_argument('--logfile') |
| 103 | return p.parse_args() |
| 104 | |
| 105 | |
| 106 | def main(): |
| 107 | """Sets up logging and runs matchers against stdin""" |
| 108 | args = ParseArgs() |
| 109 | root = logging.getLogger() |
| 110 | root.addHandler(MakeLogHandler(args.logfile)) |
| 111 | root.setLevel(logging.DEBUG) |
| 112 | ts_mon_config.SetupTsMonGlobalState('devserver_apache_log_metrics') |
| 113 | RunMatchers(sys.stdin, MATCHERS) |
| 114 | |
| 115 | |
| 116 | if __name__ == '__main__': |
| 117 | main() |