Linux运维处理及监控脚本

分享场景一:运维过程通过脚本批量删除文件。

运维时遇到在tmp文件目录下存放大最的 option__*的临时文件,由于要在晚上进行自动清理,只能考虑采用脚本操作方法,脚本设计考虑到执行效率,每次执行时负载等影响因素。综合各方面的因素把脚本设计如下:

#!/bin/sh 

cd /tmp  

    time=’date – d “ 2 day ago” “+%b%d”

 ls –l | grep “option” | grep “$ time” | awk ‘ { print $NF }’ | xargs rm –rf

 

通过自动操作以上脚本把tmp目录下 option开头的临时文件进行自动清理。

 

分享场景二:运维监控主机的磁盘空间,当使用空间超过90%就通过发mail来发警告给运维人员

 

#!/bin/bash

#monitor available disk space

SPACE='df | sed -n '/ \ / $ / p' | gawk '{print $5}' | sed's/%//'

if [ $SPACE -ge 90 ]

then

[email protected]

fi

该脚本通过对$SPACE -ge 90分析判断目前硬盘空间是否超过90%,在超过阀值时,向运维管理员邮箱[email protected] 发邮件并告知。

 

分享场景三:监控主机网卡流量

 

日常运维时如不及时掌握主机的网卡流量,当遇到主机网卡流量过大,会造成服务器网卡故障及很多不可预估问题发生 ,通过以下脚本可以有效率监控服务器网卡的流量情况,并通过脚本speedrx_after-speedrx_before查出speedrx_result是瞬时流量,及speedtx_after-speedtx_before查出speedtx_result是超出流量。

#!/bin/bash

#network

#Mike.Xu

while : ; do

speedtime='date +%m"-"%d" "%k":"%M'

speedday='date +%m"-"%d'

speedrx_before='ifconfig eth0|sed -n "8"p|awk '{print $2}'|cut -c7-'

speedtx_before='ifconfig eth0|sed -n "8"p|awk '{print $6}'|cut -c7-'

sleep 2

speedrx_after='ifconfig eth0|sed -n "8"p|awk '{print $2}'|cut -c7-'

speedtx_after='ifconfig eth0|sed -n "8"p|awk '{print $6}'|cut -c7-'

speedrx_result=$[(speedrx_after-speedrx_before)/256]

speedtx_result=$[(speedtx_after-speedtx_before)/256]

echo"$speedday$speedtime Now_In_Speed: "$speedrx_result"kbps Now_OUt_Speed: "$speedtx_result"kbps"

sleep 2

done





猜你喜欢

转载自blog.csdn.net/GGxiaobai/article/details/80377235