2022-06-17 08:39:35 +00:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
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');
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2022-06-14 12:57:03 +00:00
|
|
|
|
2022-06-17 08:39:35 +00:00
|
|
|
/* Pedir contraseña
|
|
|
|
-----------------
|
|
|
|
const readline = require('readline');
|
|
|
|
const rl = readline.createInterface({
|
|
|
|
input: process.stdin,
|
|
|
|
output: process.stdout
|
|
|
|
});
|
|
|
|
const answer = await new Promise(resolve => {
|
|
|
|
rl.question('Are you sure? (Default: no) [yes|no] ', resolve);
|
|
|
|
});
|
|
|
|
rl.close();
|
|
|
|
*/
|
2022-06-15 08:21:55 +00:00
|
|
|
|
2022-06-17 08:39:35 +00:00
|
|
|
const findAll = `WHERE d.name LIKE '%EQUIPO %'`;
|
|
|
|
const apiKey = `Bearer eyJrIjoiaHczdnZsOGd5M0xNUHJwZ3V4MGpvQzV0QVNGblpySloiLCJuIjoiZmluZEFsbF9HcmFmYW5hIiwiaWQiOjF9`;
|
|
|
|
|
|
|
|
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 numberOfObjects = 0;
|
|
|
|
let numberOfVariables = 0;
|
|
|
|
let titlePanels = 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 06:13:54 +00:00
|
|
|
console.clear();
|
2022-06-17 08:39:35 +00:00
|
|
|
console.log(colors.green.bold(`-------------------- Starting process --------------------`));
|
2022-06-17 06:13:54 +00:00
|
|
|
|
|
|
|
let responseAllUID = await fetch(urlDashboards, {
|
|
|
|
method: "GET",
|
|
|
|
headers: {
|
|
|
|
"Authorization": apiKey,
|
|
|
|
}
|
|
|
|
});
|
|
|
|
let allUID = await responseAllUID.json();
|
|
|
|
|
|
|
|
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-17 06:13:54 +00:00
|
|
|
"Authorization": apiKey,
|
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("?");
|
|
|
|
numberOfObjects++;
|
|
|
|
}
|
|
|
|
}
|
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) {
|
|
|
|
numberOfVariables++;
|
|
|
|
numberOfObjects++;
|
|
|
|
}
|
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}`;
|
|
|
|
if (titlePanels.length)
|
|
|
|
console.log(colors.yellow(linkUrl), colors.white(titlePanels.toString().split(",")));
|
|
|
|
else
|
|
|
|
console.log(colors.yellow(linkUrl));
|
|
|
|
if (numberOfVariables)
|
|
|
|
console.log(colors.magenta(`> ${numberOfVariables} variables`));
|
|
|
|
numberOfDashboards++;
|
2022-06-17 06:13:54 +00:00
|
|
|
}
|
2022-06-17 08:39:35 +00:00
|
|
|
|
|
|
|
titlePanels.pop();
|
|
|
|
numberOfVariables=0;
|
2022-06-15 06:32:29 +00:00
|
|
|
}
|
2022-06-17 06:13:54 +00:00
|
|
|
|
2022-06-17 08:39:35 +00:00
|
|
|
if (numberOfDashboards==0)
|
|
|
|
console.log(`No results found`.red);
|
2022-06-17 06:13:54 +00:00
|
|
|
else
|
2022-06-17 08:39:35 +00:00
|
|
|
console.log(colors.green.bold(`---- Have been found ${numberOfObjects} results in ${numberOfDashboards} dashboards ----`));
|
2022-06-17 06:13:54 +00:00
|
|
|
|
2022-06-14 12:57:03 +00:00
|
|
|
}
|
2022-06-16 11:07:29 +00:00
|
|
|
main();
|