-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfritz.py
executable file
·115 lines (90 loc) · 3.92 KB
/
fritz.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/env python3
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
from future import standard_library
standard_library.install_aliases()
import argparse
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
import seaborn as sns
from monitor import FritzBox
from statistics import FritzStats
# max number of bars to show on a graph (avoids overcrowding)
max_graph_size = 50
def _get_cli_arguments():
parser = argparse.ArgumentParser(description='FritzBox Monitor')
parser.add_argument("action", type=str, choices=["log", "stats"], help="action to perform")
# used by action: log
parser.add_argument('-i', '--ip-address',
nargs='?', default=None, const=None,
dest='address',
help='ip-address of the FritzBox to connect to')
parser.add_argument('-u', '--user',
nargs='?', default=None, const=None,
help='Fritzbox authentication username')
parser.add_argument('-p', '--password',
nargs='?', default=None, const=None,
help='Fritzbox authentication password')
parser.add_argument('--port',
nargs='?', default=None, const=None,
dest='port',
help='port of the FritzBox to connect to')
# used by action: stats
parser.add_argument("--logdir", default="logs", help="folder where logs are stored")
parser.add_argument("--title", default="FRITZ!Box", help="title used on graphs")
parser.add_argument("--output", default="docs", help="folder to store graphs")
parser.add_argument("--prefix", default="fig_fritz", help="prefix added to graph filenames")
args = parser.parse_args()
return args
def main():
"""
Run the tool to either extract a new system log, or build graphs from existing logs.
usage: fritz.py [-h] [-i [ADDRESS]] [-u [USER]] [-p [PASSWORD]]
[--port [PORT]] [--logdir LOGDIR] [--title TITLE]
[--output OUTPUT] [--prefix PREFIX]
{log,stats}
"""
args = _get_cli_arguments()
print(args)
if args.action == "log":
fritz = FritzBox(
address=args.address,
port=args.port,
user=args.user,
password=args.password,
)
log = fritz.get_system_log()
print(log)
elif args.action == "stats":
fritz = FritzStats(args.logdir, args.title)
downtime_df = fritz.get_downtime()
# downtime_df.to_pickle("df.pkl")
if not (downtime_df is None or downtime_df.empty):
sns.set(style="dark")
# pd.set_option('display.max_rows', len(downtime))
# print(df)
# pd.reset_option('display.max_rows')
hour_df = downtime_df.groupby(
[downtime_df.index.year, downtime_df.index.month, downtime_df.index.day,
downtime_df.index.hour]).count()
hour_df = hour_df.tail(max_graph_size) # truncate
hour_df.plot.bar(figsize=(10, 4))
plt.ylabel("# failures")
plt.xlabel("time")
plt.title("%s failures (by hour)" % args.title)
plt.legend()
plt.savefig(args.output + "/" + args.prefix + "_hourly.png", bbox_inches='tight')
day_df = downtime_df.groupby(
[downtime_df.index.year, downtime_df.index.month, downtime_df.index.day]).count()
day_df = day_df.tail(max_graph_size) # truncate
day_df.plot.bar(figsize=(10, 4))
plt.ylabel("# failures")
plt.xlabel("time")
plt.title("%s failures (by day)" % args.title)
plt.legend()
plt.savefig(args.output + "/" + args.prefix + "_daily.png", bbox_inches='tight')
if __name__ == '__main__':
main()