`
wsql
  • 浏览: 11777501 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
文章分类
社区版块
存档分类
最新评论

使用shell脚本进行服务器系统监控——页面调度与交换空间监控

 
阅读更多

#!/usr/bin/ksh


PC_LIMIT=65 # Upper limit of Swap space percentage
# before notification

THISHOST=$(hostname) # Host name of this machine

echo "/nSwap Space Report for $THISHOST/n"
date

function SUN_swap_mon
{
SW_USED=$(swap -s | awk '{print $9}' | cut -dk -f1)
SW_FREE=$(swap -s | awk '{print $11}' | cut -dk -f1)
((SW_TOTAL = SW_USED + SW_FREE))
PERCENT_USED=$(bc <<EOF
scale=4
($SW_USED / $SW_TOTAL) * 100
EOF
)

PERCENT_FREE=$(bc <<EOF
scale=4
($SW_FREE / $SW_TOTAL) * 100
EOF
)

# Convert the KB measurements to MB measurements

((SW_TOTAL_MB = SW_TOTAL / 1000))
((SW_USED_MB = SW_USED / 1000))
((SW_FREE_MB = SW_FREE / 1000))

# Produce the remaining part of the report

echo "/nTotal Amount of Swap Space:/t${SW_TOTAL_MB}MB"
echo "Total KB of Swap Space Used:/t${SW_USED_MB}MB"
echo "Total KB of Swap Space Free:/t${SW_FREE_MB}MB"
echo "/nPercent of Swap Space Used:/t${PERCENT_USED}%"
echo "/nPercent of Swap Space Free:/t${PERCENT_FREE}%"

# Grab the integer portion of the percent used

INT_PERCENT_USED=$(echo $PERCENT_USED | cut -d. -f1)

# Check to see if the percentage used maxmum threshold
# has beed exceeded

if (( PC_LIMIT <= INT_PERCENT_USED ))
then
# Percent used has exceeded the threshold, send notification

tput smso # Turn on reverse video!
echo "/n/nWARNING: Swap Space has Exceeded the ${PC_LIMIT}% Upper Limit!/n"
tput rmso # Turn off reverse video!
fi

echo "/n"
}


function Linux_swap_mon
{

free -m | grep -i swap | while read junk SW_TOTAL SW_USED SW_FREE
do
PERCENT_USED=$(bc <<EOF
scale=4
($SW_USED / $SW_TOTAL) * 100
EOF
)

PERCENT_FREE=$(bc <<EOF
scale=4
($SW_FREE / $SW_TOTAL) * 100
EOF
)

# Produce the rest of the paging space report:
echo "/nTotal Amount of Swap Space:/t${SW_TOTAL}MB"
echo "Total KB of Swap Space Used:/t${SW_USED}MB"
echo "Total KB of Swap Space Free:/t${SW_FREE}MB"
echo "/nPercent of Swap Space Used:/t${PERCENT_USED}%"
echo "/nPercent of Swap Space Free:/t${PERCENT_FREE}%"

# Grap the integer portion of the percent used to
# test for the over limit threshold

INT_PERCENT_USED=$(echo $PERCENT_USED | cut -d. -f1)

if (( PC_LIMIT <= INT_PERCENT_USED ))
then
tput smso
echo "/n/nWARNING: Paging Space has Exceeded the /
${PC_LIMIT}% Upper Limit!/n"
tput rmso
fi

done

echo "/n"
}

###########################################################

function HP_UX_swap_mon
{
############# CAPTURE AND PROCESS THE DATA ################

# Start a while read loop by using the piped in input from
# the swapinfo -tm command output.


swapinfo -tm | grep dev | while read junk SW_TOTAL SW_USED /
SW_FREE PERCENT_USED junk2
do
# Calculate the percentage of free swap space

((PERCENT_FREE = 100 - $(echo $PERCENT_USED | cut -d% -f1) ))

echo "/nTotal Amount of Swap Space:/t${SW_TOTAL}MB"
echo "Total MB of Swap Space Used:/t${SW_USED}MB"
echo "Total MB of Swap Space Free:/t${SW_FREE}MB"
echo "/nPercent of Swap Space Used:/t${PERCENT_USED}"
echo "/nPercent of Swap Space Free:/t${PERCENT_FREE}%"

# Check for paging space exceeded the predefined limit

if (( PC_LIMIT <= $(echo $PERCENT_USED | cut -d% -f1) ))
then
# Swap space is over the predefined limit, send notification

tput smso # Turn on reverse video!
echo "/n/nWARNING: Swap Space has Exceeded the/
${PC_LIMIT}% Upper Limit!/n"
tput rmso # Turn reverse video off!
fi

done

echo "/n"
}

###########################################################

function AIX_paging_mon
{
################ DEFINE VARIABLES HERE ####################

PAGING_STAT=/tmp/paging_stat.out # Paging Stat hold file

###########################################################
############# CAPTURE AND PROCESS THE DATA ################

# Load the data in a file without the column headings

lsps -s | tail +2 > $PAGING_STAT

# Start a while loop and feed the loop from the bottom using
# the $PAGING_STAT file as redirected input

while read TOTAL PERCENT
do
# Clean up the data by removing the suffixes
PAGING_MB=$(echo $TOTAL | cut -d 'MB' -f1)
PAGING_PC=$(echo $PERCENT | cut -d% -f1)

# Calculate the missing data: %Free, MB used and MB free
(( PAGING_PC_FREE = 100 - PAGING_PC ))
(( MB_USED = PAGING_MB * PAGING_PC / 100 ))
(( MB_FREE = PAGING_MB - MB_USED ))

# Produce the rest of the paging space report:
echo "/nTotal MB of Paging Space:/t$TOTAL"
echo "Total MB of Paging Space Used:/t${MB_USED}MB"
echo "Total MB of Paging Space Free:/t${MB_FREE}MB"
echo "/nPercent of Paging Space Used:/t${PERCENT}"
echo "/nPercent of Paging Space Free:/t${PAGING_PC_FREE}%"

# Check for paging space exceeded the predefined limit
if ((PC_LIMIT <= PAGING_PC))
then
# Paging space is over the limit, send notification

tput smso # Turn on reverse video!

echo "/n/nWARNING: Paging Space has Exceeded the ${PC_LIMIT}% /
Upper Limit!/n"

tput rmso # Turn off reverse video
fi

done < $PAGING_STAT

rm -f $PAGING_STAT

# Add an extra new line to the output

echo "/n"
}

###########################################################
################## BEGINNING OF MAIN ######################
###########################################################

###########################################################
################ DEFINE VARIABLES HERE ####################

PC_LIMIT=65 # Upper limit of Swap space percentage
# before notification

THISHOST=$(hostname) # Host name of this machine

###########################################################

# Find the Operating System and execute the correct function

case $(uname) in

AIX) AIX_paging_mon
;;
HP-UX) HP_UX_swap_mon
;;
Linux) Linux_swap_mon
;;
SunOS) SUN_swap_mon
;;
esac

# End of all-in-one_swapmon.ksh

分享到:
评论

相关推荐

    精通UNIX Shell脚本编程(附源代码)

    ● 针对文件系统、页面调度/交换空间、系统负载、应用程序、进程的信息收集与监视活动, 捕获系统配置 ● 打印队列管理,保证打印机正常打印 ● 使用bc工具程序在shell脚本中进行浮点数学运算 本书配套的Web网站...

    Mastering Unix Shell Scripting

    针对文件系统、页面调度/交换空间、系统负载、应用程序、进程的信息收集与监视活动, 捕获系统配置 ● 打印队列管理,保证打印机正常打印 ● 使用bc工具程序在shell脚本中进行浮点数学运算 本书...

    RED HAT LINUX 6大全

    11.10.1 使用脚本对DNS设置进行耐 压测试 222 11.10.2 使用转储和日志调试 223 11.11 其他DNS文档 223 11.12 小结 224 第12章 NIS:网络信息服务 225 12.1 了解NIS 225 12.1.1 NIS域 226 12.1.2 不同的服务器 226 ...

    精通Windows.API-函数、接口、编程实例.pdf

    第3章 开发工具配置与使用 26 3.1 使用Visual C/C++编译链接工具 26 3.1.1 编译器cl.exe 27 3.1.2 资源编译器rc.exe 31 3.1.3 链接器link.exe 32 3.1.4 其他工具 38 3.1.5 编译链接工具依赖的环境变量...

    深入分析Linux内核源码

    13.6.2 启动所需的Shell脚本文件 附录: 1 Linux 2.4内核API 2.1 驱动程序的基本函数 2.2 双向循环链表的操作 2.3 基本C库函数 2.4 Linux内存管理中Slab缓冲区 2.5 Linux中的VFS 2.6 Linux的连网 2.7 ...

    精通WindowsAPI 函数 接口 编程实例

    第3章 开发工具配置与使用 26 3.1 使用Visual C/C++编译链接工具 26 3.1.1 编译器cl.exe 27 3.1.2 资源编译器rc.exe 31 3.1.3 链接器link.exe 32 3.1.4 其他工具 38 3.1.5 编译链接工具依赖的环境变量...

    java开源包8

    Port Groper可以与用测试防火墙,干扰web 统计脚本的跟踪,为网站增加流量..往好了用什么都能干,就是不能让一个网站下线。 FTP客户端Java类库 ftp4j ftp4j是一个FTP客户端Java类库,实现了FTP客户端应具有的大部分...

    JAVA上百实例源码以及开源项目源代码

    2个目标文件,FTP的目标是:(1)提高文件的共享性(计算机程序和/或数据),(2)鼓励间接地(通过程序)使用远程计算机,(3)保护用户因主机之间的文件存储系统导致的变化,(4)为了可靠和高效地传输,虽然用户...

    JAVA上百实例源码以及开源项目

    2个目标文件,FTP的目标是:(1)提高文件的共享性(计算机程序和/或数据),(2)鼓励间接地(通过程序)使用远程计算机,(3)保护用户因主机之间的文件存储系统导致的变化,(4)为了可靠和高效地传输,虽然用户...

    ARM_Linux启动分析.pdf

    一般情况下,rc启动脚本都位于/etc/rc.d目录下,rc.sysinit中最常见的动作就是激活交换分区,检查磁盘,加载硬件模块,这些动作无论哪个运行级别都是需要优先执行的。仅当rc.sysinit执行完以后init才会执行其他的...

    java开源包1

    Port Groper可以与用测试防火墙,干扰web 统计脚本的跟踪,为网站增加流量..往好了用什么都能干,就是不能让一个网站下线。 FTP客户端Java类库 ftp4j ftp4j是一个FTP客户端Java类库,实现了FTP客户端应具有的大部分...

Global site tag (gtag.js) - Google Analytics