Skip to content

Instantly share code, notes, and snippets.

@tcaddy
Last active March 16, 2018 08:14
Show Gist options
  • Save tcaddy/aa4aec61b55d7ec944fb to your computer and use it in GitHub Desktop.
Save tcaddy/aa4aec61b55d7ec944fb to your computer and use it in GitHub Desktop.
A PhantomJS script to monitor Ellucian Student Planning / Self-Service for unresponsiveness. A PowerShell script to run the PhantomJS script and recycle the app pool, send an email.
/*
Student Planning goes down randomly and we have to recycle the app pool to fix it.
This script will try to login to Student Planning and make an AJAX request. It will
return text output (console.log() output) about whether it is working or not.
NOTE: this script relies on PhatomJS, which you can download here:
http://phantomjs.org/download.html
NOTE: change the values of the host, user, and pass variables. Optionally change the timeout variable.
*/
var proto = 'https://',
host = 'yourserver.yourschool.edu',
protoHost,
user = 'username',
pass = 'password',
timeout = 10000, // in milliseconds
timeoutOccurred = false,
page = require('webpage').create(),
fs = require('fs'),
ua = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.76 Safari/537.36';
protoHost = proto+host;
page.settings.userAgent = ua;
console.log('Loading login page...');
page.open(protoHost+'/',function(status){
if (status !== 'success') {
console.log('error loading Login page!');
phantom.exit();
} else {
console.log('Submitting login form...');
var loggedIn,
action = page.evaluate(function(){
return document.getElementsByTagName("form")[0].action;
}),
token = page.evaluate(function(){
return document.querySelector("form#loginForm input[name=__RequestVerificationToken]").value;
});
page.open(
action,
'post',
"UserName="+user+"&Password="+pass+"&__RequestVerificationToken="+token,
function(status){
if (status !== 'success') {
console.log('error logging in');
phantom.exit();
} else {
loggedIn=page.evaluate(function(){
return document.querySelector("a[href='/Student/Account/LogOff']")!==null;
});
if (loggedIn) {
console.log('Logged in OK');
var link = (page.evaluate(function(){
return document.querySelector("a[href='/Student/Planning']");
}));
if (link===null) {
console.log('Error finding Student Planning anchor tag!');
phantom.exit();
} else {
console.log('Opening Student Planning page...');
page.open(link.href,function(status){
if (status !== 'success') {
console.log('error opening Student Planning page');
phantom.exit();
} else {
console.log("Opening My Progress AJAX...");
var url = protoHost+'/Student/Planning/Programs/GetStudentProgramEvaluations?validate=false';
page.onResourceTimeout = function(request) {
timeoutOccurred = true;
console.log("\n\nTimeout opening My Progress AJAX!\nWe should recycle the app pool and send an email.");
};
page.settings.resourceTimeout = timeout;
page.open(url,{
headers: {
'X-Requested-With': 'XMLHttpRequest',
'__RequestVerificationToken': page.evaluate(function(){return document.querySelector("input[name=__RequestVerificationToken]").value;})
},
},function(status){
if (status !== 'success') {
if (timeoutOccurred) {
console.log("[timeout already occurred] Error loading AJAX.");
} else {
console.log("Error opening My Progress AJAX!\nWe should recycle the app pool and send an email.");
}
phantom.exit();
} else {
console.log("Everything looks OK.");
phantom.exit();
}
});
}
});
}
} else {
console.log('Login error');
phantom.exit();
}
}
}
);
}
});
# This script will call a PhantomJS script
# to test out that Student Planning is responding to AJAX requests.
# If there is a problem with the AJAX request, this script will notify
# via Email and attempt to recycle the app pool.
# NOTE: This script should be on the same machine hosting Student Planning / Self-Service.
# NOTE: This script must be run with administrator priviledges in order to recycle the app pool.
# NOTE: This script is intended to run periodically on the Task Scheduler
# NOTE: change the values of the following variables
# * $appPool
# * $result (use correct path to PhantomJS and the check_student_planning.js file)
# * $to
# * $body (change the value of yourschool.edu domain)
# NOTE: change the -SmtpServer parameter in the Send-MailMessage command
$result = .\phantomjs-2.0.0-windows\bin\phantomjs.exe check_student_planning.js
$lastLine = $result | Select-Object -last 1
$appPool = 'Test_SelfService'
if ($lastLine -eq "Everything looks OK.") {
Write-Host $lastLine
} else {
Write-Host "We should recycle the app pool and send an email to the EAS team"
$appPoolRecycled = $FALSE
Write-Host "Recycling application pool: " $appPool
$commandPath = [Environment]::GetEnvironmentVariable("systemroot") + "\system32\inetsrv\appcmd.exe"
if (Test-Path $commandPath) {
$command = "'"+$commandPath+"' recycle apppool '"+$appPool+"'"
$lastLine = Invoke-Expression "& $command" | Select-Object -last 1
if ($lastLine -eq ('"'+$appPool+'" successfully recycled')) {
$appPoolRecycled = $TRUE
}
} else {
Write-Host "Cannot recycle because appcmd.exe cannot be found at" $commandPath
}
$to = @('SomeRecipient@yourschool.edu')
$subject = "Student Planning - Not Responding"
$body = "The Student Planning site was not responding. An attempt was made to recycle the app pool, which was "
if ($appPoolRecycled) {
$body+= "Successful. If this problem persists, you will continue to get email notifications like this in the short term future."
} else {
$body+= "Unsuccessful! You should RDP to the Student Planning server ("+$env:computername.toLower()+".yourschool.edu) and manually recycle the '"+$appPool+"' application pool."
}
Send-MailMessage -To $to -Subject $subject -From "noreply@highpoint.edu" -Body $body -SmtpServer "smtp-server.yourschool.edu"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment