comparison src/org/dancres/blitz/tools/MonitorStats.java @ 34:6f68e94c1fb8 default tip

Add CondensedStats monitoring utility, equivalent to vmstat
author Dominic Cleal <dominic-cleal@cdo2.com>
date Thu, 05 Aug 2010 11:07:25 +0100
parents 3dc0c5604566
children
comparison
equal deleted inserted replaced
33:0b9265358617 34:6f68e94c1fb8
1 package org.dancres.blitz.tools; 1 package org.dancres.blitz.tools;
2 2
3 import java.io.IOException; 3 import java.io.IOException;
4 4 import java.rmi.RMISecurityManager;
5 import java.rmi.RemoteException; 5 import java.rmi.RemoteException;
6 import java.rmi.RMISecurityManager;
7
8 import net.jini.discovery.*;
9
10 import net.jini.lookup.*;
11 import net.jini.lookup.entry.Name;
12
13 import net.jini.core.lookup.ServiceItem;
14 import net.jini.core.lookup.ServiceTemplate;
15
16 import net.jini.core.entry.Entry;
17 6
18 import net.jini.admin.Administrable; 7 import net.jini.admin.Administrable;
19
20 import net.jini.space.JavaSpace; 8 import net.jini.space.JavaSpace;
21 9
22 import net.jini.core.transaction.TransactionException; 10 import org.dancres.blitz.remote.StatsAdmin;
23 11 import org.dancres.blitz.stats.Stat;
24 import org.dancres.blitz.remote.BlitzAdmin;
25
26 import org.dancres.jini.util.DiscoveryUtil; 12 import org.dancres.jini.util.DiscoveryUtil;
27 import org.dancres.jini.util.ServiceLocator; 13 import org.dancres.jini.util.ServiceLocator;
28
29 import org.dancres.blitz.stats.*;
30
31 import org.dancres.blitz.remote.StatsAdmin;
32 14
33 /** 15 /**
34 <p>MonitorStats accepts a spacename as an argument and a loop time. It then 16 <p>MonitorStats accepts a spacename as an argument and a loop time. It then
35 attempts to list all available statistics on the space every loop time 17 attempts to list all available statistics on the space every loop time
36 seconds. Specifying a loop time of 0 will cause MonitorStats to dump 18 seconds. Specifying a loop time of 0 will cause MonitorStats to dump
48 */ 30 */
49 public class MonitorStats { 31 public class MonitorStats {
50 private static final long MAX_DISCOVER_TIME = 15 * 1000; 32 private static final long MAX_DISCOVER_TIME = 15 * 1000;
51 33
52 public static void main(String args[]) { 34 public static void main(String args[]) {
35 new MonitorStats().startup(args);
36 }
37
38 void startup(String args[]) {
53 if (System.getSecurityManager() == null) 39 if (System.getSecurityManager() == null)
54 System.setSecurityManager(new RMISecurityManager()); 40 System.setSecurityManager(new RMISecurityManager());
55 41
56 try { 42 try {
57 try { 43 try {
91 if (DiscoveryUtil.hasInterface(myAdminProxy, 77 if (DiscoveryUtil.hasInterface(myAdminProxy,
92 StatsAdmin.class)) { 78 StatsAdmin.class)) {
93 StatsAdmin myStatsAdmin = 79 StatsAdmin myStatsAdmin =
94 (StatsAdmin) myAdminProxy; 80 (StatsAdmin) myAdminProxy;
95 81
96 if (myTimeout == 0) { 82 getWatcher(myStatsAdmin, myTimeout).run();
97 Stat[] myStats = myStatsAdmin.getStats();
98
99 System.out.println("Snapshot: " +
100 System.currentTimeMillis());
101 for (int i = 0; i < myStats.length; i++) {
102 System.out.println(myStats[i]);
103 }
104
105 System.out.println();
106
107 } else {
108 new Watcher(myStatsAdmin, myTimeout).start();
109 }
110 } else { 83 } else {
111 System.err.println("No BlitzAdmin interface found - can't be Blitz"); 84 System.err.println("No BlitzAdmin interface found - can't be Blitz");
112 } 85 }
113 } else { 86 } else {
114 System.err.println("No admin interface present - can't be Blitz"); 87 System.err.println("No admin interface present - can't be Blitz");
127 System.err.println("Failed to configure discovery"); 100 System.err.println("Failed to configure discovery");
128 anIOE.printStackTrace(System.err); 101 anIOE.printStackTrace(System.err);
129 } 102 }
130 } 103 }
131 104
132 static class Watcher extends Thread { 105 Runnable getWatcher(StatsAdmin anAdmin, long aTimeout) {
106 return new Watcher(anAdmin, aTimeout);
107 }
108
109 static class Watcher implements Runnable {
133 private StatsAdmin theAdmin; 110 private StatsAdmin theAdmin;
134 private long theTimeout; 111 private long theTimeout;
135 112
136 Watcher(StatsAdmin anAdmin, long aTimeout) { 113 Watcher(StatsAdmin anAdmin, long aTimeout) {
137 theAdmin = anAdmin; 114 theAdmin = anAdmin;
138 theTimeout = aTimeout; 115 theTimeout = aTimeout;
139 } 116 }
140 117
141 public void run() { 118 public void run() {
142 while (true) { 119 do {
143 try { 120 try {
144 Stat[] myStats = theAdmin.getStats(); 121 Stat[] myStats = theAdmin.getStats();
145 122
146 System.out.println("Snapshot: " + 123 System.out.println("Snapshot: " +
147 System.currentTimeMillis()); 124 System.currentTimeMillis());
153 130
154 Thread.sleep(theTimeout); 131 Thread.sleep(theTimeout);
155 } catch (Exception anE) { 132 } catch (Exception anE) {
156 System.err.println(anE); 133 System.err.println(anE);
157 } 134 }
158 } 135 } while (theTimeout > 0); // if timeout == 0, only run once
159 } 136 }
160 } 137 }
161 } 138 }