-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathget_bandwidth.py
executable file
·54 lines (45 loc) · 1.28 KB
/
get_bandwidth.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/env python
import logging
import sys
import click
from golosscripts.decorators import common_options, helper
@click.command()
@common_options
@helper
@click.option(
'-t',
'--type',
'type_',
default='market',
type=click.Choice(['market', 'forum', 'custom']),
help='bandwidth type to estimate. Market - transfers, forum - posting ops, custom - custom_json ops',
)
@click.option(
'-w',
'--warning',
default=99,
type=float,
help='exit with 1 error code whether bandwidth exceeded specified percent',
)
@click.option(
'-q',
'--quiet',
default=False,
is_flag=True,
help='do not show any output except errors, just return 0 if has_bandwidth or 1 if not',
)
@click.argument('account')
@click.pass_context
def main(ctx, type_, warning, quiet, account):
"""Estimate current bandwidth usage for an account."""
if quiet:
ctx.log.setLevel(logging.CRITICAL)
bw = ctx.helper.get_bandwidth(account, type_=type_)
ctx.log.info(f'used: {bw.used:.2f} KB, avail: {bw.avail:.2f} KB, usage ratio: {bw.ratio:.2%}')
if bw.ratio * 100 > warning:
ctx.log.warning('bandwidth usage ratio execeeded limit: {:.2%}'.format(bw.ratio))
sys.exit(1)
else:
sys.exit(0)
if __name__ == '__main__':
main()