21121: Basic examples work
[arvados.git] / tools / cluster-activity / arvados_cluster_activity / main.py
1 #!/usr/bin/env python3
2 # Copyright (C) The Arvados Authors. All rights reserved.
3 #
4 # SPDX-License-Identifier: AGPL-3.0
5
6 import argparse
7 import sys
8
9 import arvados
10 import arvados.util
11 import datetime
12 import ciso8601
13 import csv
14 import os
15 from prometheus_api_client.utils import parse_datetime
16 from datetime import timedelta
17
18 from prometheus_api_client import PrometheusConnect, MetricsList, Metric
19
20 def parse_arguments(arguments):
21     arg_parser = argparse.ArgumentParser()
22     arg_parser.add_argument('--start', help='Start date for the report in YYYY-MM-DD format (UTC)')
23     arg_parser.add_argument('--end', help='End date for the report in YYYY-MM-DD format (UTC)')
24     arg_parser.add_argument('--days', type=int, help='Number of days before now() to start the report')
25     args = arg_parser.parse_args(arguments)
26
27     if args.days and (args.start or args.end):
28         arg_parser.print_help()
29         print("Error: either specify --days or both --start and --end")
30         exit(1)
31
32     if not args.days and (not args.start or not args.end):
33         arg_parser.print_help()
34         print("\nError: either specify --days or both --start and --end")
35         exit(1)
36
37     if (args.start and not args.end) or (args.end and not args.start):
38         arg_parser.print_help()
39         print("\nError: no start or end date found, either specify --days or both --start and --end")
40         exit(1)
41
42     if args.days:
43         to = datetime.datetime.utcnow()
44         since = to - datetime.timedelta(days=args.days)
45
46     if args.start:
47         try:
48             since = datetime.datetime.strptime(args.start,"%Y-%m-%d")
49         except:
50             arg_parser.print_help()
51             print("\nError: start date must be in YYYY-MM-DD format")
52             exit(1)
53
54     if args.end:
55         try:
56             to = datetime.datetime.strptime(args.end,"%Y-%m-%d")
57         except:
58             arg_parser.print_help()
59             print("\nError: end date must be in YYYY-MM-DD format")
60             exit(1)
61
62     return args, since, to
63
64 def data_usage(prom, cluster):
65     metric_data = prom.get_current_metric_value(metric_name='arvados_keep_total_bytes', label_config={"cluster": cluster})
66
67     metric_object_list = MetricsList(metric_data)
68     #for item in metric_object_list:
69     #    print(item.metric_name, item.label_config, "\n")
70
71     my_metric_object = metric_object_list[0] # one of the metrics from the list
72
73     #print(my_metric_object.metric_values)
74     value = my_metric_object.metric_values.iloc[0]["y"]
75
76     for scale in ["KiB", "MiB", "GiB", "TiB", "PiB"]:
77         value = value / 1024
78         if value < 1024:
79             print(value, scale)
80             break
81
82
83 def container_usage(prom, cluster):
84
85     start_time = parse_datetime("7d")
86     end_time = parse_datetime("now")
87     chunk_size = timedelta(days=1)
88
89     metric_data = prom.get_metric_range_data(metric_name='arvados_dispatchcloud_containers_running',
90                                              label_config={"cluster": cluster},
91                                              start_time=start_time,
92                                              end_time=end_time,
93                                              chunk_size=chunk_size,
94                                              )
95
96     metric_object_list = MetricsList(metric_data)
97     my_metric_object = metric_object_list[0] # one of the metrics from the list
98
99     s = my_metric_object.metric_values.sum(numeric_only=True)
100     print(s["y"] / 4, "container minutes")
101
102
103 def main(arguments=None):
104     if arguments is None:
105         arguments = sys.argv[1:]
106
107     #args, since, to = parse_arguments(arguments)
108
109     #arv = arvados.api()
110
111     prom_host = os.environ["PROMETHEUS_HOST"]
112     prom_token = os.environ["PROMETHEUS_APIKEY"]
113
114     prom = PrometheusConnect(url=prom_host, headers={"Authorization": "Bearer "+prom_token})
115
116     for cluster in ("tordo", "pirca", "jutro"):
117         print(cluster)
118         data_usage(prom, cluster)
119         container_usage(prom, cluster)
120         print()
121
122 if __name__ == "__main__":
123     main()