From f9a2ba55ba9a81717b62b14b19c3e563944b74b2 Mon Sep 17 00:00:00 2001 From: "juvenal.diaz" Date: Sat, 24 Oct 2026 01:15:25 -0600 Subject: [PATCH] feat: monitor OCI edge and email VMs --- apps/homelab-alerts/prometheus-rules.yaml | 24 +++++++++++++++ bootstrap/edge/main.tf | 3 +- bootstrap/email/README.md | 6 ++++ bootstrap/oci/main.tf | 30 ++++++++++++++++++ bootstrap/oci/variables.tf | 12 ++++++++ bootstrap/platform/main.tf | 37 +++++++++++------------ bootstrap/platform/variables.tf | 12 ++++++++ lib/jeannie/commands.sh | 14 ++++++++- lib/jeannie/env.sh | 4 +++ lib/jeannie/pipeline.sh | 8 +++++ scripts/email-server | 13 +++++--- 11 files changed, 137 insertions(+), 26 deletions(-) diff --git a/apps/homelab-alerts/prometheus-rules.yaml b/apps/homelab-alerts/prometheus-rules.yaml index ffeb777..1e89ede 100644 --- a/apps/homelab-alerts/prometheus-rules.yaml +++ b/apps/homelab-alerts/prometheus-rules.yaml @@ -75,6 +75,30 @@ spec: description: "The {{ $labels.mountpoint }} mount on {{ $labels.instance }} is near exhaustion. Review disk usage before it becomes an outage." - name: homelab.edge rules: + - alert: HomelabOCIEdgeNodeExporterDown + expr: up{job="oci-edge-node"} == 0 + for: 10m + labels: + severity: warning + annotations: + summary: "OCI edge node metrics target is down" + description: "The OCI edge VM node exporter is unreachable. Check OCI instance health, restricted monitoring ingress, and the scrape route." + - alert: HomelabOCIEmailNodeExporterDown + expr: up{job="oci-email-node"} == 0 + for: 10m + labels: + severity: warning + annotations: + summary: "OCI email node metrics target is down" + description: "The OCI email VM node exporter is unreachable. Check the VM, restricted monitoring ingress, and the scrape route." + - alert: HomelabOCIEmailFilesystemNearlyFull + expr: 100 * (1 - node_filesystem_avail_bytes{job="oci-email-node",fstype!~"tmpfs|overlay|squashfs",mountpoint="/"} / node_filesystem_size_bytes{job="oci-email-node",fstype!~"tmpfs|overlay|squashfs",mountpoint="/"}) > 85 + for: 15m + labels: + severity: warning + annotations: + summary: "OCI email VM root filesystem is over 85% full" + description: "Review mail queues, logs, and storage before the email VM becomes unavailable." - alert: HomelabTraefik5xxSpike expr: sum(rate(traefik_service_requests_total{code=~"5.."}[5m])) > 0.05 for: 10m diff --git a/bootstrap/edge/main.tf b/bootstrap/edge/main.tf index 45b4404..fe69b04 100644 --- a/bootstrap/edge/main.tf +++ b/bootstrap/edge/main.tf @@ -215,7 +215,8 @@ install_missing_packages() { fi } -install_missing_packages ca-certificates curl openssl +install_missing_packages ca-certificates curl openssl prometheus-node-exporter +sudo systemctl enable --now prometheus-node-exporter if ! curl -fsS --connect-timeout 10 -H "Host: $server_name" "http://$backend_host:$backend_port/" >/dev/null; then echo "Warning: cannot reach Traefik backend http://$backend_host:$backend_port/ for $server_name from the edge host." >&2 diff --git a/bootstrap/email/README.md b/bootstrap/email/README.md index 7d9c175..5e72bc5 100644 --- a/bootstrap/email/README.md +++ b/bootstrap/email/README.md @@ -35,6 +35,12 @@ Without a domain it uses a temporary self-signed certificate and keeps ports `LAB_EMAIL_DOMAIN=mail.example.org`; then add DNS A/MX, SPF, DKIM, DMARC, and PTR records and request OCI SMTP port 25 access before opening mail ingress. +The setup also installs `prometheus-node-exporter`. OCI foundation apply +persists the discovered VM addresses and opens TCP 9100 only to the configured +monitoring CIDR. The platform monitoring stack adds the email target +automatically; the adopted edge VM is scraped when its restricted 9100 target +is reachable. + The provider reads OCI credentials from the normal OCI CLI configuration (`~/.oci/config`) and never stores private keys in this repository. `destroy` requires `--yes`. The stack is intentionally separate from `platform`, `apps`, diff --git a/bootstrap/oci/main.tf b/bootstrap/oci/main.tf index 96f6d74..5351447 100644 --- a/bootstrap/oci/main.tf +++ b/bootstrap/oci/main.tf @@ -272,6 +272,36 @@ resource "oci_core_network_security_group_security_rule" "email_imaps" { } } +resource "oci_core_network_security_group_security_rule" "edge_monitoring" { + count = var.enabled && var.edge_enabled && var.monitoring_enabled ? 1 : 0 + network_security_group_id = oci_core_network_security_group.edge[0].id + direction = "INGRESS" + protocol = "6" + source = trimspace(var.monitoring_cidr) != "" ? var.monitoring_cidr : var.admin_cidr + source_type = "CIDR_BLOCK" + tcp_options { + destination_port_range { + min = 9100 + max = 9100 + } + } +} + +resource "oci_core_network_security_group_security_rule" "email_monitoring" { + count = var.enabled && var.email_enabled && var.monitoring_enabled ? 1 : 0 + network_security_group_id = oci_core_network_security_group.email[0].id + direction = "INGRESS" + protocol = "6" + source = trimspace(var.monitoring_cidr) != "" ? var.monitoring_cidr : var.admin_cidr + source_type = "CIDR_BLOCK" + tcp_options { + destination_port_range { + min = 9100 + max = 9100 + } + } +} + data "oci_core_instance" "existing_edge" { count = var.enabled && var.edge_enabled && trimspace(var.edge_existing_instance_id) != "" ? 1 : 0 instance_id = var.edge_existing_instance_id diff --git a/bootstrap/oci/variables.tf b/bootstrap/oci/variables.tf index ca37533..16a73ad 100644 --- a/bootstrap/oci/variables.tf +++ b/bootstrap/oci/variables.tf @@ -112,6 +112,18 @@ variable "open_mail_ports" { default = false } +variable "monitoring_enabled" { + description = "Allow restricted node-exporter access from monitoring_cidr." + type = bool + default = true +} + +variable "monitoring_cidr" { + description = "CIDR allowed to scrape node-exporter; defaults to the admin CIDR." + type = string + default = "" +} + variable "edge_enabled" { type = bool default = true diff --git a/bootstrap/platform/main.tf b/bootstrap/platform/main.tf index e284c6b..9e121d5 100644 --- a/bootstrap/platform/main.tf +++ b/bootstrap/platform/main.tf @@ -1990,25 +1990,6 @@ resource "helm_release" "prometheus_stack" { } ] }, - { - job_name = "oci-edge-node" - static_configs = [ - { - targets = ["132.145.170.74:9100"] - labels = { - host_group = "public-edge" - } - } - ] - metrics_path = "/metrics" - scheme = "http" - relabel_configs = [ - { - source_labels = ["__address__"] - target_label = "instance" - } - ] - }, { job_name = "traefik" static_configs = [ @@ -2028,7 +2009,23 @@ resource "helm_release" "prometheus_stack" { } ] } - ], var.jeannie_api_metrics_token == "" ? [] : [{ + ], var.oci_edge_metrics_target == "" ? [] : [{ + job_name = "oci-edge-node" + static_configs = [{ + targets = [var.oci_edge_metrics_target] + labels = { host_group = "public-edge" } + }] + metrics_path = "/metrics" + scheme = "http" + }], var.oci_email_metrics_target == "" ? [] : [{ + job_name = "oci-email-node" + static_configs = [{ + targets = [var.oci_email_metrics_target] + labels = { host_group = "email" } + }] + metrics_path = "/metrics" + scheme = "http" + }], var.jeannie_api_metrics_token == "" ? [] : [{ job_name = "jeannie-api" metrics_path = "/metrics" scheme = "https" diff --git a/bootstrap/platform/variables.tf b/bootstrap/platform/variables.tf index a6e097f..35be626 100644 --- a/bootstrap/platform/variables.tf +++ b/bootstrap/platform/variables.tf @@ -260,6 +260,18 @@ variable "jeannie_api_metrics_target" { default = "api.lab2025.duckdns.org:443" } +variable "oci_edge_metrics_target" { + description = "Optional private/restricted host:port for the OCI edge node exporter." + type = string + default = "" +} + +variable "oci_email_metrics_target" { + description = "Optional private/restricted host:port for the OCI email VM node exporter." + type = string + default = "" +} + variable "jeannie_api_metrics_token" { description = "Bearer token used by Prometheus to scrape the authenticated Jeannie API metrics endpoint." type = string diff --git a/lib/jeannie/commands.sh b/lib/jeannie/commands.sh index d5bc354..31578af 100644 --- a/lib/jeannie/commands.sh +++ b/lib/jeannie/commands.sh @@ -3293,6 +3293,7 @@ email_vm_manage() { shift || true case "${email_action}" in setup) + oci_foundation_load_config while (($# > 0)); do case "$1" in --domain) export LAB_EMAIL_DOMAIN="${2:-}"; shift 2 ;; @@ -3353,6 +3354,8 @@ oci_foundation_config_names=( LAB_OCI_EMAIL_CREATE_COMPARTMENT LAB_OCI_EMAIL_COMPARTMENT_ID LAB_OCI_EMAIL_COMPARTMENT_NAME LAB_OCI_EMAIL_SUBNET_CIDR LAB_OCI_EDGE_AVAILABILITY_DOMAIN LAB_OCI_EMAIL_AVAILABILITY_DOMAIN LAB_OCI_OPEN_MAIL_PORTS LAB_OCI_AUTO_CONFIGURE_EDGE LAB_EDGE_HOST + LAB_OCI_MONITORING_ENABLED LAB_OCI_MONITORING_CIDR + LAB_EMAIL_PUBLIC_IP LAB_OCI_EDGE_METRICS_TARGET LAB_OCI_EMAIL_METRICS_TARGET ) oci_foundation_config_file() { @@ -3479,11 +3482,12 @@ oci_foundation_manage() { record_change_journal "oci-foundation" apply "$@" tofu -chdir="${stack}" apply "$@" -auto-approve local discovered_edge_ip + local discovered_email_ip discovered_edge_ip="$(tofu -chdir="${stack}" output -raw edge_public_ip 2>/dev/null || true)" + discovered_email_ip="$(tofu -chdir="${stack}" output -raw email_public_ip 2>/dev/null || true)" if [[ -n "${discovered_edge_ip}" && "${discovered_edge_ip}" != "null" ]]; then export LAB_EDGE_HOST="${discovered_edge_ip}" export TF_VAR_edge_host="${discovered_edge_ip}" - oci_foundation_save_config if [[ "${LAB_OCI_AUTO_CONFIGURE_EDGE:-true}" == "true" ]]; then printf 'Configuring edge services at %s...\n' "${discovered_edge_ip}" edge_apply @@ -3491,6 +3495,14 @@ oci_foundation_manage() { printf 'Edge services not configured automatically. LAB_EDGE_HOST=%s\n' "${discovered_edge_ip}" fi fi + if [[ -n "${discovered_email_ip}" && "${discovered_email_ip}" != "null" ]]; then + export LAB_EMAIL_PUBLIC_IP="${discovered_email_ip}" + export LAB_OCI_EMAIL_METRICS_TARGET="${discovered_email_ip}:9100" + fi + if [[ -n "${discovered_edge_ip}" && "${discovered_edge_ip}" != "null" ]]; then + export LAB_OCI_EDGE_METRICS_TARGET="${discovered_edge_ip}:9100" + fi + oci_foundation_save_config fi ;; destroy) diff --git a/lib/jeannie/env.sh b/lib/jeannie/env.sh index 09f8e0b..da51c57 100644 --- a/lib/jeannie/env.sh +++ b/lib/jeannie/env.sh @@ -273,6 +273,8 @@ export_homelab_inventory_tf_vars() { export_if_unset TF_VAR_additional_server_names "${LAB_ADDITIONAL_SERVER_NAMES_JSON:-}" export_if_unset TF_VAR_jeannie_api_metrics_target "${LAB_JEANNIE_API_METRICS_TARGET:-api.${LAB_DOMAIN:-lab2025.duckdns.org}:443}" export_if_unset TF_VAR_jeannie_api_metrics_token "${JEANNIE_SERVER_TOKEN:-${JEANNIE_REMOTE_TOKEN:-}}" + export_if_unset TF_VAR_oci_edge_metrics_target "${LAB_OCI_EDGE_METRICS_TARGET:-}" + export_if_unset TF_VAR_oci_email_metrics_target "${LAB_OCI_EMAIL_METRICS_TARGET:-}" export_if_unset TF_VAR_backend_host "${LAB_TRAEFIK_LB_IP:-}" export_if_unset TF_VAR_gitea_backend_host "${LAB_GITEA_BACKEND_HOST:-${LAB_DEBIAN_TAILSCALE_IP:-}}" export_if_unset TF_VAR_gitea_backend_port "${LAB_GITEA_HTTP_PORT:-}" @@ -348,6 +350,8 @@ export_oci_foundation_tf_vars() { export_if_unset TF_VAR_existing_email_subnet_id "${LAB_OCI_EXISTING_EMAIL_SUBNET_ID:-}" export_if_unset TF_VAR_email_subnet_cidr "${LAB_OCI_EMAIL_SUBNET_CIDR:-}" export_if_unset TF_VAR_admin_cidr "${LAB_OCI_ADMIN_CIDR:-}" + export_if_unset TF_VAR_monitoring_enabled "${LAB_OCI_MONITORING_ENABLED:-}" + export_if_unset TF_VAR_monitoring_cidr "${LAB_OCI_MONITORING_CIDR:-${LAB_OCI_ADMIN_CIDR:-}}" export_if_unset TF_VAR_open_mail_ports "${LAB_OCI_OPEN_MAIL_PORTS:-}" export_if_unset TF_VAR_edge_enabled "${LAB_OCI_EDGE_ENABLED:-}" export_if_unset TF_VAR_edge_existing_instance_id "${LAB_OCI_EDGE_EXISTING_INSTANCE_ID:-}" diff --git a/lib/jeannie/pipeline.sh b/lib/jeannie/pipeline.sh index c3dfbfe..91689f5 100644 --- a/lib/jeannie/pipeline.sh +++ b/lib/jeannie/pipeline.sh @@ -7877,6 +7877,14 @@ apps() { platform_apply() { require_debian_server "platform" + # OCI foundation persists discovered VM addresses after apply. Load those + # values before rendering the monitoring scrape configuration. + if declare -F oci_foundation_load_config >/dev/null 2>&1; then + oci_foundation_load_config + unset TF_VAR_oci_edge_metrics_target TF_VAR_oci_email_metrics_target + export_homelab_inventory_tf_vars + fi + local component="${1:-all}" local -a targets=() diff --git a/scripts/email-server b/scripts/email-server index c74f7ec..b7c0404 100755 --- a/scripts/email-server +++ b/scripts/email-server @@ -34,17 +34,19 @@ ssh_target="${ssh_user}@${email_ip}" ssh_opts=(-i "${ssh_key}" -o ConnectTimeout=15 -o StrictHostKeyChecking=accept-new) if [[ "${action}" == status ]]; then - ssh "${ssh_opts[@]}" "${ssh_target}" 'sudo systemctl is-active postfix dovecot fail2ban; sudo postfix check; sudo ss -ltnp | grep -E ":(25|465|587|993)\b" || true' + ssh "${ssh_opts[@]}" "${ssh_target}" 'sudo systemctl is-active postfix dovecot fail2ban prometheus-node-exporter; sudo postfix check; sudo ss -ltnp | grep -E ":(25|465|587|993|9100)\b" || true' exit $? fi domain="${LAB_EMAIL_DOMAIN:-}" open_mail="${LAB_EMAIL_OPEN_MAIL_PORTS:-false}" +monitoring_cidr="${LAB_OCI_MONITORING_CIDR:-${LAB_OCI_ADMIN_CIDR:-}}" echo "Preparing Postfix and Dovecot on ${ssh_target} (domain=${domain:-pending}, mail_ports=${open_mail})." -ssh "${ssh_opts[@]}" "${ssh_target}" sudo bash -s -- "${domain}" "${open_mail}" <<'REMOTE' +ssh "${ssh_opts[@]}" "${ssh_target}" sudo bash -s -- "${domain}" "${open_mail}" "${monitoring_cidr}" <<'REMOTE' set -euo pipefail domain="${1:-}" open_mail="${2:-false}" +monitoring_cidr="${3:-}" hostname_value="${domain:+mail.${domain}}" hostname_value="${hostname_value:-homelab-mail}" @@ -52,7 +54,7 @@ export DEBIAN_FRONTEND=noninteractive echo 'postfix postfix/mailname string' | debconf-set-selections echo 'postfix postfix/main_mailer_type select Internet Site' | debconf-set-selections apt-get update -apt-get install -y postfix dovecot-imapd dovecot-lmtpd fail2ban ufw openssl +apt-get install -y postfix dovecot-imapd dovecot-lmtpd fail2ban ufw openssl prometheus-node-exporter hostnamectl set-hostname "${hostname_value}" 2>/dev/null || true printf '%s\n' "${hostname_value}" >/etc/mailname @@ -104,6 +106,9 @@ ufw --force reset >/dev/null ufw default deny incoming ufw default allow outgoing ufw allow 22/tcp +if [[ -n "${monitoring_cidr}" ]]; then + ufw allow from "${monitoring_cidr}" to any port 9100 proto tcp +fi if [[ "${open_mail}" == true ]]; then ufw allow 25/tcp ufw allow 465/tcp @@ -112,7 +117,7 @@ if [[ "${open_mail}" == true ]]; then fi ufw --force enable >/dev/null -systemctl enable --now postfix dovecot fail2ban +systemctl enable --now postfix dovecot fail2ban prometheus-node-exporter systemctl restart postfix dovecot postfix check systemctl is-active postfix dovecot fail2ban