Linux shell 脚本小记

Linux Shell 脚本

Sysstat 系统监控工具:

//CendOs
yum install -y sysstat

//ubuntu
sudo apt-get install sysstat

修改HostName:

hostnamectl set-hostname <hostname>

vim /etc/sysconfig/network
HOSTNAME=<hostname>
//restart shell

Xmial:

vi /etc/mail.rc  //如果不存在,则编辑/etc/mail.rc

// 末尾加入
set from="xxx@163.com"
set smtp=smtp.163.com
set smtp-auth-user=xxx@163.com
set smtp-auth-password=邮箱密码
set smtp-auth=login

// 测试一下
mailx -s "发件人名称  邮件标题" xxx@163.com
# set E-mail address for alert [multiple]
ADDRESS="ken@iplayabc.com"
# set alert disk max-line
ALERT=10
# Exclude list of unwanted monitoring, if several partions then use "|" to separate the partitions.
# An example: EXCLUDE_LIST="/dev/hdd1|/dev/hdc5"
EXCLUDE_LIST="/auto/ripper"
#
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
#
function main_prog() {
while read output;
do
#echo $output
  usep=$(echo $output | awk '{ print $1}' | cut -d'%' -f1)
  partition=$(echo $output | awk '{print $2}')
  CPU_LOAD=`sar -P ALL 1 2 |grep 'Average.*all' |awk -F" " '{print 100.0 -$NF}'`
  if [ $usep -ge $ALERT ] ; then
     echo -e "server: $(hostname) \n 硬盘空间超出!目前用量: $usep % \n CPU: $CPU_LOAD % ,\n 警报发出时间: $(date)" | \
     mailx -s "警告: 磁盘空间异常,当前使用 $usep%" $ADDRESS
  fi
done
}

if [ "$EXCLUDE_LIST" != "" ] ; then
  df -H | grep -vE "^Filesystem|tmpfs|cdrom|${EXCLUDE_LIST}" | awk '{print $5 " " $6}' | main_prog
else
  df -H | grep -vE "^Filesystem|tmpfs|cdrom" | awk '{print $5 " " $6}' | main_prog
fi

setup:

chmod +x monitor.sh
sh -x monitor.sh

持续监控:

#crontab -e 
#CPU Utilization Monitoring
*/30 * * * * sh /SCRIPT-PATH/monitor.sh

:wq   <<-- Save & Exit
Show Comments