Wednesday, September 28, 2011

My New Writeup.

I am not good writer, still i am writing things which is need to me and people who find needful for them. 

This is my another Tech Blog. I hope its useful for all of us.

Please visit: http://linux-fundamentals.blogspot.com/


Thanks-

Tuesday, September 27, 2011

Shell Script To Monitor Mysql.

[root@ 17_johnson]# cat /usr/bin/monitor-mysql
#!/bin/bash

# mysql root/admin username
MUSER="root"
# mysql admin/root password
MPASS="im#secure"
# mysql server hostname
MHOST="localhost"
#Shell script to start MySQL server i.e. path to MySQL daemon start/stop script.
# Debain uses following script, need to setup this according to your UNIX/Linux/BSD OS.
MSTART="/etc/init.d/mysqld start"
# Email ID to send notification
EMAILID="shrikant.lokhande@example.com, lokhande.shrikant@gmail.com"
# path to mail program
MAILCMD="$(which mail)"
# path mysqladmin
MADMIN="$(which mysqladmin)"

#### DO NOT CHANGE anything BELOW ####
MAILMESSAGE="/tmp/mysql.fail.$$"

# see if MySQL server is alive or not
# 2&1 could be better but i would like to keep it simple and easy to
# understand stuff :)
$MADMIN -h $MHOST -u $MUSER -p${MPASS} ping 2>/dev/null 1>/dev/null
if [ $? -ne 0 ]; then
        echo "" >$MAILMESSAGE
        echo "Error: MySQL Server is not running/responding ping request">>$MAILMESSAGE
        echo "Hostname: $(hostname)" >>$MAILMESSAGE
        echo "Date & Time: $(date)" >>$MAILMESSAGE
        # try to start mysql
        $MSTART>/dev/null
        # see if it is started or not
        o=$(ps cax | grep -c ' mysqld$')
        if [ $o -eq 1 ]; then
                sMess="MySQL Server MySQL server successfully restarted"
        else
                sMess="MySQL server FAILED to restart"
        fi
        # Email status too
        echo "Current Status: $sMess" >>$MAILMESSAGE
        echo "" >>$MAILMESSAGE
        echo "*** This email generated by $(basename $0) shell script ***" >>$MAILMESSAGE
        echo "*** Please don't reply this email, this is just notification email ***" >>$MAILMESSAGE
        # send email
        $MAILCMD -s "MySQL server" $EMAILID < $MAILMESSAGE
else # MySQL is running :) and do nothing
        :
fi
# remove file
rm -f $MAILMESSAGE

Saturday, September 24, 2011

Script To Check Disk Usage and Send Alert URGENT/WARNING.

[root@ 17_johnson]# cat /usr/bin/DiskUsage

#!/bin/bash


#filesystems="/dev/sda1 /dev/sda2 /dev/sda5"
filesystems="/data /log  /app  /backup  /home  /"

for fs in $filesystems
do

size=`df -k  $fs |grep $fs |awk '{ print  $3 }'`

        if [  $size  -le  2500000 ]  ;then
#                mail -b "URGENT: Low disk space for $fs ($size)"
 echo  "$fs ($size) URGENT" | mail -s "Alert: Disk Usage for Ishy  $fs ($size) " shrikant.lokhande@example.com
               echo "URGENT"
                break
        fi
        if [ $size -le  5000000 ] ;then
#                 mail -b "WARNING: Low disk space for $fs ($size)
   echo  "$fs ($size) WARNING" | mail -s "Alert: Disk Usage for Ishy(LOW)  $fs ($size) " shrikant.lokhande@example.com
               echo "WRANING"
fi
done

Friday, September 23, 2011

Shell script to taking MySql Dump/Backup.

[root@17_johnson]# cat /usr/bin/mybackupsql

