snmp: convert to jsonbuilder

Closes redmine ticket 3756.
pull/5188/head
Shivani Bhardwaj 5 years ago committed by Victor Julien
parent 5d0d630237
commit 72dab0a8b7

@ -17,7 +17,7 @@
// written by Pierre Chifflier <chifflier@wzdftpd.net> // written by Pierre Chifflier <chifflier@wzdftpd.net>
use crate::json::*; use crate::jsonbuilder::{JsonBuilder, JsonError};
use crate::snmp::snmp::{SNMPState,SNMPTransaction}; use crate::snmp::snmp::{SNMPState,SNMPTransaction};
use crate::snmp::snmp_parser::{NetworkAddress,PduType}; use crate::snmp::snmp_parser::{NetworkAddress,PduType};
use std::borrow::Cow; use std::borrow::Cow;
@ -37,48 +37,51 @@ fn str_of_pdu_type(t:&PduType) -> Cow<str> {
} }
} }
#[no_mangle] fn snmp_log_response(jsb: &mut JsonBuilder, state: &mut SNMPState, tx: &mut SNMPTransaction) -> Result<(), JsonError>
pub extern "C" fn rs_snmp_log_json_response(state: &mut SNMPState, tx: &mut SNMPTransaction) -> *mut JsonT
{ {
let js = Json::object(); jsb.set_uint("version", state.version as u64)?;
js.set_integer("version", state.version as u64);
if tx.encrypted { if tx.encrypted {
js.set_string("pdu_type", "encrypted"); jsb.set_string("pdu_type", "encrypted")?;
} else { } else {
match tx.info { match tx.info {
Some(ref info) => { Some(ref info) => {
js.set_string("pdu_type", &str_of_pdu_type(&info.pdu_type)); jsb.set_string("pdu_type", &str_of_pdu_type(&info.pdu_type))?;
if info.err.0 != 0 { if info.err.0 != 0 {
js.set_string("error", &format!("{:?}", info.err)); jsb.set_string("error", &format!("{:?}", info.err))?;
} }
match info.trap_type { match info.trap_type {
Some((trap_type, ref oid, address)) => { Some((trap_type, ref oid, address)) => {
js.set_string("trap_type", &format!("{:?}", trap_type)); jsb.set_string("trap_type", &format!("{:?}", trap_type))?;
js.set_string("trap_oid", &oid.to_string()); jsb.set_string("trap_oid", &oid.to_string())?;
match address { match address {
NetworkAddress::IPv4(ip) => js.set_string("trap_address", &ip.to_string()) NetworkAddress::IPv4(ip) => {jsb.set_string("trap_address", &ip.to_string())?;},
} }
}, },
_ => () _ => ()
} }
if info.vars.len() > 0 { if info.vars.len() > 0 {
let jsa = Json::array(); jsb.open_array("vars")?;
for var in info.vars.iter() { for var in info.vars.iter() {
jsa.array_append_string(&var.to_string()); jsb.append_string(&var.to_string())?;
} }
js.set("vars", jsa); jsb.close()?;
} }
}, },
_ => () _ => ()
} }
match tx.community { if let Some(community) = &tx.community {
Some(ref c) => js.set_string("community", c), jsb.set_string("community", community)?;
_ => ()
} }
match tx.usm { if let Some(usm) = &tx.usm {
Some(ref s) => js.set_string("usm", s), jsb.set_string("usm", usm)?;
_ => ()
} }
} }
js.unwrap()
return Ok(());
}
#[no_mangle]
pub extern "C" fn rs_snmp_log_json_response(jsb: &mut JsonBuilder, state: &mut SNMPState, tx: &mut SNMPTransaction) -> bool
{
snmp_log_response(jsb, state, tx).is_ok()
} }

@ -64,29 +64,28 @@ static int JsonSNMPLogger(ThreadVars *tv, void *thread_data,
{ {
SNMPTransaction *snmptx = tx; SNMPTransaction *snmptx = tx;
LogSNMPLogThread *thread = thread_data; LogSNMPLogThread *thread = thread_data;
json_t *js, *snmpjs;
js = CreateJSONHeader(p, LOG_DIR_PACKET, "snmp", NULL); JsonBuilder *jb = CreateEveHeader(p, LOG_DIR_PACKET, "snmp", NULL);
if (unlikely(js == NULL)) { if (unlikely(jb == NULL)) {
return TM_ECODE_FAILED; return TM_ECODE_FAILED;
} }
JsonAddCommonOptions(&thread->snmplog_ctx->cfg, p, f, js); EveAddCommonOptions(&thread->snmplog_ctx->cfg, p, f, jb);
snmpjs = rs_snmp_log_json_response(state, snmptx); jb_open_object(jb, "snmp");
if (unlikely(snmpjs == NULL)) { if (!rs_snmp_log_json_response(jb, state, snmptx)) {
goto error; goto error;
} }
json_object_set_new(js, "snmp", snmpjs); jb_close(jb);
MemBufferReset(thread->buffer); MemBufferReset(thread->buffer);
OutputJSONBuffer(js, thread->snmplog_ctx->file_ctx, &thread->buffer); OutputJsonBuilderBuffer(jb, thread->snmplog_ctx->file_ctx, &thread->buffer);
json_decref(js); jb_free(jb);
return TM_ECODE_OK; return TM_ECODE_OK;
error: error:
json_decref(js); jb_free(jb);
return TM_ECODE_FAILED; return TM_ECODE_FAILED;
} }

Loading…
Cancel
Save