Linux Unix help !!

"Give respect to Time, One day at right Time, Time will respect You"

Wednesday, March 23, 2011

Script to take MBR backup

xxxxxxxxxxxx BASH To Take MBR BAckup -- Shirish Shukla xxxxxxxxxxxxx

#!/bin/bash
hdds=$(echo /dev/[hs]d?)

for hdd in $hdds
do
      ## take only last name of dir
        base=$(basename $hdd)
      ##take MBR Backup 0-512 bytes
        dd if=$hdd of=/tmp/$base.mbr count=1 bs=512
 echo $hdd
done


Wednesday, March 16, 2011

Install Configure Nagios

xxxxxxxxxx Install & Manage NAGIOS Monitoring tool by Shirish Shukla xxxxxxxxxxxxx

Nagios® is a system and network monitoring application. (Description from some source)
Some of the many features of Nagios® include:
- Monitoring of network services (SMTP, POP3, HTTP, NNTP, PING, etc.)
- Monitoring of host resources (processor load, disk usage, etc.)
- Simple plugin design that allows users to easily develop their own service checks
- Parallelized service checks
- Ability to define network host hierarchy using "parent" hosts, allowing detection of and     distinction between hosts that are down and those that are unreachable
- Contact notifications when service or host problems occur and get resolved (via email, pager,   or user-defined method)
- Ability to define event handlers to be run during service or host events for proactive          problem resolution
- Automatic log file rotation
- Support for implementing redundant monitoring hosts
- Optional web interface for viewing current network status, notification and problem history,   log file, etc.

PREREQUISITE:
=============
Apache (https)
gcc libraries

Nagios Tols:
============
# mkdir /root/SW
wget http://prdownloads.sourceforge.net/sourceforge/nagios/nagios-3.2.3.tar.gz
wget http://prdownloads.sourceforge.net/sourceforge/nagiosplug/nagios-plugins-1.4.11.tar.gz

##Adduser and groups

# useradd -m nagios 
# passwd nagios

# groupadd nagios              <--Just for confirmation
# usermod -G nagios nagios     <--Just for confirmation

# groupadd nagcmd
# usermod -a -G nagcmd nagios
# usermod -a -G nagcmd wwwrun  <--Not Require(optional) need to create group wwwrun before

INSTALLATION:
=============
# cd /root/SW
# tar xzf nagios-3.2.3.tar.gz
# cd nagios-3.2.3

# ./configure --with-command-group=nagcmd
# make all
# make install
# make install-init
# make install-config
# make install-commandmode

# vi /usr/local/nagios/etc/objects/contacts.cfg
At line
35: Change --> email           youadmin-mail-id

# make install-webconf
# htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin
Note: if you want to change admin "nagiosadmin" name  chanhe all in  "/usr/local/nagios/etc/cgi.cfg" file too .

##Start httpd(Apache)
# /etc/init.d/httpd restart
# chkconfig httpd on

### Instal nagios-plugin
# tar xzf nagios-plugins-1.4.11.tar.gz
# cd nagios-plugins-1.4.11
# ./configure --with-nagios-user=nagios --with-nagios-group=nagios
# make
# make install

#### Start nagios
# /etc/init.d/nagios restart
# chkconfig nagios on

###### How to OPERATE

# Webadmin panel
http://your-ip/nagios/  < Provide above seted id & password---

EG: To Check All Hosts without options You can set it to your default apache hosting file for your monitoring purpose
http://192.168.8.10/nagios/cgi-bin/status.cgi?host=all

### Want default as above to your ip
# vi /var/www/html/index.html
<html>
<head>
<META HTTP-EQUIV="Refresh"
CONTENT="2; URL=http://192.168.8.10/nagios/cgi-bin/status.cgi?host=all">
</head>
<body bgcolor="pink">
<br />
<br />
<br />
<br />
<br />
<br />
<Center>
<h1> <font color="green"> Wait 2 Sec.............</font></h1>
</body>
</html>

##Save and relaod httpd

Now just type your ip on browser and <---Enter

####################################################
#################Add your servers to monitor ############
###################################################
# cd /usr/local/nagios    <-- Nagios default files

# cd /usr/local/nagios/etc/objects <<-- All cfg file server's config file to monitor

# cat /usr/local/nagios/etc/nagios.cfg  <<<-- NAgios main config file

# vi /usr/local/nagios/etc/nagios.cfg  <<<--Add your own config dirs where your all server cfg file will reside

==>Add below line

  cfg_dir=/usr/local/nagios/etc/lnx-servers

# cd usr/local/nagios/etc/lnx-servers

# vi my-samba_server


##############################################################
############# HOST DEFINITION ###############################
##############################################################
 # HOST DEFINITION
   define host{
        use                     linux-server            ; defined in template
        host_name               scratch-samba.8.10
        alias                   samba-server
        address                 192.168.8.10
        }

# HOST GROUP DEFINITION (Optional, you can add multiple members to your already created group seperated by comma)
define hostgroup{
        hostgroup_name  my-servers             ; The name of the hostgroup
        alias           MY Linux Servers         ; Long name of the group
        members         scratch-samba.8.10      ; Comma separated list of hosts that belong to this group
       }

##############################################################
####################### SERVICE DEFINITIONS #################
##############################################################

###Ping check
define service{
        use                             local-service                 ; defined in template /usr/local/nagios/etc/objects/templates.cfg
        host_name                       scratch-samba.8.10
        service_description             PING
        check_command                   check_ping!100.0,20%!500.0,60% ; Defined in commnd file /usr/local/nagios/etc/objects/commands.cfg
        }


### SSH service Check
define service{
        use                             local-service        
        host_name                       scratch-samba.8.10
        service_description             SSH
        check_command                   check_ssh!-t 5 -r "OpenSSH_4.2"
        }

define service{
        use                             local-service         ; Name of service template to use
        host_name                       scratch-samba.8.10
        service_description             samba
        check_command                   check_http
        }

define service{

        use                     local-service           ; Inherit default values from a template
        host_name               scratch-samba.8.10
        service_description     SMTP
        check_command           check_smtp
        }

#check port 139
define service{
        use                     local-service           ; Inherit default values from a template
        host_name               scratch-samba.8.10
        service_description     Samba_port139
        check_command           check_tcp!139
        }

##########################################################################


## Nagios Fine tunning file (Do not replace original line comment that then add below line)
# vi /usr/local/nagios/etc/cgi.cfg

### Don't show pending satate of service
use_pending_states=0

### Disable webadmin auth
use_authentication=0

### Want  to authorize an user shukla to have all admin rights for weadmin panel
authorized_for_system_information=shukla
authorized_for_configuration_information=shukla
authorized_for_system_commands=shukla
authorized_for_all_hosts=shukla
authorized_for_all_services=shukla
authorized_for_all_service_commands=shukla
authorized_for_all_host_commands=shukla


### Want default guest user, WARN: dont' allow this
default_user_name=guest

### Want an read only user
#htpasswd -c /usr/local/nagios/etc/htpasswd.users user1
#using command add more users
authorized_for_read_only=user1

### Want to change ping syntax
ping_syntax=/bin/ping -n -U -c 5 $HOSTADDRESS$

### Refresh rate of cgi (status, statusmap, extinfo, and outages) in sec
refresh_rate=60

### Want to play sound on webpanel if error occour on network
## Plcace your audio file at
# /usr/local/nagios/share/media
##Un commen tbelow lines after adding resp audio files
#host_unreachable_sound=hostdown.wav
#host_down_sound=hostdown.wav
#service_critical_sound=critical.wav
#service_warning_sound=warning.wav
#service_unknown_sound=warning.wav
#normal_sound=noproblem.wav

### dont allow to change author name while submitting comment
lock_author_names=0


# vi /usr/local/nagios/etc/nagios.cfg

### Change log path
log_file=/usr/local/nagios/var/nagios.log

## Add your specific cfg file of server to monitor
cfg_file=/usr/local/nagios/etc/objects/myserver.cfg

## Add you dir contain your server cdf file to monitor all
cfg_dir=/usr/local/nagios/etc/my-lnx-servers

## Host update interval in sec
status_update_interval=5

## Change nagios user & Group (NOT Recomended)
nagios_user=nagios
nagios_group=nagios

## Add you own command file
check_external_commands=0
command_check_interval=-1
command_file=/usr/local/nagios/var/rw/nagios.cmd <<--As you want
external_command_buffer_slots=4096

## Nagios store his pid at
lock_file=/usr/local/nagios/var/nagios.lock


##### agios Log rotation
#       n       = None - don't rotate the log
#       h       = Hourly rotation (top of the hour)
#       d       = Daily rotation (midnight every day)
#       w       = Weekly rotation (midnight on Saturday evening)
#       m       = Monthly rotation (midnight last day of month)

log_rotation_method=d
log_archive_path=/usr/local/nagios/var/archives

##Use syslog to do so
use_syslog=1

##### MAXIMUM SERVICE CHECK SPREAD in munute
max_service_check_spread=30

### HOST FRESHNESS CHECK OPTION (Nagios chek periodically freshmess of hosts)
check_host_freshness=1
host_freshness_check_interval=20

### Change date format
#       us              (MM-DD-YYYY HH:MM:SS)
#       euro            (DD-MM-YYYY HH:MM:SS)
#       iso8601         (YYYY-MM-DD HH:MM:SS)
#       strict-iso8601  (YYYY-MM-DDTHH:MM:SS)
date_format=us

##Enable perl support
##not in use if u have not compliled nagios with perl
p1_file=/usr/local/nagios/bin/p1.pl
###disable =0
enable_embedded_perl=1

# ADMINISTRATOR EMAIL/PAGER ADDRESSES
admin_email=nagios@localhost

# DEBUG LEVEL
#          -1 = Everything
#          0 = Nothing
#          1 = Functions
#          2 = Configuration
#          4 = Process information
#          8 = Scheduled events
#          16 = Host/service checks
#          32 = Notifications
#          64 = Event broker
#          128 = External commands
#          256 = Commands
#          512 = Scheduled downtime
#          1024 = Comments

debug_level=0

# DEBUG VERBOSITY
# Values: 0 = Brief output
#         1 = More detailed
#         2 = Very detailed

debug_verbosity=1

# DEBUG FILE
debug_file=/usr/local/nagios/var/nagios.debug


############################ BASIC Checking #############################

### Check Your Config files for error

# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
# /etc/init.d/nagios start



########## Tunnning

1> Want any new updates to be check every minute (Default 5 minute)
# vi /usr/local/nagios/etc/objects/templates.cfg

check_interval      1 ; Actively check the host every 1 minutes

2> I want to check default ping of my server
check_command                   check-ping

3> I want to use all config waht i defined in my template linux-server to windows-server
Add below line in windows.cfg
use  linux-server


xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

# Faced any problem Feedback in above contact me
#===============================Scratch===============================#
# AND Many More .....................Linux is Endless                 #
#========================== Hope you Liked IT ========================#
#                                         NAGIOS -- BY Shirish Shukla #
#                                                   RHC Engineer 2010 #
#                                             shirish.linux@gmail.com #
#                                           shirishlinux.blogspot.com #
# "Give Respect To Time One Day At Right Time, Time Will Respect You" #
#=====================================================================#
# TRy Hard theres nothing that are un-achievable by HARDdd-WORKkk     #
#=====================================================================#
Nagios, Nagios linux , install nagios, config nagios linux, nagios refresh, nagios configuration linux, how to install nagios linux, basic nagios; shirish, shukla, linux engineer, linux, alert, monitoring tool linux

mysql master -slave Replication

..........mysql master slave replication configuration on LINUX.......- Shirish Shukla

Some Basics:
Port No: 3306 (TCP/UDP)

IPADDRESS_Set as per your requirement

SYATEM 1 >>  master   : 192.168.8.10
SYATEM 2 >>  Slave    : 192.168.8.20

1> Installation on both server

# yum install mysql*

# /etc/init.d/mysqld restart
# /etc/init.d/mysqld restart

                                                      ### Set root password
# mysqladmin -u root password x                       ### replace x is ur password

Login in mysql:

# mysql -u root -px                                   ### replace x is as ur password aviod direct
                                                      ### passing of password for security reason
# mysql -u root -p       <Enter>
Enter password:     <Enter Password>
mysql> exit

2> ON MASTER--192.168.8.10

# cp /etc/my.cfg  /etc/my.cfg_Org                      ### Keep a copy
# vi  /etc/my.cnf

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
###Default to using old password format for compatibility with mysql 3.x
### clients (those using the mysqlclient10 compatibility package).
old_passwords=1

server-id=1
binlog-do-db=SHUKLA               ### databases which you want to replicate
binlog-do-db=SHIRISH                # create these databases before SEE
                                    # at END BASIC MYSQL COMMANDS
                                    # use, to add in 1 line or add seperately
binlog-ignore-db=mysql            ### Input the database that should be ignored
binlog-ignore-db=test               # for replication

log-bin=/var/lib/mysql/mysqld-bin

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
relay-log=/var/log/mysql-relay-bin

# mysql -u root -p 

mysql>  grant replication slave on *.* to replication@192.168.8.20 identified by 'slave';
mysql> GRANT ALL PRIVILEGES ON *.* TO replication;

