From c14c762bdec266e7f38b8d636663ade1be55dad2 Mon Sep 17 00:00:00 2001 From: Data-Monkey Date: Thu, 25 May 2023 19:51:03 +1000 Subject: [PATCH] cleanup --- db/pialert.db | Bin 262144 -> 245760 bytes pialert/conf.py | 4 +- pialert/helper.py | 7 +- pialert/pialert.py | 173 +++++++++++-------------------------------- pialert/reporting.py | 71 +++++++++++++++++- 5 files changed, 119 insertions(+), 136 deletions(-) diff --git a/db/pialert.db b/db/pialert.db index 2e998bb24810d810e43ce4dc1c1ba7efcbb8a2cf..50a882253f85487ce8647ac016428d647dc9f395 100755 GIT binary patch delta 410 zcmZo@5NK%NpCB#xo`HeE28f}6wPK=`#fU-)sWDU%K zvWnPc&8$p}fwBrfSw;?VREsUFOo7rsi#NxesTF2oXqkNXz7nI#rU%>wtY-Yb`M&`j z{gPkUGz#cL6H{LPhYU>o(-`=t@oxi4bnz?5F*0kaJ0&J($2%4j#0QromZSz0H%UKMV1(vfUFgpmba|bf;b@0XVTJv)Ah;rZM Q4g_kj=H4E-nt8nl02@1CD*ylh delta 1880 zcmYk63v3kE6^3Wdnc1CvjtLmsF}Bwj+xUes#s-_0?ckDto!CH1O@Q^r4~z|d#Jk|P z_v``=kD3@4juD1bO0cCmkT_A9RwxQ;Q>9hYszIm`X?ZCXNhp<)KB5$&sQ0#!nvv#8 zckZ2Yzw`g+o^aR)oatqb_v-PjOgDLJ^f+7<{lv@|sy)W~Cf-iA5=x%ta|m<8lwUpo!}Ir&^WW&k<(-FDmvWU$$R znLu`4(Sa2}Hty@dZ9qC->%b*e?F_y?Z=*CTRh?2i|ML9OGwy!qzUJz5RXCF!pE!;= zsvQ|dt+B-Z7yD1`DYl2U&C*7(RzUw7eye_&dC4b!`GhM-1y;uNL%+K}WBA_BG+>50`isgKzPhuDH z$9)oe8E5oM>;fL@#~h%g{1Y*^Q@&8BvV>pn$7D_)z(k-WTs|OG&f}2*DNHWU4&Yv( z98Mm@MFwRHqz&ZfKMxA$z6oLoD2qQF#5|zItPkOnK$-mX5H1AD;I1LO29(b0hoz&7 zxOEs;1EulwFxKI6TF9I0vDlzg5%Z~NFTWAM1Z&4dR8`C&AI}72)D-JR0R6(vWWidP z$hQL+`&~CFmXbKofTw{L@K+7E1SpX+8>K9dTXl_Shf?WJf)Y?XUwPj1On4Oc4cB2; zowL|!a|}5)IFjx6?Z2|`w^!>`dMYNsr|@$~RUay&N??pKF`KsV<`F6PW@~r^_Xv4w z`Lh>gmDF&|5j+L-3{M@wJP$oB5Z9!6n%e!LZk|0N6x!5?ZihzIBE!m&N{%^>ar~!o zthPFi;#m=?ie1Ms8)%bNd>p?KA2xE)sC4cr-Z_fZKu_|_sI+#2^@mYhs$vFxkFiVc zsN|e3ECyQ7&v)S=peOj#F3bn2u;O3%?|mrel5UAzW;J(XjflODt)RqS%eR6OdkqIe z5_|P`v4=zd$6oa<_N@aLXMG;RPtZ*6C z&JUgB^gG^nyyD0-9vL-8hP};RuCLbJ+AVE^mI@!h1vRQ}QNB?iqL}vagR@eKy__(K`3AL#Jx`33bM1MY|35$8oWwICNGn&Jmu0_) z_n*g~0kv@c%W}S(>t4ob%%Wzl4@<+E_*fYGfEw9GvO6_!KH*D1&+}D6A5ehr5^e>m z=i-Q5_p?8OJArob+Y!t%Xs09)Q79@1Fp51uJ9r=}w?4-gqF4pAo&O%iG@v?b{z1$W za6jPnhonr~xa|;@0DYe?9Fm>zS=J8A?6z73hlNbJ(xb<8r{bOP2E7&Dc=ue_b>| 0 and rows[0]['par_Value'] != 'finished': - event = rows[0]['par_Value'].split('|')[0] - param = rows[0]['par_Value'].split('|')[1] - else: - return - - if event == 'test': - handle_test(param) - if event == 'run': - handle_run(param) - - # clear event execution flag - sql.execute ("UPDATE Parameters SET par_Value='finished' WHERE par_ID='Front_Event'") - - # commit to DB - db.commitDB() - -#------------------------------------------------------------------------------- -def handle_run(runType): - global last_network_scan - - mylog('info', ['[', timeNow(), '] START Run: ', runType]) - - if runType == 'ENABLE_ARPSCAN': - last_network_scan = now_minus_24h - - mylog('info', ['[', timeNow(), '] END Run: ', runType]) - -#------------------------------------------------------------------------------- -def handle_test(testType): - - mylog('info', ['[', timeNow(), '] START Test: ', testType]) - - # Open text sample - sample_txt = get_file_content(pialertPath + '/back/report_sample.txt') - - # Open html sample - sample_html = get_file_content(pialertPath + '/back/report_sample.html') - - # Open json sample and get only the payload part - sample_json_payload = json.loads(get_file_content(pialertPath + '/back/webhook_json_sample.json'))[0]["body"]["attachments"][0]["text"] - - if testType == 'REPORT_MAIL': - send_email(sample_txt, sample_html) - if testType == 'REPORT_WEBHOOK': - send_webhook (sample_json_payload, sample_txt) - if testType == 'REPORT_APPRISE': - send_apprise (sample_html, sample_txt) - if testType == 'REPORT_NTFY': - send_ntfy (sample_txt) - if testType == 'REPORT_PUSHSAFER': - send_pushsafer (sample_txt) - - mylog('info', ['[', timeNow(), '] END Test: ', testType]) - - - - - - diff --git a/pialert/reporting.py b/pialert/reporting.py index 3df6fa18..4b6de43c 100644 --- a/pialert/reporting.py +++ b/pialert/reporting.py @@ -14,7 +14,7 @@ from json2table import convert import conf from const import pialertPath, logPath from database import get_table_as_json -from files import write_file +from files import get_file_content, write_file from helper import generate_mac_links, isNewVersion, removeDuplicateNewLines, timeNow, hide_email, json_struc, updateState from logger import logResult, mylog, print_log from mqtt import mqtt_start @@ -636,4 +636,71 @@ def skip_repeated_notifications (db): """ ) print_log ('Skip Repeated end') - db.commitDB() \ No newline at end of file + db.commitDB() + + +#=============================================================================== +# UTIL +#=============================================================================== + +#------------------------------------------------------------------------------- +def check_and_run_event(db): + sql = db.sql # TO-DO + sql.execute(""" select * from Parameters where par_ID = "Front_Event" """) + rows = sql.fetchall() + + event, param = ['',''] + if len(rows) > 0 and rows[0]['par_Value'] != 'finished': + event = rows[0]['par_Value'].split('|')[0] + param = rows[0]['par_Value'].split('|')[1] + else: + return + + if event == 'test': + handle_test(param) + if event == 'run': + handle_run(param) + + # clear event execution flag + sql.execute ("UPDATE Parameters SET par_Value='finished' WHERE par_ID='Front_Event'") + + # commit to DB + db.commitDB() + +#------------------------------------------------------------------------------- +def handle_run(runType): + global last_network_scan + + mylog('info', ['[', timeNow(), '] START Run: ', runType]) + + if runType == 'ENABLE_ARPSCAN': + last_network_scan = conf.time_started - datetime.timedelta(hours = 24) + + mylog('info', ['[', timeNow(), '] END Run: ', runType]) + +#------------------------------------------------------------------------------- +def handle_test(testType): + + mylog('info', ['[', timeNow(), '] START Test: ', testType]) + + # Open text sample + sample_txt = get_file_content(pialertPath + '/back/report_sample.txt') + + # Open html sample + sample_html = get_file_content(pialertPath + '/back/report_sample.html') + + # Open json sample and get only the payload part + sample_json_payload = json.loads(get_file_content(pialertPath + '/back/webhook_json_sample.json'))[0]["body"]["attachments"][0]["text"] + + if testType == 'REPORT_MAIL': + send_email(sample_txt, sample_html) + if testType == 'REPORT_WEBHOOK': + send_webhook (sample_json_payload, sample_txt) + if testType == 'REPORT_APPRISE': + send_apprise (sample_html, sample_txt) + if testType == 'REPORT_NTFY': + send_ntfy (sample_txt) + if testType == 'REPORT_PUSHSAFER': + send_pushsafer (sample_txt) + + mylog('info', ['[', timeNow(), '] END Test: ', testType]) \ No newline at end of file