def svcs(self): try: ret = sh.run_cmd('/bin/svcs -ZaHo zone,state') except: ret = sh.run_cmd('/bin/svcs -aHo state') return ret
def count_items(self, pool, item_type): """ Count the number of datasets or snapshots in the pool. :param pool: the pool to examine. (strin) :param item_type: what you want the number of. Can be 'filesystem', 'snapshot', 'volume', or 'all'. (string) """ return len( sh.run_cmd('/usr/sbin/zfs list -rH -t %s %s' % (item_type, pool)))
def pagesize(self): """ Returns the memory page size on this system. Page size will never change, so get it once and cache it. """ if 'pagesize' not in self.last_values: pgs = sh.run_cmd('/bin/pagesize') self.last_values['pagesize'] = int(pgs) return self.last_values['pagesize']
def count_items(self, pool, item_type): """ Count the number of datasets or snapshots in the pool. :param pool: the pool to examine. (strin) :param item_type: what you want the number of. Can be 'filesystem', 'snapshot', 'volume', or 'all'. (string) """ return len(sh.run_cmd('/usr/sbin/zfs list -rH -t %s %s' % (item_type, pool)))
def num_obj(self, dataset, obj): """ return the number of volumes or snapshots held by the given dataset """ raw = sh.run_cmd('/usr/sbin/zfs list -r -d1 -Ho name -t %s %s' % (obj, dataset)) if isinstance(raw, basestring): return 1 else: return len(raw)
def zid(self): """ Return the zone id. I don't *think* this can change, so we'll cache it. """ if 'zid' in self.last_values: zid = self.last_values['zid'] else: zid = int(sh.run_cmd('/usr/sbin/zoneadm list -p').split(':')[0]) self.last_values['zid'] = zid return zid
def get_all(self, dataset): raw = sh.run_cmd('/usr/sbin/zfs get -Ho property,value all %s' % dataset) ret = {} for metric in raw: k, v = metric.split(None, 1) try: ret[k] = sh.handle_value(v) except: next return ret
def num_obj(self, dataset, obj): """ return the number of volumes or snapshots held by the given dataset """ raw = sh.run_cmd( '/usr/sbin/zfs list -r -d1 -Ho name -t %s %s' % (obj, dataset)) if isinstance(raw, basestring): return 1 else: return len(raw)
def zid(self): """ Return the zone id. I don't *think* this can change, so we'll cache it. """ if 'zid' in self.last_values: zid = self.last_values['zid'] else: zid = int(sh.run_cmd( '/usr/sbin/zoneadm list -p').split(':')[0]) self.last_values['zid'] = zid return zid
def zone_lookup(self, zid): """ In an NGZ, the zone map will always return 0 for the local zone, to work with kstats. """ try: z = self.zm[zid] except: if 'zone_name' in self.last_values: z = self.last_values['zone_name'] else: z = sh.run_cmd('/bin/zonename') self.last_values['zone_name'] = z return z
def collect_swap_cmd(self): """ Shells out to /usr/sbin/swap and sends the results as metrics. Converts to bytes from kB. Not smart, so if the output of the command changes, this will break. """ info = re.match( 'total: (\d+)k [\w ]* \+ (\d+)k.*= (\d+)k used, (\d+)k.*$', sh.run_cmd('/usr/sbin/swap -s')) for i, metric in enumerate( ['allocated', 'reserved', 'used', 'available']): if sh.wanted(metric, self.config['swap_fields']): self.publish('swap.%s' % metric, int(info.group(i + 1)) * 1024)
def collect_swap_cmd(self): """ Shells out to /usr/sbin/swap and sends the results as metrics. Converts to bytes from kB. Not smart, so if the output of the command changes, this will break. """ info = re.match( 'total: (\d+)k [\w ]* \+ (\d+)k.*= (\d+)k used, (\d+)k.*$', sh.run_cmd('/usr/sbin/swap -s')) for i, metric in enumerate(['allocated', 'reserved', 'used', 'available']): if sh.wanted(metric, self.config['swap_fields']): self.publish('swap.%s' % metric, int(info.group(i + 1)) * 1024)
def process_table(self): """ Use ps(1) to get a list of processes, zones (by ID), and task ID :returns: a dict where the key is the zone ID (cast to a string) and the value is a dict of task_id => process_name. We don't bother with args. """ raw = sh.run_cmd('/bin/ps -eo taskid,zoneid,comm') raw.pop(0) ret = {} for l in raw: task, zone, cmd = l.split() if zone not in ret.keys(): ret[zone] = {} ret[zone][task] = cmd.split('/')[-1] return ret
def zfs_list(self): """ :returns: a list of ZFS filesystems """ return sh.run_cmd('/usr/sbin/zfs list -Ho name')
def zpool(self): return sh.run_cmd('/usr/sbin/zpool list')
def fmstat(self): try: return sh.run_cmd('/usr/sbin/fmstat', True) except: raise NotImplementedError('unable to run fmstat')
def fmadm(self): try: return sh.run_cmd('/usr/sbin/fmadm faulty', True) except: raise NotImplementedError('unable to run fmadm')
def test_run_cmd(self): with self.assertRaises(NotImplementedError): sh.run_cmd('/no/such/command') with self.assertRaises(NotImplementedError): sh.run_cmd('/no/such/command', True) with self.assertRaises(Exception): sh.run_cmd('/bin/cat /etc/shadow') self.assertIsInstance(sh.run_cmd('/bin/uname -s'), basestring) self.assertEqual(sh.run_cmd('/bin/uname -s'), 'SunOS') self.assertIsInstance(sh.run_cmd('/bin/sed 5q /etc/passwd'), list) self.assertEqual(len(sh.run_cmd('/bin/sed 5q /etc/passwd')), 5) self.assertEqual(len(sh.run_cmd('/bin/sed 1q /etc/passwd', as_arr=True)), 1) self.assertNotEqual(len(sh.run_cmd('/bin/sed 1q /etc/passwd')), 1) # This test requires a suitable profile. I gave myself this # privilege: # System Telemetry:solaris:cmd:RO::/usr/bin/id:uid=0;euid=0 # if 'System Telemetry' in sh.run_cmd('/bin/profiles'): self.assertNotRegexpMatches(sh.run_cmd('/usr/bin/id'), 'root') self.assertRegexpMatches(sh.run_cmd('/usr/bin/id', True), 'root')
def zoneadm(self): zones = sunos_helpers.run_cmd('/usr/sbin/zoneadm list -pc') if isinstance(zones, basestring): zones = [zones] return zones
def vcpus(self): if 'vcpus' not in self.last_values: self.last_values['vcpus'] = len( sh.run_cmd('/usr/sbin/psrinfo', as_arr=True)) return self.last_values['vcpus']
def test_run_cmd(self): with self.assertRaises(NotImplementedError): sh.run_cmd('/no/such/command') with self.assertRaises(NotImplementedError): sh.run_cmd('/no/such/command', True) with self.assertRaises(Exception): sh.run_cmd('/bin/cat /etc/shadow') self.assertIsInstance(sh.run_cmd('/bin/uname -s'), basestring) self.assertEqual(sh.run_cmd('/bin/uname -s'), 'SunOS') self.assertIsInstance(sh.run_cmd('/bin/sed 5q /etc/passwd'), list) self.assertEqual(len(sh.run_cmd('/bin/sed 5q /etc/passwd')), 5) self.assertEqual( len(sh.run_cmd('/bin/sed 1q /etc/passwd', as_arr=True)), 1) self.assertNotEqual(len(sh.run_cmd('/bin/sed 1q /etc/passwd')), 1) # This test requires a suitable profile. I gave myself this # privilege: # System Telemetry:solaris:cmd:RO::/usr/bin/id:uid=0;euid=0 # if 'System Telemetry' in sh.run_cmd('/bin/profiles'): self.assertNotRegexpMatches(sh.run_cmd('/usr/bin/id'), 'root') self.assertRegexpMatches(sh.run_cmd('/usr/bin/id', True), 'root')
def test_get_kstat(self): self.assertEqual(sh.get_kstat('nosuch:0:kstat:name'), {}) self.assertEqual(sh.get_kstat('nosuch:0::name'), {}) self.assertEqual(sh.get_kstat('nosuch:::name'), {}) self.assertEqual(sh.get_kstat('nosuch:0:kstat'), {}) self.assertEqual(sh.get_kstat('nosuch:0'), {}) self.assertEqual(sh.get_kstat('nosuch'), {}) self.assertIsInstance(sh.get_kstat('cpu:0:sys:canch', single_val=True), long) self.assertEqual(len(sh.get_kstat('cpu::vm:pgin')), len(sh.run_cmd('/usr/sbin/psrinfo'))) self.assertEqual(len(sh.get_kstat('cpu:0:vm:pgin')), 1) self.assertEqual(sh.get_kstat('cpu:0:vm:pgin', ks_class='disk'), {}) res = sh.get_kstat('cpu:0:vm') self.assertIsInstance(res, dict) self.assertIn('cpu:0:vm:execpgin', res.keys()) self.assertIn('cpu:0:vm:crtime', res.keys()) self.assertNotIn('cpu:0:vm:crtime', sh.get_kstat('cpu:0:vm', no_times=True)) self.assertNotIn('cpu:0:vm:crtime', sh.get_kstat('cpu:0:vm', no_times=True)) self.assertNotIn('cpu_info:0:cpu_info0:brand', sh.get_kstat('cpu_info:0:cpu_info0').keys()) self.assertIn( 'cpu_info:0:cpu_info0:brand', sh.get_kstat('cpu_info:0:cpu_info0', only_num=False).keys()) self.assertIn( 'brand', sh.get_kstat('cpu_info:0:cpu_info0', only_num=False, terse=True).keys()) self.assertNotIn( 'cpu_info:0:cpu_info0:brand', sh.get_kstat('cpu_info:0:cpu_info0', only_num=False, terse=True).keys()) self.assertNotRegexpMatches(''.join(sh.get_kstat('ip:0:icmp').keys()), '[A-Z]') self.assertEqual(sh.get_kstat(':::', ks_class='nosuch'), {}) self.assertEqual(sh.get_kstat('cpu:0:vm', ks_class='disk'), {}) self.assertIn('ilb:0:global:snaptime', sh.get_kstat(':::', ks_class='kstat')) self.assertNotIn('ilb:0:global:snaptime', sh.get_kstat(':::', ks_class='kstat', no_times=True)) self.assertEqual( sh.get_kstat('cpu_info:0:cpu_info0', only_num=False, statlist=['nosuch']), {}) res = sh.get_kstat('cpu_info:0:cpu_info0', only_num=False, terse=True, statlist=('state', 'core_id')) self.assertEqual(len(res), 2) self.assertItemsEqual(res.keys(), ['state', 'core_id']) self.assertIn( 'core_id', sh.get_kstat('cpu_info:0:cpu_info0', statlist='__all__', terse=True))
def test_get_kstat(self): self.assertEqual(sh.get_kstat('nosuch:0:kstat:name'), {}) self.assertEqual(sh.get_kstat('nosuch:0::name'), {}) self.assertEqual(sh.get_kstat('nosuch:::name'), {}) self.assertEqual(sh.get_kstat('nosuch:0:kstat'), {}) self.assertEqual(sh.get_kstat('nosuch:0'), {}) self.assertEqual(sh.get_kstat('nosuch'), {}) self.assertIsInstance(sh.get_kstat('cpu:0:sys:canch', single_val=True), long) self.assertEqual(len(sh.get_kstat('cpu::vm:pgin')), len(sh.run_cmd('/usr/sbin/psrinfo'))) self.assertEqual(len(sh.get_kstat('cpu:0:vm:pgin')), 1) self.assertEqual(sh.get_kstat('cpu:0:vm:pgin', ks_class='disk'), {}) res = sh.get_kstat('cpu:0:vm') self.assertIsInstance(res, dict) self.assertIn('cpu:0:vm:execpgin', res.keys()) self.assertIn('cpu:0:vm:crtime', res.keys()) self.assertNotIn('cpu:0:vm:crtime', sh.get_kstat('cpu:0:vm', no_times=True)) self.assertNotIn('cpu:0:vm:crtime', sh.get_kstat('cpu:0:vm', no_times=True)) self.assertNotIn('cpu_info:0:cpu_info0:brand', sh.get_kstat('cpu_info:0:cpu_info0').keys()) self.assertIn('cpu_info:0:cpu_info0:brand', sh.get_kstat('cpu_info:0:cpu_info0', only_num=False).keys()) self.assertIn('brand', sh.get_kstat('cpu_info:0:cpu_info0', only_num=False, terse=True).keys()) self.assertNotIn('cpu_info:0:cpu_info0:brand', sh.get_kstat('cpu_info:0:cpu_info0', only_num=False, terse=True).keys()) self.assertNotRegexpMatches(''.join(sh.get_kstat('ip:0:icmp'). keys()), '[A-Z]') self.assertEqual(sh.get_kstat(':::', ks_class='nosuch'), {}) self.assertEqual(sh.get_kstat('cpu:0:vm', ks_class='disk'), {}) self.assertIn('ilb:0:global:snaptime', sh.get_kstat(':::', ks_class='kstat')) self.assertNotIn('ilb:0:global:snaptime', sh.get_kstat(':::', ks_class='kstat', no_times=True)) self.assertEqual(sh.get_kstat('cpu_info:0:cpu_info0', only_num=False, statlist=['nosuch']), {}) res = sh.get_kstat('cpu_info:0:cpu_info0', only_num=False, terse=True, statlist=('state', 'core_id')) self.assertEqual(len(res), 2) self.assertItemsEqual(res.keys(), ['state', 'core_id']) self.assertIn('core_id', sh.get_kstat('cpu_info:0:cpu_info0', statlist='__all__', terse=True))