This page shows you 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.
Before you begin
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
Quickstart
and then click Create.Go to Billing:
Select your
Quickstart
Cloud 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
lamp-1-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. 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:
Install agents
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
textPayload
field. However, if you configure the agent for structured data, as is done in this quickstart, then certain types of log data contain ajsonPayload
field. 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:
If you have never used Cloud Monitoring, then on your first access of Monitoring in the Google Cloud Console, a Workspace is automatically created and your project is associated with that Workspace. Otherwise, if your project isn't associated with a Workspace, then a dialog appears and you can either create a Workspace or add your project to an existing Workspace. We recommend that you create a Workspace. After you make your selection, click Add.
Click Uptime checks.
Click Create Uptime check.
For the title, enter
My Uptime Check
and then click Next.Target:
- 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 Menu arrow_drop_down. 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
To display the metrics collected by Monitoring, create a chart and a dashboard:
There are two dashboard editors: Classic and Preview. The Preview editor is being deployed gradually. The Preview editor supports a richer set of dashboard widgets than the Classic editor.
Preview
To create a dashboard using the preview editor, follow these steps:
Go to Monitoring
In the Dashboards Overview page, click Create dashboard.
Ensure Preview is displayed in the dashboard toolbar.
(Optional) Replace
Untitled Dashboard
with a descriptive name for your dashboard.Add a Line chart chart to the dashboard by dragging that entry from the Widget library to the graph area.
In the section titled What data do you want to view?, do the following:
- Select GCE VM Instance for the resource type.
- Enter CPU, and then select CPU Utilization for the metric.
The following example illustrates a Line chart:
You can use your pointer to resize or re-position the chart.
To add a second Line chart chart to the dashboard, click the dashboard toolbar to enable the Widget library, select a plot type, and then drag that entry from the Widget library to the graph area. For example, you could make the following selections:
- Select GCE VM Instance for the resource type.
- Select Received bytes for the metric.
Click Save dashboard.
Classic
To create a dashboard using the classic editor, follow these steps:
Go to Monitoring
Select Dashboards and then select Create dashboard.
Ensure Classic is displayed in the dashboard toolbar.
(Optional) Enter a name for the dashboard and click Confirm.
Click Add Chart.
Ensure the Metric tab is selected.
Under Find resource type and metric heading, click instance, cpu, usage, etc.:
- Select GCE VM Instance for the resource type.
- Enter CPU, and then select CPU Utilization for the metric.
Click Save.
To create a second chart, click Add Chart, ensure the Metric tab is selected, and then enter a resource an metric. For example, you could make the following selections:
- Select GCE VM Instance for the resource type.
- Select Received bytes for the metric.
Click Save.
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 Viewer 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.
Clean up
To avoid incurring charges to your Google Cloud account for the resources used in this quickstart, 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.
- Click the checkbox for the instance you want to delete.
- Click delete Delete to delete the instance.
What's next
See Supported Metrics for a list of metrics. If you want to create your own Monitoring metrics, see Custom metrics.
To use the Monitoring API, see the API reference.
For more information on Cloud Logging and its relation to Cloud Monitoring, see Logging.
Read our resources about DevOps and explore our research program.