Skip to content

Instantly share code, notes, and snippets.

@okram999
Created September 7, 2016 19:19
Show Gist options
  • Save okram999/ecd9b1b177cfe4a09c8ab071e0aae3ff to your computer and use it in GitHub Desktop.
Save okram999/ecd9b1b177cfe4a09c8ab071e0aae3ff to your computer and use it in GitHub Desktop.
import hudson.model.*
def jobsPerView(viewName)
{
installView = jenkins.getView(viewName)
failedJobs_StatsJobs = installView.items.findAll{job -> job.isBuildable() && job.lastCompletedBuild && job.lastCompletedBuild.result == hudson.model.Result.FAILURE }
unstableJobs_StatsJobs = installView.items.findAll{job -> job.isBuildable() && job.lastCompletedBuild && job.lastCompletedBuild.result == hudson.model.Result.UNSTABLE }
}
def printHeading(text, color)
{
println "<H2 style=\"color:"+ color + ";\">" + text + "</H2>"
}
def getlink(url, text)
{
return "<a href=\"" + url + "\">" + text + "</A> "
}
def offlineCause( cause )
{
if(cause != null)
{
if(cause instanceof hudson.node_monitors.DiskSpaceMonitorDescriptor$DiskSpace)
{
return " Diskspace to low. Size left: " + sizeLeft(cause.toString()) + " Gb"
}
return cause.toString()
}
return ""
}
def sizeLeft(spaceInString)
{
space = Long.parseLong(spaceInString)
space/=1024L; // convert to KB
space/=1024L; // convert to MB
return new BigDecimal(space).scaleByPowerOfTen(-3).toPlainString();
}
node('master')
{
jenkins = hudson.model.Hudson.instance
rootUrl = jenkins.rootUrl
nodes = jenkins.nodes
offlineNodes = nodes.findAll{slave -> (slave.toComputer().isOffline())}
if(offlineNodes.size > 0)
{
printHeading("Offline slaves", "red")
offlineNodes.each{ node ->
println( getlink(rootUrl + node.toComputer().url, node.getDisplayName()) + offlineCause(node.toComputer().offlineCause) + "<BR>")
}
}
else
{
printHeading("All nodes online", "green")
}
installView = jenkins.getView("Jobs_Stats")
failedInstallJobs = installView.items.findAll{job -> job.isBuildable() && job.lastCompletedBuild && (job.lastCompletedBuild.result == hudson.model.Result.FAILURE
|| job.lastCompletedBuild.result == hudson.model.Result.UNSTABLE) }
if(failedInstallJobs.size > 0)
{
printHeading("Failed (un)install jobs", "red")
failedInstallJobs.each{ job ->
println( getlink(rootUrl + job.url, job.getDisplayName()) + " " + job.lastCompletedBuild.description+ "<BR>")
}
}
else
{
printHeading("All (un)install jobs ok", "green")
}
jobsPerView("Jobs_Stats")
if(failedJobs_StatsJobs.size > 0)
{
printHeading("Failed " + viewName + " jobs", "red")
failedJobs_StatsJobs.each{ job ->
println( getlink(rootUrl + job.url, job.getDisplayName()) + "<BR>")
}
}
if(unstableJobs_StatsJobs.size > 0)
{
printHeading("Unstable " + viewName + " jobs", "orange")
unstableJobs_StatsJobs.each{ job ->
println( getlink(rootUrl + job.url, job.getDisplayName()) + "<BR>")
}
}
if(failedJobs_StatsJobs.size == 0 && unstableJobs_StatsJobs == 0)
{
printHeading("All " + viewName + " jobs OK", "green")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment