1 23 24 29 package com.sun.enterprise.jmx.kstat; 30 31 import javax.management.*; 32 33 import java.lang.Runtime ; 34 import java.lang.Process ; 35 import java.io.InputStream ; 36 import java.io.BufferedInputStream ; 37 import java.io.InputStreamReader ; 38 39 import com.sun.cli.util.LineReaderImpl; 40 import java.util.ArrayList ; 41 import java.util.Arrays ; 42 import java.util.ListIterator ; 43 import java.util.Iterator ; 44 import java.util.HashMap ; 45 import java.util.HashSet ; 46 import java.util.Set ; 47 48 import com.sun.cli.util.stringifier.ArrayStringifier; 49 50 final class kstatModule 51 { 52 final String mName; 53 final HashMap mStats; 54 55 public 56 kstatModule( String name ) 57 { 58 mName = name; 59 mStats = new HashMap (); 60 } 61 62 public void 63 add( kstat stat ) 64 { 65 mStats.put( stat.getName(), stat ); 66 } 67 68 public kstat 69 getkstat( String name ) 70 { 71 return( (kstat)mStats.get( name ) ); 72 } 73 74 public String 75 getName() 76 { 77 return( mName ); 78 } 79 80 public Set 81 getNames() 82 { 83 return( mStats.keySet() ); 84 } 85 } 86 87 88 class kstatCache implements kstatRepository 89 { 90 long mRefreshMillis; 91 long mLastRefreshMillis; 92 93 final HashMap mkstats; 94 final HashMap mModules; 95 96 private static void 97 dm( Object o ) 98 { 99 System.out.println( o.toString() ); 100 } 101 102 public 103 kstatCache() 104 { 105 mRefreshMillis = 30 * 1000; 106 mLastRefreshMillis = 0; 107 mkstats = new HashMap (); 108 mModules = new HashMap (); 109 } 110 111 public void 112 clear() 113 { 114 mkstats.clear(); 115 mModules.clear(); 116 } 117 118 private kstat 119 parsekstatAttr( final String line ) 120 { 121 String [] tok = line.split( ":" ); 123 124 kstat stat = null; 127 if ( tok.length == 4 ) 128 { 129 final String moduleName = tok[ 0 ]; 131 final int instanceNumber = Integer.parseInt( tok[ 1 ] ); 132 final String name = tok[ 2 ]; 133 final String attrAll = tok[ 3 ]; 134 135 tok = attrAll.split( "[ \t]+" ); 136 137 if ( tok.length == 2 ) 138 { 139 final String attrName = tok[ 0 ].trim(); 140 final String attrValue = tok[ 1 ].trim(); 141 142 final kstat.kstatAttribute attr = new kstat.kstatAttribute( attrName, attrValue ); 143 144 final String scopedName = 145 kstat.getScopedName( moduleName, instanceNumber, name ); 146 stat = (kstat)mkstats.get( scopedName ); 147 if ( stat == null ) 148 { 149 stat = new kstat( moduleName, instanceNumber, name ); 150 } 151 stat.addAttribute( attr ); 152 } 153 } 154 155 return( stat ); 156 } 157 158 private void 159 add( final kstat stat ) 160 { 161 mkstats.put( stat.getScopedName( ), stat ); 162 163 final String moduleName = stat.getModuleName(); 164 165 kstatModule module = (kstatModule)mModules.get( moduleName ); 166 if ( module == null ) 167 { 168 module = new kstatModule( moduleName ); 169 mModules.put( moduleName, module ); 170 } 171 172 module.add( stat ); 173 } 174 175 private void 176 processResults( String [] lines ) 177 throws java.io.IOException 178 { 179 final ListIterator iter = Arrays.asList( lines ).listIterator( ); 180 181 while( iter.hasNext() ) 182 { 183 final String line = ((String )iter.next()).trim(); 184 185 final kstat stat = parsekstatAttr( line ); 186 if ( stat != null ) 187 { 188 add( stat ); 189 } 190 } 191 } 192 193 private Process 194 invoke_kstat( String args ) 195 throws java.io.IOException 196 { 197 final String execString = "kstat -p " + ((args == null) ? "" : args); 198 199 dm( "invoking kstat as: " + execString ); 200 return( Runtime.getRuntime().exec( execString ) ); 202 } 203 204 205 206 private String [] 207 readResults( final InputStream resultsStream ) 208 throws java.io.IOException 209 { 210 final InputStreamReader reader = new InputStreamReader ( resultsStream ); 211 212 final StringBuffer sbuf = new StringBuffer (); 213 int count; 214 final char [] cbuf = new char [ 256 * 1024 ]; 215 while ( (count = reader.read( cbuf, 0, cbuf.length)) >= 0 ) 216 { 217 sbuf.append( cbuf, 0, count ); 218 } 219 220 return( sbuf.toString().split( "\n" ) ); 221 } 222 223 public void 224 refresh( String scopedName ) 225 throws java.io.IOException 226 { 227 if ( scopedName == null || scopedName.equals( "" ) ) 228 { 229 clear(); 230 } 231 232 final Process proc = invoke_kstat( scopedName ); 233 234 final InputStream resultsStream = proc.getInputStream(); 235 236 final String [] outputLines = readResults( resultsStream ); 237 238 processResults( outputLines ); 239 240 try 241 { 242 proc.waitFor(); 243 } 244 catch( InterruptedException e ) 245 { 246 System.err.println( "Interrupted: " + e.toString() ); 247 } 248 249 mLastRefreshMillis = System.currentTimeMillis(); 250 } 251 252 public void 253 refresh() 254 throws java.io.IOException 255 { 256 refresh( "" ); 257 } 258 259 void 260 maybeRefresh() 261 { 262 if ( ( System.currentTimeMillis() - mLastRefreshMillis ) > mRefreshMillis ) 263 { 264 try 265 { 266 refresh(); 267 } 268 catch( java.io.IOException e ) 269 { 270 System.err.println("couldn't refresh kstat" ); 271 } 272 } 273 } 274 275 public void 276 setRefreshMillis( long millis) 277 { 278 mRefreshMillis = millis; 279 maybeRefresh(); 280 } 281 282 public Set 283 getModuleNames() 284 { 285 maybeRefresh(); 286 287 return( mModules.keySet() ); 288 } 289 290 public Set 291 getNamesInModule( String moduleName ) 292 { 293 maybeRefresh(); 294 295 Set names = null; 296 297 final kstatModule module = (kstatModule)mModules.get( moduleName ); 298 299 if ( module != null ) 300 { 301 names = module.getNames(); 302 } 303 return( names ); 304 } 305 306 public kstat 307 getkstat( String moduleName, String name ) 308 { 309 maybeRefresh(); 310 311 final kstatModule module = (kstatModule)mModules.get( moduleName ); 312 kstat stat = null; 313 if ( module != null ) 314 { 315 stat = module.getkstat( name ); 316 } 317 318 return( stat ); 319 } 320 321 public String 322 query_kstatAttribute( String module, int instance, String name, String attributeName ) 323 { 324 maybeRefresh(); 325 return( null ); 326 } 327 }; 328 329 330 333 public final class kstatMgr implements kstatMgrMBean, MBeanRegistration 334 { 335 MBeanServer mServer; 336 ObjectName mMyName; 337 final kstatCache mCache; 338 339 public final static String KSTAT_DOMAIN = "kstat"; 340 341 private static void 342 dm( Object o ) 343 { 344 System.out.println( o.toString() ); 345 } 346 347 public 348 kstatMgr() 349 { 350 mCache = new kstatCache(); 351 mServer = null; 352 mMyName = null; 353 } 355 356 357 public ObjectName 358 preRegister(MBeanServer server, ObjectName name) 359 { 360 mServer = server; 361 mMyName = name; 362 363 return( mMyName ); 364 } 365 366 public void 367 postRegister( Boolean registrationDone ) 368 { 369 } 370 371 public void 372 preDeregister() 373 { 374 } 376 public void 377 postDeregister( ) 378 { 379 } 381 382 383 private static final String TYPE_PROPERTY = "type=kstat"; 384 private static final String NAME_PROPERTY= "name"; 385 private static final String MODULE_PROPERTY = "kstat-module"; 386 private static final String INSTANCE_PROPERTY = "kstat-instance"; 387 private static final String KSTAT_NAME_PROPERTY = "kstat-name"; 388 private static final String CLASS_PROPERTY = "kstat-class"; 389 private static final char PROPERTY_DELIM = ','; 390 private static final char PROPERTY_VALUE_DELIM = '='; 391 392 393 private String 394 mapkstatName( final String name) 395 { 396 String result = name; 397 398 if ( name.indexOf( ',' ) >= 0 ) 399 { 400 final char [] chars = name.toCharArray(); 401 402 for( int i = 0; i < chars.length; ++i ) 403 { 404 if ( chars[ i ] == ',' ) 405 { 406 chars[ i ] = '.'; 407 } 408 } 409 410 result = new String ( chars ); 411 412 } 413 return( result ); 414 } 415 416 private String 417 createObjectNameString( String domain, final kstat stat ) 418 { 419 final String kstatName = mapkstatName( stat.getName() ); 421 422 final String nameProperty = stat.getModuleName() + "." + kstatName; 423 424 final String name = domain + ":" + 425 TYPE_PROPERTY + PROPERTY_DELIM + 426 NAME_PROPERTY + PROPERTY_VALUE_DELIM + nameProperty + PROPERTY_DELIM + 427 KSTAT_NAME_PROPERTY + PROPERTY_VALUE_DELIM + kstatName + PROPERTY_DELIM + 428 MODULE_PROPERTY + PROPERTY_VALUE_DELIM + stat.getModuleName(); 429 430 434 435 return( name ); 436 } 437 438 private void 439 addMBeanFor_kstat( final kstat stat ) 440 { 441 String objectNameString = createObjectNameString( KSTAT_DOMAIN, stat ); 442 443 try 444 { 445 final kstatMBean mb = new kstatMBean( this, stat ); 446 447 final ObjectName objectName = new ObjectName( objectNameString ); 448 449 unregisterMBean( objectName ); 450 mServer.registerMBean( mb, objectName ); 451 } 452 catch( Exception e ) 453 { 454 System.err.println( "Can't add object named: " + objectNameString ); 455 456 System.err.println( e.getMessage() ); 457 } 458 } 459 460 private void 461 addMBeansForModule( final String moduleName ) 462 { 463 final Set names = mCache.getNamesInModule( moduleName ); 464 final Iterator iter = names.iterator(); 465 466 while ( iter.hasNext() ) 467 { 468 addMBeanFor_kstat( mCache.getkstat( moduleName, (String )iter.next() ) ); 469 } 470 } 471 472 private void 473 addMBeansForModules( final Set moduleNames ) 474 throws Exception 475 { 476 final Iterator iter = moduleNames.iterator(); 477 478 while ( iter.hasNext() ) 479 { 480 addMBeansForModule( (String )iter.next() ); 481 } 482 } 483 484 private void 485 unregisterMBean( ObjectName name ) 486 { 487 try 488 { 489 mServer.unregisterMBean( name ); 490 } 491 catch( InstanceNotFoundException e ) 492 { 493 } 495 catch( MBeanRegistrationException e ) 496 { 497 } 499 } 500 501 private void 502 unregisterAll( Iterator objectNames ) 503 throws MBeanRegistrationException 504 { 505 while ( objectNames.hasNext() ) 506 { 507 unregisterMBean( (ObjectName)objectNames.next() ); 508 } 509 } 510 511 513 public synchronized void 514 initkstats() 515 throws Exception 516 { 517 refresh(); 518 } 519 520 public synchronized void 521 clearkstats() 522 { 523 mCache.clear(); 524 525 try 526 { 527 final ObjectName pattern = new ObjectName( KSTAT_DOMAIN + ":type=kstat,*" ); 528 final Set allkstatMBeans = mServer.queryNames( pattern, null ); 529 530 unregisterAll( allkstatMBeans.iterator() ); 531 } 532 catch( MalformedObjectNameException e ) 533 { 534 assert( false ); 536 } 537 catch( MBeanRegistrationException e ) 538 { 539 assert( false ); 541 } 542 } 543 544 545 public void 546 refresh( String scopedName ) 547 throws Exception 548 { 549 mCache.refresh( scopedName ); 550 551 final Set moduleNames = mCache.getModuleNames(); 552 addMBeansForModules( moduleNames ); 553 } 554 555 public void 556 refresh( ) 557 throws Exception 558 { 559 clearkstats(); 560 561 refresh( null ); 562 } 563 }; 564 565 566 | Popular Tags |