|
| 1 | +import subprocess |
| 2 | + |
| 3 | +from smtLayer import msgs |
| 4 | + |
| 5 | + |
| 6 | +class VMCPHandler: |
| 7 | + |
| 8 | + def __init__(self, rh): |
| 9 | + """ |
| 10 | + Initialise the handler with a Request Handle. |
| 11 | + |
| 12 | + Input: |
| 13 | + rh - smtLayer ReqHandle instance |
| 14 | + """ |
| 15 | + self._rh = rh |
| 16 | + |
| 17 | + def _query_system_processor(self): |
| 18 | + """ |
| 19 | + To get LPAR total cpu and cpu in use |
| 20 | + |
| 21 | + Returns cpu_total and cpu_use. |
| 22 | + """ |
| 23 | + |
| 24 | + cmd = ["sudo", "/sbin/vmcp", "query PROCESSORS EXPANDED"] |
| 25 | + strCmd = ' '.join(cmd) |
| 26 | + rh.printSysLog("Invoking: " + strCmd) |
| 27 | + |
| 28 | + cpu_total = 0 |
| 29 | + cpu_use = 0 |
| 30 | + rh = self._rh |
| 31 | + |
| 32 | + try: |
| 33 | + response = subprocess.check_output( |
| 34 | + cmd, |
| 35 | + close_fds=True, |
| 36 | + stderr=subprocess.STDOUT) |
| 37 | + response = bytes.decode(response) |
| 38 | + |
| 39 | + for line in response.splitlines(): |
| 40 | + parts = line.split() |
| 41 | + |
| 42 | + if len(parts) < 4 or parts[0] != 'PROCESSOR': |
| 43 | + continue |
| 44 | + status_row = parts[2] |
| 45 | + type_row = parts[3] |
| 46 | + if (status_row.find('MASTER') != -1 or |
| 47 | + status_row == 'ALTERNATE' or |
| 48 | + status_row == 'PARKED'): |
| 49 | + cpu_use = cpu_use + 1 |
| 50 | + if (type_row == 'CP' or type_row == 'IFL'): |
| 51 | + cpu_total = cpu_total + 1 |
| 52 | + |
| 53 | + except subprocess.CalledProcessError as e: |
| 54 | + rh.printLn("ES", msgs.msg['0415'][1] % (modId, strCmd, |
| 55 | + e.returncode, e.output)) |
| 56 | + results = msgs.msg['0415'][0] |
| 57 | + results['rs'] = e.returncode |
| 58 | + rh.updateResults(results) |
| 59 | + |
| 60 | + except Exception as e: |
| 61 | + rh.printLn("ES", msgs.msg['0421'][1] % (modId, strCmd, |
| 62 | + type(e).__name__, str(e))) |
| 63 | + rh.updateResults(msgs.msg['0421'][0]) |
| 64 | + |
| 65 | + return cpu_total, cpu_use |
| 66 | + |
| 67 | + def _purge_reader(self): |
| 68 | + """ |
| 69 | + Purge all reader files for rh.userid |
| 70 | + |
| 71 | + Returns a standard results dict. |
| 72 | + """ |
| 73 | + rh = self._rh |
| 74 | + cmd = ["sudo", "/sbin/vmcp", 'purge', rh.userid, 'rdr', 'all'] |
| 75 | + strCmd = ' '.join(cmd) |
| 76 | + rh.printSysLog("Invoking: " + strCmd) |
| 77 | + |
| 78 | + |
| 79 | + results = {'overallRC': 0, |
| 80 | + 'rc': 0, |
| 81 | + 'rs': 0, |
| 82 | + 'errno': 0, |
| 83 | + 'strError': '', |
| 84 | + 'response': ''} |
| 85 | + |
| 86 | + try: |
| 87 | + output = subprocess.check_output( |
| 88 | + cmd, |
| 89 | + close_fds=True, |
| 90 | + stderr=subprocess.STDOUT) |
| 91 | + |
| 92 | + results['response'] = output.decode().strip() |
| 93 | + |
| 94 | + except subprocess.CalledProcessError as e: |
| 95 | + rh.printLn("ES", msgs.msg['0415'][1] % (modId, strCmd, |
| 96 | + e.returncode, e.output)) |
| 97 | + results = msgs.msg['0415'][0] |
| 98 | + results['rs'] = e.returncode |
| 99 | + rh.updateResults(results) |
| 100 | + |
| 101 | + except Exception as e: |
| 102 | + rh.printLn("ES", msgs.msg['0421'][1] % (modId, strCmd, |
| 103 | + type(e).__name__, str(e))) |
| 104 | + rh.updateResults(msgs.msg['0421'][0]) |
| 105 | + |
| 106 | + return results |
| 107 | + |
| 108 | + def _create_nic(self, vdev): |
| 109 | + """ |
| 110 | + Create a QDIO NIC at virtual device address <vdev> for rh.userid. |
| 111 | + |
| 112 | + Returns a standard results dict. |
| 113 | + """ |
| 114 | + rh = self._rh |
| 115 | + rh.printSysLog("Enter VMCPHandler.create_nic, userid: %s vdev: %s" |
| 116 | + % (rh.userid, vdev)) |
| 117 | + |
| 118 | + try: |
| 119 | + result = self._run(['DEFINE', 'NIC', vdev, 'TYPE', 'QDIO']) |
| 120 | + if result['overallRC'] != 0: |
| 121 | + rh.printSysLog("Exit VMCPHandler.create_nic, rc: " + |
| 122 | + str(result['overallRC'])) |
| 123 | + return result |
| 124 | + |
| 125 | + verify = self._run(['QUERY', 'VIRTUAL', 'NIC', vdev]) |
| 126 | + if verify['overallRC'] == 0: |
| 127 | + result['response'] = verify['response'] |
| 128 | + |
| 129 | + except subprocess.CalledProcessError as e: |
| 130 | + rh.printLn("ES", msgs.msg['0415'][1] % (modId, strCmd, |
| 131 | + e.returncode, e.output)) |
| 132 | + results = msgs.msg['0415'][0] |
| 133 | + results['rs'] = e.returncode |
| 134 | + rh.updateResults(results) |
| 135 | + |
| 136 | + except Exception as e: |
| 137 | + rh.printLn("ES", msgs.msg['0421'][1] % (modId, strCmd, |
| 138 | + type(e).__name__, str(e))) |
| 139 | + rh.updateResults(msgs.msg['0421'][0]) |
| 140 | + |
| 141 | + rh.printSysLog("Exit VMCPHandler.create_nic, rc: " + |
| 142 | + str(result['overallRC'])) |
| 143 | + return result |
| 144 | + |
| 145 | + |
| 146 | + def _query_osa(self): |
| 147 | + """ |
| 148 | + Query OSA for rh.userid. |
| 149 | + |
| 150 | + Returns a standard results dict. |
| 151 | + """ |
| 152 | + OSA_info = {} |
| 153 | + |
| 154 | + try: |
| 155 | + result = self._run(['QUERY', 'OSA']) |
| 156 | + if result['overallRC'] != 0: |
| 157 | + return OSA_info |
| 158 | + |
| 159 | + output = result['response'] |
| 160 | + for line in output.splitlines(): |
| 161 | + parts = line.split() |
| 162 | + if len(parts) < 3 or parts[0] != 'OSA': |
| 163 | + continue |
| 164 | + |
| 165 | + osa_addr = parts[1] |
| 166 | + status_word = parts[2] |
| 167 | + |
| 168 | + osa_type = 'OSA' |
| 169 | + if 'DEVTYPE' in parts: |
| 170 | + dt_idx = parts.index('DEVTYPE') |
| 171 | + if dt_idx + 1 < len(parts): |
| 172 | + osa_type = parts[dt_idx + 1] |
| 173 | + |
| 174 | + if osa_type not in OSA_info: |
| 175 | + OSA_info[osa_type] = { |
| 176 | + 'FREE': [], 'BOXED': [], 'OFFLINE': [], 'ATTACHED': []} |
| 177 | + |
| 178 | + if status_word == 'ATTACHED': |
| 179 | + attached_id = parts[4] if len(parts) > 4 else 'UNKNOWN' |
| 180 | + OSA_info[osa_type]['ATTACHED'].append((attached_id, osa_addr)) |
| 181 | + elif status_word in OSA_info[osa_type]: |
| 182 | + OSA_info[osa_type][status_word].append(osa_addr) |
| 183 | + |
| 184 | + except subprocess.CalledProcessError as e: |
| 185 | + rh.printLn("ES", msgs.msg['0415'][1] % (modId, strCmd, |
| 186 | + e.returncode, e.output)) |
| 187 | + results = msgs.msg['0415'][0] |
| 188 | + results['rs'] = e.returncode |
| 189 | + rh.updateResults(results) |
| 190 | + |
| 191 | + except Exception as e: |
| 192 | + # All other exceptions. |
| 193 | + rh.printLn("ES", msgs.msg['0421'][1] % (modId, strCmd, |
| 194 | + type(e).__name__, str(e))) |
| 195 | + rh.updateResults(msgs.msg['0421'][0]) |
| 196 | + |
| 197 | + return OSA_info |
0 commit comments