blob: 7e8c9c8403139c303e862613638691f512d80a47 [file] [log] [blame]
Joey Armstrong44fa7d82022-11-01 17:46:04 -04001# -*- python -*-
2# -----------------------------------------------------------------------
Joey Armstrong9fadcbe2024-01-17 19:00:37 -05003# Copyright 2022-2024 Open Networking Foundation (ONF) and the ONF Contributors
Girish Gowdra80e9c0a2022-02-07 16:38:44 -08004#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
Joey Armstrong44fa7d82022-11-01 17:46:04 -040016# -----------------------------------------------------------------------
Girish Gowdra80e9c0a2022-02-07 16:38:44 -080017
18# This tool collects instantaneous memory usage for voltha pods
19
20
21import argparse
22import requests
23from datetime import datetime
24
25containers_for_mem_collection = ["voltha", "ofagent", "adapter-open-olt", "adapter-open-onu"]
26
27
28def main(address, out_folder, namespace="default"):
29 """
30 Query Prometheus and generate instantaneous memory consumptions for all the pods under test
31 :param address: string The address of the Prometheus instance to query
32 :param out_folder: string The output folder (where to save the .pdf files)
33 :param namespace: string The pod namespace
34 :return: void
35 """
36 for container in containers_for_mem_collection:
37 container_mem_query = 'container_memory_working_set_bytes{namespace="%s",container="%s"}' % (namespace, container)
38
39 mem_params = {
40 "query": container_mem_query,
41 }
42 print("mem usage query: %s" % mem_params)
43
44 r = requests.get("http://%s/api/v1/query" % address, mem_params)
45 print("Downloading mem info from: %s" % r.url)
46 container_cpu = r.json()["data"]["result"]
47 # print("result for container %s is : " % container, container_cpu)
Hardik Windlassfec6c272022-03-21 16:28:20 +000048 if len(container_cpu) > 0:
Girish Gowdra80e9c0a2022-02-07 16:38:44 -080049 print(container_cpu[0]["value"][1])
50 fp = open(out_folder+"/"+container+".txt", "a")
51 result_in_csv_fmt = "%s,%s\n" % (datetime.now().strftime("%H:%M:%S.%f - %b %d %Y"), container_cpu[0]["value"][1])
52 fp.write(result_in_csv_fmt)
53 else:
54 print("error fetching memory usage for container: %s", container)
55
56
57if __name__ == "__main__":
58 parser = argparse.ArgumentParser(prog="mem_consumption")
59 parser.add_argument("-a", "--address", help="The address of the Prometheus instance we're targeting",
60 default="127.0.0.1:31301")
61 parser.add_argument("-o", "--output", help="Where to output the generated files",
62 default="voltha-pods-mem-consumption")
63 parser.add_argument("-n", "--namespace", help="Kubernetes namespace for collecting metrics",
64 default="voltha")
65
66 args = parser.parse_args()
Hardik Windlassfec6c272022-03-21 16:28:20 +000067 main(args.address, args.output, args.namespace)
Joey Armstrongb87031c2022-12-17 22:03:24 -050068
69# [EOF]