oneacct_helper.rb

OpenNebula Systems Support Team, 10/13/2017 10:53 AM

Download (8.65 KB)

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

    
17
require 'one_helper'
18
require 'optparse/time'
19

    
20
class AcctHelper < OpenNebulaHelper::OneHelper
21
    START_TIME_ACCT = {
22
        :name   => "start_time",
23
        :short  => "-s TIME",
24
        :large  => "--start TIME" ,
25
        :description => "First day of the data to retrieve",
26
        :format => Time
27
    }
28

    
29
    END_TIME_ACCT = {
30
        :name   => "end_time",
31
        :short  => "-e TIME",
32
        :large  => "--end TIME" ,
33
        :description => "Last day of the data to retrieve",
34
        :format => Time
35
    }
36

    
37
    START_TIME_SHOWBACK = {
38
        :name   => "start_time",
39
        :short  => "-s TIME",
40
        :large  => "--start TIME" ,
41
        :description => "First month of the data",
42
        :format => Time
43
    }
44

    
45
    END_TIME_SHOWBACK = {
46
        :name   => "end_time",
47
        :short  => "-e TIME",
48
        :large  => "--end TIME" ,
49
        :description => "Last month of the data",
50
        :format => Time
51
    }
52

    
53
    USERFILTER = {
54
        :name   => "userfilter",
55
        :short  => "-u user",
56
        :large  => "--userfilter user" ,
57
        :description => "User name or id to filter the results",
58
        :format => String,
59
        :proc => lambda { |o, options|
60
            OpenNebulaHelper.rname_to_id(o, "USER")
61
        }
62
    }
63

    
64
    GROUP = {
65
        :name   => "group",
66
        :short  => "-g group",
67
        :large  => "--group group" ,
68
        :description => "Group name or id to filter the results",
69
        :format => String,
70
        :proc => lambda { |o, options|
71
            OpenNebulaHelper.rname_to_id(o, "GROUP")
72
        }
73
    }
74

    
75
    HOST = {
76
        :name   => "host",
77
        :short  => "-H HOST",
78
        :large  => "--host HOST" ,
79
        :description => "Host name or id to filter the results",
80
        :format => String,
81
        :proc => lambda { |o, options|
82
            OpenNebulaHelper.rname_to_id(o, "HOST")
83
        }
84
    }
85

    
86
    XPATH = {
87
        :name   => "xpath",
88
        :large  => "--xpath XPATH_EXPRESSION" ,
89
        :description => "Xpath expression to filter the results. \
90
            For example: oneacct --xpath 'HISTORY[ETIME>0]'",
91
        :format => String
92
    }
93

    
94
    XML = {
95
        :name  => "xml",
96
        :short => "-x",
97
        :large => "--xml",
98
        :description => "Show the resource in xml format"
99
    }
100

    
101
    JSON = {
102
        :name  => "json",
103
        :short => "-j",
104
        :large => "--json",
105
        :description => "Show the resource in json format"
106
    }
107

    
108
    SPLIT={
109
        :name  => "split",
110
        :large => "--split",
111
        :description => "Split the output in a table for each VM"
112
    }
113

    
114
    ACCT_OPTIONS     = [START_TIME_ACCT, END_TIME_ACCT, USERFILTER, GROUP, HOST, XPATH, XML, JSON, SPLIT]
115
    SHOWBACK_OPTIONS = [START_TIME_SHOWBACK, END_TIME_SHOWBACK, USERFILTER, GROUP, XML, JSON]
116

    
117
    ACCT_TABLE = CLIHelper::ShowTable.new(self.table_conf("oneacct.yaml"), nil) do
118
        column :UID, "User ID", :size=>4 do |d|
119
            d["UID"]
120
        end
121

    
122
        column :VID, "Virtual Machine ID", :size=>4 do |d|
123
            d["OID"]
124
        end
125

    
126
        column :SEQ, "History record sequence number", :size=>3 do |d|
127
            d["SEQ"]
128
        end
129

    
130
        column :HOSTNAME, "Host name", :left, :size=>15 do |d|
131
            d["HOSTNAME"]
132
        end
133

    
134
        column :"ACTION", "VM state change action", :left, :size=>16 do |d|
135
            VirtualMachine.get_history_action d["ACTION"]
136
        end
137

    
138
        column :REASON, "VM state change reason", :left, :size=>4 do |d|
139
            VirtualMachine.get_reason d["REASON"]
140
        end
141

    
142
        column :START_TIME, "Start time", :size=>14 do |d|
143
            OpenNebulaHelper.time_to_str(d['STIME'])
144
        end
145

    
146
        column :END_TIME, "End time", :size=>14 do |d|
147
            OpenNebulaHelper.time_to_str(d['ETIME'])
148
        end
149

    
150
        column :MEMORY, "Assigned memory", :size=>6 do |d|
