Thursday, 16 October 2014

Script monitor and restart Mysql service

The following script which will monitor MySQL Server service and will automatically restart it if its down.

#!/bin/bash

# MySQL Server root/admin username
MUSER="root"

# MySQL Server admin/root password
MPASS="SET-ROOT-PASSWORD"

# MySQL Server hostname
MHOST="localhost"

#Shell script to start MySQL server i.e. path to MySQL daemon start/stop script.
MSTART="/etc/init.d/mysql start"

# Email ID to send notification
EMAILID="you@your-email.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
You just have to do some minor changes in this script (mysql password, email ID etc). 

Then set a cron to run according to your requirement for the above script.

for eg.
*/15 * * * * ./(path to script) > /dev/null 2>&1
This will run the script in every 15 mins.

Thursday, 4 September 2014

Disk space consumption

Command to get teh path of file which is consuming large disk space in the server:

du -a /home | sort -n -r | head -n 10

Core dump files

The possible reason for the core files getting generated is when a php process is killed, apache creates core files under your account.

If it is caused due to php/apache then you can get rid off those core files by editing the httpd start up file on the server end.
To Disable creation of core dump files:
root@server [~]# vi /etc/init.d/httpd
Search for ulimit lines .For eg : you can see these lines

ulimit -n 1024
ulimit -n 4096
ulimit -n 8192
ulimit -n 16384
You need to add ulimit -c 0 at the end. Which will look like :

ulimit -n 1024
ulimit -n 4096
ulimit -n 8192
ulimit -n 16384
ulimit -c 0

Save changes and quit.

Now kill / stop apache service and then start apache service on the server.

root@server [~]# service httpd stop
root@server [~]# service httpd stop
httpd (no pid file) not running
root@server [~]# service httpd startssl
root@server [~]# service httpd startssl
httpd (pid 21154) already running

Handling spamming


1:  Command to delete spam mails from a particular account

grep -lr user@domainname.tld /var/spool/exim/input/* |  xargs rm -vf


Symlinks

Command to disable all symlinks under a folder:

find -type l -exec unlink {} \;

Updating SPF for all accounts in cPanel servers

One way of fixing:

The /usr/local/cpanel/Cpanel/SPF.pm file handles the SPF installation script that runs "/usr/local/cpanel/bin/spf_installer username" and is also used in cPanel > Email Authentication area for setting the IP for the SPF record. First, make a backup of the file:

cp /usr/local/cpanel/Cpanel/SPF.pm /usr/local/cpanel/Cpanel/SPF.pm.bak

Now revise the lines that have this:
my $mainip = Cpanel::DIp::getmainserverip();

To have this instead:


my $mainip = '192.12.12.12';

This will then use the hard-coded IP when installing the SPF records. This will occur for all parked, addon, and subdomains on the account for the SPF installation. Whenever the user in cPanel > Email Authentication area enables SPF records, the hard-coded IP will be used.

Please note that the code entry occurs thrice in the file, so you would need to revise all the entries. If you only revise the initial entry, only the main domain will be changed to that new hard-coded IP, while parked, addon, and subdomains will not be.

You may also want to put that file into the exclude list for cPanel so it does not get overwritten on cPanel updates:

echo "/usr/local/cpanel/Cpanel/SPF.pm" >> /etc/cpanelsync.exclude



Script to update SPF for all accounts

#!/bin/bash
IFS="$"
cd /var/named
/bin/ls -- /var/cpanel/users | grep -v "root\|system\|passwd\|cpanel\|nobody\|mysql\|\`\|\-\|\." | while read CPUSER; do
echo "Installing SPF for '${CPUSER}'";
/usr/local/cpanel/bin/spf_installer "${CPUSER}" > /dev/null
done

Exim syntax error

Issue Error Messages:
SMTP call from (workstation) [IP] dropped: too many syntax or protocol errors (last command was "RCPT TO:

Solution:
When sending emails using outlook you might receive the error above. It seems that when you copy & paste the email addresses into the To field, they include quotations around them that the server did not know how to handle (i.e. ‘name@website.com’ instead of just name@website.com).

Remove the quotations & then try sending emails.