-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVMMonitoring.psm1
More file actions
129 lines (112 loc) · 4.09 KB
/
Copy pathVMMonitoring.psm1
File metadata and controls
129 lines (112 loc) · 4.09 KB
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
$ConfPath = $PSScriptRoot+"\Configuration.json"
If((Test-Path $ConfPath) -eq $False) {
New-Item -Path $ConfPath -ItemType File
$json = "{
}" | Out-File $ConfPath
}
Function Get-MonitoringMode {
<#
.SYNOPSIS
Retrieve the monitoring mode of a virtual machine
.EXAMPLE
Get-MonitoringMode -VMId c885c954-b9d0-4f58-a3a0-19cf21ea7980
.DESCRIPTION
Retrieve the monitoring mode of a virtual machine
.NOTES
Function called on virtual machine details page on app
.LINK
https://github.com/Goldenlagen/EasyCloud_PSModules/tree/main/VMMonitoring
#>
Param(
[Parameter(Mandatory)]
$VMId
)
Process {
$Data = Get-Content $ConfPath | ConvertFrom-Json
Return $Data.$VMId
}
}
Function Update-MonitoringMode {
<#
.SYNOPSIS
Change monitoring mode to true or false and return confirmation
.EXAMPLE
Update-MonitoringMode -VMId c885c954-b9d0-4f58-a3a0-19cf21ea7980 -isMonitored $True -VirtualizationServer VMSRV01
.INPUTS
ID of a virtual machine
Monitoring value as true or false
Name of a virtualization server
.OUTPUTS
Confirmation message
.DESCRIPTION
This function will update the configuration file, set true or false the monitoring mode for the provided
virtual machine name and it will enable ressource metering on true and disable resource metering on false
.NOTES
Function called on virtual machine details page on app when button monitoring on/off is activated
.LINK
https://github.com/Goldenlagen/EasyCloud_PSModules/tree/main/VMMonitoring
#>
Param(
[Parameter(Mandatory)]
$VMId,
[Parameter(Mandatory)]
[ValidateSet($false, $true, 0, 1)]
$isMonitored,
[Parameter(Mandatory)]
$VirtualizationServer
)
Process {
$VMName = (Get-VM -Id $VMId -ComputerName $VirtualizationServer).Name
$Data = Get-Content $ConfPath | ConvertFrom-Json
If($null -ne $Data.$VMId) {
$Data.$VMId = $isMonitored
} Else {
$Data | Add-Member @{$VMId = $isMonitored}
}
If($Data.$VMId) {
Enable-VMResourceMetering -VMName $VMName -ComputerName $VirtualizationServer
}
Else {
Disable-VMResourceMetering -VMName $VMName -ComputerName $VirtualizationServer
}
$Data | ConvertTo-Json | Out-File $ConfPath
Return "Monitoring have been set to $isMonitored for VirtualMachine $VMName"
}
}
Function Get-MonitoringData {
<#
.SYNOPSIS
Retrieving monitoring data (CPU, RAM, Disk) to display in app graph
.EXAMPLE
Get-MonitoringData -VMId c885c954-b9d0-4f58-a3a0-19cf21ea7980 -VirtualizationServer VMSRV01
.INPUTS
ID of a virtual machine
Name of a virtualization server
.OUTPUTS
String data formated into JSON
.DESCRIPTION
This function will collect monitoring data, cpu, ram, disk by using measure vm command it will format the
output as a JSON
.NOTES
Function called every n seconds when on virtual machine details page and monitoring is activated
.LINK
https://github.com/Goldenlagen/EasyCloud_PSModules/tree/main/VMMonitoring
#>
Param(
[Parameter(Mandatory)]
$VMId,
[Parameter(Mandatory)]
$VirtualizationServerName
)
Process {
$Config = Get-Content $ConfPath | ConvertFrom-Json
$VMName = (Get-VM -Id $VMId -ComputerName $VirtualizationServerName).Name
If($Config.$VMId) {
Return (Measure-VM -VMName $VMName -ComputerName $VirtualizationServerName | Select-Object VMName, AvgRam, VMId, AvgCPU, TotalDisk | ConvertTo-Json)
}
Else {
Return "Monitoring disabled"
}
}
}
Export-ModuleMember -Function Update-MonitoringMode, Get-MonitoringData, Get-MonitoringMode