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:
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.
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
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:
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.
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
@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.