Commit ebbcf2fb authored by Johannes Barthel's avatar Johannes Barthel
Browse files

initial version

parents
This diff is collapsed.
[package]
name = "freeswitch-watchdog"
version = "0.1.0"
authors = ["Johannes Barthel <johannes.barthel@student.kit.edu>"]
edition = "2018"
license = "MIT OR Apache-2.0"
[dependencies]
reqwest = { version = "0.10", features = ["blocking"] }
anyhow = "1.0.31"
[package.metadata.deb]
depends = "$auto, systemd"
section = "httpd"
priority = "optional"
assets = [
["freeswitch-watchdog.service", "/lib/systemd/system/", "644"],
["target/release/freeswitch-watchdog", "usr/bin/", "755"]
]
[Unit]
Description=watchdog for obscure freeswitch bug that prevents websocket connections
ConditionPathExists=/usr/bin/freeswitch-watchdog
After=network.target
Requires=network.target
[Service]
Type=simple
User=root
Group=root
Restart=on-failure
RestartSec=10
startLimitIntervalSec=60
WorkingDirectory=/tmp
ExecStart=/usr/bin/freeswitch-watchdog
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=freeswitch-watchdog
[Install]
WantedBy=multi-user.target
use std::process::Command;
fn sleep_secs(s : u64) {
std::thread::sleep(std::time::Duration::from_secs(s));
}
fn is_freeswitch_active() -> bool {
let output = Command::new("systemctl").args(&["show", "freeswitch.service", "--no-page"]).output().expect("could not run systemctl");
let output = String::from_utf8(output.stdout).expect("systemctl output is not valid utf-8");
let is_running = output.contains("SubState=running");
is_running
}
fn is_freeswitch_working (url : &str) -> bool {
let mut tries_left = 3;
while tries_left > 0 {
let res = reqwest::blocking::Client::new()
.get(url)
.timeout(std::time::Duration::from_secs(10))
.send();
let mut is_err = false;
if res.is_err() {
is_err = true;
} else if let Ok(res) = res {
if res.status() != 400 {
is_err = true;
}
}
if is_err == false {
return true
}
tries_left -= 1;
println!("Error polling freeswitch, trying again in 2 seconds…");
sleep_secs(2);
}
return false;
}
fn restart_freeswitch () {
loop {
println!("restarting services…");
let status = Command::new("systemctl").args(&["restart", "freeswitch.service", "bbb-fsesl-akka.service"]).status();
let mut restarted = false;
if status.is_ok() {
if let Ok(ref status) = status {
if status.success() {
restarted = true;
}
}
}
if restarted {
println!("restart complete, waiting 10 minutes");
sleep_secs(10 * 60);
return;
} else {
println!("error restarting services:\n {:?}", status);
println!("trying again in 30s");
sleep_secs(30);
}
}
}
fn main() {
let hostname = Command::new("hostname").arg("-f").output().expect("could not get FQDN");
let hostname = String::from_utf8(hostname.stdout).expect("hostname is not valid utf-8");
let hostname = hostname.trim();
let url = format!("https://{}/ws", hostname);
// println!("waiting for 10 minutes before beginning checks");
// sleep_secs(10 * 60);
// println!("now starting to periodically poll freeswitch");
loop {
// wait until freeswitch is runnning
while !is_freeswitch_active() {
sleep_secs(30);
}
// check for freeswitch status
let is_err = is_freeswitch_working(&url);
if is_err {
println!("freeswitch is in invalid state");
restart_freeswitch();
} else {
// everything fine, poll again in 30s
sleep_secs(30);
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment