From ce8f17db7945c7937ba1eb22cbd31d56828d57eb Mon Sep 17 00:00:00 2001 From: John Lenihan Date: Fri, 4 Oct 2019 15:06:08 +0100 Subject: [PATCH 1/3] WIP:Sample monitoring/metrics --- opsramp/binding.py | 8 +++++ opsramp/metric.py | 35 ++++++++++++++++++++ opsramp/resources.py | 40 +++++++++++++++++++++++ samples/monitoring.py | 74 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 157 insertions(+) create mode 100755 opsramp/metric.py create mode 100755 opsramp/resources.py create mode 100755 samples/monitoring.py diff --git a/opsramp/binding.py b/opsramp/binding.py index 76dfade..dfa8ea6 100755 --- a/opsramp/binding.py +++ b/opsramp/binding.py @@ -24,6 +24,8 @@ from opsramp.base import ApiObject, ApiWrapper from opsramp.globalconfig import GlobalConfig from opsramp.tenant import Tenant +from opsramp.metric import Metric +from opsramp.resources import Resources def connect(url, key, secret): @@ -58,3 +60,9 @@ def config(self): def tenant(self, name): return Tenant(self, name) + + def metric(self, name): + return Metric(self, name) + + def resources(self, name): + return Resources(self, name) diff --git a/opsramp/metric.py b/opsramp/metric.py new file mode 100755 index 0000000..3a3e552 --- /dev/null +++ b/opsramp/metric.py @@ -0,0 +1,35 @@ +#!/usr/bin/env python +# +# A minimal Python language binding for the OpsRamp REST API. +# +# metric.py +# Classes related to metrics +# +# (c) Copyright 2019 Hewlett Packard Enterprise Development LP +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from __future__ import print_function +import datetime +from opsramp.base import ApiWrapper + + +class Metric(ApiWrapper): + def __init__(self, parent, uuid): + super(Metric, self).__init__(parent.api, 'metric') + self.uuid = uuid + + def get(self, rtype, resource): + suffix = '/tenants/%s/rtypes/%s/resources/%s/metrics' % \ + (self.uuid, rtype, resource) + return self.api.get(suffix) diff --git a/opsramp/resources.py b/opsramp/resources.py new file mode 100755 index 0000000..ea9c85d --- /dev/null +++ b/opsramp/resources.py @@ -0,0 +1,40 @@ +#!/usr/bin/env python +# +# A minimal Python language binding for the OpsRamp REST API. +# +# resources.py +# Classes related to resources +# +# (c) Copyright 2019 Hewlett Packard Enterprise Development LP +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from __future__ import print_function +import datetime +from opsramp.base import ApiWrapper + + +class Resources(ApiWrapper): + def __init__(self, parent, uuid): + super(Resources, self).__init__(parent.api, '') + self.uuid = uuid + + def get(self, resource): + suffix = '/tenants/%s/resources/%s' % \ + (self.uuid, resource) + return self.api.get(suffix) + + def search(self): + suffix = '/tenants/%s/resources/search?queryString=Id' % \ + self.uuid + return self.api.get(suffix) diff --git a/samples/monitoring.py b/samples/monitoring.py new file mode 100755 index 0000000..abeb71e --- /dev/null +++ b/samples/monitoring.py @@ -0,0 +1,74 @@ +#!/usr/bin/env python +# +# Exercise the opsramp module as an illustration of how to use it. +# +# (c) Copyright 2019 Hewlett Packard Enterprise Development LP +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# From Opsramp API Documentation e.g. +# https://docs.opsramp.com/monitoring-apis/ +# https://docs.opsramp.com/metric-apis/ +# +# Assign Templates to Resource +# Search Templates +# Get Assigned Templates of Resource +# Unassign Templates from Resource +# +# Save Metrics on a Resource +# Get Metrics on a Resource +# Get Metric Type +# Get Metric (Time series) on a Resource +# +# https:///api/v2/metric/tenants/{clientId}/rtypes/{rtype}/resources/{resource}/metrics +# https://sramp.com/api/v2//tenants/client_2703/resources/search +# https:///api/v2/tenants/{tenantId}/resources/search +from __future__ import print_function +import os +import yaml + +import opsramp.binding +import opsramp.metric +import opsramp.resources + + +def connect(): + url = os.environ['OPSRAMP_URL'] + key = os.environ['OPSRAMP_KEY'] + secret = os.environ['OPSRAMP_SECRET'] + return opsramp.binding.connect(url, key, secret) + + +def main(): + tenant_id = os.environ['OPSRAMP_TENANT_ID'] + + ormp = connect() + tenant = ormp.tenant(tenant_id) + monitoring = tenant.monitoring() + templates = monitoring.templates() + + resources = ormp.resources(tenant_id) + res = resources.search() + # print("resources: %s\n" % res) + + # res = templates.search() + # print("Templates Search: %s\n" % res) + + rtype = 'DEVICE' + resource = '9e8b99ad-f9db-4e81-bfad-40249d9d9dac' + # metrics = ormp.metric.get(tenant_id, rtype, resource) + metrics = ormp.metric(tenant_id) + res = metrics.get(rtype, resource) + print("Metrics: %s\n" % res) +if __name__ == "__main__": + main() From 7f1b6e9efd598f138dd73a8c909604965c35cb0c Mon Sep 17 00:00:00 2001 From: John Lenihan Date: Mon, 7 Oct 2019 14:37:36 +0100 Subject: [PATCH 2/3] More WIP monitoring/metrics --- opsramp/metric.py | 7 +++++++ samples/monitoring.py | 38 ++++++++++++++++++++++++++++++-------- 2 files changed, 37 insertions(+), 8 deletions(-) diff --git a/opsramp/metric.py b/opsramp/metric.py index 3a3e552..01741cc 100755 --- a/opsramp/metric.py +++ b/opsramp/metric.py @@ -33,3 +33,10 @@ def get(self, rtype, resource): suffix = '/tenants/%s/rtypes/%s/resources/%s/metrics' % \ (self.uuid, rtype, resource) return self.api.get(suffix) + + def search(self, rtype, resource, metric, starttime, endtime, ts_type): + suffix='/search?tenant=%s&rtype=%s&' \ + 'resource=%s&metric=%s&startTime=%s&endTime=%s' \ + '×eries_type=%s' % \ + (self.uuid, rtype, resource, metric, starttime, endtime, ts_type) + return self.api.get(suffix) diff --git a/samples/monitoring.py b/samples/monitoring.py index abeb71e..476e63b 100755 --- a/samples/monitoring.py +++ b/samples/monitoring.py @@ -31,11 +31,14 @@ # Get Metric (Time series) on a Resource # # https:///api/v2/metric/tenants/{clientId}/rtypes/{rtype}/resources/{resource}/metrics +# https:///api/v2/metric/search?tenant=client_20&rtype=DEVICE&resource=ab342123-5bfb-434c-aae4-03611ca020d9&metric=system.cpu.utilization&startTime=1536643494&endTime=1536661494 # https://sramp.com/api/v2//tenants/client_2703/resources/search # https:///api/v2/tenants/{tenantId}/resources/search from __future__ import print_function import os import yaml +import json +import time import opsramp.binding import opsramp.metric @@ -48,27 +51,46 @@ def connect(): secret = os.environ['OPSRAMP_SECRET'] return opsramp.binding.connect(url, key, secret) - def main(): tenant_id = os.environ['OPSRAMP_TENANT_ID'] + device_id = os.environ['OPSRAMP_DEVICE_ID'] ormp = connect() tenant = ormp.tenant(tenant_id) monitoring = tenant.monitoring() templates = monitoring.templates() + # Get list of Opsramp Resources for this tenant resources = ormp.resources(tenant_id) res = resources.search() - # print("resources: %s\n" % res) + pretty_res = json.dumps(res, sort_keys=True, indent=4, separators=('.', ': ')) + print("resources: %s\n" % pretty_res) + + # Get a list of Opsramp Templates for this tenant + res = templates.search() + pretty_res = json.dumps(res, sort_keys=True, indent=4, separators=('.', ': ')) + print("Templates Search: %s\n" % pretty_res) - # res = templates.search() - # print("Templates Search: %s\n" % res) + # An example of getting Opsramp Metrics of type 'DEVICE' for a particular + # Resource within a particular tenant. + rtype = 'DEVICE' + metrics = ormp.metric(tenant_id) + res = metrics.get(rtype, device_id) + pretty_res = json.dumps(res, sort_keys=True, indent=4, separators=('.', ': ')) + print("Metrics: %s\n" % pretty_res) + # An example of getting time-based Opsramp metrics for + # a particular resource within a particular tenant. rtype = 'DEVICE' - resource = '9e8b99ad-f9db-4e81-bfad-40249d9d9dac' - # metrics = ormp.metric.get(tenant_id, rtype, resource) metrics = ormp.metric(tenant_id) - res = metrics.get(rtype, resource) - print("Metrics: %s\n" % res) + metric_name = "azure.cpu" + starttime = int((time.time() - (60*60*24))) + endtime = int(time.time()) + ts_type = "RealTime" + + res = metrics.search(rtype, device_id, metric_name, starttime, endtime, ts_type) + pretty_res = json.dumps(res, sort_keys=True, indent=4, separators=('.', ': ')) + print("Time Series Metrics: %s\n" % pretty_res) + if __name__ == "__main__": main() From 2d435ad49fc5110912df71801b1240d142135d45 Mon Sep 17 00:00:00 2001 From: John Lenihan Date: Mon, 7 Oct 2019 15:53:29 +0100 Subject: [PATCH 3/3] Add some realtime metrics examples. --- samples/monitoring.py | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/samples/monitoring.py b/samples/monitoring.py index 476e63b..afb65c3 100755 --- a/samples/monitoring.py +++ b/samples/monitoring.py @@ -77,20 +77,37 @@ def main(): metrics = ormp.metric(tenant_id) res = metrics.get(rtype, device_id) pretty_res = json.dumps(res, sort_keys=True, indent=4, separators=('.', ': ')) - print("Metrics: %s\n" % pretty_res) + print("Realtime Metrics: %s\n" % pretty_res) - # An example of getting time-based Opsramp metrics for + # An example of getting realtime time-based Opsramp metrics for # a particular resource within a particular tenant. rtype = 'DEVICE' metrics = ormp.metric(tenant_id) metric_name = "azure.cpu" + # 24 hours ago starttime = int((time.time() - (60*60*24))) + # now endtime = int(time.time()) ts_type = "RealTime" res = metrics.search(rtype, device_id, metric_name, starttime, endtime, ts_type) pretty_res = json.dumps(res, sort_keys=True, indent=4, separators=('.', ': ')) - print("Time Series Metrics: %s\n" % pretty_res) + print("Realtime CPU Metrics: %s\n" % pretty_res) + + # Another example of getting realtime time-based Opsramp metrics for + # a particular resource within a particular tenant. + rtype = 'DEVICE' + metrics = ormp.metric(tenant_id) + metric_name = "cloud.instance.state" + # 24 hours ago + starttime = int((time.time() - (60*60*24))) + # now + endtime = int(time.time()) + ts_type = "RealTime" + + res = metrics.search(rtype, device_id, metric_name, starttime, endtime, ts_type) + pretty_res = json.dumps(res, sort_keys=True, indent=4, separators=('.', ': ')) + print("Realtime instance state Metrics: %s\n" % pretty_res) if __name__ == "__main__": main()