Monitor a Compute Engine virtual machine

Learn how to monitor a Compute Engine virtual machine (VM) instance with Cloud Monitoring.

If you want to monitor an Amazon EC2 VM instance, see the quickstart Getting started with AWS monitoring.

In this quickstart, you do the following:

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

For step-by-step guidance on this task directly in console, click Guide me:

Guide me

The following sections take you through the same steps as clicking Guide me.

Before you begin

Some of the steps in this document might not work correctly if your organization applies constraints to your Google Cloud environment. In that case, you might not be able to complete tasks like creating public IP addresses or service account keys. If you make a request that returns an error about constraints, see how to Develop applications in a constrained Google Cloud environment.

To use Cloud Monitoring, you need to create a Google Cloud project, and enable billing for your project. To create a Cloud project and enable billing, do the following:

  1. In the console, go to New Project:

    Create a New Project

  2. In the Project Name field, enter Quickstart and then click Create.

  3. Go to Billing:

    Go to Billing

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

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

Create a Compute Engine instance

  1. In the console, go to Compute and then select 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.
    • Ensure the Boot disk is configured for Debian GNU/Linux.
    • 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:

    Display the Apache2 default page.

Install agents

The instructions in this section are specific to Debian. See Installing the Cloud Monitoring agent and Installing the Cloud Logging agent for installation instructions for other operating systems, links to troubleshooting guides, and steps to verify agent installation.

The Cloud 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 and start the Cloud Monitoring agent:

    • Add the package repository and update the package list:

      curl -sSO
      sudo bash
      sudo apt-get update
    • Install the agent:

      sudo apt-get install stackdriver-agent
    • Start the agent:

      sudo service stackdriver-agent start
  3. Install, configure, and start the Cloud Logging agent:

    • Add the package repository and update the package list:

      curl -sSO
      sudo bash
      sudo apt-get update
    • Install the agent:

      sudo apt-get install google-fluentd
    • Install the default agent configuration for ingesting structured data to Cloud Logging:

      sudo apt-get install google-fluentd-catch-all-config-structured

      The Cloud Logging agent must be configured to ingest structured data or unstructured data. These are exclusive options. If you configure the agent for unstructured data, then all log entries contain a textPayload field. However, if you configure the agent for structured data, as is done in this quickstart, then certain types of log data contain a jsonPayload field. For more information, see Structured logging operations.

    • Start the agent

      sudo service google-fluentd start

Create an uptime check and an alerting policy

To create an uptime check, do the following:

  1. In the console, select Monitoring:

    Go to Monitoring

  2. In the navigation pane, click Uptime checks.

  3. Click Create Uptime check:

    Create an uptime check dialog.

  4. For the title, enter My Uptime Check and then click Next.

  5. Target:

    1. Select HTTP as the protocol.
    2. Select Instance as the Resource Type.
    3. In the Applies To field, select Single and for the instance name select lamp-1-vm.
    4. Leave all other fields at their default values and click Next.
  6. Response Validation: Leave these fields at their default values and click Next

  7. Alert & Notification:

    • Ensure that the toggle's label is Alerting is enabled.
    • Leave the name and duration fields at their default values.
    • To add a notification channel to the alerting policy, in the text box labeled Notification channels, click Menu. Select the channels to add and click OK. The notifications are grouped alphabetically for each channel type.

      To add an entry to the checkbox list, click Manage notification channels and follow the instructions. When you return to this dialog, click Refresh.

  8. To verify your uptime check configuration, 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.

  9. Click Create. When the create action is successful, the message Check and alert created is displayed and then the Uptime checks dashboard page is displayed.

    In the uptime checks dashboard, your new uptime check is listed. If you click the check name, then you open the detail view for that uptime check. This view displays several charts, shows the uptime percentage and the configuration information, and lists the configured alert policies. To view a policy, click its name.

    You can also view the alert policy by starting at the Alerting page. From the alerting page, the Policies pane lists a subset of policies. To view a list of all policies, click See all policies.

Create a dashboard and chart

  1. In the Google Cloud console, go to the Monitoring page.

    Go to Monitoring

  2. In the navigation pane, select Dashboards and click Create dashboard.
  3. Optional: Update the dashboard title with a descriptive name for your dashboard.
  4. Drag the Line chart widget from the Widget library to the graph area.

    Typically, the chart is configured to display the CPU utilization of a Compute Engine virtual machine:

    example of a newly line chart.
  5. Expand the Resource and Metric menu, enter CPU in the filter bar, and then use the submenus to select a specific resource type and metric:
    1. In the Active resources menu, select VM instance.
    2. In the Active metric categories menu, select Instance.
    3. In the Active metrics menu, select CPU load (1m).
    4. Click Apply.
  6. Optional: To configure how the data is viewed, use the Filter, Group By, Aggregator, and chart type menus. For example, you can group by resource or metric labels. For more information, see Selecting metrics.
  7. Optional: For each additional widget that you want to add to your dashboard, do the following:
    1. In the dashboard toolbar, click Add chart.
    2. Select a widget from the menu.
    3. Configure the widget.
    For example, you could add a line chart and configure it to show the Received bytes metric for your VM instances.

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

Go to Monitoring. In the Uptime checks tile, wait until the icon for the My Uptime Check changes to a green circle with a check mark. The maximum wait time for this stage is 5 minutes:

Display the 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 the logs for the resources displayed in a chart, do the following:

  1. Go to the dashboard that displays the chart of interest.

  2. In the chart, click More , and then click View logs:

    Display selecting View logs from chart more menu.

Alternatively, you can go to Logging, and then specify the filter parameters:

  1. In the console, go to Logging:

    Go to Logging

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

    • Click menu and select Clear filters and return to basic mode.
    • 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.

      Display a view view of logs from console.

Clean up

To avoid incurring charges to your Google Cloud account for the resources used on this page, follow these steps.

Delete the project

If you created a new Cloud project for this quickstart, then delete the Cloud project. Otherwise, skip this section.

  1. In the console, go to the Manage resources page.

    Go to Manage resources

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

Delete quickstart resources

If you used an existing Cloud project for this quickstart, delete your alerting policy, uptime checks, dashboard, and VM instance:

  1. Go to Monitoring:

    Go to Monitoring

  2. Delete the alerting policy:

    1. Select Alerting.
    2. Select My Uptime Check Policy and then click Delete .
  3. Delete the uptime check:

    1. Select Uptime Checks.
    2. Select My Uptime check and then click Delete .
  4. Delete the dashboard:

    1. Select Dashboards.
    2. For the dashboard named Quickstart dashboard, click Delete . Confirm the deletion operation.
  5. Delete the VM instance you created:

    1. In the console, go to the VM instances page.

      Go to VM instances

    2. Select the checkbox for the instance that you want to delete.
    3. To delete the instance, click More actions, click Delete, and then follow the instructions.

What's next