-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(agent): init alerts & notifiers (#2)
- Loading branch information
1 parent
7f280f4
commit c09381e
Showing
15 changed files
with
528 additions
and
128 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Import Third-party Dependencies | ||
import dayjs from "dayjs"; | ||
import { Logger } from "pino"; | ||
|
||
// Import Internal Dependencies | ||
import { DbRule, getDB } from "./database"; | ||
import { Notifier } from "./notifier"; | ||
import { SigynNotifiers, SigynRule, getConfig } from "./config"; | ||
|
||
export function createAlert(rule: DbRule, config: SigynRule, logger: Logger) { | ||
const notifier = Notifier.getNotifier(logger); | ||
const rulegNotifiers = config.notifiers ?? []; | ||
const globalNotifiers = Object.keys(getConfig().notifiers) as (keyof SigynNotifiers)[]; | ||
const notifierNames = rulegNotifiers.length > 0 ? rulegNotifiers : globalNotifiers; | ||
|
||
for (const notifierName of notifierNames) { | ||
notifier.sendAlert({ rule, notifier: notifierName }); | ||
} | ||
|
||
getDB().prepare("INSERT INTO alerts (ruleId, createdAt) VALUES (?, ?)").run( | ||
rule.id, | ||
dayjs().unix() | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Import Node.js Dependencies | ||
import path from "node:path"; | ||
import fs from "node:fs"; | ||
|
||
export interface SigynConfig { | ||
notifiers: SigynNotifiers; | ||
rules: SigynRule[] | ||
} | ||
|
||
export interface SigynNotifiers { | ||
discord?: DiscordNotifier; | ||
} | ||
|
||
export interface DiscordNotifier { | ||
webhookUrl: string; | ||
} | ||
|
||
export interface SigynRule { | ||
name: string; | ||
logql: string; | ||
polling: string; | ||
alert: SigynAlert; | ||
disabled?: boolean; | ||
notifiers?: (keyof SigynNotifiers)[]; | ||
} | ||
|
||
export interface SigynAlert { | ||
on: { | ||
count: number; | ||
interval: string; | ||
}, | ||
template: SigynAlertTemplate; | ||
} | ||
|
||
export interface SigynAlertTemplate { | ||
title?: string; | ||
content?: string[]; | ||
} | ||
|
||
let config: SigynConfig; | ||
|
||
export function initConfig(location: string): SigynConfig { | ||
const rawConfig = fs.readFileSync(path.join(location, "/config.json"), "utf-8"); | ||
|
||
config = JSON.parse(rawConfig); | ||
|
||
// TODO: verify configs format ? | ||
return config; | ||
} | ||
|
||
export function getConfig(): SigynConfig { | ||
if (config === undefined) { | ||
throw new Error("You must init config first"); | ||
} | ||
|
||
return config; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// NOTE: This will be redesigned & moved to @sigyn/discord. | ||
|
||
// CONSTANTS | ||
const kWebhookUsername = "Sigyn Agent"; | ||
|
||
export async function executeWebhook(options) { | ||
// pupa is ESM only, need a dynamic import for CommonJS. | ||
const { default: pupa } = await import("pupa"); | ||
const { webhookUrl, ruleConfig, rule } = options; | ||
|
||
function webhookStructure() { | ||
const counter = rule.counter; | ||
const { count, interval } = ruleConfig.alert.on; | ||
const ruleName = ruleConfig.name; | ||
const polling = ruleConfig.polling; | ||
const logql = ruleConfig.logql.replaceAll("`", "'"); | ||
const lokiUrl = "https://todo.com"; | ||
const templateData = { ruleName, count, counter, interval, polling, logql, lokiUrl }; | ||
|
||
const content: any[] = ruleConfig.alert.template.content.map((content) => pupa( | ||
content, | ||
templateData | ||
)); | ||
if (ruleConfig.alert.template.title) { | ||
content.unshift(pupa(ruleConfig.alert.template.title, templateData)); | ||
} | ||
|
||
return { | ||
content: content.join("\n"), | ||
username: kWebhookUsername | ||
}; | ||
} | ||
|
||
await fetch(webhookUrl, { | ||
method: "POST", | ||
body: JSON.stringify(webhookStructure()), | ||
headers: { | ||
"Content-Type": "application/json" | ||
} | ||
}); | ||
} |
Oops, something went wrong.