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.
- Create a Workspace.
- 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, you need to create a project and enable billing for your 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 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:
Create a Workspace
To use Stackdriver, your project must be in a Workspace. The following steps create a new Workspace:
In the Google Cloud Platform Console, select Stackdriver > Monitoring.
Your project is not in a Workspace. Select Create a new Workspace and then click Continue.
Select your quickstart project, and then click Create workspace.
On the Add Google Cloud Platform projects to monitor page, click Continue to skip ahead.
On the Monitor AWS accounts page, click Skip AWS Setup to skip ahead.
On the Install the Stackdriver Agents page, click Continue to skip ahead.
On the Get Reports by Email page, select No reports and then click Continue.
When you see the "Finished initial collection!" message, click Launch monitoring.
When you see the "Welcome to Stackdriver Monitoring!" banner and the Stackdriver dashboard, you have successfully created a Stackdriver workspace.
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 25 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 next to the instance you want to delete.
- Click the Delete button at the top of the page to delete the instance.
Delete the project
- In the GCP Console, go to the Projects page.
- In the project list, select the project you want to delete and click Delete.
- In the dialog, type the project ID, and then click Shut down to delete the project.