Setup Nagios Server and Client

Scenario
In this tutorial i am going to use two systems as mentioned below.

Nagios server:

Operating system : CentOS 6.5 minimal installation
IP Address       : 192.168.1.101/24

Nagios client:

Operating System : Ubuntu 13.04
IP Address       : 192.168.1.100/24
Prerequisites
Before installing Nagios, make sure that you’ve a properly installed and configured LAMP stack in your server. To install and configure LAMP server, refer any one of the following links.
Also install the following prerequisites too. All commands should be run as root user.
# yum install gd gd-devel gcc glibc glibc-common
Install Nagios

I tested this how-to on CentOS 6.5 minimal server, although it should work on all RHEL 6.x and its clones like CentOS 6.x and Scientific Linux 6.x.
Nagios will not be found in CentOS official repositories, so let us add the EPEL repository to install nagios. To add and enable EPEL repository :
First go to the EPEL repository download page. Download the latest EPEL rpm and install it as shown below.

For x86
# wget http://epel.mirror.net.in/epel/6/i386/epel-release-6-8.noarch.rpm
 
For x64
#wget http://epel.mirror.net.in/epel/6/x86_64/epel-release-6-8.noarch.rpm 
 # rpm -Uvh epel-release-6-8.noarch.rpm
Adding EPEL Repository to RHEL / CentOS / Scientific Linux 5.x
First go to the EPEL repository download page. Download the latest EPEL rpm and install it as shown below.
# wget http://epel.mirror.net.in/epel/5/i386/epel-release-5-4.noarch.rpm
# rpm -Uvh epel-release-5-4.noarch.rpm
Now list out the installed repositories using command:
# yum repolist


