I'm trying to think of a clean way to determine the location of machines (mainly, which datacenter they belong to) based on their network settings.
I would like it to be dynamic, and I'm thinking of using special DNS records that would be specific to the DNS server in each datacenter.
For example, you could have:
root@machine1# dig TXT mysite
...
mysite 3600 IN TXT "DC1"
...
root@machine2# dig TXT mysite
...
mysite 3600 IN TXT "DC2"
...
etc.
I know that DNS has a special LOC
record for location, but it takes coordinates, so it doesn't help in my case. Is there a standard way of addressing this issue, another special type of record for it, or some standard entries in TXT
records?
(First, excuse me for not agreeing with your question.) IMHO, apparently the right place for such host configuration information is DHCP. DHCP intended to provide a client with all the information required to use various services available on a subnet.
You can use some standard DHCP option, I find
domain-name
as the closest to your purposes. You can use something like this on you DHCP server:Alternatively (and probably even better), DHCP allows site-specific options, you can define a DHCP option named, say option
site-name
of typetext
(it also requires some numeric option code) as follows:On the DHCP client you define this option code, request the option and bake some
/etc/dhcp/dhclient-enter-hooks
script where you actually use it.EDIT: On recent distros,
dhclient-enter-hooks
no longer called, instead put your script to/etc/NetwokManager/dispatcher.d/
folder., like this:Once DHCP obtains a lease, you'll see all your DHCP variables dumped into the /tmp/dhcp-env.
Maybe you better define all your printer names, apt servers etc. each in a separate DHCP option instead of just identifying datacenter but storing all the relevant config in the client.
If you opt to use DNS anyway, I find your TXT a good option, don't know of anything standard defined for such purpose, except maybe for this:
server-id
option innamed.conf
: The ID the server should report when receiving .... a query of the name ID.SERVER with type TXT, class CHAOS.i.e in your named.conf you define
Sorta intended to identify which DNS server I'm talking to, which is sorta close to what you need.
DNS is a very strange location, if you want to put it into free-text.
From your description I derive that there are automated setup-routines that differ from data center to data center (since they use different primary DNS servers).
What I am doing - and this is my recommendation to you - works independent of the operating system: Set the SNMP-SysLocation during the automated installation and activate SNMPD. If you have multi-homed servers (i.e. many different network connections) you might have a separate administrative network as well. Setup the snmpd to listen in that network using the "AgentAddress" directive in snmpd.conf.
If you want something simpler, just text, like "US/IL/Chicago/1060 W Addison St/floor 1/rack 2", use TXT records. That's what they're for. Script it up using dynamic DNS if necessary. TXT it can be in forward DNS attached to host names, or reverse DNS attached to IP addresses or even whole subnets (which might make the most sense for your case).
What's the numeric structure of your network like? As long as you're not sharing subnets between locations, you could grep a text file that maps a subnet to a data center. Could even make simple script to lookup the name, and then grep the file with the ip address from the lookup
10.11.0.0 Datacenter1
10.12.0.0 Datacenter1
10.13.0.0 Datacenter1
10.51.0.0 Datacenter2
10.52.0.0 Datacenter2
10.53.0.0 Datacenter2
Now you say "mainly" location.... if you want rows, racks, enclosures, slots... that's a different beast where the answer about SNMP-SysLocation would be better.
I personally wouldn't recommend using DNS, as it is a centrally managed service meaning that if a server moves locations, you'd have to update the location information on the DNS server and wouldn't be able to simply control it on the system being moved.
If, on all the systems you're managing you can run SNMP and you know that the SNMP configurations allow you to store a system location (I believe they should, as Nils mentioned), you could run SNMP and have the systems store a location entry there. This would be slightly better in that you can control the server location on the server itself - if it moves, you can change the location easily and if the server is off, you won't be getting outdated information. However, there is the issue that if SNMP is off, you wouldn't know where the server is since you wouldn't be able to query SNMP.
I believe your best choice for managing this type of information properly would be to use a DC asset management system - as to which one, that would be up to you (there are plenty to choose from).
If all I had to work with is DNS I'd probably look at creating an srv record for each dns server to record the location which you could then retrieve, however something like active directory would be a far better solution
Just a theory here, and I'm not a linux guy at all. If this was windows, I would simply setup dynamic DNS update ona single specific NIC that i know would always be plugged into network "x", and disable dynamic updates on the other NIC. Then I would know by the IP as the server moves around where its located.
However, since it looks like you don't have that option, one thing you could look into a pearl scirpt (as an example) that updates a CSV/TXT file or maybe even DNS.
The basic script would be:
IMO what might be better, is to actually have a central server use a script to look for a server by name and figure out its IP and then match it to a datacenter and store that in a CSV. Have it run once a day and create a new CSV daily. So all you'd have to do DNS wise, is simply make sure the A record is correct for the servers that move.
Edit 1:
The other thing that i don't think you've got covered is, how are you planning on handling purging a record after a server moves? Are you going to set an age limit on the record so that its deleted if not updated in "x" days? That would be another advantage to just using a simple centralized CSV. You could even use that centralized CSV to update DNS daily.
I actually set this up in our datacenters thusly:
caveats:
This has proven a very effective method of allowing developers to create identical code that is then treated differently based on the calling location (server). For example, http://someapp:someport/blah/foo.do can be directed to an entirely different IP address (in our case
There's actually and interesting answer on stackoverflow which describes how to do this:
https://stackoverflow.com/questions/2663371/longitude-and-latitude-value-from-ip-address
Basically you can obtain this information by using the following command:
Then parse the output from the response:
Here, this command strips out the details:
Take a look at LLDP. It should be able to tell you what switch/router a specific server is on. From there, you can tell what datacenter it's in (either based on it's name, or you can store some additional metadata about it)