1 use std::sync::atomic::Ordering;
2 use std::collections::LinkedList;
3 use std::sync::{Arc, Mutex};
6 use crate::datastore::{Store, AddressState, U64Setting, RegexSetting};
8 use crate::START_SHUTDOWN;
14 V4RoutingTableSize(usize),
15 V6RoutingTableSize(usize),
16 RoutingTablePaths(usize),
20 lines: LinkedList<String>,
22 connection_count: u64,
29 stats: Arc<Mutex<Stats>>,
33 pub fn new(store: &'static Store) -> Printer {
34 let stats: Arc<Mutex<Stats>> = Arc::new(Mutex::new(Stats {
35 lines: LinkedList::new(),
42 let thread_arc = Arc::clone(&stats);
43 std::thread::spawn(move || {
45 std::thread::sleep(std::time::Duration::from_secs(1));
47 let stdout = std::io::stdout();
48 let mut out = stdout.lock();
51 let stats = thread_arc.lock().unwrap();
52 if START_SHUTDOWN.load(Ordering::Relaxed) && stats.connection_count == 0 {
56 out.write_all(b"\x1b[2J\x1b[;H\n").expect("stdout broken?");
57 for line in stats.lines.iter() {
58 out.write_all(line.as_bytes()).expect("stdout broken?");
59 out.write_all(b"\n").expect("stdout broken?");
62 out.write_all(b"\nNode counts by status:\n").expect("stdout broken?");
63 for i in 0..AddressState::get_count() {
64 out.write_all(format!("{:22}: {}\n", AddressState::from_num(i).unwrap().to_str(),
65 store.get_node_count(AddressState::from_num(i).unwrap())
66 ).as_bytes()).expect("stdout broken?");
68 let generations = store.get_bloom_node_count();
69 out.write_all(b"Bloom filter generations contain:").expect("stdout broken?");
70 for generation in &generations {
71 out.write_all(format!(" {}", generation).as_bytes()).expect("stdout broken?");
74 out.write_all(format!(
75 "\n\nCurrent connections open/in progress: {}\n", stats.connection_count).as_bytes()).expect("stdout broken?");
76 out.write_all(format!(
77 "Current block count: {}\n", stats.header_count).as_bytes()).expect("stdout broken?");
79 out.write_all(format!(
80 "Timeout for full run (in seconds): {} (\"t x\" to change to x seconds)\n", store.get_u64(U64Setting::RunTimeout)
81 ).as_bytes()).expect("stdout broken?");
82 out.write_all(format!(
83 "Minimum protocol version: {} (\"v x\" to change value to x)\n", store.get_u64(U64Setting::MinProtocolVersion)
84 ).as_bytes()).expect("stdout broken?");
85 out.write_all(format!(
86 "Subversion match regex: {} (\"s x\" to change value to x)\n", store.get_regex(RegexSetting::SubverRegex).as_str()
87 ).as_bytes()).expect("stdout broken?");
89 out.write_all(b"\nRetry times (in seconds):\n").expect("stdout broken?");
90 for i in 0..AddressState::get_count() {
91 let scan_secs = store.get_u64(U64Setting::RescanInterval(AddressState::from_num(i).unwrap()));
92 out.write_all(format!(
93 "{:22} ({:2}): {:5} (ie {} hrs, {} min)\n", AddressState::from_num(i).unwrap().to_str(), i,
94 scan_secs, scan_secs / 60 / 60, (scan_secs / 60) % 60,
95 ).as_bytes()).expect("stdout broken?");
98 out.write_all(format!(
99 "\nBGP Routing Table: {} v4 nets, {} v6 nets, {} max paths\n",
100 stats.v4_table_size, stats.v6_table_size, stats.paths).as_bytes()).expect("stdout broken?");
102 out.write_all(b"\nCommands:\n").expect("stdout broken?");
103 out.write_all(b"q: quit\n").expect("stdout broken?");
104 out.write_all(format!(
105 "r x y: Change retry time for status x (int value, see retry times section for name mappings) to y (in seconds)\n"
106 ).as_bytes()).expect("stdout broken?");
107 out.write_all(format!(
108 "w x: Change the amount of time a node is considered WAS_GOOD after it fails to x from {} (in seconds)\n",
109 store.get_u64(U64Setting::WasGoodTimeout)
110 ).as_bytes()).expect("stdout broken?");
111 out.write_all(b"a x: Scan node x\n").expect("stdout broken?");
112 out.write_all(b"b x: BGP Lookup IP x\n").expect("stdout broken?");
113 out.write_all(b"\x1b[s").expect("stdout broken?"); // Save cursor position and provide a blank line before cursor
114 out.write_all(b"\x1b[;H\x1b[2K").expect("stdout broken?");
115 out.write_all(b"Most recent log:\n").expect("stdout broken?");
116 out.write_all(b"\x1b[u").expect("stdout broken?"); // Restore cursor position and go up one line
119 out.flush().expect("stdout broken?");
127 pub fn add_line(&self, line: String, err: bool) {
128 let mut stats = self.stats.lock().unwrap();
130 stats.lines.push_back("\x1b[31m".to_string() + &line + "\x1b[0m");
132 stats.lines.push_back(line);
134 if stats.lines.len() > 150 {
135 stats.lines.pop_front();
139 pub fn set_stat(&self, s: Stat) {
141 Stat::HeaderCount(c) => self.stats.lock().unwrap().header_count = c,
142 Stat::NewConnection => self.stats.lock().unwrap().connection_count += 1,
143 Stat::ConnectionClosed => self.stats.lock().unwrap().connection_count -= 1,
144 Stat::V4RoutingTableSize(c) => self.stats.lock().unwrap().v4_table_size = c,
145 Stat::V6RoutingTableSize(c) => self.stats.lock().unwrap().v6_table_size = c,
146 Stat::RoutingTablePaths(c) => self.stats.lock().unwrap().paths = c,