diff --git a/install.sh b/install.sh index 1fd3bfb24b..267c08ad26 100755 --- a/install.sh +++ b/install.sh @@ -1150,7 +1150,8 @@ CLI_CONF_FILES="src/cli/etc/onegroup.yaml \ src/cli/etc/onevnet.yaml \ src/cli/etc/oneacl.yaml \ src/cli/etc/onedatastore.yaml \ - src/cli/etc/onecluster.yaml" + src/cli/etc/onecluster.yaml \ + src/cli/etc/oneacct.yaml" ETC_CLIENT_FILES="src/cli/etc/group.default" diff --git a/share/install_gems/install_gems b/share/install_gems/install_gems index 6ddea676d9..9e3be33a59 100755 --- a/share/install_gems/install_gems +++ b/share/install_gems/install_gems @@ -2,26 +2,26 @@ require 'pp' -DEFAULT=%w{optional sunstone quota cloud ozones_server auth_ldap} +$nokogiri='nokogiri' + +DEFAULT=%w{sunstone quota cloud ozones_server auth_ldap} if defined?(RUBY_VERSION) && RUBY_VERSION>="1.8.7" SQLITE='sqlite3' - else SQLITE='sqlite3-ruby --version 1.2.0' end GROUPS={ - :optional => ['nokogiri'], :quota => [SQLITE, 'sequel'], :sunstone => ['json', 'rack', 'sinatra', 'thin', 'sequel', SQLITE], :cloud => %w{amazon-ec2 rack sinatra thin uuidtools curb json}, :ozones_client => %w{json}, - :ozones_server => %w{json data_mapper dm-sqlite-adapter dm-mysql-adapter}+[ + :ozones_server => %w{json sequel}+[ SQLITE, 'mysql' ], - :ozones_server_sqlite => %w{json data_mapper dm-sqlite-adapter}< %w{json data_mapper dm-mysql-adapter mysql}, + :ozones_server_sqlite => %w{json sequel}< %w{json sequel mysql}, :auth_ldap => 'net-ldap' } @@ -220,12 +220,14 @@ def help puts DEFAULT.join(' ') puts puts "Use --check parameter to search for non installed libraries." + puts "Use --no-nokogiri parameter if you don't want to install" + puts "nokogiri gem." end def get_gems(packages) - packages.map do |package| + (packages.map do |package| GROUPS[package.to_sym] - end.flatten.uniq-installed_gems + end+[$nokogiri]).flatten.uniq-installed_gems end def detect_distro @@ -427,6 +429,11 @@ install_rubygems command='' params=ARGV +if params.include?('--no-nokogiri') + params-=['--no-nokogiri'] + $nokogiri=nil +end + if params.include?('-h') params-=['-h'] command='help' diff --git a/src/cli/etc/oneacct.yaml b/src/cli/etc/oneacct.yaml new file mode 100644 index 0000000000..0e79bf3db1 --- /dev/null +++ b/src/cli/etc/oneacct.yaml @@ -0,0 +1,53 @@ +--- +:VID: + :desc: Virtual Machine ID + :size: 4 + +:SEQ: + :desc: History record sequence number + :size: 3 + +:HOSTNAME: + :desc: Host name + :size: 15 + :left: true + +:REASON: + :desc: VM state change reason + :size: 4 + :left: true + +:START_TIME: + :desc: Start time + :size: 14 + +:END_TIME: + :desc: End time + :size: 14 + +:MEMORY: + :desc: Assigned memory + :size: 6 + +:CPU: + :desc: Number of CPUs + :size: 3 + +:NET_RX: + :desc: Data received from the network + :size: 6 + +:NET_TX: + :desc: Data sent to the network + :size: 6 + +:default: +- :VID +- :HOSTNAME +- :REASON +- :START_TIME +- :END_TIME +- :MEMORY +- :CPU +- :NET_RX +- :NET_TX \ No newline at end of file diff --git a/src/cli/oneacct b/src/cli/oneacct index a9101f5bc1..ecd1554fbb 100755 --- a/src/cli/oneacct +++ b/src/cli/oneacct @@ -37,10 +37,15 @@ require 'json' require 'optparse' require 'optparse/time' -class AcctHelper - def initialize(client) - @client = client +################################################################################ +# CLI Helper +################################################################################ + +class AcctHelper < OpenNebulaHelper::OneHelper + + def self.conf_file + "oneacct.yaml" end =begin @@ -64,7 +69,7 @@ class AcctHelper =end def list_history(data) - table = CLIHelper::ShowTable.new(nil, self) do + table = CLIHelper::ShowTable.new(self.class.table_conf, self) do column :VID, "Virtual Machine ID", :size=>4 do |d| d["OID"] end @@ -73,11 +78,11 @@ class AcctHelper d["SEQ"] end - column :HOSTNAME, "Host name", :size=>15 do |d| + column :HOSTNAME, "Host name", :left, :size=>15 do |d| d["HOSTNAME"] end - column :REASON, "VM state change reason", :size=>4 do |d| + column :REASON, "VM state change reason", :left, :size=>4 do |d| VirtualMachine.get_reason d["REASON"] end @@ -89,20 +94,20 @@ class AcctHelper OpenNebulaHelper.time_to_str(d['ETIME']) end - column :MEMORY, "Assigned memory", :right, :size=>6 do |d| + column :MEMORY, "Assigned memory", :size=>6 do |d| OpenNebulaHelper.unit_to_str(d["VM/TEMPLATE/MEMORY"].to_i, {}, 'M') end - column :CPU, "Number of CPUs", :right, :size=>3 do |d| + column :CPU, "Number of CPUs", :size=>3 do |d| d["VM/TEMPLATE/CPU"] end - column :NET_RX, "Data received from the network", :right, :size=>6 do |d| + column :NET_RX, "Data received from the network", :size=>6 do |d| # NET is measured in bytes, unit_to_str expects KBytes OpenNebulaHelper.unit_to_str(d["VM/NET_RX"].to_i / 1024.0, {}) end - column :NET_TX, "Data sent to the network", :right, :size=>6 do |d| + column :NET_TX, "Data sent to the network", :size=>6 do |d| # NET is measured in bytes, unit_to_str expects KBytes OpenNebulaHelper.unit_to_str(d["VM/NET_TX"].to_i / 1024.0, {}) end @@ -114,10 +119,7 @@ class AcctHelper end public - def list_users(xml_info, options=nil) - - xmldoc = XMLElement.new - xmldoc.initialize_xml(xml_info, 'HISTORY_RECORDS') + def list_users(xmldoc, options=nil) uids = xmldoc.retrieve_elements('HISTORY/VM/UID') @@ -133,8 +135,10 @@ public uids.each do |uid| CLIHelper.scr_bold CLIHelper.scr_underline - # TODO: username? - puts "# User #{uid} " + + username = + xmldoc.retrieve_elements(["HISTORY/VM[UID=#{uid}]/UNAME"]).uniq; + puts "# User #{uid} #{username} " CLIHelper.scr_restore puts @@ -167,6 +171,28 @@ public end end +################################################################################ +# Helper methods +################################################################################ + +def redo_xmldoc(xmldoc, xpath_str) + xml_str = "" + + xmldoc.each(xpath_str) do |history| + xml_str << history.to_xml + end + + xml_str << "" + + xmldoc = XMLElement.new + xmldoc.initialize_xml(xml_str, 'HISTORY_RECORDS') + + return xmldoc +end + +################################################################################ +# Main command +################################################################################ options = Hash.new @@ -183,10 +209,24 @@ opts = OptionParser.new do |opts| options[:end]=ext end - # TODO: Allow username - opts.on("-u", "--user user", Integer, - "User id to make accounting" ) do |ext| - options[:user]=ext.to_i + opts.on("-u", "--user user", String, + "User id to filter the results" ) do |ext| + options[:user]=ext + end + + opts.on("-g", "--group group", String, + "Group id to filter the results" ) do |ext| + options[:group]=ext + end + + opts.on("-H", "--host hostname", String, + "Host id to filter the results" ) do |ext| + options[:host]=ext + end + + opts.on("--xpath expression", String, + "Xpath expression to filter the results. For example: oneacct --xpath 'HISTORY[ETIME>0]'" ) do |ext| + options[:xpath]=ext end opts.on("-j", "--json", @@ -204,6 +244,11 @@ opts = OptionParser.new do |opts| options[:split]=ext end + opts.on("-h", "--help", "Show this message" ) do + puts opts + exit + end + opts.on() end @@ -218,7 +263,7 @@ end client = OpenNebula::Client.new -acct_helper = AcctHelper.new(client) +acct_helper = AcctHelper.new() time_start = -1 time_end = -1 @@ -226,19 +271,72 @@ filter_flag = VirtualMachinePool::INFO_ALL time_start = options[:start].to_i if options[:start] time_end = options[:end].to_i if options[:end] -filter_flag = options[:user].to_i if options[:user] +if options[:user] + rc = OpenNebulaHelper.rname_to_id(options[:user], "USER") -ret = client.call("vmpool.accounting", + if rc[0] == 0 + filter_flag = rc[1] + else + puts rc[1] + exit -1 + end +end + +xml_str = client.call("vmpool.accounting", filter_flag, time_start, time_end) -if OpenNebula.is_error?(ret) - puts ret.message +if OpenNebula.is_error?(xml_str) + puts xml_str.message exit -1 end +xmldoc = XMLElement.new +xmldoc.initialize_xml(xml_str, 'HISTORY_RECORDS') + +xpath = nil +hid = nil +gid = nil + +if options[:host] + rc = OpenNebulaHelper.rname_to_id(options[:host], "HOST") + + if rc[0] == 0 + hid = rc[1] + else + puts rc[1] + exit -1 + end +end + +if options[:group] + rc = OpenNebulaHelper.rname_to_id(options[:group], "GROUP") + + if rc[0] == 0 + gid = rc[1] + else + puts rc[1] + exit -1 + end +end + +if options[:host] && options[:group] + xpath = "HISTORY[VM/GID=#{gid} and HID=#{hid}]" +elsif options[:host] + xpath = "HISTORY[HID=#{hid}]" +elsif options[:group] + xpath = "HISTORY[VM/GID=#{gid}]" +end + +xmldoc = redo_xmldoc(xmldoc, xpath) if !xpath.nil? + + +if options[:xpath] + xmldoc = redo_xmldoc(xmldoc, options[:xpath]) +end + case options[:format] when :table @@ -270,14 +368,12 @@ when :table puts end - acct_helper.list_users(ret, options) + acct_helper.list_users(xmldoc, options) when :xml - puts ret + puts xmldoc.to_xml when :json - xmldoc = XMLElement.new - xmldoc.initialize_xml(ret, 'HISTORY_RECORDS') puts xmldoc.to_hash.to_json end