Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

create incident when mid server fails

BanuMahalakshmi
Tera Contributor

Hi,

 

Please let me know correction in the script, I want to Run a scheduled job every 1 hour to check mid server status.

If any of the mid servers has been stopped for 6 hours, open incident.
Open one incident per mid server when down (if open incident, do not open another).

 

gr = new GlideRecord('ecc_agent');
gr.addQuery('statusSTARTSWITHDown');
gr.addQuery('last_refreshed', '<', gs.nowDateTime());
gr.query();
while (gr.next()) {
createIncident();
}


function createIncident() {
var inc = new GlideRecord('incident');
inc.addEncodedQuery("active=true^sys_created_onONToday@javascript&colon;gs.beginningOfToday()@javascript&colon;gs.endOfToday()^short_descriptionSTARTSWITHMid Server status is Down on");
inc.query();
if (!(inc.hasNext())) {
var incGr = new GlideRecord("incident");
incGr.initialize();
incGr.state = 1;
incGr.urgency = 2;
incGr.contact_type = 'self-service';
incGr.caller_id = "6816f79cc0a8016401c5a33be04be441";
incGr.short_description = "Mid Server status is Down on " + gs.nowDateTime() + ".";
incGr.insert();
}
}

1 ACCEPTED SOLUTION

AnveshKumar M
Tera Sage
Tera Sage

Hi @BanuMahalakshmi ,

You can utilize the following script to create Incidents for the Individual MID Servers whenever they are down for last 6 hours and more.

 

var eccGr = new GlideRecord('ecc_agent');
eccGr.addEncodedQuery('status=Down^stoppedRELATIVELT@hour@ago@6');
eccGr.query();
while (eccGr.next()) {
	var mid_server_name = eccGr.getValue('name');
	var stopped_at = eccGr.getValue('stopped');
	createIncident(mid_server_name, stopped_at);
}


function createIncident(mid_server_name, stopped_at) {
	var short_description = "Mid Server: " + mid_server_name + " status is Down since " + stopped_at;
	
	var incGr = new GlideRecord('incident');
	incGr.addEncodedQuery("short_descriptionSTARTSWITHMid Server: " + mid_server_name + " status is Down since^stateNOT IN6,7,8^active=true");
	incGr.query();
	
	if (incGr._next()) {
		incGr.work_notes = "This MID Server is still down";
		incGr.update();
	} else {
		incGr.initialize();
		incGr.state = 1;
		incGr.impact = 2;
		incGr.urgency = 2;
		incGr.contact_type = 'self-service';
		incGr.caller_id = "6816f79cc0a8016401c5a33be04be441";
		incGr.short_description = short_description;
		incGr.insert();
	}
}

 

If you want to auto resolve the incidents, if they are back online use the following script.

 

var eccGr = new GlideRecord('ecc_agent');
eccGr.addEncodedQuery('status=Down^stoppedRELATIVELT@hour@ago@6');
eccGr.query();
while (eccGr.next()) {
	var mid_server_name = eccGr.getValue('name');
	var stopped_at = eccGr.getValue('stopped');
	createIncident(mid_server_name, stopped_at);
}

var eccGrUp = new GlideRecord('ecc_agent');
eccGrUp.addEncodedQuery('status=Up');
eccGrUp.query();
while (eccGrUp.next()) {
	var mid_server_name = eccGrUp.getValue('name');
	var started_at = eccGrUp.getValue('started');
	resolveIncident(mid_server_name, started_at);
}


function createIncident(mid_server_name, stopped_at) {
	var short_description = "Mid Server: " + mid_server_name + " status is Down since " + stopped_at;
	
	var incGr = new GlideRecord('incident');
	incGr.addEncodedQuery("short_descriptionSTARTSWITHMid Server: " + mid_server_name + " status is Down since^stateNOT IN6,7,8^active=true");
	incGr.query();
	
	if (incGr._next()) {
		incGr.work_notes = "This MID Server is still down";
		incGr.update();
	} else {
		incGr.initialize();
		incGr.state = 1;
		incGr.impact = 2;
		incGr.urgency = 2;
		incGr.contact_type = 'self-service';
		incGr.caller_id = "6816f79cc0a8016401c5a33be04be441";
		incGr.short_description = short_description;
		incGr.insert();
	}
}

function resolveIncident(mid_server_name, started_at) {
	
	var incGr = new GlideRecord('incident');
	incGr.addEncodedQuery("short_descriptionSTARTSWITHMid Server: " + mid_server_name + " status is Down since^stateNOT IN6,7,8^active=true");
	incGr.query();
	
	if (incGr._next()) {
		incGr.state = 6;
		incGr.work_notes = "This MID Server is back online at " + started_at;
		incGr.comments = "This MID Server is back online at " + started_at;
		incGr.close_code = "Resolved by caller";
		incGr.close_notes = "This MID Server is back online at " + started_at;
		incGr.update();
	}
}

 

Please mark my answer helpful and accept as a solution if it helped 👍✔️

