2N/A#
2N/A# CDDL HEADER START
2N/A#
2N/A# The contents of this file are subject to the terms of the
2N/A# Common Development and Distribution License (the "License").
2N/A# You may not use this file except in compliance with the License.
2N/A#
2N/A# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A# or http://www.opensolaris.org/os/licensing.
2N/A# See the License for the specific language governing permissions
2N/A# and limitations under the License.
2N/A#
2N/A# When distributing Covered Code, include this CDDL HEADER in each
2N/A# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A# If applicable, add the following below this CDDL HEADER, with the
2N/A# fields enclosed by brackets "[]" replaced with your own identifying
2N/A# information: Portions Copyright [yyyy] [name of copyright owner]
2N/A#
2N/A# CDDL HEADER END
2N/A#
2N/A
2N/A#
2N/A# Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
2N/A#
2N/A
2N/A""" A thin wrapper around the kstats support provided by rad """
2N/A
2N/Afrom __future__ import absolute_import
2N/Aimport rad.client as client
2N/Aimport rad.util as util
2N/Aimport os
2N/A
2N/A_RAD_KSTAT_DOMAIN = "com.oracle.solaris.rad.kstat"
2N/A_RAD_KSTAT_MODULE = "/usr/lib/rad/module/kstat.so"
2N/A
2N/Adef index_kstat_named(ss):
2N/A """
2N/A Given a list of kstat_named, returns a dictionary of its stats
2N/A indexed by name.
2N/A """
2N/A return dict([(x.name, x.value) for x in ss])
2N/A
2N/Aclass Kstats:
2N/A """ Represents the system's kstats """
2N/A
2N/A ctlname = client.Name(_RAD_KSTAT_DOMAIN, [("type", "Control")])
2N/A
2N/A def __init__(self, conn = None):
2N/A if conn is None:
2N/A self._rc = util.connect_unix()
2N/A else:
2N/A self._rc = conn
2N/A
2N/A self._ctl = self._rc.get_object(Kstats.ctlname)
2N/A
2N/A def __safe_wrap(self, name):
2N/A try:
2N/A return self._rc.get_object(name)
2N/A except:
2N/A pass
2N/A return None
2N/A
2N/A @staticmethod
2N/A def connect_private():
2N/A """
2N/A Create a connection to a private (piped) minimalist (kstat module
2N/A only) RAD instance.
2N/A
2N/A Returns:
2N/A
2N/A rad.client.RadConnection: the private connection
2N/A """
2N/A return util.connect_private([ _RAD_KSTAT_MODULE ])
2N/A
2N/A def lookup(self, module, name, instance):
2N/A """
2N/A Look up a single kstat instance by module, name, and instance.
2N/A """
2N/A return self._rc.get_object(
2N/A client.Name(_RAD_KSTAT_DOMAIN, [("type", "Kstat"),
2N/A ("module", module), ("name", name),
2N/A ("instance", "%d" % instance)]))
2N/A
2N/A def lookup_many(self, module = None, name = None, instance = None,
2N/A clazz = None):
2N/A """
2N/A Retrieve a list of kstat instances, filtered by optional module, name,
2N/A and instance.
2N/A """
2N/A keys = [("type", "Kstat")]
2N/A if module is not None:
2N/A keys.append(("module", module))
2N/A if name is not None:
2N/A keys.append(("name", name))
2N/A if instance is not None:
2N/A keys.append(("instance", "%d" % instance))
2N/A if clazz is not None:
2N/A keys.append(("klass", clazz))
2N/A o = self._rc.list_objects(client.Name(_RAD_KSTAT_DOMAIN, keys))
2N/A
2N/A # Enumeration and lookup can race; silently drop failures
2N/A return filter (lambda x : x is not None,
2N/A [ self.__safe_wrap(name) for name in o ])
2N/A
2N/A def update(self):
2N/A """ Call update() on the singleton control instance """
2N/A self._ctl.update()
2N/A
2N/A def close(self):
2N/A """ Close the connection to the RAD server """
2N/A self._rc.close()