1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * ident "%Z%%M% %I% %E% SMI" 24 * 25 * Copyright (c) 2001 by Sun Microsystems, Inc. 26 * All rights reserved. 27 * 28 * stats.java 29 */ 30 31 package com.sun.wbem.solarisprovider.srm; 32 33 import java.util.*; 34 import java.net.InetAddress; 35 import javax.wbem.cim.*; 36 import javax.wbem.client.*; 37 38 public class stats { 39 /* 40 * -u username 41 * -p project 42 * -h host 43 * -I id to auth 44 * -P passwd 45 */ 46 47 CIMClient client = null; 48 String user = new String(""); 49 String project = new String(""); 50 String host = ""; 51 String id = new String(""); 52 String passwd = new String(""); 53 boolean showproject = false; 54 boolean listprops = false; 55 boolean checkonly = false; 56 int intervalms = 5000; 57 58 String usage = new String( 59 "Usage: " + this.getClass().getName() + "\n" + 60 "\t" + "-u userid -p projectname\n" + 61 "\t" + "-r{aw} [property name] -d{elta} [property name]\n" + 62 "\t" + "-L{ist all property names}\n" + 63 "\t" + "-C{heck for existence}\n" + 64 "\t" + "-i [update interval (sec)]\n" + 65 "\t" + "-h host -I auth_user_id -P auth_user_passwd\n" + 66 "\n" + 67 "-I and -P are always required.\n" + 68 "At least one of -r or -d is required unless -L or -C are used.\n" + 69 "If -L is specifed, -r, -d, and -i are ignored.\n" + 70 "If -C is specified, one of -u or -p must be used.\n" + 71 "Default value for -i is " + 72 Integer.toString(intervalms / 1000) + " seconds.\n" + 73 "Default value for -h is \"localhost\"."); 74 75 badarg(String s)76 void badarg(String s) { 77 System.err.println("Ignoring invalid argument \"" + s + "\""); 78 } 79 80 parseargs(String args[])81 void parseargs(String args[]) { 82 int argc = args.length; 83 int i, l; 84 85 for (i = 0; i < argc; i++) { 86 87 if (args[i].startsWith("-u")) { 88 89 // specific user to monitor 90 if ((l = args[i].length()) > 2) 91 user = args[i].substring(2, l); 92 else if ((i < argc - 1) && (!args[i + 1].startsWith("-"))) 93 user = args[++i]; 94 else 95 badarg(args[i]); 96 97 } else if (args[i].startsWith("-p")) { 98 99 // specific project to monitor 100 if ((l = args[i].length()) > 2) 101 project = args[i].substring(2, l); 102 else if ((i < argc - 1) && (!args[i + 1].startsWith("-"))) 103 project = args[++i]; 104 else 105 badarg(args[i]); 106 107 } else if (args[i].startsWith("-h")) { 108 109 // hostname 110 if ((l = args[i].length()) > 2) 111 host = args[i].substring(2, l); 112 else if ((i < argc - 1) && (!args[i + 1].startsWith("-"))) 113 host = args[++i]; 114 else 115 badarg(args[i]); 116 117 } else if (args[i].startsWith("-i")) { 118 119 // update interval 120 String s = Integer.toString(intervalms / 1000); 121 if ((l = args[i].length()) > 2) 122 s = args[i].substring(2, l); 123 else if ((i < argc - 1) && (!args[i + 1].startsWith("-"))) 124 s = args[++i]; 125 else 126 badarg(args[i]); 127 intervalms = Integer.valueOf(s).intValue() * 1000; 128 129 } else if (args[i].startsWith("-I")) { 130 131 // user authentication identity 132 if ((l = args[i].length()) > 2) 133 id = args[i].substring(2, l); 134 else if ((i < argc - 1) && (!args[i + 1].startsWith("-"))) { 135 id = args[++i]; 136 } 137 else 138 badarg(args[i]); 139 140 } else if (args[i].startsWith("-P")) { 141 142 // user authentication password 143 if ((l = args[i].length()) > 2) 144 passwd = args[i].substring(2, l); 145 else if ((i < argc - 1) && (!args[i + 1].startsWith("-"))) 146 passwd = args[++i]; 147 else 148 badarg(args[i]); 149 150 } else if (args[i].startsWith("-r")) { 151 152 // raw property to be monitored 153 String tmp = new String(""); 154 if ((l = args[i].length()) > 2) 155 tmp = args[i].substring(2, l); 156 else if ((i < argc - 1) && (!args[i + 1].startsWith("-"))) 157 tmp = args[++i]; 158 else { 159 badarg(args[i]); 160 continue; 161 } 162 props.addElement(tmp); 163 164 } else if (args[i].startsWith("-d")) { 165 166 // delta property to be monitored 167 String tmp = new String(""); 168 if ((l = args[i].length()) > 2) 169 tmp = args[i].substring(2, l); 170 else if ((i < argc - 1) && (!args[i + 1].startsWith("-"))) 171 tmp = args[++i]; 172 else { 173 badarg(args[i]); 174 continue; 175 } 176 props.addElement(tmp); 177 deltaprops.add(tmp); 178 179 } else if (args[i].startsWith("-L")) { 180 181 // list property names only 182 listprops = true; 183 184 } else if (args[i].startsWith("-C")) { 185 186 // check for existence only 187 checkonly = true; 188 189 } else { 190 191 // unknown arg 192 System.err.println(usage); 193 return; 194 195 } 196 } 197 } 198 199 Vector props = new Vector(); 200 HashSet deltaprops = new HashSet(); 201 printHeader(String s, Vector v)202 void printHeader(String s, Vector v) { 203 System.out.println((showproject ? "Project " : " User ") + s); 204 Enumeration e = v.elements(); 205 while (e.hasMoreElements()) 206 System.out.print((String) e.nextElement() + "\t"); 207 System.out.println(""); 208 } 209 210 211 // compute val1 - val2 delta(CIMValue val1, CIMValue val2)212 String delta(CIMValue val1, CIMValue val2) { 213 String rv = new String("empty"); 214 215 if (val1 == null || val2 == null) 216 return new String("null"); 217 218 Object a = val1.getValue(); 219 Object b = val2.getValue(); 220 221 if (!(a instanceof Number) || !(b instanceof Number)) 222 rv = new String("NAN"); 223 else if (a instanceof UnsignedInt64 && b instanceof UnsignedInt64) { 224 long x = ((UnsignedInt64) a).longValue(); 225 long y = ((UnsignedInt64) b).longValue(); 226 long z = x - y; 227 rv = new String(Long.toString(z)); 228 } else if (a instanceof UnsignedInt32 && b instanceof UnsignedInt32) { 229 int x = ((UnsignedInt32) a).intValue(); 230 int y = ((UnsignedInt32) b).intValue(); 231 int z = x - y; 232 rv = new String(Integer.toString(z)); 233 } else if (a instanceof Float && b instanceof Float) { 234 float x = ((Float) a).floatValue(); 235 float y = ((Float) b).floatValue(); 236 float z = x - y; 237 rv = new String(Float.toString(z)); 238 } else 239 rv = new String("unknown"); 240 241 return rv; 242 } 243 244 checkOnly(String name)245 void checkOnly(String name) { 246 CIMInstance ci = null; 247 248 String className = new String("Solaris_Active"); 249 className += (showproject ? "Project" : "User"); 250 251 CIMObjectPath op = new CIMObjectPath(className); 252 253 if (showproject) { 254 op.addKey("ProjectName", 255 new CIMValue(new String(name))); 256 op.addKey("CreationClassName", 257 new CIMValue("Solaris_ActiveProject")); 258 } else { 259 op.addKey("UserID", 260 new CIMValue(new Integer(name))); 261 op.addKey("CreationClassName", 262 new CIMValue("Solaris_ActiveUser")); 263 } 264 op.addKey("CSCreationClassName", 265 new CIMValue("Solaris_ComputerSystem")); 266 op.addKey("CSName", 267 new CIMValue(host)); 268 op.addKey("OSCreationClassName", 269 new CIMValue("Solaris_OperatingSystem")); 270 op.addKey("OSName", 271 new CIMValue("SunOS")); 272 273 try { 274 ci = client.getInstance(op, false); 275 } catch (CIMException x) { 276 System.err.println(x); 277 Runtime.getRuntime().exit(-1); 278 } 279 280 System.err.println("CI: " + ci); 281 282 Runtime.getRuntime().exit(0); 283 } 284 285 validateProps()286 void validateProps() { 287 // assume User and ProcessAggregate are the same... 288 String className = new String( 289 "Solaris_UserProcessAggregateStatisticalInformation"); 290 CIMObjectPath op = new CIMObjectPath(className); 291 CIMClass c = null; 292 293 try { 294 c = client.getClass(op, false); 295 } catch (CIMException x) { 296 System.err.println(x); 297 Runtime.getRuntime().exit(-1); 298 } 299 300 String propname = null; 301 Enumeration e = props.elements(); 302 while (e.hasMoreElements()) { 303 propname = (String) e.nextElement(); 304 if (c.getProperty(propname) == null) { 305 System.err.println( 306 "Unknown property `" + propname + "' -- try -L."); 307 Runtime.getRuntime().exit(-1); 308 } 309 } 310 } 311 312 listProps()313 void listProps() { 314 // assume User and ProcessAggregate are the same... 315 String className = new String( 316 "Solaris_UserProcessAggregateStatisticalInformation"); 317 318 try { 319 CIMObjectPath op = new CIMObjectPath(className); 320 CIMClass c = client.getClass(op, false); 321 Vector v = c.getAllProperties(); 322 Enumeration e = v.elements(); 323 while (e.hasMoreElements()) { 324 CIMProperty p = (CIMProperty) e.nextElement(); 325 System.out.println("\t" + p.getName()); 326 } 327 } catch (Exception x) { 328 System.out.println(x); 329 } 330 } 331 332 _main(String args[])333 public void _main(String args[]) { 334 String name = new String(""); 335 336 // handle comd-line args 337 parseargs(args); 338 339 // default to localhost 340 if (host.length() == 0) { 341 try { 342 host = new String(InetAddress.getLocalHost().getHostName()); 343 } catch (Exception x) { 344 System.err.println(x); 345 Runtime.getRuntime().exit(-1); 346 } 347 } 348 349 // always need auth id 350 if (id.length() == 0 || passwd.length() == 0) { 351 System.err.println("Username (-I) and password (-P) are required."); 352 System.err.println(usage); 353 Runtime.getRuntime().exit(-1); 354 } 355 356 // initialize client connection to CIMOM 357 try { 358 CIMNameSpace ns = new CIMNameSpace(host); 359 UserPrincipal principal = new UserPrincipal(id); 360 PasswordCredential credential = new PasswordCredential(passwd); 361 client = new CIMClient(ns, principal, credential); 362 // System.err.println("CLIENT: " + client); 363 } catch (CIMException x) { 364 System.err.println(x); 365 Runtime.getRuntime().exit(-1); 366 } 367 368 // just list available properties? 369 if (listprops) { 370 listProps(); 371 Runtime.getRuntime().exit(0); 372 } 373 374 // always need a user id or project name 375 if (user.length() == 0) { 376 if (project.length() == 0) { 377 System.err.println( 378 "User (-u) or project (-p) must be specified."); 379 System.err.println(usage); 380 Runtime.getRuntime().exit(-1); 381 } else { 382 name = project; 383 showproject = true; 384 } 385 } else 386 name = user; 387 388 // just check for entity existence? 389 if (checkonly) { 390 checkOnly(name); 391 Runtime.getRuntime().exit(0); 392 } 393 394 395 // examine specified process aggregate 396 String className = new String("Solaris_"); 397 className += (showproject ? "Project" : "User"); 398 className += "ProcessAggregateStatisticalInformation"; 399 400 // check that all specified props exist 401 validateProps(); 402 403 printHeader(name, props); 404 405 long timestamp = 0; 406 long lastTimestamp = 0; 407 408 Hashtable oldVals = new Hashtable(); 409 Hashtable newVals = new Hashtable(); 410 String p; 411 412 try { 413 414 CIMObjectPath op = new CIMObjectPath(className); 415 op.addKey("Name", new CIMValue(name)); 416 // System.err.println("OP: " + op); 417 418 while (true) { 419 CIMInstance ci = client.getInstance(op, false); 420 // System.err.println("CI: " + ci); 421 422 CIMValue val = ci.getProperty("Timestamp").getValue(); 423 timestamp = ((UnsignedInt64) val.getValue()).longValue(); 424 newVals.put("Timestamp", val); 425 426 if (timestamp > lastTimestamp) { 427 Enumeration e = props.elements(); 428 while (e.hasMoreElements()) { 429 p = (String) e.nextElement(); 430 val = ci.getProperty(p).getValue(); 431 newVals.put(p, val); 432 } 433 lastTimestamp = timestamp; 434 435 e = props.elements(); 436 while (e.hasMoreElements()) { 437 p = (String) e.nextElement(); 438 String s = new String(); 439 CIMValue o = (CIMValue) oldVals.get(p); 440 CIMValue n = (CIMValue) newVals.get(p); 441 442 { 443 String a = "x"; 444 String b = "x"; 445 if (o != null) 446 a = o.toString(); 447 if (n != null) 448 b = n.toString(); 449 // System.out.print(p + ": " + a + "/" + b + "\t"); 450 } 451 452 if (deltaprops.contains(p)) { 453 s = delta(n, o); 454 oldVals.put(p, n); 455 } else { 456 s = n.toString(); 457 } 458 System.out.print(s + "\t"); 459 } 460 System.out.println(); 461 462 } 463 Thread.sleep(intervalms); 464 } 465 466 } catch (Exception x) { 467 x.printStackTrace(); 468 } 469 } 470 471 main(String args[])472 public static void main(String args[]) { 473 stats inst = new stats(); 474 inst._main(args); 475 476 } 477 } 478