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) ⇒ 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) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/helper_classes/system.rb', line 37 def iptables(*args) 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 != '' System.run_str("#{@iptables_cmd} #{@iptables_wait} #{args.join(' ')}") else return '' end end |
#ntpd_wait(n = 60) ⇒ Object
Waits for NTP to be synchronized or for n seconds
65 66 67 68 69 70 71 72 73 |
# File 'lib/helper_classes/system.rb', line 65 def ntpd_wait(n = 60) Thread.new { (1..n).each { break if System.ntpdstratum < 16 sleep 1 } yield } end |
#ntpdoffset ⇒ Object
Returns the offset of the ntpd-synchronization: nil -> not synchronized else -> synchronization in ms
78 79 80 81 82 83 84 |
# File 'lib/helper_classes/system.rb', line 78 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 end |
#ntpdstratum ⇒ Object
Returns the stratum of the ntpd-synchronization: 16 -> not synchronized 2..15 -> synchronized
57 58 59 60 61 62 |
# File 'lib/helper_classes/system.rb', line 57 def ntpdstratum ret = System.run_str('ntpq -c "rv 0 stratum"') return 16 if ret =~ /connection refused/i stratum = ret.gsub(/.*=/, '') return stratum 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 |