Module: VirtualMachineMonitor

Included in:
VCenterDriver::VirtualMachine
Defined in:
lib/vm_monitor.rb

Overview

————————————————————————– # Copyright 2002-2022, OpenNebula Project, OpenNebula Systems #

#

Licensed under the Apache License, Version 2.0 (the “License”); you may # not use this file except in compliance with the License. You may obtain # a copy of the License at #

#

www.apache.org/licenses/LICENSE-2.0 #

#

Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an “AS IS” BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ————————————————————————— #

Constant Summary collapse

POLL_ATTRIBUTE =
OpenNebula::VirtualMachine::Driver::POLL_ATTRIBUTE
VM_STATE =
OpenNebula::VirtualMachine::Driver::VM_STATE

Instance Method Summary collapse

Instance Method Details

#infoObject

Generates a OpenNebula IM Driver valid string with the monitor info



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
# File 'lib/vm_monitor.rb', line 180

def info
    # return 'STATE=d' if @state == 'd'

    if @vm_info
        guest_ip = @vm_info['guest.ipAddress']
    else
        guest_ip = self['guest.ipAddress']
    end

    used_cpu    = @monitor[:used_cpu]
    used_memory = @monitor[:used_memory]
    netrx       = @monitor[:netrx]
    nettx       = @monitor[:nettx]
    diskrdbytes = @monitor[:diskrdbytes]
    diskwrbytes = @monitor[:diskwrbytes]
    diskrdiops  = @monitor[:diskrdiops]
    diskwriops  = @monitor[:diskwriops]

    if @vm_info
        esx_host = @vm_info[:esx_host_name].to_s
    else
        esx_host = self['runtime.host.name'].to_s
    end

    if @vm_info
        guest_state = @vm_info['guest.guestState'].to_s
    else
        guest_state = self['guest.guestState'].to_s
    end

    if @vm_info
        vmware_tools = @vm_info['guest.toolsRunningStatus'].to_s
    else
        vmware_tools = self['guest.toolsRunningStatus'].to_s
    end

    if @vm_info
        vm_name = @vm_info['name'].to_s
    else
        vm_name = self['name'].to_s
    end

    if @vm_info
        vmtools_ver = @vm_info['guest.toolsVersion'].to_s
    else
        vmtools_ver = self['guest.toolsVersion'].to_s
    end

    if @vm_info
        vmtools_verst = @vm_info['guest.toolsVersionStatus2'].to_s
    else
        vmtools_verst = self['guest.toolsVersionStatus2'].to_s
    end

    if @vm_info
        rp_name = @vm_info[:rp_list]
                  .select do |item|
                      item[:ref] == @vm_info['resourcePool']._ref
                  end
                  .first[:name] rescue ''

        rp_name = 'Resources' if rp_name.empty?
    else
        rp_name = self['resourcePool'].name
    end

    str_info = ''

    str_info = 'GUEST_IP=' << guest_ip.to_s << "\n" if guest_ip

    if @guest_ip_addresses && !@guest_ip_addresses.empty?
        str_info << 'GUEST_IP_ADDRESSES="' << @guest_ip_addresses.to_s \
                 << '" '
    end

    str_info << "#{POLL_ATTRIBUTE[:cpu]}=" << used_cpu.to_s << "\n"
    str_info << "#{POLL_ATTRIBUTE[:memory]}=" << used_memory.to_s << "\n"
    str_info << "#{POLL_ATTRIBUTE[:netrx]}=" << netrx.to_s << "\n"
    str_info << "#{POLL_ATTRIBUTE[:nettx]}=" << nettx.to_s << "\n"

    str_info << 'DISKRDBYTES=' << diskrdbytes.to_s << "\n"
    str_info << 'DISKWRBYTES=' << diskwrbytes.to_s << "\n"
    str_info << 'DISKRDIOPS='  << diskrdiops.to_s  << "\n"
    str_info << 'DISKWRIOPS='  << diskwriops.to_s  << "\n"

    str_info << 'VCENTER_ESX_HOST="' << esx_host << '" ' << "\n"
    str_info << 'VCENTER_GUEST_STATE=' << guest_state << "\n"
    str_info << 'VCENTER_VM_NAME="' << vm_name << '" ' << "\n"
    str_info << 'VCENTER_VMWARETOOLS_RUNNING_STATUS=' \
             << vmware_tools << "\n"
    str_info << 'VCENTER_VMWARETOOLS_VERSION=' << vmtools_ver << "\n"
    str_info << 'VCENTER_VMWARETOOLS_VERSION_STATUS=' \
             << vmtools_verst << "\n"
    str_info << 'VCENTER_RP_NAME="' << rp_name << '" ' << "\n"

    info_disks.each do |disk|
        next if disk[1].no_exists?

        # disk[0] contains the disk ID in OpenNebula or the disk path if
        #         there is no corresponding OpenNebula disk
        # disk[1] contains the vcenter resource corresponding to the VM disk

        # Delete special characters
        name = disk[0].gsub(/[^0-9A-Za-z]/, '_')

        str_info << "DISK_#{name}_ACTUAL_PATH=\"[" <<
            disk[1].ds.name << '] ' << disk[1].path << '" ' << "\n"
    end

    str_info
end

#monitor(stats) ⇒ Object

monitor function used when poll action is called for all vms



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
# File 'lib/vm_monitor.rb', line 40

