]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/frontend/src/app/shared/services/prometheus-silence-matcher.service.ts
7aec6d1d37ccc362e99fed2d28f8e7106b3f7ec2
[ceph.git] / ceph / src / pybind / mgr / dashboard / frontend / src / app / shared / services / prometheus-silence-matcher.service.ts
1 import { Injectable } from '@angular/core';
2
3 import _ from 'lodash';
4
5 import {
6 AlertmanagerSilenceMatcher,
7 AlertmanagerSilenceMatcherMatch
8 } from '../models/alertmanager-silence';
9 import { PrometheusRule } from '../models/prometheus-alerts';
10
11 @Injectable({
12 providedIn: 'root'
13 })
14 export class PrometheusSilenceMatcherService {
15 private valueAttributePath = {
16 alertname: 'name',
17 instance: 'alerts.0.labels.instance',
18 job: 'alerts.0.labels.job',
19 severity: 'labels.severity'
20 };
21
22 singleMatch(
23 matcher: AlertmanagerSilenceMatcher,
24 rules: PrometheusRule[]
25 ): AlertmanagerSilenceMatcherMatch {
26 return this.multiMatch([matcher], rules);
27 }
28
29 multiMatch(
30 matchers: AlertmanagerSilenceMatcher[],
31 rules: PrometheusRule[]
32 ): AlertmanagerSilenceMatcherMatch {
33 if (matchers.some((matcher) => matcher.isRegex)) {
34 return undefined;
35 }
36 matchers.forEach((matcher) => {
37 rules = this.getMatchedRules(matcher, rules);
38 });
39 return this.describeMatch(rules);
40 }
41
42 private getMatchedRules(
43 matcher: AlertmanagerSilenceMatcher,
44 rules: PrometheusRule[]
45 ): PrometheusRule[] {
46 const attributePath = this.getAttributePath(matcher.name);
47 return rules.filter((r) => _.get(r, attributePath) === matcher.value);
48 }
49
50 private describeMatch(rules: PrometheusRule[]): AlertmanagerSilenceMatcherMatch {
51 let alerts = 0;
52 rules.forEach((r) => (alerts += r.alerts.length));
53 return {
54 status: this.getMatchText(rules.length, alerts),
55 cssClass: alerts ? 'has-success' : 'has-warning'
56 };
57 }
58
59 getAttributePath(name: string): string {
60 return this.valueAttributePath[name];
61 }
62
63 private getMatchText(rules: number, alerts: number): string {
64 const msg = {
65 noRule: $localize`Your matcher seems to match no currently defined rule or active alert.`,
66 noAlerts: $localize`no active alerts`,
67 alert: $localize`1 active alert`,
68 alerts: $localize`${alerts} active alerts`,
69 rule: $localize`Matches 1 rule`,
70 rules: $localize`Matches ${rules} rules`
71 };
72
73 const rule = rules > 1 ? msg.rules : msg.rule;
74 const alert = alerts ? (alerts > 1 ? msg.alerts : msg.alert) : msg.noAlerts;
75
76 return rules ? $localize`${rule} with ${alert}.` : msg.noRule;
77 }
78 }