added custom.json option and introduced placeholdr

This commit is contained in:
Zulqarnain 2024-11-07 01:24:26 -05:00
parent be168c03ba
commit d6e72f638c
3 changed files with 233 additions and 12 deletions

View File

@ -40,30 +40,71 @@ def login():
access_token = create_access_token(identity=username) access_token = create_access_token(identity=username)
return jsonify(access_token=access_token) return jsonify(access_token=access_token)
# Function to replace placeholders in the issue configuration
def replace_placeholders(issue_config, paths):
for log in issue_config.get('logs', []):
log_file = log.get('log_file')
if log_file:
for placeholder, actual_path in paths.items():
log['log_file'] = log['log_file'].replace(f"{{{placeholder}}}", actual_path)
for command in issue_config.get('commands', []):
comm = command.get('comm')
if comm:
for placeholder, actual_path in paths.items():
command['comm'] = command['comm'].replace(f"{{{placeholder}}}", actual_path)
# Function to merge custom config into main config
def merge_configs(main_config, custom_config):
for key, value in custom_config.items():
if key in main_config:
# Merge commands and logs if issue type already exists
main_config[key]['commands'].extend(value.get('commands', []))
main_config[key]['logs'].extend(value.get('logs', []))
else:
# Add new issue type if it doesn't exist
main_config[key] = value
@app.route('/get_logs', methods=['GET']) @app.route('/get_logs', methods=['GET'])
def get_logs(): def get_logs():
issue_type = request.args.get("issue_type") issue_type = request.args.get("issue_type")
try: try:
# Load config_paths.json for dynamic paths
with open('/opt/ticket-ai/src/config_paths.json') as paths_file:
paths = json.load(paths_file)
# Load main config.json
with open('/opt/ticket-ai/src/config.json') as config_file: with open('/opt/ticket-ai/src/config.json') as config_file:
config = json.load(config_file) config = json.load(config_file)
if issue_type not in config:
return jsonify({"error": "Invalid issue type"})
issue_config = config[issue_type]
# response = {log.get('log_file').split('*')[0]: read_log_from_dir(*split_directory_and_file(log.get('log_file')), log['lines']) if '*' in log.get('log_file') else read_log(log['log_file'], log['lines']) for log in issue_config['logs']}
# response.update({comm.get('tag'): os.popen(comm.get('comm')).read() for comm in issue_config['commands']})
response = { # Check for custom.json and merge if it exists
log.get('log_file').split('*')[0]: f"```\n{read_log_from_dir(*split_directory_and_file(log.get('log_file')), log['lines'])}\n```" if '*' in log.get('log_file') else f"```\n{read_log(log['log_file'], log['lines'])}\n```" custom_config_path = '/opt/ticket-ai/src/custom.json'
for log in issue_config['logs'] if os.path.exists(custom_config_path):
} with open(custom_config_path) as custom_file:
response.update({ custom_config = json.load(custom_file)
comm.get('tag'): f"```\n{os.popen(comm.get('comm')).read()}\n```" for comm in issue_config['commands'] merge_configs(config, custom_config)
})
if issue_type not in config:
return jsonify({"error": "Invalid issue type"})
# Get the specific configuration for the issue type
issue_config = config[issue_type]
# Replace placeholders in logs and commands
replace_placeholders(issue_config, paths)
# Process logs and commands
response = {
log.get('log_file').split('*')[0]: f"```\n{read_log_from_dir(*split_directory_and_file(log.get('log_file')), log['lines'])}\n```" if '*' in log.get('log_file') else f"```\n{read_log(log['log_file'], log['lines'])}\n```"
for log in issue_config['logs']
}
response.update({
comm.get('tag'): f"```\n{os.popen(comm.get('comm')).read()}\n```" for comm in issue_config['commands']
})
except Exception as e: except Exception as e:
return jsonify({"error": str(e)}) return jsonify({"error": str(e)})
return jsonify(response) return jsonify(response)
if __name__ == '__main__': if __name__ == '__main__':
app.run(debug=True, host="0.0.0.0") app.run(debug=True, host="0.0.0.0")

175
src/config.json Normal file
View File

@ -0,0 +1,175 @@
{
"APEX Upgrade Request": {
"commands": [
{
"comm": "df -h | head -n 1 && df -h | grep '^/dev/sd'",
"tag": "disk_usage"
},
{
"comm": "/opt/ticket-ai/src/scripts/tablespaceUsage.sh;sleep 5;cat /tmp/tablespace_usage.tmp",
"tag": "tablespace_usage"
},
{
"comm": "/opt/ticket-ai/src/scripts/apexUsage.sh;sleep 5;cat /tmp/apex_usage.tmp",
"tag": "apex_usage"
}
],
"logs": [
{
"log_file": "{oracle_diag_log}",
"lines": 10
}
]
},
"Server Performance Problem": {
"commands": [
{
"comm": "top -b -n 1",
"tag": "top"
},
{
"comm": "iostat -x 1 3",
"tag": "iostat"
},
{
"comm": "df -h | head -n 1 && df -h | grep '^/dev/sd'",
"tag": "df"
},
{
"comm": "/opt/ticket-ai/src/scripts/processfinder.sh;sleep 5;cat /tmp/findsql.tmp",
"tag": "processfinder"
},
{
"comm": "/opt/ticket-ai/src/scripts/tablespaceUsage.sh;sleep 5;cat /tmp/tablespace_usage.tmp",
"tag": "tablespaceusage"
},
{
"comm": "cat /opt/ords/config/databases/default/pool.xml",
"tag": "ordsconfig"
},
{
"comm": "echo Current Date and time on server: ; date",
"tag": "datetime"
},
{
"comm": "find {virtualmin_error_log} -type f -name '*error_log*' -mmin -15 -exec sh -c 'echo \"===== {} =====\"; tail -n 50 {}' \\;",
"tag": "apacheerrorlogs"
}
],
"logs": [
{
"log_file": "{tomcat_catalina_log}",
"lines": 150
},
{
"log_file": "{oracle_diag_log}",
"lines": 50
},
{
"log_file": "/var/log/messages",
"lines": 25
}
]
},
"Server Unavailable": {
"commands": [
{
"comm": "top -b -n 1",
"tag": "top"
},
{
"comm": "iostat -x 1 3",
"tag": "iostat"
},
{
"comm": "df -h | head -n 1 && df -h | grep '^/dev/sd'",
"tag": "df"
},
{
"comm": "/opt/ticket-ai/src/scripts/processfinder.sh;sleep 5;cat /tmp/findsql.tmp",
"tag": "processfinder"
},
{
"comm": "/opt/ticket-ai/src/scripts/tablespaceUsage.sh;sleep 5;cat /tmp/tablespace_usage.tmp",
"tag": "tablespaceusage"
},
{
"comm": "cat /opt/ords/config/databases/default/pool.xml",
"tag": "ordsconfig"
},
{
"comm": "echo Current Date and time on server: ; date",
"tag": "datetime"
},
{
"comm": "find {virtualmin_error_log} -type f -name '*error_log*' -mmin -15 -exec sh -c 'echo \"===== {} =====\"; tail -n 50 {}' \\;",
"tag": "apacheerrorlogs"
}
],
"logs": [
{
"log_file": "{tomcat_catalina_log}",
"lines": 150
},
{
"log_file": "{oracle_diag_log}",
"lines": 50
},
{
"log_file": "/var/log/messages",
"lines": 25
}
]
},
"Email Problem": {
"commands": [
{
"comm": "timeout 5 bash -c 'echo > /dev/tcp/relay.maxapex.net/2525' && echo 'Relay Connected' || echo 'Relay Connection failed'",
"tag": "relay_connection"
},
{
"comm": "df -h",
"tag": "df"
}
],
"logs": [
{
"log_file": "/var/log/messages",
"lines": 150
}
]
},
"Password Problem": {
"commands": [
{
"comm": "for jail in $(sudo fail2ban-client status | grep \"Jail list\" | cut -d ':' -f2 | tr ',' ' '); do echo \"$jail:\"; sudo fail2ban-client status \"$jail\" | grep \"Banned IP list\"; done",
"tag": "banned_ips"
}
],
"logs": [
{
"log_file": "/var/log/secure",
"lines": 150
}
]
},
"Wallet Problem": {
"commands": [
{
"comm": "output=$(su - oracle -s /bin/bash -c 'orapki wallet display -wallet /home/oracle/wallet/') && echo \"$output\"",
"tag": "wallet_certs"
}
],
"logs": [
{
"log_file": "/var/log/secure",
"lines": 1
}
]
}
}

5
src/config_paths.json Normal file
View File

@ -0,0 +1,5 @@
{
"oracle_diag_log": "/opt/oracle/diag/rdbms/xe/XE/trace/alert_XE.log",
"virtualmin_error_log": "/var/log/virtualmin/error_log",
"tomcat_catalina_log": "/opt/tomcat/logs/catalina.out"
}