MKP 1.0.5, added debug log decorator function
This commit is contained in:
parent
30e0581959
commit
abff64b3b5
@ -4,6 +4,9 @@ import getopt
|
|||||||
import sys
|
import sys
|
||||||
import random
|
import random
|
||||||
import os
|
import os
|
||||||
|
from time import (
|
||||||
|
time, localtime, strftime,
|
||||||
|
)
|
||||||
|
|
||||||
"""
|
"""
|
||||||
Sample output
|
Sample output
|
||||||
@ -46,6 +49,55 @@ OPTIONS:
|
|||||||
-p, --password Password
|
-p, --password Password
|
||||||
-h, --help Show this help message and exit
|
-h, --help Show this help message and exit
|
||||||
""")
|
""")
|
||||||
|
|
||||||
|
|
||||||
|
debugLogFilename = ""
|
||||||
|
SEP = "|"
|
||||||
|
#SEP = "\t"
|
||||||
|
TIMEFMT = "%Y-%m-%d %H:%M:%S"
|
||||||
|
FLOATFMT = "{:.4f}"
|
||||||
|
|
||||||
|
'''
|
||||||
|
Decorator function for debugging purposes
|
||||||
|
creates a file with many information regarding the function call, like:
|
||||||
|
timestamp
|
||||||
|
name of function
|
||||||
|
runtime
|
||||||
|
number of arguments
|
||||||
|
number of keyword arguments
|
||||||
|
return value of function call
|
||||||
|
type of return value
|
||||||
|
all parameters given to function
|
||||||
|
'''
|
||||||
|
def debugLog(function):
|
||||||
|
def wrapper(*args, **kwargs):
|
||||||
|
# execute function and measure runtime
|
||||||
|
start = time()
|
||||||
|
value = function(*args, **kwargs)
|
||||||
|
end = time()
|
||||||
|
# get number of args and kwargs
|
||||||
|
len_args = len(args)
|
||||||
|
len_kwargs = len(kwargs)
|
||||||
|
# format the output
|
||||||
|
seconds = FLOATFMT.format(end - start)
|
||||||
|
local_time = strftime(TIMEFMT,localtime(start))
|
||||||
|
# get function name
|
||||||
|
fname = function.__name__
|
||||||
|
# out1: Timestamp|Name of Function|Runtime|Num Args|Num Kwargs|Return Value|Return Value Type
|
||||||
|
out1 = f"{local_time}{SEP}{fname}{SEP}{seconds}{SEP}{len_args}{SEP}{len_kwargs}{SEP}{value}{SEP}{type(value)}"
|
||||||
|
# out2: all arguments
|
||||||
|
out2 = ""
|
||||||
|
for arg in args:
|
||||||
|
out2 = out2 + SEP + str(arg)
|
||||||
|
# out 3: all keyowrd arguments
|
||||||
|
out3 = ""
|
||||||
|
if len_kwargs > 0:
|
||||||
|
for key, val in kwargs.items():
|
||||||
|
out3 = out3 + SEP + key + ":" + str(val)
|
||||||
|
with open(debugLogFilename, "a") as f:
|
||||||
|
f.write(f"{out1}{out2}{out3}\n")
|
||||||
|
return value
|
||||||
|
return wrapper
|
||||||
|
|
||||||
# Parameters coming from CheckMK
|
# Parameters coming from CheckMK
|
||||||
opt_hostname = ""
|
opt_hostname = ""
|
||||||
@ -97,6 +149,12 @@ long_options = [
|
|||||||
'hostname=', 'username=', 'password=', 'help'
|
'hostname=', 'username=', 'password=', 'help'
|
||||||
]
|
]
|
||||||
|
|
||||||
|
def createDebugFilename(hostname):
|
||||||
|
home_path = os.getenv("HOME")
|
||||||
|
tmp_path = f"{home_path}/tmp"
|
||||||
|
file_name = f"{tmp_path}/hal9001_{hostname}_debug.log"
|
||||||
|
return file_name
|
||||||
|
|
||||||
def getOptions():
|
def getOptions():
|
||||||
global opt_hostname
|
global opt_hostname
|
||||||
global opt_username
|
global opt_username
|
||||||
@ -119,6 +177,7 @@ def showOptions():
|
|||||||
print(f"Username: {opt_username}")
|
print(f"Username: {opt_username}")
|
||||||
print(f"Password: {opt_password}")
|
print(f"Password: {opt_password}")
|
||||||
|
|
||||||
|
#@debugLog
|
||||||
def calculateNewUserStorage(current_storage):
|
def calculateNewUserStorage(current_storage):
|
||||||
# let the chance that no change occured be at 90%
|
# let the chance that no change occured be at 90%
|
||||||
no_change = random.randint(1, 100)
|
no_change = random.randint(1, 100)
|
||||||
@ -140,6 +199,7 @@ def calculateNewUserStorage(current_storage):
|
|||||||
new_storage = 1024000
|
new_storage = 1024000
|
||||||
return float(new_storage)
|
return float(new_storage)
|
||||||
|
|
||||||
|
#@debugLog
|
||||||
def calculateNewStorageCounters(ul_bytes, dl_bytes):
|
def calculateNewStorageCounters(ul_bytes, dl_bytes):
|
||||||
# let the chance that no change occured be at 2%
|
# let the chance that no change occured be at 2%
|
||||||
no_change = random.randint(1, 100)
|
no_change = random.randint(1, 100)
|
||||||
@ -158,10 +218,12 @@ def calculateNewStorageCounters(ul_bytes, dl_bytes):
|
|||||||
new_dl_bytes = dl_bytes + amount_dl
|
new_dl_bytes = dl_bytes + amount_dl
|
||||||
return float(new_ul_bytes), float(new_dl_bytes)
|
return float(new_ul_bytes), float(new_dl_bytes)
|
||||||
|
|
||||||
|
#@debugLog
|
||||||
def doCmkHalStatusOutput(status, version, username, ipaddress):
|
def doCmkHalStatusOutput(status, version, username, ipaddress):
|
||||||
print("<<<hal9001_status:sep(59)>>>")
|
print("<<<hal9001_status:sep(59)>>>")
|
||||||
print(f"{status};{version};{username};{ipaddress}")
|
print(f"{status};{version};{username};{ipaddress}")
|
||||||
|
|
||||||
|
#@debugLog
|
||||||
def doCmkHalUsersOutput(users, hostname):
|
def doCmkHalUsersOutput(users, hostname):
|
||||||
print("<<<hal9001_users:sep(59)>>>")
|
print("<<<hal9001_users:sep(59)>>>")
|
||||||
home_path = os.getenv("HOME")
|
home_path = os.getenv("HOME")
|
||||||
@ -188,6 +250,7 @@ def doCmkHalUsersOutput(users, hostname):
|
|||||||
# create output
|
# create output
|
||||||
print(f"{user};{realname};{new_storage};{quota_max}")
|
print(f"{user};{realname};{new_storage};{quota_max}")
|
||||||
|
|
||||||
|
#@debugLog
|
||||||
def doCmkHalStoragesOutput(storages, hostname):
|
def doCmkHalStoragesOutput(storages, hostname):
|
||||||
print("<<<hal9001_storages:sep(59)>>>")
|
print("<<<hal9001_storages:sep(59)>>>")
|
||||||
home_path = os.getenv("HOME")
|
home_path = os.getenv("HOME")
|
||||||
@ -226,6 +289,7 @@ def doCmkHalStoragesOutput(storages, hostname):
|
|||||||
# create output
|
# create output
|
||||||
print(f"{storage};{realname};{new_ul_bytes};{new_dl_bytes}")
|
print(f"{storage};{realname};{new_ul_bytes};{new_dl_bytes}")
|
||||||
|
|
||||||
|
#@debugLog
|
||||||
def getStatus():
|
def getStatus():
|
||||||
# randomly set one of the three available states
|
# randomly set one of the three available states
|
||||||
status_index = random.randint(1,100)
|
status_index = random.randint(1,100)
|
||||||
@ -240,6 +304,7 @@ def getStatus():
|
|||||||
hal_status = hal_status_list[0]
|
hal_status = hal_status_list[0]
|
||||||
return hal_status, hal_version
|
return hal_status, hal_version
|
||||||
|
|
||||||
|
#@debugLog
|
||||||
def doLogin(hostname, username, password):
|
def doLogin(hostname, username, password):
|
||||||
# simulate the login to our HAL system
|
# simulate the login to our HAL system
|
||||||
# give it a chance of 2% to fail to demonstrate an error from time to time
|
# give it a chance of 2% to fail to demonstrate an error from time to time
|
||||||
@ -250,6 +315,7 @@ def doLogin(hostname, username, password):
|
|||||||
return False, 404
|
return False, 404
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
global debugLogFilename
|
||||||
getOptions()
|
getOptions()
|
||||||
#showOptions()
|
#showOptions()
|
||||||
# some checks
|
# some checks
|
||||||
@ -266,6 +332,7 @@ def main():
|
|||||||
sys.stderr.write(f"No password given.\n")
|
sys.stderr.write(f"No password given.\n")
|
||||||
showUsage()
|
showUsage()
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
debugLogFilename = createDebugFilename(opt_hostname)
|
||||||
success, code = doLogin(opt_hostname, opt_username, opt_password)
|
success, code = doLogin(opt_hostname, opt_username, opt_password)
|
||||||
if success:
|
if success:
|
||||||
status, version = getStatus()
|
status, version = getStatus()
|
||||||
|
BIN
mkp/hal9001-1.0.5.mkp
Normal file
BIN
mkp/hal9001-1.0.5.mkp
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user