Module: Usagewatch

Defined in:
lib/usagewatch_ext/mac.rb,
lib/usagewatch_ext/linux.rb

Class Method Summary collapse

Class Method Details

.uw_bandrxObject



113
114
115
116
117
118
# File 'lib/usagewatch_ext/mac.rb', line 113

def self.uw_bandrx
  read1 =`netstat -ib | grep -e "en1" -m 1 | awk '{print $7}'`
  sleep 1
  read2=`netstat -ib | grep -e "en1" -m 1 | awk '{print $7}'`
  (((read2.to_f - read1.to_f)/1024)/1024).round(3)
end

.uw_bandtxObject



120
121
122
123
124
125
# File 'lib/usagewatch_ext/mac.rb', line 120

def self.uw_bandtx
  send1=`netstat -ib | grep -e "en1" -m 1 | awk '{print $10}'`
  sleep 1
  send2=`netstat -ib | grep -e "en1" -m 1 | awk '{print $10}'`
  (((send2.to_f - send1.to_f)/1024)/1024).round(3)
end

.uw_cputopObject

return hash of top ten proccesses by cpu consumption example [[“apache2”, 12.0], [“passenger”, 13.2]]



75
76
77
# File 'lib/usagewatch_ext/mac.rb', line 75

def self.uw_cputop
  top %w"$11 $3"
end

.uw_cpuusedObject

Show the percentage of cpu used



67
68
69
70
71
# File 'lib/usagewatch_ext/mac.rb', line 67

def self.uw_cpuused
  top = `top -l1 | awk '/CPU usage/'`
  top = top.gsub(/[\,a-zA-Z:]/, "").split(" ")
  top[0].to_f
end

.uw_diskavailableObject

Show disk space available in GB



30
31
32
33
34
35
36
37
38
39
# File 'lib/usagewatch_ext/mac.rb', line 30

def self.uw_diskavailable
  df = `df`
  parts = df.split(" ").map { |s| s.to_i }
  sum = 0
  for i in (9..parts.size - 1).step(6) do
    sum += parts[i+1]
  end
  round = sum.round(2)
  totaldiskavailable = ((round/1024)/1024).round(2)
end

.uw_diskavailable_on(location) ⇒ Object

Show disk space available on location(partition) in GB



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/usagewatch_ext/mac.rb', line 42

def self.uw_diskavailable_on(location)
  df = `df`
  df.split("\n")[1..-1].each do |line|
    parts = line.split(" ")
    if parts.last == location
      diskavailableon = ((parts[3].to_i.round(2)/1024)/1024).round(2)
      break
    end
  end
  diskavailableon ? diskavailableon : "location invalid"
end

.uw_diskusedObject

Show disk used in GB



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/usagewatch_ext/mac.rb', line 4

def self.uw_diskused
  df = `df -kl`
  sum = 0.00
  df.each_line.with_index do |line, line_index|
    next if line_index.eql? 0
    line = line.split(" ")
    next if line[0] =~ /localhost/  #ignore backup filesystem
    sum += ((line[2].to_f)/1024)/1024
  end
  sum.round(2)
end

.uw_diskused_on(location) ⇒ Object

Show disk space used on location(partition) in GB



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/usagewatch_ext/mac.rb', line 17

def self.uw_diskused_on(location)
  df = `df`
  df.split("\n")[1..-1].each do |line|
    parts = line.split(" ")
    if parts.last == location
      diskusedon = ((parts[2].to_i.round(2)/1024)/1024).round(2)
      break
    end
  end
  diskusedon ? diskusedon : "location invalid"
end

.uw_diskused_percObject

Show the percentage of disk used.



55
56
57
58
59
60
61
62
63
64
# File 'lib/usagewatch_ext/mac.rb', line 55

def self.uw_diskused_perc
  df, total, used  = `df -kl`, 0.0, 0.0
  df.each_line.with_index do |line, line_index|
    line = line.split(" ")
    next if line_index.eql? 0 or line[0] =~ /localhost/ #ignore backup filesystem
    total  += to_gb line[3].to_f
    used   += to_gb line[2].to_f
  end
  ((used/total) * 100).round(2)
end

.uw_httpconnsObject

Show the current http connections on 80 port



138
139
140
# File 'lib/usagewatch_ext/mac.rb', line 138

def self.uw_httpconns
  `netstat -an | grep :80 |wc -l`.to_i
end

.uw_loadObject

Show the average of load in the last minute



103
104
105
106
107
108
109
110
111
# File 'lib/usagewatch_ext/mac.rb', line 103

def self.uw_load
  iostat = `iostat -w1 -c 2 | awk '{print $7}'`
  cpu = 0.0
  iostat.each_line.with_index do |line, line_index|
    next if line_index.eql? 0 or  line_index.eql? 1 or  line_index.eql? 2
    cpu = line.split(" ").last.to_f.round(2)
  end
  cpu
end

.uw_memtopObject

return hash of top ten proccesses by mem consumption example [[“apache2”, 12.0], [“passenger”, 13.2]]



91
92
93
# File 'lib/usagewatch_ext/mac.rb', line 91

def self.uw_memtop
  top %w"$11 $4"
end

.uw_memusedObject

Percentage of mem used



96
97
98
99
100
# File 'lib/usagewatch_ext/mac.rb', line 96

def self.uw_memused
  top = `top -l1 | awk '/PhysMem/'`
  top = top.gsub(/[\.\,a-zA-Z:]/, "").split(" ").reverse
  ((top[1].to_f / (top[0].to_f + top[1].to_f)) * 100).round(2)
end