The CreatorCon Call for Content is officially open! Get started here.

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

Narsing1
Mega Sage

I think instead of this complicated code, use the flow like this

  • Trigger it based on the update to the MID

Narsing1_0-1704739783760.png

  • Create Record Action like this

Narsing1_1-1704739826217.png

 

Thanks,

Narsing

  •  

Sandeep Rajput
Tera Patron
Tera Patron

@BanuMahalakshmi Here is the updated script.

 

Screenshot 2024-01-10 at 11.25.57 PM.png

var gr = new GlideRecord('ecc_agent');
gr.addQuery('statusSTARTSWITHDown');
gr.addEncodedQuery('last_refreshed<javascript&colon;gs.beginningOfCurrentMinute()');
gr.query();
gs.print('row', gr.getRowCount());
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();
    }

}

-O-
Kilo Patron

The encoded query should be:

status=Down^last_refreshedRELATIVELT@hour@ago@6

-O-
Kilo Patron

Also I would change the logic a bit in that if an incident for the MID server exists I would add a work note that it is still down.

Perhaps even make sure that the Status is Open or Work in Progress.

Because and incident could be Resolved which is still active, which means a technician did something to fix it, it thinks the problem is fixed, but in fact it is not.

This would make sure that the incident that is not really fixing a MID server problem stays opened.

 

I would also find a better way to identify which incident is for which MID server.

Short description can be changed for whatever reason by anyone at any time, which would lead to loosing the connection between the incident and the MID server.

Perhaps utilize some hidden field, like Correlation display?

Unless that field is already utilized by some other process.

Best would be an m2m table where incidents are linked to MID servers.