How to setup RabbitMQ Queues monitoring using Nagios

Installing the Nagios RabbitMQ plugin.

 

The RabbitMQ plugin here we are going to use is validated listed under monitoring section by https://www.rabbitmq.com/monitoring.html

https://github.com/nagios-plugins-rabbitmq/nagios-plugins-rabbitmq

The installation is done as follows:

 

The Perl library can be downloaded and installed as follows:

cd /tmp/

wget http://search.cpan.org/CPAN/authors/id/T/TO/TONVOON/Nagios-Plugin-0.36.tar.gz

tar xvf Nagios-Plugin-0.36.tar.gz

cd Nagios-Plugin-0.36

perl Makefile.PL

make

make test

make install

 

In case you do not have the make, Please install it using following command to complete the make install of Nagios Plugin:

ap install make

Ofcourse the primary requirement is to have the nrpe setup installed

This can be done as follows in a ubuntu system:

# apt install nagios-nrpe-server nagios-plugins

 

Now we can go ahead and start the setup of rabbitMQ plugin.

 

cd /usr/local/nagios/libexec/

clone the repository.

git clone https://github.com/nagios-plugins-rabbitmq/nagios-plugins-rabbitmq.git

The repository already contains the executables in script directory which we will use for monitoring after completion.

We now have to provide the required perl libraries to support the rabbitmq-nagios plugins and execute them.

Lets us now proceed and organize the directory structure and move directory nagios-plugins-rabbitmq to /usr/local/nagios/libexec/

# mv nagios-plugins-rabbitmq /usr/local/nagios/libexec/

Change the ownership to nagios user, This step is important and must to done as a prerequisite,

ensure that you have the desired nagios user already present by running the below command

# id nagios

Change the ownership to nagios user and group.

# sudo chown nagios:nagios nagios-plugins-rabbitmq -R

The files and directories now should be owned by the nagios user and group

Now we can go ahead and install the required modules from cpan.

Run the cpan command to fetch and install perl dependencies

 

On some systems you might face some problems while trying to install ” install Monitoring::Plugin ”

cpan[1]> install Monitoring::Plugin

You may try this

cpan[1]> install Nagios::Monitoring::Plugin

After installing it, Next install LWP

cpan[2]> install LWP

After installing it, Next install JSON

cpan[3]> install JSON

Type exit to exit out of the cpan prompt.

cpan[4]> exit

Terminal does not support GetHistory.
Lockfile removed.

 

Alternatively you may also run the cpanm and avoid entering into the cpan subshell and issue the perl install commands

# apt install cpanminus

You may also use this to install Nagios::Plugin

After the installation is completed without any errors you would have successfully installed the RabbitMQ nagios plugins.

Now check the installation by going to scripts directory

# /usr/local/nagios/libexec/nagios-plugins-rabbitmq/scripts

Lets look at what plugins are provided for rabbitmq

/usr/local/nagios/libexec/nagios-plugins-rabbitmq/scripts# ls
check_rabbitmq_aliveness check_rabbitmq_connections check_rabbitmq_objects check_rabbitmq_partition check_rabbitmq_server check_rabbitmq_watermark check_rabbitmq_cluster check_rabbitmq_exchange check_rabbitmq_overview check_rabbitmq_queue check_rabbitmq_shovels

As most of them are set with executable bit, run any check by giving absolute path or relative prefixing ./check_rabbitmq_aliveness

You can pass -h to print the help menu

The commands shall take the mandatory option of Hostname Port followed by the login credentials -u username and -p password

[email protected]:/usr/local/nagios/libexec/nagios-plugins-rabbitmq/scripts# sudo ./check_rabbitmq_server -H “RabbitMQ1.linuxcent.com” -u “guest” -p “guest”

Output

RABBITMQ_SERVER OK – Memory OK (2.63%) Process OK (0.08%) FD OK (0.27%) Sockets OK (0.10%) | Memory=2.63%;80;90 Process=0.08%;80;90 FD=0.27%;80;90 Sockets=0.10%;80;90

 

Similarly the other important plugin is to get the queue status.

This plugin provides various checks on the RabbitMQ

The most useful checks are monitoring and alerting the queue names on features such as the Messages in Queue, Messages in Ready State, Un-Acknowledged messages and the number of Consumers.

The order for the Warning and Critical are

messages ,messages_ready ,messages_unacknowledged ,consumers

Here we are monitoring the RabbitMQ queue download_queue, for the messages_ready if exceeding 0 then, warning notification will be sent.

[email protected]:/usr/local/nagios/libexec/nagios-plugins-rabbitmq/scripts# ./check_rabbitmq_queue -H “RabbitMQ1.linuxcent.com” -u “guest” -p “guest” –queue “download_queue” -w -1,0,-1,-1

Output

RABBITMQ_QUEUE CRITICAL – messages_ready CRITICAL (4219), messages OK (4269) messages_unacknowledged OK (50) consumers OK (2) | messages=4269;; messages_ready=4219;;0 messages_unacknowledged=50;; consumers=2;;

 

What is Bash

What is Bash

Bash is a program (toolbox) used to interact between user and operating system.

The BASH acronym is for the ‘Bourne-Again SHell’. Bash is one of many shells, or command language interpreters, in Unix system.

I called bash a “toolbox”, because it contains large variety of shell tools, for example, tools for read files, calculate math operations, control devices…

The main difference between bash and any other program is that bash was not created to process certain task. Bash was developed to take commands from user using the text-based interface.

One of the advantages of bash is that users can put list of commands to a one (script) and execute these commands from file.

Most of bash scripts have ‘.sh’ extension, for example: script.sh. Linux is an extensionless system, but it is good practice to add .sh extension.

VI Basic Commands for UNIX in Nutshell

In this chapter, we will explore vi – the excellent Unix editor. There are many ways to edit text files in Unix; however, one of the best is using screen-oriented editors like vi who allow you to see context lines around a line that needs editing.

VIM, or Vi IMproved (commonly shortened to vim) is an improved version of the vi editor. It has rapidly grown in popularity because it can be used as both a command line and graphical interface text-editor with more features than standard vi that you might not find elsewhere like syntax highlighting and multitasking capabilities.

  • It’s usually available on all the flavors of Unix system.
  • Its implementations are very similar across the board.
  • It requires very few resources.
  • It is more user-friendly than other editors such as the ed or the ex.

VI Basic Commands for UNIX in Nutshell

Vi has 3 basic modes of operation: command (default), input, last line mode.

VI Editor Command mode

In command mode, you can run commands to search, copy, move, remove text.

VI Editor Input mode

In input mode, you can insert text into the file. Everything you type will be interpreted as text. many ways how to activate input mode (vi is case sensitive):

  • i – Inserts text before the cursor.
  • I – Inserts text at the beginning of the line.
  • o – Opens a new blank line below the cursor.
  • O – Opens a new blank line above the cursor.
  • a – Appends text after the cursor.
  • A – Appends text at the end of the line.

VI Editor Last line mode

To get into the last line mode type ‘:’ only from command mode. After type ‘:’ you will see a colon character appear at the beginning of the last line of your vi editor. It means vi is ready for type a “last line command”. To end vi type ‘q’ from last line mode.
You can return to command mode from input or last line mode pressing Esc.

Moving the Cursor

Key Cursor movement
w Forward one word.
b Back one word.
e To the end of the current word.
$ To the end of the line.
0(zero) To the beginning of the line.
^ To the first non-whitespace character on the line.
G Goes to the last line of the file.
IG Goes to the first line of the file.
Ctrl + F Pages forward one screen.
Ctrl + B Pages back one screen.
Ctrl + D Scrolls down one-half screen.
Ctrl + U Scrolls up one-half screen.
Ctrl + L Refreshes the screen.

Text-Deletion Commands

Command Function
R Overwrites or replaces characters on the line at and to the right of cursor. To terminate press Esc.
C Changes or overwrites characters from cursor to the end of the line.
s Substitutes a string for a character at the cursor.
x Deletes a character at the cursor.
dw Deletes a word or part of the word to the right of the cursor.
dd Deletes the line containing the cursor.
D Deletes the line from the cursor to the right end of the line.
:n, nd Deletes lines n-n. Example :2,80d deletes lines 2-80.

Text-Changing Commands

Command Function
cw Changes or overwrites characters at the cursor location to the end of that word.
r Replaces the character at the cursor with one other character.
J Join the current line and the line below.
xp Transposes the character at the cursor and the character to the right of the cursor.
~ Changes the case of the letter, either uppercase or lowercase, at the cursor.
u Undo the previous command.
. Repeats the previous command.

Text-Replacing Commands

Command Function
/string Searches forward for the string from the cursor.
?string Searches backward for the string.
n Searches for the next occurrence of the string. Use this command after searching for a string.
N Searches for the previous occurrence of the string. Use this command after searching for a string.
:%s/old/new/g Searches for the old string and replaces it iwth the new string globally.

Copy and Paste Commands

Command Function
yy Yanks a copy of the line
p Puts yanked or deleted text under the line containing the cursor.
P Put
:n,n co n Copies lines n-n and puts them after line n. Example: 1, 5 co 8 copies lines 1-5 and puts them after line 8.
:n,n m n Moves lines n-n to line n.
Example: 1,5 m 8 moves lines 1-5 to line 8.

