-
Notifications
You must be signed in to change notification settings - Fork 0
/
HealthCheckAnywhere.ps1
114 lines (100 loc) · 4.05 KB
/
HealthCheckAnywhere.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
Set-ExecutionPolicy Unrestricted -Scope CurrentUser
$senderMailAddress = "[email protected]"
[string[]]$reipientMailAddress= "[email protected]","[email protected]"
$PingMachines = @(
("10.0.84.123","2638","username","passwd"),#prod anywhere
("10.0.84.124","2638","username","username") #test anywhere
)
function SendAnEmailToCrew($subject, $body, [bool]$AsHtml)
{
if($AsHtml -eq $true)
{
Send-MailMessage -From $senderMailAddress -To $reipientMailAddress -Subject $subject -Body $body -BodyAsHtml -Priority High -SmtpServer 'smtp.contoso.com'
}else
{
Send-MailMessage -From $senderMailAddress -To $reipientMailAddress -Subject $subject -Body $body -Priority High -SmtpServer 'smtp.contoso.com'
}
}
function CheckSqlAnyWhere($name)
{
[void][System.Reflection.Assembly]::LoadWithPartialName("Sap.Data.SQLAnywhere.v4.5")
# Connection string
$connectionString = "Host="+$name[0]+":"+$name[1]+";UserID="+$name[2]+";Password="+$name[3]
# Create SAConnection object
$conn = New-Object Sap.Data.SQLAnywhere.SAConnection($connectionString)
$firstStartDate = (GET-DATE)
Try
{
# Connect to remote IQ server
$conn.Open()
# simple query
$Query = 'select now() as NowDate'
# Create a SACommand object and feed it the simple $Query
$command = New-Object Sap.Data.SQLAnywhere.SACommand($Query, $conn)
# Execute the query on the remote IQ server
$reader = $command.ExecuteReader()
# create a DataTable object
$Datatable = New-Object System.Data.DataTable
# Load the results into the $DataTable object
$DataTable.Load($reader)
# Send the results to the screen in a formatted table
#$DataTable | Format-Table -Auto
$reader.Dispose();
$conn.Close();
$StartDate=[datetime]$DataTable.Rows[0].NowDate
$EndDate=(GET-DATE)
$difInMinutes= NEW-TIMESPAN –Start $StartDate –End $EndDate
#if($difInMinutes.TotalMilliseconds -gt 3000)
#{
# $a = $($difInMinutes.TotalMilliseconds.ToString() +"script duration between open and close connection "+ $(NEW-TIMESPAN –Start $firstStartDate –End $EndDate).TotalMilliseconds.ToString())
# SendAnEmailToCrew 'There was a latency in milisecond(s)' $a $false
#}
#else
#{
# Write-Host $difInMinutes.TotalMilliseconds "latency SqlAnyWhere" -ForegroundColor Green
#}
$DataTable.Dispose();
}
Catch [System.Management.Automation.MethodInvocationException]
{
$er= "SQL Anywhere is Down! Query is 'select now() as NowDate' Check Date is : "+ $(Get-Date).ToString()
Write-Host $er $DataTable.Rows[0].NowDate.ToString()
SendAnEmailToCrew 'SqlAnywhere Connectivity Check Services' $er $false
}
Catch
{
$ErrorMessage = $_.Exception.Message
$FailedItem = $_.Exception.ItemName
SendAnEmailToCrew 'SqlAnywhere Connectivity Check Services' "Failed to read file $FailedItem. The error message was $ErrorMessage" $false
Break;
}
}
function PingCheck()
{
foreach ($name in $PingMachines)
{
if (Test-Connection -ComputerName $name[0] -Count 1 -ErrorAction SilentlyContinue)
{
Write-Host $name[0] "machine is up"
if (Test-NetConnection $name[0] -Port $name[1] -InformationLevel Quiet -ErrorAction SilentlyContinue)
{
Write-Host $name[0] "port" $name[1]" is up"
if($name[1] -eq 2638)
{
Write-Host $name[0] "checking for SqlAnyWhere" -ForegroundColor Gray
CheckSqlAnyWhere $name
}
}
else{
Write-Host $name[0] $name[1]",down"
SendAnEmailToCrew "E-belediye $name port is down!" $name" is down!" $false
}
}
else
{
Write-Host $name[0]",down"
SendAnEmailToCrew "E-belediye Machine is down!" $name[0]" is down!" $false
}
}
}
PingCheck