module Sys
Constants
- PS_FIELDS
- PS_FIELD_TABLE
- PS_FIELD_TRANSFORMS
Public Class Methods
Is this BSD?
# File lib/epitools/sys/os.rb, line 61 def self.bsd? os == "BSD" or os == "Darwin" end
A metaprogramming helper that allows you to write platform-specific methods which the user can call with one name. Here's how to use it:
Define these methods:
reboot_linux, reboot_darwin, reboot_windows
Call the magic method:
cross_platform_method(:reboot)
Now the user can execute “reboot” on any platform!
(Note: If you didn't create a method for a specific platform, then you'll get NoMethodError exception when the “reboot” method is called on that platform.)
# File lib/epitools/sys/os.rb, line 81 def self.cross_platform_method(name) platform_method_name = "#{name}_#{os.downcase}" metaclass.instance_eval do define_method(name) do |*args| begin self.send(platform_method_name, *args) rescue NoMethodError raise NotImplementedError.new("#{name} is not yet supported on #{os}.") end end end end
Is this Darwin?
# File lib/epitools/sys/os.rb, line 49 def self.darwin? os == "Darwin" end
# File lib/epitools/sys/net.rb, line 7 def self.hostname_linux `uname -n`.strip end
# File lib/epitools/sys/net.rb, line 11 def self.hostname_mac `uname -n`.strip.gsub(/\.local$/, '') end
# File lib/epitools/sys/net.rb, line 15 def self.hostname_windows raise NotImplementedError end
BSD: Return a hash of (device, IP address) pairs.
eg: {“en0”=>“192.168.1.101”}
# File lib/epitools/sys/net.rb, line 28 def self.interfaces_bsd sections = `ifconfig`.split(/^(?=[^\t])/) sections_with_relevant_ip = sections.select {|i| i =~ /inet/ } device_ips = {} sections_with_relevant_ip.each do |section| device = section[/[^:]+/] ip = section[/inet ([^ ]+)/, 1] device_ips[device] = ip end device_ips end
Darwin: Do whatever BSD does
# File lib/epitools/sys/net.rb, line 45 def self.interfaces_darwin interfaces_bsd end
Linux: Return a hash of (device, IP address) pairs.
eg: {“eth0”=>“192.168.1.101”}
# File lib/epitools/sys/net.rb, line 54 def self.interfaces_linux sections = `/sbin/ifconfig`.split(/^(?=Link encap:Ethernet)/) sections_with_relevant_ip = sections.select {|i| i =~ /inet/ } device_ips = {} sections_with_relevant_ip.each do |section| device = section[/([\w\d]+)\s+Link encap:Ethernet/, 1] ip = section[/inet addr:([^\s]+)/, 1] device_ips[device] = ip end device_ips end
Windows: Return a hash of (device name, IP address) pairs.
# File lib/epitools/sys/net.rb, line 71 def self.interfaces_windows result = {} `ipconfig`.split_before(/^\w.+:/).each do |chunk| chunk.grep(/^Ethernet adapter (.+):\s*$/) do name = $1 chunk.grep(/IPv[46] Address[\.\ ]+: (.+)$/) do address = $1.strip result[name] = address end end end result end
Is this Linux?
# File lib/epitools/sys/os.rb, line 35 def self.linux? os == "Linux" end
Is this a Mac? (aka. Darwin?)
# File lib/epitools/sys/os.rb, line 56 def self.mac?; darwin?; end
# File lib/epitools/sys/mem.rb, line 18 def self.memstat_darwin #$ vm_stat #Mach Virtual Memory Statistics: (page size of 4096 bytes) #Pages free: 198367. #Pages active: 109319. #Pages inactive: 61946. #Pages speculative: 18674. #Pages wired down: 70207. #"Translation faults": 158788687. #Pages copy-on-write: 17206973. #Pages zero filled: 54584525. #Pages reactivated: 8768. #Pageins: 176076. #Pageouts: 3757. #Object cache: 16 hits of 255782 lookups (0% hit rate) #$ iostat raise "Not implemented" end
# File lib/epitools/sys/mem.rb, line 7 def self.memstat_linux #$ free # total used free shared buffers cached #Mem: 4124380 3388548 735832 0 561888 968004 #-/+ buffers/cache: 1858656 2265724 #Swap: 2104504 166724 1937780 #$ vmstat raise "Not implemented" end
Get an array of mounted filesystems (as fancy objects)
# File lib/epitools/sys/mounts.rb, line 6 def self.mounts if linux? IO.popen(["findmnt", "--raw"]) { |io| io.drop(1).map { |line| Mount.new line } } else raise NotImplementedError.new("I dunno, how do you find mounts on #{os}?") end end
Return the current operating system: Darwin, Linux, or Windows.
# File lib/epitools/sys/os.rb, line 6 def self.os return @os if @os require 'rbconfig' if defined? RbConfig host_os = RbConfig::CONFIG['host_os'] else host_os = Config::CONFIG['host_os'] end case host_os when /darwin/ @os = "Darwin" when /bsd/ @os = "BSD" when /linux/ @os = "Linux" when /mingw|mswin|cygwin/ @os = 'Windows' else #raise "Unknown OS: #{host_os.inspect}" end @os end
List all (or specified) processes, and return ProcessInfo
objects. (Takes an optional list of pids as arguments.)
# File lib/epitools/sys/ps.rb, line 9 def self.ps(*pids) raise "that's too many pids!" if pids.size > 999_999 options = PS_FIELDS.join(',') pids = pids.map(&:to_i) if pids.any? command = "ps -p #{pids.join(',')} -o #{options}" else command = "ps awx -o #{options}" end lines = `#{command}`.lines.to_a lines[1..-1].map do |line| fields = line.split if fields.size > PS_FIELDS.size fields = fields[0..PS_FIELDS.size-2] + [fields[PS_FIELDS.size-1..-1].join(" ")] end fields = PS_FIELDS.zip(fields).map { |name, value| value.send(PS_FIELD_TRANSFORMS[name]) } ProcessInfo.new(*fields) end end
# File lib/epitools/sys/misc.rb, line 47 def self.temperatures #/Applications/Utilities/TemperatureMonitor.app/Contents/MacOS/tempmonitor -a -l #CPU Core 1: 28 C #CPU Core 2: 28 C #SMART Disk Hitachi HTS543216L9SA02 (090831FBE200VCGH3D5F): 40 C #SMC CPU A DIODE: 41 C #SMC CPU A HEAT SINK: 42 C #SMC DRIVE BAY 1: 41 C #SMC NORTHBRIDGE POS 1: 46 C #SMC WLAN CARD: 45 C raise NotImplementedError.new("Sorry") end
Trap signals!
usage: trap(“EXIT”, “HUP”, “ETC”, :ignore=>) { |signal| puts “Got #{signal}!” } (Execute Signal.list to see what's available.)
No paramters defaults to all signals except VTALRM, CHLD, CLD, and EXIT.
# File lib/epitools/sys/misc.rb, line 11 def self.trap(*args, &block) options = if args.last.is_a?(Hash) then args.pop else Hash.new end args = [args].flatten signals = if args.any? then args else Signal.list.keys end ignore = %w[ VTALRM CHLD CLD EXIT ] unless ignore = options[:ignore] ignore = [ignore] unless ignore.is_a? Array signals = signals - ignore signals.each do |signal| p [:sig, signal] Signal.trap(signal) { yield signal } end end
# File lib/epitools/sys/ps.rb, line 38 def self.tree tree = Sys.ps.group_by(&:ppid) Hash[tree.map do |ppid, children| kvs = children.map { |child| [child.pid, tree.delete(child.pid)] } [ppid, Hash[kvs]] end] end
Is this Windows?
# File lib/epitools/sys/os.rb, line 42 def self.windows? os == "Windows" end
Public Instance Methods
Darwin: Open the webpage in a new chrome tab.
# File lib/epitools/sys/misc.rb, line 41 def browser_open_darwin(url) system("open", "-a", "chrome", url) end
Linux: Open an URL in the default browser (using “xdg-open”).
# File lib/epitools/sys/misc.rb, line 34 def browser_open_linux(url) system("xdg-open", url) end