SystemTap: Getting Started with Dynamic EC2 Instance Diagnostics

Just because you can’t see or hear your Amazon EC2 instances the way you can in your local data center, doesn’t mean you shouldn’t be concerned about them. Think of SystemTap as a fabulous window into your VM.

Have you ever wondered what’s going on deep inside your Linux instance? Why some processes run so slowly? Why your disk is thrashing around so wildly? Have you tried to write your own system’s tools and wish that you were able to export or access certain systems’ internal variables or information? Have you tried to write your own Linux kernel module but felt that the learning curve was way too steep?

Let me introduce you to SystemTap.

(The beautiful SystemTap logo was designed by Andy Fitzsimon – who sat next to me when we were both working at Red Hat in Singapore.)
SystemTap is an open source software package to help you analyze or troubleshoot a running Linux instance. It provides a dynamic instrumentation framework that lets you probe a system to gather information and a scripting language translator that interprets scripts written in a C or Perl-like language into C code, that could be compiled into Linux kernel modules. These modules will then be loaded into the system to generate outcome specified by your script.

But enough introduction. Let’s get started already!

Installing SystemTap on your AWS EC2 instance

This article assumes that you know how to create and launch an EC2 instance. If not, I recommend taking Cloud Academy’s Compute Fundamentals for AWS Course as part of the AWS Fundamentals Learning Path.

Connect to the instance using the OpenSSH SSH client.

$ ssh -i "cloudacademy-keypair.pem" ec2-user@[redacted]

Note the Linux kernel version we are using. In this case, it’s version 3.10.0, and the release number is 229.el7.

$ uname -a
Linux ip-[redacted].ap-southeast-1.compute.internal 3.10.0-229.el7.x86_64 #1 SMP Thu Jan 29 18:37:38 EST 2015 x86_64 x86_64 x86_64 GNU/Linux

To prepare the Linux instance for kernel-space probing with SystemTap, we will need to install some RPM packages, including kernel-debuginfo and systemtap. We need to ensure that all the kernel packages we will install on the instance have the same version and of the same architecture.

To install a debuginfo package, we’ll enable the “Red Hat Enterprise Linux Server 7 Debug (Debug RPMs)” yum repository that, by default, is disabled.

$ sudo su -
# yum repolist disabled
Loaded plugins: amazon-id, rhui-lb
repo id                                                                repo name
rhui-REGION-rhel-server-debug-extras/7Server/x86_64                    Red Hat Enterprise Linux Server 7 Extra Debug (Debug RPMs)
rhui-REGION-rhel-server-debug-optional/7Server/x86_64                  Red Hat Enterprise Linux Server 7 Optional Debug (Debug RPMs)
rhui-REGION-rhel-server-debug-rh-common/7Server/x86_64                 Red Hat Enterprise Linux Server 7 RH Common Debug (Debug RPMs)
rhui-REGION-rhel-server-debug-rhscl/7Server/x86_64                     Red Hat Enterprise Linux Server 7 RHSCL Debug (Debug RPMs)
rhui-REGION-rhel-server-debug-supplementary/7Server/x86_64             Red Hat Enterprise Linux Server 7 Supplementary Debug (Debug RPMs)
rhui-REGION-rhel-server-extras/7Server/x86_64                          Red Hat Enterprise Linux Server 7 Extra(RPMs)
rhui-REGION-rhel-server-optional/7Server/x86_64                        Red Hat Enterprise Linux Server 7 Optional (RPMs)
rhui-REGION-rhel-server-releases-debug/7Server/x86_64                  Red Hat Enterprise Linux Server 7 Debug (Debug RPMs)
rhui-REGION-rhel-server-releases-source/7Server/x86_64                 Red Hat Enterprise Linux Server 7 (SRPMs)
rhui-REGION-rhel-server-rhscl/7Server/x86_64                           Red Hat Enterprise Linux Server 7 RHSCL (RPMs)
rhui-REGION-rhel-server-source-extras/7Server/x86_64                   Red Hat Enterprise Linux Server 7 Extra (SRPMs)
rhui-REGION-rhel-server-source-optional/7Server/x86_64                 Red Hat Enterprise Linux Server 7 Optional (SRPMs)
rhui-REGION-rhel-server-source-rh-common/7Server/x86_64                Red Hat Enterprise Linux Server 7 RH Common (SRPMs)
rhui-REGION-rhel-server-source-rhscl/7Server/x86_64                    Red Hat Enterprise Linux Server 7 RHSCL (SRPMs)
rhui-REGION-rhel-server-source-supplementary/7Server/x86_64            Red Hat Enterprise Linux Server 7 Supplementary (SRPMs)
rhui-REGION-rhel-server-supplementary/7Server/x86_64                   Red Hat Enterprise Linux Server 7 Supplementary (RPMs)
repolist: 0

Now we’ll use yum-config-manager to enable the repo.

# yum-config-manager --enable "Red Hat Enterprise Linux Server 7 Debug (Debug RPMs)"

Now you’ll be able to search for the debuginfo packages.

# yum search kernel | grep ^kernel-debuginfo
kernel-debuginfo.x86_64 : Debug information for package kernel
kernel-debuginfo-common-x86_64.x86_64 : Kernel source files used by

To ensure that we have the same Linux kernel version, we need to install the kernel and kernel-devel packages along with the kernel-debuginfo packages. If yum returns a different kernel version from the kernel-debuginfo packages, you may need to specify the version manually.

# yum -y install kernel-debuginfo kernel kernel-devel

Depending on your Internet connectivity, it may take some time to download these packages. To give you an idea how large some of these packages are, the kernel-debuginfo-common package is 41MB and the kernel-debuginfo package is 259MB. Fortunately, unless the kernel version and release number change, you only need to install these packages once.

You will need to reboot your machine to apply the new kernel.

# init 6

Once we’re running again, let’s reconnect to the instance.

$ ssh -i "cloudacademy-keypair.pem" ec2-user@[redacted]

Verify that we are using the right kernel version. Notice that the release number is now 229.11.1.el7. Each time Red Hat spins a new kernel, it will update the release number of the package.

$ uname -a
Linux ip-[redacted].ap-southeast-1.compute.internal 3.10.0-229.11.1.el7.x86_64 #1 SMP Wed Jul 22 12:06:11 EDT 2015 x86_64 x86_64 x86_64 GNU/Linux

We can use the rpm command to make sure that the version we booted is the same as the debuginfo packages we have installed. This is important.

$ rpm -qa | grep ^kernel
kernel-tools-3.10.0-229.el7.x86_64
kernel-3.10.0-229.el7.x86_64
kernel-debuginfo-common-x86_64-3.10.0-229.11.1.el7.x86_64
kernel-3.10.0-229.11.1.el7.x86_64
kernel-devel-3.10.0-229.11.1.el7.x86_64
kernel-tools-libs-3.10.0-229.el7.x86_64
kernel-debuginfo-3.10.0-229.11.1.el7.x86_64

Next, use yum to list all the systemtap-related packages.

# yum list systemtap*
Loaded plugins: amazon-id, rhui-lb
Available Packages
systemtap.x86_64                                              2.6-10.el7_1                                    rhui-REGION-rhel-server-releases
systemtap-client.x86_64                                       2.6-10.el7_1                                    rhui-REGION-rhel-server-releases
systemtap-debuginfo.i686                                      2.6-10.el7_1                                    rhui-REGION-rhel-server-releases-debug
systemtap-debuginfo.x86_64                                    2.6-10.el7_1                                    rhui-REGION-rhel-server-releases-debug
systemtap-devel.x86_64                                        2.6-10.el7_1                                    rhui-REGION-rhel-server-releases
systemtap-initscript.x86_64                                   2.6-10.el7_1                                    rhui-REGION-rhel-server-releases
systemtap-runtime.x86_64                                      2.6-10.el7_1                                    rhui-REGION-rhel-server-releases
systemtap-sdt-devel.i686                                      2.6-10.el7_1                                    rhui-REGION-rhel-server-releases
systemtap-sdt-devel.x86_64                                    2.6-10.el7_1                                    rhui-REGION-rhel-server-releases
systemtap-server.x86_64                                       2.6-10.el7_1                                    rhui-REGION-rhel-server-releases

