1
0
mirror of https://github.com/pcvolkmer/checkbar.git synced 2025-04-19 11:06:50 +00:00

Add check for plain TCP connections

This commit is contained in:
Paul-Christian Volkmer 2022-12-26 19:05:50 +01:00
parent 5e9018112c
commit 774a843005
2 changed files with 39 additions and 2 deletions

View File

@ -24,6 +24,11 @@ url = "https://host2.example.com"
name = "App 1"
url = "https://app.example.com/actuator/health"
check_type = "Actuator"
[[checks]]
name = "App 2"
url = "tcp://app.example.com:12345"
check_type = "Tcp"
----
Each host or application to be checked consists of `name` and `url`.
@ -33,8 +38,9 @@ You can optionally specify `check_type`:
* `Html`: Default value, checks if a request is successful and returns HTTP OK - 200.
* `Actuator`: Like `Html`, but checks if _Actuator_ shows that the application is up and running.
** `Up`: Actuator response indicates, application is up and running.
** `Warn`: Got response but it is not Actuator health `UP` response or redirect.
** `Warn`: Got response, but it is not Actuator health `UP` response or redirect.
** `Down`: No response.
* `Tcp`: Checks if TCP connection to given host and port can be established
To use more than one configuration, pass the config file location as first argument to the application.

View File

@ -1,10 +1,13 @@
use reqwest::Url;
use serde::Deserialize;
use serde_json::json;
use std::env;
use std::fmt::{Display, Formatter, Result};
use std::fs;
use std::process;
use std::str::FromStr;
use std::time::Duration;
use tokio::net::TcpStream;
use tokio::task;
#[derive(Deserialize)]
@ -29,10 +32,11 @@ struct CheckConfig {
click_cmd: Option<String>,
}
#[derive(Deserialize)]
#[derive(Deserialize, PartialEq)]
enum CheckType {
Http,
Actuator,
Tcp,
}
#[derive(Deserialize)]
@ -87,6 +91,33 @@ enum CheckState {
}
async fn check_host(check_config: &CheckConfig) -> CheckResult {
if check_config.check_type == Some(CheckType::Tcp) {
if let Ok(url) = Url::from_str(check_config.url.as_str()) {
if url.scheme() == "tcp" {
if url.host_str().is_some() && url.port().is_some() {
let state = match TcpStream::connect(format!(
"{}:{}",
url.host_str().unwrap(),
url.port().unwrap()
))
.await
{
Ok(_) => CheckState::Up,
_ => CheckState::Down,
};
return CheckResult {
name: check_config.name.to_string(),
state,
};
}
}
}
return CheckResult {
name: check_config.name.to_string(),
state: CheckState::Down,
};
}
let state = match reqwest::get(check_config.url.as_str()).await {
Ok(r) => {
if r.status().is_success() {