Cross check user replication
======================
mysql> use mysql;
mysql> select * from user where User='replication';        ### must show 'Y' in most fields specially in "Super_priv"

####  Now Craete some database and tables

mysql> CREATE DATABASE SHIRISH;
mysql> use SHIRISH;
mysql> create table members(name char(20), age varchar(20));
mysql> show tables;
mysql> insert into members(name, age) values ('Shirish Shukla', 23);
mysql> insert into members(name, age) values ('Shirish Shukla-2', 23);
mysql> insert into members(name, age) values ('Shirish Shukla-3', 23);
mysql> select * from members;
+------------------+------+
| name             | age  |
+------------------+------+
| Shirish Shukla | 23   |
| Shirish Shukla-2 | 23   |
| Shirish Shukla-3 | 23   |
+------------------+------+
3 rows in set (0.00 sec)

mysql> exit

3> ON SLAVE--192.168.8.20
# cp /etc/my.cfg  /etc/my.cfg_Org               ### Keep a copy
# vi  /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Default to using old password format for compatibility with mysql 3.x
# clients (those using the mysqlclient10 compatibility package).
old_passwords=1

server-id=2
master-host = 192.168.8.10
master-user = replication
master-password = slave
master-port = 3306
log-bin=/var/lib/mysql/mysqld-bin

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
relay-log=/var/log/mysql-relay-bin

# mysql -u root -p

mysql> stop slave;
mysql> CHANGE MASTER TO MASTER_HOST='192.168.8.10',MASTER_USER='replication',MASTER_PASSWORD='slave';
mysql> GRANT ALL PRIVILEGES on *.* TO replication                                                              

Cross check user replication
======================
mysql> use mysql;
mysql> select * from user where User='replication';        ### must show 'Y' in most fields specially in "Super_priv"

mysql> start slave;
mysql> show slave status\G;
*************************** 1. row ***************************
             Slave_IO_State: Waiting for master to send event            <1>                             
                Master_Host: 192.168.8.10                                 <2>
                Master_User: replication                                  <3>
                Master_Port: 3306                                         <4>
              Connect_Retry: 60
            Master_Log_File: mysqld-bin.000008                            <5>           
        Read_Master_Log_Pos: 223                                          <6>
             Relay_Log_File: mysqld-relay-bin.000002
              Relay_Log_Pos: 361
      Relay_Master_Log_File: mysqld-bin.000008            
           Slave_IO_Running: Yes                                          <7>
          Slave_SQL_Running: Yes                                          <8>
            Replicate_Do_DB:
        Replicate_Ignore_DB:
         Replicate_Do_Table:
     Replicate_Ignore_Table:
    Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
                 Last_Errno: 0
                 Last_Error:
               Skip_Counter: 0
        Exec_Master_Log_Pos: 223                                          <9>
            Relay_Log_Space: 361
            Until_Condition: None
             Until_Log_File:
              Until_Log_Pos: 0
         Master_SSL_Allowed: No
         Master_SSL_CA_File:
         Master_SSL_CA_Path:
            Master_SSL_Cert:
          Master_SSL_Cipher:
             Master_SSL_Key:
      Seconds_Behind_Master: 0                                           <10>
1 row in set (0.01 sec)

ERROR:
No query specified


#### CROSS check some things in above;
<1>Always "Waiting for master to send event" if unable to connect then report or restrat
<2>Muyst show master IP
<3>Show user as set above
<4>PORT 3306
<5>must match with  below (show master status;) FILE
<6>must match with  below (show master status;) Position
        On master RUN
        mysql> show master status;
+-------------------+-----------+-------------------------------+------------------------+
| File              | Position  | Binlog_Do_DB                  | Binlog_Ignore_DB      |
+-------------------+-----------+-------------------------------+------------------------+
| mysqld-bin.000008 |      223  | SHUKLA,SHIRISH,SHUKLA,SHIRISH | mysql,test,mysql,test |
+-------------------+-----------+-------------------------------+------------------------+

<7>Must show Yes
<8>Must show Yes
<9> SAME As <6>
<10>Must show limited sec diff < 2 or 0 sec but not 'NULL'


3> TESTING MASTER SLAVE
#### Now cross check is replacition done properly or not !!!

mysql> show databases;
mysql> use SHIRISH;  
mysql> show tables;        <--- Compare with master
mysql> select * from members;  <--- Compare with master

