diff options
author | Ismaël Bouya <ismael.bouya@normalesup.org> | 2018-05-13 14:42:51 +0200 |
---|---|---|
committer | Ismaël Bouya <ismael.bouya@normalesup.org> | 2018-05-13 14:49:10 +0200 |
commit | 6b95d3bd38307c1bd320a16368d0a3753601730d (patch) | |
tree | 7a6f4d826dcbbf6b87889855a151cf424f7750e3 /modules/role/templates | |
parent | c8511b2019b44360887d33a9626ec14c81736aee (diff) | |
download | Puppet-6b95d3bd38307c1bd320a16368d0a3753601730d.tar.gz Puppet-6b95d3bd38307c1bd320a16368d0a3753601730d.tar.zst Puppet-6b95d3bd38307c1bd320a16368d0a3753601730d.zip |
Add logger for cryptoportfolio
Diffstat (limited to 'modules/role/templates')
-rw-r--r-- | modules/role/templates/cryptoportfolio/api_logger.py.erb | 52 | ||||
-rw-r--r-- | modules/role/templates/cryptoportfolio/cryptoportfolio-log.service.erb | 11 |
2 files changed, 63 insertions, 0 deletions
diff --git a/modules/role/templates/cryptoportfolio/api_logger.py.erb b/modules/role/templates/cryptoportfolio/api_logger.py.erb new file mode 100644 index 0000000..e5f0b30 --- /dev/null +++ b/modules/role/templates/cryptoportfolio/api_logger.py.erb | |||
@@ -0,0 +1,52 @@ | |||
1 | #!/usr/bin/python | ||
2 | |||
3 | import time | ||
4 | import urllib3 | ||
5 | import json | ||
6 | from systemd import journal | ||
7 | |||
8 | urllib3.disable_warnings() | ||
9 | http = urllib3.PoolManager() | ||
10 | |||
11 | webhook_url = "<%= @slack_logger %>" | ||
12 | webhook_icon = "https://git.immae.eu/releases/logger.png" | ||
13 | |||
14 | def send_to_discord(message): | ||
15 | def send_chunk(chunk): | ||
16 | data = { | ||
17 | "avatar_url": webhook_icon, | ||
18 | "username": "Logger", | ||
19 | "content": "```\n{}\n```".format(chunk), | ||
20 | } | ||
21 | encoded = json.dumps(data).encode('utf-8') | ||
22 | |||
23 | r = http.request("POST", webhook_url, | ||
24 | headers={'Content-Type': 'application/json'}, | ||
25 | body=encoded) | ||
26 | if r.status == 429: | ||
27 | b = json.loads(r.data) | ||
28 | time.sleep(b["retry_after"] / 900) | ||
29 | r = http.request("POST", webhook_url, | ||
30 | headers={'Content-Type': 'application/json'}, | ||
31 | body=encoded) | ||
32 | assert r.status < 299 | ||
33 | |||
34 | chunk = "" | ||
35 | for line in message.split("\n"): | ||
36 | if len(line) + len(chunk) > 2000 - 17: | ||
37 | send_chunk(chunk) | ||
38 | chunk = line | ||
39 | else: | ||
40 | chunk += "\n" + line | ||
41 | send_chunk(chunk) | ||
42 | |||
43 | j = journal.Reader() | ||
44 | j.add_match(_SYSTEMD_UNIT="cryptoportfolio-app.service") | ||
45 | j.seek_tail() | ||
46 | list(j) | ||
47 | |||
48 | while True: | ||
49 | lines = [entry["MESSAGE"] for entry in list(j)] | ||
50 | if len(lines) > 0: | ||
51 | send_to_discord("\n".join(lines)) | ||
52 | time.sleep(3) | ||
diff --git a/modules/role/templates/cryptoportfolio/cryptoportfolio-log.service.erb b/modules/role/templates/cryptoportfolio/cryptoportfolio-log.service.erb new file mode 100644 index 0000000..a5f40ea --- /dev/null +++ b/modules/role/templates/cryptoportfolio/cryptoportfolio-log.service.erb | |||
@@ -0,0 +1,11 @@ | |||
1 | [Unit] | ||
2 | Description=Cryptoportfolio logger | ||
3 | |||
4 | [Service] | ||
5 | Type=simple | ||
6 | |||
7 | UMask=007 | ||
8 | |||
9 | ExecStart=/usr/local/bin/api_logger | ||
10 | |||
11 | Restart=on-failure | ||