Statistics
| Branch: | Tag: | Revision:

one / src / cli / onevm @ 6329d8b3

History | View | Annotate | Download (22 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
    :uid=> {
43
        :name => "UID",
44
        :desc => "Id of the owner",
45
        :size => 4,
46
        :proc => lambda {|d,e|
47
            d["UID"]
48
        }
49
    },
50
    :gid=> {
51
        :name => "GID",
52
        :desc => "Id of the group",
53
        :size => 4,
54
        :proc => lambda {|d,e|
55
            d.gid
56
        }
57
    },
58
    :name => {
59
        :name => "NAME",
60
        :desc => "Name of the domain",
61
        :size => 8,
62
        :proc => lambda {|d,e|
63
            d.name
64
        }
65
    },
66
    :user=> {
67
        :name => "USER",
68
        :desc => "Name of the owner",
69
        :size => 8,
70
        :proc => lambda {|d,e|
71
            "TODO"
72
        }
73
    },
74
    :stat => {
75
        :name => "STAT",
76
        :desc => "Actual status of the VM",
77
        :size => 4,
78
        :proc => lambda {|d,e|
79
            d.status
80
        }
81
    },
82
    :cpu => {
83
        :name => "CPU",
84
        :desc => "CPU percentage used by the VM",
85
        :size => 3,
86
        :proc => lambda {|d,e|
87
            d["CPU"]
88
        }
89
    },
90
    :mem => {
91
        :name => "MEM",
92
        :desc => "Memory used by the VM",
93
        :size => 7,
94
        :kbytes => true,
95
        :proc => lambda {|d,e| d["MEMORY"] }
96
    },
97
    :hostname => {
98
        :name => "HOSTNAME",
99
        :desc => "Machine where the VM is running",
100
        :size => 15,
101
        :proc => lambda {|d,e|
102
            d["HISTORY/HOSTNAME"]
103
        }
104
    },
105
    :time => {
106
        :name => "TIME",
107
        :desc => "Time since the VM was submitted",
108
        :size => 11,
109
        :proc => lambda {|d,e| str_running_time(d) }
110
    },
111

    
112
    :default => [:id, :user, :name, :stat, :cpu, :mem, :hostname, :time]
113
}
114

    
115
ShowTableHistory={
116
    :id => {
117
        :name => "ID",
118
        :desc => "ONE identifier for the VM",
119
        :size => 4,
120
        :proc => lambda {|d,e| d["ID"] }
121
    },
122
    :seq => {
123
        :name => "SEQ",
124
        :desc => "Sequence number",
125
        :size => 3,
126
        :proc => lambda {|d,e| d["SEQ"] }
127
    },
128
    :hostname => {
129
        :name => "HOSTNAME",
130
        :desc => "Name of the host where the VM was submited",
131
        :size => 15,
132
        :proc => lambda {|d,e| d["HOST_NAME"] }
133
    },
134
    :stime => {
135
        :name => "STIME",
136
        :desc => "Start time",
137
        :size => 14,
138
        :proc => lambda {|d,e|
139
            t=Time.at(d["STIME"].to_i)
140
            t.strftime("%m/%d %H:%M:%S")
141
        }
142
    },
143
    :etime => {
144
        :name => "ETIME",
145
        :desc => "End time",
146
        :size => 14,
147
        :proc => lambda {|d,e|
148
            if d["ETIME"].to_i==0
149
                "--"
150
            else
151
                t=Time.at(d["ETIME"].to_i)
152
                t.strftime("%m/%d %H:%M:%S")
153
            end
154
        }
155
    },
156
    :time => {
157
        :name => "TIME",
158
        :desc => "Total time",
159
        :size => 11,
160
        :proc => lambda {|d,e|
161
            d["TIME"]
162
        }
163
    },
164
    :reason => {
165
        :name => "REASON",
166
        :desc => "Reason for state change",
167
        :size => "6",
168
        :proc => lambda {|d,e|
169
            OpenNebula::VirtualMachine.get_reason(d["REASON"])
170
        }
171
    },
172

    
173
    :default => [:id, :seq, :hostname, :stime, :etime, :time, :reason]
174
}
175

    
176
class VmShow
177

    
178
    def initialize(client, filter_flag="-2")
179
        @vmpool=OpenNebula::VirtualMachinePool.new(client, filter_flag.to_i)
180
        @table=ShowTable.new(ShowTableVM)
