forked from taishin/rbvmoni-zabbix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrbvmoni-zabbix.rb
executable file
·359 lines (312 loc) · 9.91 KB
/
rbvmoni-zabbix.rb
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#!/usr/bin/ruby
require 'rubygems'
require 'zbxapi'
require 'logger'
require 'rbvmomi'
require 'fileutils'
def print_usage
puts "usage: rbvmomi_zabbix.rb (vCenter Host) (vCenter Username) (vCenter Password) (Prefix Groups Name) (Zabbix URL)"
exit
end
print_usage if ARGV.size != 5
vcHost = ARGV[0]
vcUser = ARGV[1]
vcPass = ARGV[2]
dsName = ARGV[3]
$zbxUrl = ARGV[4]
##################################
#User Defined Parameters
#Exclude Vmware Templates? (jaganz)
$includeVmwareTemplates = false
#Deprovisioning Method (delete host or move to deprovisioned host group?)
$EnableDeprovisioningHostGroup = true
$DEPROV_GROUP = "Deprovisioned Hosts"
#Zabbix Valid Login
$zuser = "Admin"
$zpass = "zabbix"
#Other Related Stuff
ESX_GROUP = "#{dsName} ESXi"
DS_GROUP = "#{dsName} Datastore"
VM_GROUP = "#{dsName} VirtualMachine"
ESX_TEMPLATE = "Template-vSphere-ESXi"
DS_TEMPLATE = "Template-vSphere-Datastore"
VM_TEMPLATE = "Template-vSphere-VM"
FILEPATH = "/tmp/vsphere"
################
# SCRIPT START #
################
class Zbx < ZabbixAPI
def initialize()
@user = $zuser
@pass = $zpass
begin
@zbxapi = ZabbixAPI.new($zbxUrl).login(@user, @pass)
rescue => exc
p exc
$log.error(exc)
exit
end
end
def send_zbx(zbxCmd, zbxHash)
begin
@zbxapi.raw_api(zbxCmd, zbxHash)
rescue => exc
p exc
$log.error(exc)
exit
end
end
def search_zbxGroup(groupName)
zbxHash = {
:name => groupName
}
send_zbx("hostgroup.exists", zbxHash)
end
def create_zbxGroup(groupName)
zbxHash = {
:name => groupName
}
send_zbx("hostgroup.create", zbxHash)
end
def get_zbxGroupId(groupName)
if search_zbxGroup(groupName)
zbxHash = {
:name => groupName
}
send_zbx("hostgroup.getobjects", zbxHash)[0]['groupid']
else
create_zbxGroup(groupName)['groupids'][0]
end
end
def search_zbxTemplate(templateName)
zbxHash = {
:name => templateName
}
send_zbx("template.exists", zbxHash)
end
def get_zbxTemplateId(templateName)
unless search_zbxTemplate(templateName)
errmsg = "#{templateName} is not exist."
$log.error(errmsg)
abort(errmsg)
end
@zbxapi.raw_api("template.getobjects", {
:name => templateName
})[0]['templateid']
end
def search_zbxHost(hostName)
zbxHash = {
:host => hostName
}
send_zbx("host.exists", zbxHash)
end
def create_zbxHost(hosts, groupName, templateName)
@groupId = get_zbxGroupId(groupName)
@templateId = get_zbxTemplateId(templateName)
hosts.each {|host|
zbxHash = {
:host => host,
:interfaces => [{
:type => 1,
:main => 1,
:useip => 1,
:ip => "127.0.0.1",
:dns => "",
:port => "10050"
}],
:groups => [{
:groupid => @groupId
}],
:templates => [{
:templateid => @templateId
}]
} unless search_zbxHost(host)
send_zbx("host.create", zbxHash)
}
end
def get_zbxHostId(hostName)
zbxHash = {
:host => hostName
}
send_zbx("host.getobjects", zbxHash)[0]['hostid']
end
def delete_zbxHost(hostName)
zbxHash = {
:hostid => get_zbxHostId(hostName)
} if search_zbxHost(hostName)
send_zbx("host.delete", zbxHash) if zbxHash
end
def deprov_zbxHost(hostName, deprovGroup)
if search_zbxGroup(deprovGroup) == false
create_zbxGroup(deprovGroup)
end
@deprovGroupid = get_zbxGroupId(deprovGroup)
zbxHash = {
:hostid => get_zbxHostId(hostName),
:groups => [{
:groupid => @deprovGroupid
}],
:status => 1
} if search_zbxHost(hostName)
send_zbx("host.update", zbxHash) if zbxHash
end
end
class VSphere < RbVmomi::VIM
def initialize(host, user, pass)
@host = host
@user = user
@password = pass
begin
@vim = RbVmomi::VIM.connect :host => @host, :user => @user, :password => @password, :insecure => true
rescue => exc
p exc
$log.error(exc)
exit
end
begin
@dc = @vim.serviceInstance.find_datacenter
rescue => exc
p exc
$log.error(exc)
exit
end
end
def get_host_status(type)
new_list = Array.new
case type
when "host"
@dc.hostFolder.childEntity.each do |vmhost|
vmhost.host.grep(RbVmomi::VIM::HostSystem).each do |stat|
newname = stat.name.gsub(/:/,"-")
stat_fileName = "h_#{newname}"
new_list << newname unless File.exist?($filePath + stat_fileName)
statData = {
"host-Hostname" => stat.name,
"host-Product" => stat.summary.config.product.fullName,
"host-HardwareMode" => stat.summary.hardware.model,
"host-CPUModel" => stat.summary.hardware.cpuModel,
"host-CPUMHz" => stat.summary.hardware.cpuMhz,
"host-CPUCore" => stat.summary.hardware.numCpuCores,
"host-CPUUsage" => stat.summary.quickStats.overallCpuUsage,
"host-TotalMemorySize" => stat.summary.hardware.memorySize/1024/1024,
"host-MemoryUsage" => stat.summary.quickStats.overallMemoryUsage,
"host-PowerState" => stat.summary.runtime.powerState,
"host-MaintenanceMode" => stat.summary.runtime.inMaintenanceMode,
"host-Uptime" => stat.summary.quickStats.uptime
}
writefile(stat_fileName, statData)
end
if new_list.length > 0
unless defined?(@zbxapi)
@zbxapi = Zbx.new
end
@zbxapi.create_zbxHost(new_list, ESX_GROUP, ESX_TEMPLATE)
end
end
when "ds"
@dc.datastore.grep(RbVmomi::VIM::Datastore).each do |stat|
newname = stat.name.gsub(/:/,"-")
stat_fileName = "d_#{newname}"
new_list << newname unless File.exist?($filePath + stat_fileName)
vm_list =[]
stat.vm.grep(RbVmomi::VIM::VirtualMachine).each {|v| vm_list << v.name }
statData = {
"ds-Name" => stat.name,
"ds-Capacity" => stat.summary.capacity,
"ds-FreeSpace" => stat.summary.freeSpace,
"ds-VM" => vm_list.join(', ')
}
writefile(stat_fileName, statData)
end
if new_list.length > 0
unless defined?(@zbxapi)
@zbxapi = Zbx.new
end
@zbxapi.create_zbxHost(new_list, DS_GROUP, DS_TEMPLATE)
end
when "vm"
@dc.vmFolder.childEntity.grep(RbVmomi::VIM::VirtualMachine).each do |stat|
if stat.summary.config.template != true || (stat.summary.config.template == true && includeVmwareTemplates == true ) #exclude templates (jaganz)
newname = stat.name.gsub(/:/,"-")
stat_fileName = "v_#{newname}"
new_list << newname unless File.exist?($filePath + stat_fileName)
statData = {
"vm-Name" => stat.name,
"vm-ESXi" => stat.runtime.host.name,
"vm-powerState" => stat.summary.runtime.powerState,
"vm-guestFullName" => stat.summary.guest.guestFullName,
"vm-HostName" => stat.summary.guest.hostName,
"vm-IPAddress" => stat.summary.guest.ipAddress,
"vm-VMwareTools" => stat.summary.guest.toolsStatus,
"vm-maxCpuUsage" => stat.summary.runtime.maxCpuUsage,
"vm-numCpu" => stat.summary.config.numCpu,
"vm-overallCpuUsage" => stat.summary.quickStats.overallCpuUsage,
"vm-memorySizeMB" => stat.summary.config.memorySizeMB,
"vm-hostMemoryUsage" => stat.summary.quickStats.hostMemoryUsage,
"vm-guestMemoryUsage" => stat.summary.quickStats.guestMemoryUsage,
"vm-UncommittedStorage" => stat.summary.storage.uncommitted,
"vm-UsedStorage" => stat.summary.storage.committed,
"vm-UnsharedStorage" => stat.summary.storage.unshared,
"vm-Uptime" => stat.summary.quickStats.uptimeSeconds
}
writefile(stat_fileName, statData)
end
if new_list.length > 0
unless defined?(@zbxapi)
@zbxapi = Zbx.new
end
@zbxapi.create_zbxHost(new_list, VM_GROUP, VM_TEMPLATE)
end
end #exclude Templates
end
end
end
def writefile(fileName, data)
begin
statsFile = open($filePath + fileName, "w")
rescue => exc
p exc
$log.error(exc)
exit
end
data.each_pair {|key, value| statsFile.puts "#{key}:#{value}"}
statsFile.close
end
#Manage Deprovisioning based on delta time of last updated stat files
def stats_file_age_check(time)
Dir::glob($filePath + "*").each do |f|
if Time.now - File.stat(f).mtime >= time
/\A[vhd]_(.*)\z/ =~ File.basename(f)
unless defined?(zbxapi)
@zbxapi = Zbx.new
end
if $EnableDeprovisioningHostGroup == true
if $DEPROV_GROUP.to_s.strip.length == 0
log.error("Error in deprovisioning step: $EnableDeprovisioningHostGroup is activated but not DEPROV_GROUP is defined.")
raise "$EnableDeprovisioningHostGroup is activated but DEPROV_GROUP is not defined."
else
@zbxapi.deprov_zbxHost($1, $DEPROV_GROUP)
$log.info($1 + " deprovisioned (moved into deprovisioning group) on zabbix after " + time.to_s + " seconds without updates")
end
else
$log.info($1 + " deprovisioned (deleted) on zabbix after " + time.to_s + " seconds without updates")
@zbxapi.delete_zbxHost($1)
File.delete(f)
end
end
end
end
def stats_file_check(zbx_host, fileName)
create_zbxHost(hosts, groupName, templateName) unless File.exist?(fileName)
end
$filePath = FILEPATH + "/stats/"
FileUtils.mkdir_p($filePath) unless File.exists?($filePath)
logPath = FILEPATH + "/logs/"
FileUtils.mkdir_p(logPath) unless File.exists?(logPath)
$log = Logger.new(logPath + 'rbvmoni-zabbix.log', 'weekly')
stats_file_age_check(3600 * 24)
ds = VSphere.new(vcHost, vcUser, vcPass)
ds.get_host_status("host")
ds.get_host_status("ds")
ds.get_host_status("vm")
puts 0