Compare commits
4 Commits
master
...
4691-grafa
Author | SHA1 | Date |
---|---|---|
Vicent Llopis | 98d2029582 | |
Vicent Llopis | 617b44d90e | |
Vicent Llopis | 4f6636b51c | |
Vicent Llopis | 79faeed8b3 |
|
@ -0,0 +1,38 @@
|
|||
extends: [eslint:recommended, google, plugin:jasmine/recommended]
|
||||
parserOptions:
|
||||
ecmaVersion: 2018
|
||||
sourceType: "module"
|
||||
plugins:
|
||||
- jasmine
|
||||
env:
|
||||
jasmine: true
|
||||
rules:
|
||||
require-jsdoc: 0
|
||||
no-undef: 0
|
||||
max-len: ["error", {code: 120}]
|
||||
eqeqeq: 0
|
||||
operator-linebreak: 0
|
||||
radix: 0
|
||||
guard-for-in: 0
|
||||
camelcase: 0
|
||||
default-case: 0
|
||||
no-eq-null: 0
|
||||
no-console: ["error"]
|
||||
no-warning-comments: 0
|
||||
no-empty: [error, allowEmptyCatch: true]
|
||||
complexity: 0
|
||||
max-depth: 0
|
||||
comma-dangle: 0
|
||||
bracketSpacing: 0
|
||||
space-infix-ops: 1
|
||||
no-invalid-this: 0
|
||||
space-before-function-paren: [error, never]
|
||||
prefer-const: 0
|
||||
curly: [error, multi-or-nest]
|
||||
indent: [error, 4]
|
||||
arrow-parens: [error, as-needed]
|
||||
no-multiple-empty-lines: ["error", { "max": 1, "maxEOF": 1 }]
|
||||
space-in-parens: ["error", "never"]
|
||||
jasmine/no-focused-tests: 0
|
||||
jasmine/prefer-toHaveBeenCalledWith: 0
|
||||
arrow-spacing: ["error", { "before": true, "after": true }]
|
|
@ -1,5 +1,5 @@
|
|||
{
|
||||
"grafanaUrl": "https://grafana.example.com",
|
||||
"user": "user",
|
||||
"password": "1234"
|
||||
"grafanaUrl": "https://grafana.verdnatura.es",
|
||||
"user": "vicent",
|
||||
"password": "llopis.19263"
|
||||
}
|
|
@ -1,7 +1,17 @@
|
|||
{
|
||||
// Use IntelliSense para saber los atributos posibles.
|
||||
// Mantenga el puntero para ver las descripciones de los existentes atributos.
|
||||
// Para más información, visite: https://go.microsoft.com/fwlink/?linkid=830387
|
||||
"version": "0.2.0",
|
||||
"configurations": []
|
||||
"configurations": [
|
||||
{
|
||||
"type": "node",
|
||||
"request": "attach",
|
||||
"name": "Attach",
|
||||
"restart": true,
|
||||
"timeout": 50000
|
||||
}, {
|
||||
"type": "node",
|
||||
"request": "attach",
|
||||
"name": "Attach by process ID",
|
||||
"processId": "${command:PickProcess}"
|
||||
}
|
||||
]
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
// Coloque su configuración en este archivo para sobrescribir la configuración predeterminada y de usuario.
|
||||
{
|
||||
// Carácter predeterminado de final de línea.
|
||||
"files.eol": "\n",
|
||||
"editor.codeActionsOnSave": {
|
||||
"source.fixAll.eslint": true
|
||||
}
|
||||
}
|
|
@ -1,5 +1,5 @@
|
|||
# Grafana Find
|
||||
Search for a string both in the grafana queries.
|
||||
Search for a string both in the grafana queries and in the variables.
|
||||
|
||||
## Requirements
|
||||
|
||||
|
|
302
main.js
302
main.js
|
@ -6,6 +6,31 @@ const colors = require('colors');
|
|||
const os = require('os');
|
||||
const fs = require('fs');
|
||||
const getopts = require('getopts');
|
||||
const mysql = require('mysql2/promise');
|
||||
|
||||
const conGrafana = mysql.createPool({
|
||||
host: "dev-db.verdnatura.es",
|
||||
user: "grafanaPermissions",
|
||||
password: "grafana",
|
||||
port : 3307,
|
||||
queryTimeout: 100
|
||||
});
|
||||
|
||||
const conVicent = mysql.createPool({
|
||||
host: "dev-db.verdnatura.es",
|
||||
user: "vicent",
|
||||
password: "llopis.19263",
|
||||
port : 3307
|
||||
});
|
||||
|
||||
// con.connect(function(err) {
|
||||
// if (err) {
|
||||
// return console.error('error: ' + err.message);
|
||||
// }
|
||||
|
||||
// console.log('Connected to the MySQL server.');
|
||||
// });
|
||||
|
||||
|
||||
const error = `[ERROR]: `.bold;
|
||||
|
||||
|
@ -23,7 +48,7 @@ const opts = getopts(process.argv.slice(2), {
|
|||
'version',
|
||||
'help'
|
||||
]
|
||||
});
|
||||
});
|
||||
|
||||
if (opts.version) {
|
||||
process.exit();
|
||||
|
@ -60,25 +85,23 @@ if (!config) {
|
|||
}
|
||||
|
||||
const findAll = opts._[0];
|
||||
const regexRawSQL = new RegExp(findAll, 'i');
|
||||
const grafanaUrl = config.grafanaUrl;
|
||||
let user = config.user;
|
||||
let passw = config.password;
|
||||
let numberOfDashboards = 0;
|
||||
let totalObjects = 0;
|
||||
let numberOfPanels = 0;
|
||||
let numberOfVariables = 0;
|
||||
let numberOfObjects = 0;
|
||||
let titlePanels = new Array;
|
||||
let titleAlerts = new Array;
|
||||
let nameVariables = new Array;
|
||||
|
||||
// URIs
|
||||
const grafanaApi = `${grafanaUrl}/api`;
|
||||
const urlOrganizations = `${grafanaUrl}/api/orgs`;
|
||||
const urlDashboards = `${grafanaApi}/search?orgId=`;
|
||||
const urlUID = `${grafanaApi}/dashboards/uid/`;
|
||||
const urlAlerts = `${grafanaApi}/v1/provisioning/alert-rules?orgId=`;
|
||||
let numberOfDashboards = 0;
|
||||
let totalDashboards = 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(){
|
||||
if (!user) {
|
||||
|
@ -137,46 +160,43 @@ async function main(){
|
|||
} catch (notfound) {
|
||||
console.error(`${error}Server '${config.grafanaUrl}' not found`.red);
|
||||
process.exit(1);
|
||||
};
|
||||
}
|
||||
let AllOrganizations = await response.json();
|
||||
|
||||
if (AllOrganizations.message === 'invalid username or password') {
|
||||
if (AllOrganizations.message==='invalid username or password') {
|
||||
console.error(`\n${error}Invalid username or password\n`.red);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
console.clear();
|
||||
console.log(
|
||||
`───────── Grafana-Find (${packageJson.description}) v${packageJson.version} ────────`.bold.bgYellow.bgBrightWhite
|
||||
`───────── Grafana-Find LOCAL (${packageJson.description}) v${packageJson.version} ────────`.yellow.bold
|
||||
);
|
||||
console.log(colors.green.bold(`──────────────────── Starting process ────────────────────\n`));
|
||||
|
||||
for (let x in AllOrganizations) {
|
||||
console.log(colors.red.bold(`🏢 Organization: ${AllOrganizations[x].name} 🏢\n`.underline));
|
||||
|
||||
console.log(colors.gray.bold(`-{Organization: ${AllOrganizations[x].name}}-\n`));
|
||||
response = await fetch(`${urlDashboards}${AllOrganizations[x].id}`, {
|
||||
method: "GET",
|
||||
headers: {
|
||||
Authorization: credentials
|
||||
},
|
||||
redirect: 'manual'
|
||||
});
|
||||
|
||||
console.log(colors.white.bold(`🔎 Searching in dashboards...\n`));
|
||||
if (response.status === 302) {
|
||||
response = await fetch(`${urlDashboards}${AllOrganizations[x].id}`, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
Accept: 'application/json'
|
||||
},
|
||||
redirect: 'manual'
|
||||
});
|
||||
}
|
||||
|
||||
let allUID = await response.json();
|
||||
|
||||
if (allUID.message === 'Unauthorized') {
|
||||
console.log(colors.red.bold(allUID.message))
|
||||
process.exit();
|
||||
}
|
||||
|
||||
for (let i in allUID) {
|
||||
let url = `${urlUID}${allUID[i].uid}`;
|
||||
response = await fetch(url, {
|
||||
|
@ -198,56 +218,33 @@ async function main(){
|
|||
}
|
||||
|
||||
let data = await response.json();
|
||||
|
||||
let isFound = false;
|
||||
let isFoundSomething = false;
|
||||
|
||||
const dashboard = data.dashboard;
|
||||
if (dashboard) {
|
||||
if (dashboard.panels)
|
||||
for (const panel of dashboard.panels) {
|
||||
// Panels
|
||||
if (panel.targets)
|
||||
for (const target of panel.targets) {
|
||||
isFound = regexRawSQL.test(target.rawSql);
|
||||
if (isFound) {
|
||||
if (panel.title)
|
||||
if (panel.title==' ')
|
||||
titlePanels.push(`(null)`.italic);
|
||||
else
|
||||
titlePanels.push(panel.title);
|
||||
else
|
||||
titlePanels.push(`(undefined)`.italic);
|
||||
numberOfPanels++;
|
||||
isFoundSomething=true;
|
||||
}
|
||||
}
|
||||
// Rows
|
||||
if (panel.panels)
|
||||
for (const subpanel of panel.panels) {
|
||||
if (subpanel.targets)
|
||||
for (const target of subpanel.targets) {
|
||||
isFound = regexRawSQL.test(target.rawSql);
|
||||
if (isFound) {
|
||||
if (subpanel.title)
|
||||
if (subpanel.title==' ')
|
||||
titlePanels.push(`(null)`.italic);
|
||||
else
|
||||
titlePanels.push(subpanel.title);
|
||||
else
|
||||
titlePanels.push(`(undefined)`.italic);
|
||||
numberOfPanels++;
|
||||
isFoundSomething=true;
|
||||
}
|
||||
}
|
||||
const firstWordAfterFrom = getFirstWordAfterFrom(target.rawSql);
|
||||
if(firstWordAfterFrom == 'ost_ticket' || firstWordAfterFrom == 'osticket'|| firstWordAfterFrom == 'redmine') continue;
|
||||
|
||||
const sql = replaceVariables(target.rawSql);
|
||||
|
||||
await queryAndGrant(sql);
|
||||
}
|
||||
}
|
||||
|
||||
if (dashboard.templating)
|
||||
if (dashboard.templating){
|
||||
for (const list of dashboard.templating.list) {
|
||||
isFound = regexRawSQL.test(list.query);
|
||||
if (isFound) {
|
||||
nameVariables.push(list.name)
|
||||
numberOfVariables++;
|
||||
isFoundSomething=true;
|
||||
const firstWord = getFirstWord(list.query);
|
||||
if (firstWord != 'SELECT' || firstWord != 'WITH') continue;
|
||||
|
||||
const sql = replaceVariables(list.query);
|
||||
|
||||
await queryAndGrant(sql);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -285,53 +282,13 @@ async function main(){
|
|||
numberOfPanels = 0;
|
||||
numberOfVariables = 0;
|
||||
}
|
||||
totalObjects = numberOfDashboards + totalObjects;
|
||||
|
||||
totalDashboards = numberOfDashboards + totalDashboards;
|
||||
if (!numberOfDashboards)
|
||||
console.log(`No results found\n`.gray);
|
||||
|
||||
console.log(colors.white.bold(`🔎 Searching in alerts...\n`));
|
||||
|
||||
response = await fetch(`${urlAlerts}${AllOrganizations[x].id}`, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
Accept: 'application/json',
|
||||
Authorization: credentials
|
||||
},
|
||||
redirect: 'manual'
|
||||
});
|
||||
let allAlerts = await response.json();
|
||||
|
||||
isFound = isFoundSomething = false;
|
||||
if (allAlerts.title === 'Access denied')
|
||||
console.log(`${allAlerts.title}\n`.red);
|
||||
else {
|
||||
for (const alert of allAlerts)
|
||||
for (const data of alert.data)
|
||||
if (data?.model?.rawSql) {
|
||||
isFound = regexRawSQL.test(data.model.rawSql);
|
||||
if (isFound) {
|
||||
if (alert?.title)
|
||||
if (alert.title == ' ')
|
||||
titleAlerts.push(`(null)`.italic);
|
||||
else {
|
||||
const linkUrl = `${grafanaUrl}/alerting/${alert.uid}/edit?orgId=${AllOrganizations[x].id}`;
|
||||
console.log((linkUrl).yellow.underline, alert.title, '\n');
|
||||
numberOfObjects++;
|
||||
totalObjects++;
|
||||
}
|
||||
else
|
||||
titleAlerts.push(`(undefined)`.italic);
|
||||
isFoundSomething = true;
|
||||
}
|
||||
console.log(`No results found\n`.green);
|
||||
numberOfDashboards = 0;
|
||||
}
|
||||
|
||||
if (!isFoundSomething)
|
||||
console.log(`No results found\n`.gray);
|
||||
}
|
||||
};
|
||||
|
||||
console.log(colors.green.bold(`─────── Have been found ${numberOfObjects} similarities in ${totalObjects} objects ───────\n`));
|
||||
console.log(colors.green.bold(`──────── Have been found ${numberOfObjects} results in ${totalDashboards} dashboards ────────\n`));
|
||||
|
||||
if (!response) {
|
||||
console.log(`${error}The server don't exists`);
|
||||
|
@ -341,4 +298,141 @@ async function main(){
|
|||
process.exit();
|
||||
|
||||
}
|
||||
|
||||
main();
|
||||
|
||||
|
||||
function getFirstWord(text) {
|
||||
const words = text.split(" ");
|
||||
if (words.length > 0) {
|
||||
return words[0];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function getLastWord(text) {
|
||||
const words = text.split(" ");
|
||||
if (words.length > 0) {
|
||||
return words[words.length - 1];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function getFirstWordBetweenQuotes(text) {
|
||||
const pattern = /'([^']+)'/;
|
||||
const match = text.match(pattern);
|
||||
if (match) {
|
||||
return match[1];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function replaceVariables(text){
|
||||
let sql = text;
|
||||
|
||||
sql = sql.replaceAll('$__timeGroupAlias', 'util.mock2');
|
||||
sql = sql.replaceAll('$__timeGroup', 'util.mock2');
|
||||
sql = sql.replaceAll('$__timeFilter', 'util.mock');
|
||||
sql = sql.replaceAll('$__timeFrom()', `'2022-01-01'`);
|
||||
sql = sql.replaceAll('$__timeTo()', `'2022-01-01'`);
|
||||
sql = sql.replaceAll('${__from}', `'2022-01-01'`);
|
||||
sql = sql.replaceAll('${__to}', `'2022-01-01'`);
|
||||
sql = sql.replaceAll(/\$\w+/g, 1);
|
||||
|
||||
|
||||
return sql;
|
||||
}
|
||||
|
||||
function getFirstWordAfterFrom(text) {
|
||||
const pattern = /FROM\s+(\w+)/;
|
||||
const match = text.match(pattern);
|
||||
if (match) {
|
||||
return match[1];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function addVn(query) {
|
||||
return query.replace(/(FROM|JOIN)\s+(?!\w+\.)(\w+)/g, '$1 vn.$2');
|
||||
}
|
||||
|
||||
function addVnInFunctions(str){
|
||||
let words = str.split(" ");
|
||||
|
||||
for (let i = 0; i < words.length; i++) {
|
||||
if (words[i].startsWith("firstDayOfWeek") || words[i].startsWith("lastDayOfWeek")) {
|
||||
words[i] = "vn." + words[i];
|
||||
}
|
||||
}
|
||||
|
||||
str = words.join(" ");
|
||||
return str;
|
||||
}
|
||||
|
||||
function errorFn() {
|
||||
throw 'hola'
|
||||
}
|
||||
async function queryAndGrant(sql){
|
||||
console.log(sql);
|
||||
let entry;
|
||||
try{
|
||||
console.log('ENTRY ', new Date())
|
||||
entry = new Date().getTime();
|
||||
//const timeout = setTimeout(errorFn, 1000)
|
||||
const value = await conGrafana.query({
|
||||
sql: sql,
|
||||
timeout: 1000 // 1 second
|
||||
});
|
||||
//clearTimeout(timeout)
|
||||
console.log('VALUE ', value, ' ', new Date().getTime() - entry, 's')
|
||||
console.log('STOP ', new Date())
|
||||
|
||||
} catch (err) {
|
||||
if(err.code == 'ER_TABLEACCESS_DENIED_ERROR') {
|
||||
const table = getLastWord(err.sqlMessage); // p.e: `account`.`user`
|
||||
const user = getFirstWordBetweenQuotes(err.sqlMessage); // p.e: grafanaPermissions
|
||||
|
||||
const grant = `GRANT SELECT ON ${table} TO '${user}'@'%';`;
|
||||
console.log(grant);
|
||||
try{
|
||||
await conVicent.query(grant);
|
||||
} catch (err){
|
||||
// console.log(err);
|
||||
if(err.code != 'ER_NO_SUCH_TABLE') await queryAndGrant(sql);
|
||||
}
|
||||
} else if(err.code == 'ER_PROCACCESS_DENIED_ERROR'){
|
||||
const routine = getLastWord(err.sqlMessage); // p.e: 'util.mock'
|
||||
const test = routine.replace(/'/g, "");
|
||||
const user = getFirstWordBetweenQuotes(err.sqlMessage); // p.e: grafanaPermissions
|
||||
|
||||
|
||||
const grant = `GRANT EXECUTE ON FUNCTION ${test} TO '${user}'@'%';`;
|
||||
console.log(grant);
|
||||
try{
|
||||
await conVicent.query(grant);
|
||||
} catch (err){
|
||||
// console.log(err);
|
||||
await queryAndGrant(sql);
|
||||
}
|
||||
} else if(err.code == 'ER_NO_DB_ERROR'){
|
||||
console.log(err);
|
||||
if (!err.sql.includes("users")) {
|
||||
sql = addVn(sql);
|
||||
await queryAndGrant(sql);
|
||||
}
|
||||
} else if(err.code == 'ER_SP_DOES_NOT_EXIST'){
|
||||
console.log(err);
|
||||
const sqlComplete = addVnInFunctions(sql);
|
||||
await queryAndGrant(sqlComplete);
|
||||
} else if(err.code == 'PROTOCOL_SEQUENCE_TIMEOUT'){
|
||||
console.log('NEXT POR TIMEOUT', new Date().getTime() - entry, 's')
|
||||
} else {
|
||||
console.log(err);
|
||||
throw 'ELSE ' + err;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
10
package.json
10
package.json
|
@ -1,12 +1,15 @@
|
|||
{
|
||||
"name": "grafana-find",
|
||||
"version": "1.0.5",
|
||||
"version": "1.0.3",
|
||||
"author": "Verdnatura Levante SL",
|
||||
"description": "Grafana Find Tool",
|
||||
"license": "GPL-3.0",
|
||||
"dependencies": {
|
||||
"colors": "^1.4.0",
|
||||
"eslint-config-google": "^0.14.0",
|
||||
"getopts": "^2.3.0",
|
||||
"mysql": "^2.18.1",
|
||||
"mysql2": "^3.1.0",
|
||||
"node-fetch": "^2.6.7",
|
||||
"object-hash": "^3.0.0"
|
||||
},
|
||||
|
@ -14,5 +17,8 @@
|
|||
"grafana-find": "main.js",
|
||||
"gfind": "main.js"
|
||||
},
|
||||
"main": "main.js"
|
||||
"main": "main.js",
|
||||
"devDependencies": {
|
||||
"eslint": "^8.33.0"
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue