1 2 23 24 package net.fenyo.gnetwatch.actions; 25 26 import net.fenyo.gnetwatch.*; 27 import net.fenyo.gnetwatch.GUI.VisualElement; 28 import net.fenyo.gnetwatch.actions.Action.InterruptCause; 29 import net.fenyo.gnetwatch.activities.*; 30 import net.fenyo.gnetwatch.data.*; 31 import net.fenyo.gnetwatch.targets.*; 32 33 import java.io.*; 34 import java.net.*; 35 import java.util.*; 36 37 import org.snmp4j.*; 38 import org.snmp4j.smi.*; 39 import org.snmp4j.mp.*; 40 import org.snmp4j.transport.*; 41 import org.snmp4j.util.*; 42 import org.snmp4j.event.*; 43 44 import org.apache.commons.logging.Log; 45 import org.apache.commons.logging.LogFactory; 46 47 53 54 public class ActionSNMP extends Action { 55 private static Log log = LogFactory.getLog(ActionPing.class); 56 57 private boolean interrupted = false; 58 private boolean interfaces_created = false; 59 60 private Map<Integer , TargetInterface> targets = new HashMap<Integer , TargetInterface>(); 61 private Map<Integer , Long > last_total_bytes_received = new HashMap<Integer , Long >(); 62 private Map<Integer , Long > last_total_bytes_sent = new HashMap<Integer , Long >(); 63 private Map<Integer , Long > last_total_bytes_received_time = new HashMap<Integer , Long >(); 64 private Map<Integer , Long > last_total_bytes_sent_time = new HashMap<Integer , Long >(); 65 66 71 public ActionSNMP(final net.fenyo.gnetwatch.targets.Target target, final Background background) { 74 super(target, background); 75 setItem("snmp"); 76 } 77 78 82 public ActionSNMP() { 85 setItem("snmp"); 86 } 87 88 93 public String getQueueName() { 95 return "snmp"; 96 } 97 98 103 public long getMaxDelay() { 105 return 30000000; 106 } 107 108 114 public void interrupt(final InterruptCause cause) { 117 interrupted = true; 118 } 119 120 127 public void invoke() throws IOException, InterruptedException { 130 if (isDisposed() == true) return; 131 132 super.invoke(); 133 134 if (interfaces_created == false) { 135 interfaces_created = true; 136 137 final SNMPQuerier querier; 138 if (TargetIPv4.class.isInstance(getTarget())) { 139 querier = ((TargetIPv4) getTarget()).getSNMPQuerier(); 140 } else if (TargetIPv6.class.isInstance(getTarget())) { 141 querier = ((TargetIPv6) getTarget()).getSNMPQuerier(); 142 } else return; 143 144 getGUI().setStatus(getGUI().getConfig().getPattern("get_if_list", querier.getAddress().toString().substring(1))); 145 final java.util.List <TableEvent> interfaces = querier.getInterfaces(); 146 if (querier.isSNMPCapable()) getTarget().setImageHostSNMP(); 147 for (final TableEvent table_event : interfaces) { 148 try { 149 synchronized (getGUI().sync_tree) { 150 if (table_event == null || table_event.getColumns() == null || table_event.getColumns().length < 2 || 151 table_event.getColumns()[0] == null || table_event.getColumns()[0].getVariable() == null || 152 table_event.getColumns()[1] == null || table_event.getColumns()[1].getVariable() == null) { 153 log.info("can not get the full interface list of " + querier.getAddress().toString().substring(1)); 154 getGUI().setStatus(getGUI().getConfig().getPattern("cannot_get_if_list", querier.getAddress().toString().substring(1))); 155 } else { 156 getGUI().setStatus(getGUI().getConfig().getPattern("received_if_list", querier.getAddress().toString().substring(1))); 157 final TargetInterface foo = 158 new TargetInterface(getTarget().toString() + ".interface[" + 159 table_event.getColumns()[0].getVariable().toString() + "]", 160 ((OctetString) table_event.getColumns()[1].getVariable()).toASCII(' ')); 161 if (getGUI().containsCanonicalInstance(foo)) { 162 targets.put(new Integer (table_event.getColumns()[0].getVariable().toString()), 163 (TargetInterface) getGUI().getCanonicalInstance(foo)); 164 return; 165 } 166 getGUI().asyncExecIfNeeded(new Runnable () { 167 public void run() { 168 synchronized (getGUI().sync_tree) { 169 if (getGUI().containsCanonicalInstance(foo)) return; 170 foo.addTarget(getGUI(), getTarget()); 171 targets.put(new Integer (table_event.getColumns()[0].getVariable().toString()), 172 (TargetInterface) getGUI().getCanonicalInstance(foo)); 173 } 174 } 175 }); 176 } 177 } 178 } catch (final AlgorithmException ex) { 179 log.error("Exception", ex); 180 } 181 } 182 } 183 184 final SNMPQuerier querier; 185 if (TargetIPv4.class.isInstance(getTarget())) { 186 querier = ((TargetIPv4) getTarget()).getSNMPQuerier(); 187 } else if (TargetIPv6.class.isInstance(getTarget())) { 188 querier = ((TargetIPv6) getTarget()).getSNMPQuerier(); 189 } else return; 190 191 final java.util.List <TableEvent> interfaces = querier.getInterfaces(); 192 synchronized (getGUI().sync_tree) { 193 for (final TableEvent table_event : interfaces) { 194 if (table_event == null || table_event.getColumns() == null || table_event.getColumns().length == 0 || 195 table_event.getColumns()[0] == null || table_event.getColumns()[0].getVariable() == null) continue; 196 197 final int interface_index = table_event.getColumns()[0].getVariable().toInt(); 198 final TargetInterface target_interface = targets.get(interface_index); 199 if (target_interface == null) continue; 200 201 getGUI().setStatus(getGUI().getConfig().getPattern("received_snmp", querier.getAddress().toString().substring(1))); 202 203 if (table_event.getColumns().length <= 8 || table_event.getColumns()[8] == null || 204 table_event.getColumns()[8].getVariable() == null) continue; 205 final long total_bytes_received = table_event.getColumns()[8].getVariable().toLong(); 206 207 if (last_total_bytes_received.containsKey(interface_index) == false) { 208 last_total_bytes_received.put(interface_index, total_bytes_received); 209 last_total_bytes_received_time.put(interface_index, System.currentTimeMillis()); 210 } 211 else if (last_total_bytes_received.get(interface_index) != total_bytes_received) { 212 target_interface.addEvent(new EventBytesReceived(total_bytes_received - last_total_bytes_received.get(interface_index))); 213 final long last_time = last_total_bytes_received_time.get(interface_index); 214 final long last_val = last_total_bytes_received.get(interface_index); 215 216 if (target_interface.getChildren().isEmpty() == false && target_interface.getChildren().get(0).getItem().equals("ingress")) 218 target_interface.getChildren().get(0).setDescription("" + (int) ((8 * 1000 * (double) (total_bytes_received - last_val)) / (System.currentTimeMillis() - last_time)) + " bit/s"); 219 220 if (target_interface.getChildren().size() == 2 && target_interface.getChildren().get(1).getItem().equals("ingress")) 221 target_interface.getChildren().get(1).setDescription("" + (int) ((8 * 1000 * (double) (total_bytes_received - last_val)) / (System.currentTimeMillis() - last_time)) + " bit/s"); 222 223 last_total_bytes_received.put(interface_index, total_bytes_received); 224 last_total_bytes_received_time.put(interface_index, System.currentTimeMillis()); 225 } 226 227 if (table_event.getColumns().length <= 9 || table_event.getColumns()[9] == null || 228 table_event.getColumns()[9].getVariable() == null) continue; 229 final long total_bytes_sent = table_event.getColumns()[9].getVariable().toLong(); 230 if (last_total_bytes_sent.containsKey(interface_index) == false) { 231 last_total_bytes_sent.put(interface_index, total_bytes_sent); 232 last_total_bytes_sent_time.put(interface_index, System.currentTimeMillis()); 233 } 234 else if (last_total_bytes_sent.get(interface_index) != total_bytes_sent) { 235 target_interface.addEvent(new EventBytesSent(total_bytes_sent - last_total_bytes_sent.get(interface_index))); 236 final long last_time = last_total_bytes_sent_time.get(interface_index); 237 final long last_val = last_total_bytes_sent.get(interface_index); 238 239 if (target_interface.getChildren().isEmpty() == false && target_interface.getChildren().get(0).getItem().equals("egress")) 240 target_interface.getChildren().get(0).setDescription("" + (int) ((8 * 1000 * (double) (total_bytes_sent - last_val)) / (System.currentTimeMillis() - last_time)) + " bit/s"); 241 if (target_interface.getChildren().size() == 2 && target_interface.getChildren().get(1).getItem().equals("egress")) 242 target_interface.getChildren().get(1).setDescription("" + (int) ((8 * 1000 * (double) (total_bytes_sent - last_val)) / (System.currentTimeMillis() - last_time)) + " bit/s"); 243 244 last_total_bytes_sent.put(interface_index, total_bytes_sent); 245 last_total_bytes_sent_time.put(interface_index, System.currentTimeMillis()); 246 } 247 } 248 } 249 } 250 251 256 protected void disposed() { 257 super.disposed(); 259 260 interrupt(InterruptCause.removed); 262 } 263 } 264 | Popular Tags |