|
| 1 | +#!/usr/bin/env python |
| 2 | +""" |
| 3 | +* ******************************************************* |
| 4 | +* Copyright (c) VMware, Inc. 2021. All Rights Reserved. |
| 5 | +* SPDX-License-Identifier: MIT |
| 6 | +* ******************************************************* |
| 7 | +* |
| 8 | +* DISCLAIMER. THIS PROGRAM IS PROVIDED TO YOU "AS IS" WITHOUT |
| 9 | +* WARRANTIES OR CONDITIONS OF ANY KIND, WHETHER ORAL OR WRITTEN, |
| 10 | +* EXPRESS OR IMPLIED. THE AUTHOR SPECIFICALLY DISCLAIMS ANY IMPLIED |
| 11 | +* WARRANTIES OR CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY, |
| 12 | +* NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. |
| 13 | +""" |
| 14 | + |
| 15 | +__author__ = 'VMware, Inc.' |
| 16 | +__vcenter_version__ = '6.7+' |
| 17 | + |
| 18 | +from vmware.vapi.vsphere.client import create_vsphere_client |
| 19 | +from datetime import datetime, timedelta |
| 20 | + |
| 21 | +from samples.vsphere.common import (sample_cli, sample_util) |
| 22 | +from samples.vsphere.common.ssl_helper import get_unverified_session |
| 23 | + |
| 24 | +""" |
| 25 | +Description: Demonstrates monitoring api workflow |
| 26 | +1. List all memory and cpu counters |
| 27 | +2. Query and print daily averages |
| 28 | +""" |
| 29 | + |
| 30 | +MEMORY_CATEGORY = "com.vmware.applmgmt.mon.cat.memory" |
| 31 | +CPU_CATEGORY = "com.vmware.applmgmt.mon.cat.cpu" |
| 32 | +CATEGORIES = (MEMORY_CATEGORY, CPU_CATEGORY) |
| 33 | +METRIC_TITLE = "Metric" |
| 34 | +DAY = timedelta(days=1) |
| 35 | +DATE_FORMAT = "%Y-%m-%d" |
| 36 | + |
| 37 | +parser = sample_cli.build_arg_parser() |
| 38 | +args = sample_util.process_cli_args(parser.parse_args()) |
| 39 | +session = get_unverified_session() if args.skipverification else None |
| 40 | +client = create_vsphere_client(server=args.server, |
| 41 | + username=args.username, |
| 42 | + password=args.password, |
| 43 | + session=session) |
| 44 | + |
| 45 | +# Get the Monitoring interface |
| 46 | +monitoring = client.appliance.Monitoring |
| 47 | + |
| 48 | +# List the available counters |
| 49 | +counters = monitoring.list() |
| 50 | + |
| 51 | +# Get the names of counters that relate to CPU and Memory categories |
| 52 | +conterIds = [] |
| 53 | +for counter in counters: |
| 54 | + if counter.category in CATEGORIES: |
| 55 | + conterIds.append(counter.id) |
| 56 | + |
| 57 | +# Compute interval for last few days |
| 58 | +end = datetime.now() |
| 59 | +start = end - timedelta(days=2) |
| 60 | + |
| 61 | +# Query timeseries data |
| 62 | +query = monitoring.MonitoredItemDataRequest(names=conterIds, |
| 63 | + interval="DAY1", |
| 64 | + function="AVG", |
| 65 | + start_time=start, |
| 66 | + end_time=end) |
| 67 | +data = monitoring.query(query) |
| 68 | + |
| 69 | + |
| 70 | +print("Example: Query Monitoring for Timeseries Data:") |
| 71 | +print("-------------------\n") |
| 72 | + |
| 73 | +# Create title and row format strings |
| 74 | +# We need one labeled column for every day between start and end |
| 75 | + |
| 76 | +# Reserve 25 characters for the first column with metric name |
| 77 | +title = METRIC_TITLE + (" " * (25 - len(METRIC_TITLE))) |
| 78 | +columnFormat = "{0:25}" |
| 79 | + |
| 80 | +idx = 0 |
| 81 | +timestamp = start |
| 82 | +# Create columns for each day |
| 83 | +while timestamp <= end: |
| 84 | + # 24 characters per column. In the title use 10 for date and 14 padding. |
| 85 | + title += timestamp.strftime(DATE_FORMAT) + " " * 14 |
| 86 | + columnFormat += "{{1[{}]:24}}".format(idx) |
| 87 | + # increment |
| 88 | + idx = idx + 1 |
| 89 | + timestamp = timestamp + DAY |
| 90 | + |
| 91 | +print(title) |
| 92 | +for item in data: |
| 93 | + print(columnFormat.format(item.name, item.data)) |
0 commit comments