Skip to content
This repository was archived by the owner on Dec 17, 2024. It is now read-only.

Commit ebb4224

Browse files
[Docker] Add a new TimescaleDB + v15 based image
As Timescale offers best disk usage by far
1 parent 5db531d commit ebb4224

4 files changed

+261
-0
lines changed

Diff for: docker-launcher-timescale.sh

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
#!/bin/bash
2+
3+
mkdir /var/run/grafana && chown grafana /var/run/grafana
4+
5+
if [ ! -f /pgwatch2/persistent-config/self-signed-ssl.key -o ! -f /pgwatch2/persistent-config/self-signed-ssl.pem ] ; then
6+
openssl req -x509 -newkey rsa:4096 -keyout /pgwatch2/persistent-config/self-signed-ssl.key -out /pgwatch2/persistent-config/self-signed-ssl.pem -days 3650 -nodes -sha256 -subj '/CN=pw2'
7+
cp /pgwatch2/persistent-config/self-signed-ssl.pem /etc/ssl/certs/ssl-cert-snakeoil.pem
8+
cp /pgwatch2/persistent-config/self-signed-ssl.key /etc/ssl/private/ssl-cert-snakeoil.key
9+
chown postgres /etc/ssl/certs/ssl-cert-snakeoil.pem /etc/ssl/private/ssl-cert-snakeoil.key
10+
chmod -R 0600 /etc/ssl/certs/ssl-cert-snakeoil.pem /etc/ssl/private/ssl-cert-snakeoil.key
11+
chmod -R o+rx /pgwatch2/persistent-config
12+
fi
13+
14+
# enable password encryption by default from v1.8.0
15+
if [ ! -f /pgwatch2/persistent-config/default-password-encryption-key.txt ]; then
16+
echo -n "${RANDOM}${RANDOM}${RANDOM}${RANDOM}" > /pgwatch2/persistent-config/default-password-encryption-key.txt
17+
chmod 0600 /pgwatch2/persistent-config/default-password-encryption-key.txt
18+
fi
19+
20+
GRAFANASSL="${PW2_GRAFANASSL,,}" # to lowercase
21+
if [ "$GRAFANASSL" == "1" ] || [ "${GRAFANASSL:0:1}" == "t" ]; then
22+
$(grep -q 'protocol = http$' /etc/grafana/grafana.ini)
23+
if [ "$?" -eq 0 ] ; then
24+
sed -i 's/protocol = http.*/protocol = https/' /etc/grafana/grafana.ini
25+
fi
26+
fi
27+
28+
if [ -n "$PW2_GRAFANAUSER" ] ; then
29+
sed -i "s/admin_user =.*/admin_user = ${PW2_GRAFANAUSER}/" /etc/grafana/grafana.ini
30+
fi
31+
32+
if [ -n "$PW2_GRAFANAPASSWORD" ] ; then
33+
sed -i "s/admin_password =.*/admin_password = ${PW2_GRAFANAPASSWORD}/" /etc/grafana/grafana.ini
34+
fi
35+
36+
if [ -n "$PW2_GRAFANANOANONYMOUS" ] ; then
37+
CFG=$(cat <<-'HERE'
38+
[auth.anonymous]
39+
enabled = false
40+
HERE
41+
)
42+
echo "$CFG" >> /etc/grafana/grafana.ini
43+
fi
44+
45+
if [ ! -f /pgwatch2/persistent-config/db-bootstrap-done-marker ] ; then
46+
47+
if [ ! -d /var/lib/postgresql/15 ]; then
48+
mkdir /var/lib/postgresql/15 && chown -R postgres:postgres /var/lib/postgresql/15
49+
pg_dropcluster 15 main
50+
pg_createcluster --locale en_US.UTF-8 15 main
51+
echo "include = 'pgwatch_postgresql.conf'" >> /etc/postgresql/15/main/postgresql.conf
52+
cp /pgwatch2/postgresql_timescale.conf /etc/postgresql/15/main/pgwatch_postgresql.conf
53+
cp /pgwatch2/pg_hba.conf /etc/postgresql/15/main/pg_hba.conf
54+
fi
55+
56+
pg_ctlcluster 15 main start -- --wait
57+
58+
su -c "psql -d postgres -f /pgwatch2/bootstrap/change_pw.sql" postgres
59+
su -c "psql -d postgres -f /pgwatch2/bootstrap/grant_monitor_to_pgwatch2.sql" postgres
60+
su -c "psql -d postgres -f /pgwatch2/bootstrap/create_db_pgwatch.sql" postgres
61+
su -c "psql -d pgwatch2 -f /pgwatch2/bootstrap/revoke_public_create.sql" postgres
62+
su -c "psql -d postgres -f /pgwatch2/bootstrap/create_db_grafana.sql" postgres
63+
su -c "psql -d postgres -f /pgwatch2/bootstrap/create_db_metric_store.sql" postgres
64+
su -c "psql -d pgwatch2 -f /pgwatch2/sql/config_store/config_store.sql" postgres
65+
su -c "psql -d pgwatch2 -f /pgwatch2/sql/config_store/metric_definitions.sql" postgres
66+
su -c "psql -d pgwatch2_metrics -f /pgwatch2/sql/metric_store/00_schema_base.sql" postgres
67+
su -c "psql -d pgwatch2_metrics -f /pgwatch2/sql/metric_store/01_old_metrics_cleanup_procedure.sql" postgres
68+
69+
su -c "psql -d pgwatch2_metrics -f /pgwatch2/sql/metric_store/timescale/ensure_partition_timescale.sql" postgres
70+
su -c "psql -d pgwatch2_metrics -f /pgwatch2/sql/metric_store/timescale/change_compression_interval.sql" postgres
71+
su -c "psql -d pgwatch2_metrics -f /pgwatch2/sql/metric_store/timescale/metric_store_timescale.sql" postgres
72+
su -c "psql -d pgwatch2_metrics -f /pgwatch2/sql/metric_store/timescale/change_chunk_interval.sql" postgres
73+
su -c "psql -d pgwatch2_metrics -f /pgwatch2/sql/metric_store/metric-time/ensure_partition_metric_time.sql" postgres
74+
75+
su -c "psql -d pgwatch2 -f /pgwatch2/metrics/00_helpers/get_load_average/9.1/metric.sql" postgres
76+
su -c "psql -d pgwatch2 -f /pgwatch2/metrics/00_helpers/get_stat_statements/9.4/metric.sql" postgres
77+
su -c "psql -d pgwatch2 -f /pgwatch2/metrics/00_helpers/get_stat_activity/9.2/metric.sql" postgres
78+
su -c "psql -d pgwatch2 -f /pgwatch2/metrics/00_helpers/get_stat_replication/9.2/metric.sql" postgres
79+
su -c "psql -d pgwatch2 -f /pgwatch2/metrics/00_helpers/get_table_bloat_approx/9.5/metric.sql" postgres
80+
su -c "psql -d pgwatch2 -f /pgwatch2/metrics/00_helpers/get_table_bloat_approx_sql/12/metric.sql" postgres
81+
su -c "psql -d pgwatch2 -f /pgwatch2/metrics/00_helpers/get_wal_size/10/metric.sql" postgres
82+
su -c "psql -d pgwatch2 -f /pgwatch2/metrics/00_helpers/get_psutil_cpu/9.1/metric.sql" postgres
83+
su -c "psql -d pgwatch2 -f /pgwatch2/metrics/00_helpers/get_psutil_mem/9.1/metric.sql" postgres
84+
su -c "psql -d pgwatch2 -f /pgwatch2/metrics/00_helpers/get_psutil_disk/9.1/metric.sql" postgres
85+
su -c "psql -d pgwatch2 -f /pgwatch2/metrics/00_helpers/get_psutil_disk_io_total/9.1/metric.sql" postgres
86+
su -c "psql -d pgwatch2 -c 'create extension pg_qualstats'" postgres
87+
88+
if [ -n "$PW2_TESTDB" ] ; then
89+
su -c "psql -d pgwatch2 -f /pgwatch2/bootstrap/insert_test_monitored_db.sql" postgres
90+
fi
91+
92+
touch /pgwatch2/persistent-config/db-bootstrap-done-marker
93+
94+
pg_ctlcluster 15 main stop -- --wait
95+
96+
fi
97+
98+
sleep 1
99+
100+
exec /usr/local/bin/supervisord --configuration=/etc/supervisor/supervisord.conf --nodaemon

