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. In the Project Name field, replace My Project with Lamp Quickstart. This example has a Project Name of Lamp Quickstart 41931.

  3. Click Create.

To enable billing for your project:

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

    Go to Billing

  2. Select your Lamp 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 Lamp 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:

    Lamp 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 Logging and Monitoring:

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

  2. Install the Stackdriver Monitoring and Logging agents by running the following commands on your instance:

    Monitoring agent

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

    Logging agent

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

Create an uptime check and an alerting policy

Uptime checks verify that your web server is always accessible. The alerting policy controls who is notified if the uptime checks should fail.

To create an uptime check and an alerting policy:

  1. Go back to the Stackdriver Monitoring console.

  2. Click Create an Uptime Check on the dashboard. Otherwise, go to Uptime Checks in the left-hand menu, then select Uptime Checks Overview and then click Create an Uptime Check in the new page.

    You see the New Uptime Check panel:

    Create an uptime check

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

    • Title: Lamp Uptime Check
    • Check type: HTTP
    • Resource Type: Instance
    • Applies To: Single, lamp-1-vm
    • Check every: 1 minute
    • Leave the other fields with their default values.
  4. Click Test to verify your uptime check is working. If you see a Connection error - refused message, you might have not installed the Apache HTTP Server. For other errors, see Uptime checks.

  5. Click Save.

  6. You see the following panel — click Create Alerting Policy:

    Uptime Check Created

  7. You see the following dialog:

    Create new Alerting Policy

  8. Fill in the sections as follows:

    • The Conditions section is already set up. You don't have to change it. If you are curious, click Edit and see some parameters that affect the uptime check.

    • In the Notifications section, click Add Notification and fill in your email address.

    • In the Documentation section, click Add Documentation and enter: Stackdriver LAMP quickstart example.
    • In the Name this policy section, you can accept the default Uptime Check Policy.
  9. Click Save Policy.

Create a dashboard and chart

Display the metrics collected by Monitoring in your own charts and dashboards:

  1. In the left-hand menu of Stackdriver Monitoring console, select Dashboards > Create Dashboard.

    Go to the Create Dashboard page

  2. Click Add Chart. You see the Add Chart panel:

    Add chart blank

  3. Select Metric. In the Find resource type and metric drop-down list, select CPU load(1m). Leave the other fields with their default values. You see a new chart in the Preview section of the panel.

    Add chart

  4. Click Save.

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

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

  7. Click Save.

  8. In the new dashboard, change Untitled Dashboard to Stackdriver LAMP 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 Lamp Uptime Check changes from a grey circle with an exclamation mark to a green circle with a check mark:

Uptime check pass

12:15 Lamp 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 Google Cloud Platform 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 > Lamp 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 LAMP 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 will 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