From 5a8be94cdcfc34e499c1f531ec94acec51562112 Mon Sep 17 00:00:00 2001 From: Jokob-sk Date: Thu, 9 Mar 2023 22:30:36 +1100 Subject: [PATCH] Plugins 0.4 - UI work + refactoring --- back/pialert.py | 14 ++-- docker-compose.yml | 4 +- front/php/server/dbHelper.php | 32 +++++++- front/plugins.php | 98 ++++++++++++++++++++--- front/plugins/nmap_services/config.json | 5 +- front/plugins/website_monitor/config.json | 3 +- 6 files changed, 131 insertions(+), 25 deletions(-) diff --git a/back/pialert.py b/back/pialert.py index d7d7354d..d3820155 100755 --- a/back/pialert.py +++ b/back/pialert.py @@ -465,13 +465,15 @@ def importConfigs (): # ----------------- plugins = get_plugins_configs() - mylog('none', ['[', timeNow(), '] Plugins: Number of dynamically loaded plugins: ', len(plugins.dict)]) + mylog('none', ['[', timeNow(), '] Plugins: Number of dynamically loaded plugins: ', len(plugins.list)]) # handle plugins for plugin in plugins.list: print_plugin_info(plugin, ['display_name','description']) - pref = plugin["unique_prefix"] + pref = plugin["unique_prefix"] + + # if plugin["enabled"] == 'true': # collect plugin level language strings collect_lang_strings(plugin, pref) @@ -3657,25 +3659,25 @@ def isNewVersion(): return newVersionAvailable +#------------------------------------------------------------------------------- #------------------------------------------------------------------------------- # Plugins #------------------------------------------------------------------------------- +#------------------------------------------------------------------------------- def get_plugins_configs(): pluginsDict = [] pluginsList = [] for root, dirs, files in os.walk(pluginsPath): - for d in dirs: # Loop over directories, not files - pluginsDict.append(json.loads(get_file_content(pluginsPath + "/" + d + '/config.json'), object_hook=custom_plugin_decoder)) + for d in dirs: # Loop over directories, not files pluginsList.append(json.loads(get_file_content(pluginsPath + "/" + d + '/config.json'))) return plugins_class(pluginsDict, pluginsList) #------------------------------------------------------------------------------- class plugins_class: - def __init__(self, dict, list): - self.dict = dict + def __init__(self, list): self.list = list #------------------------------------------------------------------------------- diff --git a/docker-compose.yml b/docker-compose.yml index 9ae32ef6..30e093c3 100755 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -7,9 +7,9 @@ services: network_mode: "host" restart: unless-stopped volumes: - - ${APP_DATA_LOCATION}/pialert/config2:/home/pi/pialert/config + - ${APP_DATA_LOCATION}/pialert/config:/home/pi/pialert/config # - ${APP_DATA_LOCATION}/pialert/db/pialert.db:/home/pi/pialert/db/pialert.db - - ${APP_DATA_LOCATION}/pialert/db2:/home/pi/pialert/db + - ${APP_DATA_LOCATION}/pialert/db:/home/pi/pialert/db # (optional) useful for debugging if you have issues setting up the container - ${LOGS_LOCATION}:/home/pi/pialert/front/log # DELETE START anyone trying to use this file: comment out / delete BELOW lines, they are only for development purposes diff --git a/front/php/server/dbHelper.php b/front/php/server/dbHelper.php index cea387cb..4e5bee47 100755 --- a/front/php/server/dbHelper.php +++ b/front/php/server/dbHelper.php @@ -67,7 +67,7 @@ case 'create': create($skipCache, $defaultValue, $expireMinutes, $dbtable, $columns, $values ); break; // case 'read' : read($skipCache, $defaultValue, $expireMinutes, $dbtable, $columns, $values); break; case 'update': update($key, $id, $skipCache, $defaultValue, $expireMinutes, $dbtable, $columns, $values); break; - // case 'delete': delete($skipCache, $defaultValue, $expireMinutes, $dbtable, $columns, $values); break; + case 'delete': delete($key, $id, $dbtable); break; default: logServerConsole ('Action: '. $action); break; } } @@ -80,6 +80,7 @@ function update($key, $id, $skipCache, $defaultValue, $expireMinutes, $dbtable, global $db; + // handle one or multiple columns if(strpos($columns, ',') !== false) { $columnsArr = explode(",", $columns); @@ -88,6 +89,7 @@ function update($key, $id, $skipCache, $defaultValue, $expireMinutes, $dbtable, $columnsArr = array($columns); } + // handle one or multiple values if(strpos($values, ',') !== false) { $valuesArr = explode(",", $values); @@ -145,7 +147,33 @@ function create($skipCache, $defaultValue, $expireMinutes, $dbtable, $columns, $ $result = $db->query($sql); if (! $result == TRUE) { - echo "Error creating etry\n\n$sql \n\n". $db->lastErrorMsg(); + echo "Error creating entry\n\n$sql \n\n". $db->lastErrorMsg(); + return; + } +} + +//------------------------------------------------------------------------------ +// delete +//------------------------------------------------------------------------------ +function delete($key, $id, $dbtable) +{ + global $db; + + // handle one or multiple ids + if(strpos($id, ',') !== false) + { + $idsArr = explode(",", $id); + }else + { + $idsArr = array($id); + } + + // Insert new value + $sql = 'DELETE FROM '.$dbtable.' WHERE "'.$key.'" IN ('. $id .')'; + $result = $db->query($sql); + + if (! $result == TRUE) { + echo "Error deleting entry\n\n$sql \n\n". $db->lastErrorMsg(); return; } } diff --git a/front/plugins.php b/front/plugins.php index 8454be2c..e0bf6fbe 100755 --- a/front/plugins.php +++ b/front/plugins.php @@ -49,8 +49,6 @@ function getFormControl(dbColumnDef, value, index) { break; case 'textboxsave': - console.log(value) - value = value == 'null' ? '' : value; // hide 'null' values id = `${dbColumnDef.column}_${index}` @@ -207,6 +205,8 @@ function generateTabs() $.each(pluginDefinitions, function(index, obj) { + + headersHtml = "" // headers = [] colDefinitions = [] @@ -235,7 +235,7 @@ function generateTabs() { clm += ''+ pluginUnprocessedEvents[i][colDefinitions[j].column] +'' } - evRows += '' + clm + '' + evRows += `${clm}` eveCount++; } } @@ -252,7 +252,7 @@ function generateTabs() { clm += ''+ pluginHistory[i][colDefinitions[j].column] +'' } - hiRows += '' + clm + '' + hiRows += `${clm}` histCount++; } } @@ -269,7 +269,7 @@ function generateTabs() { clm += ''+ getFormControl(colDefinitions[j], pluginObjects[i][colDefinitions[j].column], pluginObjects[i]["Index"]) +'' } - obRows += '' + clm + '' + obRows += `${clm}` obCount++; } } @@ -280,7 +280,7 @@ function generateTabs()
@@ -356,8 +366,72 @@ function generateTabs() activetab = '' // only first tab is active }); + + initTabs() + } +// -------------------------------------------------------- +var lastPrefix = '' +function initTabs() +{ + // events on tab change + $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) { + var target = $(e.target).attr("href") // activated tab + + // save the last prefix + if(target.includes('_') == false ) + { + lastPrefix = target.split('#')[1] + } + + // show the objectsTarget if no specific pane selected or if selected is + + if($('#objectsTarget_'+ lastPrefix).is(":hidden") && $('#eventsTarget_'+ lastPrefix).is(":hidden") && $('#historyTarget_'+ lastPrefix).is(":hidden")) + { + $('#objectsTarget_'+ lastPrefix).show(); + } else + { + $('#objectsTarget_'+ lastPrefix).hide(); + } + + }); +} +// -------------------------------------------------------- +plugPrefix = '' +dbTable = '' + +function purgeAll(callback) { + plugPrefix = arguments[0]; // plugin prefix + dbTable = arguments[1]; // DB table + // Ask + showModalWarning('' + ' ' + plugPrefix + ' ' + dbTable , '', + '', '', "purgeAllExecute"); +} + +// -------------------------------------------------------- +dbIndexes = '' + +function purgeAllExecute() { + + // Execute + // console.log("targetLogFile:" + targetLogFile) + // console.log("logFileAction:" + logFileAction) + + idArr = $('#NMAPSRV table[data-my-dbtable="Plugins_Objects"] tr[data-my-index]').map(function(){return $(this).attr("data-my-index");}).get(); + + $.ajax({ + method: "POST", + url: "php/server/dbHelper.php", + data: { action: "delete", dbtable: 'Plugins_Objects', key: 'Index', id:idArr.toString() }, + success: function(data, textStatus) { + showModalOk ('Result', data ); + } + }) + +} + + // ----------------------------------------------------------------------------- getData() diff --git a/front/plugins/nmap_services/config.json b/front/plugins/nmap_services/config.json index 1d6d32e5..bc58b17a 100755 --- a/front/plugins/nmap_services/config.json +++ b/front/plugins/nmap_services/config.json @@ -1,11 +1,12 @@ { "code_name": "nmap_services", - "unique_prefix": "NMAPSRV", + "unique_prefix": "NMAPSRV", + "enabled": true, "data_source": "pialert-db-query", "localized": ["display_name", "description", "icon"], "display_name" : [{ "language_code":"en_us", - "string" : "Services" + "string" : "Services (NMAP)" }], "icon":[{ "language_code":"en_us", diff --git a/front/plugins/website_monitor/config.json b/front/plugins/website_monitor/config.json index 4bcff216..3b940c80 100755 --- a/front/plugins/website_monitor/config.json +++ b/front/plugins/website_monitor/config.json @@ -1,6 +1,7 @@ { "code_name": "website_monitor", - "unique_prefix": "WEBMON", + "unique_prefix": "WEBMON", + "enabled": true, "data_source": "python-script", "localized": ["display_name", "description", "icon"], "display_name" : [{