module LinuxSystemInfo

Constants

VERSION

Public Class Methods

all() click to toggle source
# File lib/LinuxSystemInfo.rb, line 46
def all
  {
    :info       => info,
    :hostname   => hostname,
    :cpu        => { info: cpu, usage: cpu_usage },
    :ram        => memory,
    :storage    => storage,
    :network    => network,
    :connection => connection,
    :video      => video,
    :audio      => audio,
    :usb        => usb
  }
end
audio() click to toggle source
# File lib/components/media.rb, line 15
def audio
  data = Array.new
  audio = `lspci`
  audios = audio.split("\n").grep /Audio/
  audios.each do |audio|
    audio = audio.split(':')
    data.push audio[2].strip
  end
  data
end
connection() click to toggle source
# File lib/components/network.rb, line 3
def connection
  output = Hash.new
  network = `ifconfig`
  network = network.split("\n\n")

  network.each do |data|
    interface = data.scan(/^[a-z]+\d?/).first
    next if interface == 'lo'
    next if data.match(/addr:(\d+\.)+\d+/).to_s.sub('addr:', '').empty?
    output[interface] = {
      :mac       => data.match(/HWaddr [a-z0-9:]+/).to_s.sub('HWaddr ', ''),
      :ip        => data.match(/inet addr:(\d+\.)+\d+/).to_s.sub('inet addr:', ''),
      :mask      => data.match(/Mask:(\d+\.?)+/).to_s.sub('Mask:', ''),
      :broadcast => data.match(/Bcast:(\d+\.?)+/).to_s.sub('Bcast:', ''),
      :ip6       => data.match(/inet6 addr: (([a-z]|\d)+([a-z]|\d)+(:|::)+)+(([a-z]|\d)+(\/)*([a-z]|\d)+)/).to_s.sub('inet6 addr:', '').strip
    }
  end
  output
end
cpu() click to toggle source
# File lib/components/processors.rb, line 3
def cpu
  cpu = `lscpu`
  cpu = cpu.split("\n")

  cpu_detail = `cat /proc/cpuinfo`
  cpu_detail = cpu_detail.split("\n")

  output = {
    :model         => {
      :name   => cpu_detail.grep(/model name/).first.split(':').last,
      :number => cpu.grep(/Model/).first.split.last
    },
    :address_sizes => cpu_detail.grep(/address sizes/).first.split(':').last,
    :architecture  => cpu.grep(/Architecture/).first.split.last,
    :threads       => cpu.grep(/Thread/).first.split.last,
    :cores         => cpu.grep(/Core/).first.split.last,
    :socket        => cpu.grep(/Socket/).first.split.last,
    :family        => cpu.grep(/family/).first.split.last,
    :flags         => cpu_detail.grep(/flags/).first.split(':').last
  }

  (1..50).each do |l|
    find = "L#{l}[a-z]* cache:"
    unless cpu.grep(/#{find}/) == []
      output.merge!({ :"L#{l}" => cpu.grep(/#{find}/).first.split(':').last.strip })
    end
  end

  output
end
cpu_usage() click to toggle source

In percentage

# File lib/components/processors.rb, line 35
def cpu_usage
  status = `mpstat`.split("\n").last.split
  usage = (100 - status[12].to_f).round(2)
  usage = 100 if usage > 100
  usage = 0 if usage < 0

  {
    user: status[3],
    nice: status[4],
    system: status[5],
    iowait: status[6],
    irq: status[7],
    soft: status[8],
    steal: status[9],
    guest: status[10],
    gnice: status[11],
    idle: status[12],
    usage: usage
  }
end
hostname() click to toggle source
# File lib/LinuxSystemInfo.rb, line 42
def hostname
  `hostname`.strip
end
info() click to toggle source
# File lib/LinuxSystemInfo.rb, line 11
def info
  uptime = `uptime`.strip
  uptime = uptime.split(',')

  if uptime[1].match(/\d+ (user|users)/).nil?
    running_time = [ uptime[0], uptime[1] ].join ', '
    users = uptime[2]
  else
    running_time = uptime[0]
    users = uptime[1]
  end

  {
    os: `cat /proc/version`.strip,
    uptime: running_time.strip,
    users: users.strip
  }
end
memory() click to toggle source
# File lib/components/memory.rb, line 3
def memory
  ram = `free -m`
  ram = ram.split("\n")[1].split
  {
    :unit    => 'Megabyte',
    :total   => ram[1],
    :used    => ram[2],
    :free    => ram[3],
    :shared  => ram[4],
    :buffers => ram[5],
    :cached  => ram[6]
  }
end
network() click to toggle source
# File lib/components/network.rb, line 23
def network
  data = Array.new
  connection = `lspci`
  connection = connection.split("\n")
  connections = connection.grep /(Network|Ethernet)/
  connections.each do |connection|
    connection = connection.split(':')
    data.push connection[2].strip
  end
  data
end
processes() click to toggle source
# File lib/components/processors.rb, line 56
def processes
  users = 'root,nobody,syslog'
  procs = `ps u -U '#{users}' -u '#{users}' -N`
  output = Hash.new
  procs = procs.split("\n")
  procs.slice!(0)
  procs.each do |proc|
    proc = proc.split
    output[proc[1]] = {
      user: proc[0],      pid: proc[1],    cpu: proc[2],
      mem: proc[3],      vsz: proc[4],    rss: proc[5],
      tty: proc[6],     stat: proc[7],  start: proc[8],
      time: proc[9],  command: proc[10]
    }
  end
  output
end
storage() click to toggle source
# File lib/components/memory.rb, line 17
def storage
  output = Hash.new
  storage = `df -h`
  storage = storage.split("\n")
  storage.each do |file|
    file = file.split
    next if file[0][0..4] != '/dev/'
    next if file[1].nil?
    output[file[0]] = {
      :size            => file[1],
      :used            => file[2],
      :available       => file[3],
      :used_percentage => file[4],
      :mount           => file[5]
    }
  end
  output
end
to_s() click to toggle source
# File lib/LinuxSystemInfo.rb, line 61
def to_s
  JSON.pretty_generate(all)
end
usb() click to toggle source
# File lib/LinuxSystemInfo.rb, line 30
def usb
  data = Array.new
  usb = `lspci`
  usb = usb.split("\n")
  usbs = usb.grep /USB/
  usbs.each do |usb|
    usb = usb.split(':')
    data.push usb[2].strip
  end
  data
end
video() click to toggle source
# File lib/components/media.rb, line 3
def video
  data = Array.new
  video = `lspci`
  # Graphics Controller
  videos = video.split("\n").grep /(VGA|Graphics Controller)/
  videos.each do |video|
    video = video.split(':')
    data.push video[2].strip
  end
  data
end