简体   繁体   中英

How to read the contents of /timestamps file in jenkins manually

I am trying to read the /timestamps file that gets generated for the build but I am not able to understand the file format as when I try to open it is contains some special characters. I wanted to use this file for a script which will run as a batch command after the build has completed. Any help would be appreciated. Thanks

It is considered bad practice to rely of the internal data-structure of a plugin like this.
Suggest you contact the writer of that plugin and see if he can give some API for that.

If you're using the timestamper plugin https://plugins.jenkins.io/timestamper/

You can read the log using the plugin's API.

http://${JENKINS_IP_PORT}/job/${JOB_NAME}/${JOB_NUMBER}/console

will return the console view with the timestamps if the plugin is enabled or or without it if it's disabled.

http://${JENKINS_IP_PORT}/job/${JOB_NAME}/${JOB_NUMBER}/consoleText

will return the log as plain text


Timestamper Plugin API

But when the plugin is enabled for the job, you can use the API which is available here under the "Scripting" section. The response is a plain text format.

You can format the timestamp that is returned to be date, time, date + time, elapsed time (time since job started), etc.

examples

http://${JENKINS_IP_PORT}/job/${JOB_NAME}/${JOB_NUMBER}/timestamps/?time=yyyy-MM-dd%20HH:mm:ss&appendLog

2020-03-19 13:16:47  Jenkins did something here
2020-03-19 13:16:47  and this is something else that happend
2020-03-19 13:16:47  This line shows some output
2020-03-19 13:16:47  etc.
2020-03-19 13:16:47  etc.
2020-03-19 13:16:47  ...
2020-03-19 13:16:47  ...
2020-03-19 13:16:47
2020-03-19 13:16:47

returns the timestamp and appends the log. The date is formatted as "2020-03-19 16:30:23"

using M instead of MM will return the month without a preceding 0.

using MMM will return the month as a 3 letters month, eg "MAR"

http://${JENKINS_IP_PORT}/job/${JOB_NAME}/${JOB_NUMBER}/timestamps

2.667
2.670
2.768
32.778
32.778
32.820

Will return the timestamps without the log lines. Simple plain text list of the seconds since the build started.

http://${JENKINS_IP_PORT}/job/${JOB_NAME}/${JOB_NUMBER}/timestamps/?time=yyyy-MM-dd%20HH:mm:ss

2020-03-19 13:16:47
2020-03-19 13:16:47
2020-03-19 13:16:47
2020-03-19 13:16:47
2020-03-19 13:16:47
2020-03-19 13:16:47
2020-03-19 13:16:47
2020-03-19 13:16:47
2020-03-19 13:16:47

Will return a formatted list of timestamps

Warning

The Timestamper API is available only if a job enabled the plugin. Accessing the API url in other cases will return an empty response.

Six years later... I had the same question, and could not find an answer. So I pieced together my own solution based on tidbits found around the Internet and my own experimentation.

I was writing a web script to generate a real-time report, and it required Jenkins logs with timestamps. Using the Timestamper URL/API was not an option for me, as my webserver blocked the sending of HTTP requests to other webservers. So I needed to be able to extract the timestamp info directly from the timestamps file, which was available on my server via disk mounts.

Note that I am using Jenkins 2.190.1 with Timestamper 1.10. Your mileage may vary.

Brief Explanation:

Basically, the timestamps file gives the elapsed time between logs, in milliseconds. The data is listed in base-128 (varint) format. So you need to read the file one byte at a time. If any byte is greater than 0x7F (127), then you need to take the lowest 7-bits, shift left by 7 bits, and then read the next byte; repeat until the read byte is greater than 0x7F.

Detailed Explanation (including Perl code):

For example, given the following bytes (read from the start of a real timestamps file):

08 01 2c 04 0c 04 25 01 04 02 00 02 fe 47 11 ...

The first byte, 0x08, is less than 0x7f, so the first log occurred 8ms after the start of the build. The second byte, 0x01, means that the second log occurred 1ms after the first log. The third log occurred 0x2C ms (44ms) after the second log. And so on.

Things are a little different for the 13th log (byte 0xfe). Since 0xfe > 0x7f, we only need the lowest 7-bits.

0xfe & 0x7f = 1111 1110 & 0111 1111 == 0111 1110 = 0x7e == 126

And then we left-shift the result by 7-bits (ie multiply by 128):

0x7e << 7 == 0x3f00 == 16128

Then we read the next byte (0x47), which is less than 0x7F, so it is the last byte for this (13th) log. So we simply add this value to the left-shifted value above:

0x3f00 + 0x47 = 0x3f47 = 16128 + 71 = 16199

So the 13th log occurred 16199ms (or 16.199s) after the 12th log.

Repeat this process for every byte in the timestamps file.

This gives the elapsed times (in ms) between each log in the file. To find the actual epoch-timestamps, use the last-modified time of the timestamps file, and work backwards. Then display the epoch-time of each log in whatever date/time format you desire.

Perl Code Example:

The following Perl function works (Perl 5.22). It is based on the get_varint() function in [ https://github.com/danaj/BitStream/blob/master/lib/Data/BitStream/Code/Varint.pm][1] but the Varint.pm module isn't available on my server (and getting the IT folks to install it would actually take much longer than it took me to write and test the following code). It may not be pretty, but it works.

I hope it helps.

my @Timestamps = ();
my $path = "/full/path/to/timestamps";

if(open(TS, '<:raw', $path))
  {
   my($byte, $ok, $shift);
   my $elapsed = 0;
   my $ms = 0;

   while()
     {
      $ok = read(TS, $byte, 1);

      if(!defined($ok) || $ok <= 0)
        { last; }

      $byte = sprintf("%d", ord($byte));
      $shift = 7;
      $ms = $byte & 0x7F;

      while($byte > 127)
        {
         $ok = read(TS, $byte, 1);

         if(!defined($ok) || $ok <= 0)
           { last; }

         $byte = sprintf("%d", ord($byte));
         $ms |= ($byte & 0x7F) << $shift;
         $shift += 7;
        } # end while byte

      $elapsed += $ms;
      push(@Timestamps, $elapsed);
     } # end while 

   close(TS);
  } # end if open

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM