github.com/noironetworks/cilium-net@v1.6.12/contrib/scripts/netperf_reporter.py (about) 1 #!/usr/bin/env python3 2 3 # Copyright 2018 Authors of Cilium 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. 16 17 # ---------------------------------------------------------------------- 18 # This script read the output of netperf results from Kubernetes upstream 19 # project and pushes the results to the given Prometheus server. 20 # Needed variables to run this script: 21 # - PROMETHEUS_URL: metrics gateway URL, example: 22 # https://localhost:8080/metrics/job/some_job 23 # - PROMETHEUS_USR: Prometheus metrics gateway user 24 # - PROMETHEUS_PSW: Prometheus user password 25 26 import csv 27 import logging 28 import os 29 import re 30 import requests 31 import sys 32 33 logging.basicConfig(stream=sys.stdout, level=logging.DEBUG) 34 35 PROMETHEUS_CONFIG = dict( 36 URL="", 37 USR="", 38 PSW="") 39 40 41 def trim_filename(filename): 42 """ 43 trim_filename:delete all the spaces in the given filename and returns and 44 string 45 """ 46 result = [] 47 space_pattern = re.compile(r"\s") 48 49 with open(filename) as fp: 50 for row in fp: 51 result.append("{0}\n".format(space_pattern.sub("", row))) 52 return result 53 54 55 def read_data(filename): 56 """ 57 read the csv result netperf and return an string with an array with 58 key=>value data to push to any metrics system. 59 """ 60 MSS_key = "MSS" 61 result = [] 62 data = trim_filename(filename) 63 64 csv_reader = csv.DictReader(data) 65 for row in csv_reader: 66 key = re.sub(r"[0-9]", "", row.get(MSS_key)) 67 for mtu in row.keys(): 68 if mtu == "" or mtu == MSS_key: 69 continue 70 val = row.get(mtu) 71 if val is None or val == "": 72 continue 73 result.append(('{0}{{mss="{1}"}}'.format(key, mtu), val)) 74 logging.info("Retrieved '{0}' metrics".format(len(result))) 75 return result 76 77 78 def push_to_prometheus(data): 79 """ 80 it receives a tuple with key value storage and push the info to 81 prometheus config server given in the ENV variables 82 """ 83 result = "" 84 for metric, value in data: 85 metric_key = metric.replace(".", "_") 86 result += "{0} {1}\n".format(metric_key, value) 87 logging.info("Metric {0} has the value {1}".format(metric_key, value)) 88 req = requests.post( 89 PROMETHEUS_CONFIG.get("URL"), 90 data=result, 91 auth=(PROMETHEUS_CONFIG.get("USR"), PROMETHEUS_CONFIG.get("PSW"))) 92 if req.status_code == 202: 93 logging.info("Data pushed correctly to prometheus") 94 return True 95 logging.error( 96 "Cannot push data to prometheus:" 97 "err='{0.text}' status_code={0.status_code}".format(req)) 98 return False 99 100 101 if __name__ == "__main__": 102 for key, val in PROMETHEUS_CONFIG.items(): 103 PROMETHEUS_CONFIG[key] = os.environ.get("PROMETHEUS_{0}".format(key)) 104 105 if len(sys.argv) == 1: 106 logging.error("CSV file to retrieved data is not defined.") 107 sys.exit(1) 108 try: 109 data = read_data(sys.argv[1]) 110 except os.FileNotFoundError: 111 logging.error("{0} cannot be oponened".format(sys.argv[0])) 112 sys.exit(1) 113 114 if len(data) == 0: 115 logging.error("No data was retrieved") 116 sys.exit(1) 117 push_to_prometheus(data)