Created
January 20, 2014 03:02
-
-
Save kevinbin/8514215 to your computer and use it in GitHub Desktop.
some os resource monitor function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
function GetPID #User #Name | |
{ | |
PsUser=$1 | |
PsName=$2 | |
pid=`ps -u $PsUser|grep $PsName|grep -v grep|grep -v vi|grep -v dbx \ | |
|grep -v tail|grep -v start|grep -v stop |sed -n 1p |awk '{print $1}'` | |
echo $pid | |
} | |
function GetCpu | |
{ | |
CpuValue=`ps -p $1 -o pcpu |grep -v CPU | awk '{print $1}' | awk -F. '{print $1}'` | |
echo $CpuValue | |
} | |
function CheckCpu | |
{ | |
local PID=$1 | |
local THRESHOLD=$2 | |
cpu=`GetCpu $PID` | |
if [ $cpu -gt $THRESHOLD ]; then | |
echo "The usage of cpu is larger than ${THRESHOLD}%" | |
else | |
echo "The usage of cpu is normal" | |
fi | |
} | |
function GetMem | |
{ | |
MEMUsage=`ps -o vsz -p $1|grep -v VSZ` | |
(( MEMUsage /= 1000)) | |
echo $MEMUsage | |
} | |
function CheckMem | |
{ | |
local PID=$1 | |
local THRESHOLD=$2 | |
mem=`GetMem $PID` | |
if [ $mem -gt $THRESHOLD ]; then | |
echo “The usage of memory is larger than ${THRESHOLD}M” | |
else | |
echo “The usage of memory is normal” | |
fi | |
} | |
function GetDes | |
{ | |
DES=`ls /proc/$1/fd | wc -l` | |
echo $DES | |
} | |
function CheckDes | |
{ | |
local PID=$1 | |
local THRESHOLD=$2 | |
des=`GetDes $PID` | |
if [ $des -gt $THRESHOLD ]; then | |
echo "The number of des is larger than $THRESHOLD" | |
else | |
echo "The number of des is normal" | |
fi | |
} | |
function Listening | |
{ | |
Listeningnum=`ss -anl | grep ":$1 " | wc -l` | |
if [ $Listeningnum == 0 ]; then | |
echo "0" | |
else | |
echo "1" | |
fi | |
} | |
function CheckPort | |
{ | |
local PORT=$1 | |
isListen=`Listening $PORT` | |
if [ $isListen -eq 1 ]; then | |
echo "The port is listening" | |
else | |
echo "The port is not listening" | |
fi | |
} | |
function GetSysCPU | |
{ | |
CpuIdle=`vmstat 1 5 |sed -n '3,$p' \ | |
|awk '{x = x + $15} END {print x/5}' |awk -F. '{print $1}'` | |
CpuNum=`echo "100-$CpuIdle" | bc` | |
echo $CpuNum | |
} | |
function CheckSysCPU | |
{ | |
local THRESHOLD=$1 | |
cpu=`GetSysCPU` | |
echo "The system CPU is $cpu" | |
if [ $cpu -gt ${THRESHOLD} ];then | |
echo "The usage of system cpu is larger than ${THRESHOLD}%" | |
else | |
echo "The usage of system cpu is normal" | |
fi | |
} | |
function GetDiskSpc | |
{ | |
if [ $# -ne 1 ]; then | |
return 1 | |
fi | |
Folder="$1$" | |
DiskSpace=`df -k |grep $Folder |awk '{print $5}' |awk -F% '{print $1}'` | |
echo $DiskSpace | |
} | |
function CheckDiskSpc | |
{ | |
local Mount=$1 | |
local THRESHOLD=$2 | |
DiskSpace=`GetDiskSpc $Mount` | |
echo "The system $Mount disk space is $DiskSpace%" | |
if [ $DiskSpace -gt $THRESHOLD ]; then | |
echo "The usage of system disk($Mount) is larger than ${THRESHOLD}%" | |
else | |
echo "The usage of system disk($Mount) is normal" | |
fi | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment