Tuesday, December 2, 2014

PowerShell script to check the last 5 mins real-time VMWARE CPU READY SUMMATION

#add-pssnapin VMware.VimAutomation.Core
param(
$vm=$(throw "Provide the vm server name as is in the Vsphere"),
$vcenter=$(throw "Provide the Vcenter name example: VCenter Server"),
$port=$(throw "Provide the port number to connect example: 443"),
$user=$(throw "Provide the read-only user name to connect to Vcenter example: Domain\username"),
$pass=$(throw " Provide the password to connect to the vCenter password")
)
#$vm = "VMware Server"
#$vcenter = "Vcenter Server"
#$port = 443
#$user = "Domain\username"
#$pass = "password"
$statcpuready = 0
add-pssnapin VMware.VimAutomation.Core
#Write-Host $vm $vcenter $port $user $pass
$certresponse = Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false
$return = Connect-VIServer -Server $vcenter -Port $port -User $user -Password $pass

$vcpu = (Get-VM -Name $vm).NumCpu
[int]$statcpuready = ((Get-VM -Name $vm | Get-Stat -Stat cpu.ready.summation -Realtime| Where {$_.Instance -eq ""}| Select-Object -First 15 value | Measure-Object -Property Value -Average).Average)
#Write-Host "No. of vCPU:"$vcpu ",CPU ready summation in (ms):" $statcpuready
if ( $vcpu -eq 4 ) # warning 4000ms Critical 7000ms
{
$warnlevel = 20
$warnlevelms = 4000
$criticallevel = 35
$criticallevelms = 7000
}
if ( $vcpu -eq 2 ) # warning 2000ms critical 3500ms
{
$warnlevel = 10
$warnlevelms = 2000
$criticallevel = 17
$criticallevelms = 3500
}
if ( $vcpu -eq 1 ) # warning 1000ms critical 1500ms
{
$warnlevel = 5
$warnlevelms = 1000
$criticallevelms = 1500
}
$statcpureadyper = (($statcpuready) / (20*1000))*100
#Write-Host $statcpureadyper
if ($statcpureadyper -gt $criticallevel) {
Write-Host "CRITICAL:CPU Ready (Avg 5mins)%="$statcpureadyper"|CPUReadySummation(Avg5mins)(ms)="$statcpuready";"$warnlevelms";"$criticallevelms
exit(2)
} elseif ($statcpureadyper -gt $warnlevel) {
Write-Host "WARNING:CPU Ready (Avg 5mins)%="$statcpureadyper"|CPUReadySummation(Avg5mins)(ms)="$statcpuready";"$warnlevelms";"$criticallevelms
exit(1)
} elseif ($statcpureadyper -lt $warnlevel) {
Write-Host "OK:CPU Ready (Avg 5mins)%="$statcpureadyper"|CPUReadySummation(Avg5mins)(ms)="$statcpuready";"$warnlevelms";"$criticallevelms
#Write-Host "OK:CPU Ready (Avg 5mins)%="$statcpureadyper"|CPU Ready (Avg 5mins)%="$statcpureadyper"%;"$warnlevel";"$criticallevel";0;100;CPU Ready Summation in (ms)(Avg 5mins)="$statcpuready";0;0"
exit(0)
}
disconnect-viserver -confirm:$false

PowerShell Script to change the CNAME Target host name on the DNS server

#PowerShell Code to change the CNAME Target host name in the DNS, this was designed to meet, #failover of the servers and need to be triggered on the DNS server to switch to Stand-by server




param(
[string] $ServerName=$(throw "DNS Server parameter is required example:.\dnsA.ps1 xyz.domain.com domain.com democname.domain.com demo1.domain.com"),
[string] $ContainerName=$(throw "Container Name or Domain Name example.com required example:.\dnsA.ps1 xyz.domain.com domain.com democname.domain.com demo1.domain.com"),
[string] $ownername=$(throw "Provide the Cname is required example:.\dnsA.ps1 xyz.domain.com domain.com democname.domain.com demo1.domain.com"),
[string] $newprimaryName=$(throw "target Host Name to be changed is required example:.\dnsA.ps1 xyz.domain.com domain.com democname.domain.com demo1.domain.com")
)

#Write-Host $serverName, $ContainerName, $ownerName, $newprimaryName
$rec=Get-WMIObject -Computer $ServerName `
    -Namespace "root\MicrosoftDNS" -Class "MicrosoftDNS_CNAMEType" `
    -Filter "ContainerName='$ContainerName' AND OwnerName='$ownerName'"
$newprimaryName = $newprimaryName + "."
#Write-Host $newprimaryName


if ($newprimaryName.equals($rec.PrimaryName))
{
Write-Host "You are passing the same target hostname to be changed"
exit(2)
}
else
{
$res = $rec.modify($rec.TTL,$newprimaryName)
}


$ret=Get-WMIObject -Computer $ServerName `
    -Namespace "root\MicrosoftDNS" -Class "MicrosoftDNS_CNAMEType" `
    -Filter "ContainerName='$ContainerName' AND OwnerName='$ownerName'"


if ($newprimaryName.equals($ret.PrimaryName))
{
Write-Host "Successful change:"
Write-Host $ret.PrimaryName
exit(0)
}


$ret=$null
$rec=$null

Friday, November 7, 2014

To check http or https webpage availability using www::Mechanize perl module

#!/usr/bin/perl
#use strict;
#use warnings;
use WWW::Mechanize;
use HTTP::Cookies;
use Time::HiRes;
use Getopt::Long;
my ($expect,$url);
GetOptions(
        'url1=s'                 => \$url1,
        'expect=s'              => \$expect,
        'debug=s'               => \$debug) or HELP_MESSAGE();

my $mech = WWW::Mechanize->new(autocheck => 0);
$mech->cookie_jar(HTTP::Cookies->new());
my $start1 = Time::HiRes::time();
$mech->get ($url1);
my $responsecode1 = $mech->status();
if (defined $debug)
{
print $mech->content( raw => 1 );
}
my $time1 = sprintf("%.2f",Time::HiRes::time()-$start1);
 if( $responsecode1 != 200 )
        {
#       my $time1 = sprintf("%.2f",Time::HiRes::time()-$start1);
        print "Unable to get the URL, check the URL, response code:$responsecode1". "|time=" . $time1 . "s;60" . "\n";
        exit(2);
        }
else
{
        $match= $mech->content( raw => 1 );
        if ($match =~ m/$expect/)
         {
        print "response code:$responsecode1". "|time=" . $time1 . "s;60" . "\n";
        exit(0);
         }
        else
        {
        print "Content Match error, possible redirection, response code:$responsecode1". "|time=" . $time1 . "s;60" . "\n";
        exit(2);
        }
}
sub HELP_MESSAGE {
        print "$0 -u <url> -e <expect> -d \"debug\"\n";
        print "\t -u <url> # url string to run basic auth against do not prefix with http or https\n";
        print "\t -e <expect> # string to query on the page\n";
        print "\t -d \"debug\"# debugging mode print page content\n";
        print "\t e.g $0 -u \"https\:\/\/foobar.com\/foo\" -e \"Hello\"\n";
        print "\t Please follow the syntax as mentioned above in sequence don't change the parameter position \n";
        exit 0;
}

Thursday, October 30, 2014

To replace the nsclient.ini file content on the remote server and restart the nscp service.


To replace the nsclient.ini file content on the remote server and restart the nscp service.




clear
ForEach ($system in Get-Content "C:\system.txt")
{
Write-Host $system
get-childItem "\\$system\c$\Program Files\NSClient++\nsclient.ini" -recurse | ForEach {
(Get-Content $_ | ForEach {$_ -replace 'allow arguments = false', 'allow arguments = true'}) | Set-Content $_
}
get-childItem "\\$system\c$\Program Files\NSClient++\nsclient.ini" -recurse | ForEach {
(Get-Content $_ | ForEach {$_ -replace 'allow nasty characters = false', 'allow nasty characters = true'}) | Set-Content $_
}
#Change this values to suit your needs:
$SvcName = 'nscp'
$SvrName = $system
#Initialize variables:
[string]$WaitForIt = ""
[string]$Verb = ""
[string]$Result = "FAILED"
$svc = (get-service -computername $SvrName -name $SvcName)
Write-host "$SvcName on $SvrName is $($svc.status)"
Switch ($svc.status) {
'Stopped' {
Write-host "Starting $SvcName..."
$Verb = "start"
$WaitForIt = 'Running'
$svc.Start()}
'Running' {
Write-host "Stopping $SvcName..."
$Verb = "stop"
$WaitForIt = 'Stopped'
$svc.Stop()}
Default {
Write-host "$SvcName is $($svc.status). Taking no action."}
}
if ($WaitForIt -ne "") {
Try { # For some reason, we cannot use -ErrorAction after the next statement:
$svc.WaitForStatus($WaitForIt,'00:00:10')
} Catch {
Write-host "After waiting for 10 Seconds, $SvcName failed to $Verb."
}
$svc = (get-service -computername $SvrName -name $SvcName)
if ($svc.status -eq $WaitForIt) {$Result = 'SUCCESS'}
Write-host "$Result`: $SvcName on $SvrName is $($svc.status)"
}
$svc.Start()
$WaitForIt = 'Running'
if ($WaitForIt -ne "") {
Try { # For some reason, we cannot use -ErrorAction after the next statement:
$svc.WaitForStatus($WaitForIt,'00:00:10')
} Catch {
Write-host "After waiting for 10 Seconds, $SvcName failed to $Verb."
}
$svc = (get-service -computername $SvrName -name $SvcName)
if ($svc.status -eq $WaitForIt) {$Result = 'SUCCESS'}
else { $Result = 'UNSUCCESSFUL'}
Write-host "$Result`: $SvcName on $SvrName is $($svc.status)"
}
#Invoke-Command -ComputerName $system {Restart-Service nscp}
Write-Host "----------------------------------------------"
}
# c:\system.txt should contain the server names in every newline.
# The Invoke-Command requires WinRM service to be running to restart the remote service and it need to be configured as well, using "winrm quickconfig"