Then install the main systemtap package, and yum will take care of its dependencies.

# yum -y install systemtap

Once all the systemtap-related packages are installed, exit from the root shell to return to the normal user shell.

# logout

Running our first SystemTap script

We have installed all the necessary RPM packages and we are almost ready to run our very first “Hello World” script!

SystemTap uses a simple permission model to grant users’ permissions to use the tool to prevent people from misusing or abusing the tool. Unless you’re a member of the stapusr and stapdev groups, you will not be allowed to build and run SystemTap scripts. Since we are going to write and run our own script, and we hopefully know exactly what we are doing, we will add ourselves to the stapusr and stapdev groups.

$ sudo usermod -G stapusr,stapdev ec2-user
$ grep ^stap /etc/group
stapusr:x:156:ec2-user
stapsys:x:157:
stapdev:x:158:ec2-user

You will need to logout and re-login to the instance to reflect that you are now members of these two groups.

$ logout
$ ssh -i "cloudacademy-keypair.pem" ec2-user@[redacted]

Type id to see if you are in stapusr and stapdev groups.

$ id
uid=1000(ec2-user) gid=1000(ec2-user) groups=1000(ec2-user),156(stapusr),158(stapdev) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023

If you are, let’s party on! stap is the SystemTap tool you’ll use to run the script.
There are two ways to write a script. You can either write it in a .stp extension file, or simply specify the script on the command-line with the -e option.

We will opt for the latter since we will use a one-liner script to test if the installation works as expected.

$ stap -e 'probe begin { log("hello world") exit () }'
hello world

Voilà! We have successfully run our first SystemTap script!

In our next article, we will explore some interesting scripts that come with SystemTap.

Avatar

Written by

Eugene Teo

Eugene Teo is a director of security at a US-based technology company. He is interested in applying machine learning techniques to solve problems in the security domain.


Related Posts

Alisha Reyes
Alisha Reyes
— December 10, 2019

New Lab Challenges: Push Your Skills to the Next Level

Build hands-on experience using real accounts on AWS, Azure, Google Cloud Platform, and more Meaningful cloud skills require more than book knowledge. Hands-on experience is required to translate knowledge into real-world results. We see this time and time again in studies about how pe...

Read more
  • AWS
  • Azure
  • Google Cloud
  • hands-on
  • labs
Alisha Reyes
Alisha Reyes
— December 5, 2019

New on Cloud Academy: AWS Solution Architect Lab Challenge, Azure Hands-on Labs, Foundation Certificate in Cyber Security, and Much More

Now that Thanksgiving is over and the craziness of Black Friday has died down, it's now time for the busiest season of the year. Whether you're a last-minute shopper or you already have your shopping done, the holidays bring so much more excitement than any other time of year. Since our...

Read more
  • AWS
  • AWS solution architect
  • AZ-203
  • Azure
  • cyber security
  • FCCS
  • Foundation Certificate in Cyber Security
  • Google Cloud Platform
  • Kubernetes
Avatar
Cloud Academy Team
— December 4, 2019

Understanding Enterprise Cloud Migration

What is enterprise cloud migration? Cloud migration is about moving your data, applications, and even infrastructure from your on-premises computers or infrastructure to a virtual pool of on-demand, shared resources that offer compute, storage, and network services at scale. Why d...

