Benoit Crestey
ServiceNow Employee
Options
- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
on 07-25-2023 06:12 AM
Use case
- To speed up ACC Agent deployment, some customers will include the ACC agent in an Virtual Machine image, resulting to error to generate new agent_id and server Serial Number.
- You can use the script if you want to be sure that the agents doesn't share same identifiers
- The script can also be used to list all agents that reported to the server
Issue
- At the initial ACC agent deployment :
- ACC agent_id is generated
- Discovery of Serial Number of the server is done
- Then it is stored in a text file : /usr/share/servicenow/agent-client-collector/serial_number.txt
- On UNIX, the goal is to avoid to use commands using privileges to get the Serial Number
- If ACC is installed on a server, but then I new server is created froml a clone, it will copy some files
- The result will be strange behaviour of Agents and related CIs in ServiceNow : the servers will mix, and it could generate some issues with Agent, Server and Virtual Machines errors on statuses
- To avoid that it is necessary to follow the instructions described here : https://docs.servicenow.com/bundle/utah-it-operations-management/page/product/agent-client-collector...
Behaviour of the script
- The script will scan the ECC queue records to look at discovered data
- Timeframe is Today and Yesterday
- If it detect ACC agents with the same agent_it or Serial Number, but different host name, it will display as a duplicate
How to use
- Execute the script as a background script
Result of the Script
Execution and no duplicates found :
*** Script: Duplicates
Full list
59a639818fba550a;rXXXXXXXX04;10.80.80.91;VMware-42 32 e5 a0 b7 db cc 9f-b7 0a cf dd 90 b6 a0 dd;rXXXXXXXX04.prod.local;Red Hat Enterprise Linux Server;7.9.0
3b851818047d1b8d;cXXXXXXXXX1;10.80.80.51;VMware-42 32 1b ee 5f fb cc 28-91 fb 4e dd 82 e1 26 dd;cXXXXXXXXX1.prod.local;Red Hat Enterprise Linux Server;7.9.0
91e243ef65fcb40d;bXXXXXXXXX2;10.80.8.50;VMware-42 3c 36 5c 76 b9 cc 69-93 f7 7f ad dd f8 2d dd;bXXXXXXXXX2.prod.local;Red Hat Enterprise Linux Server;7.9.0
- Here, no duplicate was found, no ACC agents seems to have reported same agent_id or Serial Number.
- It is possible to export the CSV list of agents if needed
Execution with duplicates found :
*** Script: duplicate ID : 59c639818fba550a (3)
59c639818fba550a;rXXXXXXXX04;10.80.80.91;VMware-42 32 e5 a0 b7 db cc 9f-b7 0a cf dd 90 b6 a0 dd;rXXXXXXXX04.prod.local;Red Hat Enterprise Linux Server;7.9.0
59c639818fba550a;cXXXXXXXXX1;10.80.80.121;VMware-42 32 e5 a0 b7 db cc 9f-b7 0a cf dd 90 b6 a0 dd;cXXXXXXXXX1.prod.local;Red Hat Enterprise Linux Server;7.9.0
59c639818fba550a;bXXXXXXXXX2;10.80.80.57;VMware-42 32 e5 a0 b7 db cc 9f-b7 0a cf dd 90 b6 a0 dd;bXXXXXXXXX2.prod.local;Red Hat Enterprise Linux Server;7.9.0
duplicate SN : VMware-42 32 e5 a0 b7 db cc 9f-b7 0a cf dd 90 b6 a0 dd (3)
59c639818fba550a;rXXXXXXXX04;10.80.80.91;VMware-42 32 e5 a0 b7 db cc 9f-b7 0a cf dd 90 b6 a0 dd;rXXXXXXXX04.prod.local;Red Hat Enterprise Linux Server;7.9.0
59c639818fba550a;cXXXXXXXXX1;10.80.80.121;VMware-42 32 e5 a0 b7 db cc 9f-b7 0a cf dd 90 b6 a0 dd;cXXXXXXXXX1.prod.local;Red Hat Enterprise Linux Server;7.9.0
59c639818fba550a;bXXXXXXXXX2;10.80.80.57;VMware-42 32 e5 a0 b7 db cc 9f-b7 0a cf dd 90 b6 a0 dd;bXXXXXXXXX2.prod.local;Red Hat Enterprise Linux Server;7.9.0
Full list
59c639818fba550a;rXXXXXXXX04;10.80.80.91;VMware-42 32 e5 a0 b7 db cc 9f-b7 0a cf dd 90 b6 a0 dd;rXXXXXXXX04.prod.local;Red Hat Enterprise Linux Server;7.9.0
59c639818fba550a;cXXXXXXXXX1;10.80.80.121;VMware-42 32 e5 a0 b7 db cc 9f-b7 0a cf dd 90 b6 a0 dd;cXXXXXXXXX1.prod.local;Red Hat Enterprise Linux Server;7.9.0
59c639818fba550a;bXXXXXXXXX2;10.80.80.57;VMware-42 32 e5 a0 b7 db cc 9f-b7 0a cf dd 90 b6 a0 dd;bXXXXXXXXX2.prod.local;Red Hat Enterprise Linux Server;7.9.0
3b851818047d1b8d;zXXXXXXXXXz;10.80.80.51;VMware-42 32 1b ee 5f fb cc 28-91 fb 4e dd 82 e1 26 dd;zXXXXXXXXXz.prod.local;Red Hat Enterprise Linux Server;7.9.0
91e243ef65fcb40d;aXXXXXXXXX2;10.80.8.50;VMware-42 3c 36 5c 76 b9 cc 69-93 f7 7f ad dd f8 2d dd;aXXXXXXXXX2.prod.local;Red Hat Enterprise Linux Server;7.9.0
Here we can see 3 agents are reporting the same agent_id and Serial Numbers. This will generate some issues :
- Probably there will be only one agent in the sn_agent_cmdb_ci_agent table, but related to one of the 3 servers
- The servers will probably merge in the CMDB
- The Virtual Machines associated to the server will probably have strange statuses (retired) and relationships will be in duplicates
In this scenario, it is needed to reinstall the agents, and possibly to clean the data in the CMDB
Script
helper = {
list: {},
checkDuplicates: function () {
var grECC = new GlideRecord("ecc_queue");
grECC.addEncodedQuery("sys_created_onONYesterday@javascript:gs.beginningOfYesterday()@javascript:gs.endOfYesterday()^ORsys_created_onONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()^name=on_demand_request^queue=input^topic=MonitoringProbe");
//grECC.addEncodedQuery("sys_id=75da68f21b84f550fb9aeb9f7b4bcb63");
//grECC.setLimit(10);
grECC.query();
while (grECC.next()) {
this.parseECCRecord(grECC);
}
//for (var j = 0; j < this.list.length; j++) {
uniqueID = {};
uniqueSN = {};
for (var j in this.list) {
var id = this.list[j].agent_id;
if (uniqueID[id] == undefined) {
uniqueID[id] = 1;
} else {
uniqueID[id]++;
}
var sn = this.list[j].sn;
if (uniqueSN[sn] == undefined) {
uniqueSN[sn] = 1;
} else {
uniqueSN[sn]++;
}
}
var res = "Duplicates\n";
for (var j in uniqueID) {
if (uniqueID[j] > 1) {
res += "duplicate ID : " + j + " (" + uniqueID[j] + ")" + "\n";
for (var k in this.list) {
if (j == this.list[k].agent_id) {
res += this.list[k].agent_id + ";" + this.list[k].name + ";" + this.list[k].ip + ";" + this.list[k].sn + ";" + this.list[k].fqdn + ";" + this.list[k].os + ";" + this.list[k].os_version + "\n";
}
}
}
}
for (var j in uniqueSN) {
if (uniqueSN[j] > 1) {
res += "duplicate SN : " + j + " (" + uniqueSN[j] + ")" + "\n";
for (k in this.list) {
if (j == this.list[k].sn) {
res += this.list[k].agent_id + ";" + this.list[k].name + ";" + this.list[k].ip + ";" + this.list[k].sn + ";" + this.list[k].fqdn + ";" + this.list[k].os + ";" + this.list[k].os_version + "\n";
}
}
}
}
res += "\n" + "Full list" + "\n";
for (j in this.list) {
res += this.list[j].agent_id + ";" + this.list[j].name + ";" + this.list[j].ip + ";" + this.list[j].sn + ";" + this.list[j].fqdn + ";" + this.list[j].os + ";" + this.list[j].os_version + "\n";
}
gs.print(res);
},
parseECCRecord: function(grECC) {
var helperX = new global.XMLHelper(this.getPayload(grECC));
var obj = helperX.toObject();
if (obj != undefined) {
var res = obj['result']['output'];
res = new JSON().decode(res);
for (var i = 0; i < res.length; i++) {
var res2 = this.parseOutput(res[i]);
if (res2 != null) {
this.list[res2.key] = res2;
//gs.print(new JSON().encode(res2));
}
}
} else {
gs.print("error for : " + grECC.getValue("sys_id"));
}
},
parseOutput: function(outJSON) {
var agent_id = outJSON["agent_id"];
var name = null;
var sn = null;
var ip = null;
var fqdn = null;
var os = null;
var os_version = null;
if (outJSON["check"] != undefined) {
var x = new JSON().decode(outJSON["check"]["output"])
for (var i in x) {
if (i == "basic_inventory") {
name = x[i]["name"];
sn = x[i]["serial_number"];
ip = x[i]["ip_address"];
fqdn = x[i]["fully_qualified_domain_name"];
os = x[i]["operating_system"];
os_version = x[i]["operating_system_version"];
}
}
}
if (sn != undefined) {
var key = agent_id + "_" + fqdn + "_" + sn;
return { key: key, agent_id: agent_id, name : name, ip: ip, sn: sn, fqdn: fqdn, os: os, os_version: os_version}
}
return null;
},
getPayload: function(gr) {
var res = gr.getValue("payload");
if (res.startsWith("<see_attachment/>")) {
res = this.getAttachmentContentsAsString("ecc_queue", gr.getValue("sys_id"));
}
return res;
},
getAttachmentContentsAsString: function(tableName, recordID) {
var gsa = new GlideSysAttachment();
var bytesInFile = gsa.getBytes(tableName, recordID);
var dataAsString = Packages.java.lang.String(bytesInFile);
dataAsString = String(dataAsString);
return dataAsString;
}
}
helper.checkDuplicates();
Comments
Jesse Bate
ServiceNow Employee
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
02-22-2024
08:07 PM
There is an error with the encoded query in the script where ':' has been escaped to : , can you please fix the script in your post?
Regards,
Jesse