Dan Smith | c2772c2 | 2022-04-08 08:48:49 -0700 | [diff] [blame] | 1 | #!/usr/bin/python3 |
| 2 | |
| 3 | import argparse |
Dan Smith | 64d6867 | 2022-04-22 07:58:29 -0700 | [diff] [blame] | 4 | import csv |
Dan Smith | c2772c2 | 2022-04-08 08:48:49 -0700 | [diff] [blame] | 5 | import datetime |
| 6 | import glob |
| 7 | import itertools |
| 8 | import json |
Dan Smith | 64d6867 | 2022-04-22 07:58:29 -0700 | [diff] [blame] | 9 | import logging |
Dan Smith | c2772c2 | 2022-04-08 08:48:49 -0700 | [diff] [blame] | 10 | import os |
Dan Smith | c2772c2 | 2022-04-08 08:48:49 -0700 | [diff] [blame] | 11 | import re |
| 12 | import socket |
| 13 | import subprocess |
| 14 | import sys |
Dan Smith | 1b601c7 | 2022-04-25 07:47:56 -0700 | [diff] [blame] | 15 | |
| 16 | try: |
| 17 | import psutil |
| 18 | except ImportError: |
| 19 | psutil = None |
| 20 | print('No psutil, process information will not be included', |
| 21 | file=sys.stderr) |
| 22 | |
| 23 | try: |
| 24 | import pymysql |
| 25 | except ImportError: |
| 26 | pymysql = None |
| 27 | print('No pymysql, database information will not be included', |
| 28 | file=sys.stderr) |
Dan Smith | c2772c2 | 2022-04-08 08:48:49 -0700 | [diff] [blame] | 29 | |
Dan Smith | 64d6867 | 2022-04-22 07:58:29 -0700 | [diff] [blame] | 30 | LOG = logging.getLogger('perf') |
| 31 | |
Dan Smith | c2772c2 | 2022-04-08 08:48:49 -0700 | [diff] [blame] | 32 | # https://www.elastic.co/blog/found-crash-elasticsearch#mapping-explosion |
| 33 | |
| 34 | |
| 35 | def tryint(value): |
| 36 | try: |
| 37 | return int(value) |
| 38 | except (ValueError, TypeError): |
| 39 | return value |
| 40 | |
| 41 | |
| 42 | def get_service_stats(service): |
| 43 | stats = {'MemoryCurrent': 0} |
| 44 | output = subprocess.check_output(['/usr/bin/systemctl', 'show', service] + |
| 45 | ['-p%s' % stat for stat in stats]) |
| 46 | for line in output.decode().split('\n'): |
| 47 | if not line: |
| 48 | continue |
| 49 | stat, val = line.split('=') |
Harald Jensås | bab0c92 | 2022-04-26 15:46:56 +0200 | [diff] [blame] | 50 | stats[stat] = tryint(val) |
Dan Smith | c2772c2 | 2022-04-08 08:48:49 -0700 | [diff] [blame] | 51 | |
| 52 | return stats |
| 53 | |
| 54 | |
| 55 | def get_services_stats(): |
| 56 | services = [os.path.basename(s) for s in |
Dan Smith | e85c68e | 2022-05-26 09:31:36 -0700 | [diff] [blame] | 57 | glob.glob('/etc/systemd/system/devstack@*.service')] + \ |
| 58 | ['apache2.service'] |
Dan Smith | c2772c2 | 2022-04-08 08:48:49 -0700 | [diff] [blame] | 59 | return [dict(service=service, **get_service_stats(service)) |
| 60 | for service in services] |
| 61 | |
| 62 | |
| 63 | def get_process_stats(proc): |
| 64 | cmdline = proc.cmdline() |
| 65 | if 'python' in cmdline[0]: |
| 66 | cmdline = cmdline[1:] |
| 67 | return {'cmd': cmdline[0], |
| 68 | 'pid': proc.pid, |
| 69 | 'args': ' '.join(cmdline[1:]), |
| 70 | 'rss': proc.memory_info().rss} |
| 71 | |
| 72 | |
| 73 | def get_processes_stats(matches): |
| 74 | me = os.getpid() |
| 75 | procs = psutil.process_iter() |
| 76 | |
| 77 | def proc_matches(proc): |
| 78 | return me != proc.pid and any( |
| 79 | re.search(match, ' '.join(proc.cmdline())) |
| 80 | for match in matches) |
| 81 | |
| 82 | return [ |
| 83 | get_process_stats(proc) |
| 84 | for proc in procs |
| 85 | if proc_matches(proc)] |
| 86 | |
| 87 | |
| 88 | def get_db_stats(host, user, passwd): |
| 89 | dbs = [] |
Dan Smith | 1cdf413 | 2022-05-23 13:56:13 -0700 | [diff] [blame] | 90 | try: |
| 91 | db = pymysql.connect(host=host, user=user, password=passwd, |
| 92 | database='stats', |
| 93 | cursorclass=pymysql.cursors.DictCursor) |
| 94 | except pymysql.err.OperationalError as e: |
| 95 | if 'Unknown database' in str(e): |
| 96 | print('No stats database; assuming devstack failed', |
| 97 | file=sys.stderr) |
| 98 | return [] |
| 99 | raise |
| 100 | |
Dan Smith | c2772c2 | 2022-04-08 08:48:49 -0700 | [diff] [blame] | 101 | with db: |
| 102 | with db.cursor() as cur: |
Dan Smith | fe52d7f | 2022-04-28 12:34:38 -0700 | [diff] [blame] | 103 | cur.execute('SELECT db,op,count FROM queries') |
Dan Smith | c2772c2 | 2022-04-08 08:48:49 -0700 | [diff] [blame] | 104 | for row in cur: |
| 105 | dbs.append({k: tryint(v) for k, v in row.items()}) |
| 106 | return dbs |
| 107 | |
| 108 | |
| 109 | def get_http_stats_for_log(logfile): |
| 110 | stats = {} |
Dan Smith | 64d6867 | 2022-04-22 07:58:29 -0700 | [diff] [blame] | 111 | apache_fields = ('host', 'a', 'b', 'date', 'tz', 'request', 'status', |
| 112 | 'length', 'c', 'agent') |
| 113 | ignore_agents = ('curl', 'uwsgi', 'nova-status') |
Dan Smith | fe7cfa6 | 2022-06-23 09:25:22 -0700 | [diff] [blame] | 114 | ignored_services = set() |
Dan Smith | 64d6867 | 2022-04-22 07:58:29 -0700 | [diff] [blame] | 115 | for line in csv.reader(open(logfile), delimiter=' '): |
| 116 | fields = dict(zip(apache_fields, line)) |
| 117 | if len(fields) != len(apache_fields): |
| 118 | # Not a combined access log, so we can bail completely |
| 119 | return [] |
| 120 | try: |
| 121 | method, url, http = fields['request'].split(' ') |
| 122 | except ValueError: |
| 123 | method = url = http = '' |
| 124 | if 'HTTP' not in http: |
| 125 | # Not a combined access log, so we can bail completely |
| 126 | return [] |
Dan Smith | c2772c2 | 2022-04-08 08:48:49 -0700 | [diff] [blame] | 127 | |
Dan Smith | 64d6867 | 2022-04-22 07:58:29 -0700 | [diff] [blame] | 128 | # Tempest's User-Agent is unchanged, but client libraries and |
| 129 | # inter-service API calls use proper strings. So assume |
| 130 | # 'python-urllib' is tempest so we can tell it apart. |
| 131 | if 'python-urllib' in fields['agent'].lower(): |
| 132 | agent = 'tempest' |
| 133 | else: |
| 134 | agent = fields['agent'].split(' ')[0] |
| 135 | if agent.startswith('python-'): |
| 136 | agent = agent.replace('python-', '') |
| 137 | if '/' in agent: |
| 138 | agent = agent.split('/')[0] |
Dan Smith | c2772c2 | 2022-04-08 08:48:49 -0700 | [diff] [blame] | 139 | |
Dan Smith | 64d6867 | 2022-04-22 07:58:29 -0700 | [diff] [blame] | 140 | if agent in ignore_agents: |
| 141 | continue |
| 142 | |
| 143 | try: |
| 144 | service, rest = url.strip('/').split('/', 1) |
| 145 | except ValueError: |
| 146 | # Root calls like "GET /identity" |
| 147 | service = url.strip('/') |
| 148 | rest = '' |
| 149 | |
Dan Smith | fe7cfa6 | 2022-06-23 09:25:22 -0700 | [diff] [blame] | 150 | if not service.isalpha(): |
| 151 | ignored_services.add(service) |
| 152 | continue |
| 153 | |
Dan Smith | 64d6867 | 2022-04-22 07:58:29 -0700 | [diff] [blame] | 154 | method_key = '%s-%s' % (agent, method) |
| 155 | try: |
| 156 | length = int(fields['length']) |
| 157 | except ValueError: |
| 158 | LOG.warning('[%s] Failed to parse length %r from line %r' % ( |
| 159 | logfile, fields['length'], line)) |
| 160 | length = 0 |
| 161 | stats.setdefault(service, {'largest': 0}) |
| 162 | stats[service].setdefault(method_key, 0) |
| 163 | stats[service][method_key] += 1 |
| 164 | stats[service]['largest'] = max(stats[service]['largest'], |
| 165 | length) |
Dan Smith | c2772c2 | 2022-04-08 08:48:49 -0700 | [diff] [blame] | 166 | |
Dan Smith | fe7cfa6 | 2022-06-23 09:25:22 -0700 | [diff] [blame] | 167 | if ignored_services: |
| 168 | LOG.warning('Ignored services: %s' % ','.join( |
| 169 | sorted(ignored_services))) |
| 170 | |
Dan Smith | c2772c2 | 2022-04-08 08:48:49 -0700 | [diff] [blame] | 171 | # Flatten this for ES |
| 172 | return [{'service': service, 'log': os.path.basename(logfile), |
| 173 | **vals} |
| 174 | for service, vals in stats.items()] |
| 175 | |
| 176 | |
| 177 | def get_http_stats(logfiles): |
| 178 | return list(itertools.chain.from_iterable(get_http_stats_for_log(log) |
| 179 | for log in logfiles)) |
| 180 | |
| 181 | |
| 182 | def get_report_info(): |
| 183 | return { |
| 184 | 'timestamp': datetime.datetime.now().isoformat(), |
| 185 | 'hostname': socket.gethostname(), |
Dan Smith | 64d6867 | 2022-04-22 07:58:29 -0700 | [diff] [blame] | 186 | 'version': 2, |
Dan Smith | c2772c2 | 2022-04-08 08:48:49 -0700 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | |
| 190 | if __name__ == '__main__': |
| 191 | process_defaults = ['privsep', 'mysqld', 'erlang', 'etcd'] |
| 192 | parser = argparse.ArgumentParser() |
| 193 | parser.add_argument('--db-user', default='root', |
| 194 | help=('MySQL user for collecting stats ' |
| 195 | '(default: "root")')) |
| 196 | parser.add_argument('--db-pass', default=None, |
| 197 | help='MySQL password for db-user') |
| 198 | parser.add_argument('--db-host', default='localhost', |
| 199 | help='MySQL hostname') |
| 200 | parser.add_argument('--apache-log', action='append', default=[], |
| 201 | help='Collect API call stats from this apache log') |
| 202 | parser.add_argument('--process', action='append', |
| 203 | default=process_defaults, |
| 204 | help=('Include process stats for this cmdline regex ' |
| 205 | '(default is %s)' % ','.join(process_defaults))) |
| 206 | args = parser.parse_args() |
| 207 | |
Dan Smith | 64d6867 | 2022-04-22 07:58:29 -0700 | [diff] [blame] | 208 | logging.basicConfig(level=logging.WARNING) |
| 209 | |
Dan Smith | c2772c2 | 2022-04-08 08:48:49 -0700 | [diff] [blame] | 210 | data = { |
| 211 | 'services': get_services_stats(), |
Dan Smith | 1b601c7 | 2022-04-25 07:47:56 -0700 | [diff] [blame] | 212 | 'db': pymysql and args.db_pass and get_db_stats(args.db_host, |
| 213 | args.db_user, |
| 214 | args.db_pass) or [], |
| 215 | 'processes': psutil and get_processes_stats(args.process) or [], |
Dan Smith | c2772c2 | 2022-04-08 08:48:49 -0700 | [diff] [blame] | 216 | 'api': get_http_stats(args.apache_log), |
| 217 | 'report': get_report_info(), |
| 218 | } |
| 219 | |
| 220 | print(json.dumps(data, indent=2)) |