Chinaunix

标题: [原创] 与Solaris Volume Manager metadevice 恢复及监测有关的几个脚本 [打印本页]

作者: susbin    时间: 2006-09-30 04:08
标题: [原创] 与Solaris Volume Manager metadevice 恢复及监测有关的几个脚本
[原创]  与Solaris Volume Manager metadevice 恢复及监测有关的几个脚本

作者: susbin@chinaunix.net

如引用请注明出处。欢迎指教。


前不久有网友提出关于如何恢复metadevice/metadb 的问题,这就需要
在系统正常时把metadevice 及metadb的设置信息保存起来。下面的第一个
脚本md.save 就能把Sun Volume Manager的设置信息存入几个文件里。
一般是在做好了metadevice ,或在做改动之前,及做改动之后,手工执行
这个脚本。也可以把它加到crontab 里,每星期或每月执行一次。
有了这些文件,当系统有问题时,就能比较容易的恢复metadevice/metadb。

另一个常遇到的问题是,如何定时监测metadevice 及metadb的运行状态。
下面的第二个脚本md.chk就能监测,当metadevice 有问题时还能给系统
管理员送出email。一般是把它加到crontab 里,每一小时执行一次。
很重要的生产服务器,也有每十五分钟执行一次的。

Sun 也有一个脚本 metacheck.sh  1.3,可惜太笨重。在网上搜了一下,发现
没什么人用。在Solaris 9 上试了一下,报出一些错后,停了。
http://docs.sun.com/app/docs/doc/806-6111/6jf2ve3ml?a=view


把监测metadevice的工作交给cron去做,就要求cron 不能停下来。
下面的第三个脚本cron.chk就是检测cron的。如果cron不在了,它能给
系统管理员送出email,也能自动执行/etc/rc2.d/S75cron  来启动cron。
可以把cron.chk加到crontab 里,每一天或几小时执行一次。

把这个脚本做些改动,就可以让它检测其他进程,比如 httpd.

第四个脚本targz.md.conf 是用来打包,压缩第一个脚本保存下来的metadevice
及metadb的文件的。把这些文件打包,压缩后存到两个不同地方(硬盘),就更安全。

把这个脚本做适当改动,就可以用它来新建.tar.gz 文件。

使用脚本前请仔细阅读,如不当使用造成系统损坏与本文作者无关。
以下脚本都在Solaris9 64-bit SPARC上测试通过。

1.
md.save
-------
#!/bin/ksh
# Save SDS configuration as files.
# Written by susbin@chinaunix.net

#t_stamp=`date '+%y_%m_%d-%H:%M:%S'`
t_stamp=`date '+%m_%d_%y-%H:%M:%S'`

log_dir1=/etc/lvm/recover
#log_dir2=

if [ ! -d $log_dir1 ]; then
   mkdir $log_dir1
fi

cmd_dir=/usr/sbin
#cmd_dir=/usr/opt/SUNWmd/sbin

if [ -x ${cmd_dir}/metastat ]
then
   CMD=${cmd_dir}/metastat
else
   echo "Can't find the command metastat!"
   exit 0
fi

echo " "
echo "Saving SDS configuration info to $log_dir1 ... "

/usr/bin/cat /etc/vfstab > ${log_dir1}/${t_stamp}.vfstab
/usr/bin/cat /kernel/drv/md.conf  | grep -v "#" > ${log_dir1}/${t_stamp}.kernel.md.conf
/usr/bin/sed '/^$/d' /etc/system | grep -v "*" | grep -v "set" > $log_dir1/${t_stamp}.system

$CMD -p > ${log_dir1}/${t_stamp}.metadevices
$CMD -t > ${log_dir1}/${t_stamp}.md_stat+time
${cmd_dir}/metadb > ${log_dir1}/${t_stamp}.replicas

/usr/bin/chmod 444 ${log_dir1}/${t_stamp}.*
#/usr/bin/cp -p ${log_dir1}/${t_stamp}.* $log_dir2

echo "Done. The files are at:"
echo " "
ls -l  ${log_dir1}/${t_stamp}.*
exit 0

-------

2.
md.chk
-------
#!/bin/sh
# Check metastat and send out email if problems are found.
# Written by susbin@chinaunix.net

mail_client=/usr/bin/mailx
email_1=you@abc.com
#email_2=

meta_stat=/usr/sbin/metastat
#meta_stat=/usr/opt/SUNWmd/sbin/metastat

NotOk_msg="Metastats are NOT Okay!"
InUse_msg="A hot spare is in use!"
Brk_msg="A metadevice is broken or unavailable!"

if [ -x $meta_stat ]
then
   CMD=$meta_stat
else
   echo "Can't find the command metastat!" | $mail_client -s "`uname -n` meta-Alert!" $email_1
   #echo "Can't find the command metastat!" | $mail_client -s "`uname -n` meta-Alert!" $email_2
   exit 0
fi

$CMD | grep State | grep -v Okay | grep -v Spare
if [ $? = 1 ] ; then
    sleep 1
    # echo "Metastats are Okay." | $mail_client -s "`uname -n` meta-info" $email_1
else
    echo $NotOk_msg | $mail_client -s "`uname -n` meta-Alert!" $email_1
    #echo $NotOk_msg | $mail_client -s "`uname -n` meta-Alert!" $email_2
    # echo $NotOk_msg
    echo " "
fi

$CMD | egrep "In use"
if [ $? = 0 ] ; then
    echo $InUse_msg | $mail_client -s "`uname -n` meta-Alert!" $email_1
    #echo $InUse_msg | $mail_client -s "`uname -n` meta-Alert!" $email_2
    # echo $InUse_msg
    echo " "
else
    exit 0
fi

$CMD | egrep "Broken|Unavailable"
if [ $? = 0 ] ; then
    echo $Brk_msg | $mail_client -s "`uname -n` meta-Alert!" $email_1
    #echo $Brk_msg | $mail_client -s "`uname -n` meta-Alert!" $email_2
    # echo $Brk_msg
    echo " "
else
    exit 0
fi

exit 0

-------

3.
cron.chk
--------
#!/bin/sh
# Check cron and send out email if it is down.
# Written by susbin@chinaunix.net

sc_name=cron.chk
t_stamp=`date '+%m_%d_%y-%H:%M:%S'`

mail_client=/usr/bin/mailx
email_1=you@xxx.com
#email_2=

up_msg="The cron deamon is running."
down_msg="The cron deamon is down!"

/usr/bin/ps -ef | grep cron | grep -v grep | grep -v $sc_name

if [ $? = 0 ] ; then
    echo $up_msg | $mail_client -s "`uname -n` cron-Info" $email_1
    #echo $up_msg | $mail_client -s "`uname -n` cron-Info" $email_2
    #echo $up_msg
    echo " "
else
    echo $down_msg | $mail_client -s "`uname -n` cron-Alert!" $email_1
    #echo $down_msg | $mail_client -s "`uname -n` cron-Alert!" $email_2
    #echo $down_msg
    echo " "
    # echo "Starting cron at $t_stamp "
    # /etc/rc2.d/S75cron start
fi

exit 0

-------

4.
targz.md.conf
-------
#!/bin/ksh
# tar-gz the saved files of SDS.
# Written by susbin@chinaunix.net

#t_stamp=`date '+%y_%m_%d-%H:%M:%S'`
t_stamp=`date '+%m_%d_%y-%H:%M:%S'`

# Create this directory before run the script.
dest_dir=/data/sds.saved

src_dir=/etc/lvm
log_dir1=recover

tar_file=sds.conf.tar

echo "Creating a .tar.gz file of ${src_dir}/${log_dir1}... "

/usr/sbin/tar cf ${dest_dir}/$tar_file -C $src_dir ./$log_dir1
/usr/bin/gzip ${dest_dir}/$tar_file
/usr/bin/mv ${dest_dir}/${tar_file}.gz ${dest_dir}/${t_stamp}.${tar_file}.gz

echo "It is done. Here is the files: "
echo " "
ls -l  ${dest_dir}/${t_stamp}.*
echo " "

-------

[ 本帖最后由 susbin 于 2006-9-30 04:16 编辑 ]
作者: Barrfee    时间: 2006-09-30 09:24
这把说“好~~!!”
作者: susbin    时间: 2006-09-30 19:17
原帖由 Barrfee 于 2006-9-30 09:24 发表
这把说“好~~!!”


不知这一句是哪一省的方言。
作者: lixu    时间: 2006-10-01 02:03
好!




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2