181
        @table_history=ShowTable.new(ShowTableHistory)
182
    end
183

    
184
    def header_vm_small
185
        scr_bold
186
        scr_underline
187
        print @table.header_str
188
        scr_restore
189
        puts ""
190
    end
191

    
192
    def header_history_small
193
        scr_bold
194
        scr_underline
195
        print @table_history.header_str
196
        scr_restore
197
        puts ""
198
    end
199

    
200
    def list_short(options=nil)
201
        res=@vmpool.info()
202

    
203
        if options
204
            @table.columns=options[:columns] if options[:columns]
205
        end
206

    
207
        if OpenNebula.is_error?(res)
208
            result=res
209
            puts res.message
210
            exit -1
211
        else
212

    
213
            if options[:filter_flag]
214
                vms=@vmpool.select{|element|
215
                    element['USERNAME']==options[:filter_flag]  }
216
            else
217
                vms=@vmpool
218
            end
219

    
220
            result=[true, ""]
221
            header_vm_small
222

    
223
            if options
224
                puts @table.data_str(vms, options)
225
            else
226
                puts @table.data_str(vms)
227
            end
228

    
229
            result
230
        end
231
    end
232

    
233
    def top(options=nil)
234
        delay=1
235
        delay=options[:delay] if options && options[:delay]
236

    
237
        result=nil
238

    
239
        begin
240
            while true
241
                scr_cls
242
                scr_move(0,0)
243
                result=list_short(options)
244
                sleep delay
245
            end
246
        rescue Exception
247
        end
248
        result
249
    end
250

    
251
    def get_vm_history(vm)
252
        {
253
            'id'        => vm.id,
254
            'seq'       => vm['HISTORY/SEQ'],
255
            'host_name' => vm['HISTORY/HOSTNAME'],
256
            'stime'     => vm['HISTORY/STIME'],
257
            'etime'     => vm['HISTORY/ETIME'],
258
            'time'      => str_running_time(vm),
259
            'reason'    => vm['HISTORY/REASON']
260
        }
261
    end
262

    
263
    def get_vms_history(vms)
264
        vms.collect do |vmid|
265
            vm=OpenNebula::VirtualMachine.new_with_id(vmid, get_one_client)
266
            result=vm.info
267

    
268
            if is_error?(result)
269
                puts "Error: "+result.message
270
                exit -1
271
            end
272

    
273
            get_vm_history(vm)
274
        end
275
    end
276

    
277
    def list_vm_history(vm, options=nil)
278
        #res=@vm.get_history(id)
279
        if options
280
            @table_history.columns=options[:columns] if options[:columns]
281
        end
282

    
283
        header_history_small
284

    
285
        if options
286
            puts @table_history.data_str([vm], options)
287
        else
288
            puts @table_history.data_str([vm])
289
        end
290
    end
291

    
292
    def list_vm_history_array(ids, options=nil)
293
        get_vms_history(ids).each {|vm|
294
            puts "History for VM #{vm['ID']}"
295
            puts
296
            list_vm_history(vm, options)
297
            puts
298
        }
299
    end
300

    
301
    def list_vm_history_all(options=nil)
302
        result=@vmpool.info
303

    
304
        if is_error?(result)
305
            puts "Error: "+result.message
306
            exit -1
307
        end
308

    
309
        @vmpool.each {|vm|
310
            puts "History for VM #{vm.id}"
311
            puts
312
            list_vm_history(get_vm_history(vm), options)
313
            puts
314
        }
315
    end
316
end
317

    
318

    
319
##########################
320
## COMMAND LINE PARSING ##
321
##########################
322

    
323
class OnevmParse < CommandParse
324

    
325
    COMMANDS_HELP=<<-EOT
326

    
327
Description:
328

    
329
This command enables the user to manage virtual machines in OpenNebula.
330
The user can allocate, deploy, migrate, suspend, resume and shutdown a virtual
331
machine with the functionality present in onevm.
332

    
333

    
334
Commands:
335

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

    
339
    <template-file-path> is a file name where the VM description is located.
340
    <template-id> is the numeric ID of a registered template (using onetemplate)
341
    <template-name> is the name of a registered template (using onetemplate)
342

    
343
    OPTION: -n STRING, --name=STRING
344
        Replaces the NAME attribute if the VM is being created from a registered
345
        Template
346

    
347
* deploy (Starts an existing VM in an specific host)
348
    onevm deploy <vm_id> <host_id>
