add cache for qm monitor functions
This commit is contained in:
parent
a00355ad4c
commit
d3ef02cdbf
@ -1,7 +1,33 @@
|
||||
import time
|
||||
import random
|
||||
import pexpect
|
||||
from functools import wraps
|
||||
|
||||
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):
|
||||
child = pexpect.spawn(f'qm monitor {vm_id}')
|
||||
try:
|
||||
|
||||
@ -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('--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-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()
|
||||
|
||||
@ -210,6 +212,8 @@ def main():
|
||||
global prefix
|
||||
prefix = args.metrics_prefix
|
||||
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:
|
||||
gauge_dict[name] = Gauge(f"{prefix}_{name}", description, labels)
|
||||
|
||||
@ -2,7 +2,7 @@ from setuptools import setup, find_packages
|
||||
|
||||
setup(
|
||||
name='pvemon',
|
||||
version='1.0.1',
|
||||
version = "1.0.1",
|
||||
packages=find_packages(),
|
||||
entry_points={
|
||||
'console_scripts': [
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user