Monitor a Compute Engine virtual machine
This document shows you how to monitor an Apache Web Server installed on a Compute Engine virtual machine (VM) instance.
If you want to monitor an Amazon EC2 VM instance, see the quickstart Getting started with AWS monitoring.
In this quickstart, you do the following:
- Create a Compute Engine VM instance.
- Install an Apache Web Server.
- Install and configure the Ops Agent for the Apache Web Server.
- Generate traffic and view metrics on the predefined Apache dashboard.
- Create an alerting policy.
- Clean up.
Before you begin
Some of the steps in this document might not work correctly if your organization applies constraints to your Google Cloud environment. In that case, you might not be able to complete tasks like creating public IP addresses or service account keys. If you make a request that returns an error about constraints, see how to Develop applications in a constrained Google Cloud environment.
You must have a Google Cloud project with billing enabled to complete this quickstart. If you don't have a Google Cloud project, or if you don't have billing enabled for your Google Cloud project, then do the following:
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project. Learn how to check if billing is enabled on a project.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project. Learn how to check if billing is enabled on a project.
If you have an email notification channel configured, then skip this step.
To set up an email notification channel, do the following:
In the Google Cloud console, select Cloud Monitoring or click the following button:
Go to Monitoring- Select Alerting.
- Click edit Edit notification channels.
- In the Email section, click Add new and complete the dialog.
Create a Compute Engine VM instance
We recommend that you create a new Compute Engine VM instance for this quickstart because you install and configure software.
In the Google Cloud console, go to Compute and then select Compute Engine:
To create a VM instance, click Create instance.
Fill in the fields for your instance as follows:
- In the Name field, enter
quickstart-vm
. - In the Machine type field, select Small.
- Ensure the Boot disk is configured for Debian GNU/Linux.
- In the Firewall field, select both Allow HTTP traffic and Allow HTTPS traffic.
Leave the rest of the fields at their default values.
- In the Name field, enter
Click Create. When your VM is ready, it appears in the list of instances in the Instances tab.
Install an Apache Web Server
To deploy an Apache Web Server on your Compute Engine VM instance, do the following:
To open a terminal to your instance, in the Connect column, click SSH.
To update the package lists on your instance, run the following command:
sudo apt-get update
To install an Apache2 HTTP Server, run the following command:
sudo apt-get install apache2 php7.0
Open your browser and connect to your Apache2 HTTP server by using the URL
http://EXTERNAL_IP
, whereEXTERNAL_IP
is the external IP address of your VM. You can find this address in the External IP column of your VM instance.You see the Apache2 default page:
Install and configure the Ops Agent
To collect logs and metrics from your Apache Web Server, install the Ops Agent by using the terminal:
To open a terminal to your VM instance, in the Connect column, click SSH.
To install the Ops Agent, run the following command:
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
You see
google-cloud-ops-agent installation succeeded.
Copy the following command, then paste it into the terminal:
The previous command creates the configuration to collect and ingest logs and metrics from the Apache Web Server. For more information about ingesting logs from the Apache Web Server, see Configure the Ops Agent for Apache Web Server.
Generate traffic and view metrics
Monitoring dashboards let you view and analyze metrics related to your services. In this quickstart, you generate metrics on your Apache Web Server and view metric data on the automatically created Apache GCE Overview dashboard.
To generate metrics on your Apache Web Server, do the following:
In the Google Cloud console, go to Compute Engine:
In the Connect column, click SSH to open a terminal to your VM instance.
To generate traffic on your Apache Web Server, run the following command:
timeout 120 bash -c -- 'while true; do curl localhost; sleep $((RANDOM % 4)) ; done'
The previous command generates traffic by making a request to the Apache Web Server every four seconds.
To view the Apache GCE Overview dashboard, do the following:
In the Google Cloud console, select Monitoring or click the following button:
Go to MonitoringIn the navigation pane, select
Dashboards.
In All Dashboards, select the Apache GCE Overview dashboard. The dashboard opens.
In the dashboard, there are several charts that contain information about your Apache and Compute Engine integration:
Create an alerting policy
To create an alerting policy that monitors a metric and sends an email notification when the traffic rate on your Apache Web Server exceeds 4 KiB/s, do the following:
In the Google Cloud console, select Monitoring or click the following button:
Go to MonitoringIn the navigation pane, select notifications Alerting and then click Create policy.
Select the time series to be monitored:
Click Select a metric and enter VM instance into the filter bar.
In the Active metric categories list, select Apache.
In the Active metrics list, select workload/apache.traffic.
The chart for Apache traffic is shown.
In the Transform data section, select the following values:
Rolling window: 1 min
Rolling window function: rate
In the Configure alert trigger section, select the following values and click Next:
Alert trigger: Any time series violations
Threshold position: Above threshold
Threshold value: 4000
In the Configure notifications and finalize alert section, select the following values:
Notification channels: The email you want to receive alerts to.
Incident autoclose duration: 30 min
Name the alert policy: Apache traffic above threshold
Click Create policy. Your alerting policy is now active.
Test the alerting policy
To test the alerting policy you just created, do the following:
In the Google Cloud console, go to Compute and then select Compute Engine:
In the Connect column, click SSH to open a terminal to your VM instance.
In the terminal, enter the following command:
timeout 120 bash -c -- 'while true; do curl localhost; sleep $((RANDOM % 4)) ; done'
The previous command generates traffic in your Apache Web Server.
After the traffic rate threshold value of 4 KiB/s is exceeded in your Apache Web Server, an email notification is sent. It might take several minutes for this process to complete.
The email notification you receive looks similar to the following:
Clean up
To avoid incurring charges to your Google Cloud account for the resources used on this page, follow these steps.
If you created a new Google Cloud project for this quickstart, then delete the Google Cloud project:
- In the Google Cloud console, go to the Manage resources page.
- In the project list, select the project that you want to delete, and then click Delete.
- In the dialog, type the project ID, and then click Shut down to delete the project.
If you created a new VM in an existing project, then delete the VM instance:
- In the Google Cloud console, go to the VM instances page.
- Select the checkbox for the instance that you want to delete.
- To delete the instance, click More actions, click Delete, and then follow the instructions.
If you used an existing VM instance for this quickstart, then uninstall the Ops Agent:
To uninstall the Ops Agent, open the terminal and run the following command:
sudo bash add-google-cloud-ops-agent-repo.sh --uninstall
What's next
To learn about creating custom dashboards, see Manage custom dashboards.
To learn how to use Cloud Logging to view logs in your Apache Web Server installed on a Compute Engine VM, see Cloud Logging for Compute Engine VMs.
For more information on Logging and its relation to Monitoring, see Logging.
For a complete list of metrics supported by Google Cloud, see Metrics list. If you want to create your own Monitoring metrics, see Custom metrics.