如何监控linux阿里云磁盘空间
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何监控linux阿里云磁盘空间相关的知识,希望对你有一定的参考价值。
Linux系统中需要监控磁盘各分区的使用情 况,避免由于各种突发情况,造成磁盘空间被消耗殆尽的情况,例如某个分区被Oracle的归档日志耗尽,导致后续的日志文件无法归档,这时ORACLE数 据库就会出现错误。监控磁盘空间的使用情况,其实有许多工具,例如Nagios等,其实最简单的还是使用Shell脚本。下面就介绍一下如何通过 Shell脚本和Crontab作业结合来实现对磁盘空间的监控、告警。一般查看磁盘各分区的使用情况可以通过df命令来查看,网上有两种获取磁盘使用百分比的Shell脚本。
1:df -h | grep /dev | awk 'print $5' | cut -f 1 -d "%"
2:df -h | grep /dev | awk 'print $5' | sed 's/%//g'
但是这两个命令还是有些bug,例如如下截图所示:当显示内容过长,导致换行时。此时上面的Shell脚本就无法获取其值。
尤其是某些特殊情况下,完全无法准确获取相应数据
此时只需在参数上稍微做一下调整即可
1:df -P | grep /dev | awk 'print $5' | cut -f 1 -d "%"
2:df -P | grep /dev | awk 'print $5' | sed 's/%//g'
下面脚本是用来监控各分区使用情况,当超过阀值(默认为90%)时,发出告警邮件,通知管理员及时处理。
#*************************************************************************
# FileName : disk_capatiy_alarm.sh
#*************************************************************************
# Author : Kerry
# CreateDate : 2013-11-07
# Description : this script is mointoring the linux disk
# capacity, if disk used more than 90%,
# then it will send a alarm email
#*************************************************************************
#! /bin/bash
email_content="/home/oracle/scripts/output/disk_sendmail.pl"
email_logfile="/home/oracle/scripts/output/diskdetail.txt";
cat /dev/null > $email_content;
cat /dev/null > $email_logfile;
SendMail()
date_today=`date +%Y_%m_%d`
subject="The server xxxxxx\'s Disk Capacity Alarm"
content="Dear All,
The server xxxx(xxx.xxx.xxx.xxx) disk capacity alarm ,please take action for it. many thanks!
"
echo "#!/usr/bin/perl" >> $email_content
echo "use Mail::Sender;" >> $email_content
echo "\$sender = new Mail::Sender smtp => 'xxx.xxx.xxx.xxx', from => 'xxx@xxx.com'; ">> $email_content
echo "\$sender->MailFile(to => 'kerry@xxx.com',">> $email_content
echo "cc=>'xxxx@xxxx.com'," >> $email_content
echo "subject => '$subject',">> $email_content
echo "msg => '$content',">> $email_content
echo "file => '$email_logfile');">> $email_content
perl $email_content
for d in `df -P | grep /dev | awk 'print $5' | sed 's/%//g'`
do
if [ $d -gt 90 ]; then
df -h >>$email_logfile;
SendMail;
exit 0;
fi
done 参考技术A 直接去yun.cloudbility.com,导入主机,监控图表一目了然,并且还能设置微信报警
Linux/Unix shell 脚本监控磁盘可用空间
Linux下监控磁盘的空闲空间的shell脚本,对于系统管理员或DBA来说,必不可少。下面是给出的一个监控磁盘空间空间shell脚本的样本,供大家参考。
1、监控磁盘的空闲空间shell脚本
- [email protected]:~/dba_scripts/custom/bin> more ck_fs_space.sh
- #!/bin/bash
- # ------------------------------------------------------------------------------+
- # CHECK FILE SYSYTEM SPACE BY THRESHOLD |
- # Filename: ck_fs_space.sh |
- # Desc: |
- # The script use to check file system space by threshold |
- # Once usage of the disk beyond the threshold, a mail alert will be sent. |
- # Deploy it by crontab. e.g. per 15 min |
- # Usage: |
- # ./ck_fs_space.sh <percent> </filesystem [/filesystem2]> |
- # |
- # Author : Robinson |
- # Blog : http://blog.csdn.net/robinson_0612 |
- # ------------------------------------------------------------------------------+
- #
- # -------------------------------
- # Set environment here
- # ------------------------------
- if [ -f ~/.bash_profile ]; then
- . ~/.bash_profile
- fi
- export host=`hostname`
- export mail_dir=/users/robin/dba_scripts/sendEmail-v1.56
- export mail_list=‘[email protected]‘
- export mail_fm=‘[email protected]‘
- tmpfile=/tmp/ck_fs_space.txt
- alert=n
- # --------------------------------
- # Check the parameter
- # --------------------------------
- max=$1
- if [ ! ${2} ]; then
- echo "No filesystems specified."
- echo "Usage: ck_fs_space.sh 90 / /u01"
- exit 1
- fi
- # --------------------------------
- # Start to check the disk space
- # --------------------------------
- while [ "${2}" ]
- do
- percent=`df -P ${2} | tail -1 | awk ‘{print $5 }‘ | cut -d‘%‘ -f1`
- if [ "${percent}" -ge "${max}" ]; then
- alert=y
- break
- fi;
- shift
- done;
- # ------------------------------------------------------------------------
- # When a partition was above the threshold then send mail with df output
- # ------------------------------------------------------------------------
- if [ ! "${alert}" = ‘n‘ ];then
- df -h >$tmpfile
- mail_sub="Disk usage beyond the threshold ${max} on ${host}."
- $mail_dir/sendEmail -u ${mail_sub} -f $mail_fm -t $mail_list -o message-file=${tmpfile}
- fi;
- exit;
2、脚本说明
a、该脚本使用了 sendEmail 工具来发送邮件。
b、使用方式为"Usage: ck_fs_space.sh 90 / /u01" 。
c、脚本中使用了一个while循环来逐个判断所有的指定分区的空闲空间是否超出阙值。
d、对于超出阙值的情形发送邮件并且附上当前服务器上磁盘空间的使用情况。
转:http://blog.csdn.net/leshami/article/details/8893943
以上是关于如何监控linux阿里云磁盘空间的主要内容,如果未能解决你的问题,请参考以下文章