https://cloud.google.com/monitoring/agent/ops-agent/installation?hl=ja
https://cloud.google.com/monitoring/docs/samples?hl=ja
https://blog.g-gen.co.jp/entry/how-to-create-monitoring-alerts
-- 1. 前作業
gcloud init
gcloud auth list
gcloud --version
gcloud projects create project01-9999999 \
--name="project01"
gcloud config list
gcloud config set project project01-9999999
gcloud config set compute/region asia-northeast1 --quiet
gcloud config set compute/zone asia-northeast1-a --quiet
gcloud beta billing accounts list
gcloud beta billing projects link project01-9999999 --billing-account=111111-111111-111111
gcloud services enable compute.googleapis.com --project project01-9999999
-- 2. Cloud Logging API と Cloud Monitoring APIを有効化
gcloud services list --enabled
gcloud services enable logging.googleapis.com \
--project project01-9999999
gcloud services enable monitoring.googleapis.com \
--project project01-9999999
-- 3. インスタンス作成
gcloud compute instances create vm01 \
--image-family=debian-11 \
--image-project=debian-cloud \
--zone=asia-northeast1-a
gcloud compute instances list
gcloud compute instances describe vm01
-- 4. 最新バージョンのエージェントをインストールする
gcloud compute ssh vm01
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
-- 5. Ops エージェントが実行されていることを確認する
[Monitoring] -> [VM インスタンス] ダッシュボード
-- 6. エージェント バージョンを確認する
dpkg-query --show --showformat '${Package} ${Version} ${Architecture} ${Status}\n' google-cloud-ops-agent
-- 7. エージェントを再起動する
sudo systemctl status google-cloud-ops-agent
sudo systemctl restart google-cloud-ops-agent
sudo systemctl status google-cloud-ops-agent
-- 8. 通知チャネルの作成
cat <<-'EOF' > a.json
{
"description": "channel01",
"displayName": "channel01",
"type": "email",
"enabled": true,
"labels": {
"email_address": "hoge@example.com"
}
}
EOF
cat a.json
gcloud beta monitoring channels create \
--channel-content-from-file=a.json
gcloud beta monitoring channels list
gcloud beta monitoring channels describe projects/project01-9999999/notificationChannels/111111111111111111
-- 9. アラートポリシーの作成
cat <<-'EOF' > b.json
{
"displayName": "policy11",
"documentation": {
"subject": "test"
},
"userLabels": {},
"conditions": [
{
"displayName": "VM Instance - CPU load (1m)",
"conditionThreshold": {
"aggregations": [
{
"alignmentPeriod": "60s",
"perSeriesAligner": "ALIGN_STDDEV"
}
],
"comparison": "COMPARISON_GT",
"duration": "0s",
"filter": "resource.type = \"gce_instance\" AND metric.type = \"agent.googleapis.com/cpu/load_1m\"",
"thresholdValue": 0.01,
"trigger": {
"count": 1
}
}
}
],
"alertStrategy": {
"notificationPrompts": [
"OPENED"
]
},
"combiner": "OR",
"enabled": true,
"notificationChannels": [
"projects/project01-9999999/notificationChannels/111111111111111111"
]
}
EOF
cat b.json
gcloud alpha monitoring policies create \
--policy-from-file=b.json
gcloud alpha monitoring policies list
gcloud alpha monitoring policies describe projects/project01-9999999/alertPolicies/1111111111111111111
-- 10. 動作確認
CPU高負荷での通知を確認
yes > /dev/null &
※CPU使用率=0のままの場合、エージェント再起動複数回実施
sudo systemctl restart google-cloud-ops-agent
-- 11. クリーンアップ
gcloud compute instances delete vm01 -q
gcloud projects list
gcloud projects delete project01-9999999 -q
gcloud beta billing projects unlink project01-9999999