目录
九、系统信息(主机名、IP地址、系统版本、内核版本、cpu名称、内存总大小、磁盘总大小)
一、DHCP
#!/bin/bash
systemctl stop firewalld
setenforce 0
echo "配置前将网卡模式更改为仅主机模式"
read -p "如果已更改(任意键下一步)"
rpm -q dhcp #检测dhcp是否安装
if [ $? -eq 0 ];then
echo "已安装dhcp"
else
echo "未安装dhcp,开始下载安装dhcp"
yum -y install dhcp &> /dev/null
echo "安装完成"
fi
#修改网卡配置
read -p "输入新ip:" xin_ip
read -p "输入新网关:" xin_way
ip=`cat /etc/sysconfig/network-scripts/ifcfg-ens33 | grep "IPADDR" | awk -F"=" 'print $2'`
way=`cat /etc/sysconfig/network-scripts/ifcfg-ens33 | grep "GATEWAY" | awk -F"=" 'print $2'`
sed -i "/IPADDR/s/$ip/$xin_ip/" /etc/sysconfig/network-scripts/ifcfg-ens33
sed -i "/GATEWAY/s/$way/$xin_way/" /etc/sysconfig/network-scripts/ifcfg-ens33
systemctl restart network
#复制配置文件
cp /usr/share/doc/dhcp*/dhcpd.conf.example /etc/dhcp/dhcpd.conf <<EOF
y
EOF
#修改配置文件
read -p "输入网段:" net
#地址池需要2个ip中间需要一个空格(如:192.168.100.10 192.168.100.30)
read -p "输入地址池范围:" dizhici
echo "subnet $net netmask 255.255.255.0 {
range $dizhici;
option routers $xin_way;
}
">> /etc/dhcp/dhcpd.conf
#启动dhcp服务
systemctl restart dhcpd
二、DNS
#!/bin/bash
#关闭防火墙
systemctl stop firewalld
systemctl disable firewalld
#关闭核心防护
setenforce 0
rpm -q bind
if [ $? -eq 0 ];then
echo "DNS服务已安装"
else
echo "DNS服务未安装,开始安装"
yum install -y bind > /dev/null
echo "DNS已安装"
fi
##修改全局配置文件
read -p "请输入ip地址" ip
sed -i '/listen-on port /listen-on/s/127.0.0.1/$ip/' /etc/named.conf
sed -i '/allow-query/s/localhost/any/' /etc/named.conf
#修改区域配置文
read -p "请输入域名(如: xiaogang):" url
sed -i '/zone/s/localhost.localdomain/$url/' /etc/named.rfc 1912.zones
sed -i '/file/s/named.localhost/$url\.zone/' /etc/named.rfc1912.zones
#修改区域数据配置文件
cp -p /var/named/named.localhost /var/named/$url.zone
cat <<EOF >/var/named/$name.com.zone
$TTL 1D #设置缓存解析结果的有效时间
@ IN SOA xiaogang.com. admin.$url.com. (
0; serial .
1D; refresh
1H; retry
1W; expire
3H ) ; minimum
NS $url.com. #记录当前区域的DNS服务器的名称
A 192.168.100.10 #记录主机IP地址
IN MX 10 $url.com. #MX为邮件交换记录,数字越大优先级越低
www IN A 192.168.100.100 #记录正向解析www.$url.com对应的IP
mail IN A 192.168.100.200 #CNAME使用别名,ftp是www的别名
* IN A 192.168.100.250 #泛域名解析,“*"代表任意主机名
EOF
#开启named服务
systemctl start named
#在另一台虚拟机上设置
#sed -ri /nameserver/s/114.114.114.114/192.168.100.10 /etc/resolv.conf
#nslookup www.$url.com
#nslookup mail.$url.com
#nslookup ftp.$url.com
三、PXE(一键装机)
#!/bin/bash
echo "=====PXE 自动安装"
#关闭防火墙,安全防护中心
systemctl stop firewalld &> /dev/null
systemctl disable firewalld &> /dev/null
setenforce 0 &> /dev/null
echo "提示:请新增一张网卡如ens37,设置为仅主机模式,共服务使用。
IP设置请根据虚拟机的虚拟网络编辑器的IP来自行定义。 本脚本安装系统,默认登录用户名为:root 密码为:root 请及时修改!!!
下面开始安装####"
echo "==============ens37网卡配置================================="
cd /etc/sysconfig/network-scripts
cp ifcfg-ens33 ifcfg-ens37
sed -i '/IPADDR/d' /etc/sysconfig/network-scripts/ifcfg-ens37 &>/dev/null
sed -i '/NETMASK/d' /etc/sysconfig/network-scripts/ifcfg-ens37 &>/dev/null
sed -i '/GATEWAY/d' /etc/sysconfig/network-scripts/ifcfg-ens37 >&/dev/null
sed -i '/DNS/d' /etc/sysconfig/network-scripts/ifcfg-ens37 &>/dev/null
sed -i '/DEVICE/d' /etc/sysconfig/network-scripts/ifcfg-ens37 &>/dev/null
sed -i '/NAME/d' /etc/sysconfig/network-scripts/ifcfg-ens37 &>/dev/null
sed -i '/UUID/d' /etc/sysconfig/network-scripts/ifcfg-ens37 &>/dev/null
sed -i '/BOOTPROTO/d' /etc/sysconfig/network-scripts/ifcfg-ens37 &>/dev/null
read -p "请输入IP地址: " IP
read -p "请输入子网掩码地址: " NET
read -p "请输入网关地址: " GAT
read -p "请输入DNS地址: " dns
echo "IPADDR=$IP
NETMASK=$NET
GATEWAY=$GAT
DNS1=$dns
BOOTPROTO=static
NAME=ens37
DEVICE=ens37" >> /etc/sysconfig/network-scripts/ifcfg-ens37
echo "网卡配置完成"
#重启网络配置,生效ens37
systemctl restart network &> /dev/null
if [ $? -eq 0 ];then
echo "网络服务重启成功"
else
echo "网络服务重启失败,请检查"
fi
echo "==============安装配置DHCP服务========"
#判断dhcp是否安装
rpm -q dhcp &> /dev/null
if [ $? -eq 0 ];then
echo "DHCP服务已安装"
else
yum -y install dhcp &> /dev/null
fi
rm -rf /etc/dhcp/dhcpd.conf &> /dev/null
cp /usr/share/doc/dhcp*/dhcpd.conf.example /etc/dhcp/dhcpd.conf <<EOF
y
EOF
#编辑DHCP的配置文件
read -p "输入DHCP网段: " wangduan
read -p "输入子网掩码: " yanma
read -p "输入地址池(中间以空格间隔): " dizhichi
read -p "输入DHCP网关IP地址: " wangguan
echo "
ddns-update-style none;
subnet $wangduan netmask $yanma {
range $dizhichi;
option routers $IP;
option domain-name-servers 114.114.114.114;
next-server $IP;
filename \"pxelinux.0\";
}" >>/etc/dhcp/dhcpd.conf
systemctl start dhcpd &> /dev/null
if [ $? -eq 0 ];then
echo "dhcp服务启动成功"
else
echo "dhcp服务启动失败"
fi
systemctl enable dhcpd &> /dev/null
echo "============配置TFTP、xinetd==========="
#判断TFTP是否存在
rpm -q tftp-server &> /dev/null
if [ $? -eq 0 ];then
echo "TFTP服务已安装"
else
yum -y install tftp-server &> /dev/null
fi
#判断xinetd是否存在
rpm -q xinetd &> /dev/null
if [ $? -eq 0 ];then
echo "tftp服务已安装"
else
yum -y install xinetd &> /dev/null
fi
#修改TFTP的配置文件
sed -i '10s/yes/no/' /etc/xinetd.d/tftp
sed -i '14s/yes/no/' /etc/xinetd.d/tftp
systemctl start xinetd &> /dev/null
if [ $? -eq 0 ];then
echo "xinetd服务启动成功"
else
echo "xinetd服务启动失败"
fi
systemctl enable xinetd &> /dev/null
systemctl start tftp &> /dev/null
if [ $? -eq 0 ];then
echo "tftp服务启动成功"
else
echo "tftp服务启动失败"
fi
systemctl enable tftp &> /dev/null
#挂载镜像
mount /dev/cdrom /mnt &> /dev/null
df -hT
#复制镜像中的文件
cp /mnt/images/pxeboot/initrd.img /var/lib/tftpboot/
cp /mnt/images/pxeboot/vmlinuz /var/lib/tftpboot/
echo "=========安装pxelinux.0和syslinux========="
yum provides */pxelinux.0 &> /dev/null
yum -y install syslinux &> /dev/null
rpm -ql syslinux | grep pxelinux.0
cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/
mkdir /var/lib/tftpboot/pxelinux.cfg
echo "default auto
prompt 0
label auto
kernel vmlinuz
append initrd=initrd.img method=ftp://$IP/centos7 ks=ftp://$IP/ks.cfg
label linux text
kernel vmlinuz
append text initrd=initrd.img method=ftp://$IP/centos7 ks=ftp://$IP/ks.cfg
label linux rescue
kernel vmlinuz
append rescue initrd=initrd.img method=ftp://$IP/centos7 ks=ftp://$IP/ks.cfg " >> /var/lib/tftpboot/pxelinux.cfg/default
echo "==============安装VSFTP===================="
#安装FTP
rpm -q vsftpd &> /dev/null
if [ $? -eq 0 ];then
echo "VSFTP服务已安装"
else
yum -y install vsftpd &> /dev/null
fi
mkdir /var/ftp/centos7
cd /var/ftp/centos7
cp -rf /mnt/* ./
systemctl start vsftpd &> /dev/null
if [ $? -eq 0 ];then
echo "vsftp服务启动成功"
else
echo "vsftp服务启动失败"
fi
systemctl enable vsftpd &> /dev/null
#实现无人值守安装
yum -y install system-config-kickstart &> /dev/null
if [ $? -eq 0 ];then
echo "kickstart安装成功"
else
echo "kickstart安装失败"
fi
echo "
#platform=x86, AMD64, 或 Intel EM64T
#version=DEVEL
# Install OS instead of upgrade
install
# Keyboard layouts
keyboard 'us'
# Root password
rootpw --plaintext root
# Use network installation
url --url="ftp://192.168.68.68/centos7"
# System language
lang en_US
# System authorization information
auth --useshadow --passalgo=sha512
# Use graphical install
graphical
firstboot --disable
# SELinux configuration
selinux --disabled
# Firewall configuration
firewall --disabled
# Network information
network --bootproto=dhcp --device=ens33
# Reboot after installation
reboot
# System timezone
timezone Asia/Shanghai
# System bootloader configuration
bootloader --location=mbr
# Clear the Master Boot Record
zerombr
# Partition clearing information
clearpart --all --initlabel
# Disk partitioning information
part /boot --fstype="xfs" --size=1024
part swap --fstype="swap" --size=4096
part / --fstype="xfs" --grow --size=1
%packages
@^gnome-desktop-environment
@base
@core
@guest-agents
@guest-desktop-agents
@input-methods
@internet-browser
@java-platform
@multimedia
@network-file-system-client
@networkmanager-submodules
@print-client
@x11
chrony
kexec-tools
%end" > /var/ftp/ks.cfg
echo "棒棒哒 !!"
四、RAID
#!/bin/bash
read -p "你要做RAID几(0、1、5、10)" a
case $a in
5)
fdisk /dev/sdb << EOF
n
\n
\n
\n
+5G
t
\nf
fd
w
EOF
fdisk /dev/sdc << EOF
n
\n
\n
\n
+5G
t
\nf
fd
w
EOF
fdisk /dev/sdd << EOF
n
\n
\n
\n
+5G
t
\nf
fd
w
EOF
fdisk /dev/sde << EOF
n
\n
\n
\n
+5G
t
\nf
fd
w
EOF
yum -y install mdadm
mdadm -C -v /dev/md5 -l5 -n3 /dev/sd[bcd]1 -x1 /dev/sde1
mkdir /opt/123
mkfs.xfs /dev/md5
mount /dev/md5 /opt/123
df -Th
;;
1)
fdisk /dev/sdb << EOF
n
\n
\n
\n
+5G
t
\nf
fd
w
EOF
fdisk /dev/sdc << EOF
n
\n
\n
\n
+5G
t
\nf
fd
w
EOF
yum -y install mdadm
mdadm -Cv /dev/md1 -l1 -n2 /dev/sd[bc]1
mkdir /opt/123
mkfs.xfs /dev/md1
mount /dev/md1 /opt/123
df -Th
;;
0)
fdisk /dev/sdb << EOF
n
\n
\n
\n
+5G
t
\nf
fd
w
EOF
fdisk /dev/sdc << EOF
n
\n
\n
\n
+5G
t
\nf
fd
w
EOF
yum -y install mdadm
mdadm -C -v /dev/md0 -l0 -n2 /dev/sd[bc]1
mkdir /opt/123
mkfs.xfs /dev/md0
mount /dev/md0 /opt/123
df -Th
;;
10)
fdisk /dev/sdb << EOF
n
\n
\n
\n
+5G
t
\nf
fd
w
EOF
fdisk /dev/sdc << EOF
n
\n
\n
\n
+5G
t
\nf
fd
w
EOF
fdisk /dev/sdd << EOF
n
\n
\n
\n
+5G
t
\nf
fd
w
EOF
fdisk /dev/sde << EOF
n
\n
\n
\n
+5G
t
\nf
fd
w
EOF
yum -y install mdadm
mdadm -C -v /dev/md0 -l0 -n2 /dev/sd[bc]1
mdadm -C -v /dev/md1 -l1 -n2 /dev/sd[de]1
mdadm -Cv /dev/md10 -l10 -n2 /dev/md0 /dev/md1
mkdir /opt/123
mkfs.xfs /dev/md10
mount /dev/md10 /opt/123
df -Th
;;
*)
echo "输入异常,重新输入5,0,1,10"
esac
五、DFS+APACHE+免密登录(expect)
#!/bin/bash
systemctl stop firewalld
setenforce 0
rpm -q rpcbind
if [ $? -eq 0 ];then
echo "rpcbind已安装"
else
echo "未安装rpcbind,需要下载安装"
yum -y install rpcbind >/dev/null
echo "已安装好"
fi
rpm -q nfs-utils
if [ $? -eq 0 ];then
echo "nfs-utils已安装"
else
echo "未安装nfs-utils,需要下载安装"
yum -y install nfs-utils >/dev/null
echo "已安装好"
fi
#创建共享目录
mkdir -p /nfs_gg
chmod 777 /nfs_gg
#rw 访问用户有读写权限 sync同步写入到内存和硬盘 no_root_squash 客户机已root身份访问时赋予赋予本地root权限
echo "/nfs_gg 192.168.100.0/24 (rw,sync,no_root_squash)" >/etc/exports
#开启相关服务
systemctl start rpcbind
systemctl enable rpcbind
systemctl start nfs
systemctl enable nfs
nmap -sT 192.168.100.10 | awk '/rpcbind/;/nfs/' &> /dev/null
if [ $? -eq 0 ];then
echo "rpcbind nfs 服务已开启"
else
echo "服务开启失败,请检查"
fi
#验证共享结果
exportfs -v >/dev/null
#加载共享结果
exportfs -r >/dev/null
#在客户机上查看共享结果
showmount -e
if [ $? -eq 0 ];then
echo "共享文件夹存在"
else
echo "共享文件夹不存在,请检查"
fi
#创建免密码登录客户机
rpm -q expect
if [ $? -eq 0 ];then
echo "expect 已安装"
else
echo "expect未安装,请检查"
yum -y install expect >/dev/null
echo "expect安装好了"
fi
/usr/bin/expect <<EOF
set timeout -1
spawn ssh root@192.168.100.20
expect {
"yes/no" { send "yes\n";exp_continue; }
"password:" { send "123456\r"; }
}
expect "root@*" { send "systemctl stop firewalld\r"}
expect "root@*" { send "systemctl disable firewalld\r"}
expect "root@*" { send "setenforce 0\r"}
expect "root@*" { send "yum -y install httpd\r"}
expect "root@*" { send "yum -y install nfs-utils rpcbind\r"}
expect "root@*" { send "systemctl start rpcbind\r"}
expect "root@*" { send "systemctl enable rpcbind\r"}
expect "root@*" { send "mount 192.168.100.10:/nfs_gg/ /var/www/html\r"}
expect "root@*" { send "systemctl start httpd\r"}
expect "root@*" { send "exit\r; }
expect eof
EOF
cd /nfs_gg
touch index.html
echo "<html><body><h1>hello world!</h1></body></htm1>" >>index.html
#永久挂载需要加载才能生效 mount -a
#{ send "echo "192.168.100.10:/nfs_gg/ /var/www/html nfs defaults,net_dev 0 0">>/etc/fstab"\r; }
六、LVM
read -p "是否需要进行分区(y/n):" num
if [ $num == y ]
then
echo "配置lvm建议不要格式化和挂载!!"
fdisk
fi
read -p "需要添加的盘(填写绝对路径):" dev
pvcreate $dev
read -p "请给予卷组名:" name
vgcreate $name $dev
read -p "请给予卷组的大小(M):" big
read -p "请给予逻辑卷的名字:" name1
lvcreate -L ${big}M -n $name1 $name
mkfs.xfs /dev/$name/$name1 &> /dev/null
mkdir /mnt/$name1
mount /dev/$name/$name1 /mnt/$name1
echo "/dev/$name/$name1 /mnt/$name/$name1 xfs defaults 0 0" >> /etc/fstab
lsblk
七、磁盘分区
##############刷新磁盘分区#######
echo "- - -" > /sys/class/scsi_host/host0/scan
echo "- - -" > /sys/class/scsi_host/host1/scan
echo "- - -" > /sys/class/scsi_host/host2/scan
#############查看有哪些盘#########################
ls /dev/sd*
read -p "选择分哪块磁盘(sd?):" dev
############查看目前磁盘分区情况###################
fdisk -l /dev/$dev
############开始分区##############################
read -p "请写出分第几个区域:" num
read -p "请写出分多大空间(M):" num1
fdisk /dev/$dev &> /dev/null <<EOF
n
p
$num
+${num1}M
w
EOF
partprobe &> /dev/null
############格式化################################
read -p "是否需要自动格式化和挂载(y/n):" j
if [ $j == y ]
then
mkfs.xfs /dev/${dev}$num &> /dev/null
mkdir /mnt/${dev}$num
mount /dev/${dev}$num /mnt/${dev}$num
############自动挂载#############################
echo "/dev/${dev}$num /mnt/${dev}$num xfs defaults 0 0" >> /etc/fstab
mount -a
fi
read -p "是否继续进行分区(y/n):" i
if [ $i == y ]
then
disk
else
echo "完成"
fi
八、监控资源(cpu、内存等)
#!/bin/bash
#===============================================================================================================================
# user yz
# time 7.22
# linux 资源监控
#===============================================================================================================================
cpu=0
mem=0
disk=0
ip=0
io=0
get_cpu_info(){
#######################获取cpu内的信息#########################"
cpu_num=`cat /proc/cpuinfo | grep "physical id" | uniq | wc -l ` #获取cpu个数
cpu_core=`cat /proc/cpuinfo | grep -c "core id"` #获取cpu核数
cpu_logic_num=`cat /proc/cpuinfo | grep "process" | wc -l ` #获取逻辑cpu
#########################获取当前cpu利用率#####################"
cpu_usr=`top -b -n 1 | grep "%Cpu" | awk '{print$2}' | cut -f 1 -d ","` #用户空间占用百分比
cpu_sys=`top -b -n 1 | grep "%Cpu" | awk '{print$4}' | cut -f 1 -d ","` #系统内核占用百分比
cpu_id=`top -b -n 1 | grep "%Cpu" | awk '{print$8}' | cut -f 1 -d ","` #查看空闲cpu百分比
cpu_wa=`top -b -n 1 | grep "%Cpu" | awk '{print$10}' | cut -f 1 -d "%"` #查看等待输入输出占百分比
###########################获取cpu负载信息####################"
cpu_load_1=`uptime | awk '{print$9}' | cut -f 1 -d ","` #获取cpu1分钟前到现在的负载均衡值
cpu_load_5=`uptime | awk '{print$10}' | cut -f 1 -d ","` #获取cpu5分钟前到现在的负载均衡值
cpu_load_15=`uptime | awk '{print$11}' | cut -f 1 -d ","` #获取cpu15分钟前到现在的负载均衡值
echo -e "\033[1;35m#####################监控Cpu资源############### $time\033[0m"
echo "#######################获取cpu内的信息#########################"
echo "物理cpu的个数:$cpu_num"
echo "系统cpu核数为:$cpu_core"
echo "逻辑cpu个数为:$cpu_logic_num"
echo "#########################获取当前cpu利用率#####################"
echo "用户空间占用cpu百分比:$cpu_usr"
echo "系统内核占用cpu百分比:$cpu_sys"
echo "空闲cpu百分比:$cpu_id"
echo "等待输入输出占cpu百分比:$cpu_wa"
echo "###########################获取cpu负载信息####################"
echo "cpu1分钟内的负载:$cpu_load_1"
echo "cpu5分钟内的负载:$cpu_load_5"
echo "cpu15分钟内的负载:$cpu_load_15"
if [ $(echo "$cpu_usr+$cpu_sys > 70 " | bc) -eq 1 ]
then
echo "cpu使用资源超过70%-偏高"
let cpu++
fi
}
get_mem_info(){
######################获取内存资源############################
mem_total=`free -m | grep "Mem" | awk '{print$2}'` #获取内存总量
mem_used=`free -m | grep "Mem" | awk '{print$3}'` #获取系统已使用的内存总量
mem_free=`free -m | grep "Mem" | awk '{print$4}'` #获取系统未使用的内存
mem_share=`free -m | grep "Mem" | awk '{print$5}'` #获取系统共享内存
mem_bc=`free -m | grep "Mem" | awk '{print$6}'` #获得系统的buff/cache
mem_available=`free -m | grep "Mem" | awk '{print$7}'` #获取系统真实可用的剩余内存
######################获取交换分区资源########################
swap_total=`free -m | grep "Swap" | awk '{print$2}'` #获取系统未使用的内存
swap_used=`free -m | grep "Swap" | awk '{print$3}'` #获取系统未使用的内存
swap_free=`free -m | grep "Swap" | awk '{print$4}'` #获取系统未使用的内存
echo -e "\033[1;35m#####################监控内存资源############### $time\033[0m"
echo "#######################获取内存信息#########################"
echo "内存总量:$mem_total M"
echo "系统已使用内存量:$mem_used M"
echo "系统未使用内存量:$mem_free M"
echo "系统中共享内存量:$mem_share M"
echo "系统高速缓冲/缓存资源:$mem_bc M"
echo "系统剩余的真实内存(用于启动新程序):$mem_available M"
echo "#######################获取交换分区信息#########################"
echo "交换分区总量:$swap_total M"
echo "系统已使用的交换分区量:$swap_used M"
echo "系统未使用的交换分区量:$swap_free M"
if [ $mem_used -gt 800 ]
then
echo "内存容量剩余量过低"
let mem++
fi
}
get_disk_info(){
######################获取磁盘空间资源########################
disk_total=`df -hT | grep "/dev/mapper/centos-root" | awk '{print$3}' | cut -f 1 -d "G"` #获取磁盘总量
disk_used=`df -hT | grep "/dev/mapper/centos-root" | awk '{print$4}' | cut -f 1 -d "G"` #获取系统已使用磁盘量
disk_free=`df -hT | grep "/dev/mapper/centos-root" | awk '{print$5}' | cut -f 1 -d "G"` #获取系统未使用磁盘量
disk_used_percent=`df -hT | grep "/dev/mapper/centos-root" | awk '{print$6}' | cut -f 1 -d "%"` #获取系统以使用资源百分比
echo -e "\033[1;35m#####################监控磁盘资源############### $time\033[0m"
echo "#######################获取磁盘信息#########################"
echo "根磁盘总量:$disk_total G"
echo "根磁盘已使用量:$disk_used G"
echo "根磁盘未使用量:$disk_free G"
echo "根磁盘已使用百分比:$disk_used_percent %"
if [ $disk_used_percent -gt 90 ]
then
echo "磁盘容量剩余量过低"
let disk++
fi
}
get_ifconfig_info(){
######################获取网卡资源########################
ip_info=`ifconfig | head -n 2 | grep "inet" | awk '{print$2}'` #获取网卡ip地址
netmask=`ifconfig | head -n 2 | grep "inet" | awk '{print$4}'` #获取网络子网掩码
netstat=`netstat -napt | grep -v "^A\|^P" |awk '{print $6}'|sort |uniq -c` #获取当前网络连接数
echo -e "\033[1;35m#####################监控网卡资源############### $time\033[0m"
echo "#######################获取网卡信息#########################"
echo "网卡IP地址:$ip_info"
echo "网卡子网掩码:$netmask"
echo "当前网络连接情况:
$netstat"
ping -c 3 -i 0.5 www.baidu.com &> /dev/null
if [ ! $? == 0 ]
then
echo "网络出现故障"
let ip++
fi
}
get_io_info(){
echo -e "\033[1;35m#####################监控I/O资源############### $time\033[0m"
######################获取I/O资源########################
echo "#######################获取I/O资源#########################"
# 每秒向设备发起的读请求次数
disk_sda_rs=`iostat -kx | grep sda| awk '{print $4}'`
# 每秒向设备发起的写请求次数
disk_sda_ws=`iostat -kx | grep sda| awk '{print $5}'`
# 向设备发起的I/O请求队列长度平均值
disk_sda_avgqu_sz=`iostat -kx | grep sda| awk '{print $9}'`
# 每次向设备发起的I/O请求平均时间
disk_sda_await=`iostat -kx | grep sda| awk '{print $10}'`
# 向设备发起的I/O服务时间均值
disk_sda_svctm=`iostat -kx | grep sda| awk '{print $11}'`
# 向设备发起I/O请求的CPU时间百分占比
disk_sda_util=`iostat -kx | grep sda| awk '{print $12}'`
echo "每秒向设备发起的读请求次数:"$disk_sda_rs
echo "每秒向设备发起的写请求次数:"$disk_sda_ws
echo "向设备发起的I/O请求队列长度平均值:"$disk_sda_avgqu_sz
echo "每次向设备发起的I/O请求平均时间:"$disk_sda_await
echo "向设备发起的I/O服务时间均值:"$disk_sda_svctm
echo "向设备发起I/O请求的CPU时间百分占比:"$disk_sda_util
if [ $(echo "$disk_sda_util > 30" | bc ) -eq 1 ]
then
echo "I/O资源繁忙"
let io++
fi
}
while true
do
get_cpu_info
if [ $cpu -gt 0 -a $cpu -lt 3 ]
then #限制警告邮件只能发两封
echo "cpu使用率过高!!" | mail -s "cpu警告" 1109047198@qq.com
fi
get_mem_info
if [ $mem -gt 0 -a $mem -lt 3 ]
then
echo "内存容量过低!!" | mail -s "内存警告" 1109047198@qq.com
fi
get_disk_info
if [ $disk -gt 0 -a $disk -lt 3 ]
then
echo "磁盘容量过低!!" | mail -s "磁盘警告" 1109047198@qq.com
fi
get_io_info
if [ $io -gt 0 -a $io -lt 3 ]
then
echo "I/O使用率过高!!" | mail -s "I/O警告" 1109047198@qq.com
fi
get_ifconfig_info
if [ $ip -gt 0 -a $ip -lt 3 ]
then
echo "网络崩了!!" | mail -s "网络警告" 1109047198@qq.com
fi
sleep 5
echo "=================================================================="
echo "=================================================================="
echo "=========================分割符====================================="
echo "=================================================================="
echo "=================================================================="
sleep 5
done
九、系统信息(主机名、IP地址、系统版本、内核版本、cpu名称、内存总大小、磁盘总大小)
#!/bin/bash
echo " 主机名: `hostname`
IP地址: $(hostname -i | awk '{print $2}')
系统版本: $(uname -a | awk -F "-" '{print $1}')
内核版本: $(cat /proc/version | egrep -o [0-9].*\._[0-9]{2})
cpu名称: $(lscpu | cat -n | sed -n '13p'| awk -F: '{print $2}')
内存总大小:$(free -h | sed -n '2p' | awk '{print $2}')
磁盘总大小:$(lsblk |sed -n '/disk/p'|awk -F '[ G]+' '{print $4}'|awk '{sum+=$1;print $1}END{print sum}'|tail -n 1)G
十、查看目录下有多少文件
#!/bin/bash
sum=0
for i in `find /opt`
do
if [ -f $i ];then
let sum++
fi
done
echo "opt目录下有$sum个文件"