ticket_ai_flask_app/install.sh
2024-02-21 19:59:37 +05:00

74 lines
1.9 KiB
Bash

#!/bin/bash
# Update System Packages
sudo yum update -y
# Install Python3 and Pip
sudo yum install -y python3 python3-pip
# Install Flask
pip3 install Flask
# Create a directory for the Flask application
mkdir -p ~/flask_app
cd ~/flask_app
# Create the Flask application file (app.py)
cat > app.py << 'EOF'
from flask import Flask, request, jsonify
import json
import subprocess
app = Flask(__name__)
def read_log(log_file, lines):
return subprocess.check_output(['tail', '-n', str(lines), log_file]).decode('utf-8')
def read_top():
return subprocess.check_output(['top', '-b', '-n', '1']).decode('utf-8')
@app.route('/get_logs', methods=['GET'])
def get_logs():
issue_type = request.args.get('issue_type')
response = {}
try:
with open('config.json') as config_file:
config = json.load(config_file)
if issue_type in config:
issue_config = config[issue_type]
for log in issue_config['logs']:
log_output = read_log(log['log_file'], log['lines'])
response[log['log_file']] = log_output
if issue_config.get('include_top'):
response['top'] = read_top()
else:
response = {"error": "Invalid issue type"}
except Exception as e:
response = {"error": str(e)}
return jsonify(response)
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0')
EOF
# Create the configuration file (config.json)
cat > config.json << 'EOF'
{
"database_issue": {
"include_top": true,
"logs": [
{ "log_file": "/var/log/db.log", "lines": 50 },
{ "log_file": "/var/log/db_error.log", "lines": 30 }
]
},
"network_issue": {
"include_top": false,
"logs": [
{ "log_file": "/var/log/network.log", "lines": 100 }
]
}
}
EOF
echo "Installation complete. Please modify config.json as needed."