Merge ~ballot/content-cache-charm/+git/content-cache-charm:request_per_ip_action into content-cache-charm:master

Proposed by Benjamin Allot
Status: Work in progress
Proposed branch: ~ballot/content-cache-charm/+git/content-cache-charm:request_per_ip_action
Merge into: content-cache-charm:master
Diff against target: 90 lines (+78/-0)
2 files modified
actions.yaml (+11/-0)
actions/get-requests-per-ip (+67/-0)
Reviewer Review Type Date Requested Status
Content Cache Charmers Pending
Review via email: mp+399323@code.launchpad.net
To post a comment you must log in.

Unmerged commits

f5e98b3... by Benjamin Allot

WIP

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/actions.yaml b/actions.yaml
2new file mode 100644
3index 0000000..6dcdfc3
4--- /dev/null
5+++ b/actions.yaml
6@@ -0,0 +1,11 @@
7+get-requests-per-ip:
8+ description: Get a quick view of the number of requests per ip on the last 15 minutes
9+ params:
10+ minutes:
11+ type: int
12+ description: Look in the last X minutes to gather the requests
13+ range:
14+ type: string
15+ description: |
16+ Pass a string in the regexp format to express a time range.
17+ i.e: "16:(3[5-9]|4[0-3]):" would look at all the logs between 16:35 and 16:43
18diff --git a/actions/get-requests-per-ip b/actions/get-requests-per-ip
19new file mode 100644
20index 0000000..9e9e758
21--- /dev/null
22+++ b/actions/get-requests-per-ip
23@@ -0,0 +1,67 @@
24+#!/usr/bin/env python3
25+
26+
27+import subprocess
28+import json
29+
30+
31+from charmhelpers.core import hookenv
32+
33+
34+LAST_MINUTES = 20
35+TOP_IPS = 20
36+
37+
38+def extract_logs_from_last_minutes(minutes: int) -> str:
39+ """Extract the logs from the last `minutes` minutes.
40+
41+ Return the logs written in the last minutes.
42+
43+ :param minutes: Number of last minutes used to extract relevant logs
44+ """
45+ # TODO: use subprocess or pure python. TDB
46+ pass
47+
48+
49+def extract_logs_from_time_range(time_range: str) -> str:
50+ """Extract the logs for the selected time_range.
51+
52+ This time range is converted in the right format depending on the haproxy
53+ or nginx logs and return the associated logs.
54+
55+ :param time_range: String in the regexp form to filter the log lines
56+ """
57+ # TODO: use subprocess or pure python. TDB
58+
59+
60+def extract_top_ips(logs_data: str, num: int) -> str:
61+ """Extract the number of requests per IP.
62+
63+ Return the `num` IPs with the greatest number of requests
64+ in the provided logs.
65+ """
66+ # TODO: probably use pure python here.
67+
68+
69+def main():
70+ """Main entry point."""
71+
72+ action_data = hookenv.function_get()
73+ # Set the default to LAST_MINUTES if no arguments
74+ if "minutes" not in action_data and "range" not in action_data:
75+ action_data["minutes"] = 20
76+
77+ # if we have range, we should ignore last_minutes
78+ elif "range" in action_data:
79+ logs_data = extract_logs_from_time_range(action_data["range"])
80+ else:
81+ logs_data = extract_logs_from_last_minutes(action_data["minutes"])
82+
83+ print(extract_top_ips(logs_data, TOP_IPS))
84+
85+
86+if __name__ == "__main__":
87+ try:
88+ main()
89+ finally:
90+ print("End of action")

Subscribers

People subscribed via source and target branches