Search Windows and Linux Networking

Showing posts with label Linux. Show all posts
Showing posts with label Linux. Show all posts

Tuesday, March 18, 2014

How to print or display colorful text on screen in terminal or in bash scripting with echo command

How to print or display colorful text on screen in terminal    


 It is always good practice to show your script related information in colorful text in terminal to highlight or attention to the user. for example you can give output of script test as successful or fail in Green and red color, you can also display error message in Red color so user can attention for result\Output.

There are many method to do this for example with help of sed , awk , echo and may more. I will show you how to display text in colorful output with the help of echo command.

suppose you wanted to display only Hello world in Red color. you can do this with echo like follow

echo -e "\e[0;31m Hello World \e[om"

or
echo -e  "\033[31m Hello World \033[0m"
Hello World

suppose you wanted to display only Hello in Red color and World in Blue then your command look like follow

echo -e "\e[0;31m Hello \e[0;34m World \e[0m"
or
echo -e  "\033[31m Hello \033[34m World \033[0m"
Hello World


For Bold text Hello  World command looks:

echo -e "\e[1m Hello World\e[0m"
or
echo -e "\033[1m Hello World\033[0m"
Hello World

to underline the Hello world command is as follow:

echo -e "\e[4m Hello World\e[0m"
 Hello World
echo -e "\e[4;31m Hello World\e[0m"
 Hello World

Following are some other color code you wanted to use in your script.


Black 0;30
Red 0;31
Green 0;32
Orange 0;33
Yellow   1;33
Blue 0;34
Purple 0;35
Cyan 0;36
Light Gray 0;37



 

Friday, March 7, 2014

How to check File or Folder exist in Bash Scripting

How to check file of folder exist in Bash scripting.


When we write any bash scripting many time, we wanted to performing any task base on validating  for file or folder is exist or not . There are many ways to check for file or folder in Bash Scripting. In following example we will see how to check file or folder exist or not to do some task.


Suppose you wanted to check if folder name Backup exist in path /home/Backup and if it does not exist then create new folder as Backup in /home directory and if it exist the display the massage saying Backup folder exist. and you also wanted to check if file name backup.tar exist in path /home/Backup directory and if it doest not exist then create tar file as backup.tar in /home/Backup directory and if it exist then display massage that saying backup.tar file exist.


!#/bin/bash

# SCRIPT: backup.sh
# AUTHOR: Your Name.
# CONTACT : Your Contact details.
# DATE: 06th Mar, 2014
# REV: 1.0 A

# PURPOSE: To check Backup folder and backup.tar file exist or not. If not exist then create  Backup directory in path /home and backup.tar file in /home/Backup direcoty.

# 1st Way to check folder exist.

test -d /home/Backup
if [ "$?" -eq 0 ]
then
   echo "\"/Backup\" Directory exist in /home"
else
   echo "\"/Backup\" Directory does not exist in /home"
   echo "Creating /\"Backup\" Directory in \"/home\" Directory."
   mkdir /home/Backup
fi


# 2nd Way to check folder exist.

if test -d /home/Backup
then
   echo "\"/Backup\" Directory exist in /home"
else
   echo "\"/Backup\" Directory does not exist in /home"
   echo "Creating /\"Backup\" Directory in \"/home\" Directory."
   mkdir /home/Backup
fi

# 3rd Way to check folder exist.

if [ -d /home/Backup ]
then
   echo "\"/Backup\" Directory exist in /home"
else
   echo "\"/Backup\" Directory does not exist in /home"
   echo "Creating /\"Backup\" Directory in \"/home\" Directory."
   mkdir /home/Backup
fi

# There are also many other ways to check folder. for like using "ls -ld /home/Backup" command .
# To check file method is same as above just replace -d option with -f if you wanted to check file and if you wanted to check link replace with -l option.

# 1st Way to check file exist.

test -f /home/Backup/backup.tar
if [ "$?" -eq 0 ]
then
   echo "\"backup.tar\" file exist in /home/Backup Directory"
else
   echo "\"backup.tar\" file does not exist in \"/home/Backup\" Directory"
   echo "Creating \"backup.tar\" file in \"/home/Backup\" Directory."
   cd /
   tar -cvpf /home/Backup/backup.tar /home/sandeep
