VMware Cloud Community
mars0077
Enthusiast
Enthusiast
Jump to solution

PowerCLI script to monitor NTP and e-mail report

Hi guys,

I am looking for a PowerCLI script I can use to check the ntpq status for all our ESXi hosts and e-mail an HTML friendly report. It would be awesome if the report could include the name of the host, NTP server configured and service status and NTP delay.

Much appreciated!! Thanks.

1 Solution

Accepted Solutions
mars0077
Enthusiast
Enthusiast
Jump to solution

Yes, you are correct. I was able to add a one liner to include the connection information for all my vCenter instances. Thanks so much for the help on this one. Much appreciated!

View solution in original post

Reply
0 Kudos
16 Replies
LucD
Leadership
Leadership
Jump to solution

The following will do most of what you asked for, except for the "delay".

Do you mean the NTP drift with that?

If yes, that is stored in a file inside the BusyBox and will require SSH to retrieve.

$sMail = @{

    From = 'me@domain.com'

    To = 'me@domain.com'

    SmtpServer = 'mail.domain.com'

    Subject = 'Report'

    BodyAsHtml = $true

    Body = Get-VMHost | Select Name,

        @{N='NTP Service Running';E={Get-VMHostService -VMHost $_ | where{$_.Key -eq 'ntpd'} | select -ExpandProperty Running}},

        @{N='NTP Server(s)';E={(Get-View -Id $_.ExtensionData.ConfigManager.DatetimeSystem).DateTimeInfo.NtpConfig.Server -join '| '}} |

        ConvertTo-Html | Out-String

}

Send-MailMessage @SMail


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
mars0077
Enthusiast
Enthusiast
Jump to solution

Thanks so much for the above script. It worked flawlessly and I am now able to get the report! Yes, I do mean drift by "delay". We do have SSH enabled, assuming this is all that is needed.

It would be awesome if this report can also include values for the fields below:

NTP:

Reach = 377 if same Green

Delay = 20MS or more is RED

Offset = -1 Anything higher is RED

jitter = 0 Anything higher is RED

These fields come from this KB article: Troubleshooting NTP on ESX and ESXi 4.x / 5.x / 6.x (1005092) | VMware KB

Thanks again for your help and large contributions!

Reply
0 Kudos
mars0077
Enthusiast
Enthusiast
Jump to solution

Hello,

Just checking to see if we could include the values below as part of the script.

NTP:

Reach = 377 if same Green

Delay = 20MS or more is RED

Offset = -1 Anything higher is RED

jitter = 0 Anything higher is RED

Thanks again!

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Well, if you are allowed to do SSH to your ESXi nodes, you can use the plink.exe command from the PuTTY Suite to execute the 'ntpq -p' command in the ESXi node.

That would look something like this

$user = 'root'

$pswd = 'pswd'

$plink = 'C:\Putty\plink.exe'

$plinkoptions = " -batch -pw $Pswd"

$cmd1 = 'ntpq -p'

$remoteCommand = '"' + $cmd1 + '"'

$sMail = @{

    From = 'me@domain.com'

    To = 'me@domain.com'

    SmtpServer = 'mail.domain.com'

    Subject = 'NTP Report'

    BodyAsHtml = $true

    Body = Get-VMHost | Select Name,

        @{N='NTP Service Running';E={Get-VMHostService -VMHost $_ | where{$_.Key -eq 'ntpd'} | select -ExpandProperty Running}},

        @{N='NTP Server(s)';E={(Get-View -Id $_.ExtensionData.ConfigManager.DatetimeSystem).DateTimeInfo.NtpConfig.Server -join '| '}},

        @{N='Reach';E={

            $command = $plink + " " + $plinkoptions + " " + $User + "@" + $_.Name + " " + $remoteCommand

            $msg = Invoke-Expression -command $command

            $fields = (($msg | where{$_ -match '^\*'}) -replace '\s+',' ').Split(' ')

            $fields[6]}},

        @{N='Delay';E={$fields[7]}},

        @{N='Offset';E={$fields[8]}},

        @{N='Jitter';E={$fields[9]}} | ConvertTo-Html | Out-String

}

