1 2 23 24 package net.fenyo.gnetwatch.actions; 25 26 import net.fenyo.gnetwatch.*; 27 import net.fenyo.gnetwatch.GUI.GUI; 28 import net.fenyo.gnetwatch.actions.Action.InterruptCause; 29 import net.fenyo.gnetwatch.activities.Background; 30 import net.fenyo.gnetwatch.data.*; 31 import net.fenyo.gnetwatch.targets.*; 32 33 import java.io.*; 34 import java.util.regex.Matcher ; 35 import java.util.regex.Pattern ; 36 37 import org.apache.commons.logging.Log; 38 import org.apache.commons.logging.LogFactory; 39 40 46 47 public class ActionPing extends Action { 48 private static Log log = LogFactory.getLog(ActionPing.class); 49 50 private ExternalCommand cmd_ping = null; 53 54 59 public ActionPing(final Target target, final Background background) { 62 super(target, background); 63 setItem("ping"); 64 } 65 66 70 public ActionPing() { 73 setItem("ping"); 74 } 75 76 81 protected void initialize(final GUI gui) { 82 super.initialize(gui); 83 if (gui != null) setImageWatch(); 84 } 85 86 91 public String getQueueName() { 93 return "icmp"; 94 } 95 96 101 public long getMaxDelay() { 103 return 4000; 104 } 105 106 112 public void interrupt(final InterruptCause cause) throws IOException { 115 if (cmd_ping != null) cmd_ping.end(); 116 } 117 118 125 public void invoke() throws IOException, InterruptedException { 128 if (isDisposed() == true) return; 129 130 try { 131 super.invoke(); 132 133 if (TargetIPv4.class.isInstance(getTarget())) { 134 final TargetIPv4 target = (TargetIPv4) getTarget(); 135 String address = target.getAddress().toString(); 136 address = address.substring(1 + address.indexOf('/')); 138 139 final String [] cmdLine = new String [] { 140 "ping", getGUI().getConfig().getProperty("ping.countparameter"), "1", address 141 }; 143 String cmd_line = ""; 144 for (final String part : cmdLine) cmd_line += part + " "; 145 getGUI().setStatus(getGUI().getConfig().getPattern("forking", cmd_line)); 147 cmd_ping = new ExternalCommand(cmdLine, true); 148 cmd_ping.fork(); 149 150 final String cmd_output = cmd_ping.readStdout(); 151 final Matcher match = 152 Pattern.compile(address + getGUI().getConfig().getProperty("ping.regex")).matcher(cmd_output); 153 if (match.find() == true) { 154 final int delay = new Integer (match.group(2)).intValue(); 155 getTarget().addEvent(new EventReachable(true, new Integer (match.group(2)).intValue())); 156 getGUI().setStatus(getGUI().getConfig().getPattern("received_icmp", address)); 157 setDescription("rtt: " + delay + " ms"); 158 } else { 159 getTarget().addEvent(new EventReachable(false)); 160 getGUI().setStatus(getGUI().getConfig().getPattern("received_icmp_timeout", address)); 161 setDescription(getGUI().getConfig().getString("unreachable")); 162 } 163 cmd_ping.end(); 164 } 165 166 if (TargetIPv6.class.isInstance(getTarget())) { 167 final TargetIPv6 target = (TargetIPv6) getTarget(); 168 String address = target.getAddress().toString(); 169 address = address.substring(1 + address.indexOf('/')); 171 172 final String [] cmdLine = new String [] { 173 "ping6", getGUI().getConfig().getProperty("ping.countparameter"), "1", address 174 }; 176 cmd_ping = new ExternalCommand(cmdLine, true); 177 cmd_ping.fork(); 178 179 final String cmd_output = cmd_ping.readStdout(); 180 final Matcher match = 182 Pattern.compile("(.|\r|\n)*:.*?([0-9]+)[^0-9]*ms(.|\r|\n)*").matcher(cmd_output); 183 if (match.find() == true) { 184 final int delay = new Integer (match.group(2)).intValue(); 185 getTarget().addEvent(new EventReachable(true, new Integer (match.group(2)).intValue())); 186 setDescription("rtt: " + delay + " ms"); 187 } else { 188 getTarget().addEvent(new EventReachable(false)); 189 setDescription(getGUI().getConfig().getString("unreachable")); 190 } 191 cmd_ping.end(); 192 } 193 194 } catch (final InterruptedException ex) { 195 getTarget().addEvent(new EventReachable(false)); 196 setDescription(getGUI().getConfig().getString("unreachable")); 197 throw ex; 198 } 199 } 200 201 206 protected void disposed() { 207 super.disposed(); 209 210 try { 212 interrupt(InterruptCause.removed); 213 } catch (final IOException ex) { 214 log.error("Exception", ex); 215 } 216 } 217 } 218 | Popular Tags |