Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Real-Time Infrastructure Monitoring With Amazon Echo

DZone's Guide to

Real-Time Infrastructure Monitoring With Amazon Echo

In this post, I will show you how I was able to monitor my infrastructure on AWS using a simple Alexa Skill.

· IoT Zone ·
Free Resource

Learn how integrating security into DevOps to deliver "DevSecOps" requires changing mindsets, processes and technology.

Years ago, managing your infrastructure through voice was like something from a science-fiction movie. But thanks to virtual assistants like Alexa, it has become a reality. In this post, I will show you how I was able to monitor my infrastructure on AWS using a simple Alexa Skill.

At a high level, the architecture of the skill is as follows.

I installed a data collector agent (Telegraf) in each EC2 instance to collect metrics about system usage (disk, memory, CPU) and send them to a time series database (InfluxDB).

Once my database is populated with metrics, Amazon Echo will transform my voice commands to intents that will trigger a Lambda function, which will use the InfluxDB REST API to query the database.

Enough with the talking — let's build this skill from scratch! Clone the following GitHub repository:

git clone https://github.com/mlabouardy/alexa-monitor

Create a simple fleet of EC2 instances using Terraform. Install the AWS provider:

terraform init

Set your own AWS credentials in variables.tfvars. Create an execution plan:

terraform plan --var-file=variables.tfvars

Provision the infrastructure:

terraform apply --var-file=variables.tfvars

You should see the IP address for each machine:

Log into AWS Management Console. You should see that your nodes have been created successfully:

To install Telegraf on each machine, I used Ansible. Update the ansible/inventory with your nodes IP addresses as follows:

[nodes]
35.178.10.226 hostname=Rabat
35.177.164.157 hostname=Paris
52.56.126.138 hostname=London

[nodes:vars]
remote_user=ec2-user
influxdb_ip=35.177.123.180

Execute the playbook:

ansible-playbook -i inventory playbook.yml --private-key=key.pem

If you connect via SSH to one of the servers, you should see the Telegraf agent is running as Docker container:

In few seconds, the InfluxDB database will be populated with some metrics:

Sign in to the Amazon Developer Portal and create a new Alexa Skill:

Create an invocation name, aws. This is the word that will trigger the Skill.

In the Intent Schema box, paste the following JSON code:

{
  "intents" : [
    {
      "intent" : "GetSystemUsage",
      "slots" : [
        {
          "name" : "Metric",
          "type" : "TYPE_OF_METRICS"
        },
        {
          "name" : "City",
          "type" : "LIST_OF_CITIES"
        }
      ]
    }
  ]
}

Create a new slot type to store the type of metric and machine hostname:

TYPE_OF_METRICS:
 
CPU
DISK
MEMORY
 
LIST_OF_CITIES:
 
Paris
Rabat
London

Under Utterances, enter all the phrases that you think you might say to interact with the skill:

Click on Next and you will move onto a page that allows us to use an ARN (Amazon Resource Name) to link to AWS Lambda.

Before that, let's create our lambda function. Go to AWS Management Console and create a new lambda function from scratch:

Note: Select US East (N. Virginia), which is a supported region for Alexa Skill Kit.

Make sure the trigger is set to Alexa Skills Kit, then select Next.

The code provided uses the InfluxDB client to fetch metrics from the database.

function MetricsDB(){
  this.influx = new Influx.InfluxDB({
    host: process.env.INFLUXDB_HOST,
    database: process.env.INFLUXDB_DATABASE
  })
}

MetricsDB.prototype.getCPU = function(machine, callback){
  this.influx.query(`
      SELECT last(usage_system) AS system, last(usage_user) AS "user"
      FROM cpu
      WHERE time > now() - 5m AND host='${machine}'
    `).then(result => {
      var system_usage = result[0].system.toFixed(2)
      var user_usage = result[0].user.toFixed(2)
      callback(`System usage is ${system_usage} percent & user usage is ${user_usage} percent`)
  }).catch(err => {
      callback(`Cannot get cpu usage values`)
  })
}

Specify the ZIP file name as your deployment package at the time you create the Lambda function. Don't forget to set the InfluxDB hostname and database name as an environment variables:

Then, go to the Configuration step of your Alexa Skill in the Amazon Developer Console and enter the Lambda Function ARN:

Click Next. Under the Service Simulator section, you'll be able to enter a sample utterance to trigger your skill:

Memory usage: Disk usage: CPU usage:

Test your skill on your Amazon Echo, Echo Dot, or any Alexa device by saying, "Alexa, ask AWS for disk usage of the machine in Paris."

Learn how enterprises are using tools to automate security in their DevOps toolchain with these DevSecOps Reference Architectures.

Topics:
iot ,real-time data ,echo ,infrastructure ,tutorial

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}