Statistics
| Branch: | Tag: | Revision:

one / src / cli / onehost @ 6329d8b3

History | View | Annotate | Download (10.1 KB)

1
#!/usr/bin/env ruby
2

    
3
# -------------------------------------------------------------------------- #
4
# Copyright 2002-2011, OpenNebula Project Leads (OpenNebula.org)             #
5
#                                                                            #
6
# Licensed under the Apache License, Version 2.0 (the "License"); you may    #
7
# not use this file except in compliance with the License. You may obtain    #
8
# a copy of the License at                                                   #
9
#                                                                            #
10
# http://www.apache.org/licenses/LICENSE-2.0                                 #
11
#                                                                            #
12
# Unless required by applicable law or agreed to in writing, software        #
13
# distributed under the License is distributed on an "AS IS" BASIS,          #
14
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #
15
# See the License for the specific language governing permissions and        #
16
# limitations under the License.                                             #
17
#--------------------------------------------------------------------------- #
18

    
19
ONE_LOCATION=ENV["ONE_LOCATION"]
20

    
21
if !ONE_LOCATION
22
    RUBY_LIB_LOCATION="/usr/lib/one/ruby"
23
else
24
    RUBY_LIB_LOCATION=ONE_LOCATION+"/lib/ruby"
25
end
26

    
27

    
28
$: << RUBY_LIB_LOCATION
29

    
30

    
31
require 'OpenNebula'
32
require 'client_utilities'
33
require 'command_parse'
34

    
35
ShowTableHost={
36
    :id => {
37
        :name => "ID",
38
        :desc => "ONE identifier for host",
39
        :size => 4,
40
        :proc => lambda {|d,e| d.id }
41
    },
42
    :name => {
43
        :name => "NAME",
44
        :desc => "Hostname",
45
        :size => 17,
46
        :left => true,
47
        :proc => lambda {|d,e| d.name }
48
    },
49
    :cid => {
50
        :name => "CID",
51
        :desc => "Cluster ID",
52
        :size => 4,
53
        :proc => lambda {|d,e| d.cluster_id }
54
    },
55
    :cluster => {
56
        :name => "CLUSTER",
57
        :desc => "Clustername",
58
        :size => 8,
59
        :left => true,
60
        :proc => lambda { "TODO" }
61
    },
62
    :rvm => {
63
        :name => "RVM",
64
        :desc => "Number of virtual machines running",
65
        :size => 3,
66
        :proc => lambda {|d,e| d["HOST_SHARE/RUNNING_VMS"] }
67
    },
68
    :tcpu => {
69
        :name => "TCPU",
70
        :desc => "Total cpu percentage",
71
        :size => 6,
72
        :proc => lambda {|d,e| d["HOST_SHARE/MAX_CPU"] }
73
    },
74
    :fcpu => {
75
        :name => "FCPU",
76
        :desc => "Free cpu percentage",
77
        :size => 6,
78
        :proc => lambda {|d,e|
79
            d["HOST_SHARE/MAX_CPU"].to_i-d["HOST_SHARE/USED_CPU"].to_i
80
        }
81
    },
82
    :acpu => {
83
        :name => "ACPU",
84
        :desc => "Available cpu percentage (not reserved by VMs)",
85
        :size => 6,
86
        :proc => lambda {|d,e|
87
            max_cpu=d["HOST_SHARE/MAX_CPU"].to_i
88
            max_cpu=100 if max_cpu==0
89
            max_cpu-d["HOST_SHARE/CPU_USAGE"].to_i
90
        }
91
    },
92
    :tmem => {
93
        :name => "TMEM",
94
        :desc => "Total memory",
95
        :size => 7,
96
        :kbytes => true,
97
        :proc => lambda {|d,e| d["HOST_SHARE/MAX_MEM"] }
98
    },
99
    :fmem => {
100
        :name => "FMEM",
101
        :desc => "Free memory",
102
        :size => 7,
103
        :kbytes => true,
104
        :proc => lambda {|d,e| d["HOST_SHARE/FREE_MEM"] }
105
    },
106
    :stat => {
107
        :name => "STAT",
108
        :desc => "Host status",
109
        :size => 4,
110
        :proc => lambda {|d,e|
111
            d.short_state_str()
112
        }
113
    },
114

    
115

    
116
    :default => [:id, :name, :cluster, :rvm, :tcpu, :fcpu, :acpu, :tmem, :fmem, :stat]
117
}
118

    
119
class HostShow
120
    def initialize(client)
