Need to match IP address to Range using Business Rule
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-28-2018 08:50 AM
I am trying to create a business rule that updates a computer's location based on the IP range it was originally discovered in. I'll have it run whenever the IP of the CI changes. The problem is the first 3 octets of an IP can be the same and they still have different locations. So I have the below code which queries the ranges that have the same first 3 octets but then I need it to pick the range with the next lowest 4th octet compared to the CI's IP.
For example if the CI's IP is 192.168.1.5 and there are ranges for 192.168.1.1/27 and 192.168.1.32/27 I need it to know to pick the 192.168.1.1 range. So I have the variable "last" that grabs the last octet of the CI's IP but I am unsure of how to set it up to compare it with the queries results. Any help is appreciated or ways to do this easier.
var ip = current.ip_address;
var shortened = ip.substring(0,ip.lastIndexOf(".")) + ".";
var last = ip.substring(ip.lastIndexOf(".")+1);
gs.log(last);
var gr = new GlideRecord('discovery_range_item');
gr.addQuery('network_ip','STARTSWITH',shortened);
gr.addQuery('active', true);
gr.query();
while(gr.next()) {
var site = gr.parent;
current.u_discovery_site = site;
}
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-29-2018 04:25 AM
try this...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-29-2018 07:49 AM
When I use this it does produce a result but with multiple rows returned it picks the wrong range. Seems pretty random. Still trying to debug but sometimes it will just give the highest range and sometimes the lowest.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-29-2018 08:17 AM
can you put the statement
gs.log(JSON.stringify(arr), mybizrule);
both before and after the sort method and post the result?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-29-2018 08:46 AM
before is:
[{"last":120,"sys_id":{}},{"last":120,"sys_id":{}}]
[{"last":144,"sys_id":{}},{"last":144,"sys_id":{}},{"last":144,"sys_id":{}},{"last":144,"sys_id":{}},{"last":144,"sys_id":{}},{"last":144,"sys_id":{}}]
[{"last":72,"sys_id":{}},{"last":72,"sys_id":{}},{"last":72,"sys_id":{}},{"last":72,"sys_id":{}},{"last":72,"sys_id":{}},{"last":72,"sys_id":{}},{"last":72,"sys_id":{}}]
[{"last":68,"sys_id":{}},{"last":68,"sys_id":{}},{"last":68,"sys_id":{}}]
[{"last":200,"sys_id":{}},{"last":200,"sys_id":{}},{"last":200,"sys_id":{}},{"last":200,"sys_id":{}}]
[{"last":112,"sys_id":{}},{"last":112,"sys_id":{}},{"last":112,"sys_id":{}},{"last":112,"sys_id":{}},{"last":112,"sys_id":{}},{"last":112,"sys_id":{}},{"last":112,"sys_id":{}},{"last":112,"sys_id":{}}]
[{"last":208,"sys_id":{}},{"last":208,"sys_id":{}},{"last":208,"sys_id":{}},{"last":208,"sys_id":{}},{"last":208,"sys_id":{}},{"last":208,"sys_id":{}},{"last":208,"sys_id":{}},{"last":208,"sys_id":{}},{"last":208,"sys_id":{}}]
[{"last":80,"sys_id":{}}]
after is: [{"last":208,"sys_id":{}},{"last":208,"sys_id":{}},{"last":208,"sys_id":{}},{"last":208,"sys_id":{}},{"last":208,"sys_id":{}},{"last":208,"sys_id":{}},{"last":208,"sys_id":{}},{"last":208,"sys_id":{}},{"last":208,"sys_id":{}}]
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-02-2018 08:48 AM
Any further help on this is appreciated. Still struggling with the logic in this.