Stackdriver Monitoring provides visibility into the performance, uptime, and overall health of cloud-powered applications. Stackdriver collects metrics, events, and metadata from Google Cloud Platform, Amazon Web Services, hosted uptime probes, application instrumentation, and a variety of common application components including Cassandra, Nginx, Apache Web Server, Elasticsearch, and many others. Stackdriver ingests that data and generates insights via dashboards, charts, and alerts. Stackdriver alerting helps you collaborate by integrating with Slack, PagerDuty, HipChat, Campfire, and more.

This hands-on lab shows you how to monitor a Google Compute Engine virtual machine (VM) instance with Stackdriver. You'll also install monitoring and logging agents for your VM which collects more information from your instance, which could include metrics and logs from 3rd party apps.

Self-paced environment setup

If you don't already have a Google Account (Gmail or Google Apps), you must create one. Sign-in to Google Cloud Platform console (console.cloud.google.com) and create a new project:

Remember the project ID, a unique name across all Google Cloud projects (the name above has already been taken and will not work for you, sorry!). It will be referred to later in this codelab as PROJECT_ID.

Next, you'll need to enable billing in the Developers Console in order to use Google Cloud resources.

Running through this codelab shouldn't cost you more than a few dollars, but it could be more if you decide to use more resources or if you leave them running (see "cleanup" section at the end of this document). Google Kubernetes Engine pricing is documented here.

New users of Google Cloud Platform are eligible for a $300 free trial.

In the GCP Console dashboard, click on the hamburger to go to Products & services > Compute Engine > VM instances, then click Create. You may need to wait a minute or two for Compute Engine to detect your environment before you can click Create.

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.

Click Create.

Wait a couple of minutes for your instance to be launched.

If you were to browse to the instance's external IP address, you'd get an error. Now you'll learn how to fix that.

In the Console, click the SSH to open a terminal to your instance.

Run the following commands in the SSH window to set up Apache2 HTTP Server:

sudo apt-get update
sudo apt-get install apache2 php7.0

When asked if you want to continue, type Y.

sudo service apache2 restart

If you visit http://[External IP], you see the Apache2 default page:

To use Stackdriver, your project must be in a Stackdriver account. The following steps create a new account that has a free 30-day trial of Stackdriver Premium service.

In the Google Cloud Platform Console, click on Products and Services to return to the left menu. In the Stackdriver section, click on Monitoring.

Click "Log in with Google" and use the Codelabs credentials to log in.

Your Codelabs project should display as your project. Select Create account.

On the following pages:

You see the message "Gathering information..." at the top of the page. Click Launch monitoring when it finishes:

A pop-up message will inform you that your account is in a free trial. Click Continue with the trial.

You see your Stackdriver account dashboard. Close the "Welcome to Stackdriver Monitoring!" banner.

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

Now you'll create an Uptime Check and an Alerting Policy.

In the Uptime Checks pane, click Create an Uptime Check button on the dashboard. You can also go to Uptime Checks in the left-hand menu, then select Uptime Checks Overview and then click Create an Uptime Check on the new page.

Edit the New Uptime Check panel by editing the following fields:

Title: Lamp Uptime Check

Check type: HTTP

Resource Type: Instance

Applies To: Single, lamp-1-vm

Check every: 1 min

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.

When you see a green check mark everything is running correctly. Click Save.

You see the following panel--click Create Alerting Policy:

Update the Alerting Policy this way:

The Conditions section is already set up. You don't have to change it.

In the Notifications section, click Add Notification and fill in your personal email address. You will need to check it for the notification.

In the Documentation section, click Add Documentation and enter: Stackdriver LAMP qwikstart example.

In the Name this policy section, you can accept the default Uptime Check Policy.

Click Save Policy.

The Dashboard will now have your Uptime Check added to it.

Display the metrics collected by Stackdriver Monitoring in your own charts and dashboards. Now you'll create the charts for the lab metrics and a custom dashboard.

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

Click Add Chart in the upper-right menu of the new dashboard.

Click into the Find resource type and metric field and start typing "CPU", then select CPU Load (1m).

You will automatically have GCE VM instance selected as the Resource type. The chart will name itself after the metric you're using, but you can rename it whatever you want.

Click Save.

Now create a second chart.

Select Add Chart in the upper-right menu of the new dashboard.

In the Find resource type and metric field start typing "Network", then choose "Received packets". Leave the other fields with their default values. You see the chart data in the Preview section.

Click Save.

You still need to name your new Dashboard! Change Untitled Dashboard to Stackdriver LAMP qwikstart dashboard.

This procedure can take up to fifteen minutes.

Before we can test the Check and Alert, we need to ensure that at least one uptime check was successful. Click on Uptime Checks > LAMP Uptime Check to observe progress as Stackdriver issues uptime checks for your service from different locations. Wait until you see "All locations passed" in the Location results pane before proceeding. Alternatively, you can go to the Monitoring Overview page and wait for a checkmark next to appear next to Lamp Uptime Check before proceeding.

To test the uptime check and get the alert to fire, go back to the Console tab and the VM Instances page. Click on your instance, and click STOP from the top menu. When the Stop VM instance window pops up, click STOP.

You might wait up to five minutes for the next uptime check to fail. The alert and notification will happen when the next failure occurs.

You'll see a notice in the Incidents pane on the Monitoring Overview page on Stackdriver.

The alert email notification will look something like this:

You don't need to wait for the email to arrive - you can read it later.

Now correct the "problem" by returning to the VM Instances page in the Console, select your instance, and click START from the top menu. When the Start VM instance window pops up, click START.

Stackdriver Monitoring and Stackdriver Logging are closely integrated. Let's check out the logs for your lab.

In the Stackdriver left-hand menu, click Logging to see the Logs Viewer. Now change the focus to see the logs you want:

You see the logs from your VM instance:

You have successfully monitored a virtual machine, setup an uptime check on it, setup and triggered alerts, and checked the logs it generated.

Next Steps / Learn More

Learn more about Stackdriver in our Codelab on Application Performance Management (APM) with Stackdriver.

Here are links to the full Stackdriver documentation, with details for each product:

Google Cloud Training & Certification

...helps you make the most of Google Cloud technologies. Our classes include technical skills and best practices to help you get up to speed quickly and continue your learning journey. We offer fundamental to advanced level training, with on-demand, live, and virtual options to suit your busy schedule. Certifications help you validate and prove your skill and expertise in Google Cloud technologies.

Manual Last Updated February 22, 2018

Lab Last Tested February 22, 2018

©2018 Google LLC All rights reserved. Google and the Google logo are trademarks of Google LLC. All other company and product names may be trademarks of the respective companies with which they are associated.