How to Populate CI Location Based on IP Address

spaceyjacey
Kilo Contributor

We would like to populate our CIs based on the ip address. For instance, if the CI's ip address is 10.6.4.16, the ".6" means that CI is physically located in our Washington DC office.

1 ACCEPTED SOLUTION

Paul Hardy
ServiceNow Employee
ServiceNow Employee

Discovery allows you to do this, we already do it for our 10,000 machines across 160+ offices. The locations are associated IP ranges and when discovery "finds" a machine it automatically sets the location of that machine. I suppose if you had a record of your IP address ranges AND an IP addresses set on each machine you could script the updating of the CI based on which range they fell in?


View solution in original post

11 REPLIES 11

We do so for our clients. In my opinion the simplest way.

kumar37
Kilo Contributor

Hi all,

am new to service now.please accept my apologies if there is anything wrong in the code.

i have the similar requirement,based on my understanding i have written the below code. but it is not giving the expected output. can any one please help me on this.

below are the requirement details and written code.

I have a requirement to update the location of the server in CMDB_CI_SERVER table w.r.to discovery schedule location.

We have OOB feature to update the location, but for some servers location is not updating, so we want to customize this.

1)   Discovery schedule has location field with one value, the same should be updated for servers in CMDB_CI_SERVER table w.r.to their IP ranges.

2) For every discovery schedule we have defined some IP ranges .the server which is present in the above tables IP range will fall in the defined range.

I have written below business rule for this .script is executing fine, but location is not populating in the CMDB_CI_SERVER table.

 

Business rule:

 

function location(){

var sysID ='8d7eb7ed4f302e842967ef4f0310c789';  

var serverip = new GlideRecord('cmdb_ci_server');

serverip.addQuery('sys_id',sysID);

serverip.Query();

while(serverip.next()){

var iprange = new GlideRecord('discovery_range_item');

var discsche = new GlideRecord('discovery_schedule');

if( serverip.getValue('ip_address') >= iprange.getValue('start_ip_address')   && serverip.getValue('ip_address') <= iprange.getValue('end_ip_address') ){

    answer = iprange.getValue('schedule');

    gs.log('answer');

    continue;  

  }

  if( answer = discsche.getValue('name')){

      answer2   = discsche.getValue('location');

    continue;  

  }

  if( answer2 != serverip.getValue('location')){

    answer3 = answer2;

  }

    serverip.location= answer3;

serverip.update();

  }

  }

 

 

Thanks,

kumar