first update

This commit is contained in:
Your Name 2024-02-23 15:29:26 +05:00
parent 394ca9fa12
commit 3b14fbf0b3
12 changed files with 59 additions and 157 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
.vscode/
playground/
venv/

View File

@ -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"]

View File

@ -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

View File

@ -1,73 +1,13 @@
#!/bin/bash #!/bin/bash
# Update System Packages if ! command -v python3 &> /dev/null
sudo yum update -y then
echo "Error: python3 could not be found"
# Install Python3 and Pip exit
sudo yum install -y python3 python3-pip else
python3 -m venv venv
# Install Flask source venv/bin/activate
pip3 install Flask pip install Flask
pip install flask_jwt_extended
# Create a directory for the Flask application chmod +x run.sh
mkdir -p ~/flask_app fi
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."

1
logs/a_error.log Normal file
View File

@ -0,0 +1 @@
this is a error log from file 'A'

1
logs/b_error.log Normal file
View File

@ -0,0 +1 @@
this is a error log from file 'B'

View File

@ -1 +0,0 @@
this is a error log

View File

@ -6,7 +6,7 @@ pip install flask_jwt_extended
python app.py python app.py
# call api # 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 # sample response
{ {
@ -16,30 +16,7 @@ http://127.0.0.1:5000/get_logs?issue_type=database_issue
} }
# run installation script # run installation script
chmod +x install_flask_app.sh ./install.sh
./install_flask_app.sh
cd ~/flask_app
python3 app.py
# install on centos # run application
yum install python36-devel ./run.sh
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

View File

@ -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
View File

@ -0,0 +1,4 @@
#!/bin/bash
source venv/bin/activate
python3 src/app.py

View File

@ -78,7 +78,7 @@ def get_logs():
issue_type = request.args.get("issue_type") issue_type = request.args.get("issue_type")
response = {} response = {}
try: try:
with open('config.json') as config_file: with open('./src/config.json') as config_file:
config = json.load(config_file) config = json.load(config_file)
if issue_type in config: if issue_type in config:
issue_config = config[issue_type] issue_config = config[issue_type]
@ -86,17 +86,23 @@ def get_logs():
# logs # logs
for log in issue_config['logs']: for log in issue_config['logs']:
directory, file_name = split_directory_and_file(log.get('log_file'))
if '*' in log.get('log_file'): if '*' in log.get('log_file'):
directory, file_name = split_directory_and_file(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']) log_output = read_log_from_dir(directory, file_name, log['lines'])
else: else:
log_output = read_log(log['log_file'], log['lines']) 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 # commands
for comm in issue_config['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: else:
response = {"error": "Invalid issue type"} response = {"error": "Invalid issue type"}

View File

@ -40,23 +40,45 @@
{ "log_file": "/var/log/messages", "lines": 50 } { "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": { "Demo2": {
"commands": [ "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": [ "logs": [
{ "log_file": "/app/logs/*error_log", "lines": 50 }
] ]
}, },
"Demo": { "Demo": {
"commands": [ "commands": [
{ "comm": "top -b -n 1", "tag": "top" }
], ],
"logs": [ "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 }
] ]
} }
} }