Skip to content

Instantly share code, notes, and snippets.

@ansig
Created August 12, 2018 11:59
Show Gist options
  • Save ansig/c9f2ac8e291d5dcb854d49f691f6c7e8 to your computer and use it in GitHub Desktop.
Save ansig/c9f2ac8e291d5dcb854d49f691f6c7e8 to your computer and use it in GitHub Desktop.
Groovy script to collect data from Lockable Resource and format it to be passed into a Graphite instance.
import org.jenkins.plugins.lockableresources.LockableResources
import org.jenkins.plugins.lockableresources.LockableResource
import java.util.Date
def resourceManager = org.jenkins.plugins.lockableresources.LockableResourcesManager.get()
timestamp = new Date().time / 1000 as Integer
data = [:]
resourceManager.allLabels.each { label ->
addLabel(label)
resourceManager.getResourcesWithLabel(label, [:]).each { resource ->
record(label, resource)
}
}
addLabel('all')
resourceManager.resources.each {
record('all', it)
}
graphiteData = []
data.each { label, entries ->
entries.each { key, value ->
graphiteData.add("jenkins.resources.${label}.${key}.count ${value} ${timestamp}")
}
}
println "graphiteData: ${graphiteData}"
def record(String label, LockableResource resource) {
incrementCount(label, 'total')
def isFree = true
if (resource.isQueued()) {
incrementCount(label, 'queued')
}
if (resource.isLocked()) {
incrementCount(label, 'locked')
isFree = false
}
if (resource.isReserved()) {
incrementCount(label, 'reserved')
isFree = false
}
if (isFree) {
incrementCount(label, 'free')
}
}
def addLabel(String name) {
data[name] = ['total': 0, 'free': 0, 'queued': 0, 'locked': 0, 'reserved': 0]
}
def incrementCount(String name, String key) {
data[name][key] += 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment