KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > fenyo > gnetwatch > actions > ActionSNMP


1
2 /*
3  * GNetWatch
4  * Copyright 2006, 2007 Alexandre Fenyo
5  * gnetwatch@fenyo.net
6  *
7  * This file is part of GNetWatch.
8  *
9  * GNetWatch is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * GNetWatch is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with GNetWatch; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22  */

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 /**
48  * Instances of this action class use SNMP to get the interface list of their target,
49  * and to get SNMP counters for each interface.
50  * @author Alexandre Fenyo
51  * @version $Id: ActionSNMP.java,v 1.21 2007/03/12 05:04:14 fenyo Exp $
52  */

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 JavaDoc, TargetInterface> targets = new HashMap<Integer JavaDoc, TargetInterface>();
61   private Map<Integer JavaDoc, Long JavaDoc> last_total_bytes_received = new HashMap<Integer JavaDoc, Long JavaDoc>();
62   private Map<Integer JavaDoc, Long JavaDoc> last_total_bytes_sent = new HashMap<Integer JavaDoc, Long JavaDoc>();
63   private Map<Integer JavaDoc, Long JavaDoc> last_total_bytes_received_time = new HashMap<Integer JavaDoc, Long JavaDoc>();
64   private Map<Integer JavaDoc, Long JavaDoc> last_total_bytes_sent_time = new HashMap<Integer JavaDoc, Long JavaDoc>();
65
66   /**
67    * Constructor.
68    * @param target target this action works on.
69    * @param background queue manager by which this action will add events.
70    */

71   // GUI thread
72
// supports any thread
73
public ActionSNMP(final net.fenyo.gnetwatch.targets.Target target, final Background background) {
74     super(target, background);
75     setItem("snmp");
76   }
77
78   /**
79    * Constructor.
80    * @param none.
81    */

82   // GUI thread
83
// supports any thread
84
public ActionSNMP() {
85     setItem("snmp");
86   }
87
88   /**
89    * Returns the associated target.
90    * @param none.
91    * @return Target associated target.
92    */

93   // any thread
94
public String JavaDoc getQueueName() {
95     return "snmp";
96   }
97
98   /**
99    * Returns the timeout associated with this action.
100    * @param none.
101    * @return long timeout.
102    */

103   // any thread
104
public long getMaxDelay() {
105     return 30000000;
106   }
107
108   /**
109    * Asks this action to stop rapidely.
110    * @param cause cause.
111    * @return void.
112    * @throws IOException IO exception.
113    */

114   // main & Background threads
115
// supports any thread
116
public void interrupt(final InterruptCause cause) {
117     interrupted = true;
118   }
119
120   /**
121    * Gets the interface list to create interfaces and gets snmp counters.
122    * @param none.
123    * @return void.
124    * @throws IOException IO exception.
125    * @throws InterruptedException exception.
126    */

127   // Queue thread
128
// supports any thread
129
public void invoke() throws IOException, InterruptedException JavaDoc {
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 JavaDoc<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 JavaDoc(table_event.getColumns()[0].getVariable().toString()),
163                     (TargetInterface) getGUI().getCanonicalInstance(foo));
164                 return;
165               }
166               getGUI().asyncExecIfNeeded(new Runnable JavaDoc() {
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 JavaDoc(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 JavaDoc<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           // limitation : seulement 2 fils pour les interfaces - il faudrait chercher les fils concernés : attention : locker
217
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   /**
252    * Called when this element is being removed.
253    * @param none.
254    * @return void.
255    */

256   protected void disposed() {
257     // remove us from the queue
258
super.disposed();
259
260     // interrupt if currently running
261
interrupt(InterruptCause.removed);
262   }
263 }
264
Popular Tags