Swiftorial Logo
Home
Swift Lessons
Tutorials
Learn More
Career
Resources

Using Google Stackdriver with Shell Scripts

Introduction to Google Stackdriver

Google Stackdriver is a powerful monitoring, logging, and diagnostics service for applications running on Google Cloud Platform (GCP) and Amazon Web Services (AWS). Integrating Stackdriver with shell scripts allows you to automate the collection and analysis of metrics, logs, and other performance data.

Setting Up Google Cloud SDK

To use Google Stackdriver with shell scripts, you need to set up the Google Cloud SDK. Follow these steps to install and configure the SDK:


#!/bin/bash

# Update the package list and install Google Cloud SDK
curl https://sdk.cloud.google.com | bash

# Restart the shell
exec -l $SHELL

# Initialize the SDK
gcloud init
                

During the initialization process, you will be prompted to authenticate and configure your default project settings.

Monitoring Virtual Machines

You can use shell scripts to monitor your virtual machines (VMs) using Stackdriver. Here is an example script to get CPU utilization of a VM instance:


#!/bin/bash

# Define variables
PROJECT_ID="your_project_id"
INSTANCE_ID="your_instance_id"
ZONE="your_instance_zone"

# Get CPU utilization from Stackdriver
gcloud monitoring time-series list \
    --project=$PROJECT_ID \
    --filter='metric.type="compute.googleapis.com/instance/cpu/utilization"' \
    --interval='endTime="2021-08-01T00:00:00Z"' \
    --instance=$INSTANCE_ID \
    --zone=$ZONE
                

Replace your_project_id, your_instance_id, and your_instance_zone with the appropriate values. This script fetches the CPU utilization of the specified VM instance.

Creating Alerts

Google Stackdriver Alerts allow you to monitor metrics and automatically perform actions when a threshold is breached. Here is a script to create a CPU utilization alert for a VM instance:


#!/bin/bash

# Define variables
PROJECT_ID="your_project_id"
POLICY_NAME="HighCPUUtilization"
CONDITION_DISPLAY_NAME="VM CPU Usage"
INSTANCE_ID="your_instance_id"
ZONE="your_instance_zone"
THRESHOLD=0.8

# Create a Stackdriver alert policy
gcloud alpha monitoring policies create \
    --project=$PROJECT_ID \
    --display-name="$POLICY_NAME" \
    --conditions="displayName=$CONDITION_DISPLAY_NAME,conditionThreshold=cpu.utilization,thresholdValue=$THRESHOLD,filter='resource.label.instance_id=$INSTANCE_ID'" \
    --notification-channels="email" \
    --notification-channels-email-recipients="your_email@example.com"
                

Replace the placeholders with your specific values. This script creates an alert that triggers when the CPU utilization of the specified VM instance exceeds 80%.

Logging to Stackdriver

Google Stackdriver Logging allows you to collect and analyze log data from various sources. Here is a script to send custom logs to Stackdriver:


#!/bin/bash

# Define variables
PROJECT_ID="your_project_id"
LOG_NAME="custom_log"
LOG_MESSAGE="This is a test log message"

# Send a custom log message to Stackdriver
gcloud logging write $LOG_NAME "$LOG_MESSAGE" --project=$PROJECT_ID --severity="INFO"
                

Replace the placeholders with your specific values. This script sends a custom log message to Stackdriver Logging.

Automating Stackdriver Setup with Shell Scripts

Here is a complete shell script to automate the setup of Stackdriver monitoring and logging:


#!/bin/bash

# Install Google Cloud SDK
curl https://sdk.cloud.google.com | bash
exec -l $SHELL
gcloud init

# Define variables
PROJECT_ID="your_project_id"
INSTANCE_ID="your_instance_id"
ZONE="your_instance_zone"
POLICY_NAME="HighCPUUtilization"
CONDITION_DISPLAY_NAME="VM CPU Usage"
THRESHOLD=0.8
LOG_NAME="custom_log"
LOG_MESSAGE="This is a test log message"

# Get CPU utilization from Stackdriver
gcloud monitoring time-series list \
    --project=$PROJECT_ID \
    --filter='metric.type="compute.googleapis.com/instance/cpu/utilization"' \
    --interval='endTime="2021-08-01T00:00:00Z"' \
    --instance=$INSTANCE_ID \
    --zone=$ZONE

# Create Stackdriver alert policy
gcloud alpha monitoring policies create \
    --project=$PROJECT_ID \
    --display-name="$POLICY_NAME" \
    --conditions="displayName=$CONDITION_DISPLAY_NAME,conditionThreshold=cpu.utilization,thresholdValue=$THRESHOLD,filter='resource.label.instance_id=$INSTANCE_ID'" \
    --notification-channels="email" \
    --notification-channels-email-recipients="your_email@example.com"

# Send a custom log message to Stackdriver
gcloud logging write $LOG_NAME "$LOG_MESSAGE" --project=$PROJECT_ID --severity="INFO"
                

Save this script as setup_stackdriver.sh and make it executable:


chmod +x setup_stackdriver.sh
./setup_stackdriver.sh
                

Conclusion

Integrating Google Stackdriver with shell scripts provides a powerful way to automate the monitoring and logging of your cloud resources. By following this tutorial, you can set up the Google Cloud SDK, monitor your VM instances, create alerts, and send custom logs to Stackdriver. This automation helps ensure your applications remain performant and reliable.