Send-MailMessage @SMail

 


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
mars0077
Enthusiast
Enthusiast
Jump to solution

Thanks again for helping out on this one sir. I have executed the new version of the script and so far I am getting everything, except for the delay, offset and jitter values. Is there anything I can do differently for lines 18 -25?  We are almost there. Much appreciated.

BTW - In case anyone else is following this one, you need to store your SSH key in the registry. To do this; you will need to execute the plink command from the command line as such: plink -v -ssh "hostname" -l "username" -pw "password". Once you do this, the script Luc provided will work.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Could you show me the output of the 'ntpq -p' command when run in the ESXi BusyBox?

Wondering if the output might differ on your ESXi nodes


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
mars0077
Enthusiast
Enthusiast
Jump to solution

Of course. See below.

~ # ntpq -p

     remote           refid      st t when poll reach   delay   offset  jitter

==============================================================================

*10.0.0.21     208.75.88.4     14 u   27 1024  377    5.391   -4.220  11.536

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Think I got it, was a matter of scoping.

$user = 'root'

$pswd = 'pswd'

$plink = 'C:\Putty\plink.exe'

$plinkoptions = " -batch -pw $Pswd"

$cmd1 = 'ntpq -p'

$remoteCommand = '"' + $cmd1 + '"'

$sMail = @{

    From = 'me@domain.com'

    To = 'me@domain.com'

    SmtpServer = 'mail.domain.com'

    Subject = 'NTP Report'

    BodyAsHtml = $true

    Body = Get-VMHost | Select Name,

        @{N='NTP Service Running';E={Get-VMHostService -VMHost $_ | where{$_.Key -eq 'ntpd'} | select -ExpandProperty Running}},

        @{N='NTP Server(s)';E={(Get-View -Id $_.ExtensionData.ConfigManager.DatetimeSystem).DateTimeInfo.NtpConfig.Server -join '| '}},

        @{N='Reach';E={

            $command = $plink + " " + $plinkoptions + " " + $User + "@" + $_.Name + " " + $remoteCommand

            $msg = Invoke-Expression -command $command

            $script:fields = (($msg | where{$_ -match '^\*'}) -replace '\s+',' ').Split(' ')

            $script:fields[6]}},

        @{N='Delay';E={$script:fields[7]}},

        @{N='Offset';E={$script:fields[8]}},

        @{N='Jitter';E={$script:fields[9]}} | ConvertTo-Html | Out-String

}

Send-MailMessage @SMail


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
mars0077
Enthusiast
Enthusiast
Jump to solution

Thank you very much sir. I will check it out and let you know how things go.

Reply
0 Kudos
mars0077
Enthusiast
Enthusiast
Jump to solution

Sir,

I executed the latest version you provided and there's two things happening:

1. If I connect to vCenter and then execute the script, I do not get the 'ntpq' related info.

2. If I point directly to an ESX host, then I will get the 'ntpq' info, however it will be the same info for all other hosts. See attached screenshot.

Thanks again!

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

1) Is SSH enabled on the ESXi nodes, and (see also 2), did you accept the host key for all ESXi nodes?

2) I guess that this is caused by the fact that you didn't accept the host key for all ESXi nodes.

The following version of the script also automates the accept ion of the host key.

$user = 'root'

$pswd = 'pswd'

$plink = 'C:\Putty\plink.exe'

$plinkoptionsPre = " -pw $Pswd"

$plinkoptions = " -batch -pw $Pswd"

$cmd1 = 'ntpq -p'

$remoteCommand = '"' + $cmd1 + '"'

$sMail = @{

    From = 'me@domain.com'

    To = 'me@domain.com'

    SmtpServer = 'mail.domain.com'

    Subject = 'NTP Report'

    BodyAsHtml = $true

    Body = Get-VMHost | Select Name,

            @{N='NTP Service Running';E={Get-VMHostService -VMHost $_ | where{$_.Key -eq 'ntpd'} | select -ExpandProperty Running}},

            @{N='NTP Server(s)';E={(Get-View -Id $_.ExtensionData.ConfigManager.DatetimeSystem).DateTimeInfo.NtpConfig.Server -join '| '}},

            @{N='Reach';E={

                $command = "echo Y | " + $plink + " " + $plinkoptionsPre + " " + $User + "@" + $_.Name + " " + """exit"""

                $dummy = Invoke-Expression -Command $command

                $command = $plink + " " + $plinkoptions + " " + $User + "@" + $_.Name + " " + $remoteCommand

                $msg = Invoke-Expression -command $command

                $script:fields = (($msg | where{$_ -match '^\*'}) -replace '\s+',' ').Split(' ')

                $script:fields[6]}},

            @{N='Delay';E={$script:fields[7]}},

            @{N='Offset';E={$script:fields[8]}},

            @{N='Jitter';E={$script:fields[9]}} | ConvertTo-Html | Out-String

}

Send-MailMessage @SMail


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

LucD
Leadership
Leadership
Jump to solution

This version of the script will start/stop the SSH service on each ESXi node, this to allow to run the ntpq command

$user = 'root'

$pswd = 'pswd'

$plink = 'C:\Putty\plink.exe'

$plinkoptionsPre = " -pw $Pswd"

$plinkoptions = " -batch -pw $Pswd"

$cmd1 = 'ntpq -p'

$remoteCommand = '"' + $cmd1 + '"'

$sMail = @{

    From = 'me@domain.com'

    To = 'me@domain.com'

    SmtpServer = 'mail.domain.com'

    Subject = 'NTP Report'

    BodyAsHtml = $true

    Body = Get-VMHost | Select Name,

            @{N='NTP Service Running';E={Get-VMHostService -VMHost $_ | where{$_.Key -eq 'ntpd'} | select -ExpandProperty Running}},

            @{N='NTP Server(s)';E={(Get-View -Id $_.ExtensionData.ConfigManager.DatetimeSystem).DateTimeInfo.NtpConfig.Server -join '| '}},

            @{N='Reach';E={

                $serviceSSH = Get-VMHostService -VMHost $_ | Where{$_.Label -eq 'SSH'}

                if(!$serviceSSH.Running){

                    Start-VMHostService -HostService $serviceSSH -Confirm:$false > $null

                }

                $command = "echo Y | " + $plink + " " + $plinkoptionsPre + " " + $User + "@" + $_.Name + " " + """exit"""

                $dummy = Invoke-Expression -Command $command

                $command = $plink + " " + $plinkoptions + " " + $User + "@" + $_.Name + " " + $remoteCommand

                $msg = Invoke-Expression -command $command

                $script:fields = (($msg | where{$_ -match '^\*'}) -replace '\s+',' ').Split(' ')

                if(!$serviceSSH.Running){

                    Stop-VMHostService -HostService $serviceSSH -Confirm:$false > $null

                }

                $script:fields[6]}},

            @{N='Delay';E={$script:fields[7]}},

            @{N='Offset';E={$script:fields[8]}},

            @{N='Jitter';E={$script:fields[9]}} | ConvertTo-Html | Out-String

}

Send-MailMessage @SMail


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

mars0077
Enthusiast
Enthusiast
Jump to solution

Sir,

I believe I have what I needed for this one sir. The script is excellent as long as I manually connect to my vCenter instances. However, I can't get the script connect to my vcenter instances as part of the execution. This is so that I can create a Windows task to automatically run it on a daily basis.

I do have other scripts where I am able to include the 'Connect-VIServer -server' variable and they work manually and scheduled. I can even connect to multiple vcenter instances with my other scripts. Is there something else I need to include so that the script can connect to my vCenter instances?

Thanks!

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You could expand the code that calculates the content of the Body property.

Instead of using the default, you could use a specific vCenter, or use all vCenters.

Let me know if that works, otherwise I can add the code.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
mars0077
Enthusiast
Enthusiast
Jump to solution

Yes, you are correct. I was able to add a one liner to include the connection information for all my vCenter instances. Thanks so much for the help on this one. Much appreciated!

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Thanks for providing the correct answer :smileygrin:


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos