通过sendmail和139邮箱监控服务器负载


#! /bin/bash

rmail="[email protected]"
L="25"
host=`hostname`
while :; do
        load=`uptime |awk '{print $10}' |cut -d'.' -f 1`
        if [ $load -gt $L ] ; then
        uptime |awk '{print $8,$9,$10,$11,$12}'>/tmp/mail.txt
        mail -s $host:`cat /tmp/mail.txt|awk '{print $3$4$5}'` $rmail </tmp/mail.txt
        sleep 600
        fi
        sleep 3
done

监控网卡流量脚本

#! /bin/bash

rmail="[email protected]"
N="2000000"
host=`hostname`
while :; do
        n=`sar -n DEV 1 2  |grep eth0 | tail -n1 |awk '{print $5}' |cut -d. -f1`
        if [ $n -gt $N ] ; then
        sar -n DEV 1 |grep eth0 | tail -n1>/tmp/sar.txt
        mail -s $host\_flow:`echo $n`  $rmail </tmp/sar.txt
        sleep 600
        fi
        sleep 3
done

监控服务器是否存活


#! /bin/bash

N=2
[email protected]
while :; do
        ipfile=/usr/local/sbin/serverip.txt
        for ip in `cat $ipfile`; do
                mailfile=/tmp/pkgloss.txt
                pkgloss=`ping -c4 $ip |grep 'transm' |awk -F',' '{print $2}' |awk '{print $1}'`
                if [ $pkgloss -lt $N ]; then
                echo $ip >$mailfile
                ping -c4 $ip |grep 'transm' >>$mailfile
                mail -s "$ip loss packets"  $rmail<$mailfile
                fi
        done
        sleep 100
done

附件备份实例脚本

#! /bin/bash

w=`date +%w`
rs=/usr/bin/rsync
rsou=ypl-web:/data/wwwroot
rde=/backup/webatt
d1=`date +%y%m%d`
d2=`date -d "-1 day" +%y%m%d`
m1=`date -d "-1 day"  +%y%m`
m2=`date +%y%m`
sou=$rsou/ypl/attachments
des=$rde/ypl-att

exec 1>/var/log/att-bak.log 2>&1
echo attbak start `date`

if [ "$w" = "0" ]; then
$rs -aur --delete  $rsou/ypl/ $des/attachments_$w
else
$rs -aur --delete --exclude=attachments  $rsou/ypl/ $des/attachments_$w
[ -d $des/attachments_$w/attachments/month_$m2 ] || mkdir -p $des/attachments_$w/attachments/month_$m2
$rs -aur $sou/month_$m1/$d2* $des/attachments_$w/attachments/month_$m1/
$rs -aur $sou/month_$m2/$d1* $des/attachments_$w/attachments/month_$m2/
$rs -aur $sou/swfupload/$d2* $des/attachments_$w/attachments/swfupload/
$rs -aur $sou/swfupload/$d1* $des/attachments_$w/attachments/swfupload/
/usr/bin/find $des/attachments_$w/attachments/ -type f -mmin +1600 |xargs rm
fi
echo attbak end `date`

监控一个机器的80端口是否打开

while :;
do
        nmap -sS -vv 121.10.118.111 |grep 80 |tail -n1>/tmp/nmap.txt
        rr80=`cat /tmp/nmap.txt|awk '{print $2}'`
        [ "$rr80" = "open" ] && mail -s rr_port_80 [email][email protected][/email] </tmp/nmap.txt
        sleep 10
done
当然可以尝试修改脚本使其监控80端口是否正常。

让命令历史永久保存并加时间戳

#!/bin/sh
grep HISTTIMEFORMAT /etc/bashrc || echo 'export HISTTIMEFORMAT="%Y-%m-%d %H:%M:%S "' >>/etc/bashrc
for U in `grep -v shutdown /etc/passwd|awk -F: '$NF~/sh/&&$NF!~/no/{print $1}'`
do
    UHOME=`cat /etc/passwd|grep "^$U"|cut -d: -f6`
    [ ! -f $UHOME/.bash_history ] && touch $UHOME/.bash_history
    chattr +a $UHOME/.bash_history
