Matteo Scandolo | eb0d11c | 2017-08-08 13:05:26 -0700 | [diff] [blame^] | 1 | |
| 2 | # Copyright 2017-present Open Networking Foundation |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | |
Srikanth Vavilapalli | f6eda8f | 2016-09-09 13:43:51 -0400 | [diff] [blame] | 17 | import plotly.plotly as py |
| 18 | import plotly.graph_objs as go |
| 19 | import time |
| 20 | import requests |
| 21 | #import ceilometerapi |
| 22 | import ast |
| 23 | |
| 24 | PLOTLY_USERNAME = 'myusername' |
| 25 | PLOTLY_API_KEY = 'myapikey' |
| 26 | XOS_IP = '1.2.3.4' |
| 27 | XOS_PORT = '9999' |
| 28 | |
| 29 | class LAGPlot(): |
| 30 | def __init__(self): |
| 31 | # use my personal account, need an API key |
| 32 | py.sign_in(PLOTLY_USERNAME, PLOTLY_API_KEY) |
| 33 | self.data = {} # all the metrics we read |
| 34 | self.ceilometer_url = 'http://%s:%s/api/tenant/monitoring/dashboard/metersamples/' % (XOS_IP, XOS_PORT) |
| 35 | |
| 36 | def getDataFromCeilometer(self): |
| 37 | ''' |
| 38 | keep track of how many times a lag member resolves |
| 39 | read new metrics from ceilometer (use timestamps to read |
| 40 | a range so we get new |
| 41 | data since last read) |
| 42 | ''' |
| 43 | url = "%s?%s" % (self.ceilometer_url,"no_hyperlinks=1&meter=broadview.pt.packet-trace-lag-resolution") |
| 44 | try: |
| 45 | response = requests.get(url, auth=('padmin@vicci.org','letmein')) |
| 46 | except requests.exceptions.RequestException as e: |
| 47 | raise e |
| 48 | samples = response.json() |
| 49 | #print samples |
| 50 | for lagresolution in samples: |
| 51 | for lagmember in ast.literal_eval(lagresolution['metadata']['lag-members']): |
| 52 | # here we add any new lagmembers to the hash |
| 53 | if not lagmember in self.data: |
| 54 | self.data[lagmember] = 0 |
| 55 | lagresolve = lagresolution['metadata']['dst-lag-member'] |
| 56 | self.data[lagresolve] = self.data[lagresolve] + 1 |
| 57 | |
| 58 | # now that we have added any new ceilometer data update the |
| 59 | # pie chart and write it out |
| 60 | print self.data |
| 61 | |
| 62 | data = [] |
| 63 | tmp = {} |
| 64 | tmp["labels"] = [] |
| 65 | tmp["values"] = [] |
| 66 | tmp["type"] = "pie" |
| 67 | for key, val in self.data.iteritems(): |
| 68 | tmp["labels"].append(key) # the lag member ID |
| 69 | tmp["values"].append(val) # count |
| 70 | data.append(tmp) |
| 71 | fig = {} |
| 72 | fig["data"] = data |
| 73 | fig["layout"] = {} |
| 74 | # put whatever metadata that makes sense, if any |
| 75 | fig["layout"]["title"] = 'BroadView LAG Resolution' |
| 76 | print fig |
| 77 | return fig |
| 78 | |
| 79 | def plotPieToFile(self, data, filename="pie.png"): |
| 80 | # takes data in the following format and writes it to file |
| 81 | if not data: |
| 82 | data = { |
| 83 | 'data': [{'labels': ['LAG001', 'LAG002', 'LAG003'], |
| 84 | 'values': [19, 26, 55], |
| 85 | 'type': 'pie'}], |
| 86 | 'layout': {'title': 'BroadView LAG Resolution metadata...'} |
| 87 | } |
| 88 | |
| 89 | py.image.save_as(data, filename=filename) |
| 90 | |
| 91 | if __name__ == "__main__": |
| 92 | x = LAGPlot() |
| 93 | while True: |
| 94 | print "Plotting data" |
| 95 | data = x.getDataFromCeilometer() |
| 96 | x.plotPieToFile(data) |
| 97 | time.sleep(30) |