first demo

This commit is contained in:
khaliiiiiil 2024-12-16 05:27:32 +01:00
commit 1974b77005
15 changed files with 1651 additions and 0 deletions

View File

@ -0,0 +1,14 @@
# Use the Alpine image as the base
FROM alpine:latest as backup_custom
# Copy the entrypoint script into the container
COPY entrypoint_backup.sh /usr/local/bin/entrypoint_backup.sh
# Switch to root user for setup
USER root
# Make the entrypoint script executable
RUN chmod +x /usr/local/bin/entrypoint_backup.sh
# Set the new entrypoint
ENTRYPOINT ["/usr/local/bin/entrypoint_backup.sh"]

View File

@ -0,0 +1,11 @@
#!/bin/sh
# Install PostgreSQL client
apk add --no-cache postgresql-client
# Wait until the PostgreSQL server is ready
until pg_isready -h admin -U admin; do
echo "Waiting for PostgreSQL..."
sleep 2
done

View File

@ -0,0 +1,4 @@
#!/bin/sh
sleep 20
odoo -i base

View File

@ -0,0 +1,27 @@
# Use a Python slim image as base
FROM python:3.9-slim as exporter_custom
# Set environment variables to avoid buffering
ENV PYTHONUNBUFFERED=1
# Create and set working directory
WORKDIR /app
# Install required dependencies
RUN apt-get update && \
apt-get install -y \
libpq-dev && \
pip install psycopg2-binary prometheus-client && \
apt-get clean
# Create a directory for logs
RUN mkdir /app/logs
# Copy the Python script into the container
COPY pg_metrics_exporter.py /app/
# Set permissions for log directory (if required)
RUN chmod 755 /app/logs
# Run the script and redirect logs to a file
CMD ["python", "/app/pg_metrics_exporter.py", ">", "/app/logs/exporter.log", "2>&1"]

View File

@ -0,0 +1,13 @@
# Use the existing Odoo image as the base
FROM odoo:latest as odoo-custom
# Copy the entrypoint script into the container
COPY entrypoint_odoo.sh /usr/local/bin/entrypoint_odoo.sh
USER root
# Make the entrypoint script executable
RUN chmod +x /usr/local/bin/entrypoint_odoo.sh
# Set the new entrypoint
ENTRYPOINT ["/usr/local/bin/entrypoint_odoo.sh"]

View File

@ -0,0 +1,59 @@
import psycopg2
from prometheus_client import start_http_server, Gauge
import time
# Configuration for database connection
DB_PARAMS = {
"host": "admin",
"database": "admin",
"user": "admin",
"password": "admin"
}
# Prometheus metrics
QUERY_CALLS = Gauge('postgresql_query_calls', 'Number of calls for each query', ['query'])
QUERY_TOTAL_TIME = Gauge('postgresql_query_total_time_ms', 'Total execution time for each query in ms', ['query'])
def fetch_metrics():
try:
# Log connection attempt
print("Connecting to PostgreSQL database...")
conn = psycopg2.connect(**DB_PARAMS)
cur = conn.cursor()
# Execute query to get data
cur.execute("""
SELECT query, calls, total_exec_time
FROM pg_stat_statements
ORDER BY total_exec_time DESC;
""")
# Iterate through results and set Prometheus metrics
for row in cur:
query = row[0].replace("\\", "\\\\").replace('"', '\\"') # Escape special characters
calls = row[1]
total_time = row[2] * 1000 # Convert seconds to milliseconds
QUERY_CALLS.labels(query=query).set(calls)
QUERY_TOTAL_TIME.labels(query=query).set(total_time)
# Log the metrics being set
print(f"Metrics set for query: {query} | Calls: {calls} | Total execution time: {total_time} ms")
cur.close()
conn.close()
except psycopg2.Error as e:
print(f"Error fetching data: {e}")
except Exception as e:
print(f"Unexpected error: {e}")
if __name__ == '__main__':
# Start Prometheus HTTP server on port 8000
start_http_server(8000)
print("Exporter running on http://localhost:8000/metrics")
# Main loop to fetch metrics at regular intervals
while True:
fetch_metrics()
time.sleep(60) # Scrape every 60 seconds