349

    
350
    States: PENDING
351

    
352
* shutdown (Shuts down an already deployed VM)
353
    onevm shutdown <vm_id>
354

    
355
    States: RUNNING
356

    
357
* livemigrate (Migrates a running VM to another host without downtime)
358
    onevm livemigrate <vm_id> <host_id>
359

    
360
    States: RUNNING
361

    
362
* migrate (Saves a running VM and starts it again in the specified host)
363
    onevm migrate <vm_id> <host_id>
364

    
365
    States: RUNNING
366

    
367
* hold (Sets a VM to hold state, scheduler will not deploy it)
368
    onevm hold <vm_id>
369

    
370
    States: PENDING
371

    
372
* release (Releases a VM from hold state, setting it to pending)
373
    onevm release <vm_id>
374

    
375
    States: HOLD
376

    
377
* stop (Stops a running VM)
378
    onevm stop <vm_id>
379

    
380
    States: RUNNING
381

    
382
* cancel (Cancels a running VM)
383
    onevm cancel <vm_id>
384

    
385
    States: RUNNING
386

    
387
* suspend (Saves a running VM)
388
    onevm suspend <vm_id>
389

    
390
    States: RUNNING
391

    
392
* resume (Resumes the execution of a saved VM)
393
    onevm resume <vm_id>
394

    
395
    States: STOPPED, SUSPENDED
396

    
397
* saveas (Set the specified vms disk to be saved in a new image (image_name) 
398
            when the vm shuts down)
399
    onevm saveas <vm_id> <disk_id> <image_name>
400
    
401
         (Set a different type for the new Image)
402
    onevm saveas <vm_id> <disk_id> <image_name> -t/--type <type>
403
        
404
* delete (Deletes a VM from the pool)
405
    onevm delete <vm_id>
406

    
407
    States: ANY
408

    
409
* restart (Forces a re-deployment of a VM in UNKNOWN or BOOT state)
410
    onevm restart <vm_id>
411

    
412
    States: UNKNOWN, BOOT
413

    
414
* resubmit (Resubmits a VM to PENDING state)
415
    onevm resubmit <vm_id>
416

    
417
    States: ANY, except SUSPENDED or DONE
418

    
419
* list (Shows VMs in the pool)
420
    onevm list <filter_flag>
421

    
422
    where filter_flag can be
423
        a, all   --> all the known VMs
424
        m, mine  --> the VMs belonging to the user in ONE_AUTH
425
        g, group --> 'mine' plus the VMs belonging to the groups
426
                        the user is member of
427
        uid      --> VMs of the user identified by this uid
428
        user     --> VMs of the user identified by the username
429

    
430
* show (Gets information about a specific VM)
431
    onevm show <vm_id>
432

    
433
* top (Lists VMs continuously)
434
    onevm top
435

    
436
* history (Gets history from VMs)
437
    onevm history [<vm_id> <vm_id> ...]
438

    
439
    if no vm_id is provided it will list history for all known VMs
440

    
441

    
442
Information Columns:
443

    
444
* ID        ONE VM identifier
445
* USER      Username of the VM owner
446
* NAME      Name of the VM
447
* STAT      Status of the VM
448
* CPU       CPU percentage used by the VM
449
* MEM       Memory used by the VM
450
* HOSTNAME  Host where the VM is being or was run
451
* TIME      Time since the submission of the VM (days hours:minutes:seconds)
452

    
453

    
454
VM States:
455

    
456
* pend	 pending
457
* hold	 VM on hold (not runnable)
458
* stop	 stopped
459
* susp	 suspended
460
* done	 finished
461
* prol	 prolog
462
* boot	 booting
463
* runn	 running
464
* migr	 migrating
465
* save	 saving the VM to disk
466
* epil	 epilog
467
* shut	 shutting down
468
* fail	 failed
469

    
470
EOT
471

    
472
    def text_commands
473
        COMMANDS_HELP
474
    end
475

    
476
    def text_command_name
477
        "onevm"
478
    end
479

    
480
    def list_options
481
        table=ShowTable.new(ShowTableVM)
482
        table.print_help
483
    end
484

    
485
    def special_options(opts, options)
486
        opts.on("-t type", "--type type", String,
487
                "Image type") do |o|
488
            options[:type]=o
489
        end
490
        opts.on_tail("-n vm_name", "--name vm_name", String,
491
                "Set VM name") do |o|