fi

# 2nd Way to check file exist.

if test -f /home/Backup/backup.tar
then
   echo "\"backup.tar\" file exist in /home/Backup Directory"
else
   echo "\"backup.tar\" file does not exist in \"/home/Backup\" Directory"
   echo "Creating \"backup.tar\" file in \"/home/Backup\" Directory."
   cd /
   tar -cvpf /home/Backup/backup.tar /home/sandeep
fi

# 3rd way to check file exist.

if [ -f /home/Backup/backup.tar ]
then
   echo "\"backup.tar\" file exist in /home/Backup Directory"
else
   echo "\"backup.tar\" file does not exist in \"/home/Backup\" Directory"
   echo "Creating \"backup.tar\" file in \"/home/Backup\" Directory."
   cd /
   tar -cvpf /home/Backup/backup.tar /home/sandeep
fi


# END

you can use any one method which is comfortable to you .

Friday, December 20, 2013

Step by Step : How to backup data using tar



Backup your data using tar

To backup or restore your data using tar always start backup/ restore from root (/) directory. To go to root (/) type the command

cd /

if you don't start backup from root (/) directory  then it will not restore data its original location. because when creating archive with tar. tar alway remove leading "/" from the member and that may be problem when restoring data at original location.

See the following example. I have started backup from home directory not from root (/) directory.

