This page shows you how to monitor a Compute Engine virtual machine (VM) instance with Stackdriver. If you want to monitor an Amazon EC2 VM instance, see Quickstart for Amazon EC2.
In this quickstart, you do the following:
- Create a Compute Engine VM instance.
- Install Apache HTTP Server.
- Install the Stackdriver 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 Stackdriver Monitoring, you need to create a GCP project, enable billing for your project, and associate your project with a Workspace.
Create a project
To create a project:
In the GCP Console, go to New Project.
In the Project Name field, enter
To enable billing for your project:
In the GCP Console, go to Billing.
Quickstartproject 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 Workspace
To create a Workspace for an existing GCP project, do the following:
Go to the GCP Console:
In the menu bar, click the drop-down list next to the Google Cloud Platform and select your GCP project.
If the Add your project to a Workspace dialog is displayed, create a new Workspace by selecting your GCP project under New Workspace and then clicking Add. In the following image, the GCP project name is
The Add your project to a Workspace dialog is displayed only when you have at least one existing Workspace available to you. The Workspaces listed under Existing Workspace are Workspaces you've created or Workspaces for GCP projects where you have editorial permission. Using this dialog, you can choose between creating a new Workspace and adding your project to an existing Workspace.
Next, Monitoring creates a new Workspace and adds your GCP project to the Workspace. During Workspace creation, Monitoring proceeds through the following phases:
- Building your Workspace
- Enabling Stackdriver APIs
- We're still collecting data for your new Workspace
These phases might take several minutes to complete. When this process is complete, the Stackdriver Monitoring console displays the Monitoring Overview pane and a welcome message:
Create a Compute Engine instance
In the GCP Console, go to Compute > 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.
- 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 the Stackdriver agents
The Stackdriver 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 the Stackdriver Monitoring agent.
curl -sSO https://dl.google.com/cloudagents/install-monitoring-agent.sh sudo bash install-monitoring-agent.sh
Install the Stackdriver Logging agent.
curl -sSO https://dl.google.com/cloudagents/install-logging-agent.sh sudo bash install-logging-agent.sh --structured
--structuredflag lets the Logging agent send structured data to Stackdriver Logging. For more information, see Structured logging operations.
Create an uptime check
Uptime checks verify that your web server is accessible from locations around the world. The alerting policy controls who is notified if the uptime checks should fail.
To create an alerting policy using that check:
Go back to the Stackdriver Monitoring console.
If you see the invitation Create an Uptime Check on the dashboard, then click it. Otherwise, go to Uptime Checks > Uptime Checks Overview and then click Add Uptime Check or Create an Uptime Check.
Fill in the following fields for the new uptime check:
- In the Title field, enter
My Uptime Check.
- In the Resource Type drop-down list, select Instance.
- In the Applies To field, enter
Leave the other fields with their default values.
- In the Title field, enter
To verify that your uptime check is working, 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.
When you click Save, the following dialog displays:
Create an alerting policy
In the Uptime Check Created pane, click Create Alerting Policy.
In the Untitled Condition field, enter a title for the alert policy condition. All other fields are in the conditions pane are automatically populated from the uptime check you created.
In the Notification Channel Type drop-down list, select Email.
Enter your email address and then click Add Notification Channel.
In the Name this policy pane, enter
My Uptime Check Policy.
Click Save. You see a summary of the policy.
Create a dashboard and chart
Display the metrics collected by Monitoring in your own charts and dashboards.
In the Stackdriver Monitoring console, go to Dashboards > Create dashboard.
In the upper-right hand corner, click Add Chart.
In the Add Chart window, click the Metric tab.
Under Find resource type and metric heading, in the instance, cpu, usage, etc. field, enter CPU, and then select CPU load(1m) from the drop-down list. Leave the other fields with their default values.
When a new chart appears in the Preview section of the panel, click Save.
To create a second chart, click Add Chart.
Click the Metric tab.
In the Find resource type and metric drop-down list, select Received bytes. Leave the other fields with their default values.
When a new chart appears in the Preview section, click Save.
In the new dashboard, change Untitled Dashboard to
Stackdriver quickstart dashboard.
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
Return to the Stackdriver Monitoring console. Wait until the icon next to the My Uptime Check changes from a gray circle with an exclamation mark 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 your logs:
In the Stackdriver Monitoring console, click Logging:
Change the Logs Viewer settings to see the logs you want:
- 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.
Return to the Stackdriver Monitoring console. To view your logs, in one of your charts, click the menu icon, and then click View logs.
To avoid incurring charges to your GCP account for the resources used in this quickstart:
Clean up Stackdriver
Remove your Stackdriver alerting policies, uptime checks, and chart so that you won't get errors when you shut down your VM instance.
To delete your alerting policy:
In the Stackdriver Monitoring console, go to Alerting > Policies Overview.
Click Delete next to the policy that you wish to remove.
To delete your uptime check:
In the Stackdriver Monitoring console, go to Uptime Checks > My Uptime Check.
Click Delete uptime check.
To delete your charts:
In the Stackdriver Monitoring console, go to Dashboards > Stackdriver quickstart dashboard.
For each chart, click Delete.
When the dashboard is empty, click the menu icon, and then click Delete.
In the "Confirm Chart Deletion" panel, click Delete.
Delete your VM instance
- In the GCP Console, go to the VM Instances page.
- Click the checkbox for the instance you want to delete.
- Click Delete delete to delete the instance.
Delete the project
- In the GCP Console, go to the Manage resources page.
- In the project list, select the project you want to delete and click Delete delete.
- In the dialog, type the project ID, and then click Shut down to delete the project.