commit a9e4cb9d3cd4eac92bdc8b5fc9628fc886659436 Author: khaliiiiiil Date: Fri Dec 6 00:58:16 2024 +0100 first commit diff --git a/Dockerfiles/backup.Dockerfile b/Dockerfiles/backup.Dockerfile new file mode 100644 index 0000000..80d895d --- /dev/null +++ b/Dockerfiles/backup.Dockerfile @@ -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"] diff --git a/Dockerfiles/exporter.Dockerfile b/Dockerfiles/exporter.Dockerfile new file mode 100644 index 0000000..4d577ec --- /dev/null +++ b/Dockerfiles/exporter.Dockerfile @@ -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"] diff --git a/Dockerfiles/odoo.Dockerfile b/Dockerfiles/odoo.Dockerfile new file mode 100644 index 0000000..a50af7d --- /dev/null +++ b/Dockerfiles/odoo.Dockerfile @@ -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"] diff --git a/deploy.yml b/deploy.yml new file mode 100644 index 0000000..4d46bcf --- /dev/null +++ b/deploy.yml @@ -0,0 +1,5 @@ +--- +- hosts: localhost + become: true + roles: + - role: docker-compose \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..aa277e2 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,109 @@ +version: "3.9" +services: + admin: + image: postgres:latest + environment: + POSTGRES_DB: admin + POSTGRES_USER: admin + POSTGRES_PASSWORD: admin + ports: + - "5432:5432" + volumes: + - postgres-data:/var/lib/postgresql/data + networks: + - testNetwork + + pgadmin: + image: dpage/pgadmin4:latest + environment: + PGADMIN_DEFAULT_EMAIL: admin@admin.com + PGADMIN_DEFAULT_PASSWORD: admin + ports: + - "5050:80" + networks: + - testNetwork + + prometheus_exporter: + image: exporter_custom + ports: + - "8000:8000" + networks: + - testNetwork + depends_on: + - admin #Make sure Postgres is up before exporter + + odoo: + image: odoo_custom + environment: + HOST: admin + USER: admin + PASSWORD: admin + DATABASE: admin + ODOO_PASSWORD: admin + ports: + - "8069:8069" + volumes: + - ./odoo.conf:/etc/odoo/odoo.conf + - ./logs/odoo:/var/log/odoo + networks: + - testNetwork + depends_on: + - admin + + grafana: + image: grafana/grafana:latest + environment: + GF_SECURITY_ADMIN_PASSWORD: grafana_pwd + GF_DATASOURCES_PROMETHEUS_URL: http://prometheus:9090 + ports: + - "3000:3000" + networks: + - testNetwork + depends_on: + - prometheus + + prometheus: + image: prom/prometheus:latest + ports: + - "9090:9090" + volumes: + - prometheus-data:/prometheus + - ./prometheus.yml:/etc/prometheus/prometheus.yml + networks: + - testNetwork + + fluentd: + image: fluent/fluentd:v1.13-1 + ports: + - "24224:24224" + volumes: + - ./logs/odoo:/var/log/odoo + - ./fluent.conf:/fluentd/etc/fluent.conf + networks: + - testNetwork + depends_on: + - odoo + + + backup: + image: backup_custom + environment: + POSTGRES_HOST: admin + POSTGRES_DB: admin + POSTGRES_USER: admin + POSTGRES_PASSWORD: admin + volumes: + - backup-data:/backup + networks: + - testNetwork + depends_on: + - admin + +networks: + testNetwork: + driver: bridge + +volumes: + postgres-data: + prometheus-data: + backup-data: \ No newline at end of file diff --git a/entrypoint_backup.sh b/entrypoint_backup.sh new file mode 100644 index 0000000..6ee9b8f --- /dev/null +++ b/entrypoint_backup.sh @@ -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 + diff --git a/entrypoint_odoo.sh b/entrypoint_odoo.sh new file mode 100644 index 0000000..4175206 --- /dev/null +++ b/entrypoint_odoo.sh @@ -0,0 +1,4 @@ +#!/bin/sh + +sleep 20 +odoo -i base diff --git a/fluent.conf b/fluent.conf new file mode 100644 index 0000000..53d1bb1 --- /dev/null +++ b/fluent.conf @@ -0,0 +1,13 @@ + + @type tail + path "/var/log/odoo/odoo.log" + pos_file "/fluentd/logs/odoo.pos" + format none + tag "odoo.log" + + + + @type file + path "/fluentd/logs/collected-logs" + + diff --git a/inventory/hosts b/inventory/hosts new file mode 100644 index 0000000..55f2674 --- /dev/null +++ b/inventory/hosts @@ -0,0 +1,2 @@ +[localhost] +localhost ansible_connection=local \ No newline at end of file diff --git a/odoo.conf b/odoo.conf new file mode 100644 index 0000000..c9b9d81 --- /dev/null +++ b/odoo.conf @@ -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 diff --git a/pg_metrics_exporter.py b/pg_metrics_exporter.py new file mode 100644 index 0000000..ea4e692 --- /dev/null +++ b/pg_metrics_exporter.py @@ -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 diff --git a/prometheus.yml b/prometheus.yml new file mode 100644 index 0000000..14ffc90 --- /dev/null +++ b/prometheus.yml @@ -0,0 +1,5 @@ +scrape_configs: + - job_name: 'pg_stat_statements' + static_configs: + - targets: ['prometheus_exporter:8000'] + scrape_interval: 15s diff --git a/roles/docker-compose/tasks/main.yml b/roles/docker-compose/tasks/main.yml new file mode 100644 index 0000000..e69de29 diff --git a/roles/docker-compose/vars/main.yml b/roles/docker-compose/vars/main.yml new file mode 100644 index 0000000..e69de29