done

mysqldump备份脚本

#! /bin/bash

STORE_NAME=`date +%m%d`
STORE_DIR='/home/backup/mysql/'
exec 1>/var/log/mysqlbak.log 2>&1

echo mysqlbak begin at `date`
[ ! -d $STORE_DIR ] && mkdir -p $STORE_DIR

/usr/local/mysql/bin/mysqldump  -uroot -p'password' databasename > $STORE_DIR/mysql-$STORE_NAME.sql
gzip $STORE_DIR/mysql-$STORE_NAME.sql

find $STORE_DIR  -mtime +7 -name '*.sql.gz' |xargs rm -f

echo mysqlbak finish at `date`

检查目录权限是否为root

#! /bin/bash

dir=/home/mysql
for file in `ls $dir`; do
        for file2 in `ls $dir/$file` ; do
                own=`ls -ld $dir/$file/$file2 |awk '{print $3}'`
                if  [ "$own" = "root" ] ; then
                        chown -R mysql:mysql $dir/$file/$file2
                        echo "$dir/$file/$file2" >>/tmp/checkown.log
                fi
        done
done

根据nginx日志抓取中国大陆IP段

#! /bin/bash

exec >>/tmp/addcnip.log 2>> /tmp/addcnip.log
echo " "
echo begin at `date`
d=`date +%y%m%d --date "-1 hour"`
d1=`date +%d --date "-1 hour"`
d2=`date +y%m%d%H --date "-1 hour"`
d3=`date +%H --date "-1 hour"`
svnpath=http://lishiming.5d6d.com/svn/cnip
logfile=/home/logs/access-$d2\_log
count1=`wc -l /usr/local/sbin/newcnip.txt|awk '{print $1}'`
accipf=/usr/local/sbin/nocnip.txt
newcnipf=/usr/local/sbin/newcnip.txt
dip=192.168.1.102
ipt=/sbin/iptables

awk '{print $1}' $logfile |sort -n|uniq |grep -v '^$'|grep -v '_'>/tmp/initip.txt
rm -f /tmp/cnip.txt
rm -f /tmp/initip2.txt
rm -f /tmp/nocnip.txt
for ip in `cat /tmp/initip.txt`; do
        n=`grep $ip $accipf|wc -l`
        if [ "$n" = "0" ] ; then
                echo $ip >>/tmp/initip2.txt
        fi
done

killall whois
for ip in `cat /tmp/initip2.txt|grep -v '^$'`; do
        [ -d /tmp/whois ] || mkdir /tmp/whois
        whois $ip >/tmp/whois/$ip
        coun=`cat /tmp/whois/$ip |grep 'ountry'|awk '{print $2}'|head -n1`
        if [ "$coun" = "CN" ] || [ "$coun" = "cn" ]; then
                ippool=`cat /tmp/whois/$ip |grep 'netnum' |awk '{print $2"-"$4}'`
                echo $ippool >>/tmp/cnip.txt
        else
                echo $ip >>/tmp/nocnip.txt
        fi
        rm -f /tmp/whois/$ip
done
cat $accipf >>/tmp/nocnip.txt
grep -v '^$' /tmp/nocnip.txt|sort -n |uniq >$accipf

