N42-WeekEight

1、显示统计占用系统内存最多的进程,并排序。
ps -o pid,%mem | sort -rk 2
2、编写脚本,使用for和while分别实现192.168.0.0/24网段内,地址是否能够ping通,若ping通则输出"success!",若ping不通则输出"fail!"

#!/bin/bash
#function:check all the ip is reacheable of the network 192.168.0.0/24 

IP_PREFIX="192.168.0"

trap "exit 0" INT
function ping4()
{
    local ip=$1
    for i in {1..255}
    do
        ping -c1 "${ip}.${i}" &>/dev/null
        if [ $? -eq 0 ]
        then
            echo -e "Ping ${ip}.${i} \033[32msuccess!\033[0m"
        else
            echo -e "ping ${ip}.${i} \033[31mfail!\033[0m"
        fi
    done
    return 0

}

function ping4_while()
{
    local ip=$1
    local i=1
    while [ ${i} -le 255 ]
    do
        ping -c1 "${ip}.${i}" &>/dev/null
        if [ $? -eq 0 ]
        then
            echo -e "Ping ${ip}.${i} \033[32msuccess!\033[0m"
        else
            echo -e "Ping ${ip}.${i} \033[31mfail!\033[0m"
        fi
        let i++
    done
}

ping4 "${IP_PREFIX}"
ping4_while "${IP_PREFIX}"

exit 0

3、每周的工作日1:30,将/etc备份至/backup目录中,保存的文件名称格式 为“etcbak-yyyy-mm-dd-HH.tar.xz”,其中日期是前一天的时间。
echo "30 01 * * 1,2,3,4,5 root tar czf /backup/etcbak-$(date -d yesterday +%Y-%m-%d-%H).tar.xz /etc" >> /etc/cronta
4、工作日时间,每10分钟执行一次磁盘空间检查,一旦发现任何分区利用率高 于80%,就发送邮件报警。
检查脚本如下,发邮件以日志代替:

#!/bin/bash
#function: check the diskspace and when the use age more then 80%, send the alarm by email


function check_disk_useage()
{
    local tmp_file="/tmp/disk_resut.file"
    df -Ph &>${tmp_file}
    while read line
    do
        local disk_partition=$(echo ${line} | awk '{print $1}')
        local use=$(echo ${line} | awk '{print $5}')
        if [[ "${use%*\%}" -gt 80 ]]
        then
            echo "The ${disk_partition} useage more than 80% and send the alarm by email"
        else
            echo "The ${disk_partition} useage is less than 80%."
        fi
    done < "${tmp_file}"
    rm -f "${tmp_file}"
    return 0
}

check_disk_useage

MAIN_RET=$?
exit ${MAIN_RET}

chmod u+x check_disk.sh
echo "*/10 * * * * root check_disk.sh" >> /etc/crontab

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值