Wednesday, January 11, 2012

Tuesday, January 3, 2012

SMTP check script in ruby.

Create ruby script to check SMTP with login credential.
You can test two authentication in this script i.e. plain and login.

vi     /opt/rubymailtest.rb


require 'net/smtp'

message = <
From: info@homework.do
To: sbtoalerts@gmail.com
Subject: test message
Date: Sat, 23 Jun 2001 16:26:43 +0900

This is a test message.
END_OF_MESSAGE


#Net::SMTP.start('mail.exmaple.com') do |smtp|
#  smtp.send_message message, 'info@homework.do',
#                             'sbtoalerts@gmail.com'


# PLAIN
#Net::SMTP.start('mail.example.com', 25, 'mail.example.com',
#                'Your Account', 'Your Password', :plain)
 

# LOGIN
Net::SMTP.start('mail.example.com', 25, 'mail.example.com', 'username', 'passwd', :login) do |smtp|
      smtp.send_message message, 'info@homework.do',
                             'sbtoalerts@gmail.com'


#puts " done"
end

Tuesday, October 11, 2011

Remove extra spaces from file in linux.

If you found spaces and non-ASCII characters with in files, remove all  from a file in place.

Solution is:
perl -i.bak -pe 's/[^[:ascii:]]//g' filename


Thanks-

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