Next install nagios with all plug-ins and nagios agents(nrpe-agent) using command:
# yum install nagios*
Note: It worked for me either if i keep SELINUX and iptables enable or disable. All you need to do is allow the apache port through iptables as shown below.
# vi /etc/sysconfig/iptables
Add the following line:
[...]
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
[...]
Save and close the file. Restart iptables.
# service iptables restart
Configure Nagios
Add the admin mail address in the nagios contact file to receive alerts from nagios server. To do that edit file /etc/nagios/objects/contacts.cfg,
# vi /etc/nagios/objects/contacts.cfg
Find the following line and enter the email id:
[...]
email                           sk@unixmen.com ;
[...
Save and close the file. Then Edit file /etc/httpd/conf.d/nagios.conf,
# vi /etc/httpd/conf.d/nagios.conf
And edit the following lines if you want to access nagios administrative console from a particular IP series. Here i want to allow nagios administrative access from 192.168.1.0/24 series only.
[...]
## Comment Lines 15 & 16 ##
#   Order allow,deny
#   Allow from all

## Uncomment and Change lines 17,18 & 19 as shown below ##
Order deny,allow
Deny from all
Allow from 127.0.0.1 192.168.1.0/24
[...]
Set nagiosadmin password
# htpasswd /etc/nagios/passwd nagiosadmin
New password:
Re-type new password:
Updating password for user nagiosadmin
Start nagios and httpd services and let them to start automatically on every boot.
# service nagios start
# service httpd start
# chkconfig nagios on
# chkconfig httpd on
Access Nagios admin console
Open nagios administrator console with URL http://nagios-server-ip/nagios and enter the username as nagiosadmin and its password which we created in the earlier steps.

Add Monitoring targets to Nagios server
Now let us add some clients to monitor by Nagios server. To do that we have to install nrpe and nagios-plugins in our monitoring targets.
On CentOS/RHEL/Scientifc Linux clients:
Like i said before, you have to add EPEL repository in your CentOS/RHEL/Scientific Linux 6.x clients to install nrpe package.
Install “nrpe” and “nagios-plugins” packages in client systems to be monitored.
# yum install nrpe nagios-plugins-all openssl
On Debian/Ubuntu clients:
$ sudo apt-get install nagios-nrpe-server nagios-plugins
Configure Monitoring targets
Edit /etc/nagios/nrpe.cfg file,
# vi /etc/nagios/nrpe.cfg
Add your Nagios server ip address
[...]
## Line 81 - Add the Nagios server IP ##
allowed_hosts=127.0.0.1 192.168.1.101
[...]
## line around 215 add some plugin like mailq posfix
command[check_mailq]=/usr/lib/nagios/plugins/check_postfix_queue -w 300 -c 1000
Start nrpe service:
# service nrpe start
# chkconfig nrpe on
For Debian/Ubuntu Clients, start nrpe service as shown below.
$ sudo /etc/init.d/nagios-nrpe-server restart
Now go back to your Nagios server to add the clients to be monitored through nagios server. Edit “/etc/nagios/nagios.cfg” file,
# vi /etc/nagios/nagios.cfg
and uncomment the following lines.
## Line 52 - Uncomment ##
cfg_dir=/etc/nagios/servers
Create a directory called “servers” under “/etc/nagios/”.
# mkdir /etc/nagios/servers
Create config file to the client to be monitored:
# vi /etc/nagios/servers/clients.cfg
define host{

use                             linux-server

host_name                       client

alias                           client

address                         192.168.1.100  ## Client IP ##

max_check_attempts              5

check_period                    24x7

notification_interval           30

notification_period             24x7

}
Finally restart nagios service.
# service nagios restart
Now open the nagios admin console in the browser and navigate to “Hosts” section in the left pane. You will see the newly added client will be visible there. Click on the host to see if there is anything wrong or alerts.


Like this way, you can define more clients by creating a separate config files “/etc/nagios/servers directory for each client.
Define services
We did define the monitoring host before. Now let us add some services of the monitoring host. For example to monitor the ssh service, add the following lines shown in red colour in the /etc/nagios/servers/clients.cfg file.
# vi /etc/nagios/servers/clients.cfg
define host{

use                             linux-server

host_name                       client

alias                           client

address                         192.168.1.100  ## Client IP ##

max_check_attempts              5

check_period                    24x7

notification_interval           30

notification_period             24x7

}

define service {
        use                             generic-service
        host_name                       client
        service_description             SSH
        check_command                   check_ssh
        notifications_enabled           0
        }
Save and close the file. Restart Nagios.
# service nagios restart
Now log in to Nagios web console and check for the added services. Navigate to Services section, you’ll see the ssh service there.
Nagios Core - Mozilla Firefox_008To know more about object definitions such as Host definitions, service definitions, contact definitions and more please do visit here. This page will describe you the description and format of all object definitions.
That’s all about now. Good Luck! Stay healthy!


cut command CUT command

Select specific field 
 
$ cut -d':' -f1 /etc/passwd                                             #f1=field1 #d=delimiter
 
Multiple feilds 
 
history
 | awk '{$1=$1;print}' | cut -d' ' -f2-       #remove beginning spaces 
and history number , cut -f2- will start printing from 2nd field to last
 
history | awk '{$1=$1;print}' | cut -d' ' -f1-4     # cut -f1-4 will print from field 1 to 4 
$ grep "/bin/bash" /etc/passwd | cut -d':' -f1,6
root:/root
bala:/home/bala
 
Multiple feilds with defined range
 
$ grep "/bin/bash" /etc/passwd | cut -d':' -f1-4,6,7
 
root:x:0:0:/root:/bin/bash
bala:x:1000:1000:/home/bala:/bin/bash 
 
Example: Usage
 
[root@mx2 ~]# cat rbllist 
server=cbl.abuseat.org
server=spamtrap.drbl.drand.net
server=spamguard.leadmon.net
server=ix.dnsbl.manitu.net
server=relays.nether.net
server=rbl.orbitrbl.com
[root@mx2 postfix]# cat rbllist | cut -d'=' -f2 | while read line; do echo $line, ; done
 
cbl.abuseat.org,
spamtrap.drbl.drand.net,
spamguard.leadmon.net,
ix.dnsbl.manitu.net,
relays.nether.net,
rbl.orbitrbl.com,

PAC (Perl Auto Connector) for Debian Linux , by David Torrejon Vaquerizas

download PAC Manager fromhttp://sourceforge.net/projects/pacmanager/
which is a debian package

Now install the pac.X.deb file from Ubuntu Software (Center GUI) or from terminal like below :-
 
#sudo dpkg -i pac-4.5.4-all.deb
 
this step is optional
# apt-get install libgtk2-appindicator-perl


du

$ du -c
This gives you a grand total as the last line of the output. eg. 

[root@mx2 MailScanner]# du -ch
... .....
116K    ./reports/cz
116K    ./reports/cy+en
1.8M    ./reports
16K    ./mcp
8.0K    ./conf.d
2.3M    .
2.3M    total


 $du -sh
Gives you directory size  eg. MailScanner is directory
[root@mx2 etc]# du -sh MailScanner
2.3M    MailScanner


[root@mx2 ~]# du -sh *
4.0K    Mail
5.9M    MailScanner-4.84.6-1
5.7M    MailScanner-4.84.6-1.rpm.tar.gz
3.3M    rpmbuild
412K    sent
100K    stuff
22M    webmin-1.680-1.noarch.rpm



mail scanner

http://blog.sekiur.com/2008/09/setting-up-a-mail-relay-on-centos-5/

http://zee.linxsol.com/system-administration/centos-62-installing-postfix-clam-anti-virus-and-mailscanner.html

http://learn-with-mono.blogspot.com/2012/12/mailscanner-clamav-spamassassin-and.html


http://ostechnix.wordpress.com/2013/02/08/setup-mail-server-using-postfixdovecotsquirrelmail-in-centosrhelscientific-linux-6-3-step-by-step/

http://www.linuxmail.info/mailscanner-postfix-clamav-spamassassin-howto-centos-5/
-------------------===================----------------------------------------------------------

http://www.mailscanner.info/man/MailScanner.conf.5.html

Tar command

Extract an archive to a directory different from the current, use the -C   or --directory, tar option, as in
 
tar -xf archive.tar -C /target/directory 
 
Tar gzip compression
$ tar cvzf archive_name.tar.gz dirname/

Tar bzipped compression
$ tar cvfj archive_name.tar.bz2 dirname/ 

Adding a file or directory to an existing archive using option -r

$ tar rvf archive_name.tar newdir/
Note: You cannot add file or directory to a compressed archive. 

Estimate the tar archive size

The following command, estimates the tar file size ( in KB ) before you create the tar file.
$ tar -cf - /directory/to/archive/ | wc -c
20480

Listing an archive using tar command

View the tar archive file content without extracting using option tvf

You can view the *.tar file content before extracting as shown below.
$ tar tvf archive_name.tar

View the *.tar.gz file content without extracting using option tvzf

You can view the *.tar.gz file content before extracting as shown below.
$ tar tvfz archive_name.tar.gz

dig DNS

# dig -t ANY  example.com

Query from specific dns server

dig -t ANY @dnsserver/ip  example.com

To check which dns server you are using :

cat /etc/resolv.conf

or(i used wifi so couldnt find in resolv.conf and ran below cmd)

nm-tools | grep DNS
==============================================
Display multiline queries
This +multiline option will show you records like the SOA records in a verbose multi-line format with human-readble comments. Here’s a sample of it.
$ dig  hotmail.com +multiline +noall +answer


root@devopserver:/home# dig +nocmd google.com ANY +multiline +noall +answer

ogi.edu.        14390 IN SOA zeal.admin.ogi.edu. hostmaster.admin.ogi.edu. (
                201301181  ; serial
                600        ; refresh (10 minutes)
                900        ; retry (15 minutes)
                3600000    ; expire (5 weeks 6 days 16 hours)
                600        ; minimum (10 minutes)
                )
ogi.edu.        14390 IN NS fork.admin.ogi.edu.
ogi.edu.        14390 IN NS dns1.ohsu.edu.
ogi.edu.        14390 IN NS zeal.admin.ogi.edu.
ogi.edu.        14390 IN NS cse.ogi.edu.
ogi.edu.        14390 IN A 129.95.20.4
ogi.edu.        14390 IN MX 5 anise.csee.ogi.edu.
ogi.edu.        14390 IN MX 15 hermes.admin.ogi.edu.
ogi.edu.        14390 IN MX 3 castor.bme.ogi.edu.
..............
+nocmd is used to supress display of dig version numbers etc.
---------------------------------------------------------------------------------------------------------------------------------
Reverse lookup example
----------------------------------------------------------------------------------------------------------------------------


root@devops-server:/home/devops1# dig +nocmd -x 8.8.8.8 +noall +answer
#use -short for short answer


------------------------------------------------------------------------------------------------------------------------------------
Other examples
------------------------------------------------------------------------------------------------------------------------------------
Query from file  eg. names.txt
$ dig -f names.txt +noall +answer
redhat.com.             60      IN      A       209.132.183.81
centos.org.             60      IN      A       72.232.194.162


$cat names.txt
redhat.com
centos.org

-------------------------------------------------------

dig +nocomments www.cyberciti.biz
+++++++++++++++++++++++++++++++++++++++++++++++
Custom script
++++++++++++++++++++++++++++++++++++++++++++++
[sshadmin@mx12 ~]$ cat digg.sh 
#!/bin/bash
#echo "Enter the domain name:"
printf "\n"
read -p "Enter the domain name: " fqdn
printf "\n"
echo "------------Query Result for NS ------------------"
printf "\n"
/usr/bin/dig +nocmd $fqdn NS  +noall +answer
printf "\n"
printf "\n"
echo "------------Query Result for A Record---------------"
printf "\n"
/usr/bin/dig +nocmd $fqdn  +noall +answer
printf "\n"
echo "------------MX Query Result------------------"
printf "\n"
printf "please wait for mx " 
sleep 1
printf "."
sleep 1
printf "."
sleep 1
printf "\n"
printf "\n"
/usr/bin/dig +nocmd $fqdn MX +noall +answer
printf "\n"
 
 
#use any for all in one
dig +nocmd google.com ANY +noall +answer 

Remove web search in Ubuntu dash board search

Save and Run below script ============================================================ #!/bin/bash GS="/usr/bin/gsettings" CCUL="com.canonical.Unity.lenses" # Figure out the version of Ubuntu that you're running V=`/usr/bin/lsb_release -rs` # The privacy problems started with 12.10, so earlier versions should do nothing if awk "BEGIN {exit !($V < 12.10 || $V >= 14.10)}"; then echo "Good news! This version of Ubuntu is not known to invade your privacy." else # Check Canonical schema is present. Take first match, ignoring case. SCHEMA="`$GS list-schemas | grep -i $CCUL | head -1`" if [ -z "$SCHEMA" ] then printf "Error: could not find Canonical schema %s.\n" "$CCUL" 1>&2 exit 1 else CCUL="$SCHEMA" fi # Turn off "Remote Search", so search terms in Dash don't get sent to the internet $GS set $CCUL remote-content-search none # If you're using earlier than 13.10, uninstall unity-lens-shopping if [ $V \< 13.10 ]; then sudo apt-get remove -y unity-lens-shopping # If you're using a later version, disable remote scopes else $GS set $CCUL disabled-scopes \ "['more_suggestions-amazon.scope', 'more_suggestions-u1ms.scope', 'more_suggestions-populartracks.scope', 'music-musicstore.scope', 'more_suggestions-ebay.scope', 'more_suggestions-ubuntushop.scope', 'more_suggestions-skimlinks.scope']" fi; # Block connections to Ubuntu's ad server, just in case if ! grep -q "127.0.0.1 productsearch.ubuntu.com" /etc/hosts; then echo -e "\n127.0.0.1 productsearch.ubuntu.com" | sudo tee -a /etc/hosts >/dev/null fi echo "All done. Enjoy your privacy." fi