forked from kwilczynski/facter-facts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
location.rb
77 lines (64 loc) · 2.15 KB
/
location.rb
1
2
3
4
5
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#
# location.rb
#
require 'facter'
#
# We choose location based on simple network to country look-up were we
# use common ISO country codes as follows ...
#
# Location can be anything: a place, a data centre, etc; same in regards
# to the country.
#
location = {
'10.0.0' => { :country => 'uk', :location => 'cambridge' },
'192.168.0' => { :country => 'uk', :location => 'london' }
}
if Facter.value(:kernel) == 'Linux'
# When we cannot match anything this will stand-out ...
this_country, this_location = 'UNKNOWN', 'UNKNOWN'
# We will store IP address of the machine we run on here ...
this_address = ''
# We work-around an issue in Facter #10278 by forcing locale settings ...
ENV['LANG'] = 'POSIX'
ENV['LC_ALL'] = 'POSIX'
#
# We utilise rely on "cat" for reading values from entries under "/proc".
# This is due to some problems with IO#read in Ruby and reading content of
# the "proc" file system that was reported more than once in the past ...
#
Facter::Util::Resolution.exec('/sbin/ifconfig 2> /dev/null').each_line do |line|
# Remove bloat ...
line.strip!
# Skip new and empty lines ...
next if line.match(/^(\r\n|\n|\s*)$|^$/)
#
# Process output and capture details about IP addresses only. This will
# select only the first matching IP address and in that regards it is very
# similar to how IP address fact in Facter works ...
#
if match = line.match(/inet addr:((?:[0-9]+\.){3}[0-9]+)/)
this_address = match[1]
# We are not interested in local host ...
break unless this_address.match(/^127\./)
else
# Skip irrelevant entries ...
next
end
end
# We only consider first three octets from within the IP address ...
this_address = this_address.split('.').slice(0, 3).join('.')
if location.has_key?(this_address)
this_country = location[this_address][:country]
this_location = location[this_address][:location]
end
Facter.add('country') do
confine :kernel => :linux
setcode { this_country }
end
Facter.add('location') do
confine :kernel => :linux
setcode { this_location }
end
end
# vim: set ts=2 sw=2 et :
# encoding: utf-8