Skip to content

Commit 2426f1d

Browse files
committed
initial commit
1 parent 27af232 commit 2426f1d

File tree

4 files changed

+192
-0
lines changed

4 files changed

+192
-0
lines changed

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024 andrii29
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# MongoDB Slow Log Analyzer
2+
3+
MongoDB Slow Log Analyzer is a Python script that processes MongoDB slow log files, extracts relevant information, and print it. For more deep analysis data is stores in an SQLite database.
4+
5+
## Requirements
6+
- MongoDB version 4.4 or higher
7+
- Python 3
8+
9+
## Installation
10+
11+
To install the required dependencies, run the following command:
12+
13+
```bash
14+
pip3 install -r requirements.txt
15+
```
16+
17+
## Usage
18+
19+
```bash
20+
python3 mongodb-slow-log-analyzer.py [log] [--db DB_PATH] [--limit ROW_LIMIT] [--char-limit CHAR_LIMIT] [--count COUNT] [--sql]
21+
22+
python3 mongodb-slow-log-analyzer.py --db ./slow-log.sql --limit 10 --char-limit 100 --count 5 /var/log/mongod.log
23+
```
24+
25+
## Check sql command
26+
```bash
27+
python3 mongodb-slow-log-analyzer.py --sql
28+
python3 mongodb-slow-log-analyzer.py --sql --limit 30 --char-limit 200 --count 100
29+
```
30+
31+
## Enable slow log
32+
```bash
33+
db.setProfilingLevel(0, { slowms: 100 })
34+
```
35+
36+
## Why Use This Script?
37+
MongoDB's built-in profiler offers a range of options for slow log analysis, but it comes with its challenges. Setting a profiling level affects server performance, and the default profiler collection, system.profile, has a 1MB size limit. To increase the size, the collection needs to be manually deleted. Handling this on replica sets adds complexity, as commands on the primary node for system.profile do not automatically propagate to replicas.
38+
39+
## Benefits of This Script:
40+
This script provides a straightforward solution for estimating slow queries without affecting server performance. It eliminates the need for manual tuning of the system.profile collection and simplifies the analysis process on replica sets.

mongodb-slow-log-analyzer.py

