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

Subscribers

People subscribed via source and target branches