def main():
    if (diskstats.OS_NAME != 'Linux'):
        print('diskstats plugin currently works for Linux only')
        print('os_name: %s' % (diskstats.os_name))
        print('Finished')
        return

    logging.basicConfig(level=logging.INFO)
    diskstats.get_dev_list()
    diskstats.dev_stats_cache = diskstats.collect_diskstats()
    logging.info('netstats_ftest: dev_list: %s' % (diskstats.dev_list))
    logging.debug('netstats_ftest: dev_stats_cache: %s'
                  % (diskstats.dev_stats_cache))
    print('netstats_ftest: ... sleeping for 10 sec...')
    time.sleep(10)
    diskstats.dev_stats_current = diskstats.collect_diskstats()
    logging.debug('netstats_ftest: dev_stats_cur: %s'
                  % (diskstats.dev_stats_current))
    metric_names = ['iops_rw', 'bytes_ps_read', 'bytes_ps_write']

    print_header()
    metric_key_vals = {}
    metric_vals = []
    for i in diskstats.dev_list:
        metric_key_vals = diskstats.calc_metrics(i)
        logging.debug('metric_key_vals: %s' % (metric_key_vals))
        metric_vals = [metric_key_vals[k] for k in metric_names]
        dev_pretty_prints(i, metric_vals)
        logging.debug('metric_vals: %s' % (metric_vals))
