Coverage for libs/sm/sr_health_check.py: 93%
31 statements
« prev ^ index » next coverage.py v7.3.2, created at 2026-07-10 07:59 +0000
« prev ^ index » next coverage.py v7.3.2, created at 2026-07-10 07:59 +0000
1# Copyright (C) Cloud Software Group, Inc.
2#
3# This program is free software; you can redistribute it and/or modify
4# it under the terms of the GNU Lesser General Public License as published
5# by the Free Software Foundation; version 2.1 only.
6#
7# This program is distributed in the hope that it will be useful,
8# but WITHOUT ANY WARRANTY; without even the implied warranty of
9# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10# GNU Lesser General Public License for more details.
11#
12# You should have received a copy of the GNU Lesser General Public License
13# along with this program; if not, write to the Free Software Foundation, Inc.,
14# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16"""
17Health check for SR, to be triggered periodically by a systemd timer. What is checked is
18SR implementation type dependent.
19"""
21from sm import SR
22from sm.core import util
23from sm.core import xs_errors
25def check_xapi_is_enabled(session, hostref):
26 host = session.xenapi.host.get_record(hostref)
27 return host['enabled']
30def main():
31 """
32 For all locally plugged SRs check that they are healthy
33 """
34 try:
35 session = util.get_localAPI_session()
36 except xs_errors.SROSError:
37 util.SMlog("Unable to open local XAPI session", priority=util.LOG_ERR)
38 return
40 try:
41 localhost = util.get_localhost_ref(session)
42 if not check_xapi_is_enabled(session, localhost):
43 # Xapi not enabled, skip and let the next timer trigger this
44 return
46 sm_types = [x['type'] for x in session.xenapi.SM.get_all_records_where(
47 'field "required_api_version" = "1.0"').values()]
48 for sm_type in sm_types:
49 srs = session.xenapi.SR.get_all_records_where(
50 f'field "type" = "{sm_type}"')
51 for sr in srs:
52 pbds = session.xenapi.PBD.get_all_records_where(
53 f'field "SR" = "{sr}" and field "host" = "{localhost}"')
54 if not pbds:
55 continue
57 pbd_ref, pbd = pbds.popitem()
58 if not pbd['currently_attached']:
59 continue
61 sr_uuid = srs[sr]['uuid']
62 sr_obj = SR.SR.from_uuid(session, sr_uuid)
63 sr_obj.check_sr(sr_uuid)
64 finally:
65 session.xenapi.session.logout()