How to get a list of Jenkins builds that ran at a particular time?
Asked Answered
G

3

7

My current Jenkins has a large number of jobs. Different folders, each with multiple jobs. I recently saw that one Jenkins slave(which is auto-scaled) is sending too many requests to another server at a particular time. However, I am unable to find which builds are run at that particular time without manually checking them. Is there any way to get this information using the API/Groovy script?

Glycogen answered 12/11, 2018 at 4:6 Comment(1)
In my answer you'll find a simple python script which gets this information via Jenkins API. Let me know if this works for you.Cotto
U
3

I wrote a very small bash script to run on the jenkins server to parse through the logfiles.

It is not sufficient to look only at the job start time. A job could have started just before your time window and even ended after your time window; it would still have run within your time window.

#!/bin/bash
start=$1
end=$2

for build_xml in jobs/*/branches/*/builds/*/build.xml
do
        startTime=$(sed -n -e "s/.*<startTime>\(.*\)<\/startTime>.*/\1/p" $build_xml)
        modificationTime=$(date '+%s' -r $build_xml)
        if [[ $modificationTime > $start ]] && [[ $startTime < $end ]]
        then
                echo "START $(date -d @${startTime::-3})   END $(date -d @$modificationTime)   : $build_xml"
        fi
done

usage:

./getBuildsRunningBetween.sh 1565535639 1565582439

would give:

START Sun Aug 11 20:29:00 CEST 2019   END Sun Aug 11 20:30:20 CEST 2019   : jobs/job-name/branches/branch-name/builds/277/build.xml
Upthrust answered 12/8, 2019 at 9:14 Comment(2)
If the jobs/pipelines are not "branched", removing the branches/* from the for loops works well.Vaios
Also need to add quotations around $build_xml in case your job names have spaces. "$build_xml" in lines 7 and 8.Goldoni
C
2

This can be easily achieved with a simple python script and Jenkins JSON REST API.

1.Prerequisites

Python 2.7 or 3.x and python requests library installed:

pip install requests

For python 3.x

pip3 install requests

Also: How to install pip

2.Python script to fetch builds between dates

import requests
from datetime import datetime


jenkins_url = "JENKINS_HOST"
username = "USERNAME"
password = "PASSWORD"
job_name = "JOB_NAME"
stop_date = datetime.strptime('23.11.2018 0:30:00', "%d.%m.%Y %H:%M:%S")        
start_date = datetime.strptime('10.11.2018 18:46:04', "%d.%m.%Y %H:%M:%S") 
request_url = "{0:s}/job/{1:s}/api/json{2:s}".format(
    jenkins_url,
    job_name,
    "?tree=builds[fullDisplayName,id,number,timestamp,url]"
)

response = requests.get(request_url, auth=(username, password)).json()
builds = []

for build in response['builds']:
    build_date = datetime.utcfromtimestamp(build['timestamp']/1000)
    if build_date >= start_date and build_date <= stop_date:
        builds.append(build)
        print("Job name: {0:s}".format(build["fullDisplayName"]))
        print("Build number: {0:d}".format(build["number"]))
        print("Build url: {0:s}".format(build["url"]))
        print("Build timestamp: {0:d}".format(build["timestamp"]))
        print("Build date: {}\n".format(build_date))

Above script works both with python 2.7 and 3.x. Now a little explanation:

First download all builds data by using JSON API and load response as JSON. Then for each build convert its timestamp to date time and compare with start and stop dates. Please note its important to divide timestamp by 1000 to get seconds not milliseconds (otherwise date conversion from timestamp will raise a ValueError).

Example output:

$ python test.py 
Job name: Dummy #21
Build number: 21
Build url: http://localhost:8080/job/Dummy/21/
Build timestamp: 1541875585881
Build date: 2018-11-10 18:46:25

Job name: Dummy #20
Build number: 20
Build url: http://localhost:8080/job/Dummy/20/
Build timestamp: 1541875564250
Build date: 2018-11-10 18:46:04

On the other hand, if you want to provide start and stop dates in a different format then remember you'll need to adjust format parameter it in strptime() function. Python datetime directives.

Few examples:

datetime.strptime("23.11.2018", "%d.%m.%Y")
datetime.strptime("2018.11.23", "%Y.%m.%d")
datetime.strptime("Jun 1 2005  1:33PM", "%b %d %Y %I:%M%p")

3.Python script to fetch build on exact date

If you are interested in finding build by its exact date just replace this line:

if build_date >= start_date and build_date <= stop_date:

with this:

if build_date == date:

where date is particular build date.

Please note! that if you want to find build by exact date you'll need to provide date with the proper format. In this case it's "%d.%m.%Y %H:%M:%S". Example:

datetime.strptime('10.11.2018 18:46:04', "%d.%m.%Y %H:%M:%S")

Otherwise, even though dates will the same to a certain point (minutes, hours, date etc.) for python it won't be equal dates.

If you want to provide another format then you'll need to adjust it for build_date variable.

build_date.strftime('%d %m,%Y')
Cotto answered 27/11, 2018 at 11:20 Comment(1)
This is nice if you know the build, but I believe the OP was asking to find any builds within the time window, since they don't know which build might be making the rogue requests.Biogen
C
1

Running jobs can be found based on color categorization called anime, using the below Jenkins API.

"Jenkins host url"/api/xml?tree=jobs[name,url,color]&xpath=/hudson/job[ends-with(color/text(),%22_anime%22)]&wrapper=jobs

Blue ones are the ones that are running

blue_anime

Chromaticity answered 12/11, 2018 at 4:36 Comment(1)
I dont want currently running builds, but build from a past day. For eg, if i want to see which jobs ran yesterday between 1pm and 2 pm(The time of high load). Can I use the API to do that?Glycogen

© 2022 - 2024 — McMap. All rights reserved.