Module: VScripts::Util::LocalSystem

Included in:
Commands::Tags2facts
Defined in:
lib/vscripts/util/local_system.rb

Overview

Local system functions library

Instance Method Summary collapse

Instance Method Details

#checksObject

Gets system checks from environment variables



69
70
71
72
73
# File 'lib/vscripts/util/local_system.rb', line 69

def checks
  config['SystemChecks']
rescue
  {}
end

#ensure_file_content(file, body) ⇒ Object

Ensures the specified file has the specified content



60
61
62
63
64
65
66
# File 'lib/vscripts/util/local_system.rb', line 60

def ensure_file_content(file, body)
  write = write_file(file, body)
  read  = IO.read(file)
  read == body || write
rescue
  write
end

#ensure_file_dir(file) ⇒ Object

Creates the directory for the specified file



44
45
46
47
# File 'lib/vscripts/util/local_system.rb', line 44

def ensure_file_dir(file)
  path = File.dirname(file)
  `mkdir -p #{path}`
end

#external_dnsObject

Tries to get the reverse dns



35
36
37
38
39
40
41
# File 'lib/vscripts/util/local_system.rb', line 35

def external_dns
  ext_ip = `wget -q -O - checkip.dyndns.org \
    | sed -e 's/[^[:digit:]|.]//g'`
  `dig +short -x #{ext_ip}`.strip
rescue
  false
end

#hostname_pathObject

Hostname file path



15
16
17
# File 'lib/vscripts/util/local_system.rb', line 15

def hostname_path
  '/etc/hostname'
end

#hosts_pathObject

Hosts file path



10
11
12
# File 'lib/vscripts/util/local_system.rb', line 10

def hosts_path
  '/etc/hosts'
end

#local_domain_nameObject

Returns the local domain name



30
31
32
# File 'lib/vscripts/util/local_system.rb', line 30

def local_domain_name
  `dnsdomainname`.strip
end

#local_fqdnObject

Returns the current fully qualified domain



20
21
22
# File 'lib/vscripts/util/local_system.rb', line 20

def local_fqdn
  `hostname -f`.strip
end

#local_host_nameObject

Returns the local host name



25
26
27
# File 'lib/vscripts/util/local_system.rb', line 25

def local_host_name
  `hostname`.strip
end

#process_checksObject

Runs each command specified and returns the name and exit status



76
77
78
79
80
81
82
83
# File 'lib/vscripts/util/local_system.rb', line 76

def process_checks
  codes = {}
  checks.each do |name, command|
    system("#{command} > /dev/null 2>&1")
    codes[name] = $CHILD_STATUS.exitstatus
  end
  codes
end

#status_codesObject

Extract the codes for each check



86
87
88
# File 'lib/vscripts/util/local_system.rb', line 86

def status_codes
  process_checks.values.compact
end

#write_file(file, body) ⇒ Object

Writes to file



50
51
52
53
54
55
56
57
# File 'lib/vscripts/util/local_system.rb', line 50

def write_file(file, body)
  ensure_file_dir(file)
  File.open(file, 'w') do |newfile|
    newfile.write(body)
  end
rescue Errno::EACCES
  puts "FATAL: You need to be root in order to write to #{file}"
end