Jenkins JSON API - Get Current Running Jobs on an Executor

Jenkins setup:

Jenkins: 2.319.2
OS: Linux - 5.4.0-122-generic
Java: 11.0.14.1 - Eclipse Adoptium (OpenJDK 64-Bit Server VM)

Too many plugins, skipped here…

Hi,

Currently I can query all the agent computers status through https://${jenkins_host}/computer/api/json. For each agent computer, it has a field idle indicating whether there are any jobs running on this node. Here are my questions:

  1. How can I list all the jobs currently running on this node? If one agent computer has multiple executors, we want to list all the jobs on those executors.

computer/api/json?tree=computer[displayName,executors[currentExecutable[*]]]&pretty=true

but you will not get job names this way afaik. It’s just not available you get displayName and fullDisplayName that might contain the job name

Thanks for taking a look. With the above tree filter URL, I was only able to get:


{
"_class": "hudson.model.Hudson"
}

Did I miss anything? Also where can I learn the tree request syntax? (any documentation would be great)

did you forget to include the computer/ in the url?

"_class": "hudson.model.Hudson" you get on the root api https://${jenkins_host}/api/json

So it must be
https://${jenkins_host}/computer/api/json?tree=computer[displayName,executors[currentExecutable[*]]]&pretty=true

There is no magic behind the tree syntax. Think of it as accessing a json object in javascript.
So above reads as
get me the entry computer and from within that displayName and executors, and from executors get me the currentExecutable and from currentExecutable get me everything

Also check the api documentation at .../api, e.g. /computer/api. Use the depth parameter to get more details when not using the treeparameter` but beware that the result can become quite big when your Jenkins is bigger

1 Like

Thanks @mawinter69 a lot for the detailed explanation on the tree query syntax!

Yeah, I did miss the computer/ in the url. I was able to get a list of nodes under computer in the JSON response, but still can’t get anything inside the executors. It’s weird that whether a node is idle or not, the executors array in response is always:

"executors": [
    {}
],

Do you know what could be

how exactly looked your call? By default Jenkins will limit the depth of information to avoid that the answer gets too big.
WIth just computer/api/json the executors are empty, with computer/api/json?depth=1 you already get much more data (most of which you’re not interested in so better use the tree syntax.

1 Like

Thanks @mawinter69! Adding the depth param worked, here is the final url that worked for me:

${JENKINS_HOST}/computer/api/json?depth=1&tree=computer[assignedLabels[name],displayName,executors[*]]&pretty=true

That said, the executor in the response still doesn’t contain the actual job:

{
"currentExecutable": {
"_class": "org.jenkinsci.plugins.workflow.support.steps.ExecutorStepExecution$PlaceholderTask$PlaceholderExecutable"
},
"idle": false,
"likelyStuck": false,
"number": 1,
"progress": 99
}

Do you know how can I get the fullDisplayName, I tried adding it to the tree syntax, but didn’t work. Is it nested in some other objects?

you need one more nested level
${JENKINS_HOST}/computer/api/json?depth=1&tree=computer[assignedLabels[name],displayName,executors[currentExecutable[*]]]&pretty=true

Then you will also not need the depth parameter I think

1 Like

@mawinter69 Amazing, this is exactly what I need! The result shows the current executables on an executors including job url and display name. Thank you very much!

Could you tell me how can I figure out all the attributes available for query? (like what can be queried). It seems the depth param really matters, I tried without the depth param and tree filter, then all the executors are empty. Does the same apply to other Jenkins APIs?

The behaviour of the api is the same everywhere.
Try to append /api to a url in Jenkins to see if there is an api available, not everything has it. There you might see additional information specific to this api endpoint. See also Remote Access API
The most commonly used apis are probably those for jobs .../job/<jobname>/api, the queue (/queue/api) and computers (/computer/api and /computer/<computername>/api)

The xml api (.../api/xml) is very powerful as you can use xpath expression to filter data, though that is hard to use.

1 Like