Plugins code cleanup 0.1

This commit is contained in:
Jokob-sk
2023-09-01 08:04:14 +10:00
parent 015ce23fe2
commit 9afed6e43b
6 changed files with 85 additions and 199 deletions

View File

@@ -16,17 +16,24 @@ import pwd
import os
from dhcp_leases import DhcpLeases
curPath = str(pathlib.Path(__file__).parent.resolve())
log_file = curPath + '/script.log'
last_run = curPath + '/last_result.log'
sys.path.append("/home/pi/pialert/front/plugins")
sys.path.append('/home/pi/pialert/pialert')
print(last_run)
from plugin_helper import Plugin_Object, Plugin_Objects
from logger import mylog, append_line_to_file
from helper import timeNowTZ
from const import logPath, pialertPath
CUR_PATH = str(pathlib.Path(__file__).parent.resolve())
LOG_FILE = os.path.join(CUR_PATH, 'script.log')
RESULT_FILE = os.path.join(CUR_PATH, 'last_result.log')
# Workflow
def main():
last_run_logfile = open(last_run, 'a')
mylog('verbose',['[DHCPLSS] In script'])
last_run_logfile = open(RESULT_FILE, 'a')
# empty file
last_run_logfile.write("")
@@ -35,48 +42,26 @@ def main():
parser.add_argument('paths', action="store", help="absolute dhcp.leases file paths to check separated by ','")
values = parser.parse_args()
# parse output
newEntries = []
# Init the file
plug_objects = Plugin_Objects( RESULT_FILE )
# parse output
if values.paths:
for path in values.paths.split('=')[1].split(','):
for path in values.paths.split('=')[1].split(','):
newEntries = get_entries(newEntries, path)
plug_objects_tmp = get_entries(path, plug_objects)
for e in newEntries:
# Insert list into the log
service_monitoring_log(e.primaryId, e.secondaryId, e.created, e.watched1, e.watched2, e.watched3, e.watched4, e.extra, e.foreignKey )
mylog('verbose',[f'[DHCPLSS] {len(plug_objects_tmp)} Entries found in "{path}"'])
plug_objects = plug_objects + plug_objects_tmp
plug_objects.write_result_file()
# -----------------------------------------------------------------------------
def service_monitoring_log(primaryId, secondaryId, created, watched1, watched2 = '', watched3 = '', watched4 = '', extra ='', foreignKey ='' ):
if watched1 == '':
watched1 = 'null'
if watched2 == '':
watched2 = 'null'
if watched3 == '':
watched3 = 'null'
if watched4 == '':
watched4 = 'null'
with open(last_run, 'a') as last_run_logfile:
# https://www.duckduckgo.com|192.168.0.1|2023-01-02 15:56:30|200|0.9898|null|null|Best search engine|null
last_run_logfile.write("{}|{}|{}|{}|{}|{}|{}|{}|{}\n".format(
primaryId,
secondaryId,
created,
watched1,
watched2,
watched3,
watched4,
extra,
foreignKey
)
)
# -----------------------------------------------------------------------------
def get_entries(newEntries, path):
def get_entries(path, plug_objects):
# PiHole dhcp.leases format
if 'pihole' in path:
@@ -88,8 +73,17 @@ def get_entries(newEntries, path):
row = line.rstrip().split()
# rows: DHCP_DateTime, DHCP_MAC, DHCP_IP, DHCP_Name, DHCP_MAC2
if len(row) == 5 :
tmpPlugObj = plugin_object_class(row[1], row[2], 'True', row[3], row[4], 'True', path)
newEntries.append(tmpPlugObj)
plug_objects.add_object(
primaryId = row[1],
secondaryId = row[2],
watched1 = 'True',
watched2 = row[3],
watched3 = row[4],
watched4 = 'True',
extra = path,
foreignKey = row[1]
)
# Generic dhcp.leases format
else:
@@ -97,28 +91,19 @@ def get_entries(newEntries, path):
leasesList = leases.get()
for lease in leasesList:
plug_objects.add_object(
primaryId = lease.ethernet,
secondaryId = lease.ip,
watched1 = lease.active,
watched2 = lease.hostname,
watched3 = lease.hardware,
watched4 = lease.binding_state,
extra = path,
foreignKey = lease.ethernet
)
tmpPlugObj = plugin_object_class(lease.ethernet, lease.ip, lease.active, lease.hostname, lease.hardware, lease.binding_state, path)
newEntries.append(tmpPlugObj)
return plug_objects
return newEntries
# -------------------------------------------------------------------
class plugin_object_class:
def __init__(self, primaryId = '',secondaryId = '', watched1 = '',watched2 = '',watched3 = '',watched4 = '',extra = '',foreignKey = ''):
self.pluginPref = ''
self.primaryId = primaryId
self.secondaryId = secondaryId
self.created = strftime("%Y-%m-%d %H:%M:%S")
self.changed = ''
self.watched1 = watched1
self.watched2 = watched2
self.watched3 = watched3
self.watched4 = watched4
self.status = ''
self.extra = extra
self.userData = ''
self.foreignKey = foreignKey
#===============================================================================
# BEGIN