/ src / models.rs
models.rs
  1  use serde::{Deserialize, Serialize};
  2  use std::collections::HashMap;
  3  
  4  pub const ALERT_VARIABLES: [(&str, &str); 11] = [
  5      ("sys", "cpu_usage"),
  6      ("sys", "mem_usage"),
  7      ("sys", "swap_usage"),
  8      ("sys", "load_avg_1"),
  9      ("sys", "load_avg_5"),
 10      ("sys", "load_avg_15"),
 11      ("net", "rx_rate"),
 12      ("net", "tx_rate"),
 13      ("disk", "read_rate"),
 14      ("disk", "write_rate"),
 15      ("disk", "disk_usage"),
 16  ];
 17  
 18  #[derive(Debug, Serialize, Deserialize)]
 19  pub struct ApiResponse<T> {
 20      pub success: bool,
 21      pub data: Option<T>,
 22      pub error: Option<String>,
 23  }
 24  
 25  #[derive(Clone, Serialize)]
 26  pub struct GeneralInfo {
 27      pub t: i64,
 28      pub sys: SystemInfo,
 29      pub mem: MemoryInfo,
 30      pub cpu: CpuInfo,
 31      pub net: NetworkInfo,
 32      pub disk: DisksInfo,
 33  }
 34  
 35  #[derive(Clone, Serialize)]
 36  pub struct SystemInfo {
 37      pub name: String,
 38      pub kernel_ver: String,
 39      pub os_ver: String,
 40      pub os_name: String,
 41      pub host_name: String,
 42      pub load_avg: Vec<f64>,
 43      pub uptime: u64,
 44  }
 45  
 46  #[derive(Clone, Serialize)]
 47  pub struct MemoryInfo {
 48      pub total_mem: u64,
 49      pub used_mem: u64,
 50      pub total_swap: u64,
 51      pub used_swap: u64,
 52  }
 53  
 54  #[derive(Clone, Serialize)]
 55  pub struct CpuInfo {
 56      pub count: usize,
 57      pub avg_usage: f32,
 58      pub usage: Vec<f32>,
 59  }
 60  
 61  #[derive(Clone, Serialize)]
 62  pub struct NetworkInfo {
 63      pub interfaces: Vec<NetworkInterface>,
 64  }
 65  
 66  #[derive(Clone, Serialize)]
 67  pub struct NetworkInterface {
 68      pub name: String,
 69      pub rx: u64,
 70      pub tx: u64,
 71  }
 72  
 73  #[derive(Clone, Serialize)]
 74  pub struct DisksInfo {
 75      pub disks: Vec<DiskInfo>,
 76  }
 77  
 78  #[derive(Clone, Serialize)]
 79  pub struct DiskInfo {
 80      pub fs: String,
 81      pub kind: String,
 82      pub total_space: u64,
 83      pub free_space: u64,
 84      pub mount_point: String,
 85      pub removable: bool,
 86      pub io: [u64; 4], // [read_bytes, write_bytes, total_read_bytes, total_write_bytes]
 87  }
 88  
 89  #[derive(Clone, Serialize)]
 90  pub struct ProcessesInfo {
 91      pub t: i64,
 92      pub processes: Vec<ProcessInfo>,
 93  }
 94  
 95  #[derive(Clone, Serialize)]
 96  pub struct ProcessInfo {
 97      pub pid: u32,
 98      pub runtime: u64,
 99      pub name: String,
100      pub mem: u64,
101      pub cpu: f32,
102      pub stat: String,
103      pub cmd: String,
104      pub env: String,
105  }
106  
107  #[derive(Debug, Clone, Serialize)]
108  pub struct DockerContainer {
109      pub id: String,
110      pub name: String,
111      pub image: String,
112      pub status: String,
113      pub state: String,
114      pub created: i64,
115      pub ports: Vec<DockerPort>,
116      pub cpu_usage: f64,
117      pub mem_usage: u64,
118      pub mem_limit: u64,
119      pub net_io: [u64; 2],
120      pub disk_io: [u64; 2],
121  }
122  
123  #[derive(Debug, Clone, Serialize)]
124  pub struct DockerPort {
125      pub ip: Option<String>,
126      pub priv_port: u16,
127      pub pub_port: Option<u16>,
128      pub protocol: String,
129  }
130  
131  #[derive(Debug, Clone, Serialize)]
132  pub struct DockerInfo {
133      pub t: i64,
134      pub containers: Vec<DockerContainer>,
135  }
136  
137  #[derive(Debug, Serialize, Deserialize)]
138  pub struct HistoricalSeries {
139      pub cat: String,
140      pub stype: String,
141      pub name: String,
142      pub timestamps: Vec<i64>,
143      pub values: Vec<f64>,
144  }
145  
146  #[derive(Debug, Serialize, Deserialize)]
147  pub struct HistoricalQueryOptions {
148      pub start_time: Option<i64>,
149      pub end_time: Option<i64>,
150      pub limit: Option<i64>,
151      pub resolution: String, // "second", "minute", "hour", "day"
152  }
153  
154  #[derive(Debug, Serialize, Deserialize)]
155  pub struct Alert {
156      pub id: String,
157      pub var: AlertVar,
158      pub threshold: f64,
159      pub operator: String,
160      pub time_window: i64,
161      pub enabled: bool,
162      pub firing: bool,
163      pub notif_methods: Vec<String>,
164  }
165  
166  #[derive(Debug, Serialize, Deserialize)]
167  pub struct AlertVar{
168      pub cat: String, // Category
169      pub var: String,   // variable name (ex. rx_rate)
170      pub resrc: String, // Resource name (ex. eth0)
171  }
172  
173  #[derive(Debug, Serialize, Deserialize)]
174  pub struct NotificationMethod {
175      pub id: String,
176      pub name: String,
177      pub kind: String,
178      pub enabled: bool,
179      pub config: NotificationConfig,
180  }
181  
182  #[derive(Debug, Serialize, Deserialize)]
183  pub enum NotificationConfig {
184      WebHook(WebHookNotif),
185      Email(EmailNotif),
186      Telegram(TelegramNotif),
187  }
188  
189  #[derive(Debug, Serialize, Deserialize)]
190  pub struct WebHookNotif {
191      pub url: String,
192      pub method: String,
193      pub headers: HashMap<String, String>,
194      pub body: String,
195  }
196  
197  #[derive(Debug, Serialize, Deserialize)]
198  pub struct EmailNotif {
199      pub server: String,
200      pub port: u16,
201      pub username: String,
202      pub password: String,
203      pub from: String,
204      pub to: String,
205      pub subject: String,
206      pub body: String,
207  }
208  
209  #[derive(Debug, Serialize, Deserialize)]
210  pub struct TelegramNotif {
211      pub token: String,
212      pub chat_id: String,
213  }