Module: SystemInspector::Linux
- Defined in:
- lib/instrumental_tools/system_inspector/linux.rb
Class Method Summary collapse
- .cpu ⇒ Object
- .cpu_file ⇒ Object
- .disk_file ⇒ Object
- .disk_io ⇒ Object
- .disk_storage ⇒ Object
- .filesystem ⇒ Object
- .load_cpu ⇒ Object
- .load_disks ⇒ Object
- .load_file ⇒ Object
- .load_filesystem ⇒ Object
- .load_memory ⇒ Object
- .loadavg ⇒ Object
- .memory ⇒ Object
- .memory_file ⇒ Object
- .mount_file ⇒ Object
- .open_files_file ⇒ Object
Class Method Details
.cpu ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/instrumental_tools/system_inspector/linux.rb', line 34 def self.cpu agg_cpu_stat = File.read(cpu_file).lines.map { |line| line.split }.detect { |values| values.first == "cpu" } output = {} if agg_cpu_stat categories = [:user, :nice, :system, :idle, :iowait] values = agg_cpu_stat.slice(1, 5).map { |v| v.to_f } SystemInspector.memory.store(:cpu_values, values.dup) if previous_values = SystemInspector.memory.retrieve(:cpu_values) index = -1 values.collect! { |value| (previous_values[index += 1] - value).abs } end data = Hash[*categories.zip(values).flatten] total = values.inject { |memo, value| memo + value } if previous_values data.each do |category, value| output["cpu.#{category}"] = value / total * 100 end end output["cpu.in_use"] = 100 - data[:idle] / total * 100 end output end |
.cpu_file ⇒ Object
10 11 12 |
# File 'lib/instrumental_tools/system_inspector/linux.rb', line 10 def self.cpu_file "/proc/stat" end |
.disk_file ⇒ Object
18 19 20 |
# File 'lib/instrumental_tools/system_inspector/linux.rb', line 18 def self.disk_file "/proc/diskstats" end |
.disk_io ⇒ Object
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/instrumental_tools/system_inspector/linux.rb', line 149 def self.disk_io output = {} device_root = "/dev/" mounted_devices = File.read(mount_file).lines.map { |l| l.split.first }.select { |device| device.index(device_root) }.map { |device| File.exists?(device) ? File.realpath(device) : nil }.compact diskstats_lines = File.read(disk_file).lines.map(&:split).select { |values| mounted_devices.include?(File.join(device_root, values[2])) } entries = diskstats_lines.map do |values| entry = {} entry[:time] = Time.now entry[:device] = values[2] entry[:utilization] = values[12].to_f SystemInspector.memory.store("disk_stats_#{entry[:device]}".to_sym, entry) end entries.each do |entry| if previous_entry = SystemInspector.memory.retrieve("disk_stats_#{entry[:device]}".to_sym) time_delta = (entry[:time] - previous_entry[:time]) * 1000 utilization_delta = entry[:utilization] - previous_entry[:utilization] output["disk.#{entry[:device]}.percent_utilization"] = utilization_delta / time_delta * 100 end end output end |
.disk_storage ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/instrumental_tools/system_inspector/linux.rb', line 127 def self.disk_storage output = {} `df -Pka`.lines.each do |line| device, total, used, available, capacity, mount = line.chomp.split if device == "tmpfs" names = ["tmpfs_#{mount.gsub(/[^[:alnum:]]/, "_")}".gsub(/_+/, "_")] elsif device =~ %r{/dev/} names = [File.basename(device)] else next end names << 'root' if mount == '/' names.each do |name| output["disk.#{name}.total_mb"] = total.to_f / 1024 output["disk.#{name}.used_mb"] = used.to_f / 1024 output["disk.#{name}.available_mb"] = available.to_f / 1024 output["disk.#{name}.available_percent"] = available.to_f / total.to_f * 100 end end output end |
.filesystem ⇒ Object
180 181 182 183 184 185 186 187 188 |
# File 'lib/instrumental_tools/system_inspector/linux.rb', line 180 def self.filesystem allocated, unused, max = File.read(open_files_file).split.map(&:to_i) open_files = allocated - unused { 'filesystem.open_files' => open_files, 'filesystem.max_open_files' => max, 'filesystem.open_files_pct_max' => (open_files.to_f / max.to_f) * 100 } end |
.load_cpu ⇒ Object
3 4 5 6 7 8 |
# File 'lib/instrumental_tools/system_inspector/linux.rb', line 3 def self.load_cpu output = { :gauges => {} } output[:gauges].merge!(cpu) output[:gauges].merge!(loadavg) output end |
.load_disks ⇒ Object
116 117 118 119 120 121 122 123 124 125 |
# File 'lib/instrumental_tools/system_inspector/linux.rb', line 116 def self.load_disks output = { :gauges => {} } if SystemInspector.command_present?('df', 'disk storage') output[:gauges].merge!(disk_storage) end if File.exists?(mount_file) && File.exists?(disk_file) output[:gauges].merge!(disk_io) end output end |
.load_file ⇒ Object
14 15 16 |
# File 'lib/instrumental_tools/system_inspector/linux.rb', line 14 def self.load_file "/proc/loadavg" end |
.load_filesystem ⇒ Object
172 173 174 175 176 177 178 |
# File 'lib/instrumental_tools/system_inspector/linux.rb', line 172 def self.load_filesystem output = { :gauges => {} } if File.exists?(open_files_file) output[:gauges].merge!(filesystem) end output end |
.load_memory ⇒ Object
71 72 73 74 75 76 77 |
# File 'lib/instrumental_tools/system_inspector/linux.rb', line 71 def self.load_memory output = { :gauges => {} } if File.exists?(memory_file) output[:gauges].merge!(memory) end output end |
.loadavg ⇒ Object
62 63 64 65 66 67 68 69 |
# File 'lib/instrumental_tools/system_inspector/linux.rb', line 62 def self.loadavg min_1, min_5, min_15 = File.read(load_file).split { 'load.1min' => min_1.to_f, 'load.5min' => min_5.to_f, 'load.15min' => min_15.to_f } end |
.memory ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/instrumental_tools/system_inspector/linux.rb', line 79 def self.memory memory_stats = Hash[File.read(memory_file).lines.map { |line| line.chomp.strip.split(/:\s+/) }.reject { |l| l.size != 2 } ] total = memory_stats["MemTotal"].to_f free = memory_stats["MemFree"].to_f used = total - free buffers = memory_stats["Buffers"].to_f cached = memory_stats["Cached"].to_f swaptotal = memory_stats["SwapTotal"].to_f swapfree = memory_stats["SwapFree"].to_f swapused = swaptotal - swapfree available = memory_stats["MemAvailable"].to_f if memory_stats["MemAvailable"] stats_to_record = { 'memory.used_mb' => used / 1024, 'memory.free_mb' => free / 1024, 'memory.buffers_mb' => buffers / 1024, 'memory.cached_mb' => cached / 1024, 'memory.free_percent' => (free / total) * 100, } if available stats_to_record['memory.available_mb'] = available / 1024 stats_to_record['memory.available_percent'] = (available / total) * 100 end if swaptotal > 0 stats_to_record.merge!({ 'swap.used_mb' => swapused / 1024, 'swap.free_mb' => swapfree / 1024, 'swap.free_percent' => (swapfree / swaptotal) * 100 }) end stats_to_record end |
.memory_file ⇒ Object
30 31 32 |
# File 'lib/instrumental_tools/system_inspector/linux.rb', line 30 def self.memory_file "/proc/meminfo" end |
.mount_file ⇒ Object
22 23 24 |
# File 'lib/instrumental_tools/system_inspector/linux.rb', line 22 def self.mount_file "/proc/mounts" end |
.open_files_file ⇒ Object
26 27 28 |
# File 'lib/instrumental_tools/system_inspector/linux.rb', line 26 def self.open_files_file "/proc/sys/fs/file-nr" end |