Class: Info

Inherits:
Object
  • Object
show all
Defined in:
lib/ServState/info.rb

Constant Summary collapse

@@old_usage =
0
@@old_total_usage =
0
@@old_traffic =
Hash.new
@@old_time_for_traffic =
Time.now
@@cpu_stat_value =
0
@@traffic_speed_value =
0

Class Method Summary collapse

Class Method Details

.cpu_statObject

fixnum % of cpu load



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ServState/info.rb', line 40

def self.cpu_stat 
  if @@old_total_usage==0 # first call
    @@old_usage=IO.readlines('/proc/stat').grep(/^cpu /).first.split[1..3].collect{|i| i.to_i}.inject(:+)
    @@old_total_usage=IO.readlines('/proc/stat').grep(/^cpu /).first.split.collect{|i| i.to_i}.inject(:+)
    sleep 1
  end
  new_usage=IO.readlines('/proc/stat').grep(/^cpu /).first.split[1..3].collect{|i| i.to_i}.inject(:+)
  usage=new_usage-@@old_usage

  new_total_usage=IO.readlines('/proc/stat').grep(/^cpu /).first.split.collect{|i| i.to_i}.inject(:+)
  total_usage=new_total_usage-@@old_total_usage

  @@old_usage=new_usage
  @@old_total_usage=new_total_usage

  ((usage/total_usage.to_f)*100).to_i
end

.disk_usageObject

hash[mount point] => [usage %, total space in gb, used space in gb, free space in gb ]



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/ServState/info.rb', line 58

def self.disk_usage 
  disk_usage=Hash.new
  df=`df`.lines.grep(/^\/dev/)
  df.each do |line|
    line=line.split
    name = line[5..-1].join(' ').chomp
    usage_per = line[4].chop.to_i
    total = (line[1].to_f/1048576).round 2
    used = (line[2].to_f/1048576).round 2
    available = (line[3].to_f/1048576).round 2
    disk_usage[name]=[usage_per,total,used,available]
  end
  disk_usage
end

.get_cpu_statObject

operations of getting cpu load or network load take a few seconds we will calculate this values in another thread and just return values by this functions



115
116
117
# File 'lib/ServState/info.rb', line 115

def self.get_cpu_stat
  @@cpu_stat_value
end

.get_traffic_speedObject



118
119
120
# File 'lib/ServState/info.rb', line 118

def self.get_traffic_speed
  @@traffic_speed_value
end

.hostnameObject



8
9
10
# File 'lib/ServState/info.rb', line 8

def self.hostname
  `hostname`.chomp
end

.interfacesObject

hash[interface name] => ipaddr as string



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/ServState/info.rb', line 12

def self.interfaces
  ifconfig=`ifconfig -a`
  ifconfig=ifconfig.lines.map do |line|
    line.chomp
  end
  key=''
  faces=Hash.new
  ifconfig.each do |line|
    unless line.start_with?' ' or line.start_with?'lo'
      key=line.split[0]
    end
    if line.include? 'inet addr:' and key
      faces[key]=line.split[1][5..-1]
    end
  end
  faces
end

.ram_usageObject



72
73
74
75
76
77
78
79
80
81
# File 'lib/ServState/info.rb', line 72

def self.ram_usage
  usage=Hash.new
  ram=`free`.lines
  # [memory type]=[total, used] in Gb
  usage[:ram]=[(ram[1].split[1].to_f/1048576).round(2),(ram[2].split[2].to_f/1048576).round(2)]
  usage[:ram_per]=(100*(ram[2].split[2].to_f/1048576)/(ram[1].split[1].to_f/1048576)).round
  usage[:swap]=[(ram[3].split[1].to_f/1048576).round(2),(ram[3].split[2].to_f/1048576).round(2)]
  usage[:swap_per]=(100*(ram[3].split[2].to_f/1048576)/(ram[3].split[1].to_f/1048576)).round if ram[3].split[1].to_f!=0
  usage
end

.traffic_statObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/ServState/info.rb', line 82

def self.traffic_stat
  new_traffic=Hash.new
  lines=IO.readlines('/proc/net/dev').grep(/:/) 
  lines.each do |line|
    line=line.split
    new_traffic[line[0].chop.to_sym]=[line[1].to_i,line[9].to_i] # [receive bytes, transmit bytes]
  end
  if @@old_traffic.size==0
    @@old_traffic=new_traffic
  end
  speeds=Hash.new
  time_diff=Time.new-@@old_time_for_traffic
  @@old_time_for_traffic=Time.new
  new_traffic.each do |interface,data|
    down=data[0]-@@old_traffic[interface][0]
    up=data[1]-@@old_traffic[interface][1]
    speeds[interface]=[(down/1024/128/time_diff).round(2),(up/1024/128/time_diff).round(2)]
  end
  @@old_traffic=new_traffic
  speeds

end

.updateObject



105
106
107
108
109
110
111
# File 'lib/ServState/info.rb', line 105

def self.update
  @@cpu_stat_value=self.cpu_stat  # run different long-time operations in different threads
  Thread.new{
    @@traffic_speed_value=self.traffic_stat
  }
  # network will be here
end

.uptimeObject

hash with fixnum values



30
31
32
33
34
35
36
37
38
# File 'lib/ServState/info.rb', line 30

def self.uptime
  upt=Hash.new
  upt[:total_sec]=IO.read('/proc/uptime').split.first.to_i
  upt[:days]=upt[:total_sec]/86400
  upt[:hours]=(upt[:total_sec]-upt[:days]*86400)/3600
  upt[:minutes]=(upt[:total_sec]-upt[:days]*86400-upt[:hours]*3600)/60
  upt[:seconds]=upt[:total_sec]-upt[:days]*86400-upt[:hours]*3600-upt[:minutes]*60
  upt
end