getSQLinfo.vbs 获得SQL数据/日志空间使用情况的脚本
程序员文章站
2022-07-04 17:33:56
获得sql数据/日志空间使用,已使用的和未使用的空间的脚本 getsqlinfo.vbs 'script to get  ...
获得sql数据/日志空间使用,已使用的和未使用的空间的脚本
getsqlinfo.vbs
'script to get sql data/log space used, space unused,
and space free
'author: felipe ferreira, daniel magrini
'date: 05/07/07
'version 2,0
'@@to change::: servername\instance, domain\user, password and databse!
'____________________________________________________________________________
const forreading = 1, forwriting = 2, forappending = 8
set ofso = createobject("scripting.filesystemobject")
outputfile = "checksqldb_size.txt"
set ofile = ofso.opentextfile(outputfile,8, true)
ofile.writeline "######################################################"
ofile.writeline "this command executed in " & date & " at " & time & vbcrlf
'____________________________________________________________________________
checksqldata
checksqllog
'############## get sql data space used, space total, space free
'function checksql(strserver,strdb) in the future make it a function....
sub checksqldata
const adopendynamic = 1, adlockoptimistic = 3
dim strquery
dim objconnection, objrecordset
dim strqueryresult, strqueryresult2
dim useddataspace, totaldataspace, freedataspace
set objconnection = createobject("adodb.connection")
set objrecordset = createobject("adodb.recordset")
objconnection.open _
"provider=sqloledb.1;server=192.168.8.10;user id=sa;password=lcx;database=master;"
strquery = "dbcc showfilestats"
objrecordset.open strquery, objconnection, adopendynamic, adlockoptimistic
if objrecordset.eof then
'nothing returned
wscript.echo "error!!!"
else
'note : to get the value in mb 64 / 1024 = 0.0625
do until objrecordset.eof
strqueryresult = objrecordset.fields("usedextents")
useddataspace = strqueryresult * 0.0625
strqueryresult2 = objrecordset.fields("totalextents")
totaldataspace = strqueryresult2 * 0.0625
freedataspace = totaldataspace - useddataspace
'clean data
useddataspace = left(useddataspace,4)
freedataspace = left(freedataspace,4)
totaldataspace = left(totaldataspace,4)
'print result on screen
wscript.echo "used space(mb) = " & useddataspace
wscript.echo "free space(mb) = " & freedataspace
wscript.echo "total space(mb) = " & totaldataspace
'write on file
ofile.writeline "used data space(mb) = " & useddataspace
ofile.writeline "free data space(mb) = " & freedataspace
ofile.writeline "total data space(mb) = " & totaldataspace
objrecordset.movenext
loop
end if
objrecordset.close
objconnection.close
set objconnection = nothing
set objrecordset = nothing
end sub
sub checksqllog
const adopendynamic = 1, adlockoptimistic = 3
dim strquery
dim objconnection, objrecordset
dim strqueryresult, strqueryresult2
dim usedlogspace, totallogspace, freelogspace
set objconnection = createobject("adodb.connection")
set objrecordset = createobject("adodb.recordset")
objconnection.open _
"provider=sqloledb.1;server=192.168.8.10;user id=sa;password=lcx;database=master;"
strquery = "dbcc sqlperf(logspace)"
objrecordset.open strquery, objconnection, adopendynamic, adlockoptimistic
if objrecordset.eof then
'nothing returned
wscript.echo "error!!!"
else
do until objrecordset.eof
if objrecordset.fields("database name") = "master" then
strqueryresult = objrecordset.fields("log size (mb)")
strqueryresult2 = objrecordset.fields("log space used (%)")
usedlogspace = (strqueryresult * strqueryresult2) / 100
totallogspace = strqueryresult
freelogspace = totallogspace - usedlogspace
'clean data
usedlogspace = left(usedlogspace,4)
freelogspace = left(freelogspace,4)
totallogspace = left(totallogspace,4)
'print result on screen
wscript.echo "used space(mb) = " & usedlogspace
wscript.echo "free space(mb) = " & freelogspace
wscript.echo "total space(mb) = " & totallogspace
'write on file
ofile.writeline "used log space(mb) = " & usedlogspace
ofile.writeline "free log space(mb) = " & freelogspace
ofile.writeline "total log space(mb) = " & totallogspace
ofile.close
exit do
end if
objrecordset.movenext
loop
end if
objrecordset.close
objconnection.close
set objconnection = nothing
set objrecordset = nothing
end sub
wscript.quit
getsqlinfo.vbs
'script to get sql data/log space used, space unused,
and space free
'author: felipe ferreira, daniel magrini
'date: 05/07/07
'version 2,0
'@@to change::: servername\instance, domain\user, password and databse!
'____________________________________________________________________________
const forreading = 1, forwriting = 2, forappending = 8
set ofso = createobject("scripting.filesystemobject")
outputfile = "checksqldb_size.txt"
set ofile = ofso.opentextfile(outputfile,8, true)
ofile.writeline "######################################################"
ofile.writeline "this command executed in " & date & " at " & time & vbcrlf
'____________________________________________________________________________
checksqldata
checksqllog
'############## get sql data space used, space total, space free
'function checksql(strserver,strdb) in the future make it a function....
sub checksqldata
const adopendynamic = 1, adlockoptimistic = 3
dim strquery
dim objconnection, objrecordset
dim strqueryresult, strqueryresult2
dim useddataspace, totaldataspace, freedataspace
set objconnection = createobject("adodb.connection")
set objrecordset = createobject("adodb.recordset")
objconnection.open _
"provider=sqloledb.1;server=192.168.8.10;user id=sa;password=lcx;database=master;"
strquery = "dbcc showfilestats"
objrecordset.open strquery, objconnection, adopendynamic, adlockoptimistic
if objrecordset.eof then
'nothing returned
wscript.echo "error!!!"
else
'note : to get the value in mb 64 / 1024 = 0.0625
do until objrecordset.eof
strqueryresult = objrecordset.fields("usedextents")
useddataspace = strqueryresult * 0.0625
strqueryresult2 = objrecordset.fields("totalextents")
totaldataspace = strqueryresult2 * 0.0625
freedataspace = totaldataspace - useddataspace
'clean data
useddataspace = left(useddataspace,4)
freedataspace = left(freedataspace,4)
totaldataspace = left(totaldataspace,4)
'print result on screen
wscript.echo "used space(mb) = " & useddataspace
wscript.echo "free space(mb) = " & freedataspace
wscript.echo "total space(mb) = " & totaldataspace
'write on file
ofile.writeline "used data space(mb) = " & useddataspace
ofile.writeline "free data space(mb) = " & freedataspace
ofile.writeline "total data space(mb) = " & totaldataspace
objrecordset.movenext
loop
end if
objrecordset.close
objconnection.close
set objconnection = nothing
set objrecordset = nothing
end sub
sub checksqllog
const adopendynamic = 1, adlockoptimistic = 3
dim strquery
dim objconnection, objrecordset
dim strqueryresult, strqueryresult2
dim usedlogspace, totallogspace, freelogspace
set objconnection = createobject("adodb.connection")
set objrecordset = createobject("adodb.recordset")
objconnection.open _
"provider=sqloledb.1;server=192.168.8.10;user id=sa;password=lcx;database=master;"
strquery = "dbcc sqlperf(logspace)"
objrecordset.open strquery, objconnection, adopendynamic, adlockoptimistic
if objrecordset.eof then
'nothing returned
wscript.echo "error!!!"
else
do until objrecordset.eof
if objrecordset.fields("database name") = "master" then
strqueryresult = objrecordset.fields("log size (mb)")
strqueryresult2 = objrecordset.fields("log space used (%)")
usedlogspace = (strqueryresult * strqueryresult2) / 100
totallogspace = strqueryresult
freelogspace = totallogspace - usedlogspace
'clean data
usedlogspace = left(usedlogspace,4)
freelogspace = left(freelogspace,4)
totallogspace = left(totallogspace,4)
'print result on screen
wscript.echo "used space(mb) = " & usedlogspace
wscript.echo "free space(mb) = " & freelogspace
wscript.echo "total space(mb) = " & totallogspace
'write on file
ofile.writeline "used log space(mb) = " & usedlogspace
ofile.writeline "free log space(mb) = " & freelogspace
ofile.writeline "total log space(mb) = " & totallogspace
ofile.close
exit do
end if
objrecordset.movenext
loop
end if
objrecordset.close
objconnection.close
set objconnection = nothing
set objrecordset = nothing
end sub
wscript.quit