Skip to content

Commit

Permalink
perf
Browse files Browse the repository at this point in the history
  • Loading branch information
Koos85 committed Feb 3, 2023
1 parent 7af8c76 commit 9e1caea
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
2 changes: 1 addition & 1 deletion lib/version.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Version string. Examples:
# '3.0.0'
# '3.0.0-alpha1'
__version__ = '3.0.0-alpha11'
__version__ = '3.0.0-alpha12'
27 changes: 18 additions & 9 deletions lib/vmwareconn.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
import ssl
from datetime import datetime, timedelta
from pyVmomi import vim, vmodl # type: ignore
from pyVim import connect

Expand All @@ -9,12 +10,14 @@


def get_alarms(ip4, username, password):
content = _get_content(ip4, username, password)
conn = _get_conn(ip4, username, password)
content = conn.RetrieveContent()
return content.rootFolder.triggeredAlarmState


def get_data(ip4, username, password, obj_type, properties):
content = _get_content(ip4, username, password)
conn = _get_conn(ip4, username, password)
content = conn.RetrieveContent()
data = _query_view(
content=content,
obj_type=obj_type,
Expand All @@ -23,8 +26,10 @@ def get_data(ip4, username, password, obj_type, properties):
return data


def get_perf(ip4, username, password, obj_type, metrics):
content = _get_content(ip4, username, password)
def get_perf(ip4, username, password, obj_type, metrics, interval):
conn = _get_conn(ip4, username, password)
content = conn.RetrieveContent()
content_time = conn.CurrentTime()
view_ref = content.viewManager.CreateContainerView(
container=content.rootFolder, type=[obj_type], recursive=True)

Expand All @@ -47,9 +52,13 @@ def get_perf(ip4, username, password, obj_type, metrics):
# nothing to query
continue

spec = vim.PerformanceManager.QuerySpec(intervalId=20, maxSample=15,
end_time = content_time
start_time = content_time - timedelta(seconds=interval + 1)
spec = vim.PerformanceManager.QuerySpec(intervalId=20,
entity=child,
metricId=metric_id)
etricId=metric_id,
startTime=start_time,
endTime=end_time)
results[child.config.instanceUuid] = result = {m: {} for m in metrics}
for stat in perf_manager.QueryStats(querySpec=[spec]):
for val in stat.value:
Expand All @@ -70,18 +79,18 @@ def drop_connnection(host):
conn._stub.DropConnections()


def _get_content(host, username, password):
def _get_conn(host, username, password):
conn, expired = AssetCache.get_value((host, 'connection'))
if expired:
conn._stub.DropConnections()
elif conn:
return conn.RetrieveContent()
return conn

conn = _get_connection(host, username, password)
if not conn:
raise ConnectionError('Unable to connect')
AssetCache.set_value((host, 'connection'), conn, MAX_CONN_AGE)
return conn.RetrieveContent()
return conn


def _get_connection(host, username, password):
Expand Down
6 changes: 5 additions & 1 deletion lib/vmwarequery.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

from .vmwareconn import get_alarms, get_data, get_perf, drop_connnection

DEFAULT_INTERVAL = 300


async def vmwarequery_alarms(
asset: Asset,
Expand Down Expand Up @@ -65,6 +67,7 @@ async def vmwarequery_perf(
if None in (username, password):
logging.error(f'missing credentails for {asset}')
raise IgnoreResultException
interval = check_config.get('_interval', DEFAULT_INTERVAL)

try:
result = await asyncio.get_event_loop().run_in_executor(
Expand All @@ -74,7 +77,8 @@ async def vmwarequery_perf(
username,
password,
obj_type,
metrics
metrics,
interval
)
except (vim.fault.InvalidLogin,
vim.fault.NotAuthenticated):
Expand Down

0 comments on commit 9e1caea

Please sign in to comment.