Cloud Monitoring: Qwik Start

Solution for Cloud Monitoring: Qwik Start. 1 lab: GSP089. Fast copy-paste commands for Google Cloud.

GSP089 — Cloud Monitoring: Qwik Start

Estimated time: 20 minutes

# 📊 Cloud Monitoring: Qwik Start > ⚠️ **Disclaimer:** This is an independent, community-made walkthrough created to help you understand why each step works. Attempt the lab yourself first and use this guide as a learning aid rather than as a shortcut to skip the underlying concepts. This guide is intended for educational purposes, hands-on practice, and Google Cloud certification preparation. It is not affiliated with, endorsed by, or officially supported by Google, Google Cloud, Google Cloud

# ==============================================================================
# ORBIT OF OPS - GSP089 (COMMAND 1 OF 2: VM & OPS AGENT)
# ==============================================================================
CYAN='\e[1;36m'
YELLOW='\e[1;33m'
GREEN='\e[1;32m'
RESET='\e[0m'
BOLD='\e[1m'

clear
echo -e "${CYAN}${BOLD}>>> ORBIT OF OPS: GSP089 INITIATED (PART 1) <<<${RESET}\n"

echo -e "${YELLOW}${BOLD}--- REQUIRED LAB INPUT ---${RESET}"
read -p "Enter your personal email address for the alert: " ALERT_EMAIL

echo -e "\n${YELLOW}${BOLD}[Orbit of Ops] Auto-fetching Project, Zone, and Region...${RESET}"
export PROJECT_ID=$(gcloud config get-value project 2>/dev/null)
export ZONE=$(gcloud compute project-info describe --format="value(commonInstanceMetadata.items[google-compute-default-zone])" 2>/dev/null | tail -n 1)

if [[ -z "$ZONE" ]]; then
    read -p "$(echo -e "${CYAN}${BOLD}Could not auto-detect Zone. Please enter the lab Zone (e.g., europe-west1-c): ${RESET}")" ZONE
    export ZONE
fi

export REGION=${ZONE%-*}
gcloud config set compute/zone $ZONE --quiet
gcloud config set compute/region $REGION --quiet

# Save environment variables securely for Command 2
echo "export ALERT_EMAIL='$ALERT_EMAIL'" > ~/.orbit_env
echo "export ZONE='$ZONE'" >> ~/.orbit_env
echo "export PROJECT_ID='$PROJECT_ID'" >> ~/.orbit_env

echo -e "✅ Project ID: ${GREEN}$PROJECT_ID${RESET}"
echo -e "✅ Zone:       ${GREEN}$ZONE${RESET}"
echo -e "✅ Region:     ${GREEN}$REGION${RESET}"
echo -e "✅ Email:      ${GREEN}$ALERT_EMAIL${RESET}\n"

echo -e "${YELLOW}${BOLD}[Orbit of Ops] Step 1: Configuring HTTP Firewall Rule...${RESET}"
gcloud compute firewall-rules create default-allow-http \
    --direction=INGRESS \
    --priority=1000 \
    --network=default \
    --action=ALLOW \
    --rules=tcp:80 \
    --source-ranges=0.0.0.0/0 \
    --target-tags=http-server \
    --quiet 2>/dev/null || true

echo -e "\n${CYAN}${BOLD}[Orbit of Ops] Step 2: Creating lamp-1-vm with Apache & Ops Agent...${RESET}"
gcloud compute instances create lamp-1-vm \
    --zone=$ZONE \
    --machine-type=e2-medium \
    --image-family=debian-12 \
    --image-project=debian-cloud \
    --tags=http-server \
    --metadata=startup-script='#! /bin/bash
apt-get update
apt-get install apache2 php -y
service apache2 restart
curl -sSO https://dl.google.com/cloudagents/add-google-cloud-ops-agent-repo.sh
bash add-google-cloud-ops-agent-repo.sh --also-install' \
    --quiet

echo -e "\n${GREEN}${BOLD}🎉 COMMAND 1 COMPLETE! Allow ~60 seconds for the startup script to finish installing Apache in the background. You can check your progress for Tasks 1 & 2.${RESET}"
# ==============================================================================
# ORBIT OF OPS - GSP089 (COMMAND 2 OF 2: MONITORING & ALERTS)
# ==============================================================================
CYAN='\e[1;36m'
YELLOW='\e[1;33m'
GREEN='\e[1;32m'
MAGENTA='\e[1;35m'
RESET='\e[0m'
BOLD='\e[1m'

# Load saved variables from Command 1
source ~/.orbit_env

echo -e "\n${CYAN}${BOLD}[Orbit of Ops] Fetching VM External IP Address...${RESET}"
export VM_IP=$(gcloud compute instances describe lamp-1-vm \
    --zone=$ZONE \
    --format='get(networkInterfaces[0].accessConfigs[0].natIP)')

echo -e "${MAGENTA}${BOLD}[Orbit of Ops] Task 3: Creating HTTP Uptime Check...${RESET}"
gcloud monitoring uptime create "Lamp Uptime Check" \
    --resource-type=uptime-url \
    --resource-labels=host=$VM_IP,project_id=$PROJECT_ID \
    --path="/" \
    --period=1 \
    --quiet

echo -e "\n${YELLOW}${BOLD}[Orbit of Ops] Task 4: Creating Notification Channel...${RESET}"
CHANNEL_ID=$(gcloud beta monitoring channels create \
    --display-name="Personal Email" \
    --type=email \
    --channel-labels=email_address=${ALERT_EMAIL} \
    --format="value(name)")

echo -e "${CYAN}${BOLD}[Orbit of Ops] Task 4: Compiling and Deploying Alerting Policy...${RESET}"

# Minified JSON to prevent Cloud Shell clipboard truncation
cat > alert-policy.json <<EOF
{"displayName":"Inbound Traffic Alert","combiner":"OR","conditions":[{"displayName":"Network traffic","conditionThreshold":{"filter":"metric.type=\"agent.googleapis.com/interface/traffic\" AND resource.type=\"gce_instance\"","comparison":"COMPARISON_GT","thresholdValue":500,"duration":"60s","aggregations":[{"alignmentPeriod":"60s","perSeriesAligner":"ALIGN_RATE"}]}}],"notificationChannels":["$CHANNEL_ID"],"documentation":{"content":"Inbound Traffic Alert","mimeType":"text/markdown"}}
EOF

gcloud alpha monitoring policies create --policy-from-file="alert-policy.json" --quiet

echo -e "\n${GREEN}${BOLD}🎉 PIPELINE AUTOMATION COMPLETE! You can now safely click 'Check my progress' on Tasks 3 and 4.${RESET}"