<commit_before>#!/usr/bin/env ruby

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

def print_info(name, value)
    value = "0" if value.nil? or value.to_s.strip.empty?
    puts "#{name}=#{value}"
end

#CPU


# CPU

cpuinfo=`cat /proc/cpuinfo`
exit(-1) if $?.exitstatus != 0

#ncpus=$(echo "$cpuinfo"|grep processor|wc -l)
#total_cpu=$(($ncpus*100))
#cpu_speed=$(echo "$cpuinfo"|grep "cpu MHz"|sort -u|sed s/[^0-9.]//g)
#cpu_speed=`printf "%.2f" $cpu_speed`
#free_cpu=`top -bin1| grep "Cpu"|cut -d ',' -f4|sed s/[^0-9.]//g`
#free_cpu=`echo "$free_cpu*$ncpus"|bc`
#used_cpu=`echo "$total_cpu - $free_cpu"|bc`
#used_cpu=`printf "%.2f" $used_cpu`


#top_text=`top -bin1`
#exit(-1) if $?.exitstatus != 0

#top_text.gsub!(/^top.*^top.*?$/m, "") # Strip first top output

#top_text.split(/\n/).each{|line|
#    if line.match('^%?Cpu')
#        line[7..-1].split(",").each{|elemento|
#            temp = elemento.strip.split(/[% ]/)
#            if temp[1]=="id"
#            idle = temp[0]
#            $free_cpu = idle.to_f * $total_cpu.to_f / 100
#            $used_cpu = $total_cpu.to_f - $free_cpu
#                break
#            end
#
#        }
#    end
#}

$total_memory = `free -k|grep "Mem:" | awk '{print $2}'`
tmp=`free -k|grep "buffers\/cache"|awk '{print $3 " " $4}'`.split

$used_memory=tmp[0]
$free_memory=tmp[1]

# get in/out used bandwith information

net_text=`cat /proc/net/dev`
exit(-1) if $?.exitstatus != 0

LOOPBACK_NAME = "lo"

netrx = 0
nettx = 0

# sum in and put used bandwith on all interface except of one loopback
# interface with a default name LOOPBACK_NAME
# drop first 2 lines of net_text file - headers
net_text.split(/\n/).drop(2).each{|line|
    unless line.match("^ *#{LOOPBACK_NAME }")
        arr = line.split(":")[1].split(" ")
        netrx += arr[0].to_i()
        nettx += arr[8].to_i()
    end
}

$netrx = netrx.to_s()
$nettx = nettx.to_s()

print_info("HYPERVISOR","ovz")

print_info("TOTALCPU",$total_cpu)

print_info("TOTALMEMORY",$total_memory)
print_info("USEDMEMORY",$used_memory)
print_info("FREEMEMORY",$free_memory)

print_info("FREECPU",$free_cpu)
print_info("USEDCPU",$used_cpu)

print_info("NETRX",$netrx)
print_info("NETTX",$nettx)<commit_msg>Fixed interface name problem: now, summing up in/put used bandwith in all interfaces except loopback<commit_after>#!/usr/bin/env ruby

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

def print_info(name, value)
    value = "0" if value.nil? or value.to_s.strip.empty?
    puts "#{name}=#{value}"
end

# CPU

cpuinfo=`cat /proc/cpuinfo`
exit(-1) if $?.exitstatus != 0

# how many virtual processors are they?
# each processor in this file appear as a single line in a format: processor: processor_id
$ncpus = cpuinfo.grep(/processor/).count
$total_cpu=$ncpus*100

# cpu speed - is always equal for each core, so read the first one
$cpu_speed = cpuinfo.grep(/cpu MHz/).to_s().split(":")[1]

top_text=`top -bin1`
exit(-1) if $?.exitstatus != 0

top_text.gsub!(/^top.*^top.*?$/m, "") # Strip first top output

top_text.split(/\n/).each{|line|
    if line.match('^%?Cpu')
        line[7..-1].split(",").each{|elemento|
            temp = elemento.strip.split(/[% ]/)
            if temp[1]=="id"
            idle = temp[0]
            $free_cpu = idle.to_f * $total_cpu.to_f / 100
            $used_cpu = $total_cpu.to_f - $free_cpu
                break
            end

        }
    end
}

$total_memory = `free -k|grep "Mem:" | awk '{print $2}'`
tmp=`free -k|grep "buffers\/cache"|awk '{print $3 " " $4}'`.split

$used_memory=tmp[0]
$free_memory=tmp[1]

# get in/out used bandwith information

net_text=`cat /proc/net/dev`
exit(-1) if $?.exitstatus != 0

LOOPBACK_NAME = "lo"

netrx = 0
nettx = 0

# sum in and put used bandwith on all interface except of one loopback
# interface with a default name LOOPBACK_NAME
# drop first 2 lines of net_text file - headers
net_text.split(/\n/).drop(2).each{|line|
    unless line.match("^ *#{LOOPBACK_NAME }")
        arr = line.split(":")[1].split(" ")
        netrx += arr[0].to_i()
        nettx += arr[8].to_i()
    end
}

$netrx = netrx.to_s()
$nettx = nettx.to_s()

print_info("HYPERVISOR","ovz")

print_info("TOTALCPU",$total_cpu)

print_info("TOTALMEMORY",$total_memory)
print_info("USEDMEMORY",$used_memory)
print_info("FREEMEMORY",$free_memory)

print_info("FREECPU",$free_cpu)
print_info("USEDCPU",$used_cpu)

print_info("CPUSPEED",$cpu_speed)

print_info("NETRX",$netrx)
print_info("NETTX",$nettx)
{'fraction_non_alphanumeric': 0.20758213984835722, 'fraction_numerical': 0.015669755686604885, 'mean_word_length': 2.9520639147802927, 'pattern_counts': {'":': 3, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1662942', 'n_tokens_mistral': 2143, 'n_tokens_neox': 2067, 'n_words': 522}