blob: 28fab4e3f53bcde7e6afcf3da2327d2886abb4c4 [file] [log] [blame]
Matteo Scandoloeb0d11c2017-08-08 13:05:26 -07001
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 Vavilapallif6eda8f2016-09-09 13:43:51 -040017import plotly.plotly as py
18import plotly.graph_objs as go
19import time
20import requests
21#import ceilometerapi
22import ast
23
24PLOTLY_USERNAME = 'myusername'
25PLOTLY_API_KEY = 'myapikey'
26XOS_IP = '1.2.3.4'
27XOS_PORT = '9999'
28
29class 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
91if __name__ == "__main__":
92 x = LAGPlot()
93 while True:
94 print "Plotting data"
95 data = x.getDataFromCeilometer()
96 x.plotPieToFile(data)
97 time.sleep(30)