grafana-find/main.js

290 lines
9.0 KiB
JavaScript
Raw Normal View History

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 getopts = require('getopts');
2022-06-17 08:39:35 +00:00
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
const usage = {
description: 'Utility to find strings in dashboards',
params: {
user: 'Grafana username',
version: 'Display the version number and exit',
help: 'Display this help message'
}
};
const opts = getopts(process.argv.slice(2), {
alias: {
version: 'v',
help: 'h'
},
boolean: [
'version',
'help'
]
});
if (opts.version) {
process.exit();
}
if (opts.help) {
2022-06-21 15:54:01 +00:00
console.log(`Usage:`.gray, `grafana-find`, `<string to search>`.magenta);
process.exit();
2022-06-20 14:43:17 +00:00
}
2022-06-21 15:54:01 +00:00
if (!opts._[0]) {
2022-06-21 15:54:01 +00:00
console.error(`${error}The string to search for is missing`.red);
2022-06-20 14:43:17 +00:00
process.exit(1);
}
if (opts._.length > 1) {
2022-06-21 15:54:01 +00:00
console.error(`${error}This command doesn't allow more parameters`.red);
2022-06-20 16:25:30 +00:00
process.exit(1);
}
2022-06-20 18:07:14 +00:00
let config;
const configPaths = [
os.homedir(),
`${__dirname}`
];
for (const configPath of configPaths) {
const configFile = `${configPath}/.grafana-find.json`;
if (fs.existsSync(configFile)) {
config = require(configFile);
break;
}
}
if (!config) {
console.error(`${error}Configuration file not found, search paths: .grafana-find.json: ${configPaths.join(':')}\n`.red);
process.exit(1);
}
2022-06-20 18:07:14 +00:00
const findAll = opts._[0];
2022-06-17 08:39:35 +00:00
const grafanaUrl = config.grafanaUrl;
let user = config.user;
let passw = config.password;
2022-06-17 08:39:35 +00:00
const grafanaApi = `${grafanaUrl}/api`;
const urlOrganizations = `${grafanaUrl}/api/orgs`;
2022-06-20 16:25:30 +00:00
const urlDashboards = `${grafanaApi}/search?orgId=`;
2022-06-17 08:39:35 +00:00
const urlUID = `${grafanaApi}/dashboards/uid/`;
let numberOfDashboards = 0;
2022-06-21 16:23:33 +00:00
let totalDashboards = 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-21 15:54:01 +00:00
if (!user) {
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
const answer = await new Promise(resolve => {
rl.question(colors.green('Enter your user: '), resolve);
});
user = `${answer}`;
if (!answer) {
console.error(`\n${error}You need to put a user\n`.red);
process.exit(0);
}
rl.close();
}
if (!passw) {
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
2022-06-15 06:32:29 +00:00
rl.stdoutMuted = true;
2022-06-20 12:32:04 +00:00
const answer = await new Promise(resolve => {
rl.question(colors.green('Enter your password: '), resolve);
rl._writeToOutput = function _writeToOutput(stringToWrite) {
if (rl.stdoutMuted)
rl.output.write("*");
else
rl.output.write(stringToWrite);
};
});
passw = `${answer}`;
if (!answer) {
console.error(`\n${error}You need to put a password\n`.red);
process.exit(0);
}
rl.close();
2022-06-17 12:45:02 +00:00
}
2022-06-20 12:32:04 +00:00
const credentials = `Basic ` + Buffer.from(`${user}:${passw}`).toString('base64');
2022-06-17 06:13:54 +00:00
2022-06-22 11:51:49 +00:00
try {
var response = await fetch(urlOrganizations, {
method: "GET",
headers: {
Authorization: credentials
}
});
} catch (notfound) {
console.error(`${error}Server '${config.grafanaUrl}' not found`.red);
process.exit(1);
};
let AllOrganizations = await response.json();
2022-06-17 06:13:54 +00:00
if (AllOrganizations.message==='invalid username or password') {
console.error(`\n${error}Invalid username or password\n`.red);
process.exit(1);
2022-06-20 12:32:04 +00:00
}
console.clear();
console.log(
2022-06-22 17:52:48 +00:00
`───────── Grafana-Find (${packageJson.description}) v${packageJson.version} ────────`.yellow.bold
);
2022-06-22 17:52:48 +00:00
console.log(colors.green.bold(`──────────────────── Starting process ────────────────────\n`));
2022-06-17 06:13:54 +00:00
for (let x in AllOrganizations) {
2022-06-22 17:52:48 +00:00
console.log(colors.gray.bold(`-{Organization: ${AllOrganizations[x].name}}-\n`));
response = await fetch(`${urlDashboards}${AllOrganizations[x].id}`, {
2022-06-15 06:32:29 +00:00
method: "GET",
headers: {
Authorization: credentials
},
redirect: 'manual'
2022-06-15 08:21:55 +00:00
});
if (response.status === 302) {
response = await fetch(`${urlDashboards}${AllOrganizations[x].id}`, {
method: 'GET',
headers: {
Accept: 'application/json'
},
redirect: 'manual'
});
}
let allUID = await response.json();
for (let i in allUID) {
let url = `${urlUID}${allUID[i].uid}`;
response = await fetch(url, {
method: "GET",
headers: {
Authorization: credentials,
},
redirect: 'manual'
});
if (response.status === 404) {
response = await fetch(url, {
method: 'GET',
headers: {
Accept: 'application/json'
},
redirect: 'manual'
});
}
let data = await response.json();
let isFound = false;
2022-06-21 15:54:01 +00:00
let isFoundSomething = false;
const dashboard = data.dashboard;
if (dashboard) {
if (dashboard.panels)
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)
2022-06-22 17:52:48 +00:00
if (panel.title==' ')
titlePanels.push(`(null)`.italic);
else
titlePanels.push(panel.title);
else
2022-06-22 17:52:48 +00:00
titlePanels.push(`(undefined)`.italic);
numberOfPanels++;
2022-06-21 15:54:01 +00:00
isFoundSomething=true;
}
}
}
if (dashboard.templating)
for (const list of dashboard.templating.list) {
isFound = regexRawSQL.test(list.query);
2022-06-17 08:39:35 +00:00
if (isFound) {
nameVariables.push(list.name)
numberOfVariables++;
2022-06-21 15:54:01 +00:00
isFoundSomething=true;
2022-06-17 08:39:35 +00:00
}
}
2022-06-16 11:07:29 +00:00
}
2022-06-21 15:54:01 +00:00
if (isFoundSomething) {
const linkUrl = `${grafanaUrl}/d/${allUID[i].uid}?orgId=${AllOrganizations[x].id}`;
console.log((linkUrl).yellow.underline, dashboard.title);
if (numberOfPanels) {
2022-06-22 17:52:48 +00:00
console.log(colors.cyan.bold(`[${numberOfPanels}] panels`));
for (let q in titlePanels) {
if (q==(titlePanels.length-1)) {
console.log(` └─${titlePanels[q]}`.cyan)
break
}
console.log(` ├─${titlePanels[q]}`.cyan)
}
}
if (numberOfVariables) {
2022-06-22 17:52:48 +00:00
console.log(colors.magenta.bold(`[${numberOfVariables}] variables`));
for (let q in nameVariables) {
if (q==(nameVariables.length-1)) {
console.log(` └─${nameVariables[q]}`.magenta)
break
}
console.log(` ├─${nameVariables[q]}`.magenta)
}
2022-06-17 08:39:35 +00:00
}
console.log('')
numberOfDashboards++;
2022-06-16 11:07:29 +00:00
}
titlePanels = [];
nameVariables= [];
2022-06-21 16:23:33 +00:00
numberOfObjects = numberOfPanels + numberOfVariables + numberOfObjects;
numberOfPanels = 0;
numberOfVariables = 0;
2022-06-16 11:07:29 +00:00
}
2022-06-21 16:23:33 +00:00
totalDashboards = numberOfDashboards + totalDashboards;
2022-06-20 16:25:30 +00:00
if (!numberOfDashboards)
2022-06-21 15:54:01 +00:00
console.log(`No results found\n`.green);
2022-06-21 16:23:33 +00:00
numberOfDashboards = 0;
2022-06-20 16:25:30 +00:00
};
2022-06-20 14:43:17 +00:00
2022-06-22 17:52:48 +00:00
console.log(colors.green.bold(`──────── Have been found ${numberOfObjects} results in ${totalDashboards} dashboards ────────\n`));
2022-06-17 06:13:54 +00:00
2022-06-22 11:51:49 +00:00
if (!response) {
console.log(`${error}The server don't exists`);
process.exit(1);
}
process.exit();
2022-06-22 11:51:49 +00:00
2022-06-14 12:57:03 +00:00
}
2022-06-16 11:07:29 +00:00
main();