2 # Copyright (C) The Arvados Authors. All rights reserved.
4 # SPDX-License-Identifier: AGPL-3.0
15 from prometheus_api_client.utils import parse_datetime
16 from datetime import timedelta
18 from prometheus_api_client import PrometheusConnect, MetricsList, Metric
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)
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")
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")
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")
43 to = datetime.datetime.utcnow()
44 since = to - datetime.timedelta(days=args.days)
48 since = datetime.datetime.strptime(args.start,"%Y-%m-%d")
50 arg_parser.print_help()
51 print("\nError: start date must be in YYYY-MM-DD format")
56 to = datetime.datetime.strptime(args.end,"%Y-%m-%d")
58 arg_parser.print_help()
59 print("\nError: end date must be in YYYY-MM-DD format")
62 return args, since, to
64 def data_usage(prom, cluster):
65 metric_data = prom.get_current_metric_value(metric_name='arvados_keep_total_bytes', label_config={"cluster": cluster})
67 metric_object_list = MetricsList(metric_data)
68 #for item in metric_object_list:
69 # print(item.metric_name, item.label_config, "\n")
71 my_metric_object = metric_object_list[0] # one of the metrics from the list
73 #print(my_metric_object.metric_values)
74 value = my_metric_object.metric_values.iloc[0]["y"]
76 for scale in ["KiB", "MiB", "GiB", "TiB", "PiB"]:
83 def container_usage(prom, cluster):
85 start_time = parse_datetime("7d")
86 end_time = parse_datetime("now")
87 chunk_size = timedelta(days=1)
89 metric_data = prom.get_metric_range_data(metric_name='arvados_dispatchcloud_containers_running',
90 label_config={"cluster": cluster},
91 start_time=start_time,
93 chunk_size=chunk_size,
96 metric_object_list = MetricsList(metric_data)
97 my_metric_object = metric_object_list[0] # one of the metrics from the list
99 s = my_metric_object.metric_values.sum(numeric_only=True)
100 print(s["y"] / 4, "container minutes")
103 def main(arguments=None):
104 if arguments is None:
105 arguments = sys.argv[1:]
107 #args, since, to = parse_arguments(arguments)
111 prom_host = os.environ["PROMETHEUS_HOST"]
112 prom_token = os.environ["PROMETHEUS_APIKEY"]
114 prom = PrometheusConnect(url=prom_host, headers={"Authorization": "Bearer "+prom_token})
116 for cluster in ("tordo", "pirca", "jutro"):
118 data_usage(prom, cluster)
119 container_usage(prom, cluster)
122 if __name__ == "__main__":