use rustmann::*;
use structopt::StructOpt;
#[derive(Debug, StructOpt)]
struct ConnOpt {
#[structopt(short, default_value = "127.0.0.1")]
host: String,
#[structopt(short, default_value = "5555")]
port: u16,
}
#[derive(Debug, StructOpt)]
#[structopt(
name = "riemannc",
about = "A simple commandline interface for riemann."
)]
enum Opt {
Query {
query: String,
#[structopt(flatten)]
conn: ConnOpt,
},
Send {
#[structopt(long)]
hostname: Option<String>,
#[structopt(short)]
time: Option<i64>,
service: String,
#[structopt(long)]
tags: Option<String>,
#[structopt(long)]
ttl: Option<f32>,
#[structopt(long)]
state: Option<String>,
metric: f64,
#[structopt(flatten)]
conn: ConnOpt,
},
}
fn to_client_options(conn: &ConnOpt) -> RiemannClientOptions {
RiemannClientOptionsBuilder::default()
.host(&conn.host)
.port(conn.port)
.build()
}
#[tokio::main]
async fn main() -> Result<(), RiemannClientError> {
let opt = Opt::from_args();
match opt {
Opt::Query { query, conn } => {
let client = RiemannClient::new(&to_client_options(&conn));
let resp = client.send_query(query).await?;
println!("{:?}", resp);
}
Opt::Send {
service,
metric,
hostname,
time,
state,
ttl,
tags,
conn,
} => {
let client = RiemannClient::new(&to_client_options(&conn));
let mut event_builder = EventBuilder::new();
event_builder = event_builder.service(service).metric_d(metric);
if let Some(hostname) = hostname {
event_builder = event_builder.host(hostname);
}
if let Some(time) = time {
event_builder = event_builder.time(time);
}
if let Some(state) = state {
event_builder = event_builder.state(state);
}
if let Some(ttl) = ttl {
event_builder = event_builder.ttl(ttl);
}
if let Some(tags) = tags {
for t in tags.split(",") {
event_builder = event_builder.add_tag(t);
}
}
let resp = client.send_events(vec![event_builder.build()]).await?;
println!("{:?}", resp);
}
}
Ok(())
}