about summary refs log tree commit diff
path: root/gatherer/src/main.rs
blob: c2d90e71514b2cbb6835571e1b55d740c56c6f9c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
mod api;
mod config;
mod db;
pub mod griph;
mod thread;

use std::sync::{
	Arc,
	atomic::{AtomicU8, AtomicU64},
};

pub use db::{Database, DbCpuinfo, DbMeminfo, DbNetinfo};
use tracing_subscriber::{EnvFilter, layer::SubscriberExt, util::SubscriberInitExt as _};

use crate::{
	config::Config,
	thread::{Gatherer, GathererConfig},
};

#[derive(Clone, Debug)]
pub struct State {
	/// bytes
	pub meminfo_upper_bound: Arc<AtomicU64>,
	pub meminfo_current: Arc<AtomicU64>,
	/// kbps
	pub netinfo_upper_bound: Arc<AtomicU64>,
	/// whole digit % cpu usage
	pub cpuinfo_upper_bound: Arc<AtomicU8>,
}

pub fn main() {
	create_tracing_subscriber();
	let conf = Config::get();
	tracing::trace!(?conf);

	let state = State {
		meminfo_upper_bound: Arc::new(AtomicU64::new(0)),
		meminfo_current: Arc::new(AtomicU64::new(0)),
		netinfo_upper_bound: Arc::new(AtomicU64::new(0)),
		cpuinfo_upper_bound: Arc::new(AtomicU8::new(0)),
	};
	let database = Database::new(conf.database_path());
	database.create_tables();
	let arc_db = Arc::new(database);

	let gatherconfig = GathererConfig {
		network: conf.network_interface().to_owned(),
		graph_directory: conf.graph_path().to_path_buf(),
		graph_update: conf.graph_update(),
		graph_on_boot: conf.graph_on_boot(),
	};
	let mut gatherer = Gatherer::new(Arc::clone(&arc_db), state.clone(), gatherconfig);
	gatherer.start();

	let api = api::ApiThread::start(
		conf.socket_path().to_owned(),
		Arc::clone(&arc_db),
		state.clone(),
		conf.graph_path().to_owned(),
	);
	tracing::info!("listening on unix:{}", conf.socket_path());

	api.wait();
}

pub fn create_tracing_subscriber() {
	tracing_subscriber::registry()
		.with(tracing_subscriber::fmt::layer())
		.with(
			EnvFilter::try_from_default_env()
				.or_else(|_| EnvFilter::try_new("info"))
				.unwrap(),
		)
		.init();
}