Created
May 22, 2024 23:20
-
-
Save CommanderStorm/03c4a49d1161f5f5ea5e97e9cd9c7d1c to your computer and use it in GitHub Desktop.
Bulk-Create monitors for uptime kuma
This file contains hidden or 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
[package] | |
name = "kuma-add" | |
version = "0.1.0" | |
edition = "2021" | |
[dependencies] | |
kuma-client = "0.6.0" | |
tokio = { version = "1.37.0", features = ["full"] } |
This file contains hidden or 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
use kuma_client::{ | |
status_page::StatusPage, | |
monitor::{MonitorGroup, MonitorHttp}, | |
tag::{Tag, TagDefinition}, | |
Client, Config, Url, | |
}; | |
use kuma_client::monitor::MonitorType; | |
use kuma_client::status_page::{PublicGroup, PublicGroupMonitor}; | |
const MONITOR_CNT:usize=200; | |
const STATUS_PAGE_CHUNKS:usize=100; | |
#[tokio::main()] | |
async fn main() { | |
// Connect to the server | |
let client = Client::connect(Config { | |
url: Url::parse("http://localhost:3001").expect("Invalid URL"), | |
username: Some("admin".to_owned()), | |
password: Some("fL@JYfPMy9c9FtL".to_owned()), | |
..Default::default() | |
}) | |
.await | |
.expect("Failed to connect to server"); | |
// Create a tag | |
let tag_definition = client | |
.add_tag(TagDefinition { | |
name: Some("example_tag".to_owned()), | |
color: Some("red".to_owned()), | |
..Default::default() | |
}) | |
.await | |
.expect("Failed to add tag"); | |
// Create a group | |
let group = client | |
.add_monitor(MonitorGroup { | |
name: Some("Example Group".to_owned()), | |
tags: vec![Tag { | |
tag_id: tag_definition.tag_id, | |
value: Some("example_group".to_owned()), | |
..Default::default() | |
}], | |
..Default::default() | |
}) | |
.await | |
.expect("Failed to add group"); | |
// Create a monitor | |
for i in 1..MONITOR_CNT { | |
println!("added monitor {}", i); | |
client | |
.add_monitor(MonitorHttp { | |
name: Some(format!("Monitor Name{}", i)), | |
url: Some("https://example.com".to_owned()), | |
parent: group.common().id().clone(), | |
tags: vec![Tag { | |
tag_id: tag_definition.tag_id, | |
value: Some("example_monitor".to_owned()), | |
..Default::default() | |
}], | |
..Default::default() | |
}) | |
.await | |
.expect("Failed to add monitor"); | |
} | |
let monitors = client.get_monitors().await.expect("Failed to get monitors"); | |
println!("amount of monitors: {}", monitors.len()); | |
client.add_status_page(StatusPage { | |
id: None, | |
slug: Some("tmp".to_owned()), | |
title: Some("tmp".to_owned()), | |
description: None, | |
icon: Some("https://www.cloudron.io/store/icons/louislam.uptimekuma.app.png".to_owned()), | |
theme: None, | |
published: None, | |
show_tags: None, | |
domain_name_list: vec![], | |
custom_css: None, | |
footer_text: None, | |
show_powered_by: None, | |
google_analytics_id: None, | |
show_certificate_expiry: None, | |
public_group_list: Some( | |
(2..MONITOR_CNT).collect::<Vec<usize>>().chunks(STATUS_PAGE_CHUNKS).map(|ids| | |
PublicGroup { | |
name: Some("group1".into()), | |
monitor_list: ids.map(|i| PublicGroupMonitor { | |
name: Some(format!("monitor {i}")), | |
weight: None, | |
id: Some(i), | |
monitor_type: Some(MonitorType::Http), | |
}).collect(), | |
id:None, | |
weight:None, | |
}) | |
), | |
}).await.unwrap(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment