1129 |
1129 |
"""Logical unit for querying nodes.
|
1130 |
1130 |
|
1131 |
1131 |
"""
|
1132 |
|
_OP_REQP = ["output_fields"]
|
|
1132 |
_OP_REQP = ["output_fields", "nodes"]
|
1133 |
1133 |
|
1134 |
1134 |
def CheckPrereq(self):
|
1135 |
1135 |
"""Check prerequisites.
|
... | ... | |
1140 |
1140 |
self.dynamic_fields = frozenset(["dtotal", "dfree",
|
1141 |
1141 |
"mtotal", "mnode", "mfree"])
|
1142 |
1142 |
|
1143 |
|
_CheckOutputFields(static=["name", "pinst", "sinst", "pip", "sip"],
|
|
1143 |
_CheckOutputFields(static=["name", "pinst_cnt", "sinst_cnt",
|
|
1144 |
"pinst_list", "sinst_list",
|
|
1145 |
"pip", "sip"],
|
1144 |
1146 |
dynamic=self.dynamic_fields,
|
1145 |
1147 |
selected=self.op.output_fields)
|
1146 |
1148 |
|
|
1149 |
self.wanted_nodes = _GetWantedNodes(self, self.op.nodes)
|
1147 |
1150 |
|
1148 |
1151 |
def Exec(self, feedback_fn):
|
1149 |
1152 |
"""Computes the list of nodes and their attributes.
|
1150 |
1153 |
|
1151 |
1154 |
"""
|
1152 |
|
nodenames = utils.NiceSort(self.cfg.GetNodeList())
|
|
1155 |
nodenames = self.wanted_nodes
|
1153 |
1156 |
nodelist = [self.cfg.GetNodeInfo(name) for name in nodenames]
|
1154 |
1157 |
|
1155 |
|
|
1156 |
1158 |
# begin data gathering
|
1157 |
1159 |
|
1158 |
1160 |
if self.dynamic_fields.intersection(self.op.output_fields):
|
... | ... | |
1173 |
1175 |
else:
|
1174 |
1176 |
live_data = dict.fromkeys(nodenames, {})
|
1175 |
1177 |
|
1176 |
|
node_to_primary = dict.fromkeys(nodenames, 0)
|
1177 |
|
node_to_secondary = dict.fromkeys(nodenames, 0)
|
|
1178 |
node_to_primary = dict([(name, set()) for name in nodenames])
|
|
1179 |
node_to_secondary = dict([(name, set()) for name in nodenames])
|
1178 |
1180 |
|
1179 |
|
if "pinst" in self.op.output_fields or "sinst" in self.op.output_fields:
|
|
1181 |
inst_fields = frozenset(("pinst_cnt", "pinst_list",
|
|
1182 |
"sinst_cnt", "sinst_list"))
|
|
1183 |
if inst_fields & frozenset(self.op.output_fields):
|
1180 |
1184 |
instancelist = self.cfg.GetInstanceList()
|
1181 |
1185 |
|
1182 |
|
for instance in instancelist:
|
1183 |
|
instanceinfo = self.cfg.GetInstanceInfo(instance)
|
1184 |
|
node_to_primary[instanceinfo.primary_node] += 1
|
1185 |
|
for secnode in instanceinfo.secondary_nodes:
|
1186 |
|
node_to_secondary[secnode] += 1
|
|
1186 |
for instance_name in instancelist:
|
|
1187 |
inst = self.cfg.GetInstanceInfo(instance_name)
|
|
1188 |
if inst.primary_node in node_to_primary:
|
|
1189 |
node_to_primary[inst.primary_node].add(inst.name)
|
|
1190 |
for secnode in inst.secondary_nodes:
|
|
1191 |
if secnode in node_to_secondary:
|
|
1192 |
node_to_secondary[secnode].add(inst.name)
|
1187 |
1193 |
|
1188 |
1194 |
# end data gathering
|
1189 |
1195 |
|
... | ... | |
1193 |
1199 |
for field in self.op.output_fields:
|
1194 |
1200 |
if field == "name":
|
1195 |
1201 |
val = node.name
|
1196 |
|
elif field == "pinst":
|
1197 |
|
val = node_to_primary[node.name]
|
1198 |
|
elif field == "sinst":
|
1199 |
|
val = node_to_secondary[node.name]
|
|
1202 |
elif field == "pinst_list":
|
|
1203 |
val = list(node_to_primary[node.name])
|
|
1204 |
elif field == "sinst_list":
|
|
1205 |
val = list(node_to_secondary[node.name])
|
|
1206 |
elif field == "pinst_cnt":
|
|
1207 |
val = len(node_to_primary[node.name])
|
|
1208 |
elif field == "sinst_cnt":
|
|
1209 |
val = len(node_to_secondary[node.name])
|
1200 |
1210 |
elif field == "pip":
|
1201 |
1211 |
val = node.primary_ip
|
1202 |
1212 |
elif field == "sip":
|
1203 |
1213 |
val = node.secondary_ip
|
1204 |
1214 |
elif field in self.dynamic_fields:
|
1205 |
|
val = live_data[node.name].get(field, "?")
|
|
1215 |
val = live_data[node.name].get(field, None)
|
1206 |
1216 |
else:
|
1207 |
1217 |
raise errors.ParameterError(field)
|
1208 |
|
val = str(val)
|
1209 |
1218 |
node_output.append(val)
|
1210 |
1219 |
output.append(node_output)
|
1211 |
1220 |
|