Statistics
| Branch: | Tag: | Revision:

one / src / cli / onevm @ 8dee8529

History | View | Annotate | Download (21.6 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
$: << RUBY_LIB_LOCATION
28

    
29

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

    
34

    
35
ShowTableVM={
36
    :id => {
37
        :name => "ID",
38
        :desc => "ONE identifier for the VM",
39
        :size => 5,
40
        :proc => lambda {|d,e| d.id }
41
    },
42
    :name => {
43
        :name => "NAME",
44
        :desc => "Name of the domain",
45
        :size => 8,
46
        :proc => lambda {|d,e|
47
            d.name
48
        }
49
    },
50
    :user=> {
51
        :name => "USER",
52
        :desc => "Name of the owner",
53
        :size => 8,
54
        :proc => lambda {|d,e|
55
            d["USERNAME"]
56
        }
57
    },
58
    :stat => {
59
        :name => "STAT",
60
        :desc => "Actual status of the VM",
61
        :size => 4,
62
        :proc => lambda {|d,e|
63
            d.status
64
        }
65
    },
66
    :cpu => {
67
        :name => "CPU",
68
        :desc => "CPU percentage used by the VM",
69
        :size => 3,
70
        :proc => lambda {|d,e|
71
            d["CPU"]
72
        }
73
    },
74
    :mem => {
75
        :name => "MEM",
76
        :desc => "Memory used by the VM",
77
        :size => 7,
78
        :kbytes => true,
79
        :proc => lambda {|d,e| d["MEMORY"] }
80
    },
81
    :hostname => {
82
        :name => "HOSTNAME",
83
        :desc => "Machine where the VM is running",
84
        :size => 15,
85
        :proc => lambda {|d,e|
86
            d["HISTORY/HOSTNAME"]
87
        }
88
    },
89
    :time => {
90
        :name => "TIME",
91
        :desc => "Time since the VM was submitted",
92
        :size => 11,
93
        :proc => lambda {|d,e| str_running_time(d) }
94
    },
95

    
96
    :default => [:id, :user, :name, :stat, :cpu, :mem, :hostname, :time]
97
}
98

    
99
ShowTableHistory={
100
    :id => {
101
        :name => "ID",
102
        :desc => "ONE identifier for the VM",
103
        :size => 4,
104
        :proc => lambda {|d,e| d["ID"] }
105
    },
106
    :seq => {
107
        :name => "SEQ",
108
        :desc => "Sequence number",
109
        :size => 3,
110
        :proc => lambda {|d,e| d["SEQ"] }
111
    },
112
    :hostname => {
113
        :name => "HOSTNAME",
114
        :desc => "Name of the host where the VM was submited",
115
        :size => 15,
116
        :proc => lambda {|d,e| d["HOST_NAME"] }
117
    },
118
    :stime => {
119
        :name => "STIME",
120
        :desc => "Start time",
121
        :size => 14,
122
        :proc => lambda {|d,e|
123
            t=Time.at(d["STIME"].to_i)
124
            t.strftime("%m/%d %H:%M:%S")
125
        }
126
    },
127
    :etime => {
128
        :name => "ETIME",
129
        :desc => "End time",
130
        :size => 14,
131
        :proc => lambda {|d,e|
132
            if d["ETIME"].to_i==0
133
                "--"
134
            else
135
                t=Time.at(d["ETIME"].to_i)
136
                t.strftime("%m/%d %H:%M:%S")
137
            end
138
        }
139
    },
140
    :time => {
141
        :name => "TIME",
142
        :desc => "Total time",
143
        :size => 11,
144
        :proc => lambda {|d,e|
145
            d["TIME"]
146
        }
147
    },
148
    :reason => {
149
        :name => "REASON",
150
        :desc => "Reason for state change",
151
        :size => "6",
152
        :proc => lambda {|d,e|
153
            OpenNebula::VirtualMachine.get_reason(d["REASON"])
154
        }
155
    },
156

    
157
    :default => [:id, :seq, :hostname, :stime, :etime, :time, :reason]
158
}
159

    
160
class VmShow
161

    
162
    def initialize(client, filter_flag="-2")
163
        @vmpool=OpenNebula::VirtualMachinePool.new(client, filter_flag.to_i)
164
        @table=ShowTable.new(ShowTableVM)
165
        @table_history=ShowTable.new(ShowTableHistory)
166
    end
167

    
168
    def header_vm_small
169
        scr_bold
170
        scr_underline
171
        print @table.header_str
172
        scr_restore
173
        puts ""
174
    end
175

    
176
    def header_history_small
177
        scr_bold
178
        scr_underline
179
        print @table_history.header_str
180
        scr_restore
181
        puts ""
182
    end
183

    
184
    def list_short(options=nil)
185
        res=@vmpool.info()
186

    
187
        if options
188
            @table.columns=options[:columns] if options[:columns]
189
        end
190

    
191
        if OpenNebula.is_error?(res)
192
            result=res
193
            puts res.message
194
            exit -1
195
        else
196

    
197
            if options[:filter_flag]
198
                vms=@vmpool.select{|element|
199
                    element['USERNAME']==options[:filter_flag]  }
200
            else
201
                vms=@vmpool
202
            end
203

    
204
            result=[true, ""]
205
            header_vm_small
206

    
207
            if options
208
                puts @table.data_str(vms, options)
209
            else
210
                puts @table.data_str(vms)
211
            end
212

    
213
            result
214
        end
215
    end
216

    
217
    def top(options=nil)
218
        delay=1
219
        delay=options[:delay] if options && options[:delay]
220

    
221
        result=nil
222

    
223
        begin
224
            while true
225
                scr_cls
226
                scr_move(0,0)
227
                result=list_short(options)
228
                sleep delay
229
            end
230
        rescue Exception
231
        end
232
        result
233
    end
234

    
235
    def get_vm_history(vm)
236
        {
237
            'id'        => vm.id,
238
            'seq'       => vm['HISTORY/SEQ'],
239
            'host_name' => vm['HISTORY/HOSTNAME'],
240
            'stime'     => vm['HISTORY/STIME'],
241
            'etime'     => vm['HISTORY/ETIME'],
242
            'time'      => str_running_time(vm),
243
            'reason'    => vm['HISTORY/REASON']
244
        }
245
    end
246

    
247
    def get_vms_history(vms)
248
        vms.collect do |vmid|
249
            vm=OpenNebula::VirtualMachine.new_with_id(vmid, get_one_client)
250
            result=vm.info
251

    
252
            if is_error?(result)
253
                puts "Error: "+result.message
254
                exit -1
255
            end
256

    
257
            get_vm_history(vm)
258
        end
259
    end
260

    
261
    def list_vm_history(vm, options=nil)
262
        #res=@vm.get_history(id)
263
        if options
264
            @table_history.columns=options[:columns] if options[:columns]
265
        end
266

    
267
        header_history_small
268

    
269
        if options
270
            puts @table_history.data_str([vm], options)
271
        else
272
            puts @table_history.data_str([vm])
273
        end
274
    end
275

    
276
    def list_vm_history_array(ids, options=nil)
277
        get_vms_history(ids).each {|vm|
278
            puts "History for VM #{vm['ID']}"
279
            puts
280
            list_vm_history(vm, options)
281
            puts
282
        }
283
    end
284

    
285
    def list_vm_history_all(options=nil)
286
        result=@vmpool.info
287

    
288
        if is_error?(result)
289
            puts "Error: "+result.message
290
            exit -1
291
        end
292

    
293
        @vmpool.each {|vm|
294
            puts "History for VM #{vm.id}"
295
            puts
296
            list_vm_history(get_vm_history(vm), options)
297
            puts
298
        }
299
    end
300
end
301

    
302

    
303
##########################
304
## COMMAND LINE PARSING ##
305
##########################
306

    
307
class OnevmParse < CommandParse
308

    
309
    COMMANDS_HELP=<<-EOT
310

    
311
Description:
312

    
313
This command enables the user to manage virtual machines in OpenNebula.
314
The user can allocate, deploy, migrate, suspend, resume and shutdown a virtual
315
machine with the functionality present in onevm.
316

    
317

    
318
Commands:
319

    
320
* create (Submits a new virtual machine, adding it to the ONE VM pool)
321
    onevm create [OPTION] {<template-file-path>, <template-id>, <template-name>}
322

    
323
    <template-file-path> is a file name where the VM description is located.
324
    <template-id> is the numeric ID of a registered template (using onetemplate)
325
    <template-name> is the name of a registered template (using onetemplate)
326

    
327
    OPTION: -n STRING, --name=STRING
328
        Replaces the NAME attribute if the VM is being created from a registered
329
        Template
330

    
331
* deploy (Starts an existing VM in an specific host)
332
    onevm deploy <vm_id> <host_id>
333

    
334
    States: PENDING
335

    
336
* shutdown (Shuts down an already deployed VM)
337
    onevm shutdown <vm_id>
338

    
339
    States: RUNNING
340

    
341
* livemigrate (Migrates a running VM to another host without downtime)
342
    onevm livemigrate <vm_id> <host_id>
343

    
344
    States: RUNNING
345

    
346
* migrate (Saves a running VM and starts it again in the specified host)
347
    onevm migrate <vm_id> <host_id>
348

    
349
    States: RUNNING
350

    
351
* hold (Sets a VM to hold state, scheduler will not deploy it)
352
    onevm hold <vm_id>
353

    
354
    States: PENDING
355

    
356
* release (Releases a VM from hold state, setting it to pending)
357
    onevm release <vm_id>
358

    
359
    States: HOLD
360

    
361
* stop (Stops a running VM)
362
    onevm stop <vm_id>
363

    
364
    States: RUNNING
365

    
366
* cancel (Cancels a running VM)
367
    onevm cancel <vm_id>
368

    
369
    States: RUNNING
370

    
371
* suspend (Saves a running VM)
372
    onevm suspend <vm_id>
373

    
374
    States: RUNNING
375

    
376
* resume (Resumes the execution of a saved VM)
377
    onevm resume <vm_id>
378

    
379
    States: STOPPED, SUSPENDED
380

    
381
* saveas (Set the specified vms disk to be saved in a new image (image_name) 
382
            when the vm shuts down)
383
    onevm saveas <vm_id> <disk_id> <image_name>
384
    
385
         (Set a different type for the new Image)
386
    onevm saveas <vm_id> <disk_id> <image_name> -t/--type <type>
387
        
388
* delete (Deletes a VM from the pool)
389
    onevm delete <vm_id>
390

    
391
    States: ANY
392

    
393
* restart (Forces a re-deployment of a VM in UNKNOWN or BOOT state)
394
    onevm restart <vm_id>
395

    
396
    States: UNKNOWN, BOOT
397

    
398
* resubmit (Resubmits a VM to PENDING state)
399
    onevm resubmit <vm_id>
400

    
401
    States: ANY, except SUSPENDED or DONE
402

    
403
* list (Shows VMs in the pool)
404
    onevm list <filter_flag>
405
        where filter_flag can be
406
            a, all   --> all the known VMs
407
            m, mine  --> the VMs belonging to the user in ONE_AUTH
408
            uid      --> VMs of the user identified by this uid
409
            user     --> VMs of the user identified by the username
410

    
411
* show (Gets information about a specific VM)
412
    onevm show <vm_id>
413

    
414
* top (Lists VMs continuously)
415
    onevm top
416

    
417
* history (Gets history from VMs)
418
    onevm history [<vm_id> <vm_id> ...]
419

    
420
    if no vm_id is provided it will list history for all known VMs
421

    
422

    
423
Information Columns:
424

    
425
* ID        ONE VM identifier
426
* USER      Username of the VM owner
427
* NAME      Name of the VM
428
* STAT      Status of the VM
429
* CPU       CPU percentage used by the VM
430
* MEM       Memory used by the VM
431
* HOSTNAME  Host where the VM is being or was run
432
* TIME      Time since the submission of the VM (days hours:minutes:seconds)
433

    
434

    
435
VM States:
436

    
437
* pend	 pending
438
* hold	 VM on hold (not runnable)
439
* stop	 stopped
440
* susp	 suspended
441
* done	 finished
442
* prol	 prolog
443
* boot	 booting
444
* runn	 running
445
* migr	 migrating
446
* save	 saving the VM to disk
447
* epil	 epilog
448
* shut	 shutting down
449
* fail	 failed
450

    
451
EOT
452

    
453
    def text_commands
454
        COMMANDS_HELP
455
    end
456

    
457
    def text_command_name
458
        "onevm"
459
    end
460

    
461
    def list_options
462
        table=ShowTable.new(ShowTableVM)
463
        table.print_help
464
    end
465

    
466
    def special_options(opts, options)
467
        opts.on("-t type", "--type type", String,
468
                "Image type") do |o|
469
            options[:type]=o
470
        end
471
        opts.on_tail("-n vm_name", "--name vm_name", String,
472
                "Set VM name") do |o|
473
            options[:vm_name] = o
474
        end
475
    end
476
end
477

    
478

    
479
def get_user_flags
480
    ops=Hash.new
481
    if ARGV[0]
482
        case ARGV[0]
483
            when "a", "all"
484
                ops[:filter_user]="-2"
485
            when "m", "mine"
486
                ops[:filter_user]="-1"
487
            else
488
                if !ARGV[0].match(/^[0123456789]+$/)
489
                    ops[:filter_user]="-2"
490
                    ops[:filter_flag]=ARGV[0]
491
                else
492
                    ops[:filter_user]=ARGV[0]
493
                end
494
        end
495
     else
496
        ops[:filter_user]="-2"
497
     end
498

    
499
     ops
500
end
501

    
502
onevm_opts=OnevmParse.new
503
onevm_opts.parse(ARGV)
504
ops=onevm_opts.options
505

    
506
result=[false, "Unknown error"]
507

    
508
command=ARGV.shift
509

    
510
case command
511
when "submit", "create"
512
    check_parameters("create", 1)
513
    vm=OpenNebula::VirtualMachine.new(
514
        OpenNebula::VirtualMachine.build_xml, get_one_client)
515

    
516
    template = ""
517
    success = false
518

    
519
    if( File.file?(ARGV[0]) )
520
        # argument is a file path
521
        begin
522
            template = File.read(ARGV[0])
523
            success = true
524
        rescue
525
            result = OpenNebula::Error.new("Can not read template: #{ARGV[0]}")
526
        end
527
    else
528
        # argument could be a template ID or a template name
529
        template_id = get_template_id(ARGV[0])
530

    
531
        template = "TEMPLATE_ID = #{template_id}"
532
        template << "\nNAME = #{ops[:vm_name]}" if ops[:vm_name]
533

    
534
        success = true
535
    end
536

    
537
    if( success )
538
        result = vm.allocate(template)
539
    end
540

    
541
    if is_successful?(result)
542
        puts "ID: " + vm.id.to_s if ops[:verbose]
543
        exit 0
544
    end
545

    
546
when "deploy"
547
    check_parameters("deploy", 2)
548
    host_id=get_host_id(ARGV[-1])
549
    args=expand_args(ARGV[0..-2])
550

    
551
    args.each do |param|
552
        vm_id=get_vm_id(param)
553

    
554
        vm=OpenNebula::VirtualMachine.new_with_id(vm_id, get_one_client)
555

    
556
        result=vm.deploy(host_id)
557
        if is_successful?(result)
558
            puts "Deploying VM" if ops[:verbose]
559
        else
560
            break
561
        end
562
    end
563

    
564
when "shutdown"
565
    check_parameters("shutdown", 1)
566
    args=expand_args(ARGV)
567

    
568
    args.each do |param|
569
        vm_id=get_vm_id(param)
570

    
571
        vm=OpenNebula::VirtualMachine.new_with_id(vm_id, get_one_client)
572
        result=vm.shutdown
573
        if is_successful?(result)
574
            puts "Shutting down VM" if ops[:verbose]
575
        else
576
            break
577
        end
578
    end
579

    
580
when "livemigrate"
581
    check_parameters("livemigrate", 2)
582
    host_id=get_host_id(ARGV[-1])
583
    args=expand_args(ARGV[0..-2])
584

    
585
    args.each do |param|
586
        vm_id=get_vm_id(param)
587

    
588
        vm=OpenNebula::VirtualMachine.new_with_id(vm_id, get_one_client)
589

    
590
        result=vm.live_migrate(host_id)
591
        if is_successful?(result)
592
            puts "Migrating VM" if ops[:verbose]
593
        else
594
            break
595
        end
596
    end
597

    
598
when "migrate"
599
    check_parameters("migrate", 2)
600
    host_id=get_host_id(ARGV[-1])
601
    args=expand_args(ARGV[0..-2])
602

    
603
    args.each do |param|
604
        vm_id=get_vm_id(param)
605

    
606
        vm=OpenNebula::VirtualMachine.new_with_id(vm_id, get_one_client)
607

    
608
        result=vm.migrate(host_id)
609
        if is_successful?(result)
610
            puts "Migrating VM" if ops[:verbose]
611
        else
612
            break
613
        end
614
    end
615

    
616
when "hold"
617
    check_parameters("hold", 1)
618
    args=expand_args(ARGV)
619

    
620
    args.each do |param|
621
        vm_id=get_vm_id(param)
622

    
623
        vm=OpenNebula::VirtualMachine.new_with_id(vm_id, get_one_client)
624

    
625
        result=vm.hold
626
        if is_successful?(result)
627
            puts "Setting VM to hold state" if ops[:verbose]
628
        else
629
            break
630
        end
631
    end
632

    
633
when "release"
634
    check_parameters("release", 1)
635
    args=expand_args(ARGV)
636

    
637
    args.each do |param|
638
        vm_id=get_vm_id(param)
639

    
640
        vm=OpenNebula::VirtualMachine.new_with_id(vm_id, get_one_client)
641

    
642
        result=vm.release
643
        if is_successful?(result)
644
            puts "Releasing VM" if ops[:verbose]
645
        else
646
            break
647
        end
648
    end
649

    
650
when "stop"
651
    check_parameters("stop", 1)
652
    args=expand_args(ARGV)
653

    
654
    args.each do |param|
655
        vm_id=get_vm_id(param)
656

    
657
        vm=OpenNebula::VirtualMachine.new_with_id(vm_id, get_one_client)
658

    
659
        result=vm.stop
660
        if is_successful?(result)
661
            puts "Stopping VM" if ops[:verbose]
662
        else
663
            break
664
        end
665
    end
666

    
667
when "cancel"
668
    check_parameters("cancel", 1)
669
    args=expand_args(ARGV)
670

    
671
    args.each do |param|
672
        vm_id=get_vm_id(param)
673

    
674
        vm=OpenNebula::VirtualMachine.new_with_id(vm_id, get_one_client)
675

    
676
        result=vm.cancel
677
        if is_successful?(result)
678
            puts "Cancelling VM" if ops[:verbose]
679
        else
680
            break
681
        end
682
    end
683

    
684
when "suspend"
685
    check_parameters("suspend", 1)
686
    args=expand_args(ARGV)
687

    
688
    args.each do |param|
689
        vm_id=get_vm_id(param)
690

    
691
        vm=OpenNebula::VirtualMachine.new_with_id(vm_id, get_one_client)
692

    
693
        result=vm.suspend
694
        if is_successful?(result)
695
            puts "Suspending VM" if ops[:verbose]
696
        else
697
            break
698
        end
699
    end
700

    
701
when "resume"
702
    check_parameters("resume", 1)
703
    args=expand_args(ARGV)
704

    
705
    args.each do |param|
706
        vm_id=get_vm_id(param)
707

    
708
        vm=OpenNebula::VirtualMachine.new_with_id(vm_id, get_one_client)
709

    
710
        result=vm.resume
711
        if is_successful?(result)
712
            puts "Resuming VM" if ops[:verbose]
713
        else
714
            break
715
        end
716
    end
717

    
718
when "restart"
719
    check_parameters("restart", 1)
720
    args=expand_args(ARGV)
721

    
722
    args.each do |param|
723
        vm_id=get_vm_id(param)
724

    
725
        vm=OpenNebula::VirtualMachine.new_with_id(vm_id, get_one_client)
726

    
727
        result=vm.restart
728
        if is_successful?(result)
729
            puts "Restarting VM" if ops[:verbose]
730
        else
731
            break
732
        end
733
    end
734

    
735
when "resubmit"
736
    check_parameters("resubmit", 1)
737
    args=expand_args(ARGV)
738

    
739
    args.each do |param|
740
        vm_id=get_vm_id(param)
741

    
742
        vm=OpenNebula::VirtualMachine.new_with_id(vm_id, get_one_client)
743

    
744
        result=vm.resubmit
745
        if is_successful?(result)
746
            puts "Resubmitting VM" if ops[:verbose]
747
        else
748
            break
749
        end
750
    end
751

    
752
when "list"
753
    ops.merge!(get_user_flags)
754
    if !ops[:xml]
755
        vmlist=VmShow.new(get_one_client, ops[:filter_user].to_i)
756

    
757
        ops[:columns]=ops[:list] if ops[:list]
758
        result=vmlist.list_short(ops)
759
    else
760
        vmpool=OpenNebula::VirtualMachinePool.new(get_one_client,
761
            ops[:filter_user].to_i)
762
        vmpool.info
763
        puts vmpool.to_xml(true)
764
    end
765

    
766
when "top"
767
    ops.merge!(get_user_flags)
768
    vmlist=VmShow.new(get_one_client, ops[:filter_user].to_i)
769
    ops[:columns]=ops[:list] if ops[:list]
770
    result=vmlist.top(ops)
771

    
772

    
773
when "history"
774
    vmlist=VmShow.new(get_one_client)
775
    ops[:columns]=ops[:list] if ops[:list]
776
    if ARGV[0]
777
        ids=ARGV.collect {|arg| get_vm_id(arg)}
778
        ids=ids.flatten.compact
779
        result=vmlist.list_vm_history_array(ids)
780
    else
781
        result=vmlist.list_vm_history_all(ops)
782
    end
783

    
784
when "delete"
785
    check_parameters("delete", 1)
786
    args=expand_args(ARGV)
787

    
788
    args.each do |param|
789
        vm_id=get_vm_id(param)
790

    
791
        vm=OpenNebula::VirtualMachine.new_with_id(vm_id, get_one_client)
792

    
793
        result=vm.finalize
794
        if is_successful?(result)
795
            puts "VM correctly deleted"  if ops[:verbose]
796
        else
797
            break
798
        end
799
    end
800
    
801
when "saveas"
802
    check_parameters("saveas", 3)
803
    vm_id = get_vm_id(ARGV[0])
804
    disk_id = ARGV[1]
805
    image_name = ARGV[2]
806

    
807
    # Get the Image ID for this disk
808
    vm = OpenNebula::VirtualMachine.new(
809
                OpenNebula::VirtualMachine.build_xml(vm_id),
810
                get_one_client)
811
                
812
    result = vm.info
813
    if !is_successful?(result)
814
        puts result.message
815
        exit -1
816
    end
817

    
818
    result = vm.save_as(disk_id.to_i, image_name)
819
    if is_successful?(result)
820
        puts "VM disk with ID #{disk_id} is prepared to be" << 
821
             " saved"  if ops[:verbose]
822
    end
823

    
824
when "show"
825
    check_parameters("get_info", 1)
826
    args=expand_args(ARGV)
827

    
828
    args.each do |param|
829

    
830
        vm_id=get_vm_id(param)
831

    
832
        vm=OpenNebula::VirtualMachine.new_with_id(vm_id, get_one_client)
833
        result=vm.info
834
        if is_successful?(result)
835
            if !ops[:xml]
836
                str="%-15s: %-20s"
837
                str_h1="%-80s"
838

    
839
                print_header(str_h1, "VIRTUAL MACHINE #{vm['ID']} INFORMATION",
840
true)
841

    
842
                puts str % ["ID", vm.id.to_s]
843
                puts str % ["NAME", vm.name]
844
                puts str % ["STATE", vm.state_str]
845
                puts str % ["LCM_STATE", vm.lcm_state_str]
846

    
847
                value=vm['STIME'].to_i
848
                if value==0
849
                    value='-'
850
                else
851
                    value=Time.at(value).strftime("%m/%d %H:%M:%S")
852
                end
853
                puts str % ["START TIME", value]
854

    
855
                value=vm['ETIME'].to_i
856
                if value==0
857
                    value='-'
858
                else
859
                    value=Time.at(value).strftime("%m/%d %H:%M:%S")
860
                end
861
                puts str % ["END TIME", value]
862

    
863
                value=vm['DEPLOY_ID']
864
                puts str % ["DEPLOY ID:", value=="" ? "-" : value]
865

    
866
                puts
867

    
868
                print_header(str_h1,"VIRTUAL MACHINE MONITORING",false)
869

    
870
                poll_attrs = {
871
                    "USED MEMORY" => "MEMORY",
872
                    "USED CPU" => "CPU",
873
                    "NET_TX" => "NET_TX",
874
                    "NET_RX" => "NET_RX"
875
                }
876

    
877
                poll_attrs.each do |k,v|
878
                    puts str % [k,vm[v]]
879
                end
880
                
881
                puts
882
                
883
                print_header(str_h1,"VIRTUAL MACHINE TEMPLATE",false)
884

    
885
                puts vm.template_str
886
            else
887
                puts vm.to_xml(true)
888
            end
889
        end
890
     end
891
else
892
    onevm_opts.print_help
893
    exit -1
894
end
895

    
896

    
897

    
898
if OpenNebula.is_error?(result)
899
    puts "Error: " + result.message
900
    exit -1
901
end
902

    
903