-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetrics-vmware.ps
155 lines (138 loc) · 4.84 KB
/
metrics-vmware.ps
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
<#
.DESCRIPTION
Use VMware module to connect to vCenter and output all metrics on systems in influxdb line format
.EXAMPLE
CI:
metrics-vmware.ps 'https://influxdb.example.ca:8086/write?db=math&u=telegraf&p=???????&precision=s'
telegraf.cfg:
[[inputs.exec]]
commands = [
'"C:\windows\system32\WindowsPowerShell\v1.0\powershell.exe" -File "C:\Program Files\Telegraf\vmware_collect_stats.ps1"',
]
data_format = "influx"
timeout = "25m"
interval = "30m"
#>
# Allow common parameters
[CmdletBinding()]
## if uri is set, send metrics directly to influx not to stdout
# 'https://influxdb.example.ca:8086/write?db=math&u=telegraf&p=???????&precision=s'
$uri=$args[0]
$vc_host=$(hostname)
# Requirements
try {
$powercli = Get-PSSnapin -Name VMware.VimAutomation.Core -Registered
if ($powercli.Version.Major -ge 6) {
Import-Module -Name VMware.VimAutomation.Core -ErrorAction Stop
} else {
Add-PSSnapin -Name VMware.VimAutomation.Core -ErrorAction Stop
Write-Warning -Message 'PowerCLI 5 snapin added; recommend upgrading your PowerCLI version'
}
}
catch {
write-eventlog -logname Application -source "Telegraf - VMware collect" -eventID 404 -entrytype Error -message 'missing modules vCenter'
throw 'missing modules vCenter'
}
# Ignore self-signed SSL certificates for vCenter Server (optional)
$null = Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -DisplayDeprecationWarnings:$false -Scope User -Confirm:$false
# Connect to vCenter
$null = Connect-VIServer $vc_host -ErrorAction Stop
# default server mode
function handle_stat($h) {
$name = $h.Name -replace ' ', '_'
Write-Debug -Message "Working on: $name"
if ($h.PowerState -eq 'PoweredOff') {
Write-Debug -Message '* skip PoweredOff'
return
}
$fb = ""
if ($h.VMHost) {
$fb = $fb + "vmware_guest,host=$name,esxi_host=$($h.VMHost.Name) "
$fb = $fb + "cpu_NumCpu=$($h.NumCpu),"
$fb = $fb + "mem_MemoryMB=$([math]::round($h.MemoryMB,0)),"
$fb = $fb + "disk_ProvisionedSpaceGB=$([math]::round($h.ProvisionedSpaceGB,2)),"
$fb = $fb + "disk_UsedSpaceGB=$([math]::round($h.UsedSpaceGB,2)) `n"
} else {
# is esxi host
$fb = $fb + "vmware_esxi,host=$name "
$fb = $fb + "cpu_NumCpu=$($h.NumCpu),"
$fb = $fb + "cpu_CpuTotalMhz=$($h.CpuTotalMhz),"
$fb = $fb + "cpu_CpuUsageMhz=$($h.CpuUsageMhz),"
$fb = $fb + "mem_MemoryTotalMB=$([math]::round($h.MemoryTotalMB,0)),"
$fb = $fb + "mem_MemoryUsageMB=$([math]::round($h.MemoryUsageMB,0)) `n"
}
$trust = 20
$stats_map = @{}
$stats = Get-Stat -Entity $h -Stat '*' -Instance '' -Realtime -MaxSamples 1
foreach ($m in $stats) {
$mi = $m.MetricId
$mv = $m.Value
if ($mv -eq 1) {
# some trust issues when value is one... if tomany ones dont trust.
$trust = $trust - 1
}
if ($mv -is [float] -or $mv -is [double]) {
$mv = $([math]::round($mv,5))
} else {
$mv = "$([math]::floor($mv))"
}
$mia = $mi.Split('.', 2)
if ($stats_map.ContainsKey($mia[0])) {
$stats_map.Item($mia[0]).add($mia[1], $mv)
} else {
$stats_map.add($mia[0], @{})
$stats_map.Item($mia[0]).add($mia[1], $mv)
}
if ($mi -eq 'cpu.usage.average') {
$stats_map.Item($mia[0]).add("$($mia[1])_cores", $([math]::round($mv * $h.NumCpu, 3)))
}
}
if ($trust -lt 0) {
# stats with tomany values of one, smells of error
Write-Debug -Message $fb
Write-Debug -Message '* data not trusted'
return 1
}
foreach ($k in $stats_map.Keys) {
$fb = $fb + "vmware_$k,host=$name "
foreach ($s in $stats_map[$k].Keys) {
$sname = $s
#$sname = $sname.Replace('.', '_')
$sname = $sname.Replace(' ', '_')
$sname = $sname.Replace('[', '_')
$sname = $sname.Replace(']', '_')
$fb = $fb + "$sname=$($stats_map["$k"]["$s"]),"
}
$fb = $fb.TrimEnd(',') + " `n"
}
if ($uri) {
$r = Invoke-WebRequest -Uri $uri -Body $fb -Method Post -UseBasicParsing -ErrorAction:Stop
} else {
Write-Host -NoNewline $fb
}
return 0
}
function handle_stat_safe($h, $pfix) {
# preform a retry if error code 1
$r = handle_stat $h $pfix
if ($r -eq 1) {
$r = handle_stat $h $pfix
}
}
try {
# Collect host and vm stats
$vmhosts = Get-VMHost
foreach ($vmhost in $vmhosts) {
handle_stat_safe $vmhost
}
$vms = Get-VM
foreach ($vm in $vms) {
handle_stat_safe $vm
}
}
catch {
write-eventlog -logname Application -source "Telegraf - VMware collect" -eventID 404 -entrytype Error -message $_.Exception.Message
}
Finally {
Disconnect-VIServer -Confirm:$false
}