Thanks,
Anvesh

View solution in original post

6 REPLIES 6

AnveshKumar M
Tera Sage
Tera Sage

Hi @BanuMahalakshmi ,

You can utilize the following script to create Incidents for the Individual MID Servers whenever they are down for last 6 hours and more.

 

var eccGr = new GlideRecord('ecc_agent');
eccGr.addEncodedQuery('status=Down^stoppedRELATIVELT@hour@ago@6');
eccGr.query();
while (eccGr.next()) {
	var mid_server_name = eccGr.getValue('name');
	var stopped_at = eccGr.getValue('stopped');
	createIncident(mid_server_name, stopped_at);
}


function createIncident(mid_server_name, stopped_at) {
	var short_description = "Mid Server: " + mid_server_name + " status is Down since " + stopped_at;
	
	var incGr = new GlideRecord('incident');
	incGr.addEncodedQuery("short_descriptionSTARTSWITHMid Server: " + mid_server_name + " status is Down since^stateNOT IN6,7,8^active=true");
	incGr.query();
	
	if (incGr._next()) {
		incGr.work_notes = "This MID Server is still down";
		incGr.update();
	} else {
		incGr.initialize();
		incGr.state = 1;
		incGr.impact = 2;
		incGr.urgency = 2;
		incGr.contact_type = 'self-service';
		incGr.caller_id = "6816f79cc0a8016401c5a33be04be441";
		incGr.short_description = short_description;
		incGr.insert();
	}
}

 

If you want to auto resolve the incidents, if they are back online use the following script.

 

var eccGr = new GlideRecord('ecc_agent');
eccGr.addEncodedQuery('status=Down^stoppedRELATIVELT@hour@ago@6');
eccGr.query();
while (eccGr.next()) {
	var mid_server_name = eccGr.getValue('name');
	var stopped_at = eccGr.getValue('stopped');
	createIncident(mid_server_name, stopped_at);
}

var eccGrUp = new GlideRecord('ecc_agent');
eccGrUp.addEncodedQuery('status=Up');
eccGrUp.query();
while (eccGrUp.next()) {
	var mid_server_name = eccGrUp.getValue('name');
	var started_at = eccGrUp.getValue('started');
	resolveIncident(mid_server_name, started_at);
}


function createIncident(mid_server_name, stopped_at) {
	var short_description = "Mid Server: " + mid_server_name + " status is Down since " + stopped_at;
	
	var incGr = new GlideRecord('incident');
	incGr.addEncodedQuery("short_descriptionSTARTSWITHMid Server: " + mid_server_name + " status is Down since^stateNOT IN6,7,8^active=true");
	incGr.query();
	
	if (incGr._next()) {
		incGr.work_notes = "This MID Server is still down";
		incGr.update();
	} else {
		incGr.initialize();
		incGr.state = 1;
		incGr.impact = 2;
		incGr.urgency = 2;
		incGr.contact_type = 'self-service';
		incGr.caller_id = "6816f79cc0a8016401c5a33be04be441";
		incGr.short_description = short_description;
		incGr.insert();
	}
}

function resolveIncident(mid_server_name, started_at) {
	
	var incGr = new GlideRecord('incident');
	incGr.addEncodedQuery("short_descriptionSTARTSWITHMid Server: " + mid_server_name + " status is Down since^stateNOT IN6,7,8^active=true");
	incGr.query();
	
	if (incGr._next()) {
		incGr.state = 6;
		incGr.work_notes = "This MID Server is back online at " + started_at;
		incGr.comments = "This MID Server is back online at " + started_at;
		incGr.close_code = "Resolved by caller";
		incGr.close_notes = "This MID Server is back online at " + started_at;
		incGr.update();
	}
}

 

Please mark my answer helpful and accept as a solution if it helped 👍✔️

Thanks,
Anvesh

SunilKumar_P
Giga Sage

Hi @BanuMahalakshmi, Can you try the below script?

 

var grEccAgent = new GlideRecord('ecc_agent');
grEccAgent.addEncodedQuery("status=Down^last_refreshedRELATIVELT@hour@ago@6");
grEccAgent.query();
while (grEccAgent.next()) {
    createIncident();
}


function createIncident() {
    var grInc = new GlideRecord('incident');
    grInc.addEncodedQuery("active=true^sys_created_onONToday@javascript&colon;gs.beginningOfToday()@javascript&colon;gs.endOfToday()^short_descriptionSTARTSWITHMid Server status is Down on");
    grInc.query();
    if (!(grInc.hasNext())) {
        var grInc = new GlideRecord("incident");
        grInc.initialize();
        grInc.state = 1;
        grInc.urgency = 2;
        grInc.contact_type = 'self-service';
        grInc.caller_id = "6816f79cc0a8016401c5a33be04be441";
        grInc.short_description = "Mid Server status is Down on " + gs.nowDateTime() + ".";
        grInc.insert();
    }
}

 

Regards,

Sunil