492
            options[:vm_name] = o
493
        end
494
    end
495
end
496

    
497

    
498
def get_user_flags
499
    ops=Hash.new
500
    if ARGV[0]
501
        case ARGV[0]
502
            when "a", "all"
503
                ops[:filter_user]="-2"
504
            when "m", "mine"
505
                ops[:filter_user]="-3"
506
            when "g", "group"
507
                ops[:filter_user]="-1"
508
            else
509
                if !ARGV[0].match(/^[0123456789]+$/)
510
                    ops[:filter_user]="-2"
511
                    ops[:filter_flag]=ARGV[0]
512
                else
513
                    ops[:filter_user]=ARGV[0]
514
                end
515
        end
516
     else
517
        ops[:filter_user]="-2"
518
     end
519

    
520
     ops
521
end
522

    
523
onevm_opts=OnevmParse.new
524
onevm_opts.parse(ARGV)
525
ops=onevm_opts.options
526

    
527
result=[false, "Unknown error"]
528

    
529
command=ARGV.shift
530

    
531
case command
532
when "submit", "create"
533
    check_parameters("create", 1)
534
    vm=OpenNebula::VirtualMachine.new(
535
        OpenNebula::VirtualMachine.build_xml, get_one_client)
536

    
537
    template = ""
538
    success = false
539

    
540
    if( File.file?(ARGV[0]) )
541
        # argument is a file path
542
        begin
543
            template = File.read(ARGV[0])
544
            success = true
545
        rescue
546
            result = OpenNebula::Error.new("Can not read template: #{ARGV[0]}")
547
        end
548
    else
549
        # argument could be a template ID or a template name
550
        template_id = get_template_id(ARGV[0])
551

    
552
        template = "TEMPLATE_ID = #{template_id}"
553
        template << "\nNAME = #{ops[:vm_name]}" if ops[:vm_name]
554

    
555
        success = true
556
    end
557

    
558
    if( success )
559
        result = vm.allocate(template)
560
    end
561

    
562
    if is_successful?(result)
563
        puts "ID: " + vm.id.to_s if ops[:verbose]
564
        exit 0
565
    end
566

    
567
when "deploy"
568
    check_parameters("deploy", 2)
569
    host_id=get_host_id(ARGV[-1])
570
    args=expand_args(ARGV[0..-2])
571

    
572
    args.each do |param|
573
        vm_id=get_vm_id(param)
574

    
575
        vm=OpenNebula::VirtualMachine.new_with_id(vm_id, get_one_client)
576

    
577
        result=vm.deploy(host_id)
578
        if is_successful?(result)
579
            puts "Deploying VM" if ops[:verbose]
580
        else
581
            break
582
        end
583
    end
584

    
585
when "shutdown"
586
    check_parameters("shutdown", 1)
587
    args=expand_args(ARGV)
588

    
589
    args.each do |param|
590
        vm_id=get_vm_id(param)
591

    
592
        vm=OpenNebula::VirtualMachine.new_with_id(vm_id, get_one_client)
593
        result=vm.shutdown
594
        if is_successful?(result)
595
            puts "Shutting down VM" if ops[:verbose]
596
        else
597
            break
598
        end
599
    end
600

    
601
when "livemigrate"
602
    check_parameters("livemigrate", 2)
603
    host_id=get_host_id(ARGV[-1])
604
    args=expand_args(ARGV[0..-2])
605

    
606
    args.each do |param|
607
        vm_id=get_vm_id(param)
608

    
609
        vm=OpenNebula::VirtualMachine.new_with_id(vm_id, get_one_client)
610

    
611
        result=vm.live_migrate(host_id)
612
        if is_successful?(result)
613
            puts "Migrating VM" if ops[:verbose]
614
        else
615
            break
616
        end
617
    end
618

    
619
when "migrate"
620
    check_parameters("migrate", 2)
621
    host_id=get_host_id(ARGV[-1])
622
    args=expand_args(ARGV[0..-2])
623

    
624
    args.each do |param|
625
        vm_id=get_vm_id(param)
626

    
627
        vm=OpenNebula::VirtualMachine.new_with_id(vm_id, get_one_client)
628

    
629
        result=vm.migrate(host_id)
630
        if is_successful?(result)
631
            puts "Migrating VM" if ops[:verbose]
632
        else
633
            break
634
        end
635
    end
636

    
637
when "hold"
638
    check_parameters("hold", 1)
