2021-05-12 09:53:49 +00:00
|
|
|
use std::sync::{Mutex, Arc};
|
2020-07-21 09:10:36 +00:00
|
|
|
use std::path::{Path, PathBuf};
|
2020-10-19 11:59:33 +00:00
|
|
|
use std::os::unix::io::AsRawFd;
|
2019-11-21 13:36:28 +00:00
|
|
|
|
2020-04-17 12:11:25 +00:00
|
|
|
use anyhow::{bail, format_err, Error};
|
2019-11-21 13:36:28 +00:00
|
|
|
use futures::*;
|
2021-09-21 05:58:47 +00:00
|
|
|
use http::request::Parts;
|
|
|
|
use http::Response;
|
|
|
|
use hyper::{Body, StatusCode};
|
|
|
|
use hyper::header;
|
|
|
|
use url::form_urlencoded;
|
2021-01-19 10:37:49 +00:00
|
|
|
|
2019-11-21 13:36:28 +00:00
|
|
|
use openssl::ssl::{SslMethod, SslAcceptor, SslFiletype};
|
2021-01-11 08:50:04 +00:00
|
|
|
use tokio_stream::wrappers::ReceiverStream;
|
2021-09-21 05:58:47 +00:00
|
|
|
use serde_json::{json, Value};
|
2019-11-21 13:36:28 +00:00
|
|
|
|
2020-01-21 11:28:01 +00:00
|
|
|
use proxmox::try_block;
|
2019-11-21 13:36:28 +00:00
|
|
|
use proxmox::api::RpcEnvironmentType;
|
2021-05-14 13:44:51 +00:00
|
|
|
use proxmox::sys::linux::socket::set_tcp_keepalive;
|
2021-09-21 05:58:40 +00:00
|
|
|
use proxmox::tools::fs::CreateOptions;
|
|
|
|
|
|
|
|
use proxmox_rest_server::ApiConfig;
|
2019-11-21 13:36:28 +00:00
|
|
|
|
2020-10-28 06:33:05 +00:00
|
|
|
use proxmox_backup::{
|
|
|
|
backup::DataStore,
|
|
|
|
server::{
|
2021-03-31 10:21:51 +00:00
|
|
|
auth::default_api_auth,
|
2020-10-28 06:33:05 +00:00
|
|
|
WorkerTask,
|
|
|
|
rest::*,
|
|
|
|
jobstate::{
|
|
|
|
self,
|
|
|
|
Job,
|
|
|
|
},
|
|
|
|
rotate_task_log_archive,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2021-07-06 09:56:35 +00:00
|
|
|
use pbs_buildcfg::configdir;
|
2021-08-31 12:01:03 +00:00
|
|
|
use pbs_systemd::time::{compute_next_event, parse_calendar_event};
|
2021-09-01 10:21:51 +00:00
|
|
|
use pbs_tools::logrotate::LogRotate;
|
2020-10-28 06:33:05 +00:00
|
|
|
|
2021-09-10 07:21:27 +00:00
|
|
|
use pbs_api_types::{
|
|
|
|
Authid, TapeBackupJobConfig, VerificationJobConfig, SyncJobConfig, DataStoreConfig,
|
|
|
|
PruneOptions,
|
|
|
|
};
|
2021-09-10 06:40:58 +00:00
|
|
|
|
2021-09-21 05:58:41 +00:00
|
|
|
use proxmox_rest_server::daemon;
|
|
|
|
|
2019-04-08 12:00:23 +00:00
|
|
|
use proxmox_backup::server;
|
2019-01-29 16:21:58 +00:00
|
|
|
use proxmox_backup::auth_helpers::*;
|
2020-10-19 11:59:33 +00:00
|
|
|
use proxmox_backup::tools::{
|
2021-05-14 13:44:51 +00:00
|
|
|
PROXMOX_BACKUP_TCP_KEEPALIVE_TIME,
|
2020-10-19 11:59:33 +00:00
|
|
|
disks::{
|
|
|
|
DiskManage,
|
|
|
|
zfs_pool_stats,
|
2021-04-12 09:24:29 +00:00
|
|
|
get_pool_from_dataset,
|
2020-10-19 11:59:33 +00:00
|
|
|
},
|
|
|
|
};
|
2019-01-28 12:17:03 +00:00
|
|
|
|
2021-09-10 06:40:58 +00:00
|
|
|
|
2020-08-13 08:29:19 +00:00
|
|
|
use proxmox_backup::api2::pull::do_sync_job;
|
2021-02-15 06:55:13 +00:00
|
|
|
use proxmox_backup::api2::tape::backup::do_tape_backup_job;
|
2020-10-28 06:33:05 +00:00
|
|
|
use proxmox_backup::server::do_verification_job;
|
2020-10-30 08:07:24 +00:00
|
|
|
use proxmox_backup::server::do_prune_job;
|
2020-08-13 08:29:19 +00:00
|
|
|
|
2020-08-26 08:24:32 +00:00
|
|
|
fn main() -> Result<(), Error> {
|
2020-06-15 08:38:30 +00:00
|
|
|
proxmox_backup::tools::setup_safe_path_env();
|
|
|
|
|
2021-09-02 10:47:11 +00:00
|
|
|
let backup_uid = pbs_config::backup_user()?.uid;
|
|
|
|
let backup_gid = pbs_config::backup_group()?.gid;
|
2020-08-26 08:26:20 +00:00
|
|
|
let running_uid = nix::unistd::Uid::effective();
|
|
|
|
let running_gid = nix::unistd::Gid::effective();
|
|
|
|
|
|
|
|
if running_uid != backup_uid || running_gid != backup_gid {
|
|
|
|
bail!("proxy not running as backup user or group (got uid {} gid {})", running_uid, running_gid);
|
|
|
|
}
|
|
|
|
|
2021-07-06 10:08:44 +00:00
|
|
|
pbs_runtime::main(run())
|
2019-02-11 13:43:26 +00:00
|
|
|
}
|
|
|
|
|
2021-09-21 05:58:47 +00:00
|
|
|
fn get_index(
|
|
|
|
auth_id: Option<String>,
|
|
|
|
language: Option<String>,
|
|
|
|
api: &ApiConfig,
|
|
|
|
parts: Parts,
|
|
|
|
) -> Response<Body> {
|
|
|
|
|
|
|
|
let (userid, csrf_token) = match auth_id {
|
|
|
|
Some(auth_id) => {
|
|
|
|
let auth_id = auth_id.parse::<Authid>();
|
|
|
|
match auth_id {
|
|
|
|
Ok(auth_id) if !auth_id.is_token() => {
|
|
|
|
let userid = auth_id.user().clone();
|
|
|
|
let new_csrf_token = assemble_csrf_prevention_token(csrf_secret(), &userid);
|
|
|
|
(Some(userid), Some(new_csrf_token))
|
|
|
|
}
|
|
|
|
_ => (None, None)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None => (None, None),
|
|
|
|
};
|
|
|
|
|
|
|
|
let nodename = proxmox::tools::nodename();
|
|
|
|
let user = userid.as_ref().map(|u| u.as_str()).unwrap_or("");
|
|
|
|
|
|
|
|
let csrf_token = csrf_token.unwrap_or_else(|| String::from(""));
|
|
|
|
|
|
|
|
let mut debug = false;
|
|
|
|
let mut template_file = "index";
|
|
|
|
|
|
|
|
if let Some(query_str) = parts.uri.query() {
|
|
|
|
for (k, v) in form_urlencoded::parse(query_str.as_bytes()).into_owned() {
|
|
|
|
if k == "debug" && v != "0" && v != "false" {
|
|
|
|
debug = true;
|
|
|
|
} else if k == "console" {
|
|
|
|
template_file = "console";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut lang = String::from("");
|
|
|
|
if let Some(language) = language {
|
|
|
|
if Path::new(&format!("/usr/share/pbs-i18n/pbs-lang-{}.js", language)).exists() {
|
|
|
|
lang = language;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let data = json!({
|
|
|
|
"NodeName": nodename,
|
|
|
|
"UserName": user,
|
|
|
|
"CSRFPreventionToken": csrf_token,
|
|
|
|
"language": lang,
|
|
|
|
"debug": debug,
|
|
|
|
});
|
|
|
|
|
|
|
|
let (ct, index) = match api.render_template(template_file, &data) {
|
|
|
|
Ok(index) => ("text/html", index),
|
|
|
|
Err(err) => ("text/plain", format!("Error rendering template: {}", err)),
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut resp = Response::builder()
|
|
|
|
.status(StatusCode::OK)
|
|
|
|
.header(header::CONTENT_TYPE, ct)
|
|
|
|
.body(index.into())
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
if let Some(userid) = userid {
|
|
|
|
resp.extensions_mut().insert(Authid::from((userid, None)));
|
|
|
|
}
|
|
|
|
|
|
|
|
resp
|
|
|
|
}
|
|
|
|
|
2019-08-29 07:45:34 +00:00
|
|
|
async fn run() -> Result<(), Error> {
|
2019-01-28 12:17:03 +00:00
|
|
|
if let Err(err) = syslog::init(
|
|
|
|
syslog::Facility::LOG_DAEMON,
|
|
|
|
log::LevelFilter::Info,
|
|
|
|
Some("proxmox-backup-proxy")) {
|
2019-02-11 13:43:26 +00:00
|
|
|
bail!("unable to inititialize syslog - {}", err);
|
2019-01-28 12:17:03 +00:00
|
|
|
}
|
|
|
|
|
2020-11-04 05:52:10 +00:00
|
|
|
// Note: To debug early connection error use
|
|
|
|
// PROXMOX_DEBUG=1 ./target/release/proxmox-backup-proxy
|
|
|
|
let debug = std::env::var("PROXMOX_DEBUG").is_ok();
|
|
|
|
|
2019-01-29 16:21:58 +00:00
|
|
|
let _ = public_auth_key(); // load with lazy_static
|
|
|
|
let _ = csrf_secret(); // load with lazy_static
|
|
|
|
|
2019-01-28 12:17:03 +00:00
|
|
|
let mut config = ApiConfig::new(
|
2021-07-06 09:56:35 +00:00
|
|
|
pbs_buildcfg::JS_DIR,
|
2021-03-31 10:21:51 +00:00
|
|
|
&proxmox_backup::api2::ROUTER,
|
|
|
|
RpcEnvironmentType::PUBLIC,
|
|
|
|
default_api_auth(),
|
2021-09-21 05:58:47 +00:00
|
|
|
get_index,
|
2021-03-31 10:21:51 +00:00
|
|
|
)?;
|
2019-01-28 12:17:03 +00:00
|
|
|
|
|
|
|
config.add_alias("novnc", "/usr/share/novnc-pve");
|
|
|
|
config.add_alias("extjs", "/usr/share/javascript/extjs");
|
2020-11-19 10:10:02 +00:00
|
|
|
config.add_alias("qrcodejs", "/usr/share/javascript/qrcodejs");
|
2019-01-28 12:17:03 +00:00
|
|
|
config.add_alias("fontawesome", "/usr/share/fonts-font-awesome");
|
|
|
|
config.add_alias("xtermjs", "/usr/share/pve-xtermjs");
|
2020-09-07 12:33:05 +00:00
|
|
|
config.add_alias("locale", "/usr/share/pbs-i18n");
|
2019-01-28 12:17:03 +00:00
|
|
|
config.add_alias("widgettoolkit", "/usr/share/javascript/proxmox-widget-toolkit");
|
2019-12-16 17:16:06 +00:00
|
|
|
config.add_alias("docs", "/usr/share/doc/proxmox-backup/html");
|
2019-01-28 12:17:03 +00:00
|
|
|
|
2021-07-06 09:56:35 +00:00
|
|
|
let mut indexpath = PathBuf::from(pbs_buildcfg::JS_DIR);
|
2020-07-21 09:10:36 +00:00
|
|
|
indexpath.push("index.hbs");
|
|
|
|
config.register_template("index", &indexpath)?;
|
2020-07-21 09:10:39 +00:00
|
|
|
config.register_template("console", "/usr/share/pve-xtermjs/index.html.hbs")?;
|
2020-07-21 09:10:36 +00:00
|
|
|
|
2021-09-21 05:58:40 +00:00
|
|
|
let backup_user = pbs_config::backup_user()?;
|
|
|
|
let mut commando_sock = proxmox_rest_server::CommandoSocket::new(crate::server::our_ctrl_sock(), backup_user.gid);
|
|
|
|
|
|
|
|
let dir_opts = CreateOptions::new().owner(backup_user.uid).group(backup_user.gid);
|
|
|
|
let file_opts = CreateOptions::new().owner(backup_user.uid).group(backup_user.gid);
|
2020-11-02 18:13:36 +00:00
|
|
|
|
2021-09-21 05:58:40 +00:00
|
|
|
config.enable_file_log(
|
|
|
|
pbs_buildcfg::API_ACCESS_LOG_FN,
|
|
|
|
Some(dir_opts),
|
|
|
|
Some(file_opts),
|
|
|
|
&mut commando_sock,
|
|
|
|
)?;
|
2020-10-16 09:06:46 +00:00
|
|
|
|
2019-01-28 12:17:03 +00:00
|
|
|
let rest_server = RestServer::new(config);
|
|
|
|
|
2019-07-02 11:33:58 +00:00
|
|
|
//openssl req -x509 -newkey rsa:4096 -keyout /etc/proxmox-backup/proxy.key -out /etc/proxmox-backup/proxy.pem -nodes
|
|
|
|
|
2021-05-12 09:53:49 +00:00
|
|
|
// we build the initial acceptor here as we cannot start if this fails
|
2021-05-11 13:53:57 +00:00
|
|
|
let acceptor = make_tls_acceptor()?;
|
2021-05-12 09:53:49 +00:00
|
|
|
let acceptor = Arc::new(Mutex::new(acceptor));
|
2019-07-02 11:33:58 +00:00
|
|
|
|
2021-05-12 09:53:49 +00:00
|
|
|
// to renew the acceptor we just add a command-socket handler
|
2021-05-11 13:53:58 +00:00
|
|
|
commando_sock.register_command(
|
|
|
|
"reload-certificate".to_string(),
|
|
|
|
{
|
2021-05-12 09:53:49 +00:00
|
|
|
let acceptor = Arc::clone(&acceptor);
|
2021-05-11 13:53:58 +00:00
|
|
|
move |_value| -> Result<_, Error> {
|
2021-05-12 09:53:49 +00:00
|
|
|
log::info!("reloading certificate");
|
|
|
|
match make_tls_acceptor() {
|
|
|
|
Err(err) => log::error!("error reloading certificate: {}", err),
|
|
|
|
Ok(new_acceptor) => {
|
|
|
|
let mut guard = acceptor.lock().unwrap();
|
|
|
|
*guard = new_acceptor;
|
|
|
|
}
|
|
|
|
}
|
2021-05-11 13:53:58 +00:00
|
|
|
Ok(Value::Null)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)?;
|
2019-02-04 13:56:07 +00:00
|
|
|
|
2021-06-02 11:27:01 +00:00
|
|
|
// to remove references for not configured datastores
|
|
|
|
commando_sock.register_command(
|
|
|
|
"datastore-removed".to_string(),
|
|
|
|
|_value| {
|
|
|
|
if let Err(err) = proxmox_backup::backup::DataStore::remove_unused_datastores() {
|
|
|
|
log::error!("could not refresh datastores: {}", err);
|
|
|
|
}
|
|
|
|
Ok(Value::Null)
|
|
|
|
}
|
|
|
|
)?;
|
|
|
|
|
2019-03-18 13:13:44 +00:00
|
|
|
let server = daemon::create_daemon(
|
|
|
|
([0,0,0,0,0,0,0,0], 8007).into(),
|
2021-05-11 13:53:58 +00:00
|
|
|
move |listener, ready| {
|
2020-10-19 11:59:33 +00:00
|
|
|
|
2021-05-12 09:53:49 +00:00
|
|
|
let connections = accept_connections(listener, acceptor, debug);
|
2021-01-11 08:50:04 +00:00
|
|
|
let connections = hyper::server::accept::from_stream(ReceiverStream::new(connections));
|
2019-09-02 13:16:21 +00:00
|
|
|
|
|
|
|
Ok(ready
|
2020-11-03 12:14:10 +00:00
|
|
|
.and_then(|_| hyper::Server::builder(connections)
|
2019-09-02 13:16:21 +00:00
|
|
|
.serve(rest_server)
|
2021-09-21 05:58:40 +00:00
|
|
|
.with_graceful_shutdown(proxmox_rest_server::shutdown_future())
|
2019-09-02 13:16:21 +00:00
|
|
|
.map_err(Error::from)
|
|
|
|
)
|
|
|
|
.map_err(|err| eprintln!("server error: {}", err))
|
|
|
|
.map(|_| ())
|
2019-03-18 13:13:44 +00:00
|
|
|
)
|
2019-03-11 08:38:35 +00:00
|
|
|
},
|
2020-11-11 08:27:51 +00:00
|
|
|
"proxmox-backup-proxy.service",
|
2019-09-02 13:16:21 +00:00
|
|
|
);
|
2019-03-11 08:38:35 +00:00
|
|
|
|
2021-07-06 09:56:35 +00:00
|
|
|
server::write_pid(pbs_buildcfg::PROXMOX_BACKUP_PROXY_PID_FN)?;
|
2019-04-25 08:38:26 +00:00
|
|
|
daemon::systemd_notify(daemon::SystemdNotify::Ready)?;
|
|
|
|
|
2019-08-29 07:45:34 +00:00
|
|
|
let init_result: Result<(), Error> = try_block!({
|
2020-11-02 18:13:36 +00:00
|
|
|
server::register_task_control_commands(&mut commando_sock)?;
|
|
|
|
commando_sock.spawn()?;
|
2021-09-21 05:58:40 +00:00
|
|
|
proxmox_rest_server::server_state_init()?;
|
2019-08-29 07:45:34 +00:00
|
|
|
Ok(())
|
|
|
|
});
|
2019-04-09 10:15:06 +00:00
|
|
|
|
2019-08-29 07:45:34 +00:00
|
|
|
if let Err(err) = init_result {
|
|
|
|
bail!("unable to start daemon - {}", err);
|
|
|
|
}
|
2019-04-08 12:00:23 +00:00
|
|
|
|
2020-05-20 06:59:45 +00:00
|
|
|
start_task_scheduler();
|
2020-05-23 08:43:08 +00:00
|
|
|
start_stat_generator();
|
2020-05-20 06:59:45 +00:00
|
|
|
|
2019-09-02 13:16:21 +00:00
|
|
|
server.await?;
|
2019-12-27 11:50:27 +00:00
|
|
|
log::info!("server shutting down, waiting for active workers to complete");
|
2021-09-21 05:58:40 +00:00
|
|
|
proxmox_rest_server::last_worker_future().await?;
|
2019-08-29 07:45:34 +00:00
|
|
|
log::info!("done - exit server");
|
2019-04-08 12:00:23 +00:00
|
|
|
|
2019-02-11 13:43:26 +00:00
|
|
|
Ok(())
|
2019-01-28 12:17:03 +00:00
|
|
|
}
|
2020-05-20 06:59:45 +00:00
|
|
|
|
2021-05-12 09:53:49 +00:00
|
|
|
fn make_tls_acceptor() -> Result<SslAcceptor, Error> {
|
2021-05-11 13:53:57 +00:00
|
|
|
let key_path = configdir!("/proxy.key");
|
|
|
|
let cert_path = configdir!("/proxy.pem");
|
|
|
|
|
|
|
|
let mut acceptor = SslAcceptor::mozilla_intermediate_v5(SslMethod::tls()).unwrap();
|
|
|
|
acceptor.set_private_key_file(key_path, SslFiletype::PEM)
|
|
|
|
.map_err(|err| format_err!("unable to read proxy key {} - {}", key_path, err))?;
|
|
|
|
acceptor.set_certificate_chain_file(cert_path)
|
|
|
|
.map_err(|err| format_err!("unable to read proxy cert {} - {}", cert_path, err))?;
|
|
|
|
acceptor.check_private_key().unwrap();
|
|
|
|
|
2021-05-12 09:53:49 +00:00
|
|
|
Ok(acceptor.build())
|
2021-05-11 13:53:57 +00:00
|
|
|
}
|
|
|
|
|
2021-05-11 13:53:54 +00:00
|
|
|
type ClientStreamResult =
|
|
|
|
Result<std::pin::Pin<Box<tokio_openssl::SslStream<tokio::net::TcpStream>>>, Error>;
|
|
|
|
const MAX_PENDING_ACCEPTS: usize = 1024;
|
|
|
|
|
2020-11-03 12:14:10 +00:00
|
|
|
fn accept_connections(
|
2020-12-04 10:53:34 +00:00
|
|
|
listener: tokio::net::TcpListener,
|
2021-05-12 09:53:49 +00:00
|
|
|
acceptor: Arc<Mutex<openssl::ssl::SslAcceptor>>,
|
2020-11-04 05:52:10 +00:00
|
|
|
debug: bool,
|
2021-05-11 13:53:54 +00:00
|
|
|
) -> tokio::sync::mpsc::Receiver<ClientStreamResult> {
|
2020-11-03 12:14:10 +00:00
|
|
|
|
2020-11-04 07:49:35 +00:00
|
|
|
let (sender, receiver) = tokio::sync::mpsc::channel(MAX_PENDING_ACCEPTS);
|
2020-11-03 12:14:10 +00:00
|
|
|
|
2021-05-12 09:53:49 +00:00
|
|
|
tokio::spawn(accept_connection(listener, acceptor, debug, sender));
|
2021-05-11 13:53:54 +00:00
|
|
|
|
|
|
|
receiver
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn accept_connection(
|
|
|
|
listener: tokio::net::TcpListener,
|
2021-05-12 09:53:49 +00:00
|
|
|
acceptor: Arc<Mutex<openssl::ssl::SslAcceptor>>,
|
2021-05-11 13:53:54 +00:00
|
|
|
debug: bool,
|
|
|
|
sender: tokio::sync::mpsc::Sender<ClientStreamResult>,
|
|
|
|
) {
|
2020-11-04 07:49:35 +00:00
|
|
|
let accept_counter = Arc::new(());
|
2020-11-03 12:14:10 +00:00
|
|
|
|
2021-05-11 13:53:54 +00:00
|
|
|
loop {
|
2021-05-12 09:53:49 +00:00
|
|
|
let (sock, _addr) = match listener.accept().await {
|
|
|
|
Ok(conn) => conn,
|
|
|
|
Err(err) => {
|
|
|
|
eprintln!("error accepting tcp connection: {}", err);
|
2021-05-11 13:53:55 +00:00
|
|
|
continue;
|
2021-05-11 13:53:54 +00:00
|
|
|
}
|
2021-05-11 13:53:55 +00:00
|
|
|
};
|
2020-11-03 12:14:10 +00:00
|
|
|
|
2021-05-11 13:53:55 +00:00
|
|
|
sock.set_nodelay(true).unwrap();
|
|
|
|
let _ = set_tcp_keepalive(sock.as_raw_fd(), PROXMOX_BACKUP_TCP_KEEPALIVE_TIME);
|
2020-11-03 12:14:10 +00:00
|
|
|
|
2021-05-12 09:53:49 +00:00
|
|
|
let ssl = { // limit acceptor_guard scope
|
|
|
|
// Acceptor can be reloaded using the command socket "reload-certificate" command
|
|
|
|
let acceptor_guard = acceptor.lock().unwrap();
|
|
|
|
|
|
|
|
match openssl::ssl::Ssl::new(acceptor_guard.context()) {
|
|
|
|
Ok(ssl) => ssl,
|
|
|
|
Err(err) => {
|
|
|
|
eprintln!("failed to create Ssl object from Acceptor context - {}", err);
|
|
|
|
continue;
|
|
|
|
},
|
|
|
|
}
|
2021-05-11 13:53:55 +00:00
|
|
|
};
|
2021-05-12 09:53:49 +00:00
|
|
|
|
2021-05-11 13:53:55 +00:00
|
|
|
let stream = match tokio_openssl::SslStream::new(ssl, sock) {
|
|
|
|
Ok(stream) => stream,
|
|
|
|
Err(err) => {
|
|
|
|
eprintln!("failed to create SslStream using ssl and connection socket - {}", err);
|
|
|
|
continue;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut stream = Box::pin(stream);
|
|
|
|
let sender = sender.clone();
|
|
|
|
|
|
|
|
if Arc::strong_count(&accept_counter) > MAX_PENDING_ACCEPTS {
|
|
|
|
eprintln!("connection rejected - to many open connections");
|
|
|
|
continue;
|
2020-11-03 12:14:10 +00:00
|
|
|
}
|
2021-05-11 13:53:55 +00:00
|
|
|
|
2021-05-11 13:53:56 +00:00
|
|
|
let accept_counter = Arc::clone(&accept_counter);
|
2021-05-11 13:53:55 +00:00
|
|
|
tokio::spawn(async move {
|
|
|
|
let accept_future = tokio::time::timeout(
|
|
|
|
Duration::new(10, 0), stream.as_mut().accept());
|
|
|
|
|
|
|
|
let result = accept_future.await;
|
|
|
|
|
|
|
|
match result {
|
|
|
|
Ok(Ok(())) => {
|
|
|
|
if sender.send(Ok(stream)).await.is_err() && debug {
|
|
|
|
eprintln!("detect closed connection channel");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(Err(err)) => {
|
|
|
|
if debug {
|
|
|
|
eprintln!("https handshake failed - {}", err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(_) => {
|
|
|
|
if debug {
|
|
|
|
eprintln!("https handshake timeout");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
drop(accept_counter); // decrease reference count
|
|
|
|
});
|
2021-05-11 13:53:54 +00:00
|
|
|
}
|
2020-11-03 12:14:10 +00:00
|
|
|
}
|
|
|
|
|
2020-05-23 08:43:08 +00:00
|
|
|
fn start_stat_generator() {
|
2021-09-21 05:58:40 +00:00
|
|
|
let abort_future = proxmox_rest_server::shutdown_future();
|
2020-05-23 08:43:08 +00:00
|
|
|
let future = Box::pin(run_stat_generator());
|
|
|
|
let task = futures::future::select(future, abort_future);
|
|
|
|
tokio::spawn(task.map(|_| ()));
|
|
|
|
}
|
|
|
|
|
2020-05-20 06:59:45 +00:00
|
|
|
fn start_task_scheduler() {
|
2021-09-21 05:58:40 +00:00
|
|
|
let abort_future = proxmox_rest_server::shutdown_future();
|
2020-05-20 06:59:45 +00:00
|
|
|
let future = Box::pin(run_task_scheduler());
|
|
|
|
let task = futures::future::select(future, abort_future);
|
|
|
|
tokio::spawn(task.map(|_| ()));
|
|
|
|
}
|
|
|
|
|
2020-09-12 13:10:47 +00:00
|
|
|
use std::time::{SystemTime, Instant, Duration, UNIX_EPOCH};
|
2020-05-20 06:59:45 +00:00
|
|
|
|
|
|
|
fn next_minute() -> Result<Instant, Error> {
|
2020-09-12 13:10:47 +00:00
|
|
|
let now = SystemTime::now();
|
|
|
|
let epoch_now = now.duration_since(UNIX_EPOCH)?;
|
|
|
|
let epoch_next = Duration::from_secs((epoch_now.as_secs()/60 + 1)*60);
|
2020-05-20 06:59:45 +00:00
|
|
|
Ok(Instant::now() + epoch_next - epoch_now)
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn run_task_scheduler() {
|
|
|
|
|
|
|
|
let mut count: usize = 0;
|
|
|
|
|
|
|
|
loop {
|
|
|
|
count += 1;
|
|
|
|
|
|
|
|
let delay_target = match next_minute() { // try to run very minute
|
|
|
|
Ok(d) => d,
|
|
|
|
Err(err) => {
|
|
|
|
eprintln!("task scheduler: compute next minute failed - {}", err);
|
2020-12-03 15:04:23 +00:00
|
|
|
tokio::time::sleep_until(tokio::time::Instant::from_std(Instant::now() + Duration::from_secs(60))).await;
|
2020-05-20 06:59:45 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if count > 2 { // wait 1..2 minutes before starting
|
|
|
|
match schedule_tasks().catch_unwind().await {
|
|
|
|
Err(panic) => {
|
|
|
|
match panic.downcast::<&str>() {
|
|
|
|
Ok(msg) => {
|
|
|
|
eprintln!("task scheduler panic: {}", msg);
|
|
|
|
}
|
|
|
|
Err(_) => {
|
|
|
|
eprintln!("task scheduler panic - unknown type");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(Err(err)) => {
|
|
|
|
eprintln!("task scheduler failed - {:?}", err);
|
|
|
|
}
|
|
|
|
Ok(Ok(_)) => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-03 15:04:23 +00:00
|
|
|
tokio::time::sleep_until(tokio::time::Instant::from_std(delay_target)).await;
|
2020-05-20 06:59:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn schedule_tasks() -> Result<(), Error> {
|
|
|
|
|
|
|
|
schedule_datastore_garbage_collection().await;
|
2020-05-20 11:00:53 +00:00
|
|
|
schedule_datastore_prune().await;
|
2020-05-22 05:50:59 +00:00
|
|
|
schedule_datastore_sync_jobs().await;
|
2020-10-20 09:10:06 +00:00
|
|
|
schedule_datastore_verify_jobs().await;
|
2021-02-15 06:55:13 +00:00
|
|
|
schedule_tape_backup_jobs().await;
|
2020-09-28 13:32:12 +00:00
|
|
|
schedule_task_log_rotate().await;
|
2020-05-20 06:59:45 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn schedule_datastore_garbage_collection() {
|
|
|
|
|
2021-09-10 06:40:58 +00:00
|
|
|
let config = match pbs_config::datastore::config() {
|
2020-05-20 06:59:45 +00:00
|
|
|
Err(err) => {
|
|
|
|
eprintln!("unable to read datastore config - {}", err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Ok((config, _digest)) => config,
|
|
|
|
};
|
|
|
|
|
|
|
|
for (store, (_, store_config)) in config.sections {
|
|
|
|
let datastore = match DataStore::lookup_datastore(&store) {
|
|
|
|
Ok(datastore) => datastore,
|
|
|
|
Err(err) => {
|
|
|
|
eprintln!("lookup_datastore failed - {}", err);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-05-20 11:00:53 +00:00
|
|
|
let store_config: DataStoreConfig = match serde_json::from_value(store_config) {
|
2020-05-20 06:59:45 +00:00
|
|
|
Ok(c) => c,
|
|
|
|
Err(err) => {
|
|
|
|
eprintln!("datastore config from_value failed - {}", err);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let event_str = match store_config.gc_schedule {
|
|
|
|
Some(event_str) => event_str,
|
|
|
|
None => continue,
|
|
|
|
};
|
|
|
|
|
|
|
|
let event = match parse_calendar_event(&event_str) {
|
|
|
|
Ok(event) => event,
|
|
|
|
Err(err) => {
|
|
|
|
eprintln!("unable to parse schedule '{}' - {}", event_str, err);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if datastore.garbage_collection_running() { continue; }
|
|
|
|
|
|
|
|
let worker_type = "garbage_collection";
|
|
|
|
|
2020-11-02 11:34:39 +00:00
|
|
|
let last = match jobstate::last_run_time(worker_type, &store) {
|
|
|
|
Ok(time) => time,
|
|
|
|
Err(err) => {
|
|
|
|
eprintln!("could not get last run time of {} {}: {}", worker_type, store, err);
|
|
|
|
continue;
|
2020-05-20 06:59:45 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let next = match compute_next_event(&event, last, false) {
|
2020-09-04 12:33:27 +00:00
|
|
|
Ok(Some(next)) => next,
|
|
|
|
Ok(None) => continue,
|
2020-05-20 06:59:45 +00:00
|
|
|
Err(err) => {
|
|
|
|
eprintln!("compute_next_event for '{}' failed - {}", event_str, err);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
};
|
2020-06-10 10:02:56 +00:00
|
|
|
|
2020-09-12 13:10:47 +00:00
|
|
|
let now = proxmox::tools::time::epoch_i64();
|
|
|
|
|
2020-05-20 06:59:45 +00:00
|
|
|
if next > now { continue; }
|
|
|
|
|
2020-10-30 11:49:43 +00:00
|
|
|
let job = match Job::new(worker_type, &store) {
|
2020-09-24 14:13:54 +00:00
|
|
|
Ok(job) => job,
|
|
|
|
Err(_) => continue, // could not get lock
|
|
|
|
};
|
|
|
|
|
2020-11-05 13:03:15 +00:00
|
|
|
let auth_id = Authid::root_auth_id();
|
2020-09-24 14:13:54 +00:00
|
|
|
|
2020-11-02 11:34:37 +00:00
|
|
|
if let Err(err) = crate::server::do_garbage_collection_job(job, datastore, auth_id, Some(event_str), false) {
|
2020-10-30 09:54:31 +00:00
|
|
|
eprintln!("unable to start garbage collection job on datastore {} - {}", store, err);
|
2020-05-20 06:59:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-20 11:00:53 +00:00
|
|
|
|
|
|
|
async fn schedule_datastore_prune() {
|
|
|
|
|
2021-09-10 06:40:58 +00:00
|
|
|
let config = match pbs_config::datastore::config() {
|
2020-05-20 11:00:53 +00:00
|
|
|
Err(err) => {
|
|
|
|
eprintln!("unable to read datastore config - {}", err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Ok((config, _digest)) => config,
|
|
|
|
};
|
|
|
|
|
|
|
|
for (store, (_, store_config)) in config.sections {
|
|
|
|
|
|
|
|
let store_config: DataStoreConfig = match serde_json::from_value(store_config) {
|
|
|
|
Ok(c) => c,
|
|
|
|
Err(err) => {
|
2020-05-22 05:50:59 +00:00
|
|
|
eprintln!("datastore '{}' config from_value failed - {}", store, err);
|
2020-05-20 11:00:53 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let event_str = match store_config.prune_schedule {
|
|
|
|
Some(event_str) => event_str,
|
|
|
|
None => continue,
|
|
|
|
};
|
|
|
|
|
|
|
|
let prune_options = PruneOptions {
|
|
|
|
keep_last: store_config.keep_last,
|
|
|
|
keep_hourly: store_config.keep_hourly,
|
|
|
|
keep_daily: store_config.keep_daily,
|
|
|
|
keep_weekly: store_config.keep_weekly,
|
|
|
|
keep_monthly: store_config.keep_monthly,
|
|
|
|
keep_yearly: store_config.keep_yearly,
|
|
|
|
};
|
|
|
|
|
2021-09-10 07:21:27 +00:00
|
|
|
if !pbs_datastore::prune::keeps_something(&prune_options) { // no prune settings - keep all
|
2020-05-20 11:00:53 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
let worker_type = "prune";
|
2020-10-30 08:48:35 +00:00
|
|
|
if check_schedule(worker_type, &event_str, &store) {
|
2020-10-30 08:07:25 +00:00
|
|
|
let job = match Job::new(worker_type, &store) {
|
|
|
|
Ok(job) => job,
|
|
|
|
Err(_) => continue, // could not get lock
|
|
|
|
};
|
2020-05-20 11:00:53 +00:00
|
|
|
|
2020-11-05 13:03:15 +00:00
|
|
|
let auth_id = Authid::root_auth_id().clone();
|
2020-10-30 08:07:25 +00:00
|
|
|
if let Err(err) = do_prune_job(job, prune_options, store.clone(), &auth_id, Some(event_str)) {
|
|
|
|
eprintln!("unable to start datastore prune job {} - {}", &store, err);
|
2020-05-20 11:00:53 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2020-05-22 05:50:59 +00:00
|
|
|
|
|
|
|
async fn schedule_datastore_sync_jobs() {
|
|
|
|
|
|
|
|
|
2021-09-08 04:57:23 +00:00
|
|
|
let config = match pbs_config::sync::config() {
|
2020-05-22 05:50:59 +00:00
|
|
|
Err(err) => {
|
|
|
|
eprintln!("unable to read sync job config - {}", err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Ok((config, _digest)) => config,
|
|
|
|
};
|
|
|
|
|
|
|
|
for (job_id, (_, job_config)) in config.sections {
|
|
|
|
let job_config: SyncJobConfig = match serde_json::from_value(job_config) {
|
|
|
|
Ok(c) => c,
|
|
|
|
Err(err) => {
|
|
|
|
eprintln!("sync job config from_value failed - {}", err);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let event_str = match job_config.schedule {
|
|
|
|
Some(ref event_str) => event_str.clone(),
|
|
|
|
None => continue,
|
|
|
|
};
|
|
|
|
|
2020-05-29 08:53:37 +00:00
|
|
|
let worker_type = "syncjob";
|
2020-10-30 08:48:35 +00:00
|
|
|
if check_schedule(worker_type, &event_str, &job_id) {
|
2020-10-30 08:07:25 +00:00
|
|
|
let job = match Job::new(worker_type, &job_id) {
|
|
|
|
Ok(job) => job,
|
|
|
|
Err(_) => continue, // could not get lock
|
|
|
|
};
|
2020-05-22 05:50:59 +00:00
|
|
|
|
2020-11-05 13:03:15 +00:00
|
|
|
let auth_id = Authid::root_auth_id().clone();
|
2020-10-30 08:07:25 +00:00
|
|
|
if let Err(err) = do_sync_job(job, job_config, &auth_id, Some(event_str)) {
|
|
|
|
eprintln!("unable to start datastore sync job {} - {}", &job_id, err);
|
2020-05-22 05:50:59 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2020-05-23 08:43:08 +00:00
|
|
|
|
2020-10-20 09:10:06 +00:00
|
|
|
async fn schedule_datastore_verify_jobs() {
|
2020-10-28 06:33:05 +00:00
|
|
|
|
2021-09-08 06:01:07 +00:00
|
|
|
let config = match pbs_config::verify::config() {
|
2020-10-20 09:10:06 +00:00
|
|
|
Err(err) => {
|
|
|
|
eprintln!("unable to read verification job config - {}", err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Ok((config, _digest)) => config,
|
|
|
|
};
|
|
|
|
for (job_id, (_, job_config)) in config.sections {
|
|
|
|
let job_config: VerificationJobConfig = match serde_json::from_value(job_config) {
|
|
|
|
Ok(c) => c,
|
|
|
|
Err(err) => {
|
|
|
|
eprintln!("verification job config from_value failed - {}", err);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let event_str = match job_config.schedule {
|
|
|
|
Some(ref event_str) => event_str.clone(),
|
|
|
|
None => continue,
|
|
|
|
};
|
2020-10-30 08:07:25 +00:00
|
|
|
|
2020-10-20 09:10:06 +00:00
|
|
|
let worker_type = "verificationjob";
|
2020-11-05 13:03:15 +00:00
|
|
|
let auth_id = Authid::root_auth_id().clone();
|
2020-10-30 08:48:35 +00:00
|
|
|
if check_schedule(worker_type, &event_str, &job_id) {
|
2020-10-30 08:07:25 +00:00
|
|
|
let job = match Job::new(&worker_type, &job_id) {
|
|
|
|
Ok(job) => job,
|
|
|
|
Err(_) => continue, // could not get lock
|
|
|
|
};
|
|
|
|
if let Err(err) = do_verification_job(job, job_config, &auth_id, Some(event_str)) {
|
|
|
|
eprintln!("unable to start datastore verification job {} - {}", &job_id, err);
|
2020-10-20 09:10:06 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-15 06:55:13 +00:00
|
|
|
async fn schedule_tape_backup_jobs() {
|
|
|
|
|
2021-09-07 10:32:01 +00:00
|
|
|
let config = match pbs_config::tape_job::config() {
|
2021-02-15 06:55:13 +00:00
|
|
|
Err(err) => {
|
|
|
|
eprintln!("unable to read tape job config - {}", err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Ok((config, _digest)) => config,
|
|
|
|
};
|
|
|
|
for (job_id, (_, job_config)) in config.sections {
|
|
|
|
let job_config: TapeBackupJobConfig = match serde_json::from_value(job_config) {
|
|
|
|
Ok(c) => c,
|
|
|
|
Err(err) => {
|
|
|
|
eprintln!("tape backup job config from_value failed - {}", err);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let event_str = match job_config.schedule {
|
|
|
|
Some(ref event_str) => event_str.clone(),
|
|
|
|
None => continue,
|
|
|
|
};
|
|
|
|
|
|
|
|
let worker_type = "tape-backup-job";
|
|
|
|
let auth_id = Authid::root_auth_id().clone();
|
|
|
|
if check_schedule(worker_type, &event_str, &job_id) {
|
|
|
|
let job = match Job::new(&worker_type, &job_id) {
|
|
|
|
Ok(job) => job,
|
|
|
|
Err(_) => continue, // could not get lock
|
|
|
|
};
|
2021-02-24 17:12:11 +00:00
|
|
|
if let Err(err) = do_tape_backup_job(job, job_config.setup, &auth_id, Some(event_str)) {
|
2021-05-01 10:01:09 +00:00
|
|
|
eprintln!("unable to start tape backup job {} - {}", &job_id, err);
|
2021-02-15 06:55:13 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-09-28 13:32:12 +00:00
|
|
|
async fn schedule_task_log_rotate() {
|
|
|
|
|
|
|
|
let worker_type = "logrotate";
|
2020-11-02 18:30:49 +00:00
|
|
|
let job_id = "access-log_and_task-archive";
|
2020-09-28 13:32:12 +00:00
|
|
|
|
|
|
|
// schedule daily at 00:00 like normal logrotate
|
|
|
|
let schedule = "00:00";
|
|
|
|
|
2020-10-30 08:48:35 +00:00
|
|
|
if !check_schedule(worker_type, schedule, job_id) {
|
2020-09-28 13:32:12 +00:00
|
|
|
// if we never ran the rotation, schedule instantly
|
|
|
|
match jobstate::JobState::load(worker_type, job_id) {
|
|
|
|
Ok(state) => match state {
|
|
|
|
jobstate::JobState::Created { .. } => {},
|
|
|
|
_ => return,
|
|
|
|
},
|
|
|
|
_ => return,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut job = match Job::new(worker_type, job_id) {
|
|
|
|
Ok(job) => job,
|
|
|
|
Err(_) => return, // could not get lock
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Err(err) = WorkerTask::new_thread(
|
|
|
|
worker_type,
|
2020-11-02 18:30:49 +00:00
|
|
|
None,
|
2020-11-05 13:03:15 +00:00
|
|
|
Authid::root_auth_id().clone(),
|
2020-09-28 13:32:12 +00:00
|
|
|
false,
|
|
|
|
move |worker| {
|
|
|
|
job.start(&worker.upid().to_string())?;
|
2021-01-19 09:54:37 +00:00
|
|
|
worker.log("starting task log rotation".to_string());
|
2020-10-19 13:11:51 +00:00
|
|
|
|
2020-09-28 13:32:12 +00:00
|
|
|
let result = try_block!({
|
2020-11-02 18:31:56 +00:00
|
|
|
let max_size = 512 * 1024 - 1; // an entry has ~ 100b, so > 5000 entries/file
|
|
|
|
let max_files = 20; // times twenty files gives > 100000 task entries
|
2020-09-28 13:32:12 +00:00
|
|
|
let has_rotated = rotate_task_log_archive(max_size, true, Some(max_files))?;
|
|
|
|
if has_rotated {
|
2021-01-19 09:54:37 +00:00
|
|
|
worker.log("task log archive was rotated".to_string());
|
2020-09-28 13:32:12 +00:00
|
|
|
} else {
|
2021-01-19 09:54:37 +00:00
|
|
|
worker.log("task log archive was not rotated".to_string());
|
2020-09-28 13:32:12 +00:00
|
|
|
}
|
|
|
|
|
2020-11-02 18:21:58 +00:00
|
|
|
let max_size = 32 * 1024 * 1024 - 1;
|
|
|
|
let max_files = 14;
|
2021-07-06 09:56:35 +00:00
|
|
|
let mut logrotate = LogRotate::new(pbs_buildcfg::API_ACCESS_LOG_FN, true)
|
2020-11-02 18:21:58 +00:00
|
|
|
.ok_or_else(|| format_err!("could not get API access log file names"))?;
|
|
|
|
|
2020-11-04 16:15:28 +00:00
|
|
|
if logrotate.rotate(max_size, None, Some(max_files))? {
|
2020-11-02 18:21:58 +00:00
|
|
|
println!("rotated access log, telling daemons to re-open log file");
|
2021-07-06 10:08:44 +00:00
|
|
|
pbs_runtime::block_on(command_reopen_logfiles())?;
|
2021-01-19 09:54:37 +00:00
|
|
|
worker.log("API access log was rotated".to_string());
|
2020-11-04 16:15:28 +00:00
|
|
|
} else {
|
2021-01-19 09:54:37 +00:00
|
|
|
worker.log("API access log was not rotated".to_string());
|
2020-11-04 16:15:28 +00:00
|
|
|
}
|
|
|
|
|
2021-07-06 09:56:35 +00:00
|
|
|
let mut logrotate = LogRotate::new(pbs_buildcfg::API_AUTH_LOG_FN, true)
|
2020-11-04 16:15:28 +00:00
|
|
|
.ok_or_else(|| format_err!("could not get API auth log file names"))?;
|
2020-11-02 18:21:58 +00:00
|
|
|
|
2020-11-04 16:15:28 +00:00
|
|
|
if logrotate.rotate(max_size, None, Some(max_files))? {
|
2021-01-19 09:54:37 +00:00
|
|
|
worker.log("API authentication log was rotated".to_string());
|
2020-11-02 18:21:58 +00:00
|
|
|
} else {
|
2021-01-19 09:54:37 +00:00
|
|
|
worker.log("API authentication log was not rotated".to_string());
|
2020-11-02 18:21:58 +00:00
|
|
|
}
|
|
|
|
|
2020-09-28 13:32:12 +00:00
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
|
|
|
|
let status = worker.create_state(&result);
|
|
|
|
|
|
|
|
if let Err(err) = job.finish(status) {
|
|
|
|
eprintln!("could not finish job state for {}: {}", worker_type, err);
|
|
|
|
}
|
|
|
|
|
|
|
|
result
|
|
|
|
},
|
|
|
|
) {
|
|
|
|
eprintln!("unable to start task log rotation: {}", err);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-11-02 18:21:58 +00:00
|
|
|
async fn command_reopen_logfiles() -> Result<(), Error> {
|
|
|
|
// only care about the most recent daemon instance for each, proxy & api, as other older ones
|
|
|
|
// should not respond to new requests anyway, but only finish their current one and then exit.
|
2021-09-21 05:58:40 +00:00
|
|
|
let sock = crate::server::our_ctrl_sock();
|
|
|
|
let f1 = proxmox_rest_server::send_command(sock, "{\"command\":\"api-access-log-reopen\"}\n");
|
2020-11-02 18:21:58 +00:00
|
|
|
|
2021-09-21 05:58:40 +00:00
|
|
|
let pid = crate::server::read_pid(pbs_buildcfg::PROXMOX_BACKUP_API_PID_FN)?;
|
|
|
|
let sock = crate::server::ctrl_sock_from_pid(pid);
|
|
|
|
let f2 = proxmox_rest_server::send_command(sock, "{\"command\":\"api-access-log-reopen\"}\n");
|
2020-11-04 15:45:12 +00:00
|
|
|
|
|
|
|
match futures::join!(f1, f2) {
|
|
|
|
(Err(e1), Err(e2)) => Err(format_err!("reopen commands failed, proxy: {}; api: {}", e1, e2)),
|
|
|
|
(Err(e1), Ok(_)) => Err(format_err!("reopen commands failed, proxy: {}", e1)),
|
|
|
|
(Ok(_), Err(e2)) => Err(format_err!("reopen commands failed, api: {}", e2)),
|
|
|
|
_ => Ok(()),
|
|
|
|
}
|
2020-11-02 18:21:58 +00:00
|
|
|
}
|
|
|
|
|
2020-05-23 08:43:08 +00:00
|
|
|
async fn run_stat_generator() {
|
|
|
|
|
2020-05-29 07:16:13 +00:00
|
|
|
let mut count = 0;
|
2020-05-23 08:43:08 +00:00
|
|
|
loop {
|
2020-05-29 07:16:13 +00:00
|
|
|
count += 1;
|
2020-06-04 08:30:47 +00:00
|
|
|
let save = if count >= 6 { count = 0; true } else { false };
|
2020-05-29 07:16:13 +00:00
|
|
|
|
2020-05-23 08:43:08 +00:00
|
|
|
let delay_target = Instant::now() + Duration::from_secs(10);
|
|
|
|
|
2020-05-29 07:16:13 +00:00
|
|
|
generate_host_stats(save).await;
|
2020-05-23 08:43:08 +00:00
|
|
|
|
2020-12-03 15:04:23 +00:00
|
|
|
tokio::time::sleep_until(tokio::time::Instant::from_std(delay_target)).await;
|
2020-05-29 07:16:13 +00:00
|
|
|
|
|
|
|
}
|
2020-05-23 08:43:08 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-05-29 07:16:13 +00:00
|
|
|
fn rrd_update_gauge(name: &str, value: f64, save: bool) {
|
2020-05-25 14:19:55 +00:00
|
|
|
use proxmox_backup::rrd;
|
2020-05-29 07:16:13 +00:00
|
|
|
if let Err(err) = rrd::update_value(name, value, rrd::DST::Gauge, save) {
|
2020-05-25 14:19:55 +00:00
|
|
|
eprintln!("rrd::update_value '{}' failed - {}", name, err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-29 07:16:13 +00:00
|
|
|
fn rrd_update_derive(name: &str, value: f64, save: bool) {
|
2020-05-25 14:19:55 +00:00
|
|
|
use proxmox_backup::rrd;
|
2020-05-29 07:16:13 +00:00
|
|
|
if let Err(err) = rrd::update_value(name, value, rrd::DST::Derive, save) {
|
2020-05-25 14:19:55 +00:00
|
|
|
eprintln!("rrd::update_value '{}' failed - {}", name, err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-29 07:16:13 +00:00
|
|
|
async fn generate_host_stats(save: bool) {
|
2020-05-24 15:28:05 +00:00
|
|
|
use proxmox::sys::linux::procfs::{
|
2020-05-25 09:40:20 +00:00
|
|
|
read_meminfo, read_proc_stat, read_proc_net_dev, read_loadavg};
|
2020-05-23 08:43:08 +00:00
|
|
|
|
2021-07-06 10:08:44 +00:00
|
|
|
pbs_runtime::block_in_place(move || {
|
2020-05-25 06:30:59 +00:00
|
|
|
|
|
|
|
match read_proc_stat() {
|
|
|
|
Ok(stat) => {
|
2020-05-29 07:16:13 +00:00
|
|
|
rrd_update_gauge("host/cpu", stat.cpu, save);
|
|
|
|
rrd_update_gauge("host/iowait", stat.iowait_percent, save);
|
2020-05-25 06:30:59 +00:00
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
eprintln!("read_proc_stat failed - {}", err);
|
2020-05-23 08:43:08 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-25 09:48:44 +00:00
|
|
|
|
2020-05-25 06:30:59 +00:00
|
|
|
match read_meminfo() {
|
|
|
|
Ok(meminfo) => {
|
2020-05-29 07:16:13 +00:00
|
|
|
rrd_update_gauge("host/memtotal", meminfo.memtotal as f64, save);
|
|
|
|
rrd_update_gauge("host/memused", meminfo.memused as f64, save);
|
|
|
|
rrd_update_gauge("host/swaptotal", meminfo.swaptotal as f64, save);
|
|
|
|
rrd_update_gauge("host/swapused", meminfo.swapused as f64, save);
|
2020-05-23 12:03:44 +00:00
|
|
|
}
|
2020-05-25 06:30:59 +00:00
|
|
|
Err(err) => {
|
|
|
|
eprintln!("read_meminfo failed - {}", err);
|
2020-05-23 12:03:44 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-24 15:28:05 +00:00
|
|
|
|
2020-05-25 06:30:59 +00:00
|
|
|
match read_proc_net_dev() {
|
|
|
|
Ok(netdev) => {
|
2021-09-08 10:22:48 +00:00
|
|
|
use pbs_config::network::is_physical_nic;
|
2020-05-25 06:30:59 +00:00
|
|
|
let mut netin = 0;
|
|
|
|
let mut netout = 0;
|
|
|
|
for item in netdev {
|
|
|
|
if !is_physical_nic(&item.device) { continue; }
|
|
|
|
netin += item.receive;
|
|
|
|
netout += item.send;
|
|
|
|
}
|
2020-05-29 07:16:13 +00:00
|
|
|
rrd_update_derive("host/netin", netin as f64, save);
|
|
|
|
rrd_update_derive("host/netout", netout as f64, save);
|
2020-05-24 15:28:05 +00:00
|
|
|
}
|
2020-05-25 06:30:59 +00:00
|
|
|
Err(err) => {
|
|
|
|
eprintln!("read_prox_net_dev failed - {}", err);
|
2020-05-24 15:28:05 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-25 09:10:07 +00:00
|
|
|
|
2020-05-25 09:40:20 +00:00
|
|
|
match read_loadavg() {
|
|
|
|
Ok(loadavg) => {
|
2020-05-29 07:16:13 +00:00
|
|
|
rrd_update_gauge("host/loadavg", loadavg.0 as f64, save);
|
2020-05-25 09:40:20 +00:00
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
eprintln!("read_loadavg failed - {}", err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-26 09:20:59 +00:00
|
|
|
let disk_manager = DiskManage::new();
|
|
|
|
|
2020-05-29 07:16:13 +00:00
|
|
|
gather_disk_stats(disk_manager.clone(), Path::new("/"), "host", save);
|
2020-05-28 10:30:54 +00:00
|
|
|
|
2021-09-10 06:40:58 +00:00
|
|
|
match pbs_config::datastore::config() {
|
2020-05-25 14:00:03 +00:00
|
|
|
Ok((config, _)) => {
|
2021-09-10 06:40:58 +00:00
|
|
|
let datastore_list: Vec<DataStoreConfig> =
|
2021-01-19 10:31:37 +00:00
|
|
|
config.convert_to_typed_array("datastore").unwrap_or_default();
|
2020-05-25 14:00:03 +00:00
|
|
|
|
|
|
|
for config in datastore_list {
|
2020-05-26 09:20:59 +00:00
|
|
|
|
2020-05-28 10:30:54 +00:00
|
|
|
let rrd_prefix = format!("datastore/{}", config.name);
|
2020-05-26 09:20:59 +00:00
|
|
|
let path = std::path::Path::new(&config.path);
|
2020-05-29 07:16:13 +00:00
|
|
|
gather_disk_stats(disk_manager.clone(), path, &rrd_prefix, save);
|
2020-05-25 14:00:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
eprintln!("read datastore config failed - {}", err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-25 06:30:59 +00:00
|
|
|
});
|
2020-05-23 08:43:08 +00:00
|
|
|
}
|
2020-05-25 09:10:07 +00:00
|
|
|
|
2020-10-30 08:48:35 +00:00
|
|
|
fn check_schedule(worker_type: &str, event_str: &str, id: &str) -> bool {
|
|
|
|
let event = match parse_calendar_event(event_str) {
|
2020-10-30 08:07:25 +00:00
|
|
|
Ok(event) => event,
|
|
|
|
Err(err) => {
|
|
|
|
eprintln!("unable to parse schedule '{}' - {}", event_str, err);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-10-30 08:48:35 +00:00
|
|
|
let last = match jobstate::last_run_time(worker_type, &id) {
|
2020-10-30 08:07:25 +00:00
|
|
|
Ok(time) => time,
|
|
|
|
Err(err) => {
|
|
|
|
eprintln!("could not get last run time of {} {}: {}", worker_type, id, err);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let next = match compute_next_event(&event, last, false) {
|
|
|
|
Ok(Some(next)) => next,
|
|
|
|
Ok(None) => return false,
|
|
|
|
Err(err) => {
|
|
|
|
eprintln!("compute_next_event for '{}' failed - {}", event_str, err);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let now = proxmox::tools::time::epoch_i64();
|
|
|
|
next <= now
|
|
|
|
}
|
|
|
|
|
2020-05-29 07:16:13 +00:00
|
|
|
fn gather_disk_stats(disk_manager: Arc<DiskManage>, path: &Path, rrd_prefix: &str, save: bool) {
|
2020-05-28 10:30:54 +00:00
|
|
|
|
2020-05-29 09:09:02 +00:00
|
|
|
match proxmox_backup::tools::disks::disk_usage(path) {
|
2020-06-09 08:01:09 +00:00
|
|
|
Ok(status) => {
|
2020-05-28 10:30:54 +00:00
|
|
|
let rrd_key = format!("{}/total", rrd_prefix);
|
2020-06-09 08:01:09 +00:00
|
|
|
rrd_update_gauge(&rrd_key, status.total as f64, save);
|
2020-05-28 10:30:54 +00:00
|
|
|
let rrd_key = format!("{}/used", rrd_prefix);
|
2020-06-09 08:01:09 +00:00
|
|
|
rrd_update_gauge(&rrd_key, status.used as f64, save);
|
2020-05-28 10:30:54 +00:00
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
eprintln!("read disk_usage on {:?} failed - {}", path, err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-29 09:09:02 +00:00
|
|
|
match disk_manager.find_mounted_device(path) {
|
|
|
|
Ok(None) => {},
|
|
|
|
Ok(Some((fs_type, device, source))) => {
|
|
|
|
let mut device_stat = None;
|
|
|
|
match fs_type.as_str() {
|
|
|
|
"zfs" => {
|
2021-04-12 09:24:29 +00:00
|
|
|
if let Some(source) = source {
|
|
|
|
let pool = get_pool_from_dataset(&source).unwrap_or(&source);
|
|
|
|
match zfs_pool_stats(pool) {
|
2020-05-29 09:09:02 +00:00
|
|
|
Ok(stat) => device_stat = stat,
|
|
|
|
Err(err) => eprintln!("zfs_pool_stats({:?}) failed - {}", pool, err),
|
2020-05-28 10:30:54 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-29 09:09:02 +00:00
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
if let Ok(disk) = disk_manager.clone().disk_by_dev_num(device.into_dev_t()) {
|
|
|
|
match disk.read_stat() {
|
|
|
|
Ok(stat) => device_stat = stat,
|
|
|
|
Err(err) => eprintln!("disk.read_stat {:?} failed - {}", path, err),
|
2020-05-28 10:30:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-29 09:09:02 +00:00
|
|
|
if let Some(stat) = device_stat {
|
|
|
|
let rrd_key = format!("{}/read_ios", rrd_prefix);
|
|
|
|
rrd_update_derive(&rrd_key, stat.read_ios as f64, save);
|
|
|
|
let rrd_key = format!("{}/read_bytes", rrd_prefix);
|
|
|
|
rrd_update_derive(&rrd_key, (stat.read_sectors*512) as f64, save);
|
2020-05-25 09:10:07 +00:00
|
|
|
|
2020-05-29 09:09:02 +00:00
|
|
|
let rrd_key = format!("{}/write_ios", rrd_prefix);
|
|
|
|
rrd_update_derive(&rrd_key, stat.write_ios as f64, save);
|
|
|
|
let rrd_key = format!("{}/write_bytes", rrd_prefix);
|
|
|
|
rrd_update_derive(&rrd_key, (stat.write_sectors*512) as f64, save);
|
2020-05-25 09:10:07 +00:00
|
|
|
|
2020-05-29 09:09:02 +00:00
|
|
|
let rrd_key = format!("{}/io_ticks", rrd_prefix);
|
|
|
|
rrd_update_derive(&rrd_key, (stat.io_ticks as f64)/1000.0, save);
|
2020-05-26 09:20:59 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-29 09:09:02 +00:00
|
|
|
Err(err) => {
|
|
|
|
eprintln!("find_mounted_device failed - {}", err);
|
|
|
|
}
|
2020-05-26 09:20:59 +00:00
|
|
|
}
|
|
|
|
}
|