#!/bin/bash
# USER VARIABLES
TIMESTAMP=$(date +%Y-%m-%d)
MYSQLUSER=root
MYSQLPWD=im#secure
MYSQLHOST=localhost
# PATH VARIABLES
MK=/bin/mkdir
GREP=/bin/grep
MYSQL=/usr/bin/mysql
MYSQLDUMP=/usr/bin/mysqldump
# CREATE MYSQL BACKUP
# Create new backup dir
$MK /backup/mysqlback_$TIMESTAMP
#Dump new files
for i in $(echo 'SHOW DATABASES;' | $MYSQL -u$MYSQLUSER -p$MYSQLPWD -h$MYSQLHOST|$GREP -v '^Database$'); do
  $MYSQLDUMP -u$MYSQLUSER -p$MYSQLPWD -h$MYSQLHOST $i >/backup/mysqlback_$TIMESTAMP/$i.sql
sleep 30
echo "$i"
done

Thursday, September 22, 2011

MysqlDump with Exclude database or perticuler table Options.

[root@linux_johnson]# cat /usr/bin/imdbbackup
#!/bin/bash
# USER VARIABLES
TIMESTAMP=$(date +%Y-%m-%d)
MYSQLUSER=root
MYSQLPWD=im#secure
MYSQLHOST=localhost
# PATH VARIABLES
MK=/bin/mkdir
GREP=/bin/grep
MYSQL=/usr/bin/mysql
MYSQLDUMP=/usr/bin/mysqldump
# CREATE MYSQL BACKUP
# Create new backup dir
$MK /backup/mysqlback_$TIMESTAMP
#Dump new files
DBname=db_production


for i in $(echo 'SHOW DATABASES;' | $MYSQL -u$MYSQLUSER -p$MYSQLPWD -h$MYSQLHOST|$GREP -v '^Database$'); do
if [ "$i" == "$DBname" ]; then
             echo " Dump taken"
  mysqldump  -u$MYSQLUSER -p$MYSQLPWD $DBname --ignore-table=db_production.clicks  --ignore-table=db_production.addresses  > /backup/mysqlback_$TIMESTAMP/$i.sql
else
  $MYSQLDUMP -u$MYSQLUSER -p$MYSQLPWD -h$MYSQLHOST $i >/backup/mysqlback_$TIMESTAMP/$i.sql
echo "$i"
fi

done

/etc/init.d/httpd reload




Wednesday, September 21, 2011

How To Clear Active record base Sessions.

[root@17_johnson]# cat current/lib/clear_sessions_data.rb

  require 'rubygems'
  require 'active_record'


  ActiveRecord::Base.establish_connection(
  :adapter => "mysql",
  :host => "localhost",
  :username => "root",
  :password =>"tm#secure",
  :database => "tm_production",
  :socket =>  "/var/lib/mysql/mysql.sock"
  )

 
  sql = 'DELETE FROM sessions WHERE updated_at < DATE_SUB(NOW(), INTERVAL 1 DAY);'
  ActiveRecord::Base.connection.execute(sql)

------------------
NOTE # Set script As cron at Every day 12.00 AM.

Tuesday, September 20, 2011

To check exact PID of running process in linux.

To check exact PID of process.
Command:
ps -efa  |  grep httpd | grep -v grep | awk '{print $2}'

-----------------------------------------------------------------------------------------
#! /bin/bash

PROCNAME=$1
PIDS=`ps -efa | grep $PROCNAME | grep -v grep | awk '{ print $2 }'`
for ff in $PIDS
do
echo "$ff"
done
------------------------------------------------------------------------------------------



do chmod 777 filename

Now  you can run the command like:
e.g.
filename httpd  or filename  processname.

Tuesday, September 13, 2011

Last_IO_Error: error connecting to master 'repl@12.18.46.78:3666' - retry-time: 10 retries: 86400

This error you get when you check " show slave status\G;"

Solution as follows:
Do Telnet:

[root@sj ~]# telnet 12.18.46.78 3666
Trying 12.18.46.78...
Connected to 12.18.46.78.
Escape character is '^]'.
uHost '12.18.46.78' is blocked because of many connection errors; unblock with 'mysqladmin flush-hosts'Connection closed by foreign host.

Do Flush host:

[root@sj ~]# mysqladmin -uroot -ppassword flush-hosts

Lets check " show slave status\G;" again.

Done.