Module: HelperClasses::System
Instance Attribute Summary
Attributes included from DPuts
#log_file, #logall_file, #max_msg_len, #mutex, #show_time, #silent, #terminal_width
Instance Method Summary collapse
- #exists?(cmd) ⇒ Boolean
- #iptables(*args, check: false) ⇒ Object
-
#ntpd_wait(n = 60) ⇒ Object
Waits for NTP to be synchronized or for n seconds.
-
#ntpdoffset ⇒ Object
Returns the offset of the ntpd-synchronization: nil -> not synchronized else -> synchronization in ms.
-
#ntpdstratum ⇒ Object
Returns the stratum of the ntpd-synchronization: 16 -> not synchronized 2..15 -> synchronized.
- #rescue_all(msg = 'Error') ⇒ Object
- #run_bool(cmd) ⇒ Object
- #run_str(cmd) ⇒ Object
Methods included from DPuts
ddputs, dlog_msg, dp, dputs, dputs_func, dputs_getcaller, dputs_out, dputs_unfunc, dputs_write, log_msg, logfile_valid
Instance Method Details
#exists?(cmd) ⇒ Boolean
20 21 22 23 |
# File 'lib/helper_classes/system.rb', line 20 def exists?(cmd) dputs(3) { "Exist command --#{cmd}--?" } run_bool("which #{cmd} > /dev/null 2>&1") end |
#iptables(*args, check: false) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/helper_classes/system.rb', line 37 def iptables(*args, check: false) if !@iptables_cmd if System.exists?('iptables') @iptables_cmd = 'iptables' @iptables_wait = (System.run_str('iptables --help') =~ /\s-w\s/) ? '-w' : '' else @iptables_cmd = '' end end if @iptables_cmd != '' if check System.run_bool("#{@iptables_cmd} #{@iptables_wait} #{args.join(' ')} 2>/dev/null") else System.run_str("#{@iptables_cmd} #{@iptables_wait} #{args.join(' ')}") end else return '' end end |
#ntpd_wait(n = 60) ⇒ Object
Waits for NTP to be synchronized or for n seconds
69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/helper_classes/system.rb', line 69 def ntpd_wait(n = 60) Thread.new { System.rescue_all do (1..n).each { break if System.ntpdoffset sleep 1 } yield end } end |
#ntpdoffset ⇒ Object
Returns the offset of the ntpd-synchronization: nil -> not synchronized else -> synchronization in ms
84 85 86 87 88 89 90 |
# File 'lib/helper_classes/system.rb', line 84 def ntpdoffset ret = System.run_str('ntpq -c "rv 0 stratum,offset"') return nil if ret =~ /connection refused/i stratum, offset = ret.split(/, /).collect { |s| s.gsub(/.*=/, '') } #return nil if stratum == '16' return offset.to_f end |
#ntpdstratum ⇒ Object
Returns the stratum of the ntpd-synchronization: 16 -> not synchronized 2..15 -> synchronized
61 62 63 64 65 66 |
# File 'lib/helper_classes/system.rb', line 61 def ntpdstratum ret = System.run_str('ntpq -c "rv 0 stratum"') return 16 if ret =~ /connection refused/i stratum = ret.gsub(/.*=/, '') return stratum.to_i end |
#rescue_all(msg = 'Error') ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/helper_classes/system.rb', line 25 def rescue_all(msg = 'Error') begin yield rescue StandardError => e dputs(0) { "#{Time.now.strftime('%a %y.%m.%d-%H:%M:%S')} - #{msg}" } dputs(0) { "#{e.inspect}" } dputs(0) { "#{e.to_s}" } e.backtrace.each { |l| dputs(0) { l } } return false end end |
#run_bool(cmd) ⇒ Object
15 16 17 18 |
# File 'lib/helper_classes/system.rb', line 15 def run_bool(cmd) dputs(3) { "Running command --#{cmd}--" } Kernel.system("#{cmd} > /dev/null 2>&1") end |
#run_str(cmd) ⇒ Object
10 11 12 13 |
# File 'lib/helper_classes/system.rb', line 10 def run_str(cmd) dputs(3) { "Running command --#{cmd}--" } %x[ #{cmd} ] end |