tof_control/switch_control/
switch_util.rs1use snmp::{SyncSession, Value};
2
3use crate::helper::switch_type::{SwitchData, SwitchError};
4
5pub fn convert_oid(oid_str: &str) -> Result<Vec<u32>, SwitchError> {
6 let mut oid_trim = oid_str.trim();
7 oid_trim = oid_trim.chars().next().map(|c| &oid_trim[c.len_utf8()..]).unwrap_or(oid_trim);
8 let oid_char: Vec<&str> = oid_trim.split(".").collect();
9
10 let mut oid: Vec<u32> = Default::default();
11 for c in oid_char {
12 oid.push(c.parse::<u32>()?);
13 }
14
15 Ok(oid)
16}
17
18pub fn snmp_get_integer(oid_str: &str, session: &mut SyncSession) -> Result<i64, SwitchError> {
19 let oid = convert_oid(oid_str)?;
20 let mut response = session.getnext(&oid)?;
21 let mut value: i64 = Default::default();
22 if let Some((_oid, Value::Integer(sys_descr))) = response.varbinds.next() {
23 value = sys_descr;
24 }
25
26 Ok(value)
27}
28
29pub fn snmp_get_unsigned32(oid_str: &str, session: &mut SyncSession) -> Result<u32, SwitchError> {
30 let oid = convert_oid(oid_str)?;
31 let mut response = session.getnext(&oid)?;
32 let mut value: u32 = Default::default();
33 if let Some((_oid, Value::Unsigned32(sys_descr))) = response.varbinds.next() {
34 value = sys_descr;
35 }
36
37 Ok(value)
38}
39
40pub fn snmp_get_octetstring(oid_str: &str, session: &mut SyncSession) -> Result<String, SwitchError> {
41 let oid = convert_oid(oid_str)?;
42 let mut response = session.getnext(&oid)?;
43 let mut value: String = Default::default();
44 if let Some((_oid, Value::OctetString(sys_descr))) = response.varbinds.next() {
45 value = String::from_utf8_lossy(sys_descr).to_string();
46 }
47
48 Ok(value)
49}
50
51pub fn snmp_get_octetstring_raw(oid_str: &str, session: &mut SyncSession) -> Result<Vec<u8>, SwitchError> {
52 let oid = convert_oid(oid_str)?;
53 let mut response = session.getnext(&oid)?;
54 let mut value: Vec<u8> = Default::default();
55 if let Some((_oid, Value::OctetString(sys_descr))) = response.varbinds.next() {
56 value = sys_descr.to_vec();
58 }
59
60 Ok(value)
61}
62
63pub fn snmp_getbulk_integer(oid_str: &str, session: &mut SyncSession) -> Result<[u8; 16], SwitchError> {
64 let oid = convert_oid(oid_str)?;
65 let response = session.getbulk(&[&oid], 0, 16)?;
66
67 let mut values: [u8; 16] = Default::default();
68
69 for (i, varbind) in response.varbinds.enumerate() {
70 if let (_oid, Value::Integer(val)) = varbind {
71 values[i] = val as u8;
72 }
73 }
74
75 Ok(values)
76}
77
78pub fn snmp_getbulk_counter64(oid_str: &str, session: &mut SyncSession) -> Result<[u64; 16], SwitchError> {
79 let oid = convert_oid(oid_str)?;
80 let response = session.getbulk(&[&oid], 0, 16)?;
81
82 let mut values: [u64; 16] = Default::default();
83
84 for (i, varbind) in response.varbinds.enumerate() {
85 if let (_oid, Value::Counter64(val)) = varbind {
86 values[i] = val;
87 }
88 }
89
90 Ok(values)
91}
92
93pub fn print_switch_data(switch: &SwitchData) {
94 println!("\tHostname: {}", switch.info.hostname);
96 println!("\tUptime: {}", switch.info.uptime);
97 println!("\tMac Address: {}", switch.info.mac_address);
98 println!("\tCPU Load Avg: {:?}", switch.info.cpu_load);
99 println!("\tPower Supply Status: {:?}", switch.info.ps_status);
100 println!("\tLink: {:?}", switch.port.link);
102 println!("\tSpeed: {:?}", switch.port.speed);
103 println!("\tFull Duplex: {:?}", switch.port.full_duplex);
104 println!("\tRx Bytes: {:?}", switch.port.rx_bytes);
105 println!("\tRx Packets: {:?}", switch.port.rx_pkts);
106 println!("\tRx Dropped Events: {:?}", switch.port.rx_drop_evts);
107 println!("\tRx Broadcast Packets: {:?}", switch.port.rx_broadcast_pkts);
108 println!("\tRx Multicast Packets: {:?}", switch.port.rx_multicast_pkts);
109 println!("\tRx CRC Error Packets: {:?}", switch.port.rx_crc_align_err_pkts);
110 println!("\tTx Bytes: {:?}", switch.port.tx_bytes);
111 println!("\tTx Packets: {:?}", switch.port.tx_pkts);
112 println!("\tTx Dropped Events: {:?}", switch.port.tx_drop_evts);
113 println!("\tTx Broadcast Packets: {:?}", switch.port.tx_broadcast_pkts);
114 println!("\tTx Multicast Packets: {:?}", switch.port.tx_multicast_pkts);
115}
116
117pub fn snmp_set_integer(oid_str: &str, value: i64, session: &mut SyncSession) -> Result<(), SwitchError> {
118 let oid = convert_oid(oid_str)?;
119 let _response = session.set(&[(&oid, Value::Integer(value))])?;
120
121 Ok(())
122}