Monitor an Apache Web Server using Ops Agent
Solution for Monitor an Apache Web Server using Ops Agent. 1 lab: GSP1108. Fast copy-paste commands for Google Cloud.
GSP1108 — Monitor an Apache Web Server using Ops Agent
Estimated time: 15 minutes
# 📊 Monitor Apache Web Server with Cloud Monitoring > ⚠️ **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 C
# ==============================================================================
# Color Variables & Branding
# ==============================================================================
GREEN='\e[1;32m'
CYAN='\e[1;36m'
YELLOW='\e[1;33m'
MAGENTA='\e[1;35m'
RED='\e[1;31m'
RESET='\e[0m'
BOLD='\e[1m'
clear
echo -e "${CYAN}${BOLD}"
cat << "EOF"
____ _ _ _ __ ___
/ __ \ | | (_) | / _| / _ \
| | | |_ __| |__ _| |_ ___ | |_ | | | |_ __ ___
| | | | '__| '_ \| | __| / _ \ | _| | | | | '_ \/ __|
| |__| | | | |_) | | |_ | (_) || | | |_| | |_) \__ \
\____/|_| |_.__/|_|\__| \___/ |_| \___/| .__/|___/
| |
|_|
EOF
echo -e "${RESET}"
echo -e "${MAGENTA}${BOLD}>>> ORBIT OF OPS: GSP1108 PART 1 INITIALIZED <<<${RESET}\n"
echo -e "${YELLOW}${BOLD}[Orbit of Ops] Auto-fetching Project and Zone...${RESET}"
export PROJECT_ID=$(gcloud config get-value project 2>/dev/null)
if [[ -z "$PROJECT_ID" ]]; then
export PROJECT_ID=$DEVSHELL_PROJECT_ID
fi
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 "⚠️ Could not auto-detect Zone. Please enter the lab Zone (e.g., us-east1-b): " ZONE
export ZONE
fi
gcloud config set compute/zone $ZONE 2>/dev/null
echo -e "✅ Project ID: ${GREEN}$PROJECT_ID${RESET}"
echo -e "✅ Zone: ${GREEN}$ZONE${RESET}\n"
echo -e "${CYAN}${BOLD}⚠️ ATTENTION: USER EMAIL REQUIRED FOR ALERTING ⚠️${RESET}"
read -p "Enter your personal email address: " USER_EMAIL
export USER_EMAIL
echo ""
echo -e "${CYAN}${BOLD}[Orbit of Ops] Task 1: Creating 'quickstart-vm' instance & Firewalls...${RESET}"
gcloud compute instances create quickstart-vm \
--zone=$ZONE \
--machine-type=e2-small \
--image-family=debian-11 \
--image-project=debian-cloud \
--tags=http-server,https-server || true
gcloud compute firewall-rules create allow-http-from-internet --target-tags=http-server --allow tcp:80 --source-ranges 0.0.0.0/0 || true
gcloud compute firewall-rules create allow-https-from-internet --target-tags=https-server --allow tcp:443 --source-ranges 0.0.0.0/0 || true
echo -e "${YELLOW}${BOLD}[Orbit of Ops] Waiting 45 seconds for VM to fully boot...${RESET}"
sleep 45
echo -e "${MAGENTA}${BOLD}[Orbit of Ops] Tasks 2 & 3: Installing Apache & Configuring Ops Agent via SSH...${RESET}"
cat << 'EOF_END' > install.sh
sudo apt-get update
sudo apt-get install apache2 php -y
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
set -e
sudo cp /etc/google-cloud-ops-agent/config.yaml /etc/google-cloud-ops-agent/config.yaml.bak
sudo tee /etc/google-cloud-ops-agent/config.yaml > /dev/null << INNEREF
metrics:
receivers:
apache:
type: apache
service:
pipelines:
apache:
receivers:
- apache
logging:
receivers:
apache_access:
type: apache_access
apache_error:
type: apache_error
service:
pipelines:
apache:
receivers:
- apache_access
- apache_error
INNEREF
sudo service google-cloud-ops-agent restart
# Task 4: Run the traffic generator in the background
nohup timeout 120 bash -c -- 'while true; do curl localhost; sleep $((RANDOM % 4)) ; done' > /dev/null 2>&1 &
EOF_END
gcloud compute scp install.sh quickstart-vm:/tmp --zone=$ZONE --quiet
gcloud compute ssh quickstart-vm --zone=$ZONE --quiet --command="bash /tmp/install.sh"
echo -e "\n${GREEN}${BOLD}✅ Part 1 Complete. Check Tasks 1, 2, 3, and 4 progress in the lab window!${RESET}"echo -e "\n${MAGENTA}${BOLD}>>> ORBIT OF OPS: GSP1108 PART 2 INITIALIZED <<<${RESET}\n"
echo -e "${YELLOW}${BOLD}[Orbit of Ops] Task 5: Creating Notification Channel...${RESET}"
cat > email-channel.json <<EOF
{
"type": "email",
"displayName": "orbitofops",
"description": "Alerting Channel",
"labels": {
"email_address": "$USER_EMAIL"
}
}
EOF
gcloud beta monitoring channels create --channel-content-from-file="email-channel.json" > /dev/null 2>&1 || true
echo -e "${YELLOW}${BOLD}[Orbit of Ops] Fetching Notification Channel ID...${RESET}"
export CHANNEL_ID=$(gcloud beta monitoring channels list --format="value(name)" --filter='displayName="orbitofops"' | head -n 1)
echo -e "✅ Notification Channel ID: ${GREEN}$CHANNEL_ID${RESET}"
echo -e "${CYAN}${BOLD}[Orbit of Ops] Creating Alerting Policy for Apache Traffic...${RESET}"
cat > vm-alert-policy.json <<EOF
{
"displayName": "Apache traffic above threshold",
"userLabels": {},
"conditions": [
{
"displayName": "VM Instance - workload/apache.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": 4000
}
}
],
"alertStrategy": {
"autoClose": "1800s"
},
"combiner": "OR",
"enabled": true,
"notificationChannels": [
"$CHANNEL_ID"
],
"severity": "SEVERITY_UNSPECIFIED"
}
EOF
gcloud alpha monitoring policies create --policy-from-file="vm-alert-policy.json" > /dev/null 2>&1 || true
echo -e "\n${GREEN}${BOLD}🎉 All blocks executed successfully! The Alert Policy is created. Click 'Check my progress' on Task 5!${RESET}"