### Now create a new table as LINUX at MASTER;
      
mysql> SHOW TABLES;
mysql> create table linux(name char(20), age varchar(20));
mysql> insert into linux(name, age) values ('Shirish Shukla', 23);
mysql> SHOW TABLES;
+--------------------------+
| Tables_in_SHIRISH       |
+--------------------------+
| linux                   |
| members                 |
+--------------------------+
2 rows in set (0.00 sec)

## now at slave

mysql> use SHIRISH;  
mysql> show tables;
mysql> select * from linux;    << MUST SHOW SAME AS MASTER JUST UPDATED>>


Some Tuning.....add below in /etc/my.conf of both master and slave........



==========================================================================
||                    MASTER SLAVE <--> MASTER SLAVE                    ||
==========================================================================

Continue above configuration and add BELOW....

                     192.168.8.10      192.168.8.20
                       SYSTEM-1          SYSTEM-2             
Now Above AS:          Master  <------>  SLAVE
                       SLAVE   <------>  MASTER2

ON SYSTEM-1
# vi /etc/my.cnf    <<--Add below lines

master-host = 192.168.8.20
master-user = replication
master-password = slave
master-port = 3306

# /etc/init.d/mysqld restart

# mysql -u root -px
mysql> stop slave;
mysql> grant replication slave on *.* TO ayam@'192.168.8.10' identified by 'x';
mysql> grant replication slave on *.* TO ayam@'192.168.8.20' identified by 'x';
mysql> GRANT ALL PRIVILEGES ON *.* TO replication@192.168.8.10;
mysql> GRANT ALL PRIVILEGES ON *.* TO replication@192.168.8.20;
mysql> GRANT ALL PRIVILEGES ON *.* TO replication;
mysql> CHANGE MASTER TO MASTER_HOST='192.168.8.20',MASTER_USER='replication',MASTER_PASSWORD='slave';
mysql> start slave;


ON SYSTEM-2
# vi /etc/my.cnf    <<--confirm below line we had already added in Part -1

## Add below lines
binlog-do-db=SHUKLA               #Databases that you want to replicate
binlog-do-db=SHIRISH
binlog-ignore-db=mysql            # input the database that should be ignored for replication
binlog-ignore-db=test

## Cross varify below to exist we had already added
master-host = 192.168.8.10
master-user = replication
master-password = slave
master-port = 3306

# mysql -u root -px
mysql> stop slave;
mysql> grant replication slave on *.* TO ayam@'192.168.8.10' identified by 'x';
mysql> grant replication slave on *.* TO ayam@'192.168.8.20' identified by 'x';
mysql> GRANT ALL PRIVILEGES ON *.* TO replication@192.168.8.10;
mysql> GRANT ALL PRIVILEGES ON *.* TO replication@192.168.8.20;
mysql> GRANT ALL PRIVILEGES ON *.* TO replication;
mysql> CHANGE MASTER TO MASTER_HOST='192.168.8.10',MASTER_USER='replication',MASTER_PASSWORD='slave';
mysql> start slave;

ON SYSTEM-1
mysql> show slave status\G;  < and cross varifie with as we done in PART-1

ON SYSTEM-2
mysql> show slave status\G;  < and cross varifie with as we done in PART-1

CROSS Checking:


###################################### BASIC MYSQL COMMANDS ###############################
##Create database SHIRISH
mysql> create database SHIRISH;
##Create Tables into database SHIRISH
mysql> use SHIRISH;
mysql> create table LINUX_USERS(name char(20), age varchar(20));
##Display created table;
mysql> show tables;
##Insert data into table LINUX_USERS
mysql> insert into LINUX_USERS(name,age) values ('Shirish Shukla', 23)
##display created entry into table LINUX_USERS
mysql> select * from shukla;

# Faced any problem Feedback in above contact me
#===============================Scratch===============================#
# AND Many More .....................Linux is Endless                 #
#========================== Hope you Liked IT ========================#
#                                          MYSQL -- BY Shirish Shukla #
#                                                   RHC Engineer 2010 #
#                                             shirish.linux@gmail.com #
#                                           shirishlinux.blogspot.com #
# "Give Respect To Time One Day At Right Time, Time Will Respect You" #
#=====================================================================#
# TRy Hard theres nothing that are un-achievable by HARDdd-WORKkk     #
#=====================================================================#
mysql, mysql installation linux, mysql installation , mysql replication, master slave, mysql master slave replication, mysql master master replication, replication

Followers

Pls LIKE my Story !!!