Joey Armstrong | 44fa7d8 | 2022-11-01 17:46:04 -0400 | [diff] [blame] | 1 | # -*- python -*- |
| 2 | # ----------------------------------------------------------------------- |
Joey Armstrong | 9fadcbe | 2024-01-17 19:00:37 -0500 | [diff] [blame] | 3 | # Copyright 2022-2024 Open Networking Foundation (ONF) and the ONF Contributors |
Girish Gowdra | 80e9c0a | 2022-02-07 16:38:44 -0800 | [diff] [blame] | 4 | # |
| 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 Armstrong | 44fa7d8 | 2022-11-01 17:46:04 -0400 | [diff] [blame] | 16 | # ----------------------------------------------------------------------- |
Girish Gowdra | 80e9c0a | 2022-02-07 16:38:44 -0800 | [diff] [blame] | 17 | |
| 18 | # This tool collects instantaneous memory usage for voltha pods |
| 19 | |
| 20 | |
| 21 | import argparse |
| 22 | import requests |
| 23 | from datetime import datetime |
| 24 | |
| 25 | containers_for_mem_collection = ["voltha", "ofagent", "adapter-open-olt", "adapter-open-onu"] |
| 26 | |
| 27 | |
| 28 | def 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 Windlass | fec6c27 | 2022-03-21 16:28:20 +0000 | [diff] [blame] | 48 | if len(container_cpu) > 0: |
Girish Gowdra | 80e9c0a | 2022-02-07 16:38:44 -0800 | [diff] [blame] | 49 | 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 | |
| 57 | if __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 Windlass | fec6c27 | 2022-03-21 16:28:20 +0000 | [diff] [blame] | 67 | main(args.address, args.output, args.namespace) |
Joey Armstrong | b87031c | 2022-12-17 22:03:24 -0500 | [diff] [blame] | 68 | |
| 69 | # [EOF] |