File Save and Quit Commands

Command Function
:w Saves the file with changes by writing to the disk
:w new_file Writes the contents of the buffer to new_file.
wq Saves the changed file and quits editor vi.
😡 Saves the changed file and quits editor vi.
ZZ Saves the changed file and quits editor vi.
:q! Quits without saving changes.

Customizing vi Session

Command Function
:set nu Shows line numbers.
:set nonu Hides line numbers.
:set ic Instructs searches to ignore cases.
:set noic Instructs searches to be case-sensitive.
set list Display invisible characters.
:set showmode Display the current mode of operation.
:set noshowmode Turns off the mode of operation display.
:set Displays all the vi variables that are set.
:set all Display all vi variables and their values.

Customizing vi Session
To automatic customization for all vi sessions do the following steps:

  • Create a file in your home directory named ‘ . exrc’
  • Enter any of the set variables into the ‘ . exrc’ file.
  • Enter each ‘set variable’ command on one line.

Vi reads ‘exrc’ file every time before starting vi sessions.

Command Function
:set nu Shows line numbers.
:set nonu Hides line numbers.
:set ic Instructs searches to ignore case.
:set noic Instructs searches to be case-sensitive.
set list Display invisible characters.
:set showmode Display the current mode of operation.
:set noshowmode Turns off the mode of operation display.
:set Displays all the vi variables that are set.
:set all Display all vi variables and their values.

Puppet: Error: Could not parse for environment : Illegal class reference

The following errors appear due to missing of certain modules.

Error: Could not parse for environment production: Illegal class reference

To resolve this error you need to install the relevant puppet module from the module-repository.

For example :

Error: 'module' has no 'info' action.  See `puppet help module`.
[vamshi@node01 manifests]$ puppet module install puppetlabs-stdlib
Notice: Preparing to install into /home/vamshi/.puppetlabs/etc/code/modules ...
Notice: Created target directory /home/vamshi/.puppetlabs/etc/code/modules
Notice: Downloading from https://forgeapi.puppet.com ...
Notice: Installing -- do not interrupt ...
/home/vamshi/.puppetlabs/etc/code/modules
└── puppetlabs-stdlib (v6.3.0)

Redirect the Std error and std output 2>&1

The redirect operation > is used in conjunction with stdoutput 1 and stderr 2.

command > [/dev/null] 2>&1

2 Represents the stderror. The &1 here represents the first argument which is /dev/null

The character 2 represents the stderr which takes the entire errors printed to the screen and then appends them to the /dev/null which is the first argument represented by &1.

So the command demonstration will be the following:

$ du -sh /* > /dev/null 2>&1

This redirect command will dump the errors and the output to /dev/null.

Explanation: the default behaviour of redirection operator is to redirect the stdout and we are redirecting them to devnul and then we followup the command with 2>&1 which mentions the stderr 2 and then redirects is to /dev/null, which is denoted by &1 describing the &1 as the first argument which is /dev/null

 

 

check linux version

The Linux OS has many Distributions and various versions with slight modifications in the kernel versions, a major and minor version identification is crucial to practical administration of a linux server. Linux being open source, the release cycles are continuous resulting in many changes with Longterm release and short term release cycles in some most known and popular linux Distributors like Centos/Debian.. A special mentioning of the generic Desktop PC/laptop based versions of fedora/Ubuntu/Arch/OpenSuse Leap-Tumbleweed Linux where fan following is heavy, the Release cycles are very aggressive with new versions releasing once in for every 2-3 weeks.

In this Section we will see how to check linux version, on most popular Distributions.
This will be handy during the kernel patch process to identify the current linux version.
Lets list our some practical linux commands and scenarios

Checking the Linux OS Version using /etc/os-release

[vamshi@node02 ]$ cat /etc/os-release
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"

Checking the Linux Version from the file /etc/issue

This file is being relinquished in latest Systemd variants of Linux, But tends to gives a less adequate information just lying around for aged linux user’s familiarity with systemd versions.

vamshi@node03:/$ cat /etc/issue
Debian GNU/Linux 10 \n \l

Check Linux Version using lsb_release:

The lsb_release prints the (Linux Standard Base) and Distribution information on the Linux host.

[vamshi@node02 cp-command]$ lsb_release -a
LSB Version:    :core-4.1-ia32:core-4.1-noarch
Distributor ID:    CentOS
Description:    CentOS Linux release 7.7.1908 (Core)
Release:    7.7.1908
Codename:    Core

How to check the running kernel version information ?

Check Linux Version information with uname command

[vamshi@node02 cp-command]$ uname -a
Linux node02.linuxcent.com 3.10.0-1062.9.1.el7.x86_64 #1 SMP Fri Dec 6 15:49:49 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

Checking the Linux Version using hostnamectl

The command hostnamectl gives the complete information of the underlying architecture, kernel version and the Linux OS name

vamshi@node03:/$ hostnamectl 
Static hostname: node03
Icon name: computer-vm
Chassis: vm
Machine ID: b4adcdb84c724856b577524ebbfa0003
Boot ID: 1a2db0a0ae8b4c5ba86b390c68af7024
Virtualization: oracle
Operating System: Debian GNU/Linux 10 (buster)
Kernel: Linux 4.19.0-5-amd64
Architecture: x86-64

How to create Temporary filesystem on Linux

The greatest advantage with a tmpfs is that you can use the faster access Volatile memory to store files instead of Secondary storage system.

Lets head off to the Demonstration using tmpfs.

Create a Directory named test-docs in /mnt

[vamshi@SERVER02 mnt]$ sudo mkdir /mnt/test-docs

We will be using the dd command to dump the data and create a file with given Block size, as below and compare the write speeds on tmpfs (Linux temporary Filesystem) vs conventional storage filesystem.

[vamshi@SERVER02 mnt]$ sudo mount -t tmpfs -o size=1G tmpfs /mnt/test-docs

Output from the mount command:

[vamshi@SERVER02 mnt]$ df -hT /mnt/test-docs/
Filesystem     Type   Size Used Avail Use% Mounted on
tmpfs          tmpfs  1.0G     0  1.0G   0% /mnt/test-docs

Now add the entry to /etc/fstab to make the mount persistent across reboots(We should write How to create an fstab entry in another article)

tmpfs       /mnt/webdocs tmpfs   nodev,nosuid,noexec,nodiratime,size=1G   0 0

With these changes it is going to perform faster system read writes during the runtime.

Please advise caution as this uses the RAM.

Now to test the setting Temporary Filesystem vs Storage Filesystem write speeds

We will demonstrate see about the temporary(tmpfs) filesystem on linux in action and practical examples.

Writing a 500MB file on the same server to a Secondary storage vs Writing it to tmpfs.

Lets us write a single 500MB file and measure its write speed and time it’s operation

[vamshi@SERVER02 mnt]$ time sudo dd if=/dev/zero of=/mnt/test-docs/dump.txt bs=1k count=500000
500000+0 records in
500000+0 records out
512000000 bytes (512 MB) copied, 0.815591 s, 628 MB/s

real    0m0.871s
user    0m0.103s
sys    0m0.760s

 

This operation finished under 0.8 seconds time which is around 100 milliseconds lesser than 1 second and write operation completed with 628MB/s.

Lets check on the file size on the tmpfs mountpoint /mnt/test-docs/

[vamshi@SERVER02 mnt]$ df -hT /mnt/test-docs/
Filesystem     Type   Size Used Avail Use% Mounted on
tmpfs          tmpfs  1.0G 489M  536M 48% /mnt/test-docs

Now lets run the same write operation to the secondary storage filesystem

[vamshi@SERVER02 mnt]$ time sudo dd if=/dev/zero of=/mnt/dump.txt bs=1k count=500000
500000+0 records in
500000+0 records out
512000000 bytes (512 MB) copied, 1.38793 s, 369 MB/s

real    0m1.510s
user    0m0.028s
sys    0m1.227s

The write operation to the disk took twice the time at a write speed of 369MB/s

The space usage on the [/code]/mnt/test-docs/[/code] after the operation is 489MB out of 1024MB.

[vamshi@SERVER02 mnt]$ df -hT /mnt/test-docs/
Filesystem     Type   Size Used Avail Use% Mounted on
tmpfs          tmpfs  1.0G 489M  536M 48% /mnt/test-docs

CONCLUSION:

The write/read speeds to tmpfs were substantially faster compared to the secondary storage block device mount points, The fact of the matter is tmpfs although the clear winner is tmpfs, it cannot be used for persistent data storage as its just sits on top of the memory and in no way offers long term data storage persistence.

So what is a practical usage of tmpfs in linux ?

The practical usage can be storing of the static web content to serve the images,css and js to fasten the request speeds.
The secondary storage as we know for straight persistence and long term data storage capabilities.

Control Structures in Linux BASH

The Linux BASH provides many operators to check against test keyword
We are going to explain the Conditional comparison Operators for the Keyword parameter totest or [

For a detailed explanation of the Keyword test or [ Please refer to our Control Structure: Bash If then Else.

 

File Comparison Operators
Condition Explanation
-d “$file” Returns true if the file exists and is a directory
-e “$file” Returns true if the file exists.
-f “$file” Returns true if the file exists and is a regular file
-h “$file” Returns true if the file exists and is a symbolic link

 

String Comparators Condition  Explanation
“$str” = “$str2” True if string $str is equal to string $str2. Not best for integers.
“$str” != “$str2” True if the strings are not equal
-z “$str” True if length of string is zero
-n “$str” True if length of string is non-zero

 

Integer Comparators Condition Explanation
“$int1” -eq “$int2” True if the integers are equal
“$int1” -ne “$int2” True if the integers are not equals
“$int1” -gt “$int2” True if $int1 is greater than $int2
“$int1” -ge “$int2” True if $int1 is greater than or equal to $int2
“$int1” -lt “$int2” True if $int1 is less than $int2
“$int1” -le “$int2” True if $int1 is less than or equal to $int2

 

How to Remove Files and Directories in Linux using Command line

The Remove command in Unix/Linux ecosystem is very powerful and effective one time operation as it is unrecoverable.

To delete different kinds of content various options can be used with the rm command in linux.

In this section , we will demonstrate how to use the go about rm, unlink, and rmdir commands to remove files and directories in Linux

The Linux system treats the files and Directories by identifying them with Inodes.

Deleting a file is a simple operation but the user has to advise a lot of caution.

Sample Syntax

$ rm filename

Now lets Demonstrate deleting a file

Let’s use the -v flag to print the verbose information onto the screen.

[vamshi@linuxcent delete-dir]$ rm -v file1 file2 file3

removed ‘file1’

removed ‘file2’

removed ‘file3’

Using -i option asks the user for interaction and deletes the file upon accepting a “y” as user input.

[vamshi@linuxcent delete-dir]$ rm -i file3
rm: remove regular file ‘file3’? y

Deleting multiple files from the command line.

Using the wildcard * to delete similar extension filenames

[vamshi@linuxcent delete-dir]$ rm -v ./*.pdf

removed ‘./samplePDFfile1.pdf’

removed ‘./samplePDFfile2.pdf’

removed ‘./samplePDFfile3.pdf’

It’s a good practice to use (-v) verbose mode while running rm command.

Now let’s focus on the best practice

[vamshi@linuxcent ]$ alias rm="rm -iv"

Ensure to export the changes to your profile as demonstrated here(Exporting the changes to user login profile permanently)

[vamshi@linuxcent ]$ rm samplefilename.txt
rm: remove regular empty file ‘file10’? y
removed ‘samplefilename.txt’

But One has to always ensure to see the file permissions and if they are currently being used by any user or a program.(fuser and what it does)

You can long list the filename to see the permissions(long listing the files on Linux system)

How to Remove the Directory/s from Linux system?

A command-line utility rmdir exists on Linux to delete the directories. But, it deletes only the Empty directory(ies).

The command rm -d or --dir is also preferred to delete an empty directory

The rm command-line utility also lets you delete the directories and its contents recursively with -r option.

Sample Command Syntax:

[vamshi@linuxcent delete-dir]$ rmdir  sampleDIR1

We can use the -v option to print the verbose information:

[vamshi@linuxcent delete-dir]$ rmdir -v sampleDIR1

rmdir: removing directory, ‘sampleDIR1’

Deleting multiple subdirectories that are empty can be done with -p option, its demonstrated as follows:

[vamshi@linuxcent ]$ rmdir -v -p sampleDIR/subdir/
rmdir: removing directory, ‘sampleDIR/subdir/’
rmdir: removing directory, ‘sampleDIR’

Deleting multiple files from the command line.

[vamshi@linuxcent ]$ rmdir sampleDIR1 sampleDIR2 sampleDIR3

Now let’s shift our focus to rm command which offers the ability to delete the directories and its contents recursively.

[vamshi@linuxcent ]$ rm -v -r sampleDIR3/
removed directory: ‘sampleDIR3/subdir3’
removed ‘sampleDIR3/samplefile3.txt’
removed directory: ‘sampleDIR3/’
[vamshi@linuxcent delete-dir]$ rm -v -r sampleDIR6 sampleDIR7 sampleDIR8
removed directory: ‘sampleDIR6/subdir6’
removed directory: ‘sampleDIR6’
removed directory: ‘sampleDIR7/subdir7’
removed directory: ‘sampleDIR7’
removed directory: ‘sampleDIR8/subdir8’
removed directory: ‘sampleDIR8’

Please also see our post on How to make a file undeletable on Linux