first update
This commit is contained in:
parent
394ca9fa12
commit
3b14fbf0b3
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
.vscode/
|
||||
playground/
|
||||
venv/
|
||||
29
Dockerfile
29
Dockerfile
@ -1,29 +0,0 @@
|
||||
# docker build --rm -t docker.io/mavenarh/ticket_ai:latest .
|
||||
|
||||
# Use the official Python image as a base
|
||||
FROM python:3.9-slim
|
||||
|
||||
# Set environment variables
|
||||
#ENV PYTHONDONTWRITEBYTECODE 1
|
||||
#ENV PYTHONUNBUFFERED 1
|
||||
|
||||
# Set the working directory in the container
|
||||
WORKDIR /app
|
||||
|
||||
# Copy the dependencies file to the working directory
|
||||
#COPY requirements.txt .
|
||||
|
||||
# Install any dependencies
|
||||
#RUN pip install --no-cache-dir -r requirements.txt
|
||||
|
||||
RUN pip install Flask
|
||||
RUN pip install flask_jwt_extended
|
||||
|
||||
# Copy the content of the local src directory to the working directory
|
||||
COPY src/ .
|
||||
|
||||
# Expose the port the app runs on
|
||||
EXPOSE 5000
|
||||
|
||||
# Run the application
|
||||
CMD ["python", "app.py"]
|
||||
@ -1,10 +0,0 @@
|
||||
version: '3'
|
||||
|
||||
services:
|
||||
flask:
|
||||
image: ticket_ai docker.io/mavenarh/ticket_ai:latest
|
||||
ports:
|
||||
- "5000:5000"
|
||||
volumes:
|
||||
- ./src/config.json:/app/config.json
|
||||
- ./logs:/app/logs
|
||||
82
install.sh
82
install.sh
@ -1,73 +1,13 @@
|
||||
#!/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."
|
||||
if ! command -v python3 &> /dev/null
|
||||
then
|
||||
echo "Error: python3 could not be found"
|
||||
exit
|
||||
else
|
||||
python3 -m venv venv
|
||||
source venv/bin/activate
|
||||
pip install Flask
|
||||
pip install flask_jwt_extended
|
||||
chmod +x run.sh
|
||||
fi
|
||||
|
||||
1
logs/a_error.log
Normal file
1
logs/a_error.log
Normal file
@ -0,0 +1 @@
|
||||
this is a error log from file 'A'
|
||||
1
logs/b_error.log
Normal file
1
logs/b_error.log
Normal file
@ -0,0 +1 @@
|
||||
this is a error log from file 'B'
|
||||
@ -1 +0,0 @@
|
||||
this is a error log
|
||||
31
readme.md
31
readme.md
@ -6,7 +6,7 @@ pip install flask_jwt_extended
|
||||
python app.py
|
||||
|
||||
# call api
|
||||
http://127.0.0.1:5000/get_logs?issue_type=database_issue
|
||||
http://127.0.0.1:5000/get_logs?issue_type=Demo
|
||||
|
||||
# sample response
|
||||
{
|
||||
@ -16,30 +16,7 @@ http://127.0.0.1:5000/get_logs?issue_type=database_issue
|
||||
}
|
||||
|
||||
# run installation script
|
||||
chmod +x install_flask_app.sh
|
||||
./install_flask_app.sh
|
||||
cd ~/flask_app
|
||||
python3 app.py
|
||||
./install.sh
|
||||
|
||||
# install on centos
|
||||
yum install python36-devel
|
||||
yum install python36-setuptools
|
||||
yum install python36-virtualenv
|
||||
|
||||
python3 -m pip install --upgrade pip
|
||||
|
||||
python3 -m virtualenv env
|
||||
|
||||
pip install Flask
|
||||
pip install flask_jwt_extended
|
||||
|
||||
# usage
|
||||
curl http://127.0.0.1:5000/get_logs?issue_type=Demo
|
||||
|
||||
# run docker image
|
||||
docker run -d \
|
||||
-p 5000:5000 \
|
||||
-v $(pwd)/src/config.json:/app/config.json \
|
||||
-v $(pwd)/logs:/app/logs \
|
||||
--name ticket_ai \
|
||||
docker.io/mavenarh/ticket_ai:latest
|
||||
# run application
|
||||
./run.sh
|
||||
|
||||
@ -1,12 +0,0 @@
|
||||
click==8.0.4
|
||||
dataclasses==0.8
|
||||
Flask==2.0.3
|
||||
Flask-JWT-Extended==4.4.2
|
||||
importlib-metadata==4.8.3
|
||||
itsdangerous==2.0.1
|
||||
Jinja2==3.0.3
|
||||
MarkupSafe==2.0.1
|
||||
PyJWT==2.4.0
|
||||
typing_extensions==4.1.1
|
||||
Werkzeug==2.0.3
|
||||
zipp==3.6.0
|
||||
4
run.sh
Normal file
4
run.sh
Normal file
@ -0,0 +1,4 @@
|
||||
#!/bin/bash
|
||||
|
||||
source venv/bin/activate
|
||||
python3 src/app.py
|
||||
14
src/app.py
14
src/app.py
@ -78,7 +78,7 @@ def get_logs():
|
||||
issue_type = request.args.get("issue_type")
|
||||
response = {}
|
||||
try:
|
||||
with open('config.json') as config_file:
|
||||
with open('./src/config.json') as config_file:
|
||||
config = json.load(config_file)
|
||||
if issue_type in config:
|
||||
issue_config = config[issue_type]
|
||||
@ -86,17 +86,23 @@ def get_logs():
|
||||
# logs
|
||||
for log in issue_config['logs']:
|
||||
|
||||
if '*' in log.get('log_file'):
|
||||
directory, file_name = split_directory_and_file(log.get('log_file'))
|
||||
|
||||
if '*' in log.get('log_file'):
|
||||
# directory, file_name = split_directory_and_file(log.get('log_file'))
|
||||
log_output = read_log_from_dir(directory, file_name, log['lines'])
|
||||
else:
|
||||
log_output = read_log(log['log_file'], log['lines'])
|
||||
|
||||
response[log['log_file']] = log_output
|
||||
# response[log['log_file']] = log_output
|
||||
response[file_name] = log_output
|
||||
|
||||
# commands
|
||||
for comm in issue_config['commands']:
|
||||
response[comm.get('tag')] = subprocess.check_output([comm.get('comm')]).decode('utf-8')
|
||||
# response[comm.get('tag')] = subprocess.check_output([comm.get('comm')]).decode('utf-8')
|
||||
response[comm.get('tag')] = os.popen(comm.get('comm')).read()
|
||||
# response[comm.get('tag')] = subprocess.run(comm.get('comm'), shell=True, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
||||
# response[comm.get('tag')] = read_top()
|
||||
|
||||
else:
|
||||
response = {"error": "Invalid issue type"}
|
||||
|
||||
@ -40,23 +40,45 @@
|
||||
{ "log_file": "/var/log/messages", "lines": 50 }
|
||||
]
|
||||
},
|
||||
|
||||
"Demo1": {
|
||||
|
||||
"commands": [
|
||||
|
||||
],
|
||||
"logs": [
|
||||
{ "log_file": "/home/arehman/Documents/Projects/Python/Maxapex/ticket_ai_flask_app/logs/*_error.log", "lines": 50 }
|
||||
]
|
||||
},
|
||||
|
||||
"Demo2": {
|
||||
|
||||
"commands": [
|
||||
{ "comm": "top", "tag": "top" }
|
||||
|
||||
],
|
||||
"logs": [
|
||||
{ "log_file": "/home/arehman/Documents/Projects/Python/Maxapex/ticket_ai_flask_app/logs/a_error.log", "lines": 50 }
|
||||
]
|
||||
},
|
||||
|
||||
"Demo3": {
|
||||
|
||||
"commands": [
|
||||
{ "comm": "top -b -n 1", "tag": "top" }
|
||||
],
|
||||
"logs": [
|
||||
{ "log_file": "/app/logs/*error_log", "lines": 50 }
|
||||
]
|
||||
},
|
||||
|
||||
"Demo": {
|
||||
|
||||
"commands": [
|
||||
{ "comm": "top -b -n 1", "tag": "top" }
|
||||
],
|
||||
|
||||
"logs": [
|
||||
{ "log_file": "/app/logs/*error_log", "lines": 50 }
|
||||
{ "log_file": "/home/arehman/Documents/Projects/Python/Maxapex/ticket_ai_flask_app/logs/a_error.log", "lines": 50 },
|
||||
{ "log_file": "/home/arehman/Documents/Projects/Python/Maxapex/ticket_ai_flask_app/logs/*_error.log", "lines": 50 }
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user