Files
harmony/examples/ms_teams_alert_channel/src/main.rs

66 lines
2.4 KiB
Rust

mod prometheus_msteams;
use harmony::{
interpret::InterpretError, inventory::Inventory, maestro::Maestro, modules::{helm::chart::HelmChartScore, monitoring::{kube_prometheus::{prometheus_alert_channel::PrometheusAlertChannel, types::{AlertChannelConfig, AlertChannelReceiver, AlertChannelRoute, WebhookConfig}}, monitoring_alerting::MonitoringAlertingScore}}, topology::K8sAnywhereTopology
};
use prometheus_msteams::prometheus_msteams_score;
use url::Url;
use serde::{Serialize, Deserialize};
#[tokio::main]
async fn main() {
let alert_channels: Vec<Box<dyn PrometheusAlertChannel>> = vec![Box::new(MSTeamsChannel {
connector: "teams-test".to_string(),
webhook_url: url::Url::parse(
"https://msteams.com/services/dummy/dummy/dummy",
)
.expect("invalid URL"),
})];
let monitoring_score = MonitoringAlertingScore {
alert_channels,
namespace: None,
};
let mut maestro = Maestro::<K8sAnywhereTopology>::initialize(
Inventory::autoload(),
K8sAnywhereTopology::new(),
)
.await
.unwrap();
maestro.register_all(vec![Box::new(monitoring_score)]);
harmony_cli::init(maestro, None).await.unwrap();
}
#[derive(Debug, Clone, Serialize, Deserialize)]
struct MSTeamsChannel {
connector: String,
webhook_url: Url,
}
#[typetag::serde]
impl PrometheusAlertChannel for MSTeamsChannel {
fn get_alert_manager_config_contribution(&self) -> Result<AlertChannelConfig, InterpretError> {
Ok(AlertChannelConfig{
receiver: AlertChannelReceiver{
name: format!("MSTeams-{}",self.connector),
slack_configs: None,
webhook_configs: Some(vec![WebhookConfig{
url: url::Url::parse("http://prometheus-msteams-prometheus-msteams.monitoring.svc.cluster.local:2000/alertmanager").expect("invalid url"),
send_resolved: true,}])
},
route: AlertChannelRoute{
receiver: format!("MSTeams-{}", self.connector),
matchers: vec!["alertname!=Watchdog".to_string()],
r#continue: true,
},
global_config: None, })
}
fn get_dependency_score(&self, ns: String) -> Option<HelmChartScore> {
Some(prometheus_msteams_score(self.connector.clone(), self.webhook_url.clone(), ns.clone()))
}
}