Monitoring in Google Cloud
Solution for Monitoring in Google Cloud. 1 lab: ARC115. Fast copy-paste commands for Google Cloud.
ARC115 — Monitoring in Google Cloud: Challenge Lab
Estimated time: 20 minutes
# Monitoring in Google Cloud Challenge Lab Follow the execution sequence below exactly as shown to successfully complete all tasks. ## 🚀 100% Automation Execution (Tasks 1, 2, 3 & 5) ### 🔹 Step 1 — Open Google Cloud Shell Open **Google Cloud Shell** from the Google Cloud Console. --- ### 🔹 Step 2 — Execute the Automation Script 1. Copy and paste the **entire command block** provided below into **Google Cloud Shell**. 2. Press **Enter**. The automation script will automatically: - ✅ L
GREEN='\e[1;32m'
CYAN='\e[1;36m'
YELLOW='\e[1;33m'
BLUE='\e[1;34m'
MAGENTA='\e[1;35m'
RESET='\e[0m'
BOLD='\e[1m'
clear
echo -e "${CYAN}${BOLD}>>> ORBIT OF OPS: ARC115 ZERO-UI AUTOMATION (FINAL) <<<${RESET}\n"
# ------------------------------------------------------------------
# Initialization & VM Discovery
# ------------------------------------------------------------------
export PROJECT_ID=$(gcloud config get-value project)
export USER_EMAIL=$(gcloud config get-value account)
export INSTANCE_NAME=$(gcloud compute instances list --format="value(name)" --limit=1)
export ZONE=$(gcloud compute instances list --format="value(zone)" --limit=1)
export VM_EXTERNAL_IP=$(gcloud compute instances describe $INSTANCE_NAME --zone=$ZONE --format='get(networkInterfaces[0].accessConfigs[0].natIP)')
echo -e "${BLUE}${BOLD}[*] Target Instance: $INSTANCE_NAME in $ZONE${RESET}"
echo -e "${BLUE}${BOLD}[*] External IP: $VM_EXTERNAL_IP${RESET}\n"
# ------------------------------------------------------------------
# Task 1: Install & Configure the Modern Ops Agent
# ------------------------------------------------------------------
echo -e "${YELLOW}${BOLD}[Orbit of Ops] Step 1: Installing & Configuring Ops Agent...${RESET}"
cat << 'EOF' > setup_agents.sh
#!/bin/bash
# 1. Install the modern Ops Agent
curl -sSO https://dl.google.com/cloudagents/add-google-cloud-ops-agent-repo.sh
sudo bash add-google-cloud-ops-agent-repo.sh --also-install
# 2. Safely configure Apache + Hostmetrics (Fixes the missing CPU load metric!)
cat << 'YAMLEOF' | sudo tee /etc/google-cloud-ops-agent/config.yaml
logging:
receivers:
syslog:
type: files
include_paths:
- /var/log/messages
- /var/log/syslog
apache_access:
type: apache_access
apache_error:
type: apache_error
service:
pipelines:
default_pipeline:
receivers: [syslog]
apache_pipeline:
receivers: [apache_access, apache_error]
metrics:
receivers:
hostmetrics:
type: hostmetrics
collection_interval: 60s
apache:
type: apache
service:
pipelines:
default_pipeline:
receivers: [hostmetrics]
apache_pipeline:
receivers: [apache]
YAMLEOF
# 3. Restart the agent to apply changes
sudo systemctl restart google-cloud-ops-agent
# 4. Generate Background Traffic for Task 3 Alert Policy Trigger
timeout 120 bash -c -- 'while true; do curl localhost | grep -oP "<title>.*</title>"; sleep .1s;done ' > /dev/null 2>&1 &
EOF
gcloud compute scp setup_agents.sh $INSTANCE_NAME:/tmp --zone=$ZONE --quiet
gcloud compute ssh $INSTANCE_NAME --zone=$ZONE --quiet --command="bash /tmp/setup_agents.sh"
echo -e "${GREEN}${BOLD}✓ Ops Agent installed and background traffic generated!${RESET}\n"
# ------------------------------------------------------------------
# Task 2: Create Uptime Check
# ------------------------------------------------------------------
echo -e "${CYAN}${BOLD}[Orbit of Ops] Step 2: Creating Uptime Check...${RESET}"
gcloud monitoring uptime create "Orbit of Ops Uptime" \
--resource-type=uptime-url \
--resource-labels=host=$VM_EXTERNAL_IP,path=/,port=80 > /dev/null 2>&1
echo -e "${GREEN}${BOLD}✓ Uptime Check active!${RESET}\n"
# ------------------------------------------------------------------
# Task 3: Notification Channel & Alert Policy
# ------------------------------------------------------------------
echo -e "${MAGENTA}${BOLD}[Orbit of Ops] Step 3: Configuring Alert Policy...${RESET}"
cat > email-channel.json <<EOF
{
"type": "email",
"displayName": "Orbit of Ops Alert",
"labels": {
"email_address": "$USER_EMAIL"
}
}
EOF
gcloud beta monitoring channels create --channel-content-from-file=email-channel.json > /dev/null 2>&1
export CHANNEL_ID=$(gcloud beta monitoring channels list --format="value(name)" --limit=1)
cat > alert-policy.json <<EOF
{
"displayName": "Orbit of Ops Traffic Alert",
"userLabels": {},
"conditions": [
{
"displayName": "VM Instance - Traffic",
"conditionThreshold": {
"filter": "resource.type = \"gce_instance\" AND metric.type = \"workload.googleapis.com/apache.traffic\"",
"aggregations": [
{
"alignmentPeriod": "60s",
"crossSeriesReducer": "REDUCE_NONE",
"perSeriesAligner": "ALIGN_RATE"
}
],
"comparison": "COMPARISON_GT",
"duration": "0s",
"trigger": {
"count": 1
},
"thresholdValue": 3072
}
}
],
"alertStrategy": {
"autoClose": "1800s"
},
"combiner": "OR",
"enabled": true,
"notificationChannels": [
"$CHANNEL_ID"
],
"severity": "SEVERITY_UNSPECIFIED"
}
EOF
gcloud alpha monitoring policies create --policy-from-file="alert-policy.json" > /dev/null 2>&1
echo -e "${GREEN}${BOLD}✓ Alert Policy deployed!${RESET}\n"
# ------------------------------------------------------------------
# Task 5: Log-Based Metric
# ------------------------------------------------------------------
echo -e "${BLUE}${BOLD}[Orbit of Ops] Step 4: Creating Log-Based Metric...${RESET}"
gcloud logging metrics create apache_requests \
--description="Count Apache 200 OK responses" \
--log-filter="resource.type=\"gce_instance\" logName=\"projects/$PROJECT_ID/logs/apache-access\" textPayload:\"200\"" > /dev/null 2>&1
echo -e "${GREEN}${BOLD}✓ Log-based metric created!${RESET}\n"
# Cleanup
rm setup_agents.sh email-channel.json alert-policy.json
echo -e "${YELLOW}${BOLD}>>> SCRIPT COMPLETE! Please execute the quick manual UI steps below for Task 4. <<<${RESET}"