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