74
docker-compose.yml Normal file
View File

@ -0,0 +1,74 @@
version: "3.3"
services:
admin:
image: postgres:latest
hostname: admin
ports:
- "5432:5432"
environment:
POSTGRES_DB: admin
POSTGRES_USER: admin
POSTGRES_PASSWORD: admin
volumes:
- postgres-data:/var/lib/postgresql/data
pgadmin:
image: dpage/pgadmin4:latest
ports:
- "5050:80"
environment:
PGADMIN_DEFAULT_EMAIL: admin@admin.com
PGADMIN_DEFAULT_PASSWORD: admin
prometheus_exporter:
image: exporter_custom
ports:
- "8000:8000"
odoo:
image: odoo_custom
ports:
- "8069:8069"
environment:
HOST: admin
USER: admin
PASSWORD: admin
DATABASE: admin
ODOO_PASSWORD: admin
volumes:
- ./files/odoo.conf:/etc/odoo/odoo.conf
- ./logs/odoo:/var/log/odoo
grafana:
image: grafana/grafana:latest
ports:
- "3000:3000"
environment:
GF_SECURITY_ADMIN_PASSWORD: grafana_pwd
GF_DATASOURCES_PROMETHEUS_URL: http://prometheus:9090
prometheus:
image: prom/prometheus:latest
ports:
- "9090:9090"
volumes:
- prometheus-data:/prometheus
- ./files/prometheus.yml:/etc/prometheus/prometheus.yml
fluentd:
image: fluent/fluentd:v1.13-1
ports:
- "24224:24224"
volumes:
- ./logs/odoo:/var/log/odoo
- ./files/fluent.conf:/fluentd/etc/fluent.conf
backup:
image: backup_custom
environment:
POSTGRES_HOST: admin
POSTGRES_DB: admin
POSTGRES_USER: admin
POSTGRES_PASSWORD: admin
volumes:
- backup-data:/backup
networks:
testNetwork:
driver: bridge
volumes:
postgres-data:
prometheus-data:
backup-data:

13
files/fluent.conf Normal file
View File

@ -0,0 +1,13 @@
<source>
@type tail
path "/var/log/odoo/odoo.log"
pos_file "/fluentd/logs/odoo.pos"
format none
tag "odoo.log"
</source>
<match odoo.log>
@type file
path "/fluentd/logs/collected-logs"
</match>

8
files/odoo.conf Normal file
View File

@ -0,0 +1,8 @@
[options]
db_host = admin
db_port = 5432
db_user = admin
db_password = admin
default_productivity_apps = True
db_name = admin
logfile = /var/log/odoo/odoo.log

5
files/prometheus.yml Normal file
View File

@ -0,0 +1,5 @@
scrape_configs:
- job_name: 'pg_stat_statements'
static_configs:
- targets: ['prometheus_exporter:8000']
scrape_interval: 15s

2
inventory Normal file
View File

@ -0,0 +1,2 @@
[localhost]
localhost ansible_connection=local

1394
logs/odoo/odoo.log Normal file

File diff suppressed because it is too large Load Diff

20
playbook.yml Normal file
View File

@ -0,0 +1,20 @@
- hosts: localhost
become: true
connection: local
tasks:
- name: Load variables from volumes.yml
set_fact:
volume_vars: "{{ lookup('file', 'volumes.yml') | from_yaml }}"
- name: Load variables from secrets.yml
set_fact:
secret_vars: "{{ lookup('file', 'secrets.yml') | from_yaml }}"
- name: Create volumes
command: docker volume create {{ item.name }}
with_items: "{{ volume_vars.volumes }}"
changed_when: false
- name: Run docker-compose up -d
command: docker-compose up -d

3
secrets.yml Normal file
View File

@ -0,0 +1,3 @@
postgres_password: admin
pgadmin_password: admin
odoo_password: admin

4
volumes.yml Normal file
View File

@ -0,0 +1,4 @@
volumes:
- name: postgres-data
- name: prometheus-data
- name: backup-data