Read more
  • AWS
  • Azure
  • Data Migration
Wendy Dessler
Wendy Dessler
— November 27, 2019

6 Reasons Why You Should Get an AWS Certification This Year

In the past decade, the rise of cloud computing has been undeniable. Businesses of all sizes are moving their infrastructure and applications to the cloud. This is partly because the cloud allows businesses and their employees to access important information from just about anywhere. ...

Read more
  • AWS
  • Certifications
  • certified
Avatar
Andrea Colangelo
— November 26, 2019

AWS Regions and Availability Zones: The Simplest Explanation You Will Ever Find Around

The basics of AWS Regions and Availability Zones We’re going to treat this article as a sort of AWS 101 — it’ll be a quick primer on AWS Regions and Availability Zones that will be useful for understanding the basics of how AWS infrastructure is organized. We’ll define each section,...

Read more
  • AWS
Avatar
Dzenan Dzevlan
— November 20, 2019

Application Load Balancer vs. Classic Load Balancer

What is an Elastic Load Balancer? This post covers basics of what an Elastic Load Balancer is, and two of its examples: Application Load Balancers and Classic Load Balancers. For additional information — including a comparison that explains Network Load Balancers — check out our post o...

Read more
  • ALB
  • Application Load Balancer
  • AWS
  • Elastic Load Balancer
  • ELB
Albert Qian
Albert Qian
— November 13, 2019

Advantages and Disadvantages of Microservices Architecture

What are microservices? Let's start our discussion by setting a foundation of what microservices are. Microservices are a way of breaking large software projects into loosely coupled modules, which communicate with each other through simple Application Programming Interfaces (APIs). ...

Read more
  • AWS
  • Docker
  • Kubernetes
  • Microservices
Nisar Ahmad
Nisar Ahmad
— November 12, 2019

Kubernetes Services: AWS vs. Azure vs. Google Cloud

Kubernetes is a popular open-source container orchestration platform that allows us to deploy and manage multi-container applications at scale. Businesses are rapidly adopting this revolutionary technology to modernize their applications. Cloud service providers — such as Amazon Web Ser...

Read more
  • AWS
  • Azure
  • Google Cloud
  • Kubernetes
Avatar
Stuart Scott
— October 31, 2019

AWS Internet of Things (IoT): The 3 Services You Need to Know

The Internet of Things (IoT) embeds technology into any physical thing to enable never-before-seen levels of connectivity. IoT is revolutionizing industries and creating many new market opportunities. Cloud services play an important role in enabling deployment of IoT solutions that min...

Read more
  • AWS
  • AWS IoT Events
  • AWS IoT SiteWise
  • AWS IoT Things Graph
  • IoT
Avatar
Cloud Academy Team
— October 23, 2019

Which Certifications Should I Get?

As we mentioned in an earlier post, the old AWS slogan, “Cloud is the new normal” is indeed a reality today. Really, cloud has been the new normal for a while now and getting credentials has become an increasingly effective way to quickly showcase your abilities to recruiters and compan...

Read more
  • AWS
  • Azure
  • Certifications
  • Cloud Computing
  • Google Cloud Platform
Valery Calderón Briz
Valery Calderón Briz
— October 22, 2019

How to Go Serverless Like a Pro

So, no servers? Yeah, I checked and there are definitely no servers. Well...the cloud service providers do need servers to host and run the code, but we don’t have to worry about it. Which operating system to use, how and when to run the instances, the scalability, and all the arch...

Read more
  • AWS
  • Lambda
  • Serverless
Avatar
Stuart Scott
— October 16, 2019

AWS Security: Bastion Hosts, NAT instances and VPC Peering

Effective security requires close control over your data and resources. Bastion hosts, NAT instances, and VPC peering can help you secure your AWS infrastructure. Welcome to part four of my AWS Security overview. In part three, we looked at network security at the subnet level. This ti...

Read more
  • AWS