UNPKG

@porosys/pss

Version:

Porosys Server Setup (pss): General-purpose server setup and automation tool (including Netdata management)

66 lines (61 loc) 2.12 kB
import path from 'path'; import { getDirname } from '../../../../utils/dirname-from-import-meta.util'; import { sshBruteforceAlertDeps } from './setup-ssh-bruteforce-alert.constant'; import { setupAlert } from '../setup-alert/setup-alert.helper'; import { SetupSshBruteforceAlertArgs } from './setup-ssh-bruteforce-alert.type'; const dirname = getDirname(import.meta); export const setupSshBruteforceAlert = async ({ alertName, threshold, timeWindow, }: SetupSshBruteforceAlertArgs) => { const config = await (await import('../../../config/config.helper')).loadConfig(); const healthdPath = config?.healthdPath; const healthScriptsPath = config?.healthScriptsPath; if (!healthdPath || !healthScriptsPath) { throw new Error( 'healthdPath and healthScriptsPath must be set in config before calling setupSshBruteforceAlert.', ); } const baseAlertDir = path.join(healthScriptsPath, alertName); const scriptPath = `${baseAlertDir}/update-${alertName}-status.sh`; const healthAlertPath = path.join(healthdPath, `${alertName}.conf`); const scriptDir = path.join(dirname); const metricName = `${alertName.replace(/_/g, '.')}.detected`; const info = `SSH brute force attack detected!`; await setupAlert({ alertName, dependencies: sshBruteforceAlertDeps, templates: [ { templatePath: path.join( scriptDir, 'update-ssh-bruteforce-status.template.sh', ), destinationPath: scriptPath, replacements: { __THRESHOLD__: threshold.toString(), __TIME_WINDOW__: timeWindow.toString(), __METRIC_NAME__: metricName, }, mode: 0o755, }, { templatePath: path.join( scriptDir, 'ssh-bruteforce-alert.template.conf', ), destinationPath: healthAlertPath, replacements: { __ALERT_NAME__: alertName, __ON__: `statsd_${metricName}_gauge`, __INFO__: info, }, }, ], cronScriptPath: scriptPath, healthdPath, healthScriptsPath, checkFiles: [healthAlertPath], }); };