Linux實戰--日常運維

1、顯示統計佔用系統內存最多的進程,並排序。

[root@client_1 ~]# ps auxw --sort=%mem
USER        PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root          2  0.0  0.0      0     0 ?        S    Apr10   0:00 [kthreadd]
root          4  0.0  0.0      0     0 ?        S<   Apr10   0:00 [kworker/0:0H]
root          6  0.0  0.0      0     0 ?        S    Apr10   0:03 [ksoftirqd/0]
root          7  0.0  0.0      0     0 ?        S    Apr10   0:00 [migration/0]
root          8  0.0  0.0      0     0 ?        S    Apr10   0:00 [rcu_bh]
root          9  0.0  0.0      0     0 ?        S    Apr10   0:17 [rcu_sched]
root         10  0.0  0.0      0     0 ?        S<   Apr10   0:00 [lru-add-drain]
root         11  0.0  0.0      0     0 ?        S    Apr10   0:01 [watchdog/0]
root         12  0.0  0.0      0     0 ?        S    Apr10   0:01 [watchdog/1]
root         13  0.0  0.0      0     0 ?        S    Apr10   0:00 [migration/1]
root         14  0.0  0.0      0     0 ?        S    Apr10   0:03 [ksoftirqd/1]
...

2、編寫腳本,使用for和while分別實現192.168.0.0/24網段內,地址是否能夠ping通,若ping通則輸出"success!",若ping不通則輸出"fail!"

#併發檢測IP段
[root@client_1 ~]# cat ip.sh 
#!/bin/bash
#
#*************************************
#author:                giyomwd
#version:               1.0
#date:                  2020-04-12
#description:           check ip
#*************************************
NETID=192.168.0
for HOSTID in {1..254};do
   {
    if ping -c1 -w1 "$NETID.$HOSTID" &> /dev/null;then
         echo $NETID.$HOSTID is up - success!
    else
         echo $NETID.$HOSTID is up - failed!
    fi
   }&
done
wait
------
while 循環實現

#!/bin/bash
NETID=192.168.0
HOSTID=1
while [ $HOSTID -le 254 ];do
   {
    if ping -c1 -w1 "$NETID.$HOSTID" &> /dev/null;then
         echo $NETID.$HOSTID is up - success!
    else
         echo $NETID.$HOSTID is up - failed!
    fi
   }&
   let HOSTID++
done
wait

---------------------------------------
#效果
[root@client_1 ~]# ./ip.sh 
192.168.0.1 is up - success!
192.168.0.3 is up - failed!
192.168.0.5 is up - failed!
192.168.0.7 is up - failed!
192.168.0.6 is up - failed!
192.168.0.16 is up - failed!
192.168.0.12 is up - failed!
192.168.0.2 is up - failed!
192.168.0.4 is up - failed!
192.168.0.14 is up - failed!
192.168.0.19 is up - failed!
192.168.0.10 is up - failed!
.....

3、每週的工作日1:30,將/etc備份至/backup目錄中,保存的文件名稱格式 爲“etcbak-yyyy-mm-dd-HH.tar.xz”,其中日期是前一天的時間

[root@client_1 script]# cat /script/backup-etc.sh 
#!/bin/bash
#
#*************************************
#author:                giyomwd
#version:               1.0
#date:                  2020-04-12
#description:          
#*************************************
/usr/bin/tar Jcf /backup/etcbak-`date -d "1 day ago" +"%Y-%m-%d-%H"`.tar.xz /etc

[root@client_1 script]# crontab -l
30 1 * * 1-5 /usr/bin/bash /script/backup-etc.sh &> /dev/null

4、工作日時間,每10分鐘執行一次磁盤空間檢查,一旦發現任何分區利用率高 於80%,就發送郵件報警

[root@client_1 script]# cat check-disk.sh 
#!/bin/bash
#
#*************************************
#author:                giyomwd
#version:               1.0
#date:                  2020-04-12
#description:          
#*************************************
WARNDISK=($(df -h | awk -F " +|%" '/^\/dev\/sd/{if($5>=80){print $1"|space-warning:"$5"%"}}'))
if [ "$WARNDISK" ];then
   for ((i=0;i<${#WARNDISK[*]};i++));do
      echo ${WARNDISK[$i]}| mail -s diskcheck  root@localhost 
   done
fi
-----------------------------
[root@client_1 script]# crontab -l
*/10 * * * 1-5 /bin/bash /script/check-disk.sh 
----------------------------------
#效果

[root@client_1 script]# cat /var/spool/mail/root 
From root@client_1.localdomain  Sun Apr 12 01:02:39 2020
Return-Path: <root@client_1.localdomain>
X-Original-To: root@localhost
Delivered-To: [email protected]
Received: by client_1.localdomain (Postfix, from userid 0)
	id 0E7252035989; Sun, 12 Apr 2020 01:02:39 +0800 (CST)
Date: Sun, 12 Apr 2020 01:02:38 +0800
To: [email protected]
Subject: diskcheck
User-Agent: Heirloom mailx 12.5 7/5/10
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Message-Id: <20200411170239.0E7252035989@client_1.localdomain>
From: root@client_1.localdomain (root)

/dev/sda1|space-warning:87%  -->報警語句
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章