Created
June 9, 2020 06:12
-
-
Save abrom/a25167638d254197bbbe2981c5203ccd to your computer and use it in GitHub Desktop.
Ruby script to extract logs for a specific Jenkins node
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby | |
## Jenkins log extractor | |
# | |
# Script iterates over the Jenkins log index file searching for log offsets matching the specified node ID. | |
# When a match is found the specified portion of the interleaved Jenkins log is streamed to STDOUT | |
# | |
# Example usage: | |
# jenkins_log_splitter /var/lib/jenkins/jobs/my_job/master/build/7/log 123 | |
# | |
if ARGV.length != 2 | |
puts "Usage: #{$PROGRAM_NAME} <jenkins log path> <node ID>" | |
exit 1 | |
end | |
log_name = ARGV[0] | |
node_id = ARGV[1] | |
start_offset = nil | |
begin | |
log = File.open log_name | |
File.readlines("#{log_name}-index").each do |line| | |
if start_offset | |
end_offset = line.split(' ').first.to_i | |
IO.copy_stream(log, $stdout, end_offset - start_offset, start_offset) | |
start_offset = nil | |
else | |
line_offset, line_node_id = line.split(' ') | |
start_offset = line_offset.to_i if line_node_id == node_id | |
end | |
end | |
rescue Errno::EPIPE | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment