1 | 1 |
deleted file mode 100755 |
... | ... |
@@ -1,77 +0,0 @@ |
1 |
-#!/usr/bin/env python3 |
|
2 |
-# -*- coding: utf-8 -*- |
|
3 |
-# Copyright © 2017-2019 Xavier G. <xavier.boobank@kindwolf.org> |
|
4 |
-# This work is free. You can redistribute it and/or modify it under the |
|
5 |
-# terms of the Do What The Fuck You Want To Public License, Version 2, |
|
6 |
-# as published by Sam Hocevar. See the COPYING file for more details. |
|
7 |
-""" |
|
8 |
-boobank-graphite takes a JSON file, as created by boobank-cacher when running |
|
9 |
-and caching a "boobank list" command, and sends it to a Graphite instance. |
|
10 |
-""" |
|
11 |
- |
|
12 |
-import sys |
|
13 |
-import json |
|
14 |
-import time |
|
15 |
-import socket |
|
16 |
-import argparse |
|
17 |
-from boobank_utils import DEFAULT_CURRENCY |
|
18 |
- |
|
19 |
- |
|
20 |
-def main(): |
|
21 |
- """ |
|
22 |
- Main function. |
|
23 |
- """ |
|
24 |
- args = parse_args() |
|
25 |
- accounts = json.load(args.json_file) |
|
26 |
- timestamp = time.time() |
|
27 |
- graphite_socket = graphite_connect(args.host, args.port) |
|
28 |
- for account in accounts.get('data', []): |
|
29 |
- send_account(graphite_socket, account, timestamp) |
|
30 |
- graphite_socket.close() |
|
31 |
- |
|
32 |
-def parse_args(): |
|
33 |
- """ |
|
34 |
- Parse command-line arguments. |
|
35 |
- """ |
|
36 |
- description = 'Send the contents of a boobank-cacher JSON file to Graphite.' |
|
37 |
- args = argparse.ArgumentParser(description=description) |
|
38 |
- args.add_argument('-gh', '--graphite-host', dest='host', default='::1', |
|
39 |
- help='Graphite host; defaults to ::1.') |
|
40 |
- args.add_argument('-gp', '--graphite-port', dest='port', default='2003', type=int, |
|
41 |
- help='Graphite port; defaults to 2003.') |
|
42 |
- args.add_argument('json_file', nargs='?', type=argparse.FileType('r'), default=sys.stdin, |
|
43 |
- help='JSON file to send to Graphite; defaults to "-" (standard input).') |
|
44 |
- return args.parse_args() |
|
45 |
- |
|
46 |
-def graphite_connect(host, port): |
|
47 |
- """ |
|
48 |
- Connect to the Graphite instance listening on host and port. |
|
49 |
- """ |
|
50 |
- addr_infos = socket.getaddrinfo(host, port, 0, 0, socket.IPPROTO_TCP) |
|
51 |
- _, _, _, _, sockaddr = addr_infos[0] |
|
52 |
- graphite_socket = socket.socket(socket.AF_INET6, socket.SOCK_STREAM, 0) |
|
53 |
- graphite_socket.connect(sockaddr) |
|
54 |
- return graphite_socket |
|
55 |
- |
|
56 |
-def get_path(account): |
|
57 |
- """ |
|
58 |
- Return the path to send to Graphite for the given bank account. |
|
59 |
- The following scheme is used: |
|
60 |
- bank.<bank id>.<account id>-balance-<currency> |
|
61 |
- """ |
|
62 |
- (account_id, bank) = account['id'].split('@') |
|
63 |
- currency = account.get('currency', DEFAULT_CURRENCY).lower() |
|
64 |
- return 'bank.%s.%s-balance-%s' % (bank, account_id, currency) |
|
65 |
- |
|
66 |
-def send_account(graphite_socket, account, timestamp): |
|
67 |
- """ |
|
68 |
- Send the balance of the given bank account to Graphite over the given TCP |
|
69 |
- socket. |
|
70 |
- """ |
|
71 |
- pattern = '%s %.2f %d\n' |
|
72 |
- values = (get_path(account), float(account['balance']), int(timestamp)) |
|
73 |
- line = pattern % values |
|
74 |
- graphite_socket.send(line.encode('utf-8')) |
|
75 |
- |
|
76 |
-if __name__ == '__main__': |
|
77 |
- main() |
... | ... |
@@ -1,4 +1,4 @@ |
1 |
-#!/usr/bin/env python |
|
1 |
+#!/usr/bin/env python3 |
|
2 | 2 |
# -*- coding: utf-8 -*- |
3 | 3 |
# Copyright © 2017-2019 Xavier G. <xavier.boobank@kindwolf.org> |
4 | 4 |
# This work is free. You can redistribute it and/or modify it under the |
... | ... |
@@ -71,7 +71,7 @@ def send_account(graphite_socket, account, timestamp): |
71 | 71 |
pattern = '%s %.2f %d\n' |
72 | 72 |
values = (get_path(account), float(account['balance']), int(timestamp)) |
73 | 73 |
line = pattern % values |
74 |
- graphite_socket.send(line) |
|
74 |
+ graphite_socket.send(line.encode('utf-8')) |
|
75 | 75 |
|
76 | 76 |
if __name__ == '__main__': |
77 | 77 |
main() |
... | ... |
@@ -69,7 +69,7 @@ def send_account(graphite_socket, account, timestamp): |
69 | 69 |
socket. |
70 | 70 |
""" |
71 | 71 |
pattern = '%s %.2f %d\n' |
72 |
- values = (get_path(account), account['balance'], int(timestamp)) |
|
72 |
+ values = (get_path(account), float(account['balance']), int(timestamp)) |
|
73 | 73 |
line = pattern % values |
74 | 74 |
graphite_socket.send(line) |
75 | 75 |
|
... | ... |
@@ -1,6 +1,6 @@ |
1 | 1 |
#!/usr/bin/env python |
2 | 2 |
# -*- coding: utf-8 -*- |
3 |
-# Copyright © 2017 Xavier G. <xavier.boobank@kindwolf.org> |
|
3 |
+# Copyright © 2017-2019 Xavier G. <xavier.boobank@kindwolf.org> |
|
4 | 4 |
# This work is free. You can redistribute it and/or modify it under the |
5 | 5 |
# terms of the Do What The Fuck You Want To Public License, Version 2, |
6 | 6 |
# as published by Sam Hocevar. See the COPYING file for more details. |
... | ... |
@@ -1,5 +1,9 @@ |
1 | 1 |
#!/usr/bin/env python |
2 | 2 |
# -*- coding: utf-8 -*- |
3 |
+# Copyright © 2017 Xavier G. <xavier.boobank@kindwolf.org> |
|
4 |
+# This work is free. You can redistribute it and/or modify it under the |
|
5 |
+# terms of the Do What The Fuck You Want To Public License, Version 2, |
|
6 |
+# as published by Sam Hocevar. See the COPYING file for more details. |
|
3 | 7 |
""" |
4 | 8 |
boobank-graphite takes a JSON file, as created by boobank-cacher when running |
5 | 9 |
and caching a "boobank list" command, and sends it to a Graphite instance. |
... | ... |
@@ -1,49 +1,73 @@ |
1 | 1 |
#!/usr/bin/env python |
2 | 2 |
# -*- coding: utf-8 -*- |
3 |
+""" |
|
4 |
+boobank-graphite takes a JSON file, as created by boobank-cacher when running |
|
5 |
+and caching a "boobank list" command, and sends it to a Graphite instance. |
|
6 |
+""" |
|
3 | 7 |
|
4 | 8 |
import sys |
5 | 9 |
import json |
6 | 10 |
import time |
7 | 11 |
import socket |
8 | 12 |
import argparse |
13 |
+from boobank_utils import DEFAULT_CURRENCY |
|
9 | 14 |
|
10 | 15 |
|
11 | 16 |
def main(): |
17 |
+ """ |
|
18 |
+ Main function. |
|
19 |
+ """ |
|
12 | 20 |
args = parse_args() |
13 |
- accounts = get_accounts(args.json_file) |
|
21 |
+ accounts = json.load(args.json_file) |
|
14 | 22 |
timestamp = time.time() |
15 |
- socket = graphite_connect(args.host, args.port) |
|
23 |
+ graphite_socket = graphite_connect(args.host, args.port) |
|
16 | 24 |
for account in accounts.get('data', []): |
17 |
- send_account(socket, account, timestamp) |
|
18 |
- socket.close() |
|
25 |
+ send_account(graphite_socket, account, timestamp) |
|
26 |
+ graphite_socket.close() |
|
19 | 27 |
|
20 | 28 |
def parse_args(): |
21 |
- args_parser = argparse.ArgumentParser(description='Send the contents of a boobank-cacher JSON file to Graphite.') |
|
22 |
- args_parser.add_argument('-gh', '--graphite-host', dest='host', default='::1', help='Graphite host; defaults to ::1.') |
|
23 |
- args_parser.add_argument('-gp', '--graphite-port', dest='port', default='2003', type=int, help='Graphite port; defaults to 2003.') |
|
24 |
- args_parser.add_argument('json_file', nargs='?', type=argparse.FileType('r'), default=sys.stdin, help='JSON file to send to Graphite; defaults to "-" (standard input).') |
|
25 |
- return args_parser.parse_args() |
|
29 |
+ """ |
|
30 |
+ Parse command-line arguments. |
|
31 |
+ """ |
|
32 |
+ description = 'Send the contents of a boobank-cacher JSON file to Graphite.' |
|
33 |
+ args = argparse.ArgumentParser(description=description) |
|
34 |
+ args.add_argument('-gh', '--graphite-host', dest='host', default='::1', |
|
35 |
+ help='Graphite host; defaults to ::1.') |
|
36 |
+ args.add_argument('-gp', '--graphite-port', dest='port', default='2003', type=int, |
|
37 |
+ help='Graphite port; defaults to 2003.') |
|
38 |
+ args.add_argument('json_file', nargs='?', type=argparse.FileType('r'), default=sys.stdin, |
|
39 |
+ help='JSON file to send to Graphite; defaults to "-" (standard input).') |
|
40 |
+ return args.parse_args() |
|
26 | 41 |
|
27 | 42 |
def graphite_connect(host, port): |
43 |
+ """ |
|
44 |
+ Connect to the Graphite instance listening on host and port. |
|
45 |
+ """ |
|
28 | 46 |
addr_infos = socket.getaddrinfo(host, port, 0, 0, socket.IPPROTO_TCP) |
29 | 47 |
_, _, _, _, sockaddr = addr_infos[0] |
30 | 48 |
graphite_socket = socket.socket(socket.AF_INET6, socket.SOCK_STREAM, 0) |
31 | 49 |
graphite_socket.connect(sockaddr) |
32 | 50 |
return graphite_socket |
33 | 51 |
|
34 |
-def get_accounts(filedesc): |
|
35 |
- return json.load(filedesc) |
|
36 |
- |
|
37 | 52 |
def get_path(account): |
38 |
- (id, bank) = account['id'].split('@') |
|
39 |
- currency = account.get('currency', 'EUR').lower() |
|
40 |
- return 'bank.%s.%s-balance-%s' % (bank, id, currency) |
|
53 |
+ """ |
|
54 |
+ Return the path to send to Graphite for the given bank account. |
|
55 |
+ The following scheme is used: |
|
56 |
+ bank.<bank id>.<account id>-balance-<currency> |
|
57 |
+ """ |
|
58 |
+ (account_id, bank) = account['id'].split('@') |
|
59 |
+ currency = account.get('currency', DEFAULT_CURRENCY).lower() |
|
60 |
+ return 'bank.%s.%s-balance-%s' % (bank, account_id, currency) |
|
41 | 61 |
|
42 |
-def send_account(socket, account, timestamp): |
|
62 |
+def send_account(graphite_socket, account, timestamp): |
|
63 |
+ """ |
|
64 |
+ Send the balance of the given bank account to Graphite over the given TCP |
|
65 |
+ socket. |
|
66 |
+ """ |
|
43 | 67 |
pattern = '%s %.2f %d\n' |
44 | 68 |
values = (get_path(account), account['balance'], int(timestamp)) |
45 | 69 |
line = pattern % values |
46 |
- socket.send(line) |
|
70 |
+ graphite_socket.send(line) |
|
47 | 71 |
|
48 | 72 |
if __name__ == '__main__': |
49 | 73 |
main() |
... | ... |
@@ -5,38 +5,34 @@ import sys |
5 | 5 |
import json |
6 | 6 |
import time |
7 | 7 |
import socket |
8 |
+import argparse |
|
8 | 9 |
|
9 | 10 |
|
10 | 11 |
def main(): |
11 |
- accounts = get_accounts() |
|
12 |
+ args = parse_args() |
|
13 |
+ accounts = get_accounts(args.json_file) |
|
12 | 14 |
timestamp = time.time() |
13 |
- socket = graphite_connect() |
|
15 |
+ socket = graphite_connect(args.host, args.port) |
|
14 | 16 |
for account in accounts.get('data', []): |
15 | 17 |
send_account(socket, account, timestamp) |
16 | 18 |
socket.close() |
17 | 19 |
|
18 |
-def graphite_connect(): |
|
19 |
- (host, port) = ('::1', 2003) |
|
20 |
- if len(sys.argv) == 4: |
|
21 |
- (host, port) = (sys.argv[2], int(sys.argv[3])) |
|
22 |
- elif len(sys.argv) == 3: |
|
23 |
- host = sys.argv[2] |
|
20 |
+def parse_args(): |
|
21 |
+ args_parser = argparse.ArgumentParser(description='Send the contents of a boobank-cacher JSON file to Graphite.') |
|
22 |
+ args_parser.add_argument('-gh', '--graphite-host', dest='host', default='::1', help='Graphite host; defaults to ::1.') |
|
23 |
+ args_parser.add_argument('-gp', '--graphite-port', dest='port', default='2003', type=int, help='Graphite port; defaults to 2003.') |
|
24 |
+ args_parser.add_argument('json_file', nargs='?', type=argparse.FileType('r'), default=sys.stdin, help='JSON file to send to Graphite; defaults to "-" (standard input).') |
|
25 |
+ return args_parser.parse_args() |
|
26 |
+ |
|
27 |
+def graphite_connect(host, port): |
|
24 | 28 |
addr_infos = socket.getaddrinfo(host, port, 0, 0, socket.IPPROTO_TCP) |
25 | 29 |
_, _, _, _, sockaddr = addr_infos[0] |
26 | 30 |
graphite_socket = socket.socket(socket.AF_INET6, socket.SOCK_STREAM, 0) |
27 | 31 |
graphite_socket.connect(sockaddr) |
28 | 32 |
return graphite_socket |
29 | 33 |
|
30 |
-def get_accounts(): |
|
31 |
- try: |
|
32 |
- accounts_json_path = sys.argv[1] |
|
33 |
- except IndexError: |
|
34 |
- accounts_json_path = '-' |
|
35 |
- if accounts_json_path == '-': |
|
36 |
- return json.load(sys.stdin) |
|
37 |
- else: |
|
38 |
- with open(accounts_json_path, 'r') as filedesc: |
|
39 |
- return json.load(filedesc) |
|
34 |
+def get_accounts(filedesc): |
|
35 |
+ return json.load(filedesc) |
|
40 | 36 |
|
41 | 37 |
def get_path(account): |
42 | 38 |
(id, bank) = account['id'].split('@') |
... | ... |
@@ -16,14 +16,15 @@ def main(): |
16 | 16 |
socket.close() |
17 | 17 |
|
18 | 18 |
def graphite_connect(): |
19 |
+ (host, port) = ('::1', 2003) |
|
19 | 20 |
if len(sys.argv) == 4: |
20 |
- socket_args = (sys.argv[2], int(sys.argv[3])) |
|
21 |
+ (host, port) = (sys.argv[2], int(sys.argv[3])) |
|
21 | 22 |
elif len(sys.argv) == 3: |
22 |
- socket_args = (sys.argv[2], 2003) |
|
23 |
- else: |
|
24 |
- socket_args = ('::1', 2003) |
|
25 |
- graphite_socket = socket.socket() |
|
26 |
- graphite_socket.connect(socket_args) |
|
23 |
+ host = sys.argv[2] |
|
24 |
+ addr_infos = socket.getaddrinfo(host, port, 0, 0, socket.IPPROTO_TCP) |
|
25 |
+ _, _, _, _, sockaddr = addr_infos[0] |
|
26 |
+ graphite_socket = socket.socket(socket.AF_INET6, socket.SOCK_STREAM, 0) |
|
27 |
+ graphite_socket.connect(sockaddr) |
|
27 | 28 |
return graphite_socket |
28 | 29 |
|
29 | 30 |
def get_accounts(): |
1 | 1 |
new file mode 100755 |
... | ... |
@@ -0,0 +1,52 @@ |
1 |
+#!/usr/bin/env python |
|
2 |
+# -*- coding: utf-8 -*- |
|
3 |
+ |
|
4 |
+import sys |
|
5 |
+import json |
|
6 |
+import time |
|
7 |
+import socket |
|
8 |
+ |
|
9 |
+ |
|
10 |
+def main(): |
|
11 |
+ accounts = get_accounts() |
|
12 |
+ timestamp = time.time() |
|
13 |
+ socket = graphite_connect() |
|
14 |
+ for account in accounts.get('data', []): |
|
15 |
+ send_account(socket, account, timestamp) |
|
16 |
+ socket.close() |
|
17 |
+ |
|
18 |
+def graphite_connect(): |
|
19 |
+ if len(sys.argv) == 4: |
|
20 |
+ socket_args = (sys.argv[2], int(sys.argv[3])) |
|
21 |
+ elif len(sys.argv) == 3: |
|
22 |
+ socket_args = (sys.argv[2], 2003) |
|
23 |
+ else: |
|
24 |
+ socket_args = ('::1', 2003) |
|
25 |
+ graphite_socket = socket.socket() |
|
26 |
+ graphite_socket.connect(socket_args) |
|
27 |
+ return graphite_socket |
|
28 |
+ |
|
29 |
+def get_accounts(): |
|
30 |
+ try: |
|
31 |
+ accounts_json_path = sys.argv[1] |
|
32 |
+ except IndexError: |
|
33 |
+ accounts_json_path = '-' |
|
34 |
+ if accounts_json_path == '-': |
|
35 |
+ return json.load(sys.stdin) |
|
36 |
+ else: |
|
37 |
+ with open(accounts_json_path, 'r') as filedesc: |
|
38 |
+ return json.load(filedesc) |
|
39 |
+ |
|
40 |
+def get_path(account): |
|
41 |
+ (id, bank) = account['id'].split('@') |
|
42 |
+ currency = account.get('currency', 'EUR').lower() |
|
43 |
+ return 'bank.%s.%s-balance-%s' % (bank, id, currency) |
|
44 |
+ |
|
45 |
+def send_account(socket, account, timestamp): |
|
46 |
+ pattern = '%s %.2f %d\n' |
|
47 |
+ values = (get_path(account), account['balance'], int(timestamp)) |
|
48 |
+ line = pattern % values |
|
49 |
+ socket.send(line) |
|
50 |
+ |
|
51 |
+if __name__ == '__main__': |
|
52 |
+ main() |