TotalDiskUsage/TotalDiskUsage.au3

254 lines
15 KiB
Plaintext

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Icon=Icon256-32.ico
#AutoIt3Wrapper_UseUpx=y
#AutoIt3Wrapper_Change2CUI=y
#AutoIt3Wrapper_Res_Description=Zabbix Total Disk Usage Tool
#AutoIt3Wrapper_Res_Fileversion=1.0.0.8
#AutoIt3Wrapper_Res_Fileversion_AutoIncrement=y
#AutoIt3Wrapper_Res_LegalCopyright=2015 Bernhard Linz
#AutoIt3Wrapper_Res_Language=1031
#AutoIt3Wrapper_Res_Field=Website|http://znil.net
#AutoIt3Wrapper_Res_Field=Manual|http://znil.net/index.php?title=Zabbix:Template_TotalDiskUsage
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <Array.au3>
#include <Date.au3>
Opt('MustDeclareVars', 1)
; ##########################################################################################################################
; ##########################################################################################################################
; # TotalDiskUsgae.exe --> #
; # 2015 Bernhard Linz / Bernhard@znil.de / http://znil.net #
; # #
; # Latest Version of this Program and Template #
; # http://znil.net/index.php?title=Zabbix:Template_Windows_Mountpoints_-_Windows_Festplatten_Mountpoints_entdecken #
; # #
; # ________ .__ __. __ __ .__ __. _______ .___________. #
; # | / | \ | | | | | | | \ | | | ____|| | #
; # `---/ / | \| | | | | | | \| | | |__ `---| |----` #
; # / / | . ` | | | | | | . ` | | __| | | #
; # / /----.| |\ | | | | `----.__| |\ | | |____ | | #
; # /________||__| \__| |__| |_______(__)__| \__| |_______| |__| #
; # #
; ##########################################################################################################################
; ##########################################################################################################################
; Catch all Error while using the WMI-Interface with own Error-Function
Global $oMyError = ObjEvent("AutoIt.Error","MyErrFunc"), $f_COMError = False
; And the Error-Function itself
Func MyErrFunc()
Local $HexNumber=hex($oMyError.number,8)
;~ ConsoleWriteError("We intercepted a COM Error !" & @CRLF & _
;~ "Number is: " & $HexNumber & @CRLF & _
;~ "WinDescription is: " & $oMyError.windescription & @CRLF & _
;~ "Source is: " & $oMyError.source & @CRLF & _
;~ "ScriptLine is: " & $oMyError.scriptline & @CRLF)
ConsoleWrite("ZBX_NOTSUPPORTED" & @CRLF)
Exit 1
Endfunc
; ##########################################################################################################################
; ##########################################################################################################################
; Function for catching the special characters - hey we using the old DOS-Console and the Console did'nt like them
Func _ANSI2OEM($text)
$text = DllCall('user32.dll', 'Int', 'CharToOem', 'str', $text, 'str', '')
Return $text[2]
EndFunc ;==>_ANSI2OEM
; ##################################################################################################################
; ######## ### ######## ######## #### ## ## ###### ####### ## ## ########
; ## ## ## ## ## ## ## ## ## ## ## ## ## ## ### ## ##
; ## ## ## ## ## ## ## ## ## ## ## ## ## #### ## ##
; ## ## ## ######## ######## ## ### ## ## ## ## ## ## ######
; ## ######### ## ## ## ## ## ## ## ## ## ## ## #### ##
; ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ### ##
; ####### ######## ## ## ######## ######## #### ## ## ####### ###### ####### ## ## ##
; ##################################################################################################################
Global $sZabbix_agentd_exe
Global $sZabbix_agentd_conf
Global $sZabbix_sender_exe
Global $sZabbix_Hostname
Global $sZabbix_Server
Global $iZabbix_Server_Port
Global $sZabbix_String2Send
Func _Zabbix_conf()
Dim $sRegTMP
Dim $iRegTMP = 0
Dim $iLoop
Dim $aRegTMP
Dim $aZabbix_config
Dim $sZabbix_config_file
Dim $a2temp
While 1
$iRegTMP = $iRegTMP + 1
$sRegTMP = RegEnumKey("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services", $iRegTMP)
If @error Then
ExitLoop
EndIf
$sRegTMP = RegRead("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\" & $sRegTMP, "ImagePath")
If @error = 0 Then
If StringInStr($sRegTMP, "zabbix_agentd.exe") > 0 Then
;ConsoleWrite("Gefunden: " & $sRegTMP & @CRLF)
$aRegTMP = StringSplit($sRegTMP, '"')
;_ArrayDisplay($aRegTMP)
$sZabbix_agentd_exe = $aRegTMP[2]
$sZabbix_sender_exe = StringReplace($sZabbix_agentd_exe, "zabbix_agentd.exe", "zabbix_sender.exe")
$sZabbix_config_file = $aRegTMP[4]
$sZabbix_agentd_conf = $sZabbix_config_file
$aZabbix_config = FileReadToArray($sZabbix_config_file)
If @error = 0 Then
For $iLoop = 0 To (UBound($aZabbix_config) -1)
If StringLeft($aZabbix_config[$iLoop], StringLen("ServerActive=")) = "ServerActive=" Then
$sZabbix_Server = StringReplace($aZabbix_config[$iLoop], "ServerActive=", "")
$sZabbix_Server = StringReplace($sZabbix_Server, " ", "")
$a2temp = StringSplit($sZabbix_Server, ":")
If $a2temp[0] = 1 Then
$iZabbix_Server_Port = 10051
Else
$sZabbix_Server = $a2temp[1]
$iZabbix_Server_Port = $a2temp[2]
EndIf
EndIf
If StringLeft($aZabbix_config[$iLoop], StringLen("Hostname=")) = "Hostname=" Then
$sZabbix_Hostname = StringReplace($aZabbix_config[$iLoop], "Hostname=", "")
$sZabbix_Hostname = StringReplace($sZabbix_Hostname, " ", "")
EndIf
Next
Else
ConsoleWrite("File not found: " & $sZabbix_config_file & @CRLF)
Exit 1
EndIf
If FileExists($sZabbix_sender_exe) = 0 Then
ConsoleWrite("zabbix_sender.exe not found at: " & $sZabbix_sender_exe & @CRLF)
Exit 1
EndIf
EndIf
EndIf
WEnd
;~ ConsoleWrite("zabbix_agentd.exe: " & '"' & $sZabbix_agentd_exe & '"' & @CRLF)
;~ ConsoleWrite("zabbix_sender.exe: " & '"' & $sZabbix_sender_exe & '"' & @CRLF)
;~ ConsoleWrite("Hostname used in Zabbix: " & '"' & $sZabbix_Hostname & '"' & @CRLF)
;~ ConsoleWrite("IP(:Port) Zabbix Server: " & '"' & $sZabbix_Server & '"' & @CRLF)
EndFunc
; #############################################################################################################################################################################################################################################################################
; ###### ######## ## ## ######## ## ## ### ## ## ## ######## ## ## #### ######## ## ## ######## ### ######## ######## #### ## ## ###### ######## ## ## ######## ######## ########
; ## ## ## ### ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ### ## ## ## ## ## ##
; ## ## #### ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## #### ## ## ## ## ## ##
; ###### ###### ## ## ## ## ## ## ## ## ## ## ## ## ###### ## ## ## ## ## ######### ## ## ## ######## ######## ## ### ###### ###### ## ## ## ## ## ###### ########
; ## ## ## #### ## ## ## ## ######### ## ## ## ## ## ## ## ## ## ## ## ## ######### ## ## ## ## ## ## ## ## ## ## #### ## ## ## ## ##
; ## ## ## ## ### ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ### ## ## ## ## ##
; ####### ###### ######## ## ## ######## ### ## ## ######## ####### ######## ####### ### ### #### ## ## ## ####### ######## ## ## ######## ######## #### ## ## ####### ###### ######## ## ## ######## ######## ## ##
; #############################################################################################################################################################################################################################################################################
Func _SendValue_with_Zabbix_Sender($sItemName, $sItemValue)
;ConsoleWrite('"' & $sZabbix_sender_exe & '"' & " -c " & '"' & $sZabbix_agentd_conf & '"' & " -k " & '"' & $sItemName & '" -o "' & $sItemValue & '"' & @CRLF)
RunWait('"' & $sZabbix_sender_exe & '"' & " -c " & '"' & $sZabbix_agentd_conf & '"' & " -k " & '"' & $sItemName & '" -o "' & $sItemValue & '"', "", @SW_HIDE, $RUN_CREATE_NEW_CONSOLE)
EndFunc
; ##########################################################################################################################
; ##########################################################################################################################
; Other needed Variables
Dim $wbemFlagReturnImmediately = 0x10
Dim $wbemFlagForwardOnly = 0x20
Dim $colItems = ""
Dim $strComputer = "localhost"
Dim $o_WMI
Dim $o_colListOfVolumes
Dim $s_JSONOutput
Dim $s_Mointpoint2IgnoreFile = @ScriptDir & "\WindowsMountPoints-ignore.txt"
Dim $h_Mountpoint2IgnoreFile
Dim $a_Mountpoint2Ignore[1] = [ 0 ]
Dim $b_IgnoreMountpoint = False
Dim $i_ServiceType
Dim $s_temp
Dim $i
Dim $s_spaces = ' ' ; 5 Spaces
Dim $i_TotalAvailable = 0
Dim $i_TotalUsed = 0
Dim $i_TotalFree = 0
Dim $iStartTime, $iRunTime
$iStartTime = _DateDiff("s", "1970/01/01 00:00:00", _NowCalc())
; ##########################################################################################################################
; ##########################################################################################################################
; Some of the mountpoints are irritating, just ignore them - read items from a file
If FileExists($s_Mointpoint2IgnoreFile) = 1 Then
$h_Mountpoint2IgnoreFile = FileOpen($s_Mointpoint2IgnoreFile, 0)
While 1
$s_temp = FileReadLine($h_Mountpoint2IgnoreFile)
If @error = -1 Then
ExitLoop
Else
If StringLen($s_temp) > 2 Then ; at least 3 or more chars
$a_Mountpoint2Ignore[0] = $a_Mountpoint2Ignore[0] + 1
ReDim $a_Mountpoint2Ignore[$a_Mountpoint2Ignore[0] + 1] ;That's AutoIt - Redim an Array without data-loss
$a_Mountpoint2Ignore[$a_Mountpoint2Ignore[0]] = $s_temp
EndIf
EndIf
WEnd
EndIf
; ##########################################################################################################################
; ##########################################################################################################################
; Get the WMI-Interface at local Computer (= .)
$o_WMI = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
; Test whether it worked and go on
If IsObj($o_WMI) Then
; Get List of all Autostart-Services
$o_colListOfVolumes = $o_WMI.ExecQuery("SELECT * FROM Win32_Volume", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
; Ok, we have the List, now look if we were interrupted last Time
; Start creating the JSON Output:
$s_JSONOutput = '{"data":[' & '@CRLF'
; Loop: Add the Services to JSON Output String Line for Line but nor more than 4000 Chars
For $objMountpoint In $o_colListOfVolumes
$b_IgnoreMountpoint = False
; Do nothing until we found the Service of last Run
; Will start after the first Part found the Service of last Time or will run from beginning if there was no last time
; Test 1: But first check if the service is one of the bad one we wanna ignore
If $a_Mountpoint2Ignore[0] > 0 Then
For $i = 1 To $a_Mountpoint2Ignore[0]
;ConsoleWrite($objMountpoint.DisplayName & "<--->" & $a_Mountpoint2Ignore[$i] & @CRLF)
If StringInStr($objMountpoint.DeviceID, $a_Mountpoint2Ignore[$i]) > 0 Then
$b_IgnoreMountpoint = True
EndIf
If StringInStr($objMountpoint.Name, $a_Mountpoint2Ignore[$i]) > 0 Then
$b_IgnoreMountpoint = True
EndIf
Next
EndIf
;Skip CD / DVD
If $objMountpoint.DriveType = 5 Then
$b_IgnoreMountpoint = True
EndIf
;~ ; Skip all normal Mountpoints with Driveletters like C:\ or D:\
;~ If StringRight($objMountpoint.Name, 2) = ":\" Then
;~ $b_IgnoreMountpoint = True
;~ EndIf
; Ignore SnapShots!
If StringLeft($objMountpoint.Name, 4) = "\\?\" Then
$b_IgnoreMountpoint = True
EndIf
; Ok, all Tests processed, now add the service to list (if we don't wanna ignore this service)
If $b_IgnoreMountpoint = False Then
;~ ConsoleWrite($objMountpoint.Name & " : " & (DriveSpaceTotal($objMountpoint.Name) * 1048576) & @CRLF)
$i_TotalAvailable = $i_TotalAvailable + (DriveSpaceTotal($objMountpoint.Name) * 1048576)
$i_TotalFree = $i_TotalFree + (DriveSpaceFree($objMountpoint.Name) * 1048576)
EndIf
Next
$i_TotalUsed = $i_TotalAvailable - $i_TotalFree
;~ ConsoleWrite("Total Available Diskspace : " & $i_TotalAvailable & @CRLF)
;~ ConsoleWrite("Total Used Diskspace : " & $i_TotalUsed & @CRLF)
;~ ConsoleWrite("Total Free Diskspace : " & $i_TotalFree & @CRLF)
_Zabbix_conf()
_SendValue_with_Zabbix_Sender("znil.TotalDiskUsage[available]", $i_TotalAvailable)
_SendValue_with_Zabbix_Sender("znil.TotalDiskUsage[used]", $i_TotalUsed)
_SendValue_with_Zabbix_Sender("znil.TotalDiskUsage[free]", $i_TotalFree)
$iRunTime = _DateDiff("s", "1970/01/01 00:00:00", _NowCalc()) - $iStartTime
ConsoleWrite($iRunTime & @CRLF)
EndIf
Exit 0