Mastering Bash
eBook - ePub

Mastering Bash

  1. 502 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Mastering Bash

About this book

Your one stop guide to making the most out of Bash programmingAbout This Book• From roots to leaves, learn how to program in Bash and automate daily tasks, pouring some spice in your scripts• Daemonize a script and make a real service of it, ensuring it's available at any time to process user-fed data or commands• This book provides functional examples that show you practical applications of commandsWho This Book Is ForIf you're a power user or system administrator involved in writing Bash scripts to automate tasks, then this book is for you. This book is also ideal for advanced users who are engaged in complex daily tasks.What You Will Learn• Understand Bash right from the basics and progress to an advanced level• Customise your environment and automate system routine tasks• Write structured scripts and create a command-line interface for your scripts• Understand arrays, menus, and functions• Securely execute remote commands using ssh• Write Nagios plugins to automate your infrastructure checks• Interact with web services, and a Slack notification script• Find out how to execute subshells and take advantage of parallelism• Explore inter-process communication and write your own daemonIn DetailSystem administration is an everyday effort that involves a lot of tedious tasks, and devious pits. Knowing your environment is the key to unleashing the most powerful solution that will make your life easy as an administrator, and show you the path to new heights. Bash is your Swiss army knife to set up your working or home environment as you want, when you want.This book will enable you to customize your system step by step, making your own real, virtual, home out of it. The journey will take you swiftly through the basis of the shell programming in Bash to more interesting and challenging tasks. You will be introduced to one of the most famous open source monitoring systems—Nagios, and write complex programs with it in any languages. You'll see how to perform checks on your sites and applications.Moving on, you'll discover how to write your own daemons so you can create your services and take advantage of inter-process communication to let your scripts talk to each other. So, despite these being everyday tasks, you'll have a lot of fun on the way. By the end of the book, you will have gained advanced knowledge of Bash that will help you automate routine tasks and manage your systems.Style and approachThis book presents step-by-step instructions and expert advice on working with Bash and writing scripts. Starting from the basics, this book serves as a reference manual where you can find handy solutions and advice to make your scripts flexible and powerful.

Frequently asked questions

Yes, you can cancel anytime from the Subscription tab in your account settings on the Perlego website. Your subscription will stay active until the end of your current billing period. Learn how to cancel your subscription.
No, books cannot be downloaded as external files, such as PDFs, for use outside of Perlego. However, you can download books within the Perlego app for offline reading on mobile or tablet. Learn more here.
Perlego offers two plans: Essential and Complete
  • Essential is ideal for learners and professionals who enjoy exploring a wide range of subjects. Access the Essential Library with 800,000+ trusted titles and best-sellers across business, personal growth, and the humanities. Includes unlimited reading time and Standard Read Aloud voice.
  • Complete: Perfect for advanced learners and researchers needing full, unrestricted access. Unlock 1.4M+ books across hundreds of subjects, including academic and specialized titles. The Complete Plan also includes advanced features like Premium Read Aloud and Research Assistant.
Both plans are available with monthly, semester, or annual billing cycles.
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Yes! You can use the Perlego app on both iOS or Android devices to read anytime, anywhere — even offline. Perfect for commutes or when you’re on the go.
Please note we cannot support devices running on iOS 13 and Android 7 or earlier. Learn more about using the app.
Yes, you can access Mastering Bash by Giorgio Zarrelli in PDF and/or ePUB format, as well as other popular books in Computer Science & Operating Systems. We have over one million books available in our catalogue for you to explore.

Information

Plug into the Real World

We are moving into the real world now, creating something that can turn out handy for your daily routine; during this process, we will have a look at the common pitfalls in coding and how to make our script reliable. Be it a short or long script, we must always ask ourselves the same questions:
  • What do we really want to accomplish?
  • How much time do we have?
  • Do we have all the resources needed?
  • Do we have the knowledge required for the task?
We will start coding with a Nagios plugin, which will give us a broad understanding of how this monitoring system is and how to make a script dynamically interact with other programs.

What is Nagios?

Nagios is one of the most widely adopted open source IT infrastructure monitoring tools, whose main interesting feature is the fact that it does not know how to monitor anything. Well, it sounds like a joke, but actually Nagios can be defined as an evaluating core, which takes some information as input and reacts accordingly. How is this information gathered? It is not the main concern of this tool and this leads us to an interesting point: Nagios leaves the task of getting the monitored data to an external plugin, which knows the following details:
  • How to connect to the monitored services
  • How to collect the data from the monitored services
  • How to evaluate the data
Inform Nagios if the values gathered are beyond or in the boundaries to raise an alarm.
So, a plugin does a lot of things and one would ask oneself what does Nagios do then? Imagine it as an exchange pod where information is flowing in and out and decisions are taken based on the configurations set; the core triggers the plugin to monitor a service; the plugin itself returns some information and Nagios takes a decision about:
  • Whether to raise an alarm
  • Send a notification
  • Whom to notify
  • For how long
  • What, if any action is taken in order to get back to normality
The core Nagios program does everything except actually knock at the door of a service, ask for information, and decide whether this information shows some issues or not.

Active and passive checks

To understand how to code a plugin, we have first to grasp how, on a broad scale, a Nagios check works. There are two different kinds of checks.

Active checks

Based on a time range, or manually triggered, an active check sees a plugin actively connecting to a service and collecting information. A typical example could be for a plugin to check the disk space: once invoked, it interfaces with (usually) the operating system, executes a df command, works on the output, extracts the value related to the disk space, evaluates it against some thresholds, and reports back a status, such as OK, WARNING, CRITICAL, or UNKNOWN.

Passive checks

In this case, Nagios does not trigger anything but waits to be contacted by some means by the service, which must be monitored. It seems quite confusing, but let's make a real-life example. How would you monitor if a disk backup has been completed successfully? One quick answer would be: knowing when the backup task starts and how long it lasts, we can define a time and invoke a script to check the task at that given hour.
Nice, but when we plan something, we must have a full understanding of how real life goes, and a backup is not our little pet in the living room, it's rather a beast, which does what it wants. A backup can last a variable amount of time depending on an unpredictable factor.
For instance, your typical backup task would copy 1 TB of data in 2 hours, starting at 03:00, out of a 6 TB disk. So, the next backup task would start at 03:00+02:00=05:00 AM, give or take some minutes. And you set up an active check for it at 05:30, and it works well for a couple of months. Then, one early morning, you receive a notification on your smartphone that the backup is in CRITICAL. You wake up, connect to the backup console and see that at 06:00 in the morning, you are asleep and the backup task has not even been started by the console. Then, you have to wait until 08:00 AM until some of your colleagues show up at the office to find out that the day before the disk, your backup has been filled with 2 extra TB of data due to an unscheduled data transfer. So, the backup task preceding the one you are monitoring lasted not for a couple of hours but 6 hours, and the task you are monitoring then started at 09:30 AM.
Long story short, your active check has been fired up too early; that is why it failed. Maybe your are tempted to move your schedule some hours ahead, but simply do not do it, as these time slots are not sliding frames. If you move your check ahead, you should then move all the checks for the subsequent tasks ahead. You do it in one week, the project manager will ask someone to delete the 2 TB excess (useless for the project now), and your schedules will be 2 hours ahead, making your monitoring useless. So, as we insisted before, planning and analyzing the context is the key factor in making a good script and, in this case, a good plugin. We have a service that does not run 24/7 like a web service or a...

Table of contents

  1. Title Page
  2. Copyright
  3. Credits
  4. About the Author
  5. About the Reviewer
  6. www.PacktPub.com
  7. Customer Feedback
  8. Preface
  9. Let's Start Programming
  10. Operators
  11. Testing
  12. Quoting and Escaping
  13. Menus, Arrays, and Functions
  14. Iterations
  15. Plug into the Real World
  16. We Want to Chat
  17. Subshells, Signals, and Job Controls
  18. Lets Make a Process Chat
  19. Living as a Daemon
  20. Remote Connections over SSH
  21. Its Time for a Timer
  22. Time for Safety