def monitor(stats)
    reset_monitor

    refresh_rate = 20 # 20 seconds between samples (realtime)

    @state = state_to_c(@vm_info['summary.runtime.powerState'])

    return if @state != VM_STATE[:active]

    cpuMhz =  @vm_info[:esx_host_cpu]

    @monitor[:used_memory] = @vm_info['summary.quickStats.hostMemoryUsage']
                             .to_i * 1024

    used_cpu = @vm_info['summary.quickStats.overallCpuUsage'].to_f / cpuMhz
    used_cpu = (used_cpu * 100).to_s
    @monitor[:used_cpu] = format('%.2f', used_cpu).to_s

    # Check for negative values
    @monitor[:used_memory] = 0 if @monitor[:used_memory].to_i < 0
    @monitor[:used_cpu]    = 0 if @monitor[:used_cpu].to_i < 0

    guest_ip_addresses = []
    unless self['guest.net'].empty?
        @vm_info['guest.net'].each do |net|
            next unless net.ipConfig
            next if net.ipConfig.ipAddress.empty?

            net.ipConfig.ipAddress.each do |ip|
                guest_ip_addresses << ip.ipAddress
            end
        end
    end

    @guest_ip_addresses = guest_ip_addresses.join(',')

    if stats.key?(@item)
        metrics = stats[@item][:metrics]

        nettx_kbpersec = 0
        if metrics['net.transmitted']
            metrics['net.transmitted'].each do |sample|
                nettx_kbpersec += sample if sample > 0
            end
        end

        netrx_kbpersec = 0
        if metrics['net.bytesRx']
            metrics['net.bytesRx'].each do |sample|
                netrx_kbpersec += sample if sample > 0
            end
        end

        read_kbpersec = 0
        if metrics['virtualDisk.read']
            metrics['virtualDisk.read'].each do |sample|
                read_kbpersec += sample if sample > 0
            end
        end

        read_iops = 0
        if metrics['virtualDisk.numberReadAveraged']
            metrics['virtualDisk.numberReadAveraged'].each do |sample|
                read_iops += sample if sample > 0
            end
        end

        write_kbpersec = 0
        if metrics['virtualDisk.write']
            metrics['virtualDisk.write'].each do |sample|
                write_kbpersec += sample if sample > 0
            end
        end

        write_iops = 0
        if metrics['virtualDisk.numberWriteAveraged']
            metrics['virtualDisk.numberWriteAveraged'].each do |sample|
                write_iops += sample if sample > 0
            end
        end
    else
        nettx_kbpersec = 0
        netrx_kbpersec = 0
        read_kbpersec  = 0
        read_iops      = 0
        write_kbpersec = 0
        write_iops     = 0
    end

    # Accumulate values if present
    if @one_item && @one_item['MONITORING/NETTX']
        previous_nettx = @one_item['MONITORING/NETTX'].to_i
    else
        previous_nettx = 0
    end

    if @one_item && @one_item['MONITORING/NETRX']
        previous_netrx = @one_item['MONITORING/NETRX'].to_i
    else
        previous_netrx = 0
    end

    if @one_item && @one_item['MONITORING/DISKRDIOPS']
        previous_diskrdiops = @one_item['MONITORING/DISKRDIOPS'].to_i
    else
        previous_diskrdiops = 0
    end

    if @one_item && @one_item['MONITORING/DISKWRIOPS']
        previous_diskwriops = @one_item['MONITORING/DISKWRIOPS'].to_i
    else
        previous_diskwriops = 0
    end

    if @one_item && @one_item['MONITORING/DISKRDBYTES']
        previous_diskrdbytes = @one_item['MONITORING/DISKRDBYTES'].to_i
    else
        previous_diskrdbytes = 0
    end

    if @one_item && @one_item['MONITORING/DISKWRBYTES']
        previous_diskwrbytes = @one_item['MONITORING/DISKWRBYTES'].to_i
    else
        previous_diskwrbytes = 0
    end

    @monitor[:nettx] = previous_nettx +
                       (nettx_kbpersec * 1024 * refresh_rate).to_i
    @monitor[:netrx] = previous_netrx +
                       (netrx_kbpersec * 1024 * refresh_rate).to_i

    @monitor[:diskrdiops]  = previous_diskrdiops + read_iops
    @monitor[:diskwriops]  = previous_diskwriops + write_iops
    @monitor[:diskrdbytes] = previous_diskrdbytes +
                             (read_kbpersec * 1024 * refresh_rate).to_i
    @monitor[:diskwrbytes] = previous_diskwrbytes +
                             (write_kbpersec * 1024 * refresh_rate).to_i
end

#reset_monitorObject



292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/vm_monitor.rb', line 292

def reset_monitor
    @monitor = {
        :used_cpu    => 0,
        :used_memory => 0,
        :netrx       => 0,
        :nettx       => 0,
        :diskrdbytes => 0,
        :diskwrbytes => 0,
        :diskrdiops  => 0,
        :diskwriops  => 0
    }
end

#state_to_c(state) ⇒ Object

Converts the VI string state to OpenNebula state convention Guest states are:

  • poweredOff The virtual machine is currently powered off.

  • poweredOn The virtual machine is currently powered on.

  • suspended The virtual machine is currently suspended.



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/vm_monitor.rb', line 26

def state_to_c(state)
    case state
    when 'poweredOn'
        VM_STATE[:active]
    when 'suspended'
        VM_STATE[:paused]
    when 'poweredOff'
        VM_STATE[:deleted]
    else
        VM_STATE[:unknown]
    end
end