Skip to content

Instantly share code, notes, and snippets.

@Lobbyra
Last active May 13, 2022 12:04
Show Gist options
  • Save Lobbyra/2ab3e18ca4be5ce5a19b0d261935361b to your computer and use it in GitHub Desktop.
Save Lobbyra/2ab3e18ca4be5ce5a19b0d261935361b to your computer and use it in GitHub Desktop.
Code of a AWS lambda used as proxy for a grafana to telegram alert
const CHAT_ID = -100424242422442442244242;
const TELEGRAM_TOKEN = "";
const TELEGRAM_HOST = "api.telegram.org";
const SPREADSHEET_ID = ""
const GOOGLE_CLOUD_KEY = ""
const nameUrlMap = new Map();
const https = require('node:https');
async function fetchSheet() {
return new Promise((resolve, reject) => {
const httpsOptions = {
host: "sheets.googleapis.com",
path: `/v4/spreadsheets/${SPREADSHEET_ID}?key=${GOOGLE_CLOUD_KEY}&includeGridData=true`,
port: 443,
method: 'GET',
};
const req = https.request(httpsOptions, (res) => {
if (res.statusCode < 200 || res.statusCode >= 300) {
return reject(new Error('statusCode=' + res.statusCode));
}
var body = [];
res.on('data', function(chunk) {
body.push(chunk);
});
res.on('end', function() {
try {
body = JSON.parse(Buffer.concat(body).toString());
} catch(e) {
reject(e);
}
console.log(body);
resolve(body.sheets[0].data[0].rowData);
});
});
req.on('error', (e) => {
reject(e.message);
});
req.end();
});
}
async function sendMsg(msg) {
var telegramPath;
var httpsOptions = {};
console.log(msg);
msg = encodeURIComponent(msg);
telegramPath = `/bot${TELEGRAM_TOKEN}/sendMessage?chat_id=${CHAT_ID}&text=${msg}`;
return new Promise((resolve, reject) => {
const httpsOptions = {
host: TELEGRAM_HOST,
path: telegramPath,
port: 443,
method: 'POST',
};
const req = https.request(httpsOptions, (res) => {
if (res.statusCode < 200 || res.statusCode >= 300) {
return reject(new Error('statusCode=' + res.statusCode));
}
var body = [];
res.on('data', function(chunk) {
body.push(chunk);
});
res.on('end', function() {
try {
body = JSON.parse(Buffer.concat(body).toString());
} catch(e) {
reject(e);
}
resolve(body);
});
});
req.on('error', (e) => {
reject(e.message);
});
// send the request
req.end();
});
}
async function setNameUrlMap() {
var colA = "";
var colB = "";
await fetchSheet().then((data) => {
for (const [i, row] of data.entries()) {
if (i === 0) {
continue ;
}
colA = row.values[0].effectiveValue.stringValue;
colB = row.values[1].effectiveValue.stringValue;
nameUrlMap.set(colB, colA);
}
});
}
exports.handler = async (event) => {
var fullAlert = JSON.parse(event.body);
var alertPayload = fullAlert.alerts[0].valueString;
if (fullAlert.receiver == "") {
await sendMsg("GRAFANA: test");
return ;
}
var regexUrlFinder = /\{.*url\=(.*)\}/gm;
var url = regexUrlFinder.exec(alertPayload)[1];
var status = fullAlert.status;
var msg = "ALERTE PING ";
var monitoringName = "";
if (status == "resolved") {
msg += "✅\n";
} else if (status == "firing") {
msg += "🔥\n";
}
await setNameUrlMap();
monitoringName = nameUrlMap.get(url);
if (monitoringName == undefined) {
monitoringName = "Nom du monitoring inconnu\n";
}
msg += `- Nom du monitoring = ${monitoringName}\n`;
msg += `- URL = [${url}]`;
console.log(nameUrlMap);
await sendMsg(msg).then((data) => {})
const response = {
statusCode: 200,
};
return response;
};
@Lobbyra
Copy link
Author

Lobbyra commented May 12, 2022

Sample

Capture d’écran 2022-05-12 à 09 19 57

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment