usr/src/lib/pykstat/rad.py
changeset 604 20d9acfeb7fb
child 691 d880d1c2a97d
equal deleted inserted replaced
603:71a20acea802 604:20d9acfeb7fb
       
     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 (the "License").
       
     6 # You may not use this file except in compliance with the License.
       
     7 #
       
     8 # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
       
     9 # or http://www.opensolaris.org/os/licensing.
       
    10 # See the License for the specific language governing permissions
       
    11 # and limitations under the License.
       
    12 #
       
    13 # When distributing Covered Code, include this CDDL HEADER in each
       
    14 # file and include the License file at usr/src/OPENSOLARIS.LICENSE.
       
    15 # If applicable, add the following below this CDDL HEADER, with the
       
    16 # fields enclosed by brackets "[]" replaced with your own identifying
       
    17 # information: Portions Copyright [yyyy] [name of copyright owner]
       
    18 #
       
    19 # CDDL HEADER END
       
    20 #
       
    21 
       
    22 #
       
    23 # Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
       
    24 #
       
    25 
       
    26 """ A thin wrapper around the kstats support provided by rad """
       
    27 
       
    28 from __future__ import absolute_import
       
    29 import rad.client as client
       
    30 import rad.adaptor as adaptor
       
    31 import rad.util as util
       
    32 import os
       
    33 
       
    34 _RAD_KSTAT_DOMAIN = "com.oracle.solaris.kstat"
       
    35 _RAD_KSTAT_MODULE = "/usr/lib/rad/module/kstat.so"
       
    36 
       
    37 # The rad interface doesn't support discriminated unions, so the kstat
       
    38 # module fakes one up with a traditional structure.  Snapshot and
       
    39 # StatAdaptor transform that structure to something more pythonic.
       
    40 
       
    41 class Snapshot:
       
    42 	def __init__(self, value):
       
    43 		self._timestamp = value.timestamp
       
    44 		self._stats = {}
       
    45 		for i in value.statistics:
       
    46 			if i.type == "char":
       
    47 				self._stats[i.name] = i.value.str
       
    48 			elif i.type == "int32":
       
    49 				self._stats[i.name] = i.value.i32
       
    50 			elif i.type == "uint32":
       
    51 				self._stats[i.name] = i.value.ui32
       
    52 			elif i.type == "int64":
       
    53 				self._stats[i.name] = i.value.i64
       
    54 			elif i.type == "uint64":
       
    55 				self._stats[i.name] = i.value.ui64
       
    56 			elif i.type == "float":
       
    57 				self._stats[i.name] = i.value.f
       
    58 			elif i.type == "double":
       
    59 				self._stats[i.name] = i.value.d
       
    60 			elif i.type == "string":
       
    61 				self._stats[i.name] = i.value.str
       
    62 
       
    63 	def timestamp(self):
       
    64 		return self._timestamp
       
    65 
       
    66 	def get(self, stat):
       
    67 		return self._stats[stat]
       
    68 
       
    69 class StatAdaptor(adaptor.Adaptor):
       
    70 	def __init__(self, statobj):
       
    71 		adaptor.Adaptor.__init__(self, statobj)
       
    72 		old_fresh = self.__dict__["get_fresh_value"]
       
    73 		self.__dict__["get_fresh_value"] = lambda: Snapshot(old_fresh())
       
    74 
       
    75 	def __getattr__(self, attr):
       
    76 		result = adaptor.Adaptor.__getattr__(self, attr)
       
    77 		if attr == "value":
       
    78 			return Snapshot(result)
       
    79 		return result
       
    80 
       
    81 class Kstats:
       
    82 	""" Represents the system's kstats """
       
    83 
       
    84 	ctlname = client.Name(_RAD_KSTAT_DOMAIN, [("type", "control")])
       
    85 
       
    86 	def __init__(self):
       
    87 		self._rc = util.connect_private([ _RAD_KSTAT_MODULE ])
       
    88 		self._ctl = adaptor.Adaptor(self._rc.get_object(Kstats.ctlname))
       
    89 
       
    90 	def lookup(self, module, name, instance):
       
    91 		return StatAdaptor(self._rc.get_object(
       
    92 		    client.Name(_RAD_KSTAT_DOMAIN, [("type", "kstat"),
       
    93 		    ("module", module), ("name", name),
       
    94 		    ("instance", "%d" % instance)])))
       
    95 
       
    96 	def lookup_many(self, module = None, name = None, instance = None,
       
    97 	    clazz = None):
       
    98 		keys = [("type", "kstat")]
       
    99 		if module:
       
   100 			keys.append(("module", module))
       
   101 		if name:
       
   102 			keys.append(("name", name))
       
   103 		if instance:
       
   104 			keys.append(("instance", "%d" % instance))
       
   105 		if clazz:
       
   106 			keys.append(("class", clazz))
       
   107 		o = self._rc.list_objects(client.Name(_RAD_KSTAT_DOMAIN, keys))
       
   108 
       
   109 		return [ StatAdaptor(self._rc.get_object(i)) for i in o ]
       
   110 
       
   111 	def update(self):
       
   112 		self._ctl.update_all()
       
   113 
       
   114 	def close(self):
       
   115 		self._rc.close()