cat $newcnipf |grep -v '^$'>>/tmp/cnip.txt
count2=`grep -v '^$' /tmp/cnip.txt|sort -n |uniq |wc -l |awk '{print $1}'`
if [ "$count1" -lt "$count2" ]; then

        sort -n /tmp/cnip.txt |uniq |grep -v '^$'>$newcnipf
        /bin/cp -f $newcnipf /tmp/cnip/$d1-cnip.txt
        $ipt -F

        for ip in `cat $newcnipf|grep -v '^$'`; do
                $ipt -I INPUT -m iprange --src-range $ip -d $dip -j DROP
        done
        for ip2 in `cat /usr/local/sbin/goodip.txt`; do
                $ipt -I INPUT -s $ip2 -d $dip -j ACCEPT
        done
        if [ "$d3" != "00" ] ; then
                /usr/bin/svn remove $svnpath\/$d.txt -m 'if exist'
        fi
        /usr/bin/svn import /tmp/cnip/$d1-cnip.txt  $svnpath\/$d.txt -m 'add cnip'


fi

echo end `date`

MySQL rsync方式备份

#! /bin/bash

exec 1>/var/log/mysqlbak.log 2>&1
data_source=/home/mysql/
backup_dir=/mnt/mysqlbak
remote_dir=1.2.3.4:/data/mysqlbak
n=`date +%d`
w=`date +%w`
echo backup of mysql begins at `date`
/etc/init.d/mysqld stop
p=/var/run/mysqld/mysqld.pid
        while ps -p $p >/dev/null;
        do
            sleep 1 && echo -n .
       done
sync;sync;sync;sleep 2;
rsync -aru --delete  $data_source/*  $backup_dir/mysql_$w/
/etc/init.d/mysqld start
echo backup of mysql  finished at `date`
rsync -aru --delete $backup_dir/mysql_$w/*  $remote_dir/mysql_$n/
echo remote backup of mysql  finished at `date`

过滤磁盘大于80%的分区

df -h|awk '$5~/[8 9][0-9]%|100%/ ' >/tmp/checkdisk.log

sed在文件中某一行最后添加一个数字

sed 's/\(^a.*\)/\1 12/' test

#cat  test
askdj
aslkd aslkdjf3e
skdjfsdfj
sdkfjk
fsdkfjksdjfkjsdf
12sdfesdf
aslkdjfkasdjf asdlfkjaskdfj

#sed 's/\(^a.*\)/\1 12/' test

askdj 12
aslkd aslkdjf3e  12
skdjfsdfj
sdkfjk
fsdkfjksdjfkjsdf
12sdfesdf
aslkdjfkasdjf asdlfkjaskdfj  12

监控网站存活状态

#! /bin/bash

chd='www.5dforum.com/bbs.php'
mailer='[email protected]'
maxrp=3
while :; do
        n1=0
        c=`ping -c 10 www.5dforum.com |grep received |awk '{print $4}' `
        if [ $c == "0" ] ; then
            for i in `seq 1 $maxrp`; do
                c=`ping -c 10 www.5dforum.com |grep received |awk '{print $4}' `
                if [ $c == "0" ] ; then
                n1=$i
                fi
                sleep 10
            done
            if [ $n1 == $maxrp ]; then
                echo "The 5dforum\'s network is down!"|mail -s 5dforum-down $mailer
                exit
            fi
        else
                if $(curl $chd|grep 'Discuz!'>/dev/null) ; then
                        sleep 30
                else
                        n2=0
                        for i in `seq 1 $maxrp`; do
                                if $(curl $chd|grep 'Discuz!'>/dev/null) ; then
                                        sleep 30
                                else
                                        n2=$i
                                fi
                        done
                        if [ $n2 == $maxrp ] ; then
                                echo "The 5dforum is something wrong, please cheack!"|mail -s 5dforum-down $mailer
                                exit
                        fi
                fi
        fi
        sleep 30
done

grep 或 egrep 或awk 过滤两个或多个关键词

grep -E '123|abc' filename  // 找出文件(filename)中包含123或者包含abc的行
egrep '123|abc' filename    //用egrep同样可以实现
awk '/123|abc/'  filename // awk 的实现方式

其他awk相关文章

计算一个文本中的所有数字

前提是,文件中只有一列数字。计算脚本为:
let sum=0 ; for i in `cat file `; do let j=$i; let sum=$j+$sum; done; echo $sum