Add Linux Host to Nagios Monitoring Server (Complete Script)

In this post, I have created a script which involves all the steps which we perform while adding Linux host in Nagios monitoring server.

Download script here: plugin-nrpe-install script

#!/bin/bash

Path1=/opt/softwares/nagios-plugins-2.1.1  ## this path may varies with the current veriosn
Path2=/opt/softwares/nrpe-2.15                   ##  of plugin and nrpe.

echo -e "\e[1;32mNagios Client Installation...\e[0m"

Packages="wget gcc glibc glibc-common gd gd-devel make net-snmp openssl-devel"
for pkg in $Packages; do

if [ -z $(rpm -qa $pkg) ];
then
echo -e "\e[1;31m$pkg not installed\e[0m"
echo "$pkg" >> /tmp/notinstalled.txt
else
echo -e "\e[1;32m$pkg installed\e[0m"
fi
done
##############################################
for i in `cat /tmp/notinstalled.txt`;
   do
   /usr/bin/yum install $i -y
#    echo "$i is not installed."
#   echo -e "\e[1;32mAll the Packages are installed\e[0m"
   >/tmp/notinstalled.txt
done
##############################################
USERID="nagios"
ID='/usr/bin/id'
/bin/egrep  -i "^${USERID}:" /etc/passwd
if [ $? -eq 0 ]; then
   echo -e "\e[1;32mUser $USERID exists in /etc/passwd \e[0m"
   echo -e "\e[1;32mNagios ID is $ID nagios \e[0m"
else
   echo -e "\e[1;32mUser $USERID does not exists in /etc/passwd \e[0m"
   echo  -e "\e[1;32mAdding user Nagios \e[0m"
   useradd nagios
   echo "nagios123" | passwd nagios --stdin
fi
##########################################
echo -e "\e[1;32mDownloading the nagios and nrpe plugins\e[0m"
mkdir /opt/softwares/
cd /opt/softwares/
/usr/bin/wget "http://www.nagios-plugins.org/download/nagios-plugins-2.1.1.tar.gz" && tar xf nagios-plugins-2.1.1.tar.gz
/usr/bin/wget --no-check-certificate "https://sourceforge.net/projects/nagios/files/nrpe-2.x/nrpe-2.15/nrpe-2.15.tar.gz" && tar xf nrpe-2.15.tar.gz
################################################
if [ ! -d "/usr/local/nagios" ]
then
    echo -e "\e[1;31Directory "/usr/local/nagios" does not exists.\e[0m"
    echo -e "\e[1;32Instaling nagios plugins\e[0m"
    cd $Path1
    ./configure
    make
    make install
    /bin/chown nagios.nagios /usr/local/nagios
    /bin/chown -R nagios.nagios /usr/local/nagios/libexec
    sleep 5
    echo -e "\e[1;32mInstalling xinetd package\e[0m"
    yum install xinetd -y
###########################################
    echo -e "\e[1;32mInstalling nrpe plugin\e[0m"
    cd $Path2
    ./configure
    make all
    make install-plugin
    make install-daemon
    make install-daemon-config
    make install-xinetd
    sleep 5
/bin/sed -i 's/127.0.0.1/127.0.0.1 localhost 192.168.1.207/g' /etc/xinetd.d/nrpe
 ## Higlighted IP will be replaced with your Nagios server IP      
/bin/echo "nrpe            5666/tcp                #NRPE" >> /etc/services

    service xinetd start
    chkconfig xinetd on
/bin/netstat -tlpn | grep xinetd
/usr/local/nagios/libexec/check_nrpe -H localhost
/bin/echo -e "\e[1;32mNAGIOS CLIENT SUCCESSFULLY INSTALLED\e[0m"
else
    echo -e "\e[1;32mDirectory /usr/local/nagios exists.\e[0m"
fi


Refernce Link: Tecmint

Any Changes/Suggestions are appreciated.

Managing user's in Linux using script

1. Simple Script to add multiple users in one go.

#!/bin/bash
filename="userslist.txt"  #< contains list of user's 
while read -r username
do
   name=$username
   echo "User created : " $username
   useradd $username
done < $filename

2. Simple script to provide a password to the list of user’s in one go.

#!/bin/bash
filename="userslist.txt" 
password="redhat"    #< provide password of your choice
while read -r username
do 
  name=$username
  echo "password provided for user :" $username
  echo "$password" | passwd $username --stdin  
done < $filename

3. Simple script to change the shell of the bulk of user’s in one go.

#!/bin/bash
filename="userslist.txt"  #< contains list of user's whose shell going to change
while read -r username
do 
  name=$username
  echo "changing shell for user :" $username
  chsh -/sbin/nologin $username   #< provide the shell you want to change to.
done < $filename


Copy directory structure from one location to another without files

Suppose that I have a directory with name xyz under /opt and I want to copy same directory structure (without files under it) to /mnt with directory name pqr.

Following is the directory structure containing sub-directories and files.

/opt/xyz/
├── dir1
│   ├── f1
│   │   └── file2
│   └── file1
├── dir2
│   └── f2
│       ├── f3
│       └── file3
└── dir3
    └── f4
        ├── f5
        │   └── file5
        └── file4

Command is:

#rsync -a --include '*/' --exclude '*' /opt/xyz/ /mnt/pqr
or
#cd /opt/xyz && find . -type d -exec mkdir -p /mnt/pqr/{} \;

After executing one of the above command we will get same directory structure without files as below.

/mnt/pqr/
├── dir1
│   └── f1
├── dir2
│   └── f2
│       └── f3
└── dir3
    └── f4
        └── f5

Reference Link: how to copy the directory structure without the files in linux






Useful Commands

1. To get IP Address of Machine.

#ifconfig eth0| grep "inet addr:"|cut -c '21-35'
192.168.1.207 

