---
title: "MySQL shell(ETPASS): M/S status, Sendmail"
author: "xxd"
date: "2009-01-19T22:18:47Z"
tags: []
slug: "mysql-shell-etpass-m-s-status-sendmail"
draft: false
lastmod: "2009-01-19T22:18:47Z"
id: 460614
url: "https://xxd.iteye.com/blog/460614"
---

This script is actually another slow log detector, but it will also send mail instead of writing logs only.

1\. monitor MySQL, if the connection &gt;= 30 then call the sendmail function  
\---------------------------------------------------------  
$

more master\_mysql\_monitor.sh

> #!/bin/sh  
> flag=30 //more than 30 connections  
> path=/root/xxd/shell/mysql/monitor  
> user=username  
> pass=password  
> host=IP  
> logd=\`date +%Y%m%d\`  
> logf=$path/logs/mm\_run\_$logd.log  
> errf=$path/logs/mm\_err\_$logd.log  
> comm="set names utf8;show full processlist;"  
> date=\`date\`  
> shel=\`echo $comm | mysql -u$user -p$pass -h$host | awk '{print $0"$"}'\`  
> line=\`echo $shel | awk '{zcou=split($0,mcom,"$");for (leij=0;leij  
> ;}' | wc -l\`  
> line=$\[line-3]
> 
> . $path/include/functions //the sendmail function see 2
> 
> if \[ $line -gt $flag ];  
> then  
> echo "--------------------------------" &gt;&gt; $errf  
> echo "$date ---- not NULL:$line" &gt;&gt; $errf  
> echo $shel | awk '{zcou=split($0,mcom,"$");for (leij=0;leij&gt; $errf  
> echo "--------------------------------" &gt;&gt; $errf  
> send\_mail "m\_mysql"  
> fi
> 
> echo "----------------------------------------" &gt;&gt; $logf  
> echo "$date ---- not NULL:$line" &gt;&gt; $logf  
> echo $shel | awk '{zcou=split($0,mcom,"$");for (leij=0;leij&gt; $logf  
> echo "----------------------------------------" &gt;&gt; $logf

$ more slave\_mysql\_monitor.sh

> #!/bin/sh  
> flag="NULL"  
> path=/root/xxd/shell/mysql/monitor  
> user=username  
> pass=password  
> host=IP  
> logd=\`date +%Y%m%d\`  
> logf=$path/logs/sm\_log\_$logd.log  
> errf=$path/logs/sm\_err\_$logd.log  
> comm="set names utf8;show slave status\\G;"  
> date=\`date\`  
> line=\`echo $comm | mysql -u$user -p$pass -h$host | awk '{if ($1=="Seconds\_Behind  
> \_Master:") print $2;}'\`
> 
> . $path/include/functions
> 
> if \[\[ $line == $flag ]];  
> then  
> echo "-----------------------------" &gt;&gt; $errf  
> echo "$date" &gt;&gt; $errf  
> echo $comm | mysql -u$user -p$pass -h$host &gt;&gt; $errf  
> echo "-----------------------------" &gt;&gt; $errf  
> send\_mail "s\_mysql"  
> fi  
> echo $date &gt;&gt; $logf

2\. The sendmail function

\--------------------  
$ more /root/xxd/

monitor/include/functions

> #!/bin/sh  
> function send\_mail() {  
> send=$1  
> case "$send" in  
> "m\_mysql")  
> /usr/sbin/sendmail -t

![](https://blogger.googleusercontent.com/tracker/4722768579593920709-1331786305151303759?l=xxd79.blogspot.com)