639
    args=expand_args(ARGV)
640

    
641
    args.each do |param|
642
        vm_id=get_vm_id(param)
643

    
644
        vm=OpenNebula::VirtualMachine.new_with_id(vm_id, get_one_client)
645

    
646
        result=vm.hold
647
        if is_successful?(result)
648
            puts "Setting VM to hold state" if ops[:verbose]
649
        else
650
            break
651
        end
652
    end
653

    
654
when "release"
655
    check_parameters("release", 1)
656
    args=expand_args(ARGV)
657

    
658
    args.each do |param|
659
        vm_id=get_vm_id(param)
660

    
661
        vm=OpenNebula::VirtualMachine.new_with_id(vm_id, get_one_client)
662

    
663
        result=vm.release
664
        if is_successful?(result)
665
            puts "Releasing VM" if ops[:verbose]
666
        else
667
            break
668
        end
669
    end
670

    
671
when "stop"
672
    check_parameters("stop", 1)
673
    args=expand_args(ARGV)
674

    
675
    args.each do |param|
676
        vm_id=get_vm_id(param)
677

    
678
        vm=OpenNebula::VirtualMachine.new_with_id(vm_id, get_one_client)
679

    
680
        result=vm.stop
681
        if is_successful?(result)
682
            puts "Stopping VM" if ops[:verbose]
683
        else
684
            break
685
        end
686
    end
687

    
688
when "cancel"
689
    check_parameters("cancel", 1)
690
    args=expand_args(ARGV)
691

    
692
    args.each do |param|
693
        vm_id=get_vm_id(param)
694

    
695
        vm=OpenNebula::VirtualMachine.new_with_id(vm_id, get_one_client)
696

    
697
        result=vm.cancel
698
        if is_successful?(result)
699
            puts "Cancelling VM" if ops[:verbose]
700
        else
701
            break
702
        end
703
    end
704

    
705
when "suspend"
706
    check_parameters("suspend", 1)
707
    args=expand_args(ARGV)
708

    
709
    args.each do |param|
710
        vm_id=get_vm_id(param)
711

    
712
        vm=OpenNebula::VirtualMachine.new_with_id(vm_id, get_one_client)
713

    
714
        result=vm.suspend
715
        if is_successful?(result)
716
            puts "Suspending VM" if ops[:verbose]
717
        else
718
            break
719
        end
720
    end
721

    
722
when "resume"
723
    check_parameters("resume", 1)
724
    args=expand_args(ARGV)
725

    
726
    args.each do |param|
727
        vm_id=get_vm_id(param)
728

    
729
        vm=OpenNebula::VirtualMachine.new_with_id(vm_id, get_one_client)
730

    
731
        result=vm.resume
732
        if is_successful?(result)
733
            puts "Resuming VM" if ops[:verbose]
734
        else
735
            break
736
        end
737
    end
738

    
739
when "restart"
740
    check_parameters("restart", 1)
741
    args=expand_args(ARGV)
742

    
743
    args.each do |param|
744
        vm_id=get_vm_id(param)
745

    
746
        vm=OpenNebula::VirtualMachine.new_with_id(vm_id, get_one_client)
747

    
748
        result=vm.restart
749
        if is_successful?(result)
750
            puts "Restarting VM" if ops[:verbose]
751
        else
752
            break
753
        end
754
    end
755

    
756
when "resubmit"
757
    check_parameters("resubmit", 1)
758
    args=expand_args(ARGV)
759

    
760
    args.each do |param|
761
        vm_id=get_vm_id(param)
762

    
763
        vm=OpenNebula::VirtualMachine.new_with_id(vm_id, get_one_client)
764

    
765
        result=vm.resubmit
766
        if is_successful?(result)
767
            puts "Resubmitting VM" if ops[:verbose]
768
        else
769
            break
770
        end
771
    end
772

    
773
when "list"
774
    ops.merge!(get_user_flags)
775
    if !ops[:xml]
776
        vmlist=VmShow.new(get_one_client, ops[:filter_user].to_i)
777

    
778
        ops[:columns]=ops[:list] if ops[:list]
779
        result=vmlist.list_short(ops)
780
    else
781
        vmpool=OpenNebula::VirtualMachinePool.new(get_one_client,
782
            ops[:filter_user].to_i)
783
        vmpool.info
784
        puts vmpool.to_xml(true)
785
    end
786

    
787
when "top"
788
    ops.merge!(get_user_flags)
