如何创建作业(或脚本或?):
检查磁盘空间;当磁盘空间仅剩1.5 GB时,然后向一些最终用户发送警报电子邮件。
Windows Edition:Windows Server 2008 R2 Standard 服务器类型:SQL Server数据库。
我没有使用Windows或基于Windows的技术的经验。做了一些搜索并找到了任务调度程序但无法确定如何应用预期条件。
答案 0 :(得分:1)
使用Windows Server
和Task Scheduler
在System Log
中执行此操作。如果可用空间低于 HKLM \ SYSTEM \ CurrentControlSet \ Services \ LanmanServer \ Parameters \ DiskSpaceThreshold 中指定的百分比,则会在System Log
中记录一个可触发任务发送的事件电子邮件。您可以按照以下步骤进行操作 -
DiskSpaceThreshold
的 DWORD 值,将其设置为所需的百分比。当条目不存在时,默认值为10. 如果要通过SQL Server
执行此操作,则可以编写脚本,然后通过作业定期运行脚本,并在磁盘空间交叉时定期运行并发送警报/邮件(使用msdb.dbo.sp_send_dbmail
)门槛值。
示例查询 -
SELECT DISTINCT DB_NAME(dovs.database_id) DBName,
mf.physical_name PhysicalFileLocation,
dovs.logical_volume_name AS LogicalName,
dovs.volume_mount_point AS Drive,
CONVERT(INT,dovs.available_bytes/1048576.0) AS FreeSpaceInMB
FROM sys.master_files mf
CROSS APPLY sys.dm_os_volume_stats(mf.database_id, mf.FILE_ID) dovs
ORDER BY FreeSpaceInMB ASC
检查列FreeSpaceInMB
另一个查询 -
EXEC master..xp_fixeddrives
检查列MBfree
修改强>
通过Sql Server
链接有关如何完成此操作的详细信息 -
link
答案 1 :(得分:0)
我正在寻找一个类似的解决方案,正在将我的头撞在墙上。经过大量的搜索,我确信自己能够自己做到这一点。这需要3个脚本,1个bat文件和2个vbs脚本(因为我实际上在警报之前发出警告)。警告发送到电子邮件,可以在下班后忽略,但警报通过电子邮件发送到手机短信网关(我最终必须努力获得直接的短信解决方案)。
让我们开始吧。这是bat文件,用于确定相关磁盘上有多少可用空间:
@echo off
:: Copyright Lou Spinuso 2017
:: This script calculates the amount of free space on a disk and returns the percentage
:: This can be used with a task scheduler job to verify that more than 10%
:: (or prefered threshold) of disk is available and then set to send alerts if it fails
:: You will require setting the path to where grep and awk exists as these are required
:: in order to extract the values necessary
:: alternatively you can use find and for to get the right values
:: this is needed to enable setting some variables in the IF statement
setlocal EnableDelayedExpansion
:: SCRIPT_HOME = home directory for script and VB Script
:: DISK_TEST = Disk to test
:: GNU_TOOLS = location of grep and awk
:: DTFile = log file written with total disk size
:: DFFile = log file written with free disk space
set SCRIPT_HOME=C:\Monitor\Disk
set DISK_TEST=C:
set GNU_TOOLS=c:\gnu_tools\bin
set DTFile=C:\Monitor\disktotal.log
set DFFile=C:\Monitor\diskfree.log
set WarnValue=10
set AlertValue=5
set PATH=%PATH%;%GNU_TOOLS%
::fsutil is a built-in windows application
fsutil volume diskfree %DISK_TEST%|grep -i "total # of bytes" |awk "{print $6}" >%DTFile%
fsutil volume diskfree %DISK_TEST%|grep -i "total # of free bytes" |awk "{print $7}" >%DFFile%
:: setting variables from files since batch files can't do it
:: directly from a command that I've been able to find
set /P DiskTotal= < %DTFile%
set /P DiskFree= < %DFFile%
:: call to function that calculates the length of the variables
:: for disk size. This is because set /a will only calcuate
:: 32 bits or up to 2,147,483,647, or 2 GB
:: and fsutil returns number of bytes which can get up to TB's
:: Using this we can effectively chop the right most digits
:: effectively dividing by orders of magnitude later
call :strlen DiskTotalLen DiskTotal
call :strlen DiskFreeLen DiskFree
::echo DiskTotalLen= %DiskTotalLen%
::echo DiskFreeLen = %DiskFreeLen%
:: setting variables just in case we are working with small disks
set DFLLeftMost=%DiskFreeLen%
set DTLLeftMost=%DiskTotalLen%
:: if statement that checks if numbers we're looking at are
:: 9,999,999 or smaller. This is because we need to make the
:: disk free variable 2 oom > than the disk size variable
:: in order to have returned a whole number
:: (no decimal values are returned)
:: also, as a result of using "setlocal EnableDelayedExpansion"
:: we should use ! instead of % in the if statement
if %DiskTotalLen% GTR 7 (
set /a Subtractor=!DiskTotalLen! - 7
set /a DTLLeftMost=!DiskTotalLen!-!Subtractor!
set /a DFLLeftMost=!DiskFreeLen!-!Subtractor!+2
)
:: dividing values by oom
call set DiskFree=%%DiskFree:~0,%DFLLeftMost%%%
:: echo %DiskFree%
call set DiskTotal=%%DiskTotal:~0,%DTLLeftMost%%%
:: echo %DiskTotal%
:: set value in text file for reading
set /a DiskPercent=%DiskFree%/%DiskTotal%
if %DiskPercent% LSS %WarnValue% (
if %DiskPercent% LSS %AlertValue% (cscript.exe %SCRIPT_HOME%\Alert.vbs "!COMPUTERNAME!" "!DiskPercent!"
) ELSE (cscript.exe %SCRIPT_HOME%\Warn.vbs "!COMPUTERNAME!" "!DiskPercent!")
)
::echo %DiskPercent% > c:\monitor\diskpercent.log
::echo %DiskPercent%
goto :eof
:: function to count the length of a variable. Not sure what this all
:: means, this was written by 'jeb' on stackoverflow
:: ht tp://stack overflow.com/questions/5837418/how-do-you-get-the-string-length-in-a-batch-file
:strlen <resultVar> <stringVar>
(
setlocal EnableDelayedExpansion
set "s=!%~2!#"
set "len=0"
for %%P in (4096 2048 1024 512 128 64 32 16 8 4 2 1) do (
if "!s:~%%P,1!" NEQ "" (
set /a "len+=%%P"
set "s=!s:~%%P!"
)
)
)
(
endlocal
set "%~1=%len%"
exit /b
)
从这里我们现在需要发送电子邮件的vbs脚本。这是&#34;警报&#34; vbs脚本命名,恰当,&#34; alert.vbs&#34;。第二个vbs,几乎与这个命名相同,&#34; warn.vbs&#34;也是必需的。您必须确保正确设置
的值也是邮件服务器的端口,如果它不是25。
if WScript.Arguments.Count = 0 then
WScript.Echo "Missing Parameters"
end if
Set objEmail = CreateObject("CDO.Message")
objEmail.From = "Monitor@yourcompany.com"
objEmail.To = "supportguyspager@yourcompany.com"
objEmail.Subject = "ALERT!!!! Server "+WScript.Arguments(0)+" is low on disk space"
objEmail.Textbody = "Critical disk space issue. Please clear some space on C drive on server . " + WScript.Arguments(0)
objEmail.Textbody = objEmail.Textbody + vbCrLf + "Server is at " + WScript.Arguments(1) +" percent free Disk Space"
objEmail.Configuration.Fields.Item ("ht tp://schemas.micro soft.com/cdo/configuration/sendusing")=2
'Name or IP of remote SMTP server
objEmail.Configuration.Fields.Item ("ht tp://schemas.micro soft.com/cdo/configuration/smtpserver")="mail.yourcompany.com"
'Server port
objEmail.Configuration.Fields.Item ("ht tp://schemas.micro soft.com/cdo/configuration/smtpserverport")=25
objEmail.Configuration.Fields.Update
objEmail.Send
set objEmail=nothing
然后我将这些设置为每15分钟在任务计划程序中的服务器上运行,当磁盘可用空间低于阈值(百分比)时,会发出警告或警报。
希望我能够为其他正在寻找类似解决方案的人提供足够好的回答。
最后一点,我不得不打破&#34;网址&#34;在代码中因为我没有足够的声誉来发布它们。你应该能够删除空白和派对。