151
            OpenNebulaHelper.unit_to_str(d["VM"]["TEMPLATE"]["MEMORY"].to_i, {}, 'M') rescue "-"
152
        end
153

    
154
        column :CPU, "Number of CPUs", :size=>3 do |d|
155
            d["VM"]["TEMPLATE"]["CPU"] rescue "-"
156
        end
157

    
158
        column :NETRX, "Data received from the network", :size=>6 do |d|
159
            # NET is measured in bytes, unit_to_str expects KBytes
160
            OpenNebulaHelper.unit_to_str(d["VM"]["MONITORING"]["NETRX"].to_i / 1024.0, {}) rescue "-"
161
        end
162

    
163
        column :NETTX, "Data sent to the network", :size=>6 do |d|
164
            # NET is measured in bytes, unit_to_str expects KBytes
165
            OpenNebulaHelper.unit_to_str(d["VM"]["MONITORING"]["NETTX"].to_i / 1024.0, {}) rescue "-"
166
        end
167

    
168
        column :DISK, "Total disk size used", :size=>6 do |d|
169
            begin
170
                # DISK size is measured in mb, unit_to_str expects KBytes
171
                total_disk_size = 0
172

    
173
                vm_id = d["VM"]["ID"].to_i
174

    
175
                disks_all = [d["VM"]["TEMPLATE"]["DISK"]].flatten.compact rescue []
176
                disks_all.each do |disk|
177
                    total_disk_size += disk["SIZE"].to_i
178
                end
179

    
180
                snapshots_all = [d["VM"]["SNAPSHOTS"]].flatten.compact rescue []
181
                snapshots_all.each do |snapshot|
182
                    snapshot_disk = [snapshot["SNAPSHOT"]].flatten.compact rescue []
183
                    snapshot_disk.each do |snapshot|
184
                        total_disk_size += snapshot["SIZE"].to_i
185
                    end
186
                end
187

    
188
                OpenNebulaHelper.unit_to_str(total_disk_size * 1024.0, {})
189
            rescue
190
                "-"
191
            end
192
        end
193

    
194
        default :VID, :HOSTNAME, :ACTION, :REASON, :START_TIME, :END_TIME, :MEMORY, :CPU, :NETRX, :NETTX, :DISK
195
    end
196

    
197
    SHOWBACK_TABLE = CLIHelper::ShowTable.new(self.table_conf("oneshowback.yaml"), nil) do
198
        column :UID, "User ID", :size=>4 do |d|
199
            d["UID"]
200
        end
201

    
202
        column :USER_NAME, "User name", :left, :size=>12 do |d|
203
            d["UNAME"]
204
        end
205

    
206
        column :GID, "Group ID", :size=>4 do |d|
207
            d["GID"]
208
        end
209

    
210
        column :GROUP_NAME, "Group name", :left, :size=>12 do |d|
211
            d["GNAME"]
212
        end
213

    
214
        column :VM_ID, "Virtual Machine ID", :size=>6 do |d|
215
            d["VMID"]
216
        end
217

    
218
        column :VM_NAME, "Virtual Machine name", :left, :size=>12 do |d|
219
            d["VMNAME"]
220
        end
221

    
222
        column :MONTH, "Month", :size=>5 do |d|
223
            d["MONTH"]
224
        end
225

    
226
        column :YEAR, "Year", :size=>5 do |d|
227
            d["YEAR"]
228
        end
229

    
230
        column :HOURS, "Hours", :size=>6 do |d|
231
            d["HOURS"]
232
        end
233

    
234
        column :COST, "Cost", :size=>15 do |d|
235
            d["TOTAL_COST"]
236
        end
237

    
238
        default :USER_NAME, :GROUP_NAME, :VM_ID, :VM_NAME, :MONTH, :YEAR, :HOURS, :COST
239
    end
240

    
241
    def self.print_start_end_time_header(start_time, end_time)
242
        print "Showing active history records from "
243

    
244
        CLIHelper.scr_bold
245
        if ( start_time != -1 )
246
            print Time.at(start_time).to_s
247
        else
248
            print "-"
249
        end
250

    
251
        CLIHelper.scr_restore
252
        print " to "
253

    
254
        CLIHelper.scr_bold
255
        if ( end_time != -1 )
256
            print Time.at(end_time).to_s
257
        else
258
            print "-"
259
        end
260

    
261
        CLIHelper.scr_restore
262
        puts
263
        puts
264
    end
265

    
266
    def self.print_user_header(user_id)
267
        CLIHelper.scr_bold
268
        CLIHelper.scr_underline
269
        puts "# User #{user_id}".ljust(80)
270
        CLIHelper.scr_restore
271
        puts
272
    end
273

    
274
    def self.print_month_header(year, month)
275
        CLIHelper.scr_bold
276
        CLIHelper.scr_underline
277
        puts "# Showback for #{month}/#{year}".ljust(80)
278
        CLIHelper.scr_restore
279
        puts
280
    end
281
end