Quickstart for Google Compute Engine

This page shows you how to monitor a Google Compute Engine virtual machine (VM) instance with Stackdriver. If instead you want to monitor an Amazon EC2 VM instance, then 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 dashboard, go to New Project:

    Create a New Project

  2. Set the Project Name field to Quickstart.

  3. Click Create.

To enable billing for your project:

  1. In the GCP Console dashboard, go to Billing:

    Go to Billing

  2. Select your Quickstart project if it is not already selected at the top of the page.

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

Create a Compute Engine instance

In the GCP Console dashboard, go to Compute > Compute Engine:

Go to Google Compute Engine

  1. Select your Quickstart project if it is not already selected at the top of the page.

  2. Create a VM instance by clicking Create.

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

    • Name: lamp-1-vm
    • Machine type: Small
    • Firewall: Select both Allow HTTP traffic and Allow HTTPS traffic

    Leave the rest of the fields with their default values.

  4. Click Create. You see the VM Instances page. Wait a couple of minutes for your instance to be launched. If you try browsing to the instance's External IP address, you get an error. You are about to fix that.

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

  6. Run the following command to update the package lists on your instance.

    sudo apt-get update
    
  7. Run the following command on your instance to set up Apache2 HTTP Server:

    sudo apt-get install apache2 php7.0
    
  8. Identify the instance's External IP address. Open your browser, and connect to your Apache2 HTTP server using the URL http://[External IP]. 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

    Because your project is not in a Workspace, you see the following message:

    Quickstart is not in a Workspace

  2. Select Create a new Workspace and then click Continue. You see the following panel:

    Create your free Workspace

  3. With your quickstart project shown, click Create workspace.

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

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

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

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

  8. You see the message "Gathering information..." at the top of the page. In a few seconds, you see the following message:

    Finished initial collection

    Click Launch monitoring.

  9. You see the "Welcome to Stackdriver Monitoring!" banner and the Stackdriver dashboard.

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 by running the following commands on your instance:

    curl -sSO https://dl.google.com/cloudagents/install-monitoring-agent.sh
    sudo bash install-monitoring-agent.sh
    
  3. Install the Stackdriver Logging agent by running the following commands on your instance:

    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, select Uptime Checks > Uptime Checks Overview from the left menu and then click Add Uptime Check or Create an Uptime Check. You see the New Uptime Check panel:

    Create an uptime check

  3. Fill in the following fields for the new uptime check:

    • Title: My Uptime Check
    • Check type: HTTP
    • Resource Type: Instance
    • Applies To: Single, lamp-1-vm
    • Path: /index.html
    • Check every: 5 minutes
    • Leave the other fields with their default values.
  4. Click Test to verify that your uptime check is working. 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. Click Save — you see the following panel:

    Uptime Check Created

Create an alerting policy

  1. In the Uptime Check Created pane, click Create Alerting Policy and you see the following dialog:

    Create Condition

  2. Place your cursor over the text Untitled Condition and enter a title for the alert policy condition, or click on the suggested title.

  3. Click Save without making any other changes in the Condition dialog. You see the following dialog:

    Create new Alerting Policy

  4. Click Add Notification Channel. From the drop-down menu, select email and add your email address.

  5. Skip Documentation.
  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, select Dashboards > Create dashboard.

    Go to the Create Dashboard page

  2. Click Add Chart, which is located in the upper right-hand corner. You see the Add Chart panel:

    Add chart blank

  3. Select the Metric pane.

  4. Under the heading Find resource type and metric, place your mouse pointer on the grayed text instance, cpu, usage, etc., click and begin typing CPU. When the option CPU load(1m) appears in the drop-down menu, select it. Leave the other fields with their default values. You see a new chart in the Preview section of the panel.

    Add chart

  5. Click Save.

  6. Create a second chart. Select Add Chart in the upper-right menu of the new dashboard.

  7. Select Metric. In the Find resource type and metric drop-down list, select Received bytes. Leave the other fields with their default values. You see a new chart in the Preview section.

  8. Click Save.

  9. 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 grey 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 My Uptime Check icon is green. Test the check and alert.

Go to the VM Instances page, select your instance, and click Stop from the top menu.

12:25 Alert notification received.

Correct the "problem" by restarting the VM. Return to the VM Instances page, select your instance, and click Start from the top menu.

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 left-hand menu, click Logging:

    Go to Stackdriver Logging

  2. You see the Logs Viewer.

  3. Change the Logs Viewer focus to see the logs you want:
    • Select G​C​E VM Instance > lamp-1-vm in the first drop-down menu.
    • Select syslog in the second drop-down menu, and click OK.
    • Leave the other fields with their default values.
  4. You see the logs from your VM instance:

    View logs from console

  5. Return to the Stackdriver Monitoring console. In one of your charts, open the settings menu and select View logs:

    View logs from chart

  6. You are shown the logs that correspond to the time span and data in the chart.

Clean up

To avoid incurring charges to your GCP account for the resources used in this quickstart:

  1. 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 left-hand menu, select Alerting > Policies Overview.
      • In your Alerting Policies Overview page, click Delete next to the policy that you wish to remove.
    • To delete your uptime check:
      • In the Stackdriver Monitoring console left-hand menu, select Uptime Checks > My Uptime Check.
      • In the upper right-hand menu, select Delete uptime check.
    • To delete your charts:
      • In the Stackdriver Monitoring console left-hand menu, select Dashboards > Stackdriver quickstart dashboard.
      • In the upper right-hand menu of each chart, select Delete.
      • When the dashboard is empty, select Delete in the settings menu on the dashboard.
      • In the "Confirm Chart Deletion" panel, click Delete.
  2. Stop your VM instance by visiting the Compute Engine VM Instances page:

    Go to Google Compute Engine VM instances

    • Select your instance.
    • Click Stop or Delete at the top of the page. You are not be charged for a stopped instance.
  3. To completely remove this Quickstart exercise, delete the project you created:

    • In the console's home page, click Go to project settings in the "Project Info" card.
    • Click Shut Down.
    • In the "Shut down project..." panel, confirm that you want to delete your project by typing your project ID in the text box.
    • Click Shut Down.

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