-
Notifications
You must be signed in to change notification settings - Fork 25
/
list_storage_groups.py
executable file
·144 lines (119 loc) · 4.63 KB
/
list_storage_groups.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/env python
# Copyright 2018 IBM Corp. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Example that lists storage groups (DPM mode, z14).
"""
import sys
import logging
import yaml
import requests.packages.urllib3
import zhmcclient
requests.packages.urllib3.disable_warnings()
if len(sys.argv) != 2:
print("Usage: %s hmccreds.yaml" % sys.argv[0])
sys.exit(2)
hmccreds_file = sys.argv[1]
with open(hmccreds_file, 'r') as fp:
hmccreds = yaml.load(fp)
examples = hmccreds.get("examples", None)
if examples is None:
print("examples not found in credentials file %s" % \
(hmccreds_file))
sys.exit(1)
list_storage_groups = examples.get("list_storage_groups", None)
if list_storage_groups is None:
print("list_storage_groups not found in credentials file %s" % \
(hmccreds_file))
sys.exit(1)
loglevel = list_storage_groups.get("loglevel", None)
if loglevel is not None:
level = getattr(logging, loglevel.upper(), None)
if level is None:
print("Invalid value for loglevel in credentials file %s: %s" % \
(hmccreds_file, loglevel))
sys.exit(1)
logmodule = list_storage_groups.get("logmodule", None)
if logmodule is None:
logmodule = '' # root logger
print("Logging for module %s with level %s" % (logmodule, loglevel))
handler = logging.StreamHandler()
format_string = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
handler.setFormatter(logging.Formatter(format_string))
logger = logging.getLogger(logmodule)
logger.addHandler(handler)
logger.setLevel(level)
hmc = list_storage_groups["hmc"]
cpcname = list_storage_groups["cpcname"]
cred = hmccreds.get(hmc, None)
if cred is None:
print("Credentials for HMC %s not found in credentials file %s" % \
(hmc, hmccreds_file))
sys.exit(1)
userid = cred['userid']
password = cred['password']
print(__doc__)
print("Using HMC %s with userid %s ..." % (hmc, userid))
session = zhmcclient.Session(hmc, userid, password)
cl = zhmcclient.Client(session)
timestats = list_storage_groups.get("timestats", False)
if timestats:
session.time_stats_keeper.enable()
print("Finding CPC %s ..." % cpcname)
try:
cpc = cl.cpcs.find(name=cpcname)
except zhmcclient.NotFound:
print("Could not find CPC %s on HMC %s" % (cpcname, hmc))
sys.exit(1)
if False:
print("Checking CPC %s to be in DPM mode ..." % cpcname)
if not cpc.dpm_enabled:
print("Storage groups require DPM mode, but CPC %s is not in DPM mode" %
cpcname)
sys.exit(1)
print("Storage Groups of CPC %s ..." % cpcname)
storage_groups = cpc.list_associated_storage_groups()
for sg in storage_groups:
part_names = [p.name for p in sg.list_attached_partitions()]
part_names_str = ', '.join(part_names) if part_names else "<none>"
print(" Storage Group: %s (type: %s, shared: %s, fulfillment: %s, "
"attached to partitions: %s)" %
(sg.name, sg.get_property('type'), sg.get_property('shared'),
sg.get_property('fulfillment-state'), part_names_str))
try:
volumes = sg.storage_volumes.list()
except zhmcclient.HTTPError as exc:
print("Error listing storage volumes of storage group %s:\n"
"HTTPError: %s" % (sg.name, exc))
volumes = []
print(" Storage Volumes: %s" % len(volumes))
if sg.get_property('type') == 'fcp':
try:
vsrs = sg.virtual_storage_resources.list()
except zhmcclient.HTTPError as exc:
print("Error listing virtual storage resources of storage group %s:\n"
"HTTPError: %s" % (sg.name, exc))
vsrs = []
for vsr in vsrs:
port = vsr.adapter_port
adapter = port.manager.parent
print(" Virtual Storage Resource: %s (devno: %s, "
"adapter.port: %s.%s, attached to partition: %s)" %
(vsr.name, vsr.get_property('device-number'),
adapter.name, port.name, vsr.attached_partition.name))
else:
print(" No Virtual Storage Resources")
session.logoff()
if timestats:
print(session.time_stats_keeper)