-
Notifications
You must be signed in to change notification settings - Fork 361
/
Cloud Monitoring Qwik Start
111 lines (87 loc) · 3.3 KB
/
Cloud Monitoring Qwik Start
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
export ZONE=
gcloud compute instances create lamp-1-vm \
--project=$DEVSHELL_PROJECT_ID \
--zone=$ZONE \
--machine-type=e2-small \
--network-interface=network-tier=PREMIUM,stack-type=IPV4_ONLY,subnet=default \
--metadata=enable-oslogin=true \
--maintenance-policy=MIGRATE \
--provisioning-model=STANDARD \
--tags=http-server \
--create-disk=auto-delete=yes,boot=yes,device-name=lamp-1-vm,image=projects/debian-cloud/global/images/debian-12-bookworm-v20240709,mode=rw,size=10,type=projects/$DEVSHELL_PROJECT_ID/zones/$ZONE/diskTypes/pd-balanced \
--no-shielded-secure-boot \
--shielded-vtpm \
--shielded-integrity-monitoring \
--labels=goog-ec-src=vm_add-gcloud \
--reservation-affinity=any
gcloud compute firewall-rules create allow-http \
--project=$DEVSHELL_PROJECT_ID \
--direction=INGRESS \
--priority=1000 \
--network=default \
--action=ALLOW \
--rules=tcp:80 \
--source-ranges=0.0.0.0/0 \
--target-tags=http-server
sleep 10
gcloud compute config-ssh --project "$DEVSHELL_PROJECT_ID" --quiet
gcloud compute ssh lamp-1-vm --project "$DEVSHELL_PROJECT_ID" --zone $ZONE --command "sudo sed -i '/buster-backports/d' /etc/apt/sources.list && sudo apt-get update && sudo apt-get install apache2 php7.3 -y && sudo service apache2 restart"
sleep 10
INSTANCE_ID="$(gcloud compute instances describe lamp-1-vm --project=$DEVSHELL_PROJECT_ID --zone=$ZONE --format='value(id)')"
gcloud monitoring uptime create lamp-uptime-check \
--resource-type="gce-instance" \
--resource-labels=project_id=$DEVSHELL_PROJECT_ID,instance_id=$INSTANCE_ID,zone=$ZONE
cat > email-channel.json <<EOF_END
{
"type": "email",
"displayName": "btecky",
"description": "hi",
"labels": {
"email_address": "$USER_EMAIL"
}
}
EOF_END
gcloud beta monitoring channels create --channel-content-from-file="email-channel.json"
# Run the gcloud command and store the output in a variable
channel_info=$(gcloud beta monitoring channels list)
# Extract the channel ID using grep and awk
channel_id=$(echo "$channel_info" | grep -oP 'name: \K[^ ]+' | head -n 1)
cat > app-engine-error-percent-policy.json <<EOF_END
{
"displayName": "Inbound Traffic Alert",
"userLabels": {},
"conditions": [
{
"displayName": "VM Instance - Network traffic",
"conditionThreshold": {
"filter": "resource.type = \"gce_instance\" AND metric.type = \"agent.googleapis.com/interface/traffic\"",
"aggregations": [
{
"alignmentPeriod": "300s",
"crossSeriesReducer": "REDUCE_NONE",
"perSeriesAligner": "ALIGN_RATE"
}
],
"comparison": "COMPARISON_GT",
"duration": "60s",
"trigger": {
"count": 1
},
"thresholdValue": 500
}
}
],
"alertStrategy": {},
"combiner": "OR",
"enabled": true,
"notificationChannels": [
"$channel_id"
],
"severity": "SEVERITY_UNSPECIFIED"
}
EOF_END
gcloud alpha monitoring policies create --policy-from-file="app-engine-error-percent-policy.json"
INSTANCE_ID=$(gcloud compute instances describe lamp-1-vm --zone=$ZONE --format='value(id)')
gcloud monitoring uptime create lamp-uptime-check \
--resource-type="gce-instance" \
--resource-labels=project_id=$DEVSHELL_PROJECT_ID,instance_id=$INSTANCE_ID,zone=$ZONE