Plugins 0.1 - Surfaced setings

This commit is contained in:
Jokob-sk
2023-02-05 16:35:25 +11:00
parent da9ca8a1f4
commit 5ed142a6b8
2 changed files with 75 additions and 12 deletions

View File

@@ -18,6 +18,7 @@ from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText from email.mime.text import MIMEText
import sys import sys
from collections import namedtuple
import subprocess import subprocess
import os import os
import re import re
@@ -57,6 +58,7 @@ pialertPath = '/home/pi/pialert'
logPath = pialertPath + '/front/log' logPath = pialertPath + '/front/log'
confPath = "/config/pialert.conf" confPath = "/config/pialert.conf"
dbPath = '/db/pialert.db' dbPath = '/db/pialert.db'
pluginsPath = pialertPath + '/front/plugins'
fullConfPath = pialertPath + confPath fullConfPath = pialertPath + confPath
fullDbPath = pialertPath + dbPath fullDbPath = pialertPath + dbPath
@@ -399,18 +401,21 @@ def importConfig ():
API_RUN_INTERVAL = ccd('API_RUN_INTERVAL', 10 , c_d, 'API update interval', 'integer', '', 'API') API_RUN_INTERVAL = ccd('API_RUN_INTERVAL', 10 , c_d, 'API update interval', 'integer', '', 'API')
API_CUSTOM_SQL = ccd('API_CUSTOM_SQL', 'SELECT * FROM Devices WHERE dev_PresentLastScan = 0' , c_d, 'Custom endpoint', 'text', '', 'API') API_CUSTOM_SQL = ccd('API_CUSTOM_SQL', 'SELECT * FROM Devices WHERE dev_PresentLastScan = 0' , c_d, 'Custom endpoint', 'text', '', 'API')
# Insert settings into the DB #Plugins
sql.execute ("DELETE FROM Settings") plugins = get_plugins_configs()
sql.executemany ("""INSERT INTO Settings ("Code_Name", "Display_Name", "Description", "Type", "Options",
"RegEx", "Value", "Group", "Events" ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)""", mySettings)
# Used to determine the next import file_print('[', timeNow(), '] Plugins: Number of dynamically loaded plugins: ', len(plugins) )
lastTimeImported = time.time() for plugin in plugins:
file_print(' ---------------------------------------------')
file_print(' Name : ', plugin.display_name )
file_print(' Description: ', plugin.description.en_us )
# Used to display a message in the UI when old (outdated) settings are loaded prefix = plugin.settings_short_prefix
initOrSetParam("Back_Settings_Imported",(round(time.time() * 1000),) )
for set in plugin.settings:
codeName = prefix + "_" + set.type
ccd(codeName, set.default_value , c_d, set.name.en_us, get_setting_type(set), str(set.options), prefix)
commitDB()
# Update scheduler # Update scheduler
global tz, mySchedules global tz, mySchedules
@@ -436,6 +441,19 @@ def importConfig ():
# Format and prepare the list of subnets # Format and prepare the list of subnets
updateSubnets() updateSubnets()
# Insert settings into the DB
sql.execute ("DELETE FROM Settings")
sql.executemany ("""INSERT INTO Settings ("Code_Name", "Display_Name", "Description", "Type", "Options",
"RegEx", "Value", "Group", "Events" ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)""", mySettings)
# Used to determine the next import
lastTimeImported = time.time()
# Is used to display a message in the UI when old (outdated) settings are loaded
initOrSetParam("Back_Settings_Imported",(round(time.time() * 1000),) )
commitDB()
file_print('[', timeNow(), '] Config: Imported new config') file_print('[', timeNow(), '] Config: Imported new config')
#=============================================================================== #===============================================================================
@@ -3459,6 +3477,38 @@ def isNewVersion():
return newVersionAvailable return newVersionAvailable
#-------------------------------------------------------------------------------
# Plugins
#-------------------------------------------------------------------------------
def get_plugins_configs():
plugins = []
for root, dirs, files in os.walk(pluginsPath):
for d in dirs: # Loop over directories, not files
# filelist.append(os.path.join(root, d))
plugins.append(json.loads(get_file_content(pluginsPath + "/" + d + '/config.json'), object_hook=custom_plugin_decoder))
return plugins
#-------------------------------------------------------------------------------
def get_setting_type(setting):
if setting.type in ['RUN']:
return 'selecttext'
if setting.type in ['ENABLE', 'FORCE_REPORT']:
return 'boolean'
if setting.type in ['TIMEOUT', 'RUN_TIMEOUT']:
return 'integer'
if setting.type in ['NOTIFY_ON']:
return 'multiselect'
return 'text'
#-------------------------------------------------------------------------------
def custom_plugin_decoder(pluginDict):
return namedtuple('X', pluginDict.keys())(*pluginDict.values())
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
# Cron-like Scheduling # Cron-like Scheduling
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------

View File

@@ -26,6 +26,19 @@
} }
}, },
"settings":[ "settings":[
{
"type": "ENABLE",
"default_value":"False",
"options": [],
"name" : {
"en_us" : "Enable plugin"
},
"description":
{
"en_us" : "Enable a regular scan of your services. You need to enable this setting for anything to be executed regarding this plugin."
}
},
{ {
"type": "RUN", "type": "RUN",
"default_value":"none", "default_value":"none",
@@ -79,7 +92,7 @@
}, },
{ {
"type": "TIMEOUT", "type": "RUN_TIMEOUT",
"default_value":5, "default_value":5,
"options": [], "options": [],
"name" : { "name" : {