Diff for: docker/Dockerfile-timescale

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
FROM golang:1.19
2+
3+
# For showing Git version via 'pgwatch2 --version'
4+
ARG GIT_HASH
5+
ARG GIT_TIME
6+
ENV GIT_HASH=${GIT_HASH}
7+
ENV GIT_TIME=${GIT_TIME}
8+
9+
ADD pgwatch2 /pgwatch2
10+
RUN cd /pgwatch2 && bash build_gatherer.sh
11+
12+
13+
FROM ubuntu:22.04
14+
15+
RUN apt-get -q update \
16+
&& DEBIAN_FRONTEND=noninteractive apt-get install -qy curl wget apt-transport-https vim git postgresql-common gnupg2 python3-pip \
17+
&& sh /usr/share/postgresql-common/pgdg/apt.postgresql.org.sh -y \
18+
&& DEBIAN_FRONTEND=noninteractive apt-get install -qy postgresql-15 postgresql-plpython3-15 python3-psutil \
19+
&& echo "deb https://packagecloud.io/timescale/timescaledb/ubuntu/ jammy main" | tee /etc/apt/sources.list.d/timescaledb.list \
20+
&& wget --quiet -O - https://packagecloud.io/timescale/timescaledb/gpgkey | apt-key add - \
21+
&& apt update \
22+
&& DEBIAN_FRONTEND=noninteractive apt-get install -qy timescaledb-2-postgresql-15 postgresql-15-pg-qualstats \
23+
&& echo "include = 'pgwatch_postgresql.conf'" >> /etc/postgresql/15/main/postgresql.conf
24+
25+
RUN arch=$(arch | sed s/aarch64/arm64/ | sed s/x86_64/amd64/) \
26+
&& DEBIAN_FRONTEND=noninteractive apt-get install -qy libfontconfig1 \
27+
&& wget -q -O grafana.deb "https://dl.grafana.com/oss/release/grafana_8.5.15_${arch}.deb" \
28+
&& dpkg -i grafana.deb && rm grafana.deb
29+
30+
31+
# Add pgwatch2 sources
32+
ADD pgwatch2 /pgwatch2
33+
# Copy over the compiled gatherer
34+
COPY --from=0 /pgwatch2/pgwatch2 /pgwatch2
35+
ADD webpy /pgwatch2/webpy
36+
37+
# For showing Git versions via :8080/versions or 'pgwatch2 --version'
38+
ARG GIT_HASH
39+
ARG GIT_TIME
40+
ENV GIT_HASH=${GIT_HASH}
41+
ENV GIT_TIME=${GIT_TIME}
42+
RUN echo "${GIT_HASH} ${GIT_TIME}" > /pgwatch2/build_git_version.txt
43+
44+
# Grafana config customizations, Web UI requirements, compilation of the Go gatherer
45+
RUN cp /pgwatch2/bootstrap/grafana_custom_config.ini /etc/grafana/grafana.ini \
46+
&& pip3 install -r /pgwatch2/webpy/requirements_pg_metrics.txt \
47+
&& grafana-cli plugins install savantly-heatmap-panel \
48+
&& pip3 install supervisor pyyaml && mkdir /var/log/supervisor \
49+
&& apt autoremove -y
50+
51+
52+
ADD grafana_dashboards /pgwatch2/grafana_dashboards
53+
54+
55+
# Set up supervisord [https://docs.docker.com/engine/admin/using_supervisord/]
56+
COPY supervisord-timescale.conf /etc/supervisor/supervisord.conf
57+
58+
# NB! When security is a concern one should definitely alter "pgwatch2" password in change_pw.sql and maybe modify pg_hba.conf accordingly
59+
COPY postgresql_timescale.conf /etc/postgresql/15/main/pgwatch_postgresql.conf
60+
COPY pg_hba.conf /etc/postgresql/15/main/pg_hba.conf
61+
COPY docker-launcher-timescale.sh pg_hba.conf /pgwatch2/
62+
63+
ENV PW2_DATASTORE postgres
64+
ENV PW2_PG_METRIC_STORE_CONN_STR postgresql://pgwatch2:pgwatch2admin@localhost:5432/pgwatch2_metrics
65+
ENV PW2_PG_SCHEMA_TYPE timescale
66+
ENV PW2_AES_GCM_KEYPHRASE_FILE /pgwatch2/persistent-config/default-password-encryption-key.txt
67+
68+
# Admin UI for configuring servers to be monitored
69+
EXPOSE 8080
70+
# Gatherer healthcheck port / metric statistics (JSON)
71+
EXPOSE 8081
72+
# Postgres DB holding the pgwatch2 config DB / metrics
73+
EXPOSE 5432
74+
# Grafana UI
75+
EXPOSE 3000
76+
# Prometheus scraping port
77+
EXPOSE 9187
78+
79+
### Volumes for easier updating to newer to newer pgwatch2 containers
80+
### NB! Backwards compatibility is not 100% guaranteed so a backup
81+
### using traditional means is still recommended before updating - see "Updating to a newer Docker version" from README
82+
83+
VOLUME /pgwatch2/persistent-config
84+
VOLUME /var/lib/postgresql
85+
VOLUME /var/lib/grafana
86+
87+
CMD ["/pgwatch2/docker-launcher-timescale.sh"]

