2022-06-17 08:39:35 +00:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
2022-06-20 14:43:17 +00:00
|
|
|
const packageJson = require('./package.json');
|
2022-06-15 06:32:29 +00:00
|
|
|
const fetch = require('node-fetch');
|
2022-06-16 11:07:29 +00:00
|
|
|
const colors = require('colors');
|
2022-06-17 08:39:35 +00:00
|
|
|
const os = require('os');
|
|
|
|
const fs = require('fs');
|
|
|
|
|
|
|
|
const homeDir = os.homedir();
|
|
|
|
const configFile = `${homeDir}/.grafana-find.json`;
|
2022-06-20 14:43:17 +00:00
|
|
|
const error = `[ERROR]: `.bold;
|
|
|
|
|
|
|
|
console.log(
|
|
|
|
`Grafana-Find (${packageJson.description})`.yellow.bold,
|
|
|
|
`v${packageJson.version}`.cyan.bold
|
|
|
|
);
|
2022-06-17 08:39:35 +00:00
|
|
|
|
|
|
|
if (!fs.existsSync(configFile)) {
|
2022-06-20 14:43:17 +00:00
|
|
|
console.error(`${error}Configuration file not found: ${configFile}\n`.red);
|
2022-06-17 08:39:35 +00:00
|
|
|
process.exit(1);
|
|
|
|
}
|
2022-06-14 12:57:03 +00:00
|
|
|
|
2022-06-20 13:37:51 +00:00
|
|
|
const user = process.argv[2];
|
|
|
|
const findAll = process.argv[3];
|
2022-06-17 08:39:35 +00:00
|
|
|
|
2022-06-20 14:43:17 +00:00
|
|
|
if (!process.argv[2] || process.argv[2]===`?` || process.argv[2]===`/?` || process.argv[2]===`help` || process.argv[2]===`/help`) {
|
|
|
|
console.log(`Usage:`.gray, `grafana-find`, `<user>`.blue, `<string to search>\n`.magenta);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!process.argv[3]) {
|
|
|
|
console.error(`${error}The string to search for is missing\n`.red);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
2022-06-17 08:39:35 +00:00
|
|
|
const config = require(configFile);
|
|
|
|
const grafanaUrl = config.grafanaUrl;
|
|
|
|
|
|
|
|
const grafanaApi = `${grafanaUrl}/api`;
|
|
|
|
const urlDashboards = `${grafanaApi}/search`;
|
|
|
|
const urlUID = `${grafanaApi}/dashboards/uid/`;
|
|
|
|
let numberOfDashboards = 0;
|
2022-06-17 12:45:02 +00:00
|
|
|
let numberOfPanels = 0;
|
2022-06-17 08:39:35 +00:00
|
|
|
let numberOfVariables = 0;
|
2022-06-17 12:45:02 +00:00
|
|
|
let numberOfObjects = 0;
|
2022-06-17 08:39:35 +00:00
|
|
|
let titlePanels = new Array;
|
2022-06-17 12:45:02 +00:00
|
|
|
let nameVariables = new Array;
|
2022-06-17 06:13:54 +00:00
|
|
|
|
2022-06-16 11:07:29 +00:00
|
|
|
const regexRawSQL = new RegExp(findAll, 'i');
|
2022-06-14 12:57:03 +00:00
|
|
|
|
2022-06-15 06:32:29 +00:00
|
|
|
async function main(){
|
|
|
|
|
2022-06-17 12:45:02 +00:00
|
|
|
const readline = require('readline');
|
|
|
|
const rl = readline.createInterface({
|
|
|
|
input: process.stdin,
|
|
|
|
output: process.stdout
|
2022-06-20 11:52:00 +00:00
|
|
|
});
|
|
|
|
|
2022-06-20 12:32:04 +00:00
|
|
|
rl.stdoutMuted = true;
|
|
|
|
|
2022-06-17 12:45:02 +00:00
|
|
|
const answer = await new Promise(resolve => {
|
|
|
|
rl.question(colors.yellow('Enter your password: '), resolve);
|
2022-06-20 12:32:04 +00:00
|
|
|
rl._writeToOutput = function _writeToOutput(stringToWrite) {
|
|
|
|
if (rl.stdoutMuted)
|
|
|
|
rl.output.write("*");
|
|
|
|
else
|
|
|
|
rl.output.write(stringToWrite);
|
|
|
|
};
|
2022-06-17 12:45:02 +00:00
|
|
|
});
|
2022-06-20 12:32:04 +00:00
|
|
|
const passw = `${answer}`;
|
2022-06-17 12:45:02 +00:00
|
|
|
if (!answer) {
|
2022-06-20 14:43:17 +00:00
|
|
|
console.error(`\n${error}You need to put a password\n`.red);
|
2022-06-17 12:45:02 +00:00
|
|
|
process.exit(0);
|
|
|
|
}
|
2022-06-20 12:32:04 +00:00
|
|
|
rl.history = rl.history.slice(1)
|
2022-06-17 12:45:02 +00:00
|
|
|
rl.close();
|
2022-06-20 12:32:04 +00:00
|
|
|
const credentials = `Basic ` + Buffer.from(`${user}:${passw}`).toString('base64');
|
2022-06-20 11:52:00 +00:00
|
|
|
|
2022-06-17 06:13:54 +00:00
|
|
|
console.clear();
|
2022-06-20 13:22:59 +00:00
|
|
|
console.log(colors.green.bold(`-------------------- Starting process --------------------\n`));
|
2022-06-17 06:13:54 +00:00
|
|
|
|
|
|
|
let responseAllUID = await fetch(urlDashboards, {
|
|
|
|
method: "GET",
|
|
|
|
headers: {
|
2022-06-20 13:22:59 +00:00
|
|
|
Authorization: credentials
|
2022-06-17 06:13:54 +00:00
|
|
|
}
|
|
|
|
});
|
2022-06-20 12:32:04 +00:00
|
|
|
|
2022-06-17 06:13:54 +00:00
|
|
|
let allUID = await responseAllUID.json();
|
|
|
|
|
2022-06-20 14:43:17 +00:00
|
|
|
if (allUID.message==='invalid username or password') {
|
|
|
|
console.error(`${error}Invalid username or password\n`.red);
|
2022-06-20 12:32:04 +00:00
|
|
|
process.exit(0);
|
|
|
|
}
|
|
|
|
|
2022-06-17 06:13:54 +00:00
|
|
|
for (let i=0; i < allUID.length; i++) {
|
|
|
|
|
|
|
|
let url = urlUID + allUID[i].uid;
|
2022-06-15 08:21:55 +00:00
|
|
|
let response = await fetch(url, {
|
2022-06-15 06:32:29 +00:00
|
|
|
method: "GET",
|
|
|
|
headers: {
|
2022-06-20 13:22:59 +00:00
|
|
|
Authorization: credentials,
|
2022-06-15 08:21:55 +00:00
|
|
|
}
|
|
|
|
});
|
2022-06-20 12:32:04 +00:00
|
|
|
|
2022-06-15 08:21:55 +00:00
|
|
|
let data = await response.json();
|
2022-06-16 11:07:29 +00:00
|
|
|
|
|
|
|
let isFound = false;
|
|
|
|
|
|
|
|
const dashboard = data.dashboard;
|
|
|
|
if (dashboard) {
|
|
|
|
if (dashboard.panels)
|
2022-06-17 08:39:35 +00:00
|
|
|
for (const panel of dashboard.panels) {
|
|
|
|
if (panel.targets)
|
|
|
|
for (const target of panel.targets) {
|
|
|
|
isFound = regexRawSQL.test(target.rawSql);
|
|
|
|
if (isFound) {
|
|
|
|
if (panel.title)
|
|
|
|
titlePanels.push(panel.title);
|
|
|
|
else
|
|
|
|
titlePanels.push("?");
|
2022-06-17 12:45:02 +00:00
|
|
|
numberOfPanels++;
|
2022-06-17 08:39:35 +00:00
|
|
|
}
|
|
|
|
}
|
2022-06-16 11:07:29 +00:00
|
|
|
}
|
|
|
|
|
2022-06-17 08:39:35 +00:00
|
|
|
if (dashboard.templating)
|
2022-06-16 11:07:29 +00:00
|
|
|
for (const list of dashboard.templating.list) {
|
|
|
|
isFound = regexRawSQL.test(list.query);
|
2022-06-17 08:39:35 +00:00
|
|
|
if (isFound) {
|
2022-06-17 12:45:02 +00:00
|
|
|
nameVariables.push(list.name)
|
2022-06-17 08:39:35 +00:00
|
|
|
numberOfVariables++;
|
|
|
|
}
|
2022-06-16 11:07:29 +00:00
|
|
|
}
|
|
|
|
}
|
2022-06-17 08:39:35 +00:00
|
|
|
|
2022-06-17 06:13:54 +00:00
|
|
|
if (isFound) {
|
2022-06-17 08:39:35 +00:00
|
|
|
const linkUrl = `${grafanaUrl}/d/${allUID[i].uid}`;
|
2022-06-17 12:45:02 +00:00
|
|
|
console.log(colors.yellow(linkUrl));
|
|
|
|
if (numberOfPanels) {
|
|
|
|
console.log(colors.cyan.bold(`> ${numberOfPanels} panels`));
|
|
|
|
console.log(colors.cyan(titlePanels.toString().split(",")));
|
|
|
|
}
|
|
|
|
if (numberOfVariables) {
|
|
|
|
console.log(colors.magenta.bold(`> ${numberOfVariables} variables`));
|
|
|
|
console.log(colors.magenta(nameVariables.toString().split(",")));
|
|
|
|
}
|
2022-06-20 13:22:59 +00:00
|
|
|
console.log('')
|
2022-06-17 08:39:35 +00:00
|
|
|
numberOfDashboards++;
|
2022-06-17 06:13:54 +00:00
|
|
|
}
|
2022-06-17 08:39:35 +00:00
|
|
|
|
2022-06-17 12:45:02 +00:00
|
|
|
titlePanels = [];
|
|
|
|
nameVariables= [];
|
|
|
|
numberOfObjects = numberOfPanels + numberOfVariables + numberOfObjects
|
|
|
|
numberOfPanels=0;
|
2022-06-17 08:39:35 +00:00
|
|
|
numberOfVariables=0;
|
2022-06-15 06:32:29 +00:00
|
|
|
}
|
2022-06-17 06:13:54 +00:00
|
|
|
|
2022-06-20 14:43:17 +00:00
|
|
|
if (numberOfDashboards===0)
|
|
|
|
console.log(`No results found\n`.yellow.bold);
|
|
|
|
|
|
|
|
console.log(colors.green.bold(`-------- Have been found ${numberOfObjects} results in ${numberOfDashboards} dashboards -------\n`));
|
2022-06-17 06:13:54 +00:00
|
|
|
|
2022-06-17 12:45:02 +00:00
|
|
|
process.exit(0);
|
2022-06-14 12:57:03 +00:00
|
|
|
}
|
2022-06-16 11:07:29 +00:00
|
|
|
main();
|