运行Amazon Linux AMI。 似乎CloudWatch不检查可用磁盘空间。 我有一些服务器,理想情况下不希望configuration每一个邮件服务器,脚本来检查磁盘空间等
有一个更简单的方法来做到这一点?
截至2012年3月,亚马逊为此提供了脚本:
适用于Linux的Amazon CloudWatch监控脚本 : http : //aws.amazon.com/code/8720044071969977
我根据需要编写了一个脚本来检查我的EC2组中的多个服务器。 它需要一个文件,每个服务器IP /域名的列表在一行上。
#! /bin/bash ADMIN="[email protected]" ALERT=85 for SERVER in `cat ~/scripts/servers.txt` do ssh -i ~/.ssh/yourkey.pem $SERVER df -H | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print $5 " " $1 }' | while read output; do echo $output usep=$(echo $output | awk '{ print $1}' | cut -d'%' -f1 ) partition=$(echo $output | awk '{ print $2 }' ) if [ $usep -ge $ALERT ]; then echo "Running out of space \"$partition ($usep%)\" on $SERVER as on $(date)" | mail -s "Alert: Almost out of disk space $usep" $ADMIN fi done done
EC2控制和监视工具无法为您提供此数据,因为实例的文件系统只能由实例本身访问 。 硬件的基本架构和安全模型都需要这个限制。 想一想,如果你的计算机之外的软件可以在你的硬盘驱动器上查看,那将会是多么糟糕!
这是一个低调的方式来使cron(安装在大多数系统无论如何)检查您的数据定期。 无论如何,您的系统应该具有处理根邮件通知的最低要求。 我build议至less有一个唯物主义的传出邮件代理,并configuration根或pipe理员别名在您pipe理的所有系统上转发给您。 包括cron
在内的许多程序都期望这种configuration
你可以添加到你的crontab中:
0 0 * * * test $(df / | grep ^/ | awk '{print $4}') -lt 1048576 && echo "Warning: Free disk space is less than 1G on /"
打破这个,这个
test
命令使用-lt
小于运算符和一个与1Gb空闲空间相等的固定值来设置简单的shell比较。 df
命令testing/
文件系统上的可用空间 grep
得到的只是你需要的输出行而不是df
包含的头文件。 awk
只是输出中的第四列,即可用空间数。 &&
表示仅在第一个命令( test x -lt y
)返回true时才运行下一个命令。 使用CloudWatch在EC2实例上进行设置的分步说明:
http://docs.aws.amazon.com/AmazonCloudWatch/latest/DeveloperGuide/mon-scripts.html
我使用这个脚本: http : //nixcraft.com/shell-scripting/3238-shell-script-check-disk-space-remote-systems.html
克朗是你的朋友。 把这个文件放到/etc/cron.daily目录下,每天运行一次:
#!/bin/sh # this script is /etc/cron.daily/diskAlert.cron ADMIN="[email protected]" ALERT=90 df -PkH | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print $5 " " $6 }' | while read output; do usep=$(echo $output | awk '{ print $1}' | cut -d'%' -f1 ) partition=$(echo $output | awk '{print $2}' ) if [ $usep -ge $ALERT ]; then echo "Running out of space \"$partition ($usep%)\" on $(hostname) as on $(date)" | mail -s "Alert: Almost out of disk space $usep%" $ADMIN fi done
注意:这个脚本会说挂载的CDROM已满。
这是我写在AWS中的DC上运行的一个快速PowerShell脚本,如果驱动器已满,则会向一组收件人发送电子邮件。 它需要一个csv 2列 – 一个名称与计算机名称,一个标题驱动器与驱动器号。 我们的AWS环境中没有邮件服务器,因此我将其configuration为通过SES发送。 您还可以稍微修改脚本,以便每隔一段时间发送一次报告(如果喜欢的话)。 只是觉得我会在这里发布,因为我发现的所有解决scheme都是针对Linux实例的。
$CSVPath = "c:\Scripts\computerNames.csv" $computerName = new-object System.Data.DataSet $computerName = Import-CSV $CSVPath $AwsUn = "" $AwsPw = ConvertTo-SecureString "" -AsPlainText -Force $cred = New-Object -typename System.Management.Automation.PSCredential -ArgumentList $AwsUn, $AwsPw Foreach($name in $computerName) { $dl = $name.drive $Utilization = Get-WmiObject win32_Volume -ComputerName $name.computerName -Filter "DriveLetter = '$($dl)'"| Foreach{ “{0:N2}” -f ((1-$_.FreeSpace / $_.Capacity)*100) } if($Utilization -gt 90) { Send-MailMessage -From Sender to Recipients -subject ( "$($name.computerName) Disk utilization" )-Body "The $dl drive on the AWS instance $($name.computerName) has $utilization% disk utilization. Please log in and delete log files or contact the Network Operations team to increase the storage allocated to this instance" -SmtpServer email-smtp.us-west-2.amazonaws.com -Credential $cred -useSSL -port 25 } }