Skip to content

Commit

Permalink
Merge pull request #4 from shu-unifra/balance-checker
Browse files Browse the repository at this point in the history
Balance checker
  • Loading branch information
dghelm authored Oct 30, 2024
2 parents 923176a + bb38ca6 commit 0389b29
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ DESCRIPTION
EXAMPLES
$ scrollsdk setup configs
$ scrollsdk setup configs --image-tag v0.0.25
$ scrollsdk setup configs --image-tag gen-configs-2eba3d2c418b16f4a66d9baadeb1c1bafdca81b1
$ scrollsdk setup configs --configs-dir custom-configs
```
Expand Down
56 changes: 53 additions & 3 deletions src/commands/setup/configs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default class SetupConfigs extends Command {

static override examples = [
'<%= config.bin %> <%= command.id %>',
'<%= config.bin %> <%= command.id %> --image-tag v0.0.25',
'<%= config.bin %> <%= command.id %> --image-tag gen-configs-2eba3d2c418b16f4a66d9baadeb1c1bafdca81b1',
'<%= config.bin %> <%= command.id %> --configs-dir custom-configs',
]

Expand Down Expand Up @@ -427,7 +427,7 @@ export default class SetupConfigs extends Command {
}

private async getDockerImageTag(providedTag: string | undefined): Promise<string> {
const defaultTag = 'gen-configs-v0.0.25';
const defaultTag = 'gen-configs-2eba3d2c418b16f4a66d9baadeb1c1bafdca81b1';

if (!providedTag) {
return defaultTag;
Expand Down Expand Up @@ -519,6 +519,20 @@ export default class SetupConfigs extends Command {
}
}

try {
this.log(chalk.blue(`generating balance-checker alert rules file...`));
const scrollMonitorProductionFilePath = path.join(targetDir, "scroll-monitor-production.yaml");
const balanceCheckerConfigFilePath = path.join(targetDir, "balance-checker-config.yaml");
const addedAlertRules = this.generateAlertRules(balanceCheckerConfigFilePath);
const existingContent = fs.readFileSync(scrollMonitorProductionFilePath, 'utf8');
const existingYaml = yaml.load(existingContent) as any;
existingYaml["kube-prometheus-stack"].additionalPrometheusRules = addedAlertRules;
fs.writeFileSync(scrollMonitorProductionFilePath, yaml.dump(existingYaml, { indent: 2 }));
}
catch {
this.error(`generating balance-checker alert rules file failed`);
}

// Remove source files after all processing is complete
for (const mapping of fileMappings) {
const sourcePath = path.join(sourceDir, mapping.source);
Expand Down Expand Up @@ -705,4 +719,40 @@ export default class SetupConfigs extends Command {

this.log(chalk.green('Configuration setup completed.'))
}
}

private generateAlertRules(sourcePath: string): any {
const yamlContent = fs.readFileSync(sourcePath, 'utf8');
const parsedYaml = yaml.load(yamlContent) as any;
const jsonConfig = JSON.parse(parsedYaml.scrollConfig);
const { addresses } = jsonConfig;

const alertRules =
[
{
groups: [
{
name: "balance-cheker-group",
rules: addresses.map((item: { address: string, min_balance_ether: string; name: string; rpc_url: string }) => ({
alert: `ether_balance_of_${item.name}`,
annotations: {
description: `Balance of ${item.name} (${item.address}) is less than threshold ${item.min_balance_ether}`,
summary: `Balance of ${item.name} is less than threshold ${item.min_balance_ether}`
},
expr: `ether_balance_of_${item.name} < ${item.min_balance_ether}`,
for: '5m',
labels: {
severity: 'critical'
}
}))
}],
labels: {
release: "scroll-monitor",
role: "alert-rules"
},
name: "balance-cheker"
}
];
return alertRules;
}

}

0 comments on commit 0389b29

Please sign in to comment.