1.用shell查看磁盘是否大于80%并发送邮箱告警。
分析如何查看磁盘占用:
# df -h | grep /dev/vda1 | awk '{print $5}' |cut -d "%" -f1 或者 # df -h | grep /dev/vda1 | awk '{print $5}' |awk -F "%" '{print $1}'
考虑到shell脚本if判断是否大于80,写入脚本:
#!/bin/bash rate=`df -h | grep /dev/vda1 | awk '{print $5}' |cut -d "%" -f1` if [ $rate -ge 80 ]; then python /root/mail.py [email protected] "邮件主题" "邮件内容/dev/vda1 The disk is full" fi
python脚本:
#!/usr/bin/python #coding:utf-8 import smtplib from email.mime.text import MIMEText import sys #发信地址 mail_user = 'admin@' //账号 #发信地址的SMTP密码 mail_pass = 'pqj0Ll6yd' //密码 def send_mail(to_list,subject,content): me = "aliyun告警平台"+"<"+mail_user+">" msg = MIMEText(content, 'plain', 'utf-8') msg['Subject'] = subject msg['From'] = me msg['to'] = to_list try: #定义阿里云提供的SMTP服务地址 s = smtplib.SMTP("smtpdm.aliyun.com", 25) //注,阿里云服务器是没开放25端口,改80端口即可 s.login(mail_user,mail_pass) s.sendmail(me,to_list,msg.as_string()) s.close() return True except Exception,e: print str(e) return False if __name__ == "__main__": send_mail(sys.argv[1], sys.argv[2], sys.argv[3])
测试结果:
2.判断nginx80端口是否开启,如果不开启,自动开启
首先网络扫描nmap默认包是不存在,安装
# yum install -y nmap
分析端口是否开放
# nmap -sT 192.168.109.136 | grep tcp |grep http |awk '{print $2}' open
用shell脚本判断
#!/bin/bash nginx=`nmap -sT 192.168.109.136 | grep tcp |grep http |awk '{print $2}'` data=`date +%Y%m%d` if [ "$http" == "open" ] then echo "$data nginx is ok! " >> /tmp/http-acc.log else /etc/init.d/nginx start &>/dev/null python /usr/local/sbin/mail.py [email protected] "监控nginx宕机自动重启告警" "$data restart nginx!!!"//可以去掉告警 echo "$data restart nginx !" >> /tmp/http-err.log fi
测试结果
3.用shell每天零晨备份mysql数据库,备份数据传输到备份服务器
#!/bin/bash datadir=/tmp/mysql/`date +%Y%m%d` //定义时间 datadb=dedecms //库名 datadb1=discuz //库名 datadb2=zrlog //库名 dbuser=root //用户 dbpass=123 //密码 backcmd=mysqldump //备份命令 注:最好绝对路径 if [ ! -d $datadir ];then //判断是否存在 mkdir -p $datadir //不存在创建 echo "mkdir successful" //成功 else echo "mkdir failure" //失败,失败可能是目录存在,不用管 fi $backcmd -u$dbuser -p$dbpass $datadb >$datadir/$datadb.sql //备份 $backcmd -u$dbuser -p$dbpass $datadb1 >$datadir/$datadb1.sql //备份 $backcmd -u$dbuser -p$dbpass $datadb2 >$datadir/$datadb2.sql //备份 rsync -avP /tmp/mysql/ 172.31.215.135:/tmp/mysql //上传到备份服务器
4.静态文件备份,要传到备份静态服务器
#!/bin/bash BCK_DIR="/tmp/data" //定义路径 WEB_DIR="/mnt/*" //定义备份路径 DATE=`date +%F` //时间 DATE_YEAR=`date +%Y` //时间 DATE_MONTH=`date +%m` //时间 days=7 //时间 if test -d $BCK_DIR/$DATE_YEAR/$DATE_MONTH //判断 then echo "directory $BCK_DIR/$DATE_YEAR/$DATE_MONTH exists." else echo "directory $BCK_DIR/$DATE_YEAR/$DATE_MONTH does not exists. make dir..." mkdir -p $BCK_DIR/$DATE_YEAR/$DATE_MONTH //不存在创建 fi # Backup tar -czf $BCK_DIR/$DATE_YEAR/$DATE_MONTH/web_$DATE.tar.gz $WEB_DIR //打包 find $BCK_DIR/$DATE_YEAR/ -type f -name "*.tar.gz" -mtime +$days -exec rm -rf {} \; //超过7天删除 rsync -avP $BCK_DIR 172.31.215.135:/tmp/data/ //拷贝到备份服务器
5.批量创建用户并设置密码,把密码写入文件
- 随机密码使用命令 mkpasswd
- 在脚本中给用户设置密码,可以使用echo 然后管道passwd命令
实现脚本如下:
#!/bin/bash for i in `seq 00 09` do useradd user$i pass=`mkpasswd` echo "user$i $pass" >> /root/shell.txt echo "$pass" |passwd --stdin user$i &> /dev/null done
批量删除用户
#!/bin/bash for i in `seq 00 09` do userdel -r user$i done