Diff for: postgresql_timescale.conf

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
listen_addresses='*'
2+
shared_buffers='64MB'
3+
work_mem='16MB'
4+
checkpoint_timeout='1h'
5+
ssl=true
6+
track_io_timing=on
7+
shared_preload_libraries = 'pg_stat_statements,timescaledb,pg_qualstats'
8+
track_functions='pl'
9+
wal_compression=zstd
10+
log_destination=csvlog
11+
logging_collector=on
12+
log_directory='/var/log/postgresql'
13+
log_filename='postgresql-%a.log'
14+
log_truncate_on_rotation=on
15+
wal_level=archive
16+
archive_mode=on
17+
archive_command='/bin/true'
18+
max_worker_processes=16
19+
max_locks_per_transaction=128

Diff for: supervisord-timescale.conf

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
[supervisord]
2+
nodaemon=true
3+
user=root
4+
pidfile=/var/run/supervisord.pid
5+
logfile=/var/log/supervisor/supervisord.log
6+
childlogdir=/var/log/supervisor
7+
8+
[unix_http_server]
9+
file=/var/run/supervisor.sock
10+
11+
[rpcinterface:supervisor]
12+
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
13+
14+
[supervisorctl]
15+
serverurl=unix:///var/run/supervisor.sock
16+
17+
[program:supervisord_bootstrap]
18+
command=/pgwatch2/bootstrap/supervisord_bootstrap_pg.sh
19+
autorestart=false
20+
startsecs=0
21+
autostart=true
22+
redirect_stderr=true
23+
stdout_logfile=/dev/stdout
24+
stdout_logfile_maxbytes=0
25+
26+
[program:postgres]
27+
command=/usr/lib/postgresql/15/bin/postgres -D /var/lib/postgresql/15/main -c config_file=/etc/postgresql/15/main/postgresql.conf
28+
user=postgres
29+
startsecs=0
30+
priority=1
31+
autostart=false
32+
33+
[program:pgwatch2]
34+
command=/pgwatch2/pgwatch2
35+
priority=300
36+
autostart=false
37+
38+
[program:webpy]
39+
command=python3 /pgwatch2/webpy/web.py
40+
priority=400
41+
autostart=false
42+
43+
[program:grafana]
44+
command=/usr/sbin/grafana-server --homepath=/usr/share/grafana --pidfile=/var/run/grafana/grafana-server.pid --config=/etc/grafana/grafana.ini --packaging=deb cfg:default.paths.provisioning=/etc/grafana/provisioning cfg:default.paths.data=/var/lib/grafana cfg:default.paths.logs=/var/log/grafana cfg:default.paths.plugins=/var/lib/grafana/plugins
45+
user=grafana
46+
startsecs=0
47+
priority=500
48+
autostart=false
49+
50+
[program:grafana_dashboard_setup]
51+
command=/pgwatch2/bootstrap/set_up_grafana_dashboards_pg.sh
52+
priority=600
53+
autorestart=false
54+
startsecs=0
55+
autostart=false

0 commit comments

Comments
 (0)