-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_servers_services.ps1
182 lines (158 loc) · 6.61 KB
/
check_servers_services.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# Script to check if server services are running
# Get alerted on Telegram
# Author : Bairaktaris Emmanuel
# Date : December 26, 2024
# Last version: January 2, 2025
# Link : https://sonam.dev
# === CONFIGURATION ===
$botToken = [System.Environment]::GetEnvironmentVariable("BOT_TOKEN", "Machine")
$chatID = [System.Environment]::GetEnvironmentVariable("BOT_CHAT_ID", "Machine")
# Define a hashtable with each server and its respective services
$serversToMonitor = @{
"server01" = @("APCPBEAgent")
"server02" = @("nodeportal.exe")
"server03" = @("Epsilon Application Service (Business Payroll)", "ZKAccessPush.ServiceApp")
"server04" = @("SasWatchDg", "OracleServiceSEN", "OracleVssWriterSEN", "OracleOraDB12Home1MTSRecoveryService", "OracleOraDB12Home1TNSListener", "SenDaemonSvc")
}
# Initialize a hashtable to store the previous states of services
$serviceStates = @{}
$checkInterval = 120 # Time in seconds to wait between each check (e.g., 120 seconds = 2 minutes)
$reportTimes = @("07:50", "10:35", "14:20") # Times to send the status report
$lastReportTime = $null # Track when the last report was sent
#function Send-TelegramMessage {
# param (
# [string]$message
# )
#
# $url = "https://api.telegram.org/bot$botToken/sendMessage"
# $payload = @{
# chat_id = $chatID
# text = $message
# }
#
# try {
# $jsonPayload = $payload | ConvertTo-Json -Depth 3
# $response = Invoke-RestMethod -Uri $url -Method Post -ContentType "application/json; charset=utf-8" -Body $jsonPayload
#
# if ($response.ok -eq $true) {
# Write-Host "✅ Message sent successfully."
# } else {
# Write-Host "❌ Failed to send message to Telegram."
# }
# }
# catch {
# Write-Host "⚠️ Error sending message: $_"
# }
#}
# Send Telegram Message
function Send-TelegramMessage {
param (
[string]$message
)
# Define the log file path
$logFilePath = "C:\tools\scripts\ServicesTelegramErrors.log"
# Use the raw emoji characters in the message
$url = "https://api.telegram.org/bot$botToken/sendMessage"
$payload = @{
chat_id = $chatID
text = $message
}
try {
# Convert to JSON, ensuring encoding is preserved
$jsonPayload = $payload | ConvertTo-Json -Depth 3 -Compress
# Send the message with the correct UTF-8 encoding
$response = Invoke-RestMethod -Uri $url -Method Post -ContentType "application/json; charset=utf-8" -Body $jsonPayload
if ($response.ok -eq $true) {
Write-Host "✅ Message sent successfully."
} else {
# Log the failed message
$errorMessage = "❌ Failed to send message at $(Get-Date): $message`nResponse: $($response | ConvertTo-Json -Depth 3)"
Add-Content -Path $logFilePath -Value $errorMessage
Write-Host $errorMessage
}
}
catch {
# Log the exception and the failed message
$errorMessage = "⚠️ Error sending message at $(Get-Date): $message`nException: $_"
Add-Content -Path $logFilePath -Value $errorMessage
Write-Host $errorMessage
}
}
# Function to check the status of services on each server
function CheckServices {
foreach ($server in $serversToMonitor.Keys) {
$servicesToMonitor = $serversToMonitor[$server]
if (-not $serviceStates.ContainsKey($server)) {
$serviceStates[$server] = @{}
}
foreach ($serviceName in $servicesToMonitor) {
try {
$service = Invoke-Command -ComputerName $server -ScriptBlock {
param($serviceName)
Get-Service -Name $serviceName -ErrorAction SilentlyContinue
} -ArgumentList $serviceName
if ($null -eq $service) {
if ($serviceStates[$server][$serviceName] -ne 'Not Found') {
Send-TelegramMessage "⚠️ ALERT: '$serviceName' not found on '$server'."
}
$serviceStates[$server][$serviceName] = 'Not Found'
}
elseif ($service.Status -ne 'Running') {
if ($serviceStates[$server][$serviceName] -ne 'Stopped') {
$serviceStates[$server][$serviceName] = 'Stopped'
Send-TelegramMessage "❌ ALERT: '$serviceName' on '$server' is DOWN."
}
}
else {
if ($serviceStates[$server][$serviceName] -eq 'Stopped') {
$serviceStates[$server][$serviceName] = 'Running'
Send-TelegramMessage "✅ ALERT: '$serviceName' on '$server' is Running again."
}
elseif ($null -eq $serviceStates[$server][$serviceName]) {
$serviceStates[$server][$serviceName] = 'Running'
}
}
}
catch {
Send-TelegramMessage "❌ ERROR: Unable to connect to server '$server' or get status of service '$serviceName'. Error: $_"
}
}
}
}
# Function to generate and send a periodic status report
function Send-StatusReport {
$statusReport = "📝 Service Status Report for all servers:`n"
foreach ($server in $serversToMonitor.Keys) {
$statusReport += "Server: $server`n"
$servicesToMonitor = $serversToMonitor[$server]
foreach ($serviceName in $servicesToMonitor) {
$status = if ($serviceStates[$server][$serviceName]) {
$serviceStates[$server][$serviceName]
} else {
"Unknown"
}
# Add ✅ or ❌ depending on the status
$statusIcon = switch ($status) {
'Running' { '✅' }
'Stopped' { '❌' }
'Not Found' { '⚠️' }
default { '❓' } # If the status is unknown, use a question mark emoji
}
# Append the server, service, and status with emoji
$statusReport += "`t- ${statusIcon} ${serviceName}: $status`n"
}
$statusReport += "`n"
}
Send-TelegramMessage $statusReport
}
# Main loop to check the services periodically
while ($true) {
CheckServices
$currentTime = (Get-Date).ToString("HH:mm")
# Send the report only if it's at a specified report time and not already sent in this minute
if ($reportTimes -contains $currentTime -and $lastReportTime -ne $currentTime) {
Send-StatusReport
$lastReportTime = $currentTime
}
Start-Sleep -Seconds $checkInterval
}