Module: Kanrisuru::Core::System
- Extended by:
- OsPackage::Define
- Defined in:
- lib/kanrisuru/core/system.rb,
lib/kanrisuru/core/system/types.rb,
lib/kanrisuru/core/system/commands.rb,
lib/kanrisuru/core/system/parsers/w.rb,
lib/kanrisuru/core/system/commands/w.rb,
lib/kanrisuru/core/system/parsers/ps.rb,
lib/kanrisuru/core/system/commands/ps.rb,
lib/kanrisuru/core/system/parsers/last.rb,
lib/kanrisuru/core/system/parsers/lsof.rb,
lib/kanrisuru/core/system/commands/free.rb,
lib/kanrisuru/core/system/commands/kill.rb,
lib/kanrisuru/core/system/commands/last.rb,
lib/kanrisuru/core/system/commands/lsof.rb,
lib/kanrisuru/core/system/parsers/lscpu.rb,
lib/kanrisuru/core/system/commands/lscpu.rb,
lib/kanrisuru/core/system/commands/nproc.rb,
lib/kanrisuru/core/system/parsers/sysctl.rb,
lib/kanrisuru/core/system/parsers/uptime.rb,
lib/kanrisuru/core/system/commands/reboot.rb,
lib/kanrisuru/core/system/commands/sysctl.rb,
lib/kanrisuru/core/system/commands/uptime.rb,
lib/kanrisuru/core/system/parsers/history.rb,
lib/kanrisuru/core/system/commands/history.rb,
lib/kanrisuru/core/system/parsers/load_env.rb,
lib/kanrisuru/core/system/commands/cpu_info.rb,
lib/kanrisuru/core/system/commands/load_env.rb,
lib/kanrisuru/core/system/commands/poweroff.rb,
lib/kanrisuru/core/system/parsers/load_average.rb,
lib/kanrisuru/core/system/commands/load_average.rb,
lib/kanrisuru/core/system/parsers/kernel_statistics.rb,
lib/kanrisuru/core/system/commands/kernel_statistics.rb
Defined Under Namespace
Modules: Parser Classes: CPUArchitecture, CPUArchitectureVulnerability, KernelStatistic, KernelStatisticCpu, LoginUser, OpenFile, ProcessInfo, SessionDetail, Uptime, UserLoggedIn
Instance Method Summary collapse
- #cpu_info(spec) ⇒ Object
- #free(type) ⇒ Object
- #history(opts = {}) ⇒ Object
- #kernel_statistics ⇒ Object
- #kill(signal, pids) ⇒ Object
- #kstat ⇒ Object
- #last(opts = {}) ⇒ Object
- #load_average ⇒ Object
- #load_env ⇒ Object
- #lscpu ⇒ Object
- #lsof(_opts = {}) ⇒ Object
- #nproc(opts = {}) ⇒ Object
- #poweroff(opts = {}) ⇒ Object
- #ps(opts = {}) ⇒ Object
- #reboot(opts = {}) ⇒ Object
- #sysctl(variable = nil) ⇒ Object
- #uptime ⇒ Object
- #w(opts = {}) ⇒ Object
- #who(opts = {}) ⇒ Object
Methods included from OsPackage::Define
Instance Method Details
#cpu_info(spec) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/kanrisuru/core/system/commands/cpu_info.rb', line 6 def cpu_info(spec) Kanrisuru.logger.info do 'DEPRECATION WARNING: cpu_info will be removed in the upcoming major release. Use lscpu instead.' end name = case spec when 'sockets' '^Socket' when 'cores_per_socket' '^Core' when 'threads' '^Thread' when 'cores' '^CPU(' else return end command = Kanrisuru::Command.new("lscpu | grep -i '#{name}' | awk '{print $NF}'") execute(command) Kanrisuru::Result.new(command, &:to_i) end |
#free(type) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/kanrisuru/core/system/commands/free.rb', line 6 def free(type) conversions = { total: 'MemTotal', free: 'MemFree', swap: 'SwapTotal', swap_free: 'SwapFree' } option = conversions[type.to_sym] raise ArgumentError, 'Invalid mem type' unless option command = Kanrisuru::Command.new("cat /proc/meminfo | grep -i '^#{option}' | awk '{print $2}'") execute(command) ## In kB Kanrisuru::Result.new(command, &:to_i) end |
#history(opts = {}) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/kanrisuru/core/system/commands/history.rb', line 6 def history(opts = {}) histfile = opts[:histfile] || '~/.bash_history' command = Kanrisuru::Command.new("HISTFILE=#{histfile}; history -r; history") if Kanrisuru::Util.present?(opts[:delete]) command.append_arg('-d', Kanrisuru::Util.array_join_string(opts[:delete], ' ')) elsif Kanrisuru::Util.present?(opts[:clear]) command.append_flag('-c') elsif Kanrisuru::Util.present?(opts[:n]) command << opts[:n] end execute_shell(command) Kanrisuru::Result.new(command) do |cmd| return if Kanrisuru::Util.present?(opts[:delete]) || Kanrisuru::Util.present?(opts[:clear]) Parser::History.parse(cmd) end end |
#kernel_statistics ⇒ Object
10 11 12 13 14 15 16 17 18 |
# File 'lib/kanrisuru/core/system/commands/kernel_statistics.rb', line 10 def kernel_statistics command = Kanrisuru::Command.new('cat /proc/stat') execute_shell(command) Kanrisuru::Result.new(command) do |cmd| Parser::KernelStatistics.parse(cmd) end end |
#kill(signal, pids) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/kanrisuru/core/system/commands/kill.rb', line 6 def kill(signal, pids) raise ArgumentError, 'Invalid signal' unless Kanrisuru::Util::Signal.valid?(signal) ## Use named signals for readabilitiy signal = Kanrisuru::Util::Signal[signal] if signal.instance_of?(Integer) command = Kanrisuru::Command.new('kill') command << "-#{signal}" command.append_array(pids) execute_shell(command) Kanrisuru::Result.new(command) end |
#kstat ⇒ Object
6 7 8 |
# File 'lib/kanrisuru/core/system/commands/kernel_statistics.rb', line 6 def kstat kernel_statistics end |
#last(opts = {}) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/kanrisuru/core/system/commands/last.rb', line 6 def last(opts = {}) command = if opts[:failed_attempts] Kanrisuru::Command.new('lastb') else Kanrisuru::Command.new('last') end command.append_flag('-i') command.append_flag('-F') command.append_arg('-f', opts[:file]) ## Some systems only use 1 space between user and TTY field ## Add an additional space in output formatting for simple parsing ## logic. command | "sed 's/ / /'" execute_shell(command) Kanrisuru::Result.new(command) do |cmd| Parser::Last.parse(cmd, opts) end end |
#load_average ⇒ Object
6 7 8 9 10 11 12 13 |
# File 'lib/kanrisuru/core/system/commands/load_average.rb', line 6 def load_average command = Kanrisuru::Command.new("cat /proc/loadavg | awk '{print $1,$2,$3}'") execute(command) Kanrisuru::Result.new(command) do |cmd| Parser::LoadAverage.parse(cmd) end end |
#load_env ⇒ Object
6 7 8 9 10 11 12 13 |
# File 'lib/kanrisuru/core/system/commands/load_env.rb', line 6 def load_env command = Kanrisuru::Command.new('env') execute_shell(command) Kanrisuru::Result.new(command) do |cmd| Parser::LoadEnv.parse(cmd) end end |
#lscpu ⇒ Object
6 7 8 9 10 11 12 13 14 |
# File 'lib/kanrisuru/core/system/commands/lscpu.rb', line 6 def lscpu command = Kanrisuru::Command.new('lscpu') execute_shell(command) Kanrisuru::Result.new(command) do |cmd| Parser::Lscpu.parse(cmd) end end |
#lsof(_opts = {}) ⇒ Object
6 7 8 9 10 11 12 13 |
# File 'lib/kanrisuru/core/system/commands/lsof.rb', line 6 def lsof(_opts = {}) command = Kanrisuru::Command.new('lsof -F pcuftDsin') execute_shell(command) Kanrisuru::Result.new(command) do |cmd| Parser::Lsof.parse(cmd) end end |
#nproc(opts = {}) ⇒ Object
6 7 8 9 10 11 12 |
# File 'lib/kanrisuru/core/system/commands/nproc.rb', line 6 def nproc(opts = {}) command = Kanrisuru::Command.new('nproc') command.append_flag('--all', opts[:all]) execute_shell(command) Kanrisuru::Result.new(command, &:to_i) end |
#poweroff(opts = {}) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/kanrisuru/core/system/commands/poweroff.rb', line 6 def poweroff(opts = {}) time = opts[:time] cancel = opts[:cancel] = opts[:message] no_wall = opts[:no_wall] command = Kanrisuru::Command.new('shutdown') if Kanrisuru::Util.present?(cancel) command.append_flag('-c') command << if else time = format_shutdown_time(time) if Kanrisuru::Util.present?(time) if Kanrisuru::Util.present?(no_wall) command.append_flag('--no-wall') else command << time if time command << if end end begin execute_shell(command) Kanrisuru::Result.new(command) rescue IOError ## When powering off 'now', ssh io stream closes ## Set exit status to 0 command.handle_status(0) Kanrisuru::Result.new(command) end end |
#ps(opts = {}) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/kanrisuru/core/system/commands/ps.rb', line 6 def ps(opts = {}) group = opts[:group] user = opts[:user] pid = opts[:pid] ppid = opts[:ppid] # tree = opts[:tree] command = Kanrisuru::Command.new('ps ww') if !user.nil? || !group.nil? || !pid.nil? || !ppid.nil? command.append_arg('--user', user.instance_of?(Array) ? user.join(',') : user) command.append_arg('--group', group.instance_of?(Array) ? group.join(',') : group) command.append_arg('--pid', pid.instance_of?(Array) ? pid.join(',') : pid) command.append_arg('--ppid', ppid.instance_of?(Array) ? ppid.join(',') : ppid) else command.append_flag('ax') end command.append_arg('-o', 'uid,user,gid,group,ppid,pid,pcpu,pmem,stat,pri,flags,policy,time,cmd') command.append_flag('--no-headers') execute(command) Kanrisuru::Result.new(command) do |cmd| Parser::Ps.parse(cmd) end end |
#reboot(opts = {}) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/kanrisuru/core/system/commands/reboot.rb', line 6 def reboot(opts = {}) time = opts[:time] cancel = opts[:cancel] = opts[:message] no_wall = opts[:no_wall] command = Kanrisuru::Command.new('shutdown') if Kanrisuru::Util.present?(cancel) command.append_flag('-c') command << if else command.append_flag('-r') time = format_shutdown_time(time) if Kanrisuru::Util.present?(time) if Kanrisuru::Util.present?(no_wall) command.append_flag('--no-wall') else command << time if time command << if end end begin execute_shell(command) Kanrisuru::Result.new(command) rescue IOError ## When rebooting 'now', ssh io stream closes ## Set exit status to 0 command.handle_status(0) Kanrisuru::Result.new(command) end end |
#sysctl(variable = nil) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/kanrisuru/core/system/commands/sysctl.rb', line 6 def sysctl(variable = nil) command = Kanrisuru::Command.new('sysctl') ## Some older versions have a bug with 255 exit code being returned command.append_valid_exit_code(255) if Kanrisuru::Util.present?(variable) command << variable else command.append_flag('--all') end execute_shell(command) Kanrisuru::Result.new(command) do |cmd| Parser::Sysctl.parse(cmd) end end |
#uptime ⇒ Object
6 7 8 9 10 11 12 13 14 15 |
# File 'lib/kanrisuru/core/system/commands/uptime.rb', line 6 def uptime ## Ported from https://github.com/djberg96/sys-uptime command = Kanrisuru::Command.new('cat /proc/uptime') execute_shell(command) Kanrisuru::Result.new(command) do |cmd| Parser::Uptime.parse(cmd) end end |
#w(opts = {}) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/kanrisuru/core/system/commands/w.rb', line 10 def w(opts = {}) users = opts[:users] command = Kanrisuru::Command.new('w -hi') command << users if Kanrisuru::Util.present?(users) execute(command) Kanrisuru::Result.new(command) do |cmd| Parser::W.parse(cmd) end end |
#who(opts = {}) ⇒ Object
6 7 8 |
# File 'lib/kanrisuru/core/system/commands/w.rb', line 6 def who(opts = {}) w(opts) end |