def main():
    if (diskstats.OS_NAME != 'Linux'):
        print('diskstats plugin currently works for Linux only')
        print('os_name: %s' % (diskstats.os_name))
        print('Finished')
        return

    logging.basicConfig(level=logging.INFO)
    diskstats.get_dev_list()
    diskstats.dev_stats_cache = diskstats.collect_diskstats()
    logging.info('netstats_ftest: dev_list: %s' % (diskstats.dev_list))
    logging.debug('netstats_ftest: dev_stats_cache: %s' %
                  (diskstats.dev_stats_cache))
    print('netstats_ftest: ... sleeping for 10 sec...')
    time.sleep(10)
    diskstats.dev_stats_current = diskstats.collect_diskstats()
    logging.debug('netstats_ftest: dev_stats_cur: %s' %
                  (diskstats.dev_stats_current))
    metric_names = ['iops_rw', 'bytes_ps_read', 'bytes_ps_write']

    print_header()
    metric_key_vals = {}
    metric_vals = []
    for i in diskstats.dev_list:
        metric_key_vals = diskstats.calc_metrics(i)
        logging.debug('metric_key_vals: %s' % (metric_key_vals))
        metric_vals = [metric_key_vals[k] for k in metric_names]
        dev_pretty_prints(i, metric_vals)
        logging.debug('metric_vals: %s' % (metric_vals))
  def test_4_diskstats_swap_cache(self):
    st_names = diskstats.diskstat_fields[3:14]
    diskstats.dev_stats_cache = diskstats.collect_diskstats()
    diskstats.dev_stats_current = diskstats.collect_diskstats()
    diskstats.swap_current_cache()
    diskstats.dev_stats_current = diskstats.collect_diskstats()

    for i in diskstats.dev_list:
      self.assertNotEqual(diskstats.dev_stats_cache[(i, 'ts')],
                          diskstats.dev_stats_current[(i, 'ts')],
                          'prev and curr timestamps should differ')
      self.assertEqual([diskstats.dev_stats_cache[(i,k)] for k in st_names],
                       [diskstats.dev_stats_current[(i,k)] for k in st_names],
                       'prev and curr dev stats should be same')
    def test_4_diskstats_swap_cache(self):
        st_names = diskstats.diskstat_fields[3:14]
        diskstats.dev_stats_cache = diskstats.collect_diskstats()
        diskstats.dev_stats_current = diskstats.collect_diskstats()
        diskstats.swap_current_cache()
        diskstats.dev_stats_current = diskstats.collect_diskstats()

        for i in diskstats.dev_list:
            self.assertNotEqual(
                diskstats.dev_stats_cache[(i, "ts")],
                diskstats.dev_stats_current[(i, "ts")],
                "prev and curr timestamps should differ",
            )
            self.assertEqual(
                [diskstats.dev_stats_cache[(i, k)] for k in st_names],
                [diskstats.dev_stats_current[(i, k)] for k in st_names],
                "prev and curr dev stats should be same",
            )
    def test_3_diskstats_metrics_calc(self):
        diskstats.dev_stats_cache = diskstats.collect_diskstats()
        diskstats.dev_stats_current = diskstats.collect_diskstats()
        metrics_key_vals = diskstats.calc_metrics(diskstats.dev_list[0])
        dev_metrics_vals = metrics_key_vals.values()

        num_metrics_keys = len(diskstats.diskstat_metrics)
        num_metrics_vals = len(dev_metrics_vals)

        try:
            self.assertEqual(num_metrics_keys, num_metrics_vals, "number of metric names not equals to vals")
            self.assertEqual(metrics_key_vals["util_pct"], 0.0, "wrong util_pct")
            self.assertEqual(metrics_key_vals["bytes_per_write"], None, "wrong bytes_per_write value")
        except:
            print("del_t: %f" % (diskstats.calc_del_t(diskstats.dev_list[0])))
            print("metris_key_vals: %s" % (metrics_key_vals))
            print("Exception: %s" % (sys.exc_info()[0]))
            raise
  def test_3_diskstats_metrics_calc(self):
    diskstats.dev_stats_cache = diskstats.collect_diskstats()
    diskstats.dev_stats_current = diskstats.collect_diskstats()
    metrics_key_vals = diskstats.calc_metrics(diskstats.dev_list[0])
    dev_metrics_vals = metrics_key_vals.values()

    num_metrics_keys = len(diskstats.diskstat_metrics)
    num_metrics_vals = len(dev_metrics_vals)

    try:
      self.assertEqual(num_metrics_keys, num_metrics_vals,
                       'number of metric names not equals to vals')
      self.assertEqual(metrics_key_vals['util_pct'], 0.0, 'wrong util_pct')
      self.assertEqual(metrics_key_vals['bytes_per_write'], None,
                       'wrong bytes_per_write value')
    except:
      print('del_t: %f' % (diskstats.calc_del_t(diskstats.dev_list[0])))
      print('metris_key_vals: %s' % (metrics_key_vals))
      print('Exception: %s' % (sys.exc_info()[0]))
      raise
    def test_2_diskstats_collection(self):
        diskstats.dev_list = []
        diskstats.DISKSTATS_FNAME = PROCFS_DISKSTAT
        diskstats.get_dev_list()
        stats_names = diskstats.diskstat_fields[3:14]
        dev_stats_current = diskstats.collect_diskstats()

        field_names = [dev_stats_current[(diskstats.dev_list[0], k)] for k in stats_names]
        num_dev = len(diskstats.dev_list)
        num_fields = len(field_names)
        num_expected_fields = (num_fields + 1) * num_dev
        self.assertTrue(len(dev_stats_current) > 0, "at least one metric")
        try:
            self.assertEqual(len(dev_stats_current), num_expected_fields, "unexpected diskstat fields and vals")
        except:
            print("dev_stats_current: %s" % (dev_stats_current))
            print("field_names: %s" % (field_names))
            print("dev_stats_current len: %d" % (len(dev_stats_current)))
            print("Exception: %s" % (sys.exc_info()[0]))
            raise
  def test_2_diskstats_collection(self):
    diskstats.dev_list = []
    diskstats.DISKSTATS_FNAME = PROCFS_DISKSTAT
    diskstats.get_dev_list()
    stats_names = diskstats.diskstat_fields[3:14]
    dev_stats_current = diskstats.collect_diskstats()

    field_names = [dev_stats_current[(diskstats.dev_list[0], k)] for k in
                   stats_names]
    num_dev = len(diskstats.dev_list)
    num_fields = len(field_names)
    num_expected_fields = (num_fields + 1) * num_dev;
    self.assertTrue(len(dev_stats_current) > 0,
                    'at least one metric')
    try:
      self.assertEqual(len(dev_stats_current), num_expected_fields,
                       'unexpected diskstat fields and vals')
    except:
      print('dev_stats_current: %s' % (dev_stats_current))
      print('field_names: %s' % (field_names))
      print('dev_stats_current len: %d' % (len(dev_stats_current)))
      print('Exception: %s' % (sys.exc_info()[0]))
      raise