Created
April 10, 2024 17:42
-
-
Save EatMoreChicken/82783c06d07efe3e2e9051249970d15e to your computer and use it in GitHub Desktop.
This Tampermonkey script extracts the a Splunk search's run duration and displays it in the title of the tab.
This file contains 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
```js | |
// ==UserScript== | |
// @name Extract Splunk Search Run Duration | |
// @namespace http://* | |
// @version 1.0 | |
// @description This script extracts the a Splunk search's run duration and displays it in the title of the tab. | |
// @match http://splunk.example.com:8000/* | |
// @grant none | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
console.log('Script is running...'); | |
// Function to format the duration | |
function formatDuration(duration) { | |
if (duration < 60) { | |
return duration.toFixed(0) + ' seconds'; | |
} else { | |
var minutes = Math.floor(duration / 60); | |
var seconds = duration % 60; | |
return minutes + ' min' + (minutes > 1 ? 's ' : ' ') + seconds.toFixed(0) + ' sec'; | |
} | |
} | |
// Function to run the script logic | |
function runScript() { | |
// Select the element with class "extract-fields-button" | |
var element = document.querySelector('.extract-fields-button'); | |
// Check if the element is found | |
if (element) { | |
console.log('Element with class "extract-fields-button" found.'); | |
// Get the value of the href attribute | |
var hrefValue = element.getAttribute('href'); | |
// Define your regular expression | |
var regex = /sid\=(?<sid>.*)/; | |
// Match the regex against the href value | |
var match = hrefValue.match(regex); | |
// Check if there's a match | |
if (match) { | |
console.log('Regex pattern matched.'); | |
// Extract the value of the named capture group 'sid' | |
var sidValue = match.groups.sid; | |
console.log('sid value:', sidValue); | |
// Build the URL using the extracted sid value and the current base URL | |
var baseUrl = `${window.location.origin}/en-US/splunkd/__raw/services/search/jobs/`; | |
var finalUrl = `${baseUrl}${sidValue}?output_mode=json&message_level=debug`; | |
console.log('Final URL:', finalUrl); | |
// Fetch data from the constructed URL | |
fetch(finalUrl) | |
.then(response => response.json()) | |
.then(data => { | |
// Log the full JSON payload | |
console.log('Full JSON:', data); | |
// Extract the desired field from the JSON payload | |
var runDuration = data.entry[0].content.runDuration; // Duration in seconds | |
console.log('runDuration:', runDuration); | |
// Format the run duration | |
var formattedDuration = formatDuration(runDuration); | |
// Update page title with formatted duration | |
document.title = document.title.split(' | ')[0] + ' | ' + formattedDuration; | |
}) | |
.catch(error => { | |
console.error('Error fetching data:', error); | |
}); | |
} else { | |
console.log('Regex pattern not found in href value.'); | |
} | |
} else { | |
console.log('Element with class "extract-fields-button" not found.'); | |
} | |
} | |
// Run the script initially | |
runScript(); | |
// Set interval to rerun the script every 5 seconds | |
setInterval(runScript, 5000); // 5000 milliseconds = 5 seconds | |
})(); | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment