-
Notifications
You must be signed in to change notification settings - Fork 0
/
diskServerMonitor.ps1
145 lines (131 loc) · 4.45 KB
/
diskServerMonitor.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
$list = gc .\list.txt
##EMAIL Setings
$msg = new-object Net.Mail.MailMessage
$smtpServer = "mx1.smtp.net"
$smtp = New-Object Net.Mail.SmtpClient($smtpServer)
$msg.From = “[email protected]”
$msg.To.Add("[email protected]”)
$warning = "80"
$critical = "90"
$warn = "W"
$crit = "C"
foreach ($server in $list)
{
$pegasus = gwmi Win32_LogicalDisk -filter drivetype=3 -computer $server
foreach ($item in $pegasus)
{
$Device = $item.DeviceID
$Disk = gwmi Win32_LogicalDisk -filter drivetype=3 -computer $server | where {$_.DeviceID -match "$Device"}
[long]$total = $Disk.Size
[long]$free = $Disk.Freespace
$used = $total - $free
$percent = $used / $total * 100
$percent = ([math]::Round([decimal]"$percent", 0))
$size = ($Disk.size/1gb)
$size = ([math]::Round([decimal]"$size", 2))
$frees = ($Disk.Freespace/1gb)
$frees = ([math]::Round([decimal]"$frees", 2))
if ($percent -gt $critical)
{
$attmp = "$server$Device"
$atlantis = ($attmp).replace(":", "")
$tmp = Test-Path $atlantis".lck"
Write-Host Does LCK Exist: $tmp -f 10
if ($tmp -ne "False")
{
$crit | out-file $atlantis".lck"
$msg.Subject = "Space Critical on", ($server)
$msg.Body = "Server:",($server), "-", "Total Size GB:", $size, "-", "Free Space GB:", $frees, "-", "Percent:", ($percent), "-", "Threshold:", ($critical),"%"
# Write-Host $msg.Subject
# Write-Host $msg.Body
$smtp.Send($msg)
}
else {
$power = (get-date).AddDays(-3)
$zpm = get-childitem $atlantis".lck"
$status = gc $atlantis".lck"
if ($status -eq "W") {
$crit | out-file $atlantis".lck"
$msg.Subject = "Space Escalated to Critical on", ($server)
$msg.Body = "Server:",($server) , "-", "Total Size GB:", $size, "-", "Free Space GB:", $frees, "-", "Percent:", ($percent), "-", "Threshold:", ($critical),"%"
# Write-Host $msg.Subject
# Write-Host $msg.Body
$smtp.Send($msg)
}
elseif ($zpm.LastWriteTime -gt $power)
{
Write-Host Less than Three Days!
}
else {
# out-file $atlantis".lck"
$msg.Subject = "Space Critical on", ($server), "for over 3 days!"
$msg.Body = "Server:",($server) , "-", "Total Size GB:", $size, "-", "Free Space GB:", $frees, "-", "Percent:", ($percent), "-", "Threshold:", ($critical),"%"
# Write-Host $msg.Subject
# Write-Host $msg.Body
$smtp.Send($msg)
}
}
}
elseif ($percent -gt $warning)
{
$attmp = "$server$Device"
$atlantis = ($attmp).replace(":", "")
$tmp = Test-Path $atlantis".lck"
Write-Host Does TMP Exist: $tmp -f 10
if ($tmp -ne "False")
{
$warn | out-file $atlantis".lck"
$msg.Subject = "Space Warning on", ($server)
$msg.Body = "Server:",($server) , "-", "Total Size GB:", $size, "-", "Free Space GB:", $frees, "-", "Percent:", ($percent), "-", "Threshold:", ($warning),"%"
# Write-Host $msg.Subject
# Write-Host $msg.Body
$smtp.Send($msg)
}
else {
$power = (get-date).AddDays(-3)
$zpm = get-childitem $atlantis".lck"
$status = gc $atlantis".lck"
if ($status -eq "C") {
$warn | out-file $atlantis".lck"
$msg.Subject = "Space Downgraded to Warning on", ($server)
$msg.Body = "Server:",($server) , "-", "Total Size GB:", $size, "-", "Free Space GB:", $frees, "-", "Percent:", ($percent), "-", "Threshold:", ($warning),"%"
# Write-Host $msg.Subject
# Write-Host $msg.Body
$smtp.Send($msg)
}
elseif ($zpm.LastWriteTime -gt $power)
{
Write-Host Less than Three Days!
}
else {
# out-file $atlantis".lck"
$msg.Subject = "Space Warning on", ($server), "for over 3 days!"
$msg.Body = "Server:",($server) , "-", "Total Size GB:", $size, "-", "Free Space GB:", $frees, "-", "Percent:", ($percent), "-", "Threshold:", ($warning),"%"
# Write-Host $msg.Subject
# Write-Host $msg.Body
$smtp.Send($msg)
}
}
}
else
{
$attmp = "$server$Device"
$atlantis = ($attmp).replace(":", "")
$tmp = Test-Path $atlantis".lck"
Write-Host Does LCK Exist: $tmp -f 10
if ($tmp -eq "True")
{
$zpm = get-childitem $atlantis".lck"
$zpm.Delete()
$msg.Subject = "Space Recovered on", ($server)
$msg.Body = "Server:",($server) , "-", "Total Size GB:", $size, "-", "Free Space GB:", $frees, "-", "Percent:", ($percent)
# Write-Host $msg.Subject
# Write-Host $msg.Body
$smtp.Send($msg)
}
else {
Write-Host ($server) - Percent: $percent is less than $warning and $critical
}
}
}
}