Coverage for drivers/sr_health_check.py : 89%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1#!/usr/bin/python3
3# Copyright (C) Cloud Software Group, Inc.
4#
5# This program is free software; you can redistribute it and/or modify
6# it under the terms of the GNU Lesser General Public License as published
7# by the Free Software Foundation; version 2.1 only.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12# GNU Lesser General Public License for more details.
13#
14# You should have received a copy of the GNU Lesser General Public License
15# along with this program; if not, write to the Free Software Foundation, Inc.,
16# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18"""
19Health check for SR, to be triggered periodically by a systemd timer. What is checked is
20SR implementation type dependent.
21"""
23import SR
24import util
25import xs_errors
27def check_xapi_is_enabled(session, hostref):
28 host = session.xenapi.host.get_record(hostref)
29 return host['enabled']
32def main():
33 """
34 For all locally plugged SRs check that they are healthy
35 """
36 try:
37 session = util.get_localAPI_session()
38 except xs_errors.SROSError:
39 util.SMlog("Unable to open local XAPI session", priority=util.LOG_ERR)
40 return
42 try:
43 localhost = util.get_localhost_ref(session)
44 if not check_xapi_is_enabled(session, localhost):
45 # Xapi not enabled, skip and let the next timer trigger this
46 return
48 sm_types = [x['type'] for x in session.xenapi.SM.get_all_records_where(
49 'field "required_api_version" = "1.0"').values()]
50 for sm_type in sm_types:
51 srs = session.xenapi.SR.get_all_records_where(
52 f'field "type" = "{sm_type}"')
53 for sr in srs:
54 pbds = session.xenapi.PBD.get_all_records_where(
55 f'field "SR" = "{sr}" and field "host" = "{localhost}"')
56 if not pbds:
57 continue
59 pbd_ref, pbd = pbds.popitem()
60 if not pbd['currently_attached']:
61 continue
63 sr_uuid = srs[sr]['uuid']
64 sr_obj = SR.SR.from_uuid(session, sr_uuid)
65 sr_obj.check_sr(sr_uuid)
66 finally:
67 session.xenapi.session.logout()
70if __name__ == "__main__": 70 ↛ 71line 70 didn't jump to line 71, because the condition on line 70 was never true
71 main()