From abff64b3b5bedadddfc4fdfe98bd09af749c0013 Mon Sep 17 00:00:00 2001 From: cmk-bonobo Date: Sun, 14 Jan 2024 15:50:03 +0100 Subject: [PATCH] MKP 1.0.5, added debug log decorator function --- .../check_mk/agents/special/agent_hal9001 | 67 ++++++++++++++++++ mkp/hal9001-1.0.5.mkp | Bin 0 -> 9040 bytes 2 files changed, 67 insertions(+) create mode 100644 mkp/hal9001-1.0.5.mkp diff --git a/local/share/check_mk/agents/special/agent_hal9001 b/local/share/check_mk/agents/special/agent_hal9001 index 506ce39..8bdc8ac 100644 --- a/local/share/check_mk/agents/special/agent_hal9001 +++ b/local/share/check_mk/agents/special/agent_hal9001 @@ -4,6 +4,9 @@ import getopt import sys import random import os +from time import ( + time, localtime, strftime, +) """ Sample output @@ -46,6 +49,55 @@ OPTIONS: -p, --password Password -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 opt_hostname = "" @@ -97,6 +149,12 @@ long_options = [ '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(): global opt_hostname global opt_username @@ -119,6 +177,7 @@ def showOptions(): print(f"Username: {opt_username}") print(f"Password: {opt_password}") +#@debugLog def calculateNewUserStorage(current_storage): # let the chance that no change occured be at 90% no_change = random.randint(1, 100) @@ -140,6 +199,7 @@ def calculateNewUserStorage(current_storage): new_storage = 1024000 return float(new_storage) +#@debugLog def calculateNewStorageCounters(ul_bytes, dl_bytes): # let the chance that no change occured be at 2% no_change = random.randint(1, 100) @@ -158,10 +218,12 @@ def calculateNewStorageCounters(ul_bytes, dl_bytes): new_dl_bytes = dl_bytes + amount_dl return float(new_ul_bytes), float(new_dl_bytes) +#@debugLog def doCmkHalStatusOutput(status, version, username, ipaddress): print("<<>>") print(f"{status};{version};{username};{ipaddress}") +#@debugLog def doCmkHalUsersOutput(users, hostname): print("<<>>") home_path = os.getenv("HOME") @@ -188,6 +250,7 @@ def doCmkHalUsersOutput(users, hostname): # create output print(f"{user};{realname};{new_storage};{quota_max}") +#@debugLog def doCmkHalStoragesOutput(storages, hostname): print("<<>>") home_path = os.getenv("HOME") @@ -226,6 +289,7 @@ def doCmkHalStoragesOutput(storages, hostname): # create output print(f"{storage};{realname};{new_ul_bytes};{new_dl_bytes}") +#@debugLog def getStatus(): # randomly set one of the three available states status_index = random.randint(1,100) @@ -240,6 +304,7 @@ def getStatus(): hal_status = hal_status_list[0] return hal_status, hal_version +#@debugLog def doLogin(hostname, username, password): # simulate the login to our HAL system # 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 def main(): + global debugLogFilename getOptions() #showOptions() # some checks @@ -266,6 +332,7 @@ def main(): sys.stderr.write(f"No password given.\n") showUsage() sys.exit(1) + debugLogFilename = createDebugFilename(opt_hostname) success, code = doLogin(opt_hostname, opt_username, opt_password) if success: status, version = getStatus() diff --git a/mkp/hal9001-1.0.5.mkp b/mkp/hal9001-1.0.5.mkp new file mode 100644 index 0000000000000000000000000000000000000000..abffe9c992d9a19d4f1cdb6fd8526a7f45b528c2 GIT binary patch literal 9040 zcmb7~MNk}omPOGJ+@0XkXmEE8?(PuWT|)y$bGQ|4CF${#%+_g@ul+>SEo}gt+7#wjL;{?P&rQ4*3|)YzRb8;S|p_d z%A)KLJb(6i-!=9+ApfCy%O*=JfZij}@Pm^`ySQrsjwryJNa%TT9(H|atE3NVrEkd8 z>7N+T&91twiQ_k9@8eOa4)Xhf)E{jqyxx zr*v$Un+%9iDgw64@@MAKyi;FDPBgddIM#c_mnRR$bBG|Z{To2J?T0{6w{(P%+#OCdOAG6ig4q!%P$OMTyGs70u6=CB{)$ z;cyaq)GcZxBZP2X+}xko>SQ+CQ3rE}>44z_g3OB&BVNn#JK)4=Eap z9R}f>qb*dss=gf~rUieQ)IX(klg7H2@FEl?yyN8o8J^#_n7|B^4cYB?M<0b8vZISH zhj%lC=6N|V`OsFl7GUkRapNlHhauhx*nqX1D(&d$v9)oNLV=Uv$i}N{!(%Jx9{;kE z_OUr1jFIR@sAH65tn9`h45d=(Q!n3K<~s|k%kYB`2;CDlX#YVB!5I{QNasmsC8}43 z&ibo7B%%CLwtvhg;-@8EUpvdeevAv^%_IY3KMeR2`$S>@- zxx%x{m<+e_Gc?~>vHd{8((8WhFx?3HCk|v};%n|NyW2Hg#Zk;&A$Ti>Ol`%DXP(r} z?+Zbt(jiV3>DzN(yV^!Pf_+*R(>lBM-Km+&srKTimQ?uJnM#Y3DntCKM}%%M=&=rR zS1~gx+(R-Q!GwVlzN3ld@w|-bP!F`^#6oJ9H3^$(Zc?9m)S1Xt44E6P(`TF zjx${zxGQWjS*Gw^apl-0JkE5BVJ1H&*K^g0pOhvvrI)WMo8|{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