diff --git a/bootstrap/oci/main.tf b/bootstrap/oci/main.tf index 0fea70b..04f8067 100644 --- a/bootstrap/oci/main.tf +++ b/bootstrap/oci/main.tf @@ -14,6 +14,10 @@ locals { public_route_table_id = try(oci_core_route_table.public[0].id, null) private_route_table_id = try(oci_core_route_table.private[0].id, null) base_tags = merge(var.freeform_tags, { managed_by = "jeannie", purpose = "homelab-learning" }) + edge_monitoring_instance_id = trimspace(var.edge_monitoring_instance_id) != "" ? var.edge_monitoring_instance_id : try(oci_core_instance.edge[0].id, try(data.oci_core_instance.monitoring_edge[0].id, try(data.oci_core_instance.existing_edge[0].id, null))) + email_monitoring_instance_id = trimspace(var.email_monitoring_instance_id) != "" ? var.email_monitoring_instance_id : try(oci_core_instance.email[0].id, try(data.oci_core_instance.monitoring_email[0].id, null)) + edge_monitoring_compartment_id = try(data.oci_core_instance.monitoring_edge[0].compartment_id, try(data.oci_core_instance.existing_edge[0].compartment_id, local.target_compartment_id)) + email_monitoring_compartment_id = try(data.oci_core_instance.monitoring_email[0].compartment_id, local.email_compartment_id) } data "oci_core_subnet" "existing_public" { @@ -26,6 +30,16 @@ data "oci_core_vcn" "existing" { vcn_id = var.existing_vcn_id } +data "oci_core_instance" "monitoring_edge" { + count = var.enabled && trimspace(var.edge_monitoring_instance_id) != "" ? 1 : 0 + instance_id = var.edge_monitoring_instance_id +} + +data "oci_core_instance" "monitoring_email" { + count = var.enabled && trimspace(var.email_monitoring_instance_id) != "" ? 1 : 0 + instance_id = var.email_monitoring_instance_id +} + data "oci_objectstorage_namespace" "homelab" { count = var.enabled && var.backup_bucket_enabled ? 1 : 0 compartment_id = var.tenancy_id @@ -48,6 +62,23 @@ resource "oci_objectstorage_bucket" "homelab_backups" { } } +resource "oci_ons_notification_topic" "monitoring" { + count = var.enabled && var.monitoring_enabled && var.monitoring_alarms_enabled ? 1 : 0 + compartment_id = local.target_compartment_id + name = var.monitoring_topic_name + description = "Jeannie OCI VM monitoring alarms" + freeform_tags = local.base_tags +} + +resource "oci_ons_subscription" "monitoring_email" { + count = var.enabled && var.monitoring_enabled && var.monitoring_alarms_enabled && trimspace(var.monitoring_notification_email) != "" ? 1 : 0 + compartment_id = local.target_compartment_id + endpoint = var.monitoring_notification_email + protocol = "EMAIL" + topic_id = oci_ons_notification_topic.monitoring[0].id + freeform_tags = local.base_tags +} + resource "oci_identity_policy" "object_storage_lifecycle" { count = var.enabled && var.backup_bucket_enabled ? 1 : 0 compartment_id = var.tenancy_id @@ -472,3 +503,71 @@ resource "oci_core_instance" "email" { metadata = { ssh_authorized_keys = var.ssh_public_key } freeform_tags = merge(local.base_tags, { role = "email" }) } + +resource "oci_monitoring_alarm" "edge_cpu_high" { + count = var.enabled && var.monitoring_enabled && var.monitoring_alarms_enabled && local.edge_monitoring_instance_id != null && trimspace(local.edge_monitoring_instance_id) != "" ? 1 : 0 + compartment_id = local.edge_monitoring_compartment_id + metric_compartment_id = local.edge_monitoring_compartment_id + namespace = "oci_computeagent" + query = "CpuUtilization[5m]{resourceId = \"${local.edge_monitoring_instance_id}\"}.mean() > 85" + severity = "WARNING" + destinations = [oci_ons_notification_topic.monitoring[0].id] + display_name = "homelab-edge-cpu-high" + is_enabled = true + pending_duration = "PT15M" + resolution = "1m" + alarm_summary = "OCI edge VM CPU utilization is above 85 percent" + body = "The Jeannie-managed edge VM has sustained high CPU utilization." + freeform_tags = local.base_tags +} + +resource "oci_monitoring_alarm" "edge_unavailable" { + count = var.enabled && var.monitoring_enabled && var.monitoring_alarms_enabled && local.edge_monitoring_instance_id != null && trimspace(local.edge_monitoring_instance_id) != "" ? 1 : 0 + compartment_id = local.edge_monitoring_compartment_id + metric_compartment_id = local.edge_monitoring_compartment_id + namespace = "oci_computeagent" + query = "CpuUtilization[5m]{resourceId = \"${local.edge_monitoring_instance_id}\"}.absent()" + severity = "CRITICAL" + destinations = [oci_ons_notification_topic.monitoring[0].id] + display_name = "homelab-edge-unavailable" + is_enabled = true + pending_duration = "PT5M" + resolution = "1m" + alarm_summary = "OCI edge VM metrics are unavailable" + body = "The Jeannie-managed edge VM has stopped reporting OCI Compute Agent metrics." + freeform_tags = local.base_tags +} + +resource "oci_monitoring_alarm" "email_cpu_high" { + count = var.enabled && var.monitoring_enabled && var.monitoring_alarms_enabled && local.email_monitoring_instance_id != null && trimspace(local.email_monitoring_instance_id) != "" ? 1 : 0 + compartment_id = local.email_monitoring_compartment_id + metric_compartment_id = local.email_monitoring_compartment_id + namespace = "oci_computeagent" + query = "CpuUtilization[5m]{resourceId = \"${local.email_monitoring_instance_id}\"}.mean() > 85" + severity = "WARNING" + destinations = [oci_ons_notification_topic.monitoring[0].id] + display_name = "homelab-email-cpu-high" + is_enabled = true + pending_duration = "PT15M" + resolution = "1m" + alarm_summary = "OCI email VM CPU utilization is above 85 percent" + body = "The Jeannie-managed email VM has sustained high CPU utilization." + freeform_tags = local.base_tags +} + +resource "oci_monitoring_alarm" "email_unavailable" { + count = var.enabled && var.monitoring_enabled && var.monitoring_alarms_enabled && local.email_monitoring_instance_id != null && trimspace(local.email_monitoring_instance_id) != "" ? 1 : 0 + compartment_id = local.email_monitoring_compartment_id + metric_compartment_id = local.email_monitoring_compartment_id + namespace = "oci_computeagent" + query = "CpuUtilization[5m]{resourceId = \"${local.email_monitoring_instance_id}\"}.absent()" + severity = "CRITICAL" + destinations = [oci_ons_notification_topic.monitoring[0].id] + display_name = "homelab-email-unavailable" + is_enabled = true + pending_duration = "PT5M" + resolution = "1m" + alarm_summary = "OCI email VM metrics are unavailable" + body = "The Jeannie-managed email VM has stopped reporting OCI Compute Agent metrics." + freeform_tags = local.base_tags +} diff --git a/bootstrap/oci/outputs.tf b/bootstrap/oci/outputs.tf index f57a72a..e502e1d 100644 --- a/bootstrap/oci/outputs.tf +++ b/bootstrap/oci/outputs.tf @@ -5,13 +5,27 @@ output "email_subnet_id" { value = local.email_subnet_id } output "private_subnet_id" { value = try(oci_core_subnet.private[0].id, null) } output "edge_instance_id" { value = try(oci_core_instance.edge[0].id, data.oci_core_instance.existing_edge[0].id, null) } output "edge_public_ip" { value = try(oci_core_instance.edge[0].public_ip, data.oci_core_instance.existing_edge[0].public_ip, null) } -output "email_instance_id" { value = try(oci_core_instance.email[0].id, null) } -output "email_public_ip" { value = try(oci_core_instance.email[0].public_ip, null) } +output "email_instance_id" { value = try(oci_core_instance.email[0].id, try(data.oci_core_instance.monitoring_email[0].id, null)) } +output "email_public_ip" { value = try(oci_core_instance.email[0].public_ip, try(data.oci_core_instance.monitoring_email[0].public_ip, null)) } output "bastion_id" { value = try(oci_bastion_bastion.homelab[0].id, var.bastion_existing_id, null) } output "bastion_target_subnet_id" { value = try(oci_bastion_bastion.homelab[0].target_subnet_id, local.bastion_target_subnet_id, null) } output "backup_bucket_id" { value = try(oci_objectstorage_bucket.homelab_backups[0].id, null) } output "backup_bucket_name" { value = try(oci_objectstorage_bucket.homelab_backups[0].name, null) } output "backup_bucket_namespace" { value = try(oci_objectstorage_bucket.homelab_backups[0].namespace, null) } +output "monitoring_topic_id" { value = try(oci_ons_notification_topic.monitoring[0].id, null) } +output "monitoring_email_subscription_id" { value = try(oci_ons_subscription.monitoring_email[0].id, null) } +output "monitoring_edge_alarm_ids" { + value = { + cpu_high = try(oci_monitoring_alarm.edge_cpu_high[0].id, null) + unavailable = try(oci_monitoring_alarm.edge_unavailable[0].id, null) + } +} +output "monitoring_email_alarm_ids" { + value = { + cpu_high = try(oci_monitoring_alarm.email_cpu_high[0].id, null) + unavailable = try(oci_monitoring_alarm.email_unavailable[0].id, null) + } +} output "next_steps" { value = [ "Set LAB_EDGE_HOST to edge_public_ip and run ./jeannie edge to configure the edge proxy.", @@ -19,5 +33,6 @@ output "next_steps" { "Reserve the email public IP and request OCI reverse DNS/PTR before production mail.", "Request OCI SMTP port 25 access; it is restricted by default for newer tenancies.", "Add DNS A/AAAA, MX, SPF, DKIM, and DMARC records only after choosing the mail domain.", + "OCI VM CPU and availability alarms are delivered through the monitoring Notifications topic; confirm the email subscription if configured.", ] } diff --git a/bootstrap/oci/variables.tf b/bootstrap/oci/variables.tf index 5759dac..34ee2fd 100644 --- a/bootstrap/oci/variables.tf +++ b/bootstrap/oci/variables.tf @@ -130,6 +130,36 @@ variable "monitoring_cidr" { default = "" } +variable "monitoring_alarms_enabled" { + description = "Create OCI Monitoring alarms and a Notifications topic for the managed OCI VMs." + type = bool + default = true +} + +variable "monitoring_topic_name" { + description = "OCI Notifications topic used by VM monitoring alarms." + type = string + default = "homelab-monitoring" +} + +variable "monitoring_notification_email" { + description = "Optional email subscription for OCI VM alarms; OCI requires confirmation before delivery." + type = string + default = "" +} + +variable "edge_monitoring_instance_id" { + description = "Persisted edge instance OCID used by OCI Monitoring alarms when the VM is adopted." + type = string + default = "" +} + +variable "email_monitoring_instance_id" { + description = "Persisted email instance OCID used by OCI Monitoring alarms when the VM is adopted." + type = string + default = "" +} + variable "backup_bucket_enabled" { description = "Create a private OCI Object Storage bucket for encrypted Jeannie backups." type = bool diff --git a/bootstrap/platform/grafana-dashboards/homelab-overview.json b/bootstrap/platform/grafana-dashboards/homelab-overview.json index 1c1e7ba..852fa56 100644 --- a/bootstrap/platform/grafana-dashboards/homelab-overview.json +++ b/bootstrap/platform/grafana-dashboards/homelab-overview.json @@ -58,6 +58,24 @@ "targets": [{"expr": "ALERTS{alertstate=\"firing\"}", "legendFormat": "{{alertname}} {{instance}}", "refId": "A"}], "title": "Firing Alerts", "type": "table" + }, + { + "datasource": {"type": "prometheus", "uid": "prometheus"}, + "fieldConfig": {"defaults": {}, "overrides": []}, + "gridPos": {"h": 8, "w": 12, "x": 0, "y": 24}, + "id": 7, + "targets": [{"expr": "up{job=~\"oci-edge-node|oci-email-node|oci-edge-containers|rpi-node|jeannie-self-heal|traefik|jeannie-api\"}", "format": "table", "instant": true, "refId": "A"}], + "title": "Service and Exporter Status", + "type": "table" + }, + { + "datasource": {"type": "prometheus", "uid": "prometheus"}, + "fieldConfig": {"defaults": {"unit": "bytes", "min": 0}, "overrides": []}, + "gridPos": {"h": 8, "w": 12, "x": 12, "y": 24}, + "id": 8, + "targets": [{"expr": "sum by (name) (container_memory_working_set_bytes{container!=\"\",image!=\"\"})", "legendFormat": "{{name}}", "refId": "A"}], + "title": "Kubernetes Container Memory", + "type": "timeseries" } ], "refresh": "30s", @@ -65,6 +83,7 @@ "tags": ["homelab", "overview", "oci"], "templating": {"list": []}, "time": {"from": "now-6h", "to": "now"}, + "description": "Unified homelab view for OCI VMs, Raspberry Pi, Kubernetes containers, exporters, and Jeannie alerts. OCI-native VM alarms notify through the managed Notifications topic.", "title": "Homelab Overview", "uid": "homelab-overview", "version": 1 diff --git a/lib/jeannie/commands.sh b/lib/jeannie/commands.sh index 7e8a476..dd6f83f 100644 --- a/lib/jeannie/commands.sh +++ b/lib/jeannie/commands.sh @@ -3601,10 +3601,10 @@ oci_foundation_config_names=( LAB_OCI_OPEN_MAIL_PORTS LAB_OCI_AUTO_CONFIGURE_EDGE LAB_EDGE_HOST LAB_OCI_AUTO_CONFIGURE_EMAIL LAB_OCI_AUTO_UPDATE_DUCKDNS LAB_DUCKDNS_DOMAINS LAB_OCI_FOUNDATION_STATE_FILE - LAB_OCI_MONITORING_ENABLED LAB_OCI_MONITORING_CIDR + LAB_OCI_MONITORING_ENABLED LAB_OCI_MONITORING_CIDR LAB_OCI_MONITORING_ALARMS_ENABLED LAB_OCI_MONITORING_TOPIC_NAME LAB_OCI_MONITORING_TOPIC_ID LAB_OCI_MONITORING_NOTIFICATION_EMAIL LAB_OCI_BACKUP_BUCKET_ENABLED LAB_OCI_BACKUP_BUCKET LAB_OCI_BACKUP_BUCKET_NAME LAB_OCI_BACKUP_BUCKET_NAMESPACE LAB_OCI_BACKUP_BUCKET_ID LAB_OCI_BASTION_ENABLED LAB_OCI_BASTION_NAME LAB_OCI_BASTION_TARGET_SUBNET_ID LAB_OCI_BASTION_SSH_SOURCE_CIDR - LAB_OCI_BASTION_CLIENT_CIDRS_JSON LAB_OCI_BASTION_MAX_SESSION_TTL LAB_OCI_BASTION_ID LAB_OCI_EDGE_COMPARTMENT_ID LAB_OCI_EDGE_INSTANCE_ID LAB_OCI_EDGE_INSTANCE_MANAGED + LAB_OCI_BASTION_CLIENT_CIDRS_JSON LAB_OCI_BASTION_MAX_SESSION_TTL LAB_OCI_BASTION_ID LAB_OCI_EDGE_COMPARTMENT_ID LAB_OCI_EDGE_INSTANCE_ID LAB_OCI_EMAIL_INSTANCE_ID LAB_OCI_EDGE_INSTANCE_MANAGED LAB_EMAIL_PUBLIC_IP LAB_OCI_EDGE_METRICS_TARGET LAB_OCI_EMAIL_METRICS_TARGET ) @@ -3962,6 +3962,7 @@ oci_foundation_manage() { local discovered_backup_bucket_name local discovered_backup_bucket_namespace local discovered_backup_bucket_id + local discovered_monitoring_topic_id local discovered_edge_instance_id local post_apply_failures=0 discovered_edge_ip="$(tofu -chdir="${stack}" output "${tofu_state_args[@]}" -raw edge_public_ip 2>/dev/null || true)" @@ -3970,7 +3971,10 @@ oci_foundation_manage() { discovered_backup_bucket_name="$(tofu -chdir="${stack}" output "${tofu_state_args[@]}" -raw backup_bucket_name 2>/dev/null || true)" discovered_backup_bucket_namespace="$(tofu -chdir="${stack}" output "${tofu_state_args[@]}" -raw backup_bucket_namespace 2>/dev/null || true)" discovered_backup_bucket_id="$(tofu -chdir="${stack}" output "${tofu_state_args[@]}" -raw backup_bucket_id 2>/dev/null || true)" + discovered_monitoring_topic_id="$(tofu -chdir="${stack}" output "${tofu_state_args[@]}" -raw monitoring_topic_id 2>/dev/null || true)" discovered_edge_instance_id="$(tofu -chdir="${stack}" output "${tofu_state_args[@]}" -raw edge_instance_id 2>/dev/null || true)" + local discovered_email_instance_id + discovered_email_instance_id="$(tofu -chdir="${stack}" output "${tofu_state_args[@]}" -raw email_instance_id 2>/dev/null || true)" if [[ "${discovered_bastion_id}" == ocid1.bastion.* ]]; then export LAB_OCI_BASTION_ID="${discovered_bastion_id}" fi @@ -3982,6 +3986,9 @@ oci_foundation_manage() { export LAB_OCI_EDGE_INSTANCE_MANAGED=false fi fi + if [[ "${discovered_email_instance_id}" == ocid1.instance.* ]]; then + export LAB_OCI_EMAIL_INSTANCE_ID="${discovered_email_instance_id}" + fi if [[ -n "${discovered_backup_bucket_name}" && "${discovered_backup_bucket_name}" != "null" ]]; then export LAB_OCI_BACKUP_BUCKET="${discovered_backup_bucket_name}" export LAB_OCI_BACKUP_BUCKET_NAME="${discovered_backup_bucket_name}" @@ -3992,13 +3999,14 @@ oci_foundation_manage() { if [[ -n "${discovered_backup_bucket_id}" && "${discovered_backup_bucket_id}" != "null" ]]; then export LAB_OCI_BACKUP_BUCKET_ID="${discovered_backup_bucket_id}" fi + if [[ "${discovered_monitoring_topic_id}" == ocid1.onstopic.* ]]; then + export LAB_OCI_MONITORING_TOPIC_ID="${discovered_monitoring_topic_id}" + fi if truthy "${LAB_OCI_BASTION_ENABLED:-true}"; then if [[ "${discovered_edge_instance_id}" == ocid1.instance.* ]]; then printf 'Enabling OCI Bastion plugin on edge instance...\n' oci_enable_bastion_plugin "${discovered_edge_instance_id}" || post_apply_failures=$((post_apply_failures + 1)) fi - local discovered_email_instance_id - discovered_email_instance_id="$(tofu -chdir="${stack}" output "${tofu_state_args[@]}" -raw email_instance_id 2>/dev/null || true)" if [[ "${discovered_email_instance_id}" == ocid1.instance.* ]]; then printf 'Enabling OCI Bastion plugin on email instance...\n' oci_enable_bastion_plugin "${discovered_email_instance_id}" || post_apply_failures=$((post_apply_failures + 1)) diff --git a/lib/jeannie/env.sh b/lib/jeannie/env.sh index 252aa0f..26f47d8 100644 --- a/lib/jeannie/env.sh +++ b/lib/jeannie/env.sh @@ -372,6 +372,11 @@ export_oci_foundation_tf_vars() { 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_monitoring_alarms_enabled "${LAB_OCI_MONITORING_ALARMS_ENABLED:-}" + export_if_unset TF_VAR_monitoring_topic_name "${LAB_OCI_MONITORING_TOPIC_NAME:-}" + export_if_unset TF_VAR_monitoring_notification_email "${LAB_OCI_MONITORING_NOTIFICATION_EMAIL:-}" + export_if_unset TF_VAR_edge_monitoring_instance_id "${LAB_OCI_EDGE_INSTANCE_ID:-}" + export_if_unset TF_VAR_email_monitoring_instance_id "${LAB_OCI_EMAIL_INSTANCE_ID:-}" export_if_unset TF_VAR_backup_bucket_enabled "${LAB_OCI_BACKUP_BUCKET_ENABLED:-}" export_if_unset TF_VAR_backup_bucket_name "${LAB_OCI_BACKUP_BUCKET_NAME:-${LAB_OCI_BACKUP_BUCKET:-}}" export_if_unset TF_VAR_backup_bucket_namespace "${LAB_OCI_BACKUP_BUCKET_NAMESPACE:-}"