Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / qa / tasks / check_counter.py
1
2 import logging
3 import json
4
5 from teuthology.task import Task
6 from teuthology import misc
7 import ceph_manager
8
9 log = logging.getLogger(__name__)
10
11
12 class CheckCounter(Task):
13     """
14     Use this task to validate that some daemon perf counters were
15     incremented by the nested tasks.
16
17     Config:
18      'cluster_name': optional, specify which cluster
19      'target': dictionary of daemon type to list of performance counters.
20      'dry_run': just log the value of the counters, don't fail if they
21                 aren't nonzero.
22
23     Success condition is that for all of the named counters, at least
24     one of the daemons of that type has the counter nonzero.
25
26     Example to check cephfs dirfrag splits are happening:
27     - install:
28     - ceph:
29     - ceph-fuse:
30     - check-counter:
31         counters:
32             mds:
33                 - "mds.dir_split"
34     - workunit: ...
35     """
36
37     def start(self):
38         log.info("START")
39
40     def end(self):
41         cluster_name = self.config.get('cluster_name', None)
42         dry_run = self.config.get('dry_run', False)
43         targets = self.config.get('counters', {})
44
45         if cluster_name is None:
46             cluster_name = self.ctx.managers.keys()[0]
47
48         for daemon_type, counters in targets.items():
49             # List of 'a', 'b', 'c'...
50             daemon_ids = list(misc.all_roles_of_type(self.ctx.cluster, daemon_type))
51             daemons = dict([(daemon_id,
52                              self.ctx.daemons.get_daemon(daemon_type, daemon_id))
53                             for daemon_id in daemon_ids])
54
55             seen = set()
56
57             for daemon_id, daemon in daemons.items():
58                 if not daemon.running():
59                     log.info("Ignoring daemon {0}, it isn't running".format(daemon_id))
60                     continue
61                 else:
62                     log.debug("Getting stats from {0}".format(daemon_id))
63
64                 manager = self.ctx.managers[cluster_name]
65                 proc = manager.admin_socket(daemon_type, daemon_id, ["perf", "dump"])
66                 response_data = proc.stdout.getvalue().strip()
67                 if response_data:
68                     perf_dump = json.loads(response_data)
69                 else:
70                     log.warning("No admin socket response from {0}, skipping".format(daemon_id))
71                     continue
72
73                 for counter in counters:
74                     subsys, counter_id = counter.split(".")
75                     if subsys not in perf_dump or counter_id not in perf_dump[subsys]:
76                         log.warning("Counter '{0}' not found on daemon {1}.{2}".format(
77                             counter, daemon_type, daemon_id))
78                         continue
79                     value = perf_dump[subsys][counter_id]
80
81                     log.info("Daemon {0}.{1} {2}={3}".format(
82                         daemon_type, daemon_id, counter, value
83                     ))
84
85                     if value > 0:
86                         seen.add(counter)
87
88             if not dry_run:
89                 unseen = set(counters) - set(seen)
90                 if unseen:
91                     raise RuntimeError("The following counters failed to be set "
92                                        "on {0} daemons: {1}".format(
93                         daemon_type, unseen
94                     ))
95
96 task = CheckCounter