此模块主要有shell下log日志,文件传输是否完成,发送邮件,创建FTP目录,判断FTP是否传输成功等函数
#!/bin/bash
#日志模块 (参数个数:3,参数名称:日志级别,日志内容,内容的格式化传参)
function log ()
{
#日志级别 debug; info; warn; error
#设置log存储路径
currentMonthLog=$(date "+%Y%m")
currentTimeLog=$(date "+%Y-%m-%d")
logPath="/data/LOG"
if [ ! -d "${logPath}" ]; then
mkdir ${logPath}
fi
if [ ! -d "${logPath}/$currentMonthLog" ]; then
mkdir ${logPath}/$currentMonthLog
fi
fileName=$(basename $0)
logfile=$logPath/$currentMonthLog/$fileName$currentTimeLog".log"
local msg;local logtype
if [ $# == 3 ]; then
logtype=$1
str="printf \"$2\" $3"
msg=`eval $str`
else
logtype=$1
msg=$2
fi
datetimeLog=`date +'%F %H:%M:%S'`
logformat="[${logtype}]\t${datetimeLog}\tfuncname:${FUNCNAME[@]/log/}\t[line:`caller 0 | awk '{print$1}'`]\t${msg}"
#打印信息
{
case $logtype in
debug)
echo -e "${logformat}" ;;
info)
echo -e "${logformat}" ;;
warn)
echo -e "${logformat}" ;;
error)
echo -e "${logformat}" ;;
esac
} | tee -a $logfile
}
#判断文件是否传输完成模块 (参数个数:1,参数名称:文件名)
function isFileTransferEnd ()
{
while( true )
do
size1=`ls -l ${1} | awk '{print $5}'`
sleep 1
size2=`ls -l ${1} | awk '{print $5}'`
if [[ ${size1} == ${size2} ]];then
log info "文件 | ${1} | 传输完成"
break;
fi
done
}
#发送邮件告警模块(mailx) (参数个数:2,参数名称:分组名称,告警内容)
function sendMail ()
{
#设置坏境变量
export PATH=/usr/local/bin:$PATH
export LANG=en_US.UTF-8
#收件人列表
CURDIR=$(cd $(dirname ${BASH_SOURCE[0]}); pwd )
Recipientlist=$CURDIR/Recipientlist.txt
group=${1}
case $group in
group1)
Addressee=`cat ${Recipientlist} |awk 'NR==1' |awk -F ':' '{print $2;}'`;;
group2)
Addressee=`cat ${Recipientlist} |awk 'NR==2' |awk -F ':' '{print $2;}'`;;
group3)
Addressee=`cat ${Recipientlist} |awk 'NR==3' |awk -F ':' '{print $2;}'`;;
*)
Addressee=`cat ${Recipientlist} |awk 'NR==1' |awk -F ':' '{print $2;}'`;;
esac
#邮件主题
Mailtheme="【云南告警】Missing data"
#邮件内容
Mailcontent=${2}
#发送邮件
echo ${Mailcontent}|mailx -v -s "${Mailtheme}" ${Addressee}
}
#发送邮件告警模块(sendEmail) (参数个数:2,参数名称:分组名称,告警内容)
function sendEmail ()
{
#设置坏境变量
#export PATH=/usr/local/bin:$PATH
#export LANG=en_US.UTF-8
#收件人列表
CURDIR=$(cd $(dirname ${BASH_SOURCE[0]}); pwd )
Recipientlist=$CURDIR/Recipientlist.txt
group=${1}
case $group in
group1)
Addressee=`cat ${Recipientlist} |awk 'NR==1' |awk -F ':' '{print $2;}'`;;
group2)
Addressee=`cat ${Recipientlist} |awk 'NR==2' |awk -F ':' '{print $2;}'`;;
group3)
Addressee=`cat ${Recipientlist} |awk 'NR==3' |awk -F ':' '{print $2;}'`;;
*)
Addressee=`cat ${Recipientlist} |awk 'NR==1' |awk -F ':' '{print $2;}'`;;
esac
#邮件主题
Mailtheme="【云南告警】Missing data"
#邮件内容
Mailcontent=${2}
#发送邮件
/usr/local/bin/sendEmail -s smtp.qiye.163.com -xu fsd_ah_warning@vixtel.com -xp Vixtel205 -f fsd_ah_warning@vixtel.com -t ${Addressee} -u ${Mailtheme} -m ${Mailcontent} -o message-content-type=text -o message-charset=utf-8
}
#创建FTP目录模块 (参数个数:1,参数名称:文件夹所在路径)(注:限制用户只能访问改路径,不能访问其他路径,需修改/etc/vsftpd/vsftpd.conf)
function createFTP()
{
#创建FTP目录
rootPath=$1
if [ ! -d "${rootPath}" ]; then
mkdir ${rootPath}
else
echo "Folder Exist"
fi
CURDIR=$(cd $(dirname ${BASH_SOURCE[0]}); pwd )
while read myline
do
fileName=`echo $myline |awk -F ' ' '{print $1}'`
password=`echo $myline |awk -F ' ' '{print $2}'`
useradd -d ${rootPath}/${fileName} ${fileName} -s /sbin/nologin
/usr/bin/expect<<EOF
spawn passwd $fileName
set timeout 300
expect "New UNIX password:"
set timeout 300
send "$password\r"
set timeout 300
expect "Retype new UNIX password:"
send "$password\r"
send "exit\r"
expect eof
EOF
done<${CURDIR}/ftpAccount.txt
}
#FTP发送文件并返回是否成功模块 (参数个数:6,参数名称:服务器IP,FTP名称,FTP密码,远程服务器路径,本地服务器路径,需发送的文件名称)
function ftpTransfer ()
{
server=$1
name=$2
passwd=$3
path=$4
lpath=$5
fileName=$6
status=`ftp -v -n $server<<END_SCRIPT
user $name $passwd
binary
cd $path
lcd $lpath
prompt
mput $fileName
close
bye
END_SCRIPT`
echo $status|grep "226 Transfer complete"
if [ $? -eq 0 ]
then
log info "Connect Succeed!!!"
exit 0
else
echo $status|grep "530 Login incorrect"
if [ $? -eq 0 ]
then
log info "FTP Connect Error!!!"
sendMail group1 "FTP Connect Error,$fileName Transfer fail"
exit 1
else
log info "No data transferred!!!"
exit 0
fi
fi
}