ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/conient/uk/org/iscream/cms/conient/HostDisplayPanel.java
Revision: 1.47
Committed: Tue Feb 25 22:14:20 2003 UTC (21 years, 2 months ago) by tdb
Branch: MAIN
Changes since 1.46: +8 -33 lines
Log Message:
Fixed a bug in Conient - removed the TCP heartbeat watching stuff.

File Contents

# Content
1 /*
2 * i-scream central monitoring system
3 * http://www.i-scream.org.uk
4 * Copyright (C) 2000-2002 i-scream
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20
21 //---PACKAGE DECLARATION---
22 package uk.org.iscream.cms.conient;
23
24 //---IMPORTS---
25 import uk.org.iscream.cms.util.*;
26 import uk.org.iscream.cms.conient.datacomponents.*;
27 import java.util.HashMap;
28 import java.util.Set;
29 import java.util.Collections;
30 import java.util.Iterator;
31 import javax.swing.BoxLayout;
32 import javax.swing.JPanel;
33 import javax.swing.JProgressBar;
34 import javax.swing.JButton;
35 import javax.swing.JLabel;
36 import javax.swing.JFrame;
37 import javax.swing.Box;
38 import java.awt.event.*;
39 import java.awt.*;
40 import javax.swing.SwingUtilities;
41 import javax.swing.ImageIcon;
42
43 /**
44 * A HostDisplayPanel is simply a JPanel containing
45 * DataComponents which relate to various host
46 * attributes. Once created, it can be asked to
47 * update the data it displays by passing it an XML
48 * packet for the host it displaying for.
49 * This then updates all the DataComponents with the
50 * values in the packet.
51 *
52 * @author $Author: tdb $
53 * @version $Id: HostDisplayPanel.java,v 1.46 2003/02/05 19:35:04 tdb Exp $
54 */
55 public class HostDisplayPanel extends JPanel {
56
57 //---FINAL ATTRIBUTES---
58
59 /**
60 * The current CVS revision of this class
61 */
62 public final String REVISION = "$Revision: 1.46 $";
63
64 //---STATIC METHODS---
65
66 //---CONSTRUCTORS---
67
68 /**
69 * Creates a new Host Display Panel and adds
70 * all the appropriate DataComponents
71 */
72 public HostDisplayPanel(String hostName) {
73 super();
74
75 // set up the Panel
76 setLayout(new BorderLayout());
77 _hostName = hostName;
78
79 // check to config to see if we want extra data or not
80 if(Configuration.getInstance().getProperty("displayExtraData").equals("1")) {
81 _extraData = true;
82 }
83
84 GridBagLayout gridbag = new GridBagLayout();
85 GridBagConstraints c = new GridBagConstraints();
86 c.fill = GridBagConstraints.HORIZONTAL;
87 c.anchor = GridBagConstraints.NORTH;
88 c.weighty = 1.0;
89 _center.setLayout(gridbag);
90
91 JPanel centerHolder = new JPanel();
92 centerHolder.add(_center);
93 add(centerHolder, "Center");
94 add(_north, "North");
95
96 addDataComponent(new StringDataComponent("Packet Type", "packet.attributes.type"));
97
98 final StringDataComponent machineName = new StringDataComponent("Host Name", "packet.attributes.machine_name");
99 addDataComponent(machineName );
100 final StringDataComponent ip = new StringDataComponent("IP Address", "packet.attributes.ip");
101 addDataComponent(ip);
102 final UptimeDataComponent uptime = new UptimeDataComponent("Uptime", "packet.os.uptime");
103 addDataComponent(uptime);
104 final StringDataComponent seq_no = new StringDataComponent("Packets since host started", "packet.attributes.seq_no");
105 addDataComponent(seq_no);
106
107 final StringDataComponent osName = new StringDataComponent("Operating System", "packet.os.name");
108 addDataComponent(osName );
109 final StringDataComponent osVer = new StringDataComponent("Operating System Version", "packet.os.version");
110 addDataComponent(osVer);
111 final StringDataComponent osRelease = new StringDataComponent("Operating System Release", "packet.os.release");
112 addDataComponent(osRelease);
113 final StringDataComponent osSysName = new StringDataComponent("System Name", "packet.os.sysname");
114 addDataComponent(osSysName );
115 final StringDataComponent netbiosName = new StringDataComponent("NetBIOS Name", "packet.os.netbios_name");
116 addDataComponent(netbiosName );
117 final StringDataComponent osPlatform = new StringDataComponent("System Architecture", "packet.os.platform");
118 addDataComponent(osPlatform );
119
120 JButton info = new JButton("Platform Information");
121 info.addActionListener(new ActionListener() {
122 public void actionPerformed(ActionEvent e) {
123 final JFrame infoFrame = new JFrame("Host platform information for - " + _hostName);
124 Box content = Box.createVerticalBox();
125 content.add(machineName);
126 content.add(ip);
127 content.add(uptime);
128 content.add(seq_no);
129 content.add(osName);
130 content.add(osVer);
131 content.add(osRelease);
132 content.add(osSysName);
133 content.add(netbiosName);
134 content.add(osPlatform);
135 infoFrame.getContentPane().add(content, "Center");
136 JButton close = new JButton("Close Window");
137 close.addActionListener(new ActionListener() {
138 public void actionPerformed(ActionEvent e) {
139 infoFrame.dispose();
140 }
141 });
142 infoFrame.getContentPane().add(close, "South");
143 infoFrame.setIconImage((new ImageIcon(getClass().getResource("/resources/server.gif"))).getImage());
144 infoFrame.pack();
145 infoFrame.setVisible(true);
146 }
147 });
148 try {
149 _dataTimer = new PacketTimer("Next Data", Integer.parseInt(Configuration.getInstance().getServerProperty("Host." + hostName, "Host.UDPUpdateTime")));
150 } catch (NumberFormatException e) {
151 throw new NumberFormatException("unable to obtain a valid value for Host.UDPUpdateTime");
152 }
153 _north.setLayout(new GridLayout(3,1));
154 _north.add(new JLabel("Showing data from - " + hostName, JLabel.CENTER));
155 _north.add(_dataTimer);
156 _north.add(info);
157
158
159 JPanel date = new JPanel();
160 addVisibleDataComponent(date, new DateDataComponent("Host Time", "packet.attributes.date"));
161 c.gridy = 0;
162 gridbag.setConstraints(date, c);
163 _center.add(date);
164
165 // holder for labels
166 JLabel label;
167
168 label = new javax.swing.JLabel("--- CPU ---", JLabel.CENTER);
169 c.gridy = 1;
170 gridbag.setConstraints(label, c);
171 _center.add(label);
172
173 // add all the DataComponents that we know about
174 JPanel cpu = new JPanel();
175 cpu.setLayout(new BoxLayout(cpu, BoxLayout.Y_AXIS));
176 addVisibleDataComponent(cpu, new CPUDataComponent("CPU Idle", "packet.cpu.idle"));
177 addVisibleDataComponent(cpu, new CPUDataComponent("CPU User", "packet.cpu.user"));
178 addVisibleDataComponent(cpu, new CPUDataComponent("CPU Kernel", "packet.cpu.kernel"));
179 addVisibleDataComponent(cpu, new CPUDataComponent("CPU i/o wait", "packet.cpu.iowait"));
180 addVisibleDataComponent(cpu, new CPUDataComponent("CPU swapping", "packet.cpu.swap"));
181 c.gridy = 2;
182 gridbag.setConstraints(cpu, c);
183 _center.add(cpu);
184
185 label = new javax.swing.JLabel("--- Load ---", JLabel.CENTER);
186 c.gridy = 3;
187 gridbag.setConstraints(label, c);
188 _center.add(label);
189
190 Box loadData = Box.createHorizontalBox();
191 addVisibleDataComponent(loadData, new ProcessesDataComponent("1 minute", "packet.load.load1"));
192 addVisibleDataComponent(loadData, new ProcessesDataComponent("5 minutes", "packet.load.load5"));
193 addVisibleDataComponent(loadData, new ProcessesDataComponent("15 minutes", "packet.load.load15"));
194
195 c.gridy = 4;
196 gridbag.setConstraints(loadData, c);
197 _center.add(loadData);
198
199 label = new javax.swing.JLabel("--- Processes ---", JLabel.CENTER);
200 c.gridy = 5;
201 gridbag.setConstraints(label, c);
202 _center.add(label);
203
204 Box processesData = Box.createHorizontalBox();
205 addVisibleDataComponent(processesData, new ProcessesDataComponent("Total", "packet.processes.total"));
206 addVisibleDataComponent(processesData, new ProcessesDataComponent("Running", "packet.processes.cpu"));
207 addVisibleDataComponent(processesData, new ProcessesDataComponent("Sleeping", "packet.processes.sleeping"));
208 addVisibleDataComponent(processesData, new ProcessesDataComponent("Stopped", "packet.processes.stopped"));
209 addVisibleDataComponent(processesData, new ProcessesDataComponent("Zombie", "packet.processes.zombie"));
210 c.gridy = 6;
211 gridbag.setConstraints(processesData, c);
212 _center.add(processesData);
213
214 label = new javax.swing.JLabel("--- Memory ---", JLabel.CENTER);
215 c.gridy = 7;
216 gridbag.setConstraints(label, c);
217 _center.add(label);
218
219 // these next two are passed the non-visible datacomponents
220 JPanel memory = new JPanel();
221 memory.setLayout(new BoxLayout(memory, BoxLayout.Y_AXIS));
222 addVisibleDataComponent(memory, new StorageDataComponent("Memory in use", "packet.memory.free", "packet.memory.total","Mb"));
223 addVisibleDataComponent(memory, new StorageDataComponent("Swap in use", "packet.swap.free", "packet.swap.total", "Mb"));
224 addVisibleDataComponent(memory, new StringDataComponent("Number of pages in", "packet.pages.swapins"));
225 addVisibleDataComponent(memory, new StringDataComponent("Number of pages out", "packet.pages.swapouts"));
226 // theses next two are DataComponents, but not VisibleDataComponents
227 // this is because they hold the value but do NOT display it
228 // just so we ignore the data, because the StorageDataComponent handles it
229 addDataComponent(new StringDataComponent("packet.memory.total", "packet.memory.total"));
230 addDataComponent(new StringDataComponent("packet.memory.used", "packet.memory.used"));
231 // maybe this one should be used somewhere?
232 addDataComponent(new StringDataComponent("packet.memory.cache", "packet.memory.cache"));
233 addDataComponent(new StringDataComponent("packet.swap.total", "packet.swap.total"));
234 addDataComponent(new StringDataComponent("packet.swap.used", "packet.swap.used"));
235 c.gridy = 8;
236 gridbag.setConstraints(memory, c);
237 _center.add(memory);
238
239 label = new javax.swing.JLabel("--- Users ---", JLabel.CENTER);
240 c.gridy = 9;
241 gridbag.setConstraints(label, c);
242 _center.add(label);
243
244 JPanel users = new JPanel();
245 users.setLayout(new BoxLayout(users, BoxLayout.Y_AXIS));
246 addVisibleDataComponent(users, new StringDataComponent("Number of users", "packet.users.count"));
247 UsersDataComponent userList = new UsersDataComponent("User List", "packet.users.list");
248 addVisibleDataComponent(users, userList);
249 c.gridy = 10;
250 gridbag.setConstraints(users, c);
251 _center.add(users);
252
253 label = new javax.swing.JLabel("--- Disks ---", JLabel.CENTER);
254 c.gridy = 11;
255 gridbag.setConstraints(label, c);
256 _center.add(label);
257 c.gridy = 12;
258 gridbag.setConstraints(_disks, c);
259 _center.add(_disks);
260
261 label = new javax.swing.JLabel("--- System Services ---", JLabel.CENTER);
262 c.gridy = 13;
263 gridbag.setConstraints(label, c);
264 _center.add(label);
265 c.gridy = 14;
266 gridbag.setConstraints(_services, c);
267 _center.add(_services);
268
269 // everything else we get is "extra", but we may still want to display it
270 if (_extraData) {
271 label = new javax.swing.JLabel("--- Extra Data ---", JLabel.CENTER);
272 c.gridy = 15;
273 gridbag.setConstraints(label, c);
274 _center.add(label);
275 c.gridy = 16;
276 gridbag.setConstraints(_extra, c);
277 _center.add(_extra);
278 }
279
280 // a bit of a gap, then a banner to advertise the product
281 c.gridy=17;
282 Component spacer = Box.createRigidArea(new Dimension(468, 30));
283 gridbag.setConstraints(spacer,c);
284 _center.add(spacer);
285 c.gridy=18;
286 JLabel image = new JLabel(new ImageIcon(getClass().getResource("/resources/banner3.gif")));
287 gridbag.setConstraints(image,c);
288 _center.add(image);
289 }
290
291 //---PUBLIC METHODS---
292
293 /**
294 * This method takes an XMLPacket containing
295 * information about the host this HostDisplayPanel
296 * is currently displaying. It then updates all of the
297 * components with the new values.
298 *
299 * It also adds a standard StringDataComponent, should
300 * an attribute come up that there are no DataComponents
301 * associated with.
302 *
303 * @param packet the XML packet
304 * @return if the update was sucessful
305 */
306 public boolean updateHost(XMLPacket packet) {
307 // set that everything is ok so far
308 boolean displaySucessful = true;
309
310 // must be a normal data packet, update the display
311 if((packet.getParam("packet.attributes.type")).equals("data")) {
312 displaySucessful = processPacket(packet);
313 _dataTimer.reset();
314 if(_dataTimerThread == null){
315 _dataTimerThread = new Thread(_dataTimer);
316 _dataTimerThread.start();
317 }
318 }
319 return displaySucessful;
320 }
321
322 //---PRIVATE METHODS---
323
324 /**
325 * Process an incoming packet, updates the components.
326 * If extra data is configured to be displayed, it will
327 * create a new StringDataComponent to display the data.
328 *
329 * @param packet the packet to process
330 *
331 * @return if the proceesing was successful
332 */
333 private boolean processPacket(XMLPacket packet) {
334 // iterate over the packets data
335 boolean displaySucessful = true;
336 Set packetSet = packet.getSet();
337 Iterator i = packetSet.iterator();
338 while (i.hasNext()) {
339 String dataKey = (String) i.next();
340
341 // if there are no components looking after this data
342 // create a new StringDataComponent for it
343 if(!_components.containsKey(dataKey)) {
344 // check if its a disk drive, if it is then we need to deal with it
345 if(dataKey.startsWith("packet.disk.p")) {
346 createDiskPanel(dataKey, packet);
347
348 // !check for services here! to display
349 } else if(dataKey.startsWith("packet.services.")) {
350 createServicesPanel(dataKey);
351
352 } else if (_extraData) {
353 addVisibleDataComponent(_extra, new StringDataComponent(dataKey, dataKey));
354 }
355 }
356
357 // try and update the component, if it doesn't like what it gets
358 // warn the user and set that this was an unsucessful display
359 if(_components.containsKey(dataKey)) {
360
361 try {
362 ((DataComponent) _components.get(dataKey)).setValue(packet);
363 } catch (DataFormatException e) {
364 Conient.addMessage("WARNING{host display}: " + e.getMessage());
365 displaySucessful = false;
366 }
367 }
368 }
369 return displaySucessful;
370 }
371
372 /**
373 * Adds a DataComponent to the list of already
374 * known data components. The list is aware of
375 * what packet attributes that a DataComponent
376 * is looking after.
377 *
378 * @param ref the packet attribute that this Component looks after
379 * @param dataComponent the actual DataComponent
380 */
381 private void addDataComponent(DataComponent dataComponent) {
382 _components.put(dataComponent.getPacketAttribute(), dataComponent);
383 }
384
385 /**
386 * Adds a DataComponent to the list of already
387 * known data components. The list is aware of
388 * what packet attributes that a DataComponent
389 * is looking after.
390 *
391 * Used for VISIBLE data components, as they are added to the display.
392 *
393 * @param ref the packet attribute that this Component looks after
394 * @param dataComponent the actual DataComponent
395 */
396 public void addVisibleDataComponent(Container holder, VisibleDataComponent dataComponent) {
397 _components.put(((DataComponent) dataComponent).getPacketAttribute(), (DataComponent) dataComponent);
398 SwingSafeAdd task = new SwingSafeAdd(holder, dataComponent);
399 SwingUtilities.invokeLater(task);
400 }
401
402 /**
403 * Creates the disk components and adds them
404 * do the list of components. It also displays
405 * any relavant component.
406 *
407 * When a packet attribute is new to the system
408 * and when it starts "packet.disk.p" then this
409 * method is called. Thus it then accounts for
410 * all attributes relating to that new disk.
411 *
412 * @param attribute the name of the attrivbute first encountered
413 * @param packet the XMLPacket the data is being pulled from
414 */
415 private void createDiskPanel(String attribute, XMLPacket packet) {
416 String diskNumber = "";
417
418 // pos is after "packet.disk.p"
419 int pos = 13;
420 while (attribute.charAt(pos) != '.') {
421 diskNumber = diskNumber + attribute.charAt(pos);
422 pos++;
423 }
424 String devAttribute = "packet.disk.p" + diskNumber + ".attributes.name";
425 String mountAttribute = "packet.disk.p" + diskNumber + ".attributes.mount";
426 String totalAttribute = "packet.disk.p" + diskNumber + ".attributes.kbytes";
427
428 // the only visible component
429 String availAttribute = "packet.disk.p" + diskNumber + ".attributes.avail";
430 DiskDataComponent disk = new DiskDataComponent("Disk", availAttribute, totalAttribute, mountAttribute, devAttribute , "Mb", 1024);
431 addVisibleDataComponent(_disks, disk);
432
433 // currently this data isn't useful, so we don't display it
434 // but we add it so that it is forgotten about
435 addDataComponent(new StringDataComponent(devAttribute, devAttribute));
436 addDataComponent(new StringDataComponent(mountAttribute, mountAttribute));
437 addDataComponent(new StringDataComponent(totalAttribute, totalAttribute));
438
439 // add any further bits of data about this disk so we
440 // don't try and do something with them later
441 // -- this is not that tidy ;)
442 Set packetSet = packet.getSet();
443 Iterator i = packetSet.iterator();
444 while (i.hasNext()) {
445 String dataKey = (String) i.next();
446 if(dataKey.startsWith("packet.disk.p" + diskNumber +".") && !_components.containsKey(dataKey)) {
447 addDataComponent(new StringDataComponent(dataKey, dataKey));
448 }
449 }
450 }
451
452 /**
453 * Creates the disk components and adds them
454 * do the list of components. It also displays
455 * any relavant component.
456 *
457 * When a packet attribute is new to the system
458 * and when it starts "packet.disk.p" then this
459 * method is called. Thus it then accounts for
460 * all attributes relating to that new disk.
461 *
462 * @param attribute the name of the attrivbute first encountered
463 */
464 private void createServicesPanel(String attribute) {
465 String serviceType = "";
466
467 // pos is after "packet.services."
468 int pos = 16;
469 while (attribute.charAt(pos) != '.') {
470 serviceType = serviceType + attribute.charAt(pos);
471 pos++;
472 }
473
474 String statusAttribute = "packet.services." + serviceType + ".attributes.status";
475 // we hide this as the service data component uses this attribute
476 addDataComponent(new StringDataComponent(serviceType + " service status", statusAttribute));
477
478 ServiceDataComponent service = new ServiceDataComponent(serviceType, "packet.services." + serviceType + ".attributes.message", statusAttribute);
479 addVisibleDataComponent(_services, service);
480 }
481
482 //---ACCESSOR/MUTATOR METHODS---
483
484 //---ATTRIBUTES---
485
486 /**
487 * The components that we already know about
488 * connected with the attributes they care about
489 * are stored here.
490 */
491 private HashMap _components = new HashMap();
492
493 /**
494 * The north panel, currently used for the platform info button.
495 */
496 private final JPanel _north = new JPanel();
497
498 /**
499 * The centre panel, where all the data is displayed.
500 */
501 private final JPanel _center = new JPanel();
502
503 /**
504 * The countdown timer for normal data packets
505 */
506 private final PacketTimer _dataTimer;
507
508 /**
509 * A place for the data timer thread
510 */
511 private Thread _dataTimerThread = null;
512
513 /**
514 * The hostname this instance is keeping data for
515 */
516 private final String _hostName;
517
518 /**
519 * The disks panel
520 */
521 private final JPanel _disks = new JPanel();
522 {
523 _disks.setLayout(new BoxLayout(_disks, BoxLayout.Y_AXIS));
524 }
525
526 /**
527 * The extra panel, used to display unknown data in the packet
528 */
529 private final JPanel _extra = new JPanel();
530 {
531 _extra.setLayout(new BoxLayout(_extra, BoxLayout.Y_AXIS));
532 }
533
534 /**
535 * The services panel, currently used to display host service information
536 * from heartbeat packets.
537 */
538 private final JPanel _services = new JPanel();
539 {
540 _services.setLayout(new BoxLayout(_services, BoxLayout.Y_AXIS));
541 }
542
543 /**
544 * Whether we a worrying about "extra" data
545 * by default we don't, but we look in the configuration
546 * in the constructor.
547 */
548 private boolean _extraData = false;
549
550 //---STATIC ATTRIBUTES---
551
552 }