Monitor a Compute Engine virtual machine
Learn how to monitor a Compute Engine virtual machine (VM) instance with Cloud Monitoring.
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 Apache HTTP Server.
- Install the Cloud Monitoring and Logging agents.
- Create an uptime check with an alerting policy.
- Create a custom dashboard and chart.
- View your logs.
- Clean up.
For step-by-step guidance on this task directly in Cloud console, click Guide me:
The following sections take you through the same steps as clicking Guide me.
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.
To use Cloud Monitoring, you need to create a Google Cloud project, and enable billing for your project. To create a Cloud project and enable billing, do the following:
In the Cloud console, go to New Project:
In the Project Name field, enter
Quickstartand then click Create.
Go to Billing:
QuickstartCloud project if it isn't already selected at the top of the page.
You are prompted to choose an existing payments profile or to create a new one.
Create a Compute Engine instance
In the Cloud console, go to Compute and then select Compute Engine:
To create a VM instance, click Create.
Fill in the fields for your instance as follows:
- In the Name field, enter
- 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. Wait a couple of minutes for your instance to launch on the VM Instances page.
To open a terminal to your instance, in the Connect column, click SSH.
Update the package lists on your instance.
sudo apt-get update
Set up the Apache2 HTTP Server.
sudo apt-get install apache2 php7.0
Open your browser and connect to your Apache2 HTTP server by using the URL
http://[External IP]. Replace
[External IP]with the external IP address of your Compute Engine instance. You see the Apache2 default page:
The instructions in this section are specific to Debian. See Installing the Cloud Monitoring agent and Installing the Cloud Logging agent for installation instructions for other operating systems, links to troubleshooting guides, and steps to verify agent installation.
The Cloud Monitoring and Logging agents pass logs and metrics from your VM instance to Monitoring and Logging:
Switch to the terminal connected to your VM instance or create a new one.
Install and start the Cloud Monitoring agent:
Add the package repository and update the package list:
curl -sSO https://dl.google.com/cloudagents/add-monitoring-agent-repo.sh sudo bash add-monitoring-agent-repo.sh sudo apt-get update
Install the agent:
sudo apt-get install stackdriver-agent
Start the agent:
sudo service stackdriver-agent start
Install, configure, and start the Cloud Logging agent:
Add the package repository and update the package list:
curl -sSO https://dl.google.com/cloudagents/add-logging-agent-repo.sh sudo bash add-logging-agent-repo.sh sudo apt-get update
Install the agent:
sudo apt-get install google-fluentd
Install the default agent configuration for ingesting structured data to Cloud Logging:
sudo apt-get install google-fluentd-catch-all-config-structured
The Cloud Logging agent must be configured to ingest structured data or unstructured data. These are exclusive options. If you configure the agent for unstructured data, then all log entries contain a
textPayloadfield. However, if you configure the agent for structured data, as is done in this quickstart, then certain types of log data contain a
jsonPayloadfield. For more information, see Structured logging operations.
Start the agent
sudo service google-fluentd start
Create an uptime check and an alerting policy
To create an uptime check, do the following:
In the Cloud console, select Monitoring:
In the navigation pane, click Uptime checks.
Click Create Uptime check:
For the title, enter
My Uptime Checkand then click Next.
- Select HTTP as the protocol.
- Select Instance as the Resource Type.
- In the Applies To field, select Single and for the instance name select lamp-1-vm.
- Leave all other fields at their default values and click Next.
Response Validation: Leave these fields at their default values and click Next
Alert & Notification:
- Ensure that the toggle's label is Alerting is enabled.
- Leave the name and duration fields at their default values.
To add a notification channel to the alerting policy, in the text box labeled Notification channels, click arrow_drop_down Menu. Select the channels to add and click OK. The notifications are grouped alphabetically for each channel type.
To add an entry to the checkbox list, click Manage notification channels and follow the instructions. When you return to this dialog, click refresh Refresh.
To verify your uptime check configuration, click Test. If you see a "Connection error - refused" message, you might have not installed the Apache HTTP Server or you might have specified the HTTPS check type rather than HTTP. For other errors, see Verify your uptime check.
Click Create. When the create action is successful, the message Check and alert created is displayed and then the Uptime checks dashboard page is displayed.
In the uptime checks dashboard, your new uptime check is listed. If you click the check name, then you open the detail view for that uptime check. This view displays several charts, shows the uptime percentage and the configuration information, and lists the configured alert policies. To view a policy, click its name.
You can also view the alert policy by starting at the Alerting page. From the alerting page, the Policies pane lists a subset of policies. To view a list of all policies, click See all policies.
Create a dashboard and chart
- In the Google Cloud console, go to the Monitoring page.
- In the navigation pane, select Dashboards and click Create dashboard.
- Optional: Update the dashboard title with a descriptive name for your dashboard.
- Drag the Line chart widget from the Widget library to the graph area.
Typically, the chart is configured to display the CPU utilization of a Compute Engine virtual machine:
- Expand the Resource and Metric menu, enter
CPUin the filter bar, and then use the submenus to select a specific resource type and metric:
- In the Active resources menu, select VM instance.
- In the Active metric categories menu, select Instance.
- In the Active metrics menu, select CPU load (1m).
- Click Apply.
- Optional: To configure how the data is viewed, use the Filter, Group By, Aggregator, and chart type menus. For example, you can group by resource or metric labels. For more information, see Selecting metrics.
- Optional: For each additional widget that you want to add to your
dashboard, do the following:
- In the dashboard toolbar, click Add chart.
- Select a widget from the menu.
- Configure the widget.
Test the check and alert
This procedure can take up to 40 minutes. A sample timeline is included for your reference. In the timeline, the current time is 12:00.
- 12:00 Wait
Go to Monitoring. In the Uptime checks tile, wait until the icon for the My Uptime Check changes to a green circle with a check mark. The maximum wait time for this stage is 5 minutes:
- 12:15 The My Uptime Check icon is green. Test the check and alert.
Go to the VM Instances page, select your instance, and click Stop.
- 12:25 Alert notification received.
Correct the "problem" by restarting the VM. Return to the VM Instances page, select your instance, and click Start.
- 12:40 Incident Resolved
Received second alert notification.
View your logs
Monitoring and Logging are closely integrated.
To view the logs for the resources displayed in a chart, do the following:
Go to the dashboard that displays the chart of interest.
In the chart, click More more_vert, and then click View logs:
Alternatively, you can go to Logging, and then specify the filter parameters:
In the Cloud console, go to Logging:
Change the Logs Explorer settings to see the logs you want:
- Click menu menu and select Clear filters and return to basic mode.
- In the first drop-down list, select GCE VM Instance, lamp-1-vm.
- In the second drop-down list, select syslog, and click OK.
Leave the other fields with their default values. The logs from your VM instance display.
To avoid incurring charges to your Google Cloud account for the resources used on this page, follow these steps.
Delete the project
If you created a new Cloud project for this quickstart, then delete the Cloud project. Otherwise, skip this section.
- In the 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.
Delete quickstart resources
If you used an existing Cloud project for this quickstart, delete your alerting policy, uptime checks, dashboard, and VM instance:
Go to Monitoring:
Delete the alerting policy:
- Select Alerting.
- Select My Uptime Check Policy and then click Delete delete.
Delete the uptime check:
- Select Uptime Checks.
- Select My Uptime check and then click Delete delete.
Delete the dashboard:
- Select Dashboards.
- For the dashboard named Quickstart dashboard, click Delete delete. Confirm the deletion operation.
Delete the VM instance you created:
- In the 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.
To use the Monitoring API, see the API reference.
For more information on Cloud Logging and its relation to Cloud Monitoring, see Logging.