Skip to content

Instantly share code, notes, and snippets.

@agile
Created September 14, 2015 15:04
Show Gist options
  • Save agile/e6310b0712334f028c06 to your computer and use it in GitHub Desktop.
Save agile/e6310b0712334f028c06 to your computer and use it in GitHub Desktop.
import java.lang.management.*;
def threadBean = ManagementFactory.getThreadMXBean();
def osBean = ManagementFactory.getOperatingSystemMXBean();
println "\n\n\n[Checking state of (master)]";
println "Current CPU Time used by Jenkins: " + threadBean.getCurrentThreadCpuTime() + "ns";
double processLoad = (osBean.getProcessCpuLoad() * 100).round(2);
double cpuLoad = (osBean.getSystemCpuLoad() * 100).round(2);
println "Process CPU Load: " + processLoad + "%";
println "CPU Load: " + cpuLoad + "%";
if (processLoad < 90) {
println "\n\n\n === Load is less than 90%, nothing to do ===\n\n\n";
println "\n\n\n[Done checking: CPU Load: " + cpuLoad + "%]\n\n\n";
return;
} else {
println "\n\n\n === Load is more than 90%, checking for stuck threads! ===\n\n\n";
}
println "\n\n\n[Checking all threads]\n\n\n";
def threadNum = 0;
def killThreadNum = 0;
def stacktraces = Thread.getAllStackTraces();
stacktraces.each { thread, stack ->
if (thread.getName().contains("trigger/TimerTrigger/check") ) {
println "=== Interrupting thread " + thread.getName()+ " ===";
thread.interrupt();
killThreadNum++;
}
threadNum++;
}
println "\n\n\n[Done checking: " + threadNum + " threads, killed " + killThreadNum + "]\n\n\n";
return; // Suppress groovy state dump
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment