Quickstart for Compute Engine

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:

  1. Create a Compute Engine VM instance.
  2. Install Apache HTTP Server.
  3. Create a Workspace.
  4. Install the Stackdriver Monitoring and Logging agents.
  5. Create an uptime check with an alerting policy.
  6. Create a custom dashboard and chart.
  7. View your logs.
  8. Clean up.

Before you begin

To use Stackdriver, you need to create a project and enable billing for your project.

To create a project:

  1. In the GCP Console, go to New Project.

    Create a New Project

  2. In the Project Name field, enter Quickstart.

  3. Click Create.

To enable billing for your project:

  1. In the GCP Console, go to Billing.

    Go to Billing

  2. Select your Quickstart project if it isn't already selected at the top of the page.

  3. You are prompted to choose an existing payments profile or to create a new one.

Create a Compute Engine instance

  1. In the GCP Console, go to Compute > Compute Engine.

    Go to Compute Engine

  2. To create a VM instance, click Create.

  3. Fill in the fields for your instance as follows:

    • In the Name field, enter lamp-1-vm.
    • 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.

  4. Click Create. Wait a couple of minutes for your instance to launch on the VM Instances page.

  5. To open a terminal to your instance, in the Connect column, click SSH.

  6. Update the package lists on your instance.

    sudo apt-get update
    
  7. Set up the Apache2 HTTP Server.

    sudo apt-get install apache2 php7.0
    
  8. 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:

    Apache2 default page

Create a Workspace

To use Stackdriver, your project must be in a Workspace. The following steps create a new Workspace:

  1. In the Google Cloud Platform Console, select Stackdriver > Monitoring.

    Go to Monitoring

  2. Your project is not in a Workspace. Select Create a new Workspace and then click Continue.

    Quickstart is not in a Workspace

  3. Select your quickstart project, and then click Create workspace.

    Create your free Workspace

  4. On the Add Google Cloud Platform projects to monitor page, click Continue to skip ahead.

  5. On the Monitor AWS accounts page, click Skip AWS Setup to skip ahead.

  6. On the Install the Stackdriver Agents page, click Continue to skip ahead.

  7. On the Get Reports by Email page, select No reports and then click Continue.

  8. When you see the "Finished initial collection!" message, click Launch monitoring.

    Finished initial collection

    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:

  1. Switch to the terminal connected to your VM instance or create a new one.

  2. Install the Stackdriver Monitoring agent.

    curl -sSO https://dl.google.com/cloudagents/install-monitoring-agent.sh
    sudo bash install-monitoring-agent.sh
    
  3. Install the Stackdriver Logging agent.

    curl -sSO https://dl.google.com/cloudagents/install-logging-agent.sh
    sudo bash install-logging-agent.sh --structured
    

    The --structured flag 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:

  1. Go back to the Stackdriver Monitoring console.

    Go to Monitoring

  2. 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.

  3. 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 Single, lamp-1-vm.
    • Leave the other fields with their default values.

      Create an uptime check

  4. 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.

  5. When you click Save, the following dialog displays:

    Uptime Check Created

Create an alerting policy

  1. In the Uptime Check Created pane, click Create Alerting Policy.

  2. 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.

    Create Condition

  3. Click Save.

  4. In the Notification Channel Type drop-down list, select Email.

    Create new Alerting Policy

  5. Enter your email address and then click Add Notification Channel.

  6. In the Name this policy pane, enter My Uptime Check Policy.

  7. 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.

  1. In the Stackdriver Monitoring console, go to Dashboards > Create dashboard.

    Go to the Create Dashboard page

  2. In the upper-right hand corner, click Add Chart.

  3. In the Add Chart window, click the Metric tab.

    Add chart blank

  4. 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.

  5. When a new chart appears in the Preview section of the panel, click Save.

    Add chart

  6. To create a second chart, click Add Chart.

  7. Click the Metric tab.

  8. In the Find resource type and metric drop-down list, select Received bytes. Leave the other fields with their default values.

  9. When a new chart appears in the Preview section, click Save.

  10. 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:

Uptime check pass

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:

  1. In the Stackdriver Monitoring console, click Logging:

    Go to Stackdriver Logging

  2. Change the Logs Viewer settings to see the logs you want:

    • In the first drop-down list, select G​C​E 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.

      View logs from console

  3. Return to the Stackdriver Monitoring console. To view your logs, in one of your charts, click the menu icon, and then click View logs.

    View logs from chart

Clean up

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:

  1. In the Stackdriver Monitoring console, go to Alerting > Policies Overview.

  2. Click Delete next to the policy that you wish to remove.

To delete your uptime check:

  1. In the Stackdriver Monitoring console, go to Uptime Checks > My Uptime Check.

  2. Click Delete uptime check.

To delete your charts:

  1. In the Stackdriver Monitoring console, go to Dashboards > Stackdriver quickstart dashboard.

  2. For each chart, click Delete.

  3. When the dashboard is empty, click the menu icon, and then click Delete.

  4. In the "Confirm Chart Deletion" panel, click Delete.

Delete your VM instance

  1. In the GCP Console, go to the VM Instances page.

    Go to the VM Instances page

  2. Click the checkbox next to the instance you want to delete.
  3. Click the Delete button at the top of the page to delete the instance.

Delete the project

  1. In the GCP Console, go to the Projects page.

    Go to the Projects page

  2. In the project list, select the project you want to delete and click Delete.
  3. In the dialog, type the project ID, and then click Shut down to delete the project.

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 Stackdriver Logging and its relation to Stackdriver Monitoring, see Logging.

Was this page helpful? Let us know how we did:

Send feedback about...

Stackdriver Monitoring
Need help? Visit our support page.