mirror of
https://github.com/jokob-sk/NetAlertX.git
synced 2025-12-07 09:36:05 -08:00
more cleanup and removed files.py module again
This commit is contained in:
@@ -3,33 +3,27 @@ import json
|
||||
|
||||
# pialert modules
|
||||
import conf
|
||||
from const import pialertPath
|
||||
from const import (apiPath, sql_devices_all, sql_nmap_scan_all, sql_pholus_scan_all, sql_events_pending_alert,
|
||||
sql_settings, sql_plugins_events, sql_plugins_history, sql_plugins_objects,sql_language_strings)
|
||||
from logger import mylog
|
||||
from files import write_file
|
||||
from database import *
|
||||
from helper import write_file
|
||||
|
||||
apiEndpoints = []
|
||||
|
||||
#===============================================================================
|
||||
# API
|
||||
#===============================================================================
|
||||
def update_api(isNotification = False, updateOnlyDataSources = []):
|
||||
mylog('verbose', [' [API] Update API not doing anything for now !'])
|
||||
return
|
||||
def update_api(db, isNotification = False, updateOnlyDataSources = []):
|
||||
mylog('verbose', ['[API] Update API starting'])
|
||||
# return
|
||||
|
||||
folder = pialertPath + '/front/api/'
|
||||
folder = apiPath
|
||||
|
||||
if isNotification:
|
||||
# Update last notification alert in all formats
|
||||
mylog('verbose', [' [API] Updating notification_* files in /front/api'])
|
||||
|
||||
write_file(folder + 'notification_text.txt' , mail_text)
|
||||
write_file(folder + 'notification_text.html' , mail_html)
|
||||
write_file(folder + 'notification_json_final.json' , json.dumps(json_final))
|
||||
# update notifications moved to reporting send_api()
|
||||
|
||||
# Save plugins
|
||||
if conf.ENABLE_PLUGINS:
|
||||
write_file(folder + 'plugins.json' , json.dumps({"data" : plugins}))
|
||||
write_file(folder + 'plugins.json' , json.dumps({"data" : conf.plugins}))
|
||||
|
||||
# prepare database tables we want to expose
|
||||
dataSourcesSQLs = [
|
||||
@@ -50,19 +44,19 @@ def update_api(isNotification = False, updateOnlyDataSources = []):
|
||||
|
||||
if updateOnlyDataSources == [] or dsSQL[0] in updateOnlyDataSources:
|
||||
|
||||
api_endpoint_class(dsSQL[1], folder + 'table_' + dsSQL[0] + '.json')
|
||||
api_endpoint_class(db, dsSQL[1], folder + 'table_' + dsSQL[0] + '.json')
|
||||
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
|
||||
|
||||
class api_endpoint_class:
|
||||
def __init__(self, db, path):
|
||||
def __init__(self, db, query, path):
|
||||
|
||||
global apiEndpoints
|
||||
self.db = db
|
||||
self.sql = db.sql
|
||||
self.jsonData = db.get_table_as_json( self.sql).json
|
||||
self.query = query
|
||||
self.jsonData = db.get_table_as_json(self.query).json
|
||||
self.path = path
|
||||
self.fileName = path.split('/')[-1]
|
||||
self.hash = hash(json.dumps(self.jsonData))
|
||||
@@ -76,7 +70,7 @@ class api_endpoint_class:
|
||||
# search previous endpoint states to check if API needs updating
|
||||
for endpoint in apiEndpoints:
|
||||
# match sql and API endpoint path
|
||||
if endpoint.sql == self.sql and endpoint.path == self.path:
|
||||
if endpoint.query == self.query and endpoint.path == self.path:
|
||||
found = True
|
||||
if endpoint.hash != self.hash:
|
||||
changed = True
|
||||
@@ -87,7 +81,7 @@ class api_endpoint_class:
|
||||
# cehck if API endpoints have changed or if it's a new one
|
||||
if not found or changed:
|
||||
|
||||
mylog('verbose', [f' [API] Updating {self.fileName} file in /front/api'])
|
||||
mylog('verbose', [f'[API] Updating {self.fileName} file in /front/api'])
|
||||
|
||||
write_file(self.path, json.dumps(self.jsonData))
|
||||
|
||||
@@ -98,5 +92,5 @@ class api_endpoint_class:
|
||||
# update hash
|
||||
apiEndpoints[changedIndex].hash = self.hash
|
||||
else:
|
||||
mylog('info', [f' [API] ERROR Updating {self.fileName}'])
|
||||
mylog('info', [f'[API] ERROR Updating {self.fileName}'])
|
||||
|
||||
|
||||
Reference in New Issue
Block a user