add cache for qm monitor functions

This commit is contained in:
illustris 2023-10-16 18:13:13 +05:30
parent a00355ad4c
commit d3ef02cdbf
Signed by: illustris
GPG Key ID: 56C8FC0B899FEFA3
3 changed files with 31 additions and 1 deletions

View File

@ -1,7 +1,33 @@
import time
import random
import pexpect import pexpect
from functools import wraps
global_qm_timeout = 10 global_qm_timeout = 10
qm_max_ttl = 600
qm_rand = 60
def ttl_cache_with_randomness(max_ttl, randomness_factor):
cache = {}
def decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
# Create a key based on the args and kwargs
key = str(args) + str(kwargs)
# Check if the key is in the cache and not expired
if key in cache:
result, timestamp = cache[key]
elapsed_time = time.time() - timestamp
if elapsed_time < max_ttl + random.uniform(-randomness_factor, randomness_factor):
return result
# Call the actual function and store the result in cache
result = func(*args, **kwargs)
cache[key] = (result, time.time())
return result
return wrapper
return decorator
@ttl_cache_with_randomness(qm_max_ttl, qm_rand)
def qm_term_cmd(vm_id, cmd, timeout=global_qm_timeout): def qm_term_cmd(vm_id, cmd, timeout=global_qm_timeout):
child = pexpect.spawn(f'qm monitor {vm_id}') child = pexpect.spawn(f'qm monitor {vm_id}')
try: try:

View File

@ -199,6 +199,8 @@ def main():
parser.add_argument('--loglevel', type=str, default='INFO', help='Set log level (DEBUG, INFO, WARNING, ERROR, CRITICAL)') parser.add_argument('--loglevel', type=str, default='INFO', help='Set log level (DEBUG, INFO, WARNING, ERROR, CRITICAL)')
parser.add_argument('--profile', type=str, default='false', help='collect metrics once, and print profiling stats') parser.add_argument('--profile', type=str, default='false', help='collect metrics once, and print profiling stats')
parser.add_argument('--qm-terminal-timeout', type=int, default=10, help='timeout for qm terminal commands') parser.add_argument('--qm-terminal-timeout', type=int, default=10, help='timeout for qm terminal commands')
parser.add_argument('--qm-max-ttl', type=int, default=600, help='cache ttl for data pulled from qm monitor')
parser.add_argument('--qm-rand', type=int, default=60, help='randomize qm monitor cache expiry')
args = parser.parse_args() args = parser.parse_args()
@ -210,6 +212,8 @@ def main():
global prefix global prefix
prefix = args.metrics_prefix prefix = args.metrics_prefix
pvecommon.global_qm_timeout = args.qm_terminal_timeout pvecommon.global_qm_timeout = args.qm_terminal_timeout
pvecommon.qm_max_ttl = args.qm_max_ttl
pvecommon.qm_rand = args.qm_rand
for name, description, labels in gauge_settings: for name, description, labels in gauge_settings:
gauge_dict[name] = Gauge(f"{prefix}_{name}", description, labels) gauge_dict[name] = Gauge(f"{prefix}_{name}", description, labels)

View File

@ -2,7 +2,7 @@ from setuptools import setup, find_packages
setup( setup(
name='pvemon', name='pvemon',
version='1.0.1', version = "1.0.1",
packages=find_packages(), packages=find_packages(),
entry_points={ entry_points={
'console_scripts': [ 'console_scripts': [