+130
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
#!/usr/bin/env python3
2+
3+
import argparse
4+
import os
5+
import json
6+
import sqlite3
7+
from tabulate import tabulate
8+
9+
def parse_log_line(line):
10+
try:
11+
return json.loads(line)
12+
except json.JSONDecodeError:
13+
return None
14+
15+
def extract_query_data(data):
16+
if data["msg"] == "Slow query" and "attr" in data and "queryHash" in data["attr"] and data["attr"]["queryHash"]:
17+
return {
18+
"hash": data["attr"]["queryHash"],
19+
"durationMillis": data["attr"]["durationMillis"],
20+
"ns": data["attr"]["ns"],
21+
"planSummary": data["attr"]["planSummary"],
22+
"command": data["attr"]["command"] if "command" in data["attr"] else None
23+
}
24+
return None
25+
26+
def create_or_update_result(result, query_data):
27+
hash_key = query_data["hash"]
28+
result["durationMillis_" + hash_key] = result.get("durationMillis_" + hash_key, 0) + query_data["durationMillis"]
29+
result["count_" + hash_key] = result.get("count_" + hash_key, 0) + 1
30+
result.setdefault("ns_" + hash_key, query_data["ns"])
31+
result.setdefault("planSummary_" + hash_key, []).append(query_data["planSummary"])
32+
result["avgDurationMillis_" + hash_key] = result["durationMillis_" + hash_key] / result["count_" + hash_key] if result["durationMillis_" + hash_key] and result["count_" + hash_key] > 0 else 0
33+
if query_data["command"] and "command_" + hash_key not in result:
34+
result["command_" + hash_key] = query_data["command"]
35+
36+
def process_slow_log(data, db, limit, char_limit, count, query_condition):
37+
hashes = set()
38+
result = {}
39+
40+
for line in data:
41+
parsed_data = parse_log_line(line)
42+
if parsed_data:
43+
query_data = extract_query_data(parsed_data)
44+
if query_data:
45+
hash_key = query_data["hash"]
46+
if hash_key not in hashes:
47+
hashes.add(hash_key)
48+
create_or_update_result(result, query_data)
49+
50+
if os.path.exists(db):
51+
os.remove(db)
52+
print(f"Old database file {db} has been dropped")
53+
54+
connection = sqlite3.connect(db)
55+
cursor = connection.cursor()
56+
57+
cursor.execute('''
58+
CREATE TABLE IF NOT EXISTS results (
59+
hash TEXT PRIMARY KEY,
60+
durationMillis INTEGER,
61+
count INTEGER,
62+
avgDurationMillis REAL,
63+
ns STRING,
64+
planSummary String,
65+
command STRING
66+
)
67+
''')
68+
69+
for hash_key in hashes:
70+
cursor.execute('''
71+
INSERT OR REPLACE INTO results (hash, durationMillis, count, avgDurationMillis, ns, planSummary, command)
72+
VALUES (?, ?, ?, ?, ?, ?, ?)
73+
''', (hash_key, result.get("durationMillis_" + hash_key, 0), result.get("count_" + hash_key, 0), result.get("avgDurationMillis_" + hash_key, 0), result.get("ns_" + hash_key, ''),
74+
str(result.get("planSummary_" + hash_key, '')), str(result.get("command_" + hash_key, ''))))
75+
connection.commit()
76+
77+
cursor.execute(f"PRAGMA table_info(results);")
78+
columns = cursor.fetchall()
79+
column_names = [column_info[1] for column_info in columns]
80+
81+
cursor.execute(f'''
82+
SELECT hash, durationMillis, count, avgDurationMillis, ns, SUBSTR(planSummary, 1, {char_limit}),
83+
SUBSTR(command, 1, {char_limit})
84+
FROM results WHERE count >= {count}{query_condition}
85+
ORDER BY avgDurationMillis DESC LIMIT {limit};
86+
''')
87+
88+
rows = cursor.fetchall()
89+
table_data = [column_names] + list(rows)
90+
91+
print(tabulate(table_data, headers="firstrow", tablefmt="fancy_grid"))
92+
connection.close()
93+
94+
def print_sql_info(db, limit, char_limit, count, query_condition):
95+
print(f'sqlite3 {db}')
96+
print('.mode column')
97+
print(f"SELECT hash, durationMillis, count, avgDurationMillis, ns, SUBSTR(planSummary, 1, {char_limit}), "
98+
f"SUBSTR(command, 1, {char_limit}) FROM results WHERE count >= {count}{query_condition} ORDER BY avgDurationMillis DESC LIMIT {limit};")
99+
print("SELECT hash, durationMillis, count, avgDurationMillis, ns, planSummary, command FROM results ORDER BY avgDurationMillis DESC;")
100+
exit()
101+
102+
def main():
103+
parser = argparse.ArgumentParser(description="Process MongoDB slow log file")
104+
105+
parser.add_argument("log", nargs="?", default="/var/log/mongod.log", help="Path to the mongodb log file (default: /var/log/mongod.log)")
106+
parser.add_argument("--db", default="./mongo_slow_logs.sql", help="Path to the SQLite database file (default: ./mongodb-slow-log.sql)")
107+
parser.add_argument("--limit", default=10, type=int, help="Limit the number of rows in SQL output (default: 10)")
108+
parser.add_argument("--char-limit", default=100, type=int, help="Limit the number of characters in SQL strings output (default: 100)")
109+
parser.add_argument("--count", default=1, type=int, help="Filter queries that appear less than this count in the log (default: 1)")
110+
parser.add_argument("--collscan", action="store_true", help="Filter queries with COLLSCAN in the results (default: no filters)")
111+
parser.add_argument("--sql", action="store_true", help="Print useful SQL information and exit")
112+
113+
args = parser.parse_args()
114+
115+
query_condition = ' AND planSummary LIKE \'%COLLSCAN%\'' if args.collscan else ''
116+
117+
if args.sql:
118+
print_sql_info(args.db, args.limit, args.char_limit, args.count, query_condition)
119+
120+
try:
121+
with open(args.log, "r") as log_file:
122+
process_slow_log(log_file, args.db, args.limit, args.char_limit, args.count, query_condition)
123+
124+
except FileNotFoundError:
125+
print(f"The file '{args.log}' does not exist.")
126+
except Exception as e:
127+
print(f"An error occurred: {e}")
128+
129+
if __name__ == "__main__":
130+
main()

requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
tabulate

0 commit comments

Comments
 (0)