alanrenouf / vCheck-vSphere

vCheck Daily Report for vSphere
MIT License
735 stars 325 forks source link

suggestion - new plug-in to check vmdk files #207

Closed funnyname closed 10 years ago

funnyname commented 10 years ago

Hello,

I have a simple script that checks if there is a vmdk file alongside the flat-vmdk in a folder of a VM. In a large and messy organisation, it can happen that the vmdk file gets deleted accidentally. A running VM will keep working, only when the VM is power cycled, the VM can't start because the vmdk file is missing. This problem can be remedied quiet simply, therefore a check to know in advance is helpful.

Now my question is, do you guys find this a helpful addition? If so, how do I change the script so it can act as a plug-in? Or can I send the script so someone can convert it to plug-in for me?

Thanks in advance.

Cheers, PB

Sneddo commented 10 years ago

Hi, I started putting a wiki article together on the plugin structure here: https://github.com/alanrenouf/vCheck-vSphere/wiki/Plugin-structure

Otherwise if you post some code we can adapt it to the plugin structure.

funnyname commented 10 years ago

OK thanks, I'm gone take a look at the wiki and report back later

funnyname commented 10 years ago

OK, here is my code. Two things to note: 1 - $DatastoreName is now via read-host configurable, this needs to be adjusted via the general script config (is this possible?) 2 - The Output ($VMDK_check_out) is not working correctly, I have not enough script knowledge to make this work. (can you have a look and adjust for me plz?)

Let me know if you need extra information, I hope this code is helpful for the Vcheck project.

# Start of Settings
[string]$DatastoreName = Read-Host "Please type the name of your datastores, you wish to examine for missing VmDisk Files (wildcard = *)"
# End of Settings

$Title = "VMDK consistency"
$Header =  "VMDK consistency"
$Comments = "This simple plug-in checks when there is a FLAT-VMDK file found in a folder, if the corresponding VMDK is also present"
$Display = "List"
$Author = "Peronnik Beijer"
$PluginVersion = 1.0
$PluginCategory = "vSphere"

$Url = "http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=1002511"

# Main plug-in script
$Datastores = Get-Datastore -Name "$DatastoreName"

foreach ($Datastore in $Datastores)
{
    If ( ((Get-Location -EA 0).Path | % {$_.Split("\")[0]}) -eq "ds:" ){
        Set-Location c: -EA 0 | Out-Null
    }
    If ( (Get-PSDrive ds -EA 0).Name -eq "ds" ){
        Remove-PSDrive ds -Force -EA 0 | Out-Null
    }
    New-PSDrive -Location (Get-Datastore "$Datastore") -Name ds -PSProvider VimDatastore -Root “” -EA 0 | Out-Null
    Set-location ds: -EA 0 | Out-Null
    If (!($?)){
        Write-Host "There was an error mounting the datastore as PSDrive, please investigate first, this plug-in will now Exit"
        Write-Error $error[0]
        Exit
    }
    $folders = Get-Item * -Exclude ".vSphere-HA" | ?{ $_.PSIsContainer } | % { $_.Name }
    foreach ( $folder in $Folders )
    {
       $vmdks = Get-ChildItem $folder\*.vmdk
        If (!($?)){
            Write-Host "There was an error getting the files from folder $folder, please investigate first, this plug-in will now Exit"
            Write-Error $error[0]
            Exit
        }
       foreach ($vmdk in $vmdks)
       {
            If ( $vmdk -eq $empty ){
                break
            }
            If ( !($vmdk.ItemType -Like "VmDiskFile") ){
                $check = $false
                $Out_Obj = $null
                $Out_Obj = New-Object -TypeName PSObject
                $Out_Obj | Add-Member -MemberType NoteProperty -Name "Info" -Value "In folder : [$Datastore)]/$folder are (flat) VMDK's found, but no VmDiskFiles! For more information to fix the problem, please take a look at: $Url"
                $VMDK_check_out = @()
                $VMDK_check_out += $Out_Obj
            }
            Else{
                $check = $true 
                Write-Host "In folder : [$Datastore)]/$folder are Vmdiskfile(s) present, no problem found" 
                break
            }
        }
    }
}    

$VMDK_check_out
funnyname commented 10 years ago

I have created a new version, still the issues of reporting are not resolved ..

# Start of Settings
[string]$DatastoreName = Read-Host "Please type the name of your datastores, you wish to examine for missing VmDisk Files (wildcard = *)"
# End of Settings

$Title = "VMDK consistency"
$Header =  "VMDK consistency"
$Comments = "This simple plug-in checks when a FLAT-VMDK file is found in a folder, if the corresponding VMDK file is also present"
$Display = "List"
$Author = "Peronnik Beijer"
$PluginVersion = 1.1
$PluginCategory = "vSphere"

$Url = "http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=1002511"

# Main plug-in script
$Datastores = Get-Datastore -Name "$DatastoreName"

foreach ($Datastore in $Datastores)
{
    New-PSDrive -Location (Get-Datastore "$Datastore") -Name "$($Datastore.Name)" -PSProvider VimDatastore -Root “” -EA 0 -Confirm:$false | Out-Null
    Set-location "$($Datastore.Name):" -EA 0 | Out-Null
    If (!($?)) {
        Write-Host "There was an error mounting the datastore as PSDrive, please investigate first, this plugin will now Exit" -ForegroundColor Red -BackgroundColor black
        Write-Error $error[0]
        Exit } 
    $folders = Get-Item * -Exclude ".vSphere-HA" | ? { $_.PSIsContainer } | % { $_.Name }
    foreach ( $folder in $Folders )
    {
        $VmDiskfileCount = 0
        $fileCount = 0
        $vmdks = Get-ChildItem "$folder\*.vmdk" -Exclude "*-ctk.vmdk"
        If (!($?)) {
            Write-Host "There was an error getting the files from folder $folder, please investigate first, this plugin will now Exit" -ForegroundColor Red -BackgroundColor black
            Write-Error $error[0]
            Exit }
        foreach ($vmdk in $vmdks)
        {
            If ($vmdk.itemtype -Like "VmDiskFile") { 
                $VmDiskfileCount += 1 } 
            Else { 
                $fileCount += 1 } 
        }
        If ( $VmDiskfileCount -ne $fileCount) {
            $InfoMsg = "In the folder : [$Datastore)]/$folder/ are flat-VMDK file(s) found, but not enough corresponding VMDK file(s)! For more information to fix the problem, please take a look at: $Url"
            Write-Host "$InfoMsg"
            $Out_Obj = $null
            $Out_Obj = New-Object -TypeName PSObject
            $Out_Obj | Add-Member -MemberType NoteProperty -Name "Info" -Value "$InfoMsg"
            $VMDK_check_out = @()
            $VMDK_check_out += $Out_Obj }
        Else {
            $InfoMsg = "The vmdk and flat-vmdk file(s) are consistent in the folder : [$Datastore)]/$folder/"
            Write-Host "$InfoMsg" }
    }
    Remove-PSDrive "$($Datastore.Name):" -Force -EA 0 | Out-Null
}  

$VMDK_check_out
funnyname commented 10 years ago

Again a new version, the output is sorted now .. Please let me know if this was helpful.

Cheers

# Start of Settings
# What Datastores do you wish to examine for missing VmDisk Files (wildcard = *)
$DatastoreName = "*"
# End of Settings

$Title = "VMDK consistency"
$Header =  "VMDK consistency"
$Comments = "This simple plug-in checks if every FLAT-VMDK file found in a folder, has a corresponding VMDK file also present"
$Display = "List"
$Author = "Peronnik Beijer"
$PluginVersion = 1.2
$PluginCategory = "vSphere"

$Url = 'http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=1002511'
$VMDK_check_out = @()

# Main plug-in script
$Datastores = Get-Datastore -Name "$DatastoreName"

foreach ( $Datastore in $Datastores )
{
    New-PSDrive -Location (Get-Datastore "$Datastore") -Name "$($Datastore.Name)" -PSProvider VimDatastore -Root “” -EA 0 -Confirm:$false | Out-Null
    Set-location "$($Datastore.Name):" -EA 0 | Out-Null
    If (!($?)) {
        Write-Host "There was an error mounting the datastore as PSDrive, please investigate first, this plugin will now Exit" -ForegroundColor Red -BackgroundColor black
        Write-Error $error[0]
        Exit } 
    $folders = Get-Item * -Exclude ".vSphere-HA" | ? { $_.PSIsContainer } | % { $_.Name }
    foreach ( $folder in $Folders )
    {
        $VmDiskfileCount = 0
        $fileCount = 0
        $vmdks = Get-ChildItem "$folder\*.vmdk" -Exclude "*-ctk.vmdk"
        If (!($?)) {
            Write-Host "There was an error getting the files from $folder, please investigate first, this plugin will now Exit" -ForegroundColor Red -BackgroundColor black
            Write-Error $error[0]
            Exit }
        foreach ($vmdk in $vmdks)
        {
            If ($vmdk.itemtype -Like "VmDiskFile") { 
                $VmDiskfileCount += 1 } 
            ElseIf ( $vmdk.itemtype -Like "File" ) { 
                $fileCount += 1 }
        }
        If ( $VmDiskfileCount -ne $fileCount) {
            $InfoMsg = "In the folder : [$Datastore)]/$folder/ are flat-VMDK file(s) found, but not enough corresponding VMDK file(s)! For more information to fix the problem, please take a look at: $Url"
            $Out_Obj = $null
            $Out_Obj = New-Object -TypeName PSObject
            $Out_Obj | Add-Member -MemberType NoteProperty -Name "Info" -Value "$InfoMsg"
            $VMDK_check_out += $Out_Obj }
    }
    Set-location "C:" -EA 0 | Out-Null
    Remove-PSDrive "$($Datastore.Name):" -Force -EA 0 | Out-Null
}  

$VMDK_check_out
Sneddo commented 10 years ago

Merged code into new plugin