789
    vmlist=VmShow.new(get_one_client, ops[:filter_user].to_i)
790
    ops[:columns]=ops[:list] if ops[:list]
791
    result=vmlist.top(ops)
792

    
793

    
794
when "history"
795
    vmlist=VmShow.new(get_one_client)
796
    ops[:columns]=ops[:list] if ops[:list]
797
    if ARGV[0]
798
        ids=ARGV.collect {|arg| get_vm_id(arg)}
799
        ids=ids.flatten.compact
800
        result=vmlist.list_vm_history_array(ids)
801
    else
802
        result=vmlist.list_vm_history_all(ops)
803
    end
804

    
805
when "delete"
806
    check_parameters("delete", 1)
807
    args=expand_args(ARGV)
808

    
809
    args.each do |param|
810
        vm_id=get_vm_id(param)
811

    
812
        vm=OpenNebula::VirtualMachine.new_with_id(vm_id, get_one_client)
813

    
814
        result=vm.finalize
815
        if is_successful?(result)
816
            puts "VM correctly deleted"  if ops[:verbose]
817
        else
818
            break
819
        end
820
    end
821
    
822
when "saveas"
823
    check_parameters("saveas", 3)
824
    vm_id = get_vm_id(ARGV[0])
825
    disk_id = ARGV[1]
826
    image_name = ARGV[2]
827

    
828
    # Get the Image ID for this disk
829
    vm = OpenNebula::VirtualMachine.new(
830
                OpenNebula::VirtualMachine.build_xml(vm_id),
831
                get_one_client)
832
                
833
    result = vm.info
834
    if !is_successful?(result)
835
        puts result.message
836
        exit -1
837
    end
838

    
839
    result = vm.save_as(disk_id.to_i, image_name)
840
    if is_successful?(result)
841
        puts "VM disk with ID #{disk_id} is prepared to be" << 
842
             " saved"  if ops[:verbose]
843
    end
844

    
845
when "show"
846
    check_parameters("get_info", 1)
847
    args=expand_args(ARGV)
848

    
849
    args.each do |param|
850

    
851
        vm_id=get_vm_id(param)
852

    
853
        vm=OpenNebula::VirtualMachine.new_with_id(vm_id, get_one_client)
854
        result=vm.info
855
        if is_successful?(result)
856
            if !ops[:xml]
857
                str="%-15s: %-20s"
858
                str_h1="%-80s"
859

    
860
                print_header(str_h1, "VIRTUAL MACHINE #{vm['ID']} INFORMATION",
861
true)
862

    
863
                puts str % ["ID", vm.id.to_s]
864
                puts str % ["NAME", vm.name]
865
                puts str % ["STATE", vm.state_str]
866
                puts str % ["LCM_STATE", vm.lcm_state_str]
867

    
868
                value=vm['STIME'].to_i
869
                if value==0
870
                    value='-'
871
                else
872
                    value=Time.at(value).strftime("%m/%d %H:%M:%S")
873
                end
874
                puts str % ["START TIME", value]
875

    
876
                value=vm['ETIME'].to_i
877
                if value==0
878
                    value='-'
879
                else
880
                    value=Time.at(value).strftime("%m/%d %H:%M:%S")
881
                end
882
                puts str % ["END TIME", value]
883

    
884
                value=vm['DEPLOY_ID']
885
                puts str % ["DEPLOY ID:", value=="" ? "-" : value]
886

    
887
                puts
888

    
889
                print_header(str_h1,"VIRTUAL MACHINE MONITORING",false)
890

    
891
                poll_attrs = {
892
                    "USED MEMORY" => "MEMORY",
893
                    "USED CPU" => "CPU",
894
                    "NET_TX" => "NET_TX",
895
                    "NET_RX" => "NET_RX"
896
                }
897

    
898
                poll_attrs.each do |k,v|
899
                    puts str % [k,vm[v]]
900
                end
901
                
902
                puts
903
                
904
                print_header(str_h1,"VIRTUAL MACHINE TEMPLATE",false)
905

    
906
                puts vm.template_str
907
            else
908
                puts vm.to_xml(true)
909
            end
910
        end
911
     end
912
else
913
    onevm_opts.print_help
914
    exit -1
915
end
916

    
917

    
918

    
919
if OpenNebula.is_error?(result)
920
    puts "Error: " + result.message
921
    exit -1
922
end
923

    
924