121
        @hostpool=OpenNebula::HostPool.new(client)
122
        @table=ShowTable.new(ShowTableHost)
123
    end
124

    
125
    def header_host_small
126
        scr_bold
127
        scr_underline
128
        print @table.header_str
129
        scr_restore
130
        puts ""
131
    end
132

    
133
    def list_short(options=nil)
134
        res=@hostpool.info
135
        if options
136
            @table.columns=options[:columns] if options[:columns]
137
        end
138

    
139
        if OpenNebula.is_error?(res)
140
            result=res
141
        else
142
            result=[true,""]
143
            header_host_small
144
            if options
145
                puts @table.data_str(@hostpool, options)
146
            else
147
                puts @table.data_str(@hostpool)
148
            end
149
            result
150
        end
151
    end
152

    
153
    def top(options=nil)
154
        delay=1
155
        delay=options[:delay] if options && options[:delay]
156

    
157
        result=nil
158

    
159
        begin
160
            while true
161
                scr_cls
162
                scr_move(0,0)
163
                result=list_short(options)
164
                sleep delay
165
            end
166
        rescue Exception
167
        end
168
        result
169
    end
170
end
171

    
172
class OnehostParse < CommandParse
173

    
174
    COMMANDS_HELP=<<-EOT
175

    
176
Description:
177

    
178
This command enables the user to manage hosts in the Open Nebula server. It
179
provides functionality to allocate, get information and delete a particular
180
host or to list all the available hosts.
181

    
182

    
183
Commands:
184

    
185
* create (Adds a new machine to the pool)
186
    onehost create <hostname> <im_mad> <vmm_mad> <tm_mad>
187

    
188
* show (Gets info from a host)
189
    onehost show <host_id>
190

    
191
* delete (Removes a machine from the pool)
192
    onehost delete <host_id>
193

    
194
* list (Lists machines in the pool)
195
    onehost list
196

    
197
* enable (Enables host)
198
    onehost enable <host_id>
199

    
200
* disable (Disables host)
201
    onehost disable <host_id>
202

    
203
* top (Lists hosts continuously)
204
    onehost top
205

    
206
* sync (synchronizes probes with remote hosts)
207
    onehost sync
208
    
209

    
210
Information Columns:
211

    
212
* HID    Host ID
213
* NAME   Host name
214
* RVM    Number of running VMs
215
* TCPU   Total CPU (percentage)
216
* FCPU   Free CPU (percentage)
217
* ACPU   Available CPU (not allocated by VMs)
218
* TMEM   Total memory
219
* FMEM   Free memory
220
* STAT   Host status
221

    
222

    
223
EOT
224

    
225
    def text_commands
226
        COMMANDS_HELP
227
    end
228

    
229
    def text_command_name
230
        "onehost"
231
    end
232

    
233
    def list_options
234
        table=ShowTable.new(ShowTableHost)
235
        table.print_help
236
    end
237

    
238
end
239

    
240
# Returns true if there are non DONE VM's in a host, false otherwise
241
def vms_in_host?(host_id)
242

    
243
    host = OpenNebula::Host.new_with_id(host_id,get_one_client)
244

    
245
    rc = host.info
246

    
247
    if OpenNebula::is_error?(rc)
248
        puts rc.message
249
        exit -1
250
    end
251

    
252
    host['HOST_SHARE/RUNNING_VMS'].to_i
253

    
254
end
255

    
256

    
257
onehost_opts=OnehostParse.new
258
onehost_opts.parse(ARGV)
259
ops=onehost_opts.options
260

    
261
result=[false, "Unknown error"]
262

    
263
command=ARGV.shift
264

    
265
case command
266
when "add", "create"
267
    check_parameters("create", 4)
268
    host=OpenNebula::Host.new(OpenNebula::Host.build_xml, get_one_client)
269
    result=host.allocate(ARGV[0], ARGV[1], ARGV[2], ARGV[3])
270

    
271
    if is_successful?(result)
272
        puts "ID: " + host.id.to_s if ops[:verbose]
273
        exit 0
274
    end
275

    
276
when "show"
277
    check_parameters("show", 1)
278
    #args=expand_args(ARGV)
279

    
280
    host_id=get_host_id(ARGV[0])
281

    
282
    host=OpenNebula::Host.new_with_id(host_id, get_one_client)
283

    
284
    result=host.info
285
    if is_successful?(result)
286
        if !ops[:xml]
287
            str    = "%-22s: %-20s"
288
            str_h1 = "%-80s"
289

    
290
            print_header(str_h1, "HOST #{host_id} INFORMATION", true)
291

    
292
            puts str % ["ID", host.id.to_s]
293
            puts str % ["NAME", host.name]
294
            puts str % ["CLUSTER", host['CLUSTER']]
295
            puts str % ["STATE", host.state_str]
296
            puts str % ["IM_MAD", host['IM_MAD']]
297
            puts str % ["VM_MAD", host['VM_MAD']]
298
            puts str % ["TM_MAD", host['TM_MAD']]
299
            puts
300

    
301
            print_header(str_h1, "HOST SHARES", false)
302

    
303
            puts str % ["MAX MEM", host['HOST_SHARE/MAX_MEM']]
304
            puts str % ["USED MEM (REAL)", host['HOST_SHARE/USED_MEM']]
305
            puts str % ["USED MEM (ALLOCATED)", host['HOST_SHARE/MEM_USAGE']]
306
            puts str % ["MAX CPU", host['HOST_SHARE/MAX_CPU']]
307
            puts str % ["USED CPU (REAL)", host['HOST_SHARE/USED_CPU']]
308
            puts str % ["USED CPU (ALLOCATED)", host['HOST_SHARE/CPU_USAGE']]
309
            puts str % ["RUNNING VMS", host['HOST_SHARE/RUNNING_VMS']]
310
            puts
311

    
312
            print_header(str_h1, "MONITORING INFORMATION", false)
313

    
314
            puts host.template_str
315
        else
316
            puts host.to_xml(true)
317
        end
318
    end
319

    
320
when "delete"
321
    check_parameters("delete", 1)
322
    args=expand_args(ARGV)
323

    
324
    args.each do |param|
325
        host_id=get_host_id(param)
326

    
327
        host = OpenNebula::Host.new_with_id(host_id,get_one_client)
328

    
329
        rc = host.info
330

    
331
        if OpenNebula::is_error?(rc)
332
            puts rc.message
333
            exit -1
334
        end
335

    
336
        if host['HOST_SHARE/RUNNING_VMS'].to_i != 0
337
            puts "Host still has associated VMs, aborting delete."
338
        else
339
            result=host.delete
340
            if is_successful?(result)
341
                puts "Host deleted" if ops[:verbose]
342
                exit 0
343
            end
344
        end
345
    end
346

    
347
when "list"
348
    if !ops[:xml]
349
        hostlist=HostShow.new(get_one_client)
350
        ops[:columns]=ops[:list] if ops[:list]
351
        result=hostlist.list_short(ops)
352
    else
353
        hostpool=OpenNebula::HostPool.new(get_one_client)
354
        hostpool.info
355
        puts hostpool.to_xml(true)
356
    end
357

    
358
when "top"
359
    hostlist=HostShow.new(get_one_client)
360
    ops[:columns]=ops[:list] if ops[:list]
361
    result=hostlist.top(ops)
362

    
363
when "enable"
364
    check_parameters("enable", 1)
365
    args=expand_args(ARGV)
366

    
367
    args.each do |param|
368
        host_id=get_host_id(param)
369
        host = OpenNebula::Host.new_with_id(host_id,get_one_client)
370
        result=host.enable
371
        if is_successful?(result)
372
            puts "Host enabled" if ops[:verbose]
373
        else
374
            break
375
        end
376
    end
377

    
378
when "disable"
379
    check_parameters("disable", 1)
380
    args=expand_args(ARGV)
381

    
382
    args.each do |param|
383
        host_id=get_host_id(param)
384
        host = OpenNebula::Host.new_with_id(host_id,get_one_client)
385
        result=host.disable
386
        if is_successful?(result)
387
            puts "Host disabled" if ops[:verbose]
388
        else
389
            break
390
        end
391
    end
392

    
393
when "sync"
394
    check_parameters("sync", 0)
395
    if ONE_LOCATION
396
        FileUtils.touch "#{ONE_LOCATION}/var/remotes"
397
    else
398
        FileUtils.touch "/var/lib/one/remotes"
399
    end
400

    
401
else
402
    onehost_opts.print_help
403
    exit -1
404
end
405

    
406
if is_error?(result)
407
    puts "Error: " + result.message
408
    exit -1
409
end
410

    
411
exit 0
412