Free space Monitoring

Easy monitoring
* Včera jsem narazil na skvělý scriptík alias “univerzální ultra script :-)”. Trochu jsem scriptík poladil a funguje fakt dobře. Pomocí něj lze monitorovat stanice i servery a vytáhnout vše potřebné pro klid admina. Function ENJOY!
‘ON Error Resume Next
Const ForReading = 1
Const LowSpace = “0.1” ‘ Represents 10%
Const AdminMail = “mejl
@email.cz
Set IE = CreateObject(“InternetExplorer.Application”)
Set WshShell = WScript.CreateObject(“WScript.Shell”)
Set FSO = CreateObject(“Scripting.FileSystemObject”)
‘Functions
‘Checks if the number is a zero
Function fCheckValue(varValue, prepender, appender)
If IsNull(varValue) Or varValue = “” Then
fCheckValue = “N/A”
Else
fCheckValue = CStr(prepender & varValue & appender)
End If
End Function
‘Format Number RND
Private Function fFormatNum(num, DropDecimal)
Dim bytes
Dim lngSize
If IsNumeric(num) Then
If Len(num) < 5 Then
lngSize = FormatNumber((num /1024), 2, vbTrue, vbTrue, vbUseDefault)
bytes = ” Gb”
Elseif Len(num) < 7 Then
lngSize = FormatNumber((num / 1024), 2, vbTrue, vbTrue, vbUseDefault)
bytes = ” Kb”
Elseif Len(num) < 10 Then
lngSize = FormatNumber((num / 1024 ^ 2), 2, vbTrue, vbTrue, vbUseDefault)
bytes = ” Mb”
Elseif Len(num) < 13 Then
lngSize = FormatNumber((num / 1024 ^ 3), 2, vbTrue, vbTrue, vbUseDefault)
bytes = ” Gb”
Elseif Len(num) < 16 Then
lngSize = FormatNumber((num / 1024 ^ 4), 2, vbTrue, vbTrue, vbUseDefault)
bytes = ” Tb”
Elseif Len(num) >= 16 Then
lngSize = FormatNumber((num / 1024 ^ 5), 2, vbTrue, vbTrue, vbUseDefault)
bytes = ” Pb”
End If
If DropDecimal Or Right(lngSize, 2) = “00” Then
fFormatNum = CStr(Round(lngSize, 0)) & bytes
Else
fFormatNum = CStr(lngSize) & bytes
End If
Else
fFormatNum = num
End If
End Function
‘Subs
Sub indexFile()
‘Output Location And Name
Set objFile = FSO.CreateTextFile(“C:\Monitoring\index.html”)
If fso.FileExists (fname) THEN
set objFile = fso.GetFile (fname)
objFile.Delete
end If
objFile.Writeline “<html dir=’rtl’>”
objFile.Writeline “”
objFile.Writeline “<head>”
objFile.Writeline “<title>Monitoring – Index</title>”
objFile.Writeline “</head>”
objFile.Writeline “”
objFile.Writeline “<frameset cols=’*,195′>”
objFile.Writeline ” <frame name=’contents’ src=’main.html’>”
objFile.Writeline ” <frame name=’main’ target=’main’ src=’comps.html’>”
objFile.Writeline ” <noframes>”
objFile.Writeline ” <body>”
objFile.Writeline “”
objFile.Writeline ” <p>This page uses frames, but your browser doesn’t support them.</p>”
objFile.Writeline “”
objFile.Writeline ” </body>”
objFile.Writeline ” </noframes>”
objFile.Writeline “</frameset>”
objFile.Writeline “”
objFile.Writeline “</html>”
End Sub
Sub MainPage()
‘Output Location And Name
Set objFile = FSO.CreateTextFile(“C:\Monitoring\main.html”)
If fso.FileExists (fname) THEN
set objFile = fso.GetFile (fname)
objFile.Delete
end If
objFile.Writeline “<p> </p>”
objFile.Writeline “<p align=’center’><H1><Font Size=7>Server Monitoring</Font></H1></p>”
objFile.Writeline “<BR>”
objFile.Writeline “<BR>”
objFile.Writeline “<p align=’center’>”
objFile.Writeline “<BR>”
objFile.Writeline “<img border=’0′ src=’file:///D:/obrazek.jpg’ width=’338′ height=’257′></p>”
objFile.Writeline “<BR>”
end sub
Sub CompIndex(sysname)
‘Output Location And Name
Set objFile = FSO.CreateTextFile(“C:\Monitoring\computers\” & sysname & “-index.html”)
objFile.Writeline “<html dir=’rtl’>”
objFile.Writeline “”
objFile.Writeline “<head>”
objFile.Writeline “<title>Monitoring – Main</title>”
objFile.Writeline “</head>”
objFile.Writeline “”
objFile.Writeline “<frameset rows=’*,100′>”
objFile.Writeline ” <frame name=’contents’ src='” & sysname & “.html’>”
objFile.Writeline ” <frame name=’main’ target=’main’ src='” & sysname & “_FreeSpace.html’>”
objFile.Writeline ” <noframes>”
objFile.Writeline ” <body>”
objFile.Writeline “”
objFile.Writeline ” <p>This page uses frames, but your browser doesn’t support them.</p>”
objFile.Writeline “”
objFile.Writeline ” </body>”
objFile.Writeline ” </noframes>”
objFile.Writeline “</frameset>”
objFile.Writeline “”
objFile.Writeline “</html>”
End Sub
Sub getfile(ServerFile)
‘Output Location And Name
Set objFile = FSO.CreateTextFile(“C:\Monitoring\comps.html”)
If fso.FileExists (fname) THEN
set objFile = fso.GetFile (fname)
objFile.Delete
end If
Set objTextFile = FSO.OpenTextFile _
(ServersFile, ForReading)
Do Until objTextFile.AtEndOfStream
strNextLine = objTextFile.Readline
arrServersList = Split(strNextLine , “,”)
For i = 0 to Ubound(arrServersList)
Server = arrServersList(i)
CompIndex(Server)
compfiles(Server)
objFile.Writeline ” <p><a target=’contents’ href=’c:\Monitoring\Computers\” & Server & “-index.html’>” & Server & ” (” & i+1 & “</a></p>”
objFile.Writeline “<BR>”
Next
Loop
objFile.Writeline “”
objFile.Writeline ” <p><a target=’contents’ href=’c:\Monitoring\main.html’>Home</a></p>”
objFile.Writeline “<BR>”
End Sub
Sub compfiles(comp)
strComputer = comp
ServerPage(strcomputer)
FreeSpace(strcomputer)
End Sub
Sub ServerPage (sysname)
oN Error Resume Next
Set objFile = FSO.CreateTextFile(“C:\Monitoring\Computers\” & sysname & “.html”)
objFile.Writeline “<p align=’center’><b><u><font size=’6′>” & sysname & “</font></u></b></p>”
objFile.Writeline “<BR>”
        objFile.WriteLine(” <tr>”)
objFile.WriteLine(” <td colspan=’6′ class=’head’ align=’center’ nowrap>”)
objFile.WriteLine(” <form name=’Tools’>”)
objFile.WriteLine(” <input type=’button’ name=’Ping’ value=’Ping System’ class=’category’ style=’color: black’>”)
objFile.WriteLine(” <script for=’Ping’ event=’onClick’ language=’VBScript’>”)
objFile.WriteLine(” Dim WshShell, WScript”)
objFile.WriteLine(” Set WshShell = createObject(“”WScript.Shell””)”)
objFile.WriteLine(” WshShell.run(“”cmd /k ping ” & sysName & “””)”)
objFile.WriteLine(” </script>”)
objFile.WriteLine(” <input type=’button’ name=’Mgmt’ value=’Manage System’ class=’category’ style=’color: black’>”)
objFile.WriteLine(” <script for=’Mgmt’ event=’onClick’ language=’VBScript’>”)
objFile.WriteLine(” Dim WshShell, WScript”)
objFile.WriteLine(” Set WshShell = createObject(“”WScript.Shell””)”)
objFile.WriteLine(” WshShell.run(“”mmc %windir%\system32\compmgmt.msc -s /computer:\\” & sysName & “””)”)
objFile.WriteLine(” </script>”)
‘        objFile.WriteLine(” <input type=’button’ name=’Regedt32′ value=’Access Registry’ class=’category’ style=’color: black’>”)
‘        objFile.WriteLine(” <script for=’Regedt32′ event=’onClick’ language=’VBScript’>”)
‘        objFile.WriteLine(” Dim WshShell, WScript”)
‘        objFile.WriteLine(” Set WshShell = CreateObject(“”WScript.Shell””)”)
‘        objFile.WriteLine(” WshShell.RegWrite “”HKCU\Software\Microsoft\RegEdt32\” & sysName & “\””, “””””)
‘        objFile.WriteLine(” WshShell.RegWrite “”HKCU\Software\Microsoft\RegEdt32\Settings\Registry””, “”” & sysName & “”””)
‘        objFile.WriteLine(” WshShell.Run(“”regedt32.exe””)”)
‘        objFile.WriteLine(” </script>”)
objFile.WriteLine(” <input type=’button’ name=’Winmsd’ value=’Diagnostic’ class=’category’ style=’color: black’>”)
objFile.WriteLine(” <script for=’Winmsd’ event=’onClick’ language=’VBScript’>”)
objFile.WriteLine(” Dim WshShell, WScript”)
objFile.WriteLine(” set WshShell = createObject(“”WScript.Shell””)”)
objFile.WriteLine(” WshShell.Run(“”winmsd /computer ” & sysName & “””)”)
objFile.WriteLine(” </script>”)
objFile.WriteLine(” <input type=’button’ name=’Eventviewer’ value=’Event Viewer’ class=’category’ style=’color: black’>”)
objFile.WriteLine(” <script for=’Eventviewer’ event=’onClick’ language=’VBScript’>”)
objFile.WriteLine(” Dim WshShell, WScript”)
objFile.WriteLine(” Set WshShell = createObject(“”WScript.Shell””)”)
objFile.WriteLine(” WshShell.Run(“”eventvwr \\” & sysName & “””)”)
objFile.WriteLine(” </script>”)
objFile.WriteLine(” <input type=’button’ name=’Explore’ value=’Explorer’ class=’category’ style=’color: black’>”)
objFile.WriteLine(” <script for=’Explore’ event=’onClick’ language=’VBScript’>”)
objFile.WriteLine(” Dim WshShell, WScript”)
objFile.WriteLine(” Set WshShell = createObject(“”WScript.Shell””)”)
objFile.WriteLine(” WshShell.Run(“”explorer \\” & sysName & “””)”)
objFile.WriteLine(” </script>”)
objFile.WriteLine(” <input type=’button’ name=’Print’ value=’Print’ class=’category’ style=’color: black’>”)
objFile.WriteLine(” <script for=’Print’ event=’onClick’>”)
objFile.WriteLine(” window.print()”)
objFile.WriteLine(” </script>”)
objFile.WriteLine(” </form>”)
objFile.WriteLine(”     </td>”)
objFile.WriteLine(” </tr>”)
Set objWMILocator = GetObject(“winmgmts:{impersonationLevel=impersonate,(Security)}!\\” & sysname & “\root\cimv2”)
Set objWMIInstance = objWMILocator.ExecQuery _
(“select * from win32_computersystem”,,48)
    For Each item In objWMIInstance
name = “<B> Name: </B>” & item.Caption
domain = “<B>Domain: </B>” & item.Domain
‘        type = “<B>Type: </B>” & item.systemtype
manufacturer = “<B>Manufacturer: </B>” & fCheckValue(item.manufacturer, Empty, Empty)
model = “<B>Model: </B>” & fCheckValue(item.model, Empty, Empty)
‘        sp = “SP: ” & item.csdversion
prosessors = “<B>Processors: </B>” & item.numberofprocessors
memory = “<B>Memory: </b>” & fFormatNum(item.totalphysicalmemory,True)
Next
Set objWMIInstance = Nothing
Set objWMIInstance = objWMILocator.ExecQuery _
(“select * from win32_operatingsystem”,,48)
For Each item In objWMIInstance
If InStr(1, item.Caption, “2000”) Then
strOS = “2000”
blnExtendedWMI = False
Elseif InStr(1, item.caption, “XP”) Then
strOS = “XP”
blnExtendedWMI = True
Elseif InStr(1, item.caption, “2003”) Then
strOS = “2003”
blnExtendedWMI = True
Else
strOS = “WinNT9x”
blnExtendedWMI = False
End If

If strOS = “2003” Then
os = “<B>OS: </B>” & Replace(item.Caption, “Microsoft(R) Windows(R)”, “”)
Else
os = “<B>OS: </B>” & Replace(item.Caption, “Microsoft Windows”, “”)
End If
osver = “<B>Version: </B>” & item.Version
sp = “<B>SP: </B>” & item.csdversion
serial = “<B>Serial #: </B>” & item.serialnumber

Next
Set objWMIInstance = Nothing
‘ Text
objFile.Writeline “<div align=’center’>”
objFile.Writeline ” <center>”
objFile.Writeline ” <table border=’1′ cellspacing=’1′ width=’100%’ id=’AutoNumber1′>”
objFile.Writeline ” <tr>”
objFile.Writeline “<td><B><U>General Information : </B></U></td>”
objFile.Writeline “<td><B><U> Server Information :</B></U></td>”
objfile.Writeline “</tr>”
objfile.Writeline “<tr>”
objFile.Writeline “<td width=’33%’>” & name & “</td>”
objfile.writeline “<td width=’34%’>” & manufacturer & “</td>”
objfile.writeline “</tr>”
objfile.writeline “<tr>”
objFile.Writeline “<td width=’33%’>” & domain & “</td>”
objfile.writeline “<td width=’34%’>” & model & “</td>”
objfile.writeline “</tr>”
objfile.writeline “<tr>”
objFile.Writeline “<td width=’33%’>” & os & “</td>”
objfile.writeline “<td width=’34%’>” & prosessors & “</td>”
objfile.writeline “</tr>”
objfile.writeline “<tr>”
objFile.Writeline “<td width=’33%’>” & osver & “</td>”
objfile.writeline “<td width=’34%’>” & memory & “</td>”
objfile.writeline “</tr>”
objfile.writeline “<tr>”
objFile.Writeline “<td width=’50%’>” & sp & “</td>”
objfile.writeline “<td width=’50%’>” & serial & “</td>”
objfile.writeline “</tr>”
objfile.writeline “</table>”
objFile.Writeline “</center>”
objFile.Writeline “</div>”
End Sub

Sub FreeSpace (sysname)
On Error Resume Next
Error = 0
Set objFile = FSO.CreateTextFile(“C:\Monitoring\Computers\” & sysname & “_FreeSpace.html”)
Set objWMILocator = GetObject(“winmgmts:{impersonationLevel=impersonate,(Security)}!\\” & sysname & “\root\cimv2”)
If Error <> 0 Then
Wscript.echo “Error !!”
End If
Set objWMIInstance = objWMILocator.ExecQuery _
(“select freephysicalmemory,totalvirtualmemorysize,freevirtualmemory,FreeSpaceInPagingFiles from win32_operatingsystem”,,48)
For Each item In objWMIInstance
mem = fFormatNum(1000 * item.freephysicalmemory, True)
objFile.Writeline “Free Memory: ” & mem
if mem <= “100 Mb” then
‘wscript.Echo “Warning ! ” & sysname & ” Has Low Memory”
end if
next
set objWMIInstance = nothing
objFile.Writeline “<div align=’center’>”
objFile.Writeline ” <center>”
objFile.Writeline ” <table border=’3′ cellspacing=’2′ width=’100%’ id=’AutoNumber1′>”
objFile.Writeline ” <tr>”
objFile.Writeline “<td>Physical Disk(s)</td>”
objFile.Writeline “</tr>”
Set objWMIInstance = objWMILocator.ExecQuery _
(“select manufacturer, description, caption, interfacetype, partitions, size, scsibus, scsitargetid, status from win32_diskdrive”)
For Each item In objWMIInstance
Set objPartitionSet = item.Associators_(“Win32_DiskDriveToDiskPartition”, “Win32_DiskPartition”)
For Each objPartition In objPartitionSet
Set objDriveSet = objPartition.Associators_(“Win32_LogicalDiskToPartition”, “Win32_LogicalDisk”)
aCounter = 0
objFile.Writeline “<tr>”
For Each objDrive In objDriveSet
objFile.Writeline “<tr>”
If objPartition.bootpartition Then
objFile.Writeline “<td>Drive:” & objDrive.Caption & ” – Boot Partition</td>”
Else
objFile.Writeline “<td>Drive:” & objDrive.Caption & “</td>”
End If
objFile.Writeline “<td>File Sys:” & objDrive.FileSystem & “</td>”
objFile.Writeline “<td>Total Size:” & fFormatNum(objDrive.Size, False) & “</td>”
drv = fFormatNum(objDrive.FreeSpace, False)
Size=fFormatNum(objDrive.Size, False)
LenDRV = Len(drv)
LenSize = Len(Size)
drv=Left(drv,LenDRV – 2)
Size=Left(Size,LenSize – 2)
FreeSpacePrecent = DRV/Size
if FreeSpacePrecent<=LowSpace then
‘    MailAdmin SysName,objDrive.Caption,drv
end if

objFile.Writeline “<td bgcolor=’#FF0002’>Free:” & drv & “</td>”
objFile.Writeline “<td>S/N:” & fCheckValue(objDrive.VolumeSerialNumber, Empty, Empty) & “</td>”
If aCounter < objPartitionSet.Count Then
‘fSpacer (5)
End If
aCounter = aCounter + 1
objFile.Writeline “</tr>”
Next
objFile.Writeline “</tr>”
Next

next
end Sub

Sub Refresher(ServerFile)
Set objTextFile = objFSO.OpenTextFile _
(ServerFile, ForReading)
Do Until objTextFile.AtEndOfStream
strNextLine = objTextFile.Readline
arrServersList = Split(strNextLine , “,”)
For i = 0 to Ubound(arrServiceList)
Server = arrServersList(i)
FreeSpace(Server)
Next
Loop
End Sub
Sub MailAdmin(SysName,DrvCaption,DrvSize)
‘Send Mail to the SysAdmin
Set objEmail = CreateObject(“CDO.Message”)
objEmail.From = AdminMail
objEmail.To = AdminMail
objEmail.Subject = “Server down”
‘Add the body
strMsg = SysName & ” has no free space on Drive ” & DrvCaption & vbcrlf
strMsg = strMsg & ” Size Is now ” & DrvSize & vbclrf & “Your attention is Required.”
objEmail.Textbody = strMsg
objEmail.Configuration.Fields.Item _
(“
http://schemas.microsoft.com/cdo/configuration/sendusing“) = 2
objEmail.Configuration.Fields.Item _
(“
http://schemas.microsoft.com/cdo/configuration/smtpserver“) = _
“mail”
objEmail.Configuration.Fields.Item _
(“
http://schemas.microsoft.com/cdo/configuration/smtpserverport“) = 25
objEmail.Configuration.Fields.Update
objEmail.Send
End Sub
Sub SetupIE(File2Load)
IE.Navigate File2Load
IE.ToolBar = True
IE.StatusBar = True
IE.Resizable = True
strComputer = “.”
Set objWMIService = GetObject(“winmgmts:” _
& “{impersonationLevel=impersonate}!\\” & strComputer & “\root\cimv2”)
Set colItems = objWMIService.ExecQuery(“Select * from Win32_DesktopMonitor”)

For Each objItem in ColItems
ScreenHeight = objItem.ScreenHeight
ScreenWidth = objItem.ScreenWidth
next

Do
Loop While IE.Busy
IE.Width = ScreenWidth
IE.Height = ScreenHeight
IE.Left = 0
IE.Top = 0
IE.Visible = True
WshShell.AppActivate(“Microsoft Internet Explorer”)
End Sub
‘Script
‘Create a Folder
Set objFolder = FSO.CreateFolder(“C:\Monitoring”)
Set objFolder = FSO.CreateFolder(“C:\Monitoring\Computers”)
‘Open the Servers File
Set objDialog = CreateObject(“UserAccounts.CommonDialog”)
objDialog.Filter = “All Files|*.*|Text Files|*.txt”
objDialog.InitialDir = “C:\”
intResult = objDialog.ShowOpen
If intResult = 0 Then
Wscript.Quit
Else
ServersFile=objDialog.FileName
End If
MainPage()
IndexFile()
getFile(ServerFile)
‘wscript.Echo “OK”
‘ Opens The File
File2Load =”c:\monitoring\index.html”
SetupIE File2Load
Do While Not IE.Busy ‘If you Close the File the script exits if not it continues to refresh the free space page
wscript.sleep 30000000
refresher(ServerFile)
If IE.Quit Then
Exit Do
End If
Loop
IE.Quit
Wscript.quit