root@sandeepk:~# tar -cvpf /backup/exampleone.tar /home/sandeep/test
tar: Removing leading `/' from member names
/home/sandeep/test/
/home/sandeep/test/test3
/home/sandeep/test/test7
/home/sandeep/test/test2
/home/sandeep/test/test8
/home/sandeep/test/testmd/
/home/sandeep/test/testmd/test3
/home/sandeep/test/testmd/test7
/home/sandeep/test/testmd/test2
/home/sandeep/test/testmd/test8
/home/sandeep/test/testmd/test5
/home/sandeep/test/testmd/test6
/home/sandeep/test/testmd/test4
/home/sandeep/test/testmd/test9
/home/sandeep/test/testmd/test1
/home/sandeep/test/test5
/home/sandeep/test/test6
/home/sandeep/test/test4
/home/sandeep/test/test9
/home/sandeep/test/test1


Now test the data using following command

 root@sandeepk:~# tar -dvf /backup/exampleone.tar
home/sandeep/test/
tar: home/sandeep/test: Warning: Cannot stat: No such file or directory
home/sandeep/test/test3
tar: home/sandeep/test/test3: Warning: Cannot stat: No such file or directory


It will be give warning massage that No such file or directory. tar is checking data home/sandeep/test/ and not from /home/saneep/test

To resole the issue you should always start your backup from root directory (/)
 and now i will take another backup same data but from root directory

root@sandeepk:~# cd /
root@sandeepk:/#

root@sandeepk:/# tar -cvpf /backup/exampletwo.tar home/sandeep/test
home/sandeep/test/
home/sandeep/test/test3
home/sandeep/test/test7
home/sandeep/test/test2
home/sandeep/test/test8
home/sandeep/test/testmd/
home/sandeep/test/testmd/test3
home/sandeep/test/testmd/test7
home/sandeep/test/testmd/test2
home/sandeep/test/testmd/test8
home/sandeep/test/testmd/test5
home/sandeep/test/testmd/test6
home/sandeep/test/testmd/test4
home/sandeep/test/testmd/test9
home/sandeep/test/testmd/test1
home/sandeep/test/test5
home/sandeep/test/test6
home/sandeep/test/test4
home/sandeep/test/test9
home/sandeep/test/test1


Now verify data using following command

root@sandeepk:/# tar -dvf /backup/exampletwo.tar
home/sandeep/test/
home/sandeep/test/test3
home/sandeep/test/test7
home/sandeep/test/test2
home/sandeep/test/test8
home/sandeep/test/testmd/
home/sandeep/test/testmd/test3
home/sandeep/test/testmd/test7
home/sandeep/test/testmd/test2
home/sandeep/test/testmd/test8
home/sandeep/test/testmd/test5
home/sandeep/test/testmd/test6
home/sandeep/test/testmd/test4
home/sandeep/test/testmd/test9
home/sandeep/test/testmd/test1
home/sandeep/test/test5
home/sandeep/test/test6
home/sandeep/test/test4
home/sandeep/test/test9
home/sandeep/test/test1



Now delete the data from your system and try to restore it

root@sandeepk:/# rm -rf /home/sandeep/test
root@sandeepk:/# ls -l /home/sandeep/test
ls: cannot access /home/sandeep/test: No such file or directory
root@sandeepk:/# cd
root@sandeepk:~# tar -xvpf /backup/exampleone.tar
home/sandeep/test/
home/sandeep/test/test3
home/sandeep/test/test7
home/sandeep/test/test2
home/sandeep/test/test8
home/sandeep/test/testmd/
home/sandeep/test/testmd/test3
home/sandeep/test/testmd/test7
home/sandeep/test/testmd/test2
home/sandeep/test/testmd/test8
home/sandeep/test/testmd/test5
home/sandeep/test/testmd/test6
home/sandeep/test/testmd/test4
home/sandeep/test/testmd/test9
home/sandeep/test/testmd/test1
home/sandeep/test/test5
home/sandeep/test/test6
home/sandeep/test/test4
home/sandeep/test/test9
home/sandeep/test/test1


Now check if your data restored its original location or not  by using ls command.


root@sandeepk:~# ls -l /home/sandeep/test
ls: cannot access /home/sandeep/test: No such file or directory
root@sandeepk:~#


It not restore at it's original location if you run ls command you will see new home directory created and restore all data in that directory.

root@sandeepk:~# ls -l
total 12824
-rw-r--r-- 1 root root 13127680 Dec 18 15:07 archive
drwxr-xr-x 3 root root     4096 Dec 20 12:19 home



now extract from root (/) directory.


root@sandeepk:~# cd /
root@sandeepk:/# tar -xvpf /backup/exampletwo.tar
home/sandeep/test/
home/sandeep/test/test3
home/sandeep/test/test7
home/sandeep/test/test2
home/sandeep/test/test8
home/sandeep/test/testmd/
home/sandeep/test/testmd/test3
home/sandeep/test/testmd/test7
home/sandeep/test/testmd/test2
home/sandeep/test/testmd/test8
home/sandeep/test/testmd/test5
home/sandeep/test/testmd/test6
home/sandeep/test/testmd/test4
home/sandeep/test/testmd/test9
home/sandeep/test/testmd/test1
home/sandeep/test/test5
home/sandeep/test/test6
home/sandeep/test/test4
home/sandeep/test/test9
home/sandeep/test/test1

root@sandeepk:/#


root@sandeepk:/# ls -l /home/sandeep/test
total 4
-rw-r--r-- 1 root root    0 Dec 20 10:29 test1
-rw-r--r-- 1 root root    0 Dec 20 10:29 test2
-rw-r--r-- 1 root root    0 Dec 20 10:29 test3
-rw-r--r-- 1 root root    0 Dec 20 10:29 test4
-rw-r--r-- 1 root root    0 Dec 20 10:29 test5
-rw-r--r-- 1 root root    0 Dec 20 10:29 test6
-rw-r--r-- 1 root root    0 Dec 20 10:29 test7
-rw-r--r-- 1 root root    0 Dec 20 10:29 test8
-rw-r--r-- 1 root root    0 Dec 20 10:29 test9
drwxr-xr-x 2 root root 4096 Dec 20 10:30 testmd







Thursday, February 28, 2013

How to convert or resize image file using ImageMagick

 convert or resize image using ImageMagick


ImageMagick is a free software suite to create, edit, compose, or convert bitmap images. It can read and write images in a variety of formats (over 100) including DPX, EXR, GIF, JPEG, JPEG-2000, PDF, PhotoCD, PNG, Postscript, SVG, and TIFF. Use ImageMagick to resize, flip, mirror, rotate, distort, shear and transform images, adjust image colors, apply various special effects, or draw text, lines, polygons, ellipses and Bézier curves. ImageMagick is available for Linux , Mac and windows.

You can download ImageMagick from http://www.imagemagick.org


For Ubuntu user can install ImageMagick using

sudo apt-get install imagemagic

For CentOS user can install ImageMagick using

yum install ImageMagick ImageMagick-devel

suppose you have image.png file and you wanted to convert it in to jpg format. You can do this with command line with following command

convert  image.png image.jpg

if you have bunch of file suppose .png and  you wanted to convent it in jpg  format then put all .png file in one new directory and go to new directory path and execute following command to convert all png format file in to .jpg format

convert *.png new.jpg

and if you wanted all your jpg file in single one pdf file then you can use following command

convert *.png imagefile.pdf

and if you wanted to create single gif file of all your  jpg file you can do this using command

convert *.jpg newimage.gif
convert -delay 20 *.jpg  newimage2.gif

you can also convert your avi file in to gif  format using command

convert -quiet -delay 1 vidio.avi vidio.gif 


You can resize image by using resize option. for example

convert image.jpg -resize 50% half_image.jpg
 

you can do lot of task using command line with ImageMagick

Thursday, October 4, 2012

Installing VNC Server in Linux (CentOS)

How to install VNC server in CentOS


Install Required Packets using yum

# yum install vnc-server gnome-desktop xorg-x11-xinit xterm gnome-applets gnome-session gnome-themes gnome-panel gdm firefox dbus-x11

Set Password for VNC server user

# vncpasswd



Type new password and confirm it for accessing vnc server

Note:- To set password to user you must login with that user to create vnc password.

# vi /etc/sysconfig/vncservers

VNCSERVERS="1:root"
VNCSERVERARGS[2]=”-geometry 800×600 -nolisten tcp -nohttpd -localhost”


#service vncserver start
#service vncserver stop

#vi .vnc/xstartup

Uncomment the following two lines for normal desktop:
 
unset SESSION_MANAGER
exec /etc/X11/xinit/xinitrc


# service vncserver start

It done. Now connect to vnc server from vncviewer with <IP_Address>:1


Wednesday, July 25, 2012

How to Map (Mount) NFS Share in Windows

If you wanted to use Linux NFS Share from Windows system to mount as drive. You can do it using mount command for that you need to install NFS Client for windows.

Installing NFS Client :-

If you are windows7 , Vistra, or Windows 2008 user then you can install NFS Client from Add/Remove Software wizard in the Control Panel.

If you are Windows 2000, Windows 2000 Service Pack 3, Windows 2000 Service Pack 4, Windows Server 2003, Windows XP user then you can download client from following location.
http://www.microsoft.com/en-us/download/confirmation.aspx?id=274






And the select Finish

After installation check if NFS service is running or not from service MMC

Start -> Run -> Services.msc




If require service is running on your system you can now Map (Mount) NFS share from command line using mount command:

mount servername:\Share_Name Drive_Letter

For Example: your server name is FileServer and you have NFS export /home/share and you wanted map this drive as Z: in your My computer, then command look like this:



Start -> Run -> CMD -> OK -> Mount FileServer:\home\share Z:


Mount FileServer:\home\share Z:

It will map your NFS Share in your system and it ready to access as disk drive from you my computer.

Note:- If your NFS share required Authentication you can provide it with mount command. 


 

 



Thursday, February 9, 2012

How to connect Windows Remote Desktop from Linux Step by Step


Connecting Windows Remote Desktop from Linux

If you are running Centos (Linux system) and wanted to connect Remote windows system from CentOS then you have to install rdesktop Remote Desktop Client. Using rdesktop you can easily connect Windows system from Linux. rdesktop is an open source client for Microsoft's RDP protocol. It work with Windows NT 4 Terminal Server,2000, XP, 2003, 2003 R2, Vista, 2008, 7, and 2008 R2. And it support RDP Version 4 and 5.

Installing rdesktop:-

You can download package go to http://pkgs.org  as per your distribution. Or you can install rdesktop from repository :-

For CentOS/RedHat:-

# yum install rdesktop

For Ubuntu:-

# apt-get install rdesktop

After that you are ready to access Windows Remote Desktop from terminal with following command. For more help use man command (man rdesktop)

# rdesktop <Server Name OR IP Address>

Eg:-

# rdesktop 192.168.125

Wednesday, February 8, 2012

How to Install and Configure Proftpd Server on CentOS Step by Step


Installing Proftpd Server on CentOS (Linux)
 
Proftpd is the Highly configurable GPL-licensed FTP server software .it is an enhanced, secure and highly configurable FTP server. Its configuration syntax is very similar to apache web server. You can know more information from http://www.proftpd.org .

Download proftpd rpm package from http://pkgs.org/ and install with rpm –ivh <Package name>.rpm

OR

If you want to install using yum then add repository  and then install proftpd server

#  cd /etc/yum.repos.d/
# wget http://centos.karan.org/kbsingh-CentOS-Extras.repo
# rpm --import http://centos.karan.org/RPM-GPG-KEY-karan.org.txt
# Vi /etc/yum.repos.d/ kbsingh-CentOS-Extras.repo
Set both enable=1
# yum clean all
# yum update all

Now install Proftpd

# yum install proftpd

Now your ftp server is ready to functional  with default configuration but you should check following values for your Basic ftp server in /etc/proftpd.conf configuration file

Server Name, Server Type and Port

Now start proftpd and set on for startup

# service proftpd start
# chkconfig proftpd on

Check running process for proftpd
# ps -ef | grep proftpd
nobody    4463     1  0 23:35 ?        00:00:00 proftpd: (accepting connections)
root      4470  4202  0 23:36 pts/2    00:00:00 grep proftpd

OR you can verify whether your FTP server is running using:

# netstat -an | grep -F tcp | grep -F LISTEN | grep -F 21
tcp        0      0 :::21                       :::*                        LISTEN


Now when you try to connect ftp server it will be ask you for your name and password type valid user name and password as per /etc/passwd file . it will take you home directory of that user. 

Anonymous FTP Server :-

If you wanted Anonymous access uncomment Anonymous directive. Or you can add following in /etc/proftpd.conf file
# vi /etc/proftpd.conf

<Anonymous /var/ftp/pub>
AnonRequirePassword       off
User                                   ftp
Group                                 ftp
RequireValidShell                off
<Directory *>
<Limit WRITE>
          DenyAll
</Limit>
</Directory>
</Anonymous>


 Note: - user and group must be valid

verify configuration :-

# proftpd –t6

If it not given any error then reload configuration

# service proftpd reload

Now if you try to access FTP server it will not ask you any password and show you contain of /var/ftp/pub directory.
If you wanted to allow user to upload data then in Limit WRITE directive change it AllowAll.

Friday, December 9, 2011

Step by Step How to: Uninstalling Zabbix Agent from Windows system.

Step by Step How to: Uninstalling Zabbix Agent from Windows system.

Suppose you have installed Zabbix Agent in C:\Program Files\Zabbix with following method :-

Echo Server=192.168.73.142 > "c:\Program Files\zabbix\zabbix_agentd.conf"
Echo Hostname=%COMPUTERNAME% >> "c:\Program Files\zabbix\zabbix_agentd.conf"
"C:\Program Files\zabbix\zabbix_agentd.exe" -c "c:\Program Files\zabbix\zabbix_agentd.conf" –i
"C:\Program Files\zabbix\zabbix_agentd.exe" -c "c:\Program Files\zabbix\zabbix_agentd.conf" –s


And you want to uninstall it now to do fire the following command from command prompt:

Note:- It is always good option to backup our configuration file want to use later.

C:\Program Files\Zabbix>zabbix_agentd.exe -x -c"C:\Program Files\Zabbix\zabbix_agentd.Conf"
C:\Program Files\Zabbix>zabbix_agentd.exe -d -c"C:\Program Files\Zabbix\zabbix_agentd.Conf"
rmdir /s "C:\Program Files\Zabbix"


This will uninstall windows zabbix agnet from your system. 1st command stop zabbix agent service , 2nd uninstalled zabbix agent and last one do cleanup by deleting Zabbix directory with all constant from it. 

Tuesday, November 29, 2011

Windows Active directory integration with Nagios using LDAP and assign different permission to users in Nagios

Active directory integration with Nagios using LDAP and assign different permission to users in Nagios

To integrate Active directory for user Authentication for nagios front end we required to make changes in httpd.conf  and cgi.cfg file before that we required one normal user in active directory to access and bind with windows active directory.  ldap module for apache by default it installed and enabled.

Suppose:- 
  1. we have windows Active Directory domain domainname.com.
  2. we have created on normal user in active directory as nagios with password P@55w0rd and password set as never expire. 
  3. we want to give three different user sandeep , atul  and ramesh at Pune organization unit in active directory  with different levels of permission like full access to all host, services, command , limited access to only services and command,and read only access.

Integrate Active directory with ldap module in apache for nagios:-

check if ldap module in installed and enabled in apache by cat command

# cat /etc/httpd/conf/httpd.conf | grep ldap
LoadModule ldap_module modules/mod_ldap.so
LoadModule authnz_ldap_module modules/mod_authnz_ldap.so


(If you see result as above then in enabled. and if not see anything then may be ldap modue not installed in your system)

write the directive in httpd.conf file to active directory configuration information.

# vi /etc/httpd/conf/httpd.conf

<Directory />
    Options FollowSymLinks
    AllowOverride None
    AuthBasicProvider ldap
    AuthType Basic
    AuthzLDAPAuthoritative off
    AuthName "Active Directory Login"
    AuthLDAPURL "ldap://192.168.100.1:3268/dc=domainname,dc=com?sAMAccountName?sub" NONE
    AuthLDAPBindDN "nagios@domainname.com"
    AuthLDAPBindPassword "P@55w0rd"

    require ldap-user "CN=Atul Chaudhari,OU=Pune,DC=domainname,DC=com"
    require ldap-user "CN=Sandeep Kapadane,OU=Pune,DC=domainname,DC=com"
   require ldap-user "CN=Ramesh Jadhav,OU=Pune,DC=domainname,DC=com"

</Directory>

Now restart nagios and httpd conft for changes take effect.
# service nagios restart
# service httpd restart

Now try to access nagios in your browser when it ask for authentication give Active directory user credential that you just mention in httpd.conf file. it will allow you login into nagios but you don't have permission to see any host or service or configuration information. as shown in screenshot



for that you have to make some changes in cgi.cfg file for user permission.
(note that user name are case sensitive for permission for eg you have to use same case that are given in this file for authenticating other wise it not show you any host, services or command )
there are diffrent set of permission in cgi file as :-
  1. SYSTEM/PROCESS INFORMATION ACCESS.
  2. CONFIGURATION INFORMATION ACCESS.
  3. SYSTEM/PROCESS COMMAND ACCESS.
  4. GLOBAL HOST/SERVICE VIEW ACCESS.
  5. GLOBAL HOST/SERVICE COMMAND ACCESS.
  6. READ-ONLY USERS
just add user login name for permission do you want. 

Set Permission to users:-

# vi /etc/nagios/cgi.cfg

 # SYSTEM/PROCESS COMMAND ACCESS
authorized_for_system_commands=nagiosadmin,sandeepk
# GLOBAL HOST/SERVICE VIEW ACCESS
authorized_for_all_services=nagiosadmin,sandeepk,atulc,rameshj
authorized_for_all_hosts=nagiosadmin,sandeepk,atulc,rameshj
# GLOBAL HOST/SERVICE COMMAND ACCESS
authorized_for_all_service_commands=nagiosadmin,sandeepk,atulc
authorized_for_all_host_commands=nagiosadmin,sandeepk
# READ-ONLY USERS
authorized_for_read_only=rameshj


Now restart nagios and httpd conft for changes take effect.
# service nagios restart
# service httpd restart

Now as per cgi configuration file different user get different set for permission in nagios front end. 

Wednesday, October 19, 2011

Adding Windows host in to Nagios Monitoring System

Monitoring windows host from Nagios monitoring system

Last time we have seen how to install nagios. Today we will see how to add Windows system in to nagios for monitoring.

suppose I wanted to monitor following  network in to nagios :-
Suppose :-
Server-PT is mail server and IP address is 192.168.1.2
Router1 has ip address eth0 192.168.1.1 and for eth1 192.168.2.1
Router0 has ip address eth1 192.168.2.2 and for eth0 192.168.3.1
and our other server are in this network like Nagios server, File server, Domain Controller and access point.
Nagios has ip address 192.168.3.2
File_server has ip address 192.168.3.3
AD_server is working as DNS, DHCP and Domain Controller server and it has ip address 192.168.3.4
Access_point has ip address 192.168.3.5

Now login in to Nagios and create object configuration file as:-

# touch /etc/nagios/hosts.cfg   (To define host)
# touch /etc/nagios/services.cfg (To define Services)
# touch /etc/nagios/hostgroups.cfg (To define hostgroups)
# touch /etc/nagios/contacts.cfg (To define Contacts details)





Give sufficient right to nagios to read file 
# chown -r nagios.nagios /etc/nagios/*

Now specify all these file and directory in nagios main configuration file.
# vi /etc/nagios/nagios.cfg
cfg_file=/etc/nagios/hosts.cfg
cfg_file=/etc/nagios/services.cfg
cfg_file=/etc/nagios/hostgroups.cfg
cfg_file=/etc/nagios/contacts.cfg

Now configure object configuration file:-

# vi /etc/nagios/contacts.cfg
 # Define contact details
define contact{
contact_name              sandeep
use                                generic-contact
alias                             Sandeep Kapadane
email                            sandeepk@domainname.com
        }

define contact{
contact_name              help
use                               generic-contact
alias                             IT Support help
email                            help@domainname.com
        }

define contact{
contact_name              manager
use                               generic-contact
alias                             Team Manager
email                            manager@domainname.com
        }

# Define contact groups

define contactgroup{
contactgroup_name     It-Team
alias                             IT Team
members                      sandeepk,help,manager
        }
# vi /etc/nagios/hosts.cfg
# Create template for host as generic-server and core-server

define host{
 name                           generic-server
notifications_enabled              1
event_handler_enabled           1
flap_detection_enabled           1
failure_prediction_enabled      1
process_perf_data                  1
retain_status_information         1
retain_nonstatus_information    1
notification_period                   24x7
register                                    0
        }

define host{
 name                           core-server
use                               generic-server
check_period               24x7
check_interval              5
retry_interval               1
max_check_attempts   10
check_command         check-host-alive
notification_interval      60
notification_options      d,u,r
contact_groups            admins
register                        0
        }

# Define host

define host{
 use                              core-server
host_name                   Router0
alias                             Router0
address                       192.168.3.1
}

define host{
 use                              core-server
host_name                   Router1
alias                             Router1
address                       192.168.1.1
parents                         Router0
}

define host{
 use                              core-server
host_name                   Server-PT
alias                             Mail Server
address                       192.168.1.2
parents                         Router1
}

define host{
 use                              core-server
host_name                   File_server
alias                             File Server
address                       192.168.3.3   
}

define host{
use                               core-server
host_name                   AD_server
alias                             Domain Controller
address                       192.168.3.4
}

define host{
use                               core-server
host_name                   Access_point
alias                             Wireless Access point
address                        192.168.3.5
}

Now define Host Groups in object configuation file

# vi /etc/nagios/hostgroups.cfg
define hostgroup{
hostgroup_name          all_hosts
alias                             All Hosts
members                      Router0, Router1, Server-PT, File_server, AD_server, Access_point
        }

define hostgroup{
hostgroup_name          Domain Controllers
alias                             Domain Controllers
members                      AD_server
}

define hostgroup{
hostgroup_name          CoreNetwork
alias                             CoreNetwork
members                      Router0, Router1,Access_point
}

define hostgroup{
hostgroup_name          CoreServer
alias                             CoreServer
members                      Server-PT, File_server, AD_server
}

Now Define Services to monitor in object configuration file services.cfg
# vi /etc/nagios/services.cfg
  # Create Template for all services.

define service{
name                                         services
active_checks_enabled               1
passive_checks_enabled             1
parallelize_check                        1
obsess_over_service                  1
check_freshness                         0
notifications_enabled                  1
event_handler_enabled               1
flap_detection_enabled               1
failure_prediction_enabled          1
process_perf_data                     1
retain_status_information            1
retain_nonstatus_information       1
is_volatile                                   0
check_period                             24x7
max_check_attempts                 3
normal_check_interval               10
retry_check_interval                  2
contact_groups                          admins
notification_options                   w,u,c,r
# notification_interval                60
notification_period                    24x7
register                                     0
        }
# create template for Ping service

define service{
use                               services
name                            ping
check_command          check_ping!1000.0,20%!2000.0,60%
register                         0
        }
# Add host for ping
define service{
use                               ping
service_description       PING
hostgroup_name           all_hosts
        }
# Monitor UP Time

define service{
use                               services
name                            uptime
check_command          check_nt!UPTIME
register                         0
        }

define service{
use                               uptime
service_description     UP TIME
host_name                   Server-PT, File_server, AD_server
hostgroup_name               
}
define service{
use                               services
name                            Uptime
check_command         check_snmp!-C public -o sysUpTime.
register                                    0
        }

define service{
use                               Uptime
service_description     UP TIME
# host_name                      
           hostgroup_name             CoreNetwork
}


# Monitoring Disk Space 

define service{
        use                               services
        name                            Disk space on c
        check_command          check_nt!USEDDISKSPACE!-l c -w 80 -c 90
        register                        0
        }
define service{
          use                             Disk space on c
          service_description     C: Drive Space
         host_name                  Server-PT, File_server, AD_server
#        hostgroup_name         Domain Controllers
}
define service{
        use                               services
        name                            Disk space on d
        check_command          check_nt!USEDDISKSPACE!-l d -w 80 -c 90
        register                         0
        }
define service{
          use                             Disk space on d
          service_description     D: Drive Space
         host_name                  Server-PT, File_server
#        hostgroup_name         Domain Controllers
}

# Monitoring CPU Load 

define service{
        use                             services
        name                          CPU Load
        check_command        check_nt!CLIENTVERSION
        register                        0
        }
define service{
        use                                    CPU Load
        service_description            CPU Load
        hostgroup_name                Domain Controllers
       host_name                          Server-PT
        }

# Monitoring Memory Usage 
define service{
        use                             services
        name                          Memory Usage
        check_command        check_nt!MEMUSE!-w 80 -c 90
        register                        0
        }
define service{
        use                                     Memory Usage
        service_description             Memory Usage
        hostgroup_name                  Domain Controllers
       host_name                            Server-PT
        }
# Monitoring System Services

define service{
        use                             services
        name                          Server Service
        check_command        check_nt!SERVICESTATE!-d SHOWALL -l lanmanServer
        register                        0
        }
define service{
        use                             Server Service
        service_description     Server Service
#       hostgroup_name         Domain Controllers
       host_name                   Server-PT, File_server, AD_server
        }

define service{
        use                             services
        name                          DNS Server Service
        check_command        check_nt!SERVICESTATE!-d SHOWALL -l Dns
        register                        0
        }
define service{
        use                                    DNS Server Service
        service_description             DNS Server Service
        hostgroup_name                  Domain Controllers
#       host_name
        }

define service{
        use                             services
        name                          DHCP Server Service
        check_command        check_nt!SERVICESTATE!-d SHOWALL -l DhcpServer
        register                       0
        }
define service{
        use                             DHCP Server Service
        service_description     DHCP Server Service
        hostgroup_name         Domain Controllers
#       host_name
       }

define service{
        use                             services
        name                          Net Logon service
        check_command        check_nt!SERVICESTATE!-d SHOWALL -l Netlogon
        register                       0
        }
define service{
        use                             Net Logon service
        service_description     Net Logon service
        hostgroup_name         Domain Controllers
#       host_name
        }

# Monitoring NSClient Version 


define service{
        use                               services
        name                            NSClinet Version
        check_command           check_nt!CLIENTVERSION
        register                        0
        }
define service{
        use                                NSClinet Version
        service_description        NSClient++ Version
#      hostgroup_name            Domain Controllers
        host_name                    Server-PT, File_server, AD_server
        }

# Next time we will see how to configure nagios for host and service escalation and dependency