显示信息如下:
当前系统版本是:CentOS Linux 7
当前系统内核是: Linux 3.10.0-1160.el7.x86_64
当前虚拟平台是:vmware
当前主机名是: localhost.localdomain
当前ip地址:192.168.80.129
#!/bin/bash
info(){
system="$(hostnamectl | grep System | awk '{print $3}')"
kernel_release="$(hostnamectl | grep Kernel | awk -F : '{print $2}')"
Virtualization=$(hostnamectl | grep Virtualization | awk '{print $2}')
server_name=$(hostname)
ipaddr=$(hostname -I)
echo "当前系统版本是:${system}"
echo "当前系统内核是:${kernel_release}"
echo "当前虚拟平台是:${Virtualization}"
echo "当前主机名是:${server_name}"
echo "当前ip地址:${ipaddr}"
}
checkerrror(){
error_info=$(dmesg | grep error)
if [ -e ${error_info} ]
then
echo "无错误日志!"
else
ehcho ${error_info}
fi
}
info
checkerrror
#!/bin/bash
close_firewalld(){
code=$(systemctl status firewalld)
if [ ${code} -eq 0 ]
then
systemctl stop firewalld
fi
}
close_selinux(){
sed -i '/^SELINUX/s/=.*/=disabled/' /etc/selinux/config
setenforce 0
}
close_iptables(){
iptables -F
service iptables save
service iptables restart
}
close_firewalld
close_selinux
close_iptables
每天凌晨打包压缩/var/log/nginx目录下所有内容,存放在/tmp/nginx目录里
压缩文件命名规范:yymmdd_logs.tar.gz
该目录保存七天内容,shell脚本保存在/usr/bin下
#!bin/bash
date="$(date +%Y%m%d)"
dir='/tmp/nginx'
backupfile='yymmdd_logs.tar.gz'
#查看/tmp/nginx是否存在,不存在则创建
checkbak(){
if [ ! -e ${dir} ]
then
mkdir ${dir}
fi
}
#压缩文件
backup(){
tar -zcvf ${dir}/${backupfile} /var/log/nginx/ > /dev/null 2>&1
echo "${backupfile} Compressed and packaged successfully !"
}
#清楚七天过期文件
cleanup(){
find ${dir} -type f -mtime +7 | xagrs rm -rf
if [ $? -eq 0 ]
then
echo "Cleaned up successfully!"
else
echo "data cleaning failed error, please pay attention in time"
fi
}
checkbak
backup
cleanup
#!/bin/bash
#检查用户是否存在,不存在则创建
checkuser(){
for i in $(seq 1 20)
do
id user${i} > /dev/null 2>&1
if [ $? -eq 0 ]
then
echo "user${i} 已存在!"
else
useradd user${i} && echo "user${i}" | passwd --stdin user${i} > /dev/null 2>&1
fi
done
}
checkuser
$1 是执行脚本的第一个参数
$2 是执行脚本的第二个参数
#!/bin/bash
useradd "$1"
echo "$2" | passwd ‐‐stdin "$1"
#!/bin/bash
#检查用户是否存在,存在则删除
checkuser(){
for i in $(seq 1 20)
do
id user${i} > /dev/null 2>&1
if [ $? -eq 0 ]
then
userdel -r user${i}
else
echo "user${i} 不存在!"
fi
done
}
checkuser
#!/bin/bash
package="ntpdate"
info=$(rpm -q ${package})
check_pkgs(){
if [ ! -e ${info} ]
then
echo "ntpdate已存在!"
else
echo "开始安装!"
yum install -y ${package} > /dev/null 2>&1
fi
}
modify_time(){
echo "开始修改时间"
rm -rf /etc/localtime && ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
/usr/sbin/ntpdate cn.pool.ntp.org > /dev/null 2>&1 && hwclock -w
}
check_pkgs
modify_time
检查某一服务是否正常运行,执行脚本的时候第一个参数为服务名
#!/bin/bash
result=$(pidof $1 | wc -l)
echo ${result}
if [ ${result} -eq 0 ]
then
echo "service does not exist !"
else
echo "Service is running normally !"
fi
ping目标主机看是否ping得通,三次ping通表示主机正常运行
将目标主机的ip地址作为第一个参数传进去
#!/bin/bash
ipaddr=$1
echo ${ipaddr}
ping_status(){
if ping -c 1 ${ipaddr} > /dev/null 2>&1
then
echo "ping ${ipaddr} is successful!"
continue
fi
}
for i in $(seq 1 3)
do
ping_status
echo "ping ${ipaddr} is failure!"
done
进阶版——对ip地址池里的主机分别进行心跳检测
ipaddr=(192.168.149.131 192.168.149.130 192.168.149.132 192.168.149.133)
for i in ${ipaddr[*]}
do
echo ".... begin to ping ${i} ....."
if ping -c 3 ${i} > /dev/null 2>&1
then
echo "ping ${i} is successful!"
else
echo "ping ${i} is failure!"
fi
done
根分区剩余空间小于20%(即已用空间大于80%) 内存已用空间大于80% 输出告警信息
配合crond每5分钟检查一次
#!/bin/bash
disk_letfspace=$(df -Th | grep -w / | awk '{print$6}' | cut -d % -f 1)
mem_used=$(free -m | grep Mem | awk '{print$3}')
mem_total=$(free -m | grep Mem | awk '{print$2}')
mem_letfspace=$[${mem_used}*100/${mem_total}]
if [ ${disk_letfspace} -gt 80 ]
then
echo "Disk free space is less than 20%!"
else
echo "${disk_letfspace}% of disk space left"
fi
if [ ${mem_letfspace} -gt 80 ]
then
echo "memory space is less than 20%!"
else
echo "${mem_letfspace}% of memory space left"
fi
crontab -l
*/5 * * * * /root/check_space.sh