ahora busca en todas las organizaciones

This commit is contained in:
Guillermo Bonet 2022-06-20 18:11:18 +02:00
parent 1e50339230
commit 70e19e70eb
2 changed files with 77 additions and 210 deletions

View File

@ -1,150 +0,0 @@
#!/usr/bin/env node
const fetch = require('node-fetch');
const colors = require('colors');
const os = require('os');
const fs = require('fs');
const getopts = require('getopts');
const homeDir = os.homedir();
const configFile = `${homeDir}/.grafana-find.json`;
if (!fs.existsSync(configFile)) {
console.error(`Configuration file not found: ${configFile}`.red);
process.exit(1);
}
const user = process.argv[2];
const findAll = process.argv[3];
const config = require(configFile);
const grafanaUrl = config.grafanaUrl;
const grafanaApi = `${grafanaUrl}/api`;
const urlDashboards = `${grafanaApi}/search`;
const urlUID = `${grafanaApi}/dashboards/uid/`;
let numberOfDashboards = 0;
let numberOfPanels = 0;
let numberOfVariables = 0;
let numberOfObjects = 0;
let titlePanels = new Array;
let nameVariables = new Array;
const regexRawSQL = new RegExp(findAll, 'i');
async function main(){
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.stdoutMuted = true;
const answer = await new Promise(resolve => {
rl.question(colors.yellow('Enter your password: '), resolve);
rl._writeToOutput = function _writeToOutput(stringToWrite) {
if (rl.stdoutMuted)
rl.output.write("*");
else
rl.output.write(stringToWrite);
};
});
const passw = `${answer}`;
if (!answer) {
console.error(`You need to put a password`.red);
process.exit(0);
}
rl.history = rl.history.slice(1)
rl.close();
const credentials = `Basic ` + Buffer.from(`${user}:${passw}`).toString('base64');
console.clear();
console.log(colors.green.bold(`-------------------- Starting process --------------------\n`));
let responseAllUID = await fetch(urlDashboards, {
method: "GET",
headers: {
Authorization: credentials
}
});
let allUID = await responseAllUID.json();
if (allUID.message=='invalid username or password') {
console.error(`Invalid username or password\n`.red);
process.exit(0);
}
for (let i=0; i < allUID.length; i++) {
let url = urlUID + allUID[i].uid;
let response = await fetch(url, {
method: "GET",
headers: {
Authorization: credentials,
}
});
let data = await response.json();
let isFound = 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)
titlePanels.push(panel.title);
else
titlePanels.push("?");
numberOfPanels++;
}
}
}
if (dashboard.templating)
for (const list of dashboard.templating.list) {
isFound = regexRawSQL.test(list.query);
if (isFound) {
nameVariables.push(list.name)
numberOfVariables++;
}
}
}
if (isFound) {
const linkUrl = `${grafanaUrl}/d/${allUID[i].uid}`;
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(",")));
}
console.log('')
numberOfDashboards++;
}
titlePanels = [];
nameVariables= [];
numberOfObjects = numberOfPanels + numberOfVariables + numberOfObjects
numberOfPanels=0;
numberOfVariables=0;
}
if (numberOfDashboards==0)
console.log(`No results found\n`.red);
else
console.log(colors.green.bold(`-------- Have been found ${numberOfObjects} results in ${numberOfDashboards} dashboards -------\n`));
process.exit(0);
}
main();

45
main.js
View File

@ -10,6 +10,9 @@ const homeDir = os.homedir();
const configFile = `${homeDir}/.grafana-find.json`;
const error = `[ERROR]: `.bold;
const user = process.argv[2];
const findAll = process.argv[3];
console.log(
`Grafana-Find (${packageJson.description})`.yellow.bold,
`v${packageJson.version}`.cyan.bold
@ -20,9 +23,6 @@ if (!fs.existsSync(configFile)) {
process.exit(1);
}
const user = process.argv[2];
const findAll = process.argv[3];
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);
@ -37,7 +37,8 @@ const config = require(configFile);
const grafanaUrl = config.grafanaUrl;
const grafanaApi = `${grafanaUrl}/api`;
const urlDashboards = `${grafanaApi}/search`;
const urlOrganizations = `${grafanaUrl}/api/orgs`;
const urlDashboards = `${grafanaApi}/search?orgId=`; //?orgId=2
const urlUID = `${grafanaApi}/dashboards/uid/`;
let numberOfDashboards = 0;
let numberOfPanels = 0;
@ -59,7 +60,7 @@ async function main(){
rl.stdoutMuted = true;
const answer = await new Promise(resolve => {
rl.question(colors.yellow('Enter your password: '), resolve);
rl.question(colors.green('Enter your password: '), resolve);
rl._writeToOutput = function _writeToOutput(stringToWrite) {
if (rl.stdoutMuted)
rl.output.write("*");
@ -76,27 +77,39 @@ async function main(){
rl.close();
const credentials = `Basic ` + Buffer.from(`${user}:${passw}`).toString('base64');
console.clear();
console.log(colors.green.bold(`-------------------- Starting process --------------------\n`));
let responseAllUID = await fetch(urlDashboards, {
let response = await fetch(urlOrganizations, {
method: "GET",
headers: {
Authorization: credentials
}
});
let allUID = await responseAllUID.json();
let AllOrganizations = await response.json();
if (allUID.message==='invalid username or password') {
console.error(`${error}Invalid username or password\n`.red);
if (AllOrganizations.message==='invalid username or password') {
console.error(`\n${error}Invalid username or password\n`.red);
process.exit(0);
}
for (let i=0; i < allUID.length; i++) {
console.clear();
console.log(colors.green.bold(`-------------------- Starting process --------------------\n`));
for (let x in AllOrganizations) {
console.log(colors.gray.bold(`(Organization: ${AllOrganizations[x].name})\n`));
response = await fetch(`${urlDashboards}${AllOrganizations[x].id}`, {
method: "GET",
headers: {
Authorization: credentials
}
});
let allUID = await response.json();
for (let i in allUID) {
let url = urlUID + allUID[i].uid;
let response = await fetch(url, {
response = await fetch(url, {
method: "GET",
headers: {
Authorization: credentials,
@ -156,6 +169,10 @@ async function main(){
numberOfVariables=0;
}
};
if (numberOfDashboards===0)
console.log(`No results found\n`.yellow.bold);