# ifconfig eth0| grep "inet addr:"|awk -F ":" '{print $2}'|cut -d " " -f 1
192.168.1.207

# ifconfig eth0 |grep "inet addr:"|cut -d: -f2|awk '{print $1}'
192.168.1.207


2. To get Hardware (MAC) address of Ethernet Device.

#ifconfig eth0| grep "HWaddr"|cut -c '39-55'
00:0C:29:85:C9:E3

#ifconfig eth0| grep "HWaddr"|awk -F " "  '{print $5}'
00:0C:29:85:C9:E3



3. To get load average at 1min, 5min and 15min.

# echo "Load1Min: `uptime |awk -F "load average:" '{print $2}'|cut -d, -f1|sed "s/ //g"`"
Load1min: 5.01

# echo "Load5Min: `uptime |awk -F "load average:" '{print $2}'|cut -d, -f2|sed "s/ //g"`"
Load1min: 2.21

# echo "Load15Min: `uptime |awk -F "load average:" '{print $2}'|cut -d, -f3|sed "s/ //g"`"
Load1min: 1.01

4. To get Memory usage in readable format.

#free -tom | awk '/Total:/ {print "Total memory: "$2" MB\nUsed memory: "$3" MB\nFree memory: "$4" MB"}'

Total Memory: 1503 MB
Used Memory: 2 MB
Free Memory: 1501 MB

Restrict Normal User to run all commands

1. Create user say test.

[root@node ~]# useradd test
[root@node ~]# passwd test
Changing password for user test.
New password:
BAD PASSWORD: it is too short
BAD PASSWORD: is too simple
Retype new password:
passwd: all authentication tokens updated successfully.

2. Create a directory say ‘commands’ under user’s home directory.

[test@node ~]$ pwd
/home/test
[test@node ~]$ mkdir /home/test/commands

3. Modify .bash_profile under user’s home directory.
By default, the environmental variable “PATH” defines the list of directories that contains binary executables which will be called when the user issues a command.

Here we are going to change the PATH variable to user’s home directory/commands folder as follows.

# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

# User specific environment and startup programs
PATH=$HOME/commands
export PATH


4. Now login with test user and try to hit some commands.

[test@node ~]$ ls
-bash: ls: command not found
[test@node ~]$ date
-bash: date: command not found
[test@node ~]$ mv
-bash: mv: command not found
[test@node ~]$ mkdir xyz
-bash: mkdir: command not found
[test@node ~]$ touch ab
-bash: touch: command not found
[test@node ~]$

5. Now create the softlinks of commands which are required for user test to execute in the directory /home/test/commands
Some examples are as follows.

[root@node ~]# ln -s /bin/date /home/test/commands/
[root@node ~]# ln -s /bin/mkdir /home/test/commands/
[root@node ~]# ln -s /bin/touch /home/test/commands/
[root@node ~]# ln -s /bin/ls /home/test/commands/

6. Now re-login as a test user and try above commands.

[root@node ~]# su - test
[test@node ~]$
[test@node ~]$ mkdir abc
[test@node ~]$ date
Sat Jun 10 13:14:27 IST 2017
[test@node ~]$ ls
abc  commands
[test@node ~]$ ls -lrth
total 8.0K
drwxrwxr-x. 2 test test 4.0K Jun 10 13:11 commands
drwxrwxr-x. 2 test test 4.0K Jun 10 13:14 abc
[test@node ~]$


How to create First Job in Jenkins with Subversion.

1. Login to Jenkins URL:

http://<server-ip>:8080, By Default Jenkins works on port 8080

Provide username and password to log in.    



2. Click ‘New Item’ on the left side to create a job.

Enter an Item Name i.e. the Job name, select Freestyle project and click OK to proceed.


Click on ‘This project is parameterized’, click on Add Parameter and select ‘String Parameter’


Now Under String Parameter provides Name as ‘Revision’, Default Value as ‘Head’, and add any Description.
This is needed when you build project with the parameter, it will ask for Revision Number of your choice else will build with Head revision by default.


3. Go to Source Code Management tab.

Select ‘Subversion’, provide the Repository URL, provide credentials who has the access to the Repository URL that you have provided.
Go to the “How to Create Project in SCM-Manager” for more details.


4. Go to the Build Tab.

Click on Add build step and select Execute Shell.
Provide the shell script path or shell script that Jenkins going to execute when you build the project.



For the easy installation of Jenkins please go to the http://pkg.jenkins-ci.org, select your OS ( like RPM or Debian based ) and the follow the steps given.




SCM-Manager Installation

1. RPM installation.

Create a new file at /etc/yum.repos.d/SCM-Manager.repo with the following content to install the scm-manager repository:

[scm-releases]
name=SCM-Manager Releases
baseurl=http://maven.scm-manager.org/nexus/content/repositories/releases
enabled=1
protect=0
gpgcheck=0
metadata_expire=30s
autorefresh=1
type=rpm-md


2. Install openjdk package.

  yum install java-1.x.0-openjdk

3. Install scm-server package.

  yum install scm-server
                           
4. By default, it runs on port 8080

Hit http://localhost:8080/scm on web browser, pProvide username, and password as ‘scmadmin’ and login.


Reference Link: SCM-Manager Configuration.

How to Create Project in SCM-Manager

1. Under Repositories tab click on Add to create a new project. 



2. Provide the Name of the Project under “Name:” field say project1
Select Subversion in drop down box under field “Type:”
Provide contact details such as email id under field “Contacts:”
Provide description of the project under field “Description:”
And click OK.



3. After creating the repository you will get the svn url under field “Url:”
This url is used for taking repository checkout and also committing the code in svn.    



4. Now add the users under Permission tab who are going to access this repository for taking checkout and committing new codes under the repository.