Skip to content

Commit

Permalink
Feature: Plugin status monitoring (#25)
Browse files Browse the repository at this point in the history
* Working status parsing for bash plugins

* Fix docs and add debug prints

* Add monitoring crate

* Send metrics to the monitoring gateway

* Add extra tags field

* Revert "Send metrics to the monitoring gateway"

This reverts commit 7f29c2b.

* Decouple telegraf monitoring from main loop

---------

Co-authored-by: Artem Starikov <contact@snejugal.ru>
  • Loading branch information
subatiq and snejugal authored Jul 23, 2024
1 parent 584ab21 commit b1aba5c
Show file tree
Hide file tree
Showing 13 changed files with 472 additions and 45 deletions.
105 changes: 105 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
workspace = { members = ["paws_config", "paws_install"] }
workspace = { members = ["paws_config", "paws_install", "paws_monitoring"] }
[package]
name = "kittypaws"
version = "0.1.0"
Expand All @@ -18,7 +18,10 @@ libloading = "0.5"
serde_yaml = "0.9.34"
paws_config = { path = "paws_config" }
paws_install = { path = "paws_install" }
paws_monitoring = { path = "paws_monitoring" }
iso8601 = { version = "0.6.1", features = ["serde"] }
serde = { version = "1.0.197", features = ["derive"] }
pyo3 = {"version" = "0.17.3", features = ["auto-initialize"]}
pyo3 = { version = "0.17.3", features = ["auto-initialize"] }
clap = { version = "4.5.4", features = ["derive"] }
uuid = { version = "1.10.0", features = ["v4"] }

12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,19 +100,21 @@ https://github.com/subatiq/kittypaws-timeburglar
```yaml
plugins:
- name: plugin01
config01: yes
config02: 42
options:
config01: yes
config02: 42
...

- name: plugin01:
config03: yes
config02: 44
options:
config03: yes
config02: 44
...
```

### Run duration

You can specify if the test run should stop after some time. To configure it, add
You can specify if the test run should stop after some time. To configure it, add

```yaml
duration: <Duration in ISO 8601 format>
Expand Down
10 changes: 6 additions & 4 deletions configs/all.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
deathloop:
target: ev_sensor
deathloop:
frequency: random
interval: PT1M
options:
target: ev_sensor
dropper:
target: ev_sensor
ip: 192.168.85.139
options:
target: ev_sensor
ip: 192.168.85.139
5 changes: 3 additions & 2 deletions configs/dropping.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
dropper:
target: ev_sensor
ip: 192.168.85.139
options:
target: ev_sensor
ip: 192.168.85.139
5 changes: 3 additions & 2 deletions configs/restarting.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
deathloop:
target: ev_sensor
deathloop:
options:
target: ev_sensor
frequency: random
interval: PT1M
26 changes: 19 additions & 7 deletions paws_config/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::{collections::HashMap, path::PathBuf};
use serde::Deserialize;

#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct Duration(std::time::Duration);

impl Duration {
Expand All @@ -26,7 +25,7 @@ impl<'de> Deserialize<'de> for Duration {
}
}

#[derive(Debug, Deserialize)]
#[derive(Debug, Deserialize, Clone)]
#[serde(rename_all = "snake_case")]
pub enum StartupOptions {
Hot,
Expand All @@ -35,13 +34,13 @@ pub enum StartupOptions {
Delayed(Duration),
}

#[derive(Debug, Deserialize)]
#[derive(Debug, Deserialize, Clone)]
pub struct RandomRange<T> {
pub min: T,
pub max: T,
}

#[derive(Debug, Deserialize)]
#[derive(Debug, Deserialize, Clone)]
#[serde(rename_all = "snake_case")]
pub enum FrequencyOptions {
Once,
Expand All @@ -51,17 +50,30 @@ pub enum FrequencyOptions {
Random(RandomRange<Duration>),
}

#[derive(Debug, Deserialize)]
#[derive(Debug, Deserialize, Clone)]
pub struct GlobalMonitoringOptions {
pub dsn: String,
pub extra_tags: Option<HashMap<String, String>>
}

#[derive(Debug, Deserialize, Clone)]
pub struct PluginMonitoringOptions {
pub frequency: FrequencyOptions,
pub extra_tags: Option<HashMap<String, String>>
}

#[derive(Debug, Deserialize, Clone)]
pub struct PluginConfig {
pub name: String,
pub startup: StartupOptions,
pub frequency: FrequencyOptions,
pub healthcheck: Option<String>,
pub monitoring: Option<PluginMonitoringOptions>,
pub options: Option<HashMap<String, String>>
}

#[derive(Debug, Deserialize)]
pub struct KittypawsConfig {
pub monitoring: Option<GlobalMonitoringOptions>,
pub duration: Option<Duration>,
pub plugins: Vec<PluginConfig>,
}
Expand Down
9 changes: 9 additions & 0 deletions paws_monitoring/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "paws_monitoring"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
telegraf = "0.6.0"
29 changes: 29 additions & 0 deletions paws_monitoring/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
pub mod telegraf;
use std::collections::HashMap;

use telegraf::TelegrafClient;

#[derive(Debug)]
pub enum StatusValue {
Int(i64),
Float(f64),
String(String),
}

pub enum MonitoringBackend {
Telegraf,
}

pub trait MetricSender: Send {
fn send_metric(
&mut self,
tags: HashMap<String, String>,
fields: HashMap<String, StatusValue>,
) -> Result<(), String>;
}

pub fn init_monitoring_backend(option: MonitoringBackend, dsn: &str) -> Box<dyn MetricSender> {
Box::new(match option {
MonitoringBackend::Telegraf => TelegrafClient::new(dsn).unwrap(),
})
}
Loading

0 comments on commit b1aba5c

Please sign in to comment.