Justin Thaler | b8807ce | 2018-05-25 19:16:20 -0500 | [diff] [blame] | 1 | #!/usr/bin/python3 |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2 | """ |
Joseph Reynolds | a2d54c5 | 2019-06-11 22:02:57 -0500 | [diff] [blame] | 3 | Copyright 2017,2019 IBM Corporation |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 5 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | you may not use this file except in compliance with the License. |
| 7 | You may obtain a copy of the License at |
| 8 | |
| 9 | http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | |
| 11 | Unless required by applicable law or agreed to in writing, software |
| 12 | distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | See the License for the specific language governing permissions and |
| 15 | limitations under the License. |
| 16 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 17 | import argparse |
| 18 | import requests |
| 19 | import getpass |
| 20 | import json |
| 21 | import os |
| 22 | import urllib3 |
| 23 | import time, datetime |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 24 | import binascii |
| 25 | import subprocess |
| 26 | import platform |
| 27 | import zipfile |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 28 | import tarfile |
| 29 | import tempfile |
| 30 | import hashlib |
Justin Thaler | a6b5df7 | 2018-07-16 11:10:07 -0500 | [diff] [blame] | 31 | import re |
Justin Thaler | 24d4efa | 2018-11-08 22:48:10 -0600 | [diff] [blame] | 32 | import uuid |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 33 | |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 34 | jsonHeader = {'Content-Type' : 'application/json'} |
| 35 | xAuthHeader = {} |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 36 | baseTimeout = 60 |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 37 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 38 | def hilight(textToColor, color, bold): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 39 | """ |
| 40 | Used to add highlights to various text for displaying in a terminal |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 41 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 42 | @param textToColor: string, the text to be colored |
| 43 | @param color: string, used to color the text red or green |
| 44 | @param bold: boolean, used to bold the textToColor |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 45 | @return: Buffered reader containing the modified string. |
| 46 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 47 | if(sys.platform.__contains__("win")): |
| 48 | if(color == "red"): |
| 49 | os.system('color 04') |
| 50 | elif(color == "green"): |
| 51 | os.system('color 02') |
| 52 | else: |
| 53 | os.system('color') #reset to default |
| 54 | return textToColor |
| 55 | else: |
| 56 | attr = [] |
| 57 | if(color == "red"): |
| 58 | attr.append('31') |
| 59 | elif(color == "green"): |
| 60 | attr.append('32') |
| 61 | else: |
| 62 | attr.append('0') |
| 63 | if bold: |
| 64 | attr.append('1') |
| 65 | else: |
| 66 | attr.append('0') |
| 67 | return '\x1b[%sm%s\x1b[0m' % (';'.join(attr),textToColor) |
| 68 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 69 | def connectionErrHandler(jsonFormat, errorStr, err): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 70 | """ |
| 71 | Error handler various connection errors to bmcs |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 72 | |
| 73 | @param jsonFormat: boolean, used to output in json format with an error code. |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 74 | @param errorStr: string, used to color the text red or green |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 75 | @param err: string, the text from the exception |
| 76 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 77 | if errorStr == "Timeout": |
| 78 | if not jsonFormat: |
| 79 | return("FQPSPIN0000M: Connection timed out. Ensure you have network connectivity to the bmc") |
| 80 | else: |
Justin Thaler | 115bca7 | 2018-05-25 19:29:08 -0500 | [diff] [blame] | 81 | conerror = {} |
| 82 | conerror['CommonEventID'] = 'FQPSPIN0000M' |
| 83 | conerror['sensor']="N/A" |
| 84 | conerror['state']="N/A" |
| 85 | conerror['additionalDetails'] = "N/A" |
| 86 | conerror['Message']="Connection timed out. Ensure you have network connectivity to the BMC" |
| 87 | conerror['LengthyDescription'] = "While trying to establish a connection with the specified BMC, the BMC failed to respond in adequate time. Verify the BMC is functioning properly, and the network connectivity to the BMC is stable." |
| 88 | conerror['Serviceable']="Yes" |
| 89 | conerror['CallHomeCandidate']= "No" |
| 90 | conerror['Severity'] = "Critical" |
| 91 | conerror['EventType'] = "Communication Failure/Timeout" |
| 92 | conerror['VMMigrationFlag'] = "Yes" |
| 93 | conerror["AffectedSubsystem"] = "Interconnect (Networking)" |
| 94 | conerror["timestamp"] = str(int(time.time())) |
| 95 | conerror["UserAction"] = "Verify network connectivity between the two systems and the bmc is functional." |
| 96 | eventdict = {} |
| 97 | eventdict['event0'] = conerror |
| 98 | eventdict['numAlerts'] = '1' |
Justin Thaler | 115bca7 | 2018-05-25 19:29:08 -0500 | [diff] [blame] | 99 | errorMessageStr = errorMessageStr = json.dumps(eventdict, sort_keys=True, indent=4, separators=(',', ': '), ensure_ascii=False) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 100 | return(errorMessageStr) |
| 101 | elif errorStr == "ConnectionError": |
| 102 | if not jsonFormat: |
| 103 | return("FQPSPIN0001M: " + str(err)) |
| 104 | else: |
Justin Thaler | 115bca7 | 2018-05-25 19:29:08 -0500 | [diff] [blame] | 105 | conerror = {} |
| 106 | conerror['CommonEventID'] = 'FQPSPIN0001M' |
| 107 | conerror['sensor']="N/A" |
| 108 | conerror['state']="N/A" |
| 109 | conerror['additionalDetails'] = str(err) |
| 110 | conerror['Message']="Connection Error. View additional details for more information" |
| 111 | conerror['LengthyDescription'] = "A connection error to the specified BMC occurred and additional details are provided. Review these details to resolve the issue." |
| 112 | conerror['Serviceable']="Yes" |
| 113 | conerror['CallHomeCandidate']= "No" |
| 114 | conerror['Severity'] = "Critical" |
| 115 | conerror['EventType'] = "Communication Failure/Timeout" |
| 116 | conerror['VMMigrationFlag'] = "Yes" |
| 117 | conerror["AffectedSubsystem"] = "Interconnect (Networking)" |
| 118 | conerror["timestamp"] = str(int(time.time())) |
| 119 | conerror["UserAction"] = "Correct the issue highlighted in additional details and try again" |
| 120 | eventdict = {} |
| 121 | eventdict['event0'] = conerror |
| 122 | eventdict['numAlerts'] = '1' |
Justin Thaler | 115bca7 | 2018-05-25 19:29:08 -0500 | [diff] [blame] | 123 | errorMessageStr = json.dumps(eventdict, sort_keys=True, indent=4, separators=(',', ': '), ensure_ascii=False) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 124 | return(errorMessageStr) |
Justin Thaler | 115bca7 | 2018-05-25 19:29:08 -0500 | [diff] [blame] | 125 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 126 | else: |
| 127 | return("Unknown Error: "+ str(err)) |
| 128 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 129 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 130 | def setColWidth(keylist, numCols, dictForOutput, colNames): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 131 | """ |
| 132 | Sets the output width of the columns to display |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 133 | |
| 134 | @param keylist: list, list of strings representing the keys for the dictForOutput |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 135 | @param numcols: the total number of columns in the final output |
| 136 | @param dictForOutput: dictionary, contains the information to print to the screen |
| 137 | @param colNames: list, The strings to use for the column headings, in order of the keylist |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 138 | @return: A list of the column widths for each respective column. |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 139 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 140 | colWidths = [] |
| 141 | for x in range(0, numCols): |
| 142 | colWidths.append(0) |
| 143 | for key in dictForOutput: |
| 144 | for x in range(0, numCols): |
| 145 | colWidths[x] = max(colWidths[x], len(str(dictForOutput[key][keylist[x]]))) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 146 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 147 | for x in range(0, numCols): |
| 148 | colWidths[x] = max(colWidths[x], len(colNames[x])) +2 |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 149 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 150 | return colWidths |
| 151 | |
| 152 | def loadPolicyTable(pathToPolicyTable): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 153 | """ |
| 154 | loads a json based policy table into a dictionary |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 155 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 156 | @param value: boolean, the value to convert |
| 157 | @return: A string of "Yes" or "No" |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 158 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 159 | policyTable = {} |
| 160 | if(os.path.exists(pathToPolicyTable)): |
| 161 | with open(pathToPolicyTable, 'r') as stream: |
| 162 | try: |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 163 | contents =json.load(stream) |
| 164 | policyTable = contents['events'] |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 165 | except Exception as err: |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 166 | print(err) |
| 167 | return policyTable |
| 168 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 169 | |
| 170 | def boolToString(value): |
| 171 | """ |
| 172 | converts a boolean value to a human readable string value |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 173 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 174 | @param value: boolean, the value to convert |
| 175 | @return: A string of "Yes" or "No" |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 176 | """ |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 177 | if(value): |
| 178 | return "Yes" |
| 179 | else: |
| 180 | return "No" |
| 181 | |
Justin Thaler | a6b5df7 | 2018-07-16 11:10:07 -0500 | [diff] [blame] | 182 | def stringToInt(text): |
| 183 | """ |
| 184 | returns an integer if the string can be converted, otherwise returns the string |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 185 | |
Justin Thaler | a6b5df7 | 2018-07-16 11:10:07 -0500 | [diff] [blame] | 186 | @param text: the string to try to convert to an integer |
| 187 | """ |
| 188 | if text.isdigit(): |
| 189 | return int(text) |
| 190 | else: |
| 191 | return text |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 192 | |
Justin Thaler | a6b5df7 | 2018-07-16 11:10:07 -0500 | [diff] [blame] | 193 | def naturalSort(text): |
| 194 | """ |
| 195 | provides a way to naturally sort a list |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 196 | |
Justin Thaler | a6b5df7 | 2018-07-16 11:10:07 -0500 | [diff] [blame] | 197 | @param text: the key to convert for sorting |
| 198 | @return list containing the broken up string parts by integers and strings |
| 199 | """ |
| 200 | stringPartList = [] |
| 201 | for c in re.split('(\d+)', text): |
| 202 | stringPartList.append(stringToInt(c)) |
| 203 | return stringPartList |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 204 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 205 | def tableDisplay(keylist, colNames, output): |
| 206 | """ |
| 207 | Logs into the BMC and creates a session |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 208 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 209 | @param keylist: list, keys for the output dictionary, ordered by colNames |
| 210 | @param colNames: Names for the Table of the columns |
| 211 | @param output: The dictionary of data to display |
| 212 | @return: Session object |
| 213 | """ |
| 214 | colWidth = setColWidth(keylist, len(colNames), output, colNames) |
| 215 | row = "" |
| 216 | outputText = "" |
| 217 | for i in range(len(colNames)): |
| 218 | if (i != 0): row = row + "| " |
| 219 | row = row + colNames[i].ljust(colWidth[i]) |
| 220 | outputText += row + "\n" |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 221 | |
Justin Thaler | a6b5df7 | 2018-07-16 11:10:07 -0500 | [diff] [blame] | 222 | output_keys = list(output.keys()) |
| 223 | output_keys.sort(key=naturalSort) |
| 224 | for key in output_keys: |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 225 | row = "" |
Justin Thaler | 8fe0c73 | 2018-07-24 14:32:35 -0500 | [diff] [blame] | 226 | for i in range(len(keylist)): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 227 | if (i != 0): row = row + "| " |
| 228 | row = row + output[key][keylist[i]].ljust(colWidth[i]) |
| 229 | outputText += row + "\n" |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 230 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 231 | return outputText |
| 232 | |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 233 | def checkFWactivation(host, args, session): |
| 234 | """ |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 235 | Checks the software inventory for an image that is being activated. |
| 236 | |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 237 | @return: True if an image is being activated, false is no activations are happening |
| 238 | """ |
| 239 | url="https://"+host+"/xyz/openbmc_project/software/enumerate" |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 240 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 241 | resp = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 242 | except(requests.exceptions.Timeout): |
| 243 | print(connectionErrHandler(args.json, "Timeout", None)) |
| 244 | return(True) |
| 245 | except(requests.exceptions.ConnectionError) as err: |
| 246 | print( connectionErrHandler(args.json, "ConnectionError", err)) |
| 247 | return True |
Justin Thaler | 3a5771b | 2019-01-23 14:31:52 -0600 | [diff] [blame] | 248 | fwInfo = resp.json()['data'] |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 249 | for key in fwInfo: |
| 250 | if 'Activation' in fwInfo[key]: |
| 251 | if 'Activating' in fwInfo[key]['Activation'] or 'Activating' in fwInfo[key]['RequestedActivation']: |
| 252 | return True |
| 253 | return False |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 254 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 255 | def login(host, username, pw,jsonFormat): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 256 | """ |
| 257 | Logs into the BMC and creates a session |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 258 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 259 | @param host: string, the hostname or IP address of the bmc to log into |
| 260 | @param username: The user name for the bmc to log into |
| 261 | @param pw: The password for the BMC to log into |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 262 | @param jsonFormat: boolean, flag that will only allow relevant data from user command to be display. This function becomes silent when set to true. |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 263 | @return: Session object |
| 264 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 265 | if(jsonFormat==False): |
| 266 | print("Attempting login...") |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 267 | mysess = requests.session() |
| 268 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 269 | r = mysess.post('https://'+host+'/login', headers=jsonHeader, json = {"data": [username, pw]}, verify=False, timeout=baseTimeout) |
Sunitha Harish | 336cda2 | 2019-07-23 02:02:52 -0500 | [diff] [blame] | 270 | if r.status_code == 200: |
| 271 | cookie = r.headers['Set-Cookie'] |
| 272 | match = re.search('SESSION=(\w+);', cookie) |
| 273 | if match: |
| 274 | xAuthHeader['X-Auth-Token'] = match.group(1) |
| 275 | jsonHeader.update(xAuthHeader) |
| 276 | loginMessage = json.loads(r.text) |
| 277 | if (loginMessage['status'] != "ok"): |
| 278 | print(loginMessage["data"]["description"].encode('utf-8')) |
| 279 | sys.exit(1) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 280 | # if(sys.version_info < (3,0)): |
| 281 | # urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) |
| 282 | # if sys.version_info >= (3,0): |
| 283 | # requests.packages.urllib3.disable_warnings(requests.packages.urllib3.exceptions.InsecureRequestWarning) |
Sunitha Harish | 336cda2 | 2019-07-23 02:02:52 -0500 | [diff] [blame] | 284 | return mysess |
| 285 | else: |
| 286 | return None |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 287 | except(requests.exceptions.Timeout): |
Justin Thaler | 115bca7 | 2018-05-25 19:29:08 -0500 | [diff] [blame] | 288 | return (connectionErrHandler(jsonFormat, "Timeout", None)) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 289 | except(requests.exceptions.ConnectionError) as err: |
Justin Thaler | 115bca7 | 2018-05-25 19:29:08 -0500 | [diff] [blame] | 290 | return (connectionErrHandler(jsonFormat, "ConnectionError", err)) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 291 | |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 292 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 293 | def logout(host, username, pw, session, jsonFormat): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 294 | """ |
| 295 | Logs out of the bmc and terminates the session |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 296 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 297 | @param host: string, the hostname or IP address of the bmc to log out of |
| 298 | @param username: The user name for the bmc to log out of |
| 299 | @param pw: The password for the BMC to log out of |
| 300 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 301 | @param jsonFormat: boolean, flag that will only allow relevant data from user command to be display. This function becomes silent when set to true. |
| 302 | """ |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 303 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 304 | r = session.post('https://'+host+'/logout', headers=jsonHeader,json = {"data": [username, pw]}, verify=False, timeout=baseTimeout) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 305 | except(requests.exceptions.Timeout): |
| 306 | print(connectionErrHandler(jsonFormat, "Timeout", None)) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 307 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 308 | if(jsonFormat==False): |
Matt Spinler | eae05b0 | 2019-01-24 12:59:34 -0600 | [diff] [blame] | 309 | if r.status_code == 200: |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 310 | print('User ' +username + ' has been logged out') |
| 311 | |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 312 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 313 | def fru(host, args, session): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 314 | """ |
| 315 | prints out the system inventory. deprecated see fruPrint and fruList |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 316 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 317 | @param host: string, the hostname or IP address of the bmc |
| 318 | @param args: contains additional arguments used by the fru sub command |
| 319 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 320 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 321 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 322 | #url="https://"+host+"/org/openbmc/inventory/system/chassis/enumerate" |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 323 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 324 | #print(url) |
| 325 | #res = session.get(url, headers=httpHeader, verify=False) |
| 326 | #print(res.text) |
| 327 | #sample = res.text |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 328 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 329 | #inv_list = json.loads(sample)["data"] |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 330 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 331 | url="https://"+host+"/xyz/openbmc_project/inventory/enumerate" |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 332 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 333 | res = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 334 | except(requests.exceptions.Timeout): |
| 335 | return(connectionErrHandler(args.json, "Timeout", None)) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 336 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 337 | sample = res.text |
| 338 | # inv_list.update(json.loads(sample)["data"]) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 339 | # |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 340 | # #determine column width's |
| 341 | # colNames = ["FRU Name", "FRU Type", "Has Fault", "Is FRU", "Present", "Version"] |
| 342 | # colWidths = setColWidth(["FRU Name", "fru_type", "fault", "is_fru", "present", "version"], 6, inv_list, colNames) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 343 | # |
| 344 | # print("FRU Name".ljust(colWidths[0])+ "FRU Type".ljust(colWidths[1]) + "Has Fault".ljust(colWidths[2]) + "Is FRU".ljust(colWidths[3])+ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 345 | # "Present".ljust(colWidths[4]) + "Version".ljust(colWidths[5])) |
| 346 | # format the output |
| 347 | # for key in sorted(inv_list.keys()): |
| 348 | # keyParts = key.split("/") |
| 349 | # isFRU = "True" if (inv_list[key]["is_fru"]==1) else "False" |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 350 | # |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 351 | # fruEntry = (keyParts[len(keyParts) - 1].ljust(colWidths[0]) + inv_list[key]["fru_type"].ljust(colWidths[1])+ |
| 352 | # inv_list[key]["fault"].ljust(colWidths[2])+isFRU.ljust(colWidths[3])+ |
| 353 | # inv_list[key]["present"].ljust(colWidths[4])+ inv_list[key]["version"].ljust(colWidths[5])) |
| 354 | # if(isTTY): |
| 355 | # if(inv_list[key]["is_fru"] == 1): |
| 356 | # color = "green" |
| 357 | # bold = True |
| 358 | # else: |
| 359 | # color='black' |
| 360 | # bold = False |
| 361 | # fruEntry = hilight(fruEntry, color, bold) |
| 362 | # print (fruEntry) |
| 363 | return sample |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 364 | |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 365 | def fruPrint(host, args, session): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 366 | """ |
| 367 | prints out all inventory |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 368 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 369 | @param host: string, the hostname or IP address of the bmc |
| 370 | @param args: contains additional arguments used by the fru sub command |
| 371 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 372 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 373 | @return returns the total fru list. |
| 374 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 375 | url="https://"+host+"/xyz/openbmc_project/inventory/enumerate" |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 376 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 377 | res = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 378 | except(requests.exceptions.Timeout): |
| 379 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 380 | |
Justin Thaler | 3a5771b | 2019-01-23 14:31:52 -0600 | [diff] [blame] | 381 | frulist={} |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 382 | # print(res.text) |
Justin Thaler | 3a5771b | 2019-01-23 14:31:52 -0600 | [diff] [blame] | 383 | if res.status_code==200: |
| 384 | frulist['Hardware'] = res.json()['data'] |
| 385 | else: |
| 386 | if not args.json: |
| 387 | return "Error retrieving the system inventory. BMC message: {msg}".format(msg=res.json()['message']) |
| 388 | else: |
| 389 | return res.json() |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 390 | url="https://"+host+"/xyz/openbmc_project/software/enumerate" |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 391 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 392 | res = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 393 | except(requests.exceptions.Timeout): |
| 394 | return(connectionErrHandler(args.json, "Timeout", None)) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 395 | # print(res.text) |
Justin Thaler | 3a5771b | 2019-01-23 14:31:52 -0600 | [diff] [blame] | 396 | if res.status_code==200: |
| 397 | frulist['Software'] = res.json()['data'] |
| 398 | else: |
| 399 | if not args.json(): |
| 400 | return "Error retrieving the system inventory. BMC message: {msg}".format(msg=res.json()['message']) |
| 401 | else: |
| 402 | return res.json() |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 403 | return frulist |
| 404 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 405 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 406 | def fruList(host, args, session): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 407 | """ |
| 408 | prints out all inventory or only a specific specified item |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 409 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 410 | @param host: string, the hostname or IP address of the bmc |
| 411 | @param args: contains additional arguments used by the fru sub command |
| 412 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 413 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 414 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 415 | if(args.items==True): |
| 416 | return fruPrint(host, args, session) |
| 417 | else: |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 418 | return fruPrint(host, args, session) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 419 | |
| 420 | |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 421 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 422 | def fruStatus(host, args, session): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 423 | """ |
| 424 | prints out the status of all FRUs |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 425 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 426 | @param host: string, the hostname or IP address of the bmc |
| 427 | @param args: contains additional arguments used by the fru sub command |
| 428 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 429 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 430 | """ |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 431 | url="https://"+host+"/xyz/openbmc_project/inventory/enumerate" |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 432 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 433 | res = session.get(url, headers=jsonHeader, verify=False) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 434 | except(requests.exceptions.Timeout): |
| 435 | return(connectionErrHandler(args.json, "Timeout", None)) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 436 | # print(res.text) |
Justin Thaler | 3a5771b | 2019-01-23 14:31:52 -0600 | [diff] [blame] | 437 | frulist = res.json()['data'] |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 438 | frus = {} |
| 439 | for key in frulist: |
| 440 | component = frulist[key] |
| 441 | isFru = False |
| 442 | present = False |
| 443 | func = False |
| 444 | hasSels = False |
| 445 | keyPieces = key.split('/') |
| 446 | fruName = keyPieces[-1] |
| 447 | if 'core' in fruName: #associate cores to cpus |
| 448 | fruName = keyPieces[-2] + '-' + keyPieces[-1] |
| 449 | if 'Functional' in component: |
| 450 | if('Present' in component): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 451 | if 'FieldReplaceable' in component: |
| 452 | if component['FieldReplaceable'] == 1: |
| 453 | isFru = True |
| 454 | if "fan" in fruName: |
| 455 | isFru = True; |
| 456 | if component['Present'] == 1: |
| 457 | present = True |
| 458 | if component['Functional'] == 1: |
| 459 | func = True |
| 460 | if ((key + "/fault") in frulist): |
| 461 | hasSels = True; |
| 462 | if args.verbose: |
| 463 | if hasSels: |
| 464 | loglist = [] |
| 465 | faults = frulist[key+"/fault"]['endpoints'] |
| 466 | for item in faults: |
| 467 | loglist.append(item.split('/')[-1]) |
| 468 | frus[fruName] = {"compName": fruName, "Functional": boolToString(func), "Present":boolToString(present), "IsFru": boolToString(isFru), "selList": ', '.join(loglist).strip() } |
| 469 | else: |
| 470 | frus[fruName] = {"compName": fruName, "Functional": boolToString(func), "Present":boolToString(present), "IsFru": boolToString(isFru), "selList": "None" } |
| 471 | else: |
| 472 | frus[fruName] = {"compName": fruName, "Functional": boolToString(func), "Present":boolToString(present), "IsFru": boolToString(isFru), "hasSEL": boolToString(hasSels) } |
Justin Thaler | fb9c81c | 2018-07-16 11:14:37 -0500 | [diff] [blame] | 473 | elif "power_supply" in fruName or "powersupply" in fruName: |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 474 | if component['Present'] ==1: |
| 475 | present = True |
| 476 | isFru = True |
| 477 | if ((key + "/fault") in frulist): |
| 478 | hasSels = True; |
| 479 | if args.verbose: |
| 480 | if hasSels: |
| 481 | loglist = [] |
| 482 | faults = frulist[key+"/fault"]['endpoints'] |
Obihörnchen | ff8035f | 2018-12-05 21:07:37 +0100 | [diff] [blame] | 483 | for item in faults: |
| 484 | loglist.append(item.split('/')[-1]) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 485 | frus[fruName] = {"compName": fruName, "Functional": "No", "Present":boolToString(present), "IsFru": boolToString(isFru), "selList": ', '.join(loglist).strip() } |
| 486 | else: |
| 487 | frus[fruName] = {"compName": fruName, "Functional": "Yes", "Present":boolToString(present), "IsFru": boolToString(isFru), "selList": "None" } |
| 488 | else: |
| 489 | frus[fruName] = {"compName": fruName, "Functional": boolToString(not hasSels), "Present":boolToString(present), "IsFru": boolToString(isFru), "hasSEL": boolToString(hasSels) } |
| 490 | if not args.json: |
| 491 | if not args.verbose: |
| 492 | colNames = ["Component", "Is a FRU", "Present", "Functional", "Has Logs"] |
| 493 | keylist = ["compName", "IsFru", "Present", "Functional", "hasSEL"] |
| 494 | else: |
| 495 | colNames = ["Component", "Is a FRU", "Present", "Functional", "Assoc. Log Number(s)"] |
| 496 | keylist = ["compName", "IsFru", "Present", "Functional", "selList"] |
| 497 | return tableDisplay(keylist, colNames, frus) |
| 498 | else: |
| 499 | return str(json.dumps(frus, sort_keys=True, indent=4, separators=(',', ': '), ensure_ascii=False)) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 500 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 501 | def sensor(host, args, session): |
| 502 | """ |
| 503 | prints out all sensors |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 504 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 505 | @param host: string, the hostname or IP address of the bmc |
| 506 | @param args: contains additional arguments used by the sensor sub command |
| 507 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 508 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 509 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 510 | url="https://"+host+"/xyz/openbmc_project/sensors/enumerate" |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 511 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 512 | res = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 513 | except(requests.exceptions.Timeout): |
| 514 | return(connectionErrHandler(args.json, "Timeout", None)) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 515 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 516 | #Get OCC status |
| 517 | url="https://"+host+"/org/open_power/control/enumerate" |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 518 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 519 | occres = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 520 | except(requests.exceptions.Timeout): |
| 521 | return(connectionErrHandler(args.json, "Timeout", None)) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 522 | if not args.json: |
| 523 | colNames = ['sensor', 'type', 'units', 'value', 'target'] |
Justin Thaler | 3a5771b | 2019-01-23 14:31:52 -0600 | [diff] [blame] | 524 | sensors = res.json()["data"] |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 525 | output = {} |
| 526 | for key in sensors: |
| 527 | senDict = {} |
| 528 | keyparts = key.split("/") |
| 529 | senDict['sensorName'] = keyparts[-1] |
| 530 | senDict['type'] = keyparts[-2] |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 531 | try: |
| 532 | senDict['units'] = sensors[key]['Unit'].split('.')[-1] |
| 533 | except KeyError: |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 534 | senDict['units'] = "N/A" |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 535 | if('Scale' in sensors[key]): |
| 536 | scale = 10 ** sensors[key]['Scale'] |
| 537 | else: |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 538 | scale = 1 |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 539 | try: |
| 540 | senDict['value'] = str(sensors[key]['Value'] * scale) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 541 | except KeyError: |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 542 | if 'value' in sensors[key]: |
| 543 | senDict['value'] = sensors[key]['value'] |
| 544 | else: |
| 545 | senDict['value'] = "N/A" |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 546 | if 'Target' in sensors[key]: |
| 547 | senDict['target'] = str(sensors[key]['Target']) |
| 548 | else: |
| 549 | senDict['target'] = 'N/A' |
| 550 | output[senDict['sensorName']] = senDict |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 551 | |
Justin Thaler | 3a5771b | 2019-01-23 14:31:52 -0600 | [diff] [blame] | 552 | occstatus = occres.json()["data"] |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 553 | if '/org/open_power/control/occ0' in occstatus: |
| 554 | occ0 = occstatus["/org/open_power/control/occ0"]['OccActive'] |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 555 | if occ0 == 1: |
| 556 | occ0 = 'Active' |
| 557 | else: |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 558 | occ0 = 'Inactive' |
| 559 | output['OCC0'] = {'sensorName':'OCC0', 'type': 'Discrete', 'units': 'N/A', 'value': occ0, 'target': 'Active'} |
| 560 | occ1 = occstatus["/org/open_power/control/occ1"]['OccActive'] |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 561 | if occ1 == 1: |
| 562 | occ1 = 'Active' |
| 563 | else: |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 564 | occ1 = 'Inactive' |
| 565 | output['OCC1'] = {'sensorName':'OCC1', 'type': 'Discrete', 'units': 'N/A', 'value': occ0, 'target': 'Active'} |
| 566 | else: |
| 567 | output['OCC0'] = {'sensorName':'OCC0', 'type': 'Discrete', 'units': 'N/A', 'value': 'Inactive', 'target': 'Inactive'} |
| 568 | output['OCC1'] = {'sensorName':'OCC1', 'type': 'Discrete', 'units': 'N/A', 'value': 'Inactive', 'target': 'Inactive'} |
| 569 | keylist = ['sensorName', 'type', 'units', 'value', 'target'] |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 570 | |
| 571 | return tableDisplay(keylist, colNames, output) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 572 | else: |
| 573 | return res.text + occres.text |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 574 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 575 | def sel(host, args, session): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 576 | """ |
| 577 | prints out the bmc alerts |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 578 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 579 | @param host: string, the hostname or IP address of the bmc |
| 580 | @param args: contains additional arguments used by the sel sub command |
| 581 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 582 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 583 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 584 | |
| 585 | url="https://"+host+"/xyz/openbmc_project/logging/entry/enumerate" |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 586 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 587 | res = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 588 | except(requests.exceptions.Timeout): |
| 589 | return(connectionErrHandler(args.json, "Timeout", None)) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 590 | return res.text |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 591 | |
| 592 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 593 | def parseESEL(args, eselRAW): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 594 | """ |
| 595 | parses the esel data and gets predetermined search terms |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 596 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 597 | @param eselRAW: string, the raw esel string from the bmc |
| 598 | @return: A dictionary containing the quick snapshot data unless args.fullEsel is listed then a full PEL log is returned |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 599 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 600 | eselParts = {} |
| 601 | esel_bin = binascii.unhexlify(''.join(eselRAW.split()[16:])) |
| 602 | #search terms contains the search term as the key and the return dictionary key as it's value |
| 603 | searchTerms = { 'Signature Description':'signatureDescription', 'devdesc':'devdesc', |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 604 | 'Callout type': 'calloutType', 'Procedure':'procedure', 'Sensor Type': 'sensorType'} |
Justin Thaler | 24d4efa | 2018-11-08 22:48:10 -0600 | [diff] [blame] | 605 | uniqueID = str(uuid.uuid4()) |
| 606 | eselBinPath = tempfile.gettempdir() + os.sep + uniqueID + 'esel.bin' |
Justin Thaler | cf1deae | 2018-05-25 19:35:21 -0500 | [diff] [blame] | 607 | with open(eselBinPath, 'wb') as f: |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 608 | f.write(esel_bin) |
| 609 | errlPath = "" |
| 610 | #use the right errl file for the machine architecture |
| 611 | arch = platform.machine() |
| 612 | if(arch =='x86_64' or arch =='AMD64'): |
| 613 | if os.path.exists('/opt/ibm/ras/bin/x86_64/errl'): |
| 614 | errlPath = '/opt/ibm/ras/bin/x86_64/errl' |
| 615 | elif os.path.exists('errl/x86_64/errl'): |
| 616 | errlPath = 'errl/x86_64/errl' |
| 617 | else: |
| 618 | errlPath = 'x86_64/errl' |
| 619 | elif (platform.machine()=='ppc64le'): |
| 620 | if os.path.exists('/opt/ibm/ras/bin/ppc64le/errl'): |
| 621 | errlPath = '/opt/ibm/ras/bin/ppc64le/errl' |
| 622 | elif os.path.exists('errl/ppc64le/errl'): |
| 623 | errlPath = 'errl/ppc64le/errl' |
| 624 | else: |
| 625 | errlPath = 'ppc64le/errl' |
| 626 | else: |
| 627 | print("machine architecture not supported for parsing eSELs") |
| 628 | return eselParts |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 629 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 630 | if(os.path.exists(errlPath)): |
Justin Thaler | cf1deae | 2018-05-25 19:35:21 -0500 | [diff] [blame] | 631 | output= subprocess.check_output([errlPath, '-d', '--file='+eselBinPath]).decode('utf-8') |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 632 | # output = proc.communicate()[0] |
| 633 | lines = output.split('\n') |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 634 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 635 | if(hasattr(args, 'fullEsel')): |
| 636 | return output |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 637 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 638 | for i in range(0, len(lines)): |
| 639 | lineParts = lines[i].split(':') |
| 640 | if(len(lineParts)>1): #ignore multi lines, output formatting lines, and other information |
| 641 | for term in searchTerms: |
| 642 | if(term in lineParts[0]): |
| 643 | temp = lines[i][lines[i].find(':')+1:].strip()[:-1].strip() |
| 644 | if lines[i+1].find(':') != -1: |
| 645 | if (len(lines[i+1].split(':')[0][1:].strip())==0): |
| 646 | while(len(lines[i][:lines[i].find(':')].strip())>2): |
Justin Thaler | 4303042 | 2018-11-08 22:50:21 -0600 | [diff] [blame] | 647 | #has multiple lines, process and update line counter |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 648 | if((i+1) <= len(lines)): |
| 649 | i+=1 |
| 650 | else: |
| 651 | i=i-1 |
| 652 | break |
Justin Thaler | 4303042 | 2018-11-08 22:50:21 -0600 | [diff] [blame] | 653 | #Append the content from the next line removing the pretty display characters |
| 654 | #Finds the first colon then starts 2 characters after, then removes all whitespace |
| 655 | temp = temp + lines[i][lines[i].find(':')+2:].strip()[:-1].strip()[:-1].strip() |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 656 | if(searchTerms[term] in eselParts): |
| 657 | eselParts[searchTerms[term]] = eselParts[searchTerms[term]] + ", " + temp |
| 658 | else: |
| 659 | eselParts[searchTerms[term]] = temp |
Justin Thaler | cf1deae | 2018-05-25 19:35:21 -0500 | [diff] [blame] | 660 | os.remove(eselBinPath) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 661 | else: |
| 662 | print("errl file cannot be found") |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 663 | |
| 664 | return eselParts |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 665 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 666 | |
Matt Spinler | 02d0dff | 2018-08-29 13:19:25 -0500 | [diff] [blame] | 667 | def getESELSeverity(esel): |
| 668 | """ |
| 669 | Finds the severity type in an eSEL from the User Header section. |
| 670 | @param esel - the eSEL data |
| 671 | @return severity - e.g. 'Critical' |
| 672 | """ |
| 673 | |
| 674 | # everything but 1 and 2 are Critical |
| 675 | # '1': 'recovered', |
| 676 | # '2': 'predictive', |
| 677 | # '4': 'unrecoverable', |
| 678 | # '5': 'critical', |
| 679 | # '6': 'diagnostic', |
| 680 | # '7': 'symptom' |
| 681 | severities = { |
| 682 | '1': 'Informational', |
| 683 | '2': 'Warning' |
| 684 | } |
| 685 | |
| 686 | try: |
| 687 | headerPosition = esel.index('55 48') # 'UH' |
| 688 | # The severity is the last byte in the 8 byte section (a byte is ' bb') |
| 689 | severity = esel[headerPosition:headerPosition+32].split(' ')[-1] |
| 690 | type = severity[0] |
| 691 | except ValueError: |
| 692 | print("Could not find severity value in UH section in eSEL") |
| 693 | type = 'x'; |
| 694 | |
| 695 | return severities.get(type, 'Critical') |
| 696 | |
| 697 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 698 | def sortSELs(events): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 699 | """ |
| 700 | sorts the sels by timestamp, then log entry number |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 701 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 702 | @param events: Dictionary containing events |
| 703 | @return: list containing a list of the ordered log entries, and dictionary of keys |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 704 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 705 | logNumList = [] |
| 706 | timestampList = [] |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 707 | eventKeyDict = {} |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 708 | eventsWithTimestamp = {} |
| 709 | logNum2events = {} |
| 710 | for key in events: |
| 711 | if key == 'numAlerts': continue |
| 712 | if 'callout' in key: continue |
| 713 | timestamp = (events[key]['timestamp']) |
| 714 | if timestamp not in timestampList: |
| 715 | eventsWithTimestamp[timestamp] = [events[key]['logNum']] |
| 716 | else: |
| 717 | eventsWithTimestamp[timestamp].append(events[key]['logNum']) |
| 718 | #map logNumbers to the event dictionary keys |
| 719 | eventKeyDict[str(events[key]['logNum'])] = key |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 720 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 721 | timestampList = list(eventsWithTimestamp.keys()) |
| 722 | timestampList.sort() |
| 723 | for ts in timestampList: |
| 724 | if len(eventsWithTimestamp[ts]) > 1: |
| 725 | tmplist = eventsWithTimestamp[ts] |
| 726 | tmplist.sort() |
| 727 | logNumList = logNumList + tmplist |
| 728 | else: |
| 729 | logNumList = logNumList + eventsWithTimestamp[ts] |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 730 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 731 | return [logNumList, eventKeyDict] |
| 732 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 733 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 734 | def parseAlerts(policyTable, selEntries, args): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 735 | """ |
| 736 | parses alerts in the IBM CER format, using an IBM policy Table |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 737 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 738 | @param policyTable: dictionary, the policy table entries |
| 739 | @param selEntries: dictionary, the alerts retrieved from the bmc |
| 740 | @return: A dictionary of the parsed entries, in chronological order |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 741 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 742 | eventDict = {} |
| 743 | eventNum ="" |
| 744 | count = 0 |
| 745 | esel = "" |
| 746 | eselParts = {} |
| 747 | i2cdevice= "" |
Matt Spinler | 02d0dff | 2018-08-29 13:19:25 -0500 | [diff] [blame] | 748 | eselSeverity = None |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 749 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 750 | 'prepare and sort the event entries' |
| 751 | for key in selEntries: |
| 752 | if 'callout' not in key: |
| 753 | selEntries[key]['logNum'] = key.split('/')[-1] |
| 754 | selEntries[key]['timestamp'] = selEntries[key]['Timestamp'] |
| 755 | sortedEntries = sortSELs(selEntries) |
| 756 | logNumList = sortedEntries[0] |
| 757 | eventKeyDict = sortedEntries[1] |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 758 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 759 | for logNum in logNumList: |
| 760 | key = eventKeyDict[logNum] |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 761 | hasEsel=False |
| 762 | i2creadFail = False |
| 763 | if 'callout' in key: |
| 764 | continue |
| 765 | else: |
| 766 | messageID = str(selEntries[key]['Message']) |
| 767 | addDataPiece = selEntries[key]['AdditionalData'] |
| 768 | calloutIndex = 0 |
| 769 | calloutFound = False |
| 770 | for i in range(len(addDataPiece)): |
| 771 | if("CALLOUT_INVENTORY_PATH" in addDataPiece[i]): |
| 772 | calloutIndex = i |
| 773 | calloutFound = True |
| 774 | fruCallout = str(addDataPiece[calloutIndex]).split('=')[1] |
| 775 | if("CALLOUT_DEVICE_PATH" in addDataPiece[i]): |
| 776 | i2creadFail = True |
Matt Spinler | d178a47 | 2018-08-31 09:48:52 -0500 | [diff] [blame] | 777 | |
| 778 | fruCallout = str(addDataPiece[calloutIndex]).split('=')[1] |
| 779 | |
| 780 | # Fall back to "I2C"/"FSI" if dev path isn't in policy table |
| 781 | if (messageID + '||' + fruCallout) not in policyTable: |
| 782 | i2cdevice = str(addDataPiece[i]).strip().split('=')[1] |
| 783 | i2cdevice = '/'.join(i2cdevice.split('/')[-4:]) |
| 784 | if 'fsi' in str(addDataPiece[calloutIndex]).split('=')[1]: |
| 785 | fruCallout = 'FSI' |
| 786 | else: |
| 787 | fruCallout = 'I2C' |
Justin Thaler | e34c43a | 2018-05-25 19:37:55 -0500 | [diff] [blame] | 788 | calloutFound = True |
| 789 | if("CALLOUT_GPIO_NUM" in addDataPiece[i]): |
| 790 | if not calloutFound: |
| 791 | fruCallout = 'GPIO' |
| 792 | calloutFound = True |
| 793 | if("CALLOUT_IIC_BUS" in addDataPiece[i]): |
| 794 | if not calloutFound: |
| 795 | fruCallout = "I2C" |
| 796 | calloutFound = True |
| 797 | if("CALLOUT_IPMI_SENSOR_NUM" in addDataPiece[i]): |
| 798 | if not calloutFound: |
| 799 | fruCallout = "IPMI" |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 800 | calloutFound = True |
| 801 | if("ESEL" in addDataPiece[i]): |
| 802 | esel = str(addDataPiece[i]).strip().split('=')[1] |
Matt Spinler | 02d0dff | 2018-08-29 13:19:25 -0500 | [diff] [blame] | 803 | eselSeverity = getESELSeverity(esel) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 804 | if args.devdebug: |
| 805 | eselParts = parseESEL(args, esel) |
| 806 | hasEsel=True |
| 807 | if("GPU" in addDataPiece[i]): |
| 808 | fruCallout = '/xyz/openbmc_project/inventory/system/chassis/motherboard/gpu' + str(addDataPiece[i]).strip()[-1] |
| 809 | calloutFound = True |
| 810 | if("PROCEDURE" in addDataPiece[i]): |
| 811 | fruCallout = str(hex(int(str(addDataPiece[i]).split('=')[1])))[2:] |
| 812 | calloutFound = True |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 813 | if("RAIL_NAME" in addDataPiece[i]): |
| 814 | calloutFound=True |
| 815 | fruCallout = str(addDataPiece[i]).split('=')[1].strip() |
| 816 | if("INPUT_NAME" in addDataPiece[i]): |
| 817 | calloutFound=True |
| 818 | fruCallout = str(addDataPiece[i]).split('=')[1].strip() |
| 819 | if("SENSOR_TYPE" in addDataPiece[i]): |
| 820 | calloutFound=True |
| 821 | fruCallout = str(addDataPiece[i]).split('=')[1].strip() |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 822 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 823 | if(calloutFound): |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 824 | if fruCallout != "": |
| 825 | policyKey = messageID +"||" + fruCallout |
Matt Spinler | 02d0dff | 2018-08-29 13:19:25 -0500 | [diff] [blame] | 826 | |
| 827 | # Also use the severity for hostboot errors |
| 828 | if eselSeverity and messageID == 'org.open_power.Host.Error.Event': |
| 829 | policyKey += '||' + eselSeverity |
| 830 | |
| 831 | # if not in the table, fall back to the original key |
| 832 | if policyKey not in policyTable: |
| 833 | policyKey = policyKey.replace('||'+eselSeverity, '') |
| 834 | |
Justin Thaler | e34c43a | 2018-05-25 19:37:55 -0500 | [diff] [blame] | 835 | if policyKey not in policyTable: |
| 836 | policyKey = messageID |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 837 | else: |
| 838 | policyKey = messageID |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 839 | else: |
| 840 | policyKey = messageID |
| 841 | event = {} |
| 842 | eventNum = str(count) |
| 843 | if policyKey in policyTable: |
| 844 | for pkey in policyTable[policyKey]: |
| 845 | if(type(policyTable[policyKey][pkey])== bool): |
| 846 | event[pkey] = boolToString(policyTable[policyKey][pkey]) |
| 847 | else: |
| 848 | if (i2creadFail and pkey == 'Message'): |
| 849 | event[pkey] = policyTable[policyKey][pkey] + ' ' +i2cdevice |
| 850 | else: |
| 851 | event[pkey] = policyTable[policyKey][pkey] |
| 852 | event['timestamp'] = selEntries[key]['Timestamp'] |
| 853 | event['resolved'] = bool(selEntries[key]['Resolved']) |
| 854 | if(hasEsel): |
| 855 | if args.devdebug: |
| 856 | event['eselParts'] = eselParts |
| 857 | event['raweSEL'] = esel |
| 858 | event['logNum'] = key.split('/')[-1] |
| 859 | eventDict['event' + eventNum] = event |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 860 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 861 | else: |
| 862 | severity = str(selEntries[key]['Severity']).split('.')[-1] |
| 863 | if severity == 'Error': |
| 864 | severity = 'Critical' |
| 865 | eventDict['event'+eventNum] = {} |
| 866 | eventDict['event' + eventNum]['error'] = "error: Not found in policy table: " + policyKey |
| 867 | eventDict['event' + eventNum]['timestamp'] = selEntries[key]['Timestamp'] |
| 868 | eventDict['event' + eventNum]['Severity'] = severity |
| 869 | if(hasEsel): |
| 870 | if args.devdebug: |
| 871 | eventDict['event' +eventNum]['eselParts'] = eselParts |
| 872 | eventDict['event' +eventNum]['raweSEL'] = esel |
| 873 | eventDict['event' +eventNum]['logNum'] = key.split('/')[-1] |
| 874 | eventDict['event' +eventNum]['resolved'] = bool(selEntries[key]['Resolved']) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 875 | count += 1 |
| 876 | return eventDict |
| 877 | |
| 878 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 879 | def selDisplay(events, args): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 880 | """ |
| 881 | displays alerts in human readable format |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 882 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 883 | @param events: Dictionary containing events |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 884 | @return: |
| 885 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 886 | activeAlerts = [] |
| 887 | historyAlerts = [] |
| 888 | sortedEntries = sortSELs(events) |
| 889 | logNumList = sortedEntries[0] |
| 890 | eventKeyDict = sortedEntries[1] |
| 891 | keylist = ['Entry', 'ID', 'Timestamp', 'Serviceable', 'Severity','Message'] |
| 892 | if(args.devdebug): |
| 893 | colNames = ['Entry', 'ID', 'Timestamp', 'Serviceable', 'Severity','Message', 'eSEL contents'] |
| 894 | keylist.append('eSEL') |
| 895 | else: |
| 896 | colNames = ['Entry', 'ID', 'Timestamp', 'Serviceable', 'Severity', 'Message'] |
| 897 | for log in logNumList: |
| 898 | selDict = {} |
| 899 | alert = events[eventKeyDict[str(log)]] |
| 900 | if('error' in alert): |
| 901 | selDict['Entry'] = alert['logNum'] |
| 902 | selDict['ID'] = 'Unknown' |
| 903 | selDict['Timestamp'] = datetime.datetime.fromtimestamp(int(alert['timestamp']/1000)).strftime("%Y-%m-%d %H:%M:%S") |
| 904 | msg = alert['error'] |
| 905 | polMsg = msg.split("policy table:")[0] |
| 906 | msg = msg.split("policy table:")[1] |
| 907 | msgPieces = msg.split("||") |
| 908 | err = msgPieces[0] |
| 909 | if(err.find("org.open_power.")!=-1): |
| 910 | err = err.split("org.open_power.")[1] |
| 911 | elif(err.find("xyz.openbmc_project.")!=-1): |
| 912 | err = err.split("xyz.openbmc_project.")[1] |
| 913 | else: |
| 914 | err = msgPieces[0] |
| 915 | callout = "" |
| 916 | if len(msgPieces) >1: |
| 917 | callout = msgPieces[1] |
| 918 | if(callout.find("/org/open_power/")!=-1): |
| 919 | callout = callout.split("/org/open_power/")[1] |
| 920 | elif(callout.find("/xyz/openbmc_project/")!=-1): |
| 921 | callout = callout.split("/xyz/openbmc_project/")[1] |
| 922 | else: |
| 923 | callout = msgPieces[1] |
| 924 | selDict['Message'] = polMsg +"policy table: "+ err + "||" + callout |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 925 | selDict['Serviceable'] = 'Unknown' |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 926 | selDict['Severity'] = alert['Severity'] |
| 927 | else: |
| 928 | selDict['Entry'] = alert['logNum'] |
| 929 | selDict['ID'] = alert['CommonEventID'] |
| 930 | selDict['Timestamp'] = datetime.datetime.fromtimestamp(int(alert['timestamp']/1000)).strftime("%Y-%m-%d %H:%M:%S") |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 931 | selDict['Message'] = alert['Message'] |
| 932 | selDict['Serviceable'] = alert['Serviceable'] |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 933 | selDict['Severity'] = alert['Severity'] |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 934 | |
| 935 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 936 | eselOrder = ['refCode','signatureDescription', 'eselType', 'devdesc', 'calloutType', 'procedure'] |
| 937 | if ('eselParts' in alert and args.devdebug): |
| 938 | eselOutput = "" |
| 939 | for item in eselOrder: |
| 940 | if item in alert['eselParts']: |
| 941 | eselOutput = eselOutput + item + ": " + alert['eselParts'][item] + " | " |
| 942 | selDict['eSEL'] = eselOutput |
| 943 | else: |
| 944 | if args.devdebug: |
| 945 | selDict['eSEL'] = "None" |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 946 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 947 | if not alert['resolved']: |
| 948 | activeAlerts.append(selDict) |
| 949 | else: |
| 950 | historyAlerts.append(selDict) |
| 951 | mergedOutput = activeAlerts + historyAlerts |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 952 | colWidth = setColWidth(keylist, len(colNames), dict(enumerate(mergedOutput)), colNames) |
| 953 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 954 | output = "" |
| 955 | if(len(activeAlerts)>0): |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 956 | row = "" |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 957 | output +="----Active Alerts----\n" |
| 958 | for i in range(0, len(colNames)): |
| 959 | if i!=0: row =row + "| " |
| 960 | row = row + colNames[i].ljust(colWidth[i]) |
| 961 | output += row + "\n" |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 962 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 963 | for i in range(0,len(activeAlerts)): |
| 964 | row = "" |
| 965 | for j in range(len(activeAlerts[i])): |
| 966 | if (j != 0): row = row + "| " |
| 967 | row = row + activeAlerts[i][keylist[j]].ljust(colWidth[j]) |
| 968 | output += row + "\n" |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 969 | |
| 970 | if(len(historyAlerts)>0): |
| 971 | row = "" |
| 972 | output+= "----Historical Alerts----\n" |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 973 | for i in range(len(colNames)): |
| 974 | if i!=0: row =row + "| " |
| 975 | row = row + colNames[i].ljust(colWidth[i]) |
| 976 | output += row + "\n" |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 977 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 978 | for i in range(0, len(historyAlerts)): |
| 979 | row = "" |
| 980 | for j in range(len(historyAlerts[i])): |
| 981 | if (j != 0): row = row + "| " |
| 982 | row = row + historyAlerts[i][keylist[j]].ljust(colWidth[j]) |
| 983 | output += row + "\n" |
| 984 | # print(events[eventKeyDict[str(log)]]) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 985 | return output |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 986 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 987 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 988 | def selPrint(host, args, session): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 989 | """ |
| 990 | prints out all bmc alerts |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 991 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 992 | @param host: string, the hostname or IP address of the bmc |
| 993 | @param args: contains additional arguments used by the fru sub command |
| 994 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 995 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 996 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 997 | if(args.policyTableLoc is None): |
| 998 | if os.path.exists('policyTable.json'): |
| 999 | ptableLoc = "policyTable.json" |
| 1000 | elif os.path.exists('/opt/ibm/ras/lib/policyTable.json'): |
| 1001 | ptableLoc = '/opt/ibm/ras/lib/policyTable.json' |
| 1002 | else: |
| 1003 | ptableLoc = 'lib/policyTable.json' |
| 1004 | else: |
| 1005 | ptableLoc = args.policyTableLoc |
| 1006 | policyTable = loadPolicyTable(ptableLoc) |
| 1007 | rawselEntries = "" |
| 1008 | if(hasattr(args, 'fileloc') and args.fileloc is not None): |
| 1009 | if os.path.exists(args.fileloc): |
| 1010 | with open(args.fileloc, 'r') as selFile: |
| 1011 | selLines = selFile.readlines() |
| 1012 | rawselEntries = ''.join(selLines) |
| 1013 | else: |
| 1014 | print("Error: File not found") |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1015 | sys.exit(1) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1016 | else: |
| 1017 | rawselEntries = sel(host, args, session) |
| 1018 | loadFailed = False |
| 1019 | try: |
| 1020 | selEntries = json.loads(rawselEntries) |
| 1021 | except ValueError: |
| 1022 | loadFailed = True |
| 1023 | if loadFailed: |
| 1024 | cleanSels = json.dumps(rawselEntries).replace('\\n', '') |
| 1025 | #need to load json twice as original content was string escaped a second time |
| 1026 | selEntries = json.loads(json.loads(cleanSels)) |
| 1027 | selEntries = selEntries['data'] |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1028 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1029 | if 'description' in selEntries: |
| 1030 | if(args.json): |
| 1031 | return("{\n\t\"numAlerts\": 0\n}") |
| 1032 | else: |
| 1033 | return("No log entries found") |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1034 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1035 | else: |
| 1036 | if(len(policyTable)>0): |
| 1037 | events = parseAlerts(policyTable, selEntries, args) |
| 1038 | if(args.json): |
| 1039 | events["numAlerts"] = len(events) |
| 1040 | retValue = str(json.dumps(events, sort_keys=True, indent=4, separators=(',', ': '), ensure_ascii=False)) |
| 1041 | return retValue |
| 1042 | elif(hasattr(args, 'fullSel')): |
| 1043 | return events |
| 1044 | else: |
| 1045 | #get log numbers to order event entries sequentially |
| 1046 | return selDisplay(events, args) |
| 1047 | else: |
| 1048 | if(args.json): |
| 1049 | return selEntries |
| 1050 | else: |
| 1051 | print("error: Policy Table not found.") |
| 1052 | return selEntries |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1053 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1054 | def selList(host, args, session): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1055 | """ |
| 1056 | prints out all all bmc alerts, or only prints out the specified alerts |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1057 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1058 | @param host: string, the hostname or IP address of the bmc |
| 1059 | @param args: contains additional arguments used by the fru sub command |
| 1060 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1061 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 1062 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1063 | return(sel(host, args, session)) |
| 1064 | |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1065 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1066 | def selClear(host, args, session): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1067 | """ |
| 1068 | clears all alerts |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1069 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1070 | @param host: string, the hostname or IP address of the bmc |
| 1071 | @param args: contains additional arguments used by the fru sub command |
| 1072 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1073 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 1074 | """ |
Matt Spinler | 47b13e9 | 2019-01-04 14:58:53 -0600 | [diff] [blame] | 1075 | url="https://"+host+"/xyz/openbmc_project/logging/action/DeleteAll" |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1076 | data = "{\"data\": [] }" |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1077 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1078 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 1079 | res = session.post(url, headers=jsonHeader, data=data, verify=False, timeout=baseTimeout) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1080 | except(requests.exceptions.Timeout): |
| 1081 | return(connectionErrHandler(args.json, "Timeout", None)) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1082 | if res.status_code == 200: |
| 1083 | return "The Alert Log has been cleared. Please allow a few minutes for the action to complete." |
| 1084 | else: |
| 1085 | print("Unable to clear the logs, trying to clear 1 at a time") |
| 1086 | sels = json.loads(sel(host, args, session))['data'] |
| 1087 | for key in sels: |
| 1088 | if 'callout' not in key: |
| 1089 | logNum = key.split('/')[-1] |
| 1090 | url = "https://"+ host+ "/xyz/openbmc_project/logging/entry/"+logNum+"/action/Delete" |
| 1091 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 1092 | session.post(url, headers=jsonHeader, data=data, verify=False, timeout=baseTimeout) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1093 | except(requests.exceptions.Timeout): |
| 1094 | return connectionErrHandler(args.json, "Timeout", None) |
| 1095 | sys.exit(1) |
| 1096 | except(requests.exceptions.ConnectionError) as err: |
| 1097 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 1098 | sys.exit(1) |
| 1099 | return ('Sel clearing complete') |
| 1100 | |
| 1101 | def selSetResolved(host, args, session): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1102 | """ |
| 1103 | sets a sel entry to resolved |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1104 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1105 | @param host: string, the hostname or IP address of the bmc |
| 1106 | @param args: contains additional arguments used by the fru sub command |
| 1107 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1108 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 1109 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1110 | url="https://"+host+"/xyz/openbmc_project/logging/entry/" + str(args.selNum) + "/attr/Resolved" |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1111 | data = "{\"data\": 1 }" |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1112 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 1113 | res = session.put(url, headers=jsonHeader, data=data, verify=False, timeout=baseTimeout) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1114 | except(requests.exceptions.Timeout): |
| 1115 | return(connectionErrHandler(args.json, "Timeout", None)) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1116 | if res.status_code == 200: |
| 1117 | return "Sel entry "+ str(args.selNum) +" is now set to resolved" |
| 1118 | else: |
| 1119 | return "Unable to set the alert to resolved" |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1120 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1121 | def selResolveAll(host, args, session): |
| 1122 | """ |
| 1123 | sets a sel entry to resolved |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1124 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1125 | @param host: string, the hostname or IP address of the bmc |
| 1126 | @param args: contains additional arguments used by the fru sub command |
| 1127 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1128 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 1129 | """ |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1130 | rawselEntries = sel(host, args, session) |
| 1131 | loadFailed = False |
| 1132 | try: |
| 1133 | selEntries = json.loads(rawselEntries) |
| 1134 | except ValueError: |
| 1135 | loadFailed = True |
| 1136 | if loadFailed: |
| 1137 | cleanSels = json.dumps(rawselEntries).replace('\\n', '') |
| 1138 | #need to load json twice as original content was string escaped a second time |
| 1139 | selEntries = json.loads(json.loads(cleanSels)) |
| 1140 | selEntries = selEntries['data'] |
| 1141 | |
| 1142 | if 'description' in selEntries: |
| 1143 | if(args.json): |
| 1144 | return("{\n\t\"selsResolved\": 0\n}") |
| 1145 | else: |
| 1146 | return("No log entries found") |
| 1147 | else: |
| 1148 | d = vars(args) |
| 1149 | successlist = [] |
| 1150 | failedlist = [] |
| 1151 | for key in selEntries: |
| 1152 | if 'callout' not in key: |
| 1153 | d['selNum'] = key.split('/')[-1] |
| 1154 | resolved = selSetResolved(host,args,session) |
| 1155 | if 'Sel entry' in resolved: |
| 1156 | successlist.append(d['selNum']) |
| 1157 | else: |
| 1158 | failedlist.append(d['selNum']) |
| 1159 | output = "" |
| 1160 | successlist.sort() |
| 1161 | failedlist.sort() |
| 1162 | if len(successlist)>0: |
| 1163 | output = "Successfully resolved: " +', '.join(successlist) +"\n" |
| 1164 | if len(failedlist)>0: |
| 1165 | output += "Failed to resolve: " + ', '.join(failedlist) + "\n" |
| 1166 | return output |
| 1167 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1168 | def chassisPower(host, args, session): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1169 | """ |
| 1170 | called by the chassis function. Controls the power state of the chassis, or gets the status |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1171 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1172 | @param host: string, the hostname or IP address of the bmc |
| 1173 | @param args: contains additional arguments used by the fru sub command |
| 1174 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1175 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 1176 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1177 | if(args.powcmd == 'on'): |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 1178 | if checkFWactivation(host, args, session): |
| 1179 | return ("Chassis Power control disabled during firmware activation") |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1180 | print("Attempting to Power on...:") |
| 1181 | url="https://"+host+"/xyz/openbmc_project/state/host0/attr/RequestedHostTransition" |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1182 | data = '{"data":"xyz.openbmc_project.State.Host.Transition.On"}' |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1183 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 1184 | res = session.put(url, headers=jsonHeader, data=data, verify=False, timeout=baseTimeout) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1185 | except(requests.exceptions.Timeout): |
| 1186 | return(connectionErrHandler(args.json, "Timeout", None)) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1187 | return res.text |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1188 | elif(args.powcmd == 'softoff'): |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 1189 | if checkFWactivation(host, args, session): |
| 1190 | return ("Chassis Power control disabled during firmware activation") |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1191 | print("Attempting to Power off gracefully...:") |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1192 | url="https://"+host+"/xyz/openbmc_project/state/host0/attr/RequestedHostTransition" |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1193 | data = '{"data":"xyz.openbmc_project.State.Host.Transition.Off"}' |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1194 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 1195 | res = session.put(url, headers=jsonHeader, data=data, verify=False, timeout=baseTimeout) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1196 | except(requests.exceptions.Timeout): |
| 1197 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 1198 | return res.text |
| 1199 | elif(args.powcmd == 'hardoff'): |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 1200 | if checkFWactivation(host, args, session): |
| 1201 | return ("Chassis Power control disabled during firmware activation") |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1202 | print("Attempting to Power off immediately...:") |
| 1203 | url="https://"+host+"/xyz/openbmc_project/state/chassis0/attr/RequestedPowerTransition" |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1204 | data = '{"data":"xyz.openbmc_project.State.Chassis.Transition.Off"}' |
| 1205 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 1206 | res = session.put(url, headers=jsonHeader, data=data, verify=False, timeout=baseTimeout) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1207 | except(requests.exceptions.Timeout): |
| 1208 | return(connectionErrHandler(args.json, "Timeout", None)) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1209 | return res.text |
| 1210 | elif(args.powcmd == 'status'): |
| 1211 | url="https://"+host+"/xyz/openbmc_project/state/chassis0/attr/CurrentPowerState" |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1212 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 1213 | res = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1214 | except(requests.exceptions.Timeout): |
| 1215 | return(connectionErrHandler(args.json, "Timeout", None)) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1216 | chassisState = json.loads(res.text)['data'].split('.')[-1] |
| 1217 | url="https://"+host+"/xyz/openbmc_project/state/host0/attr/CurrentHostState" |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1218 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 1219 | res = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1220 | except(requests.exceptions.Timeout): |
| 1221 | return(connectionErrHandler(args.json, "Timeout", None)) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1222 | hostState = json.loads(res.text)['data'].split('.')[-1] |
| 1223 | url="https://"+host+"/xyz/openbmc_project/state/bmc0/attr/CurrentBMCState" |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1224 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 1225 | res = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1226 | except(requests.exceptions.Timeout): |
| 1227 | return(connectionErrHandler(args.json, "Timeout", None)) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1228 | bmcState = json.loads(res.text)['data'].split('.')[-1] |
| 1229 | if(args.json): |
| 1230 | outDict = {"Chassis Power State" : chassisState, "Host Power State" : hostState, "BMC Power State":bmcState} |
| 1231 | return json.dumps(outDict, sort_keys=True, indent=4, separators=(',', ': '), ensure_ascii=False) |
| 1232 | else: |
| 1233 | return "Chassis Power State: " +chassisState + "\nHost Power State: " + hostState + "\nBMC Power State: " + bmcState |
| 1234 | else: |
| 1235 | return "Invalid chassis power command" |
| 1236 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1237 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1238 | def chassisIdent(host, args, session): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1239 | """ |
| 1240 | called by the chassis function. Controls the identify led of the chassis. Sets or gets the state |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1241 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1242 | @param host: string, the hostname or IP address of the bmc |
| 1243 | @param args: contains additional arguments used by the fru sub command |
| 1244 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1245 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1246 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1247 | if(args.identcmd == 'on'): |
| 1248 | print("Attempting to turn identify light on...:") |
| 1249 | url="https://"+host+"/xyz/openbmc_project/led/groups/enclosure_identify/attr/Asserted" |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1250 | data = '{"data":true}' |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1251 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 1252 | res = session.put(url, headers=jsonHeader, data=data, verify=False, timeout=baseTimeout) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1253 | except(requests.exceptions.Timeout): |
| 1254 | return(connectionErrHandler(args.json, "Timeout", None)) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1255 | return res.text |
| 1256 | elif(args.identcmd == 'off'): |
| 1257 | print("Attempting to turn identify light off...:") |
| 1258 | url="https://"+host+"/xyz/openbmc_project/led/groups/enclosure_identify/attr/Asserted" |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1259 | data = '{"data":false}' |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1260 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 1261 | res = session.put(url, headers=jsonHeader, data=data, verify=False, timeout=baseTimeout) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1262 | except(requests.exceptions.Timeout): |
| 1263 | return(connectionErrHandler(args.json, "Timeout", None)) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1264 | return res.text |
| 1265 | elif(args.identcmd == 'status'): |
| 1266 | url="https://"+host+"/xyz/openbmc_project/led/groups/enclosure_identify" |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1267 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 1268 | res = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1269 | except(requests.exceptions.Timeout): |
| 1270 | return(connectionErrHandler(args.json, "Timeout", None)) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1271 | status = json.loads(res.text)['data'] |
| 1272 | if(args.json): |
| 1273 | return status |
| 1274 | else: |
| 1275 | if status['Asserted'] == 0: |
| 1276 | return "Identify light is off" |
| 1277 | else: |
| 1278 | return "Identify light is blinking" |
| 1279 | else: |
| 1280 | return "Invalid chassis identify command" |
| 1281 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1282 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1283 | def chassis(host, args, session): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1284 | """ |
| 1285 | controls the different chassis commands |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1286 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1287 | @param host: string, the hostname or IP address of the bmc |
| 1288 | @param args: contains additional arguments used by the fru sub command |
| 1289 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1290 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 1291 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1292 | if(hasattr(args, 'powcmd')): |
| 1293 | result = chassisPower(host,args,session) |
| 1294 | elif(hasattr(args, 'identcmd')): |
| 1295 | result = chassisIdent(host, args, session) |
| 1296 | else: |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 1297 | return "This feature is not yet implemented" |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1298 | return result |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1299 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1300 | def bmcDumpRetrieve(host, args, session): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1301 | """ |
| 1302 | Downloads a dump file from the bmc |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1303 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1304 | @param host: string, the hostname or IP address of the bmc |
| 1305 | @param args: contains additional arguments used by the collectServiceData sub command |
| 1306 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1307 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1308 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1309 | dumpNum = args.dumpNum |
| 1310 | if (args.dumpSaveLoc is not None): |
| 1311 | saveLoc = args.dumpSaveLoc |
| 1312 | else: |
Justin Thaler | cf1deae | 2018-05-25 19:35:21 -0500 | [diff] [blame] | 1313 | saveLoc = tempfile.gettempdir() |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1314 | url ='https://'+host+'/download/dump/' + str(dumpNum) |
| 1315 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 1316 | r = session.get(url, headers=jsonHeader, stream=True, verify=False, timeout=baseTimeout) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1317 | if (args.dumpSaveLoc is not None): |
| 1318 | if os.path.exists(saveLoc): |
| 1319 | if saveLoc[-1] != os.path.sep: |
| 1320 | saveLoc = saveLoc + os.path.sep |
| 1321 | filename = saveLoc + host+'-dump' + str(dumpNum) + '.tar.xz' |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1322 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1323 | else: |
| 1324 | return 'Invalid save location specified' |
| 1325 | else: |
Justin Thaler | cf1deae | 2018-05-25 19:35:21 -0500 | [diff] [blame] | 1326 | filename = tempfile.gettempdir()+os.sep + host+'-dump' + str(dumpNum) + '.tar.xz' |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1327 | |
| 1328 | with open(filename, 'wb') as f: |
| 1329 | for chunk in r.iter_content(chunk_size =1024): |
| 1330 | if chunk: |
| 1331 | f.write(chunk) |
| 1332 | return 'Saved as ' + filename |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1333 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1334 | except(requests.exceptions.Timeout): |
| 1335 | return connectionErrHandler(args.json, "Timeout", None) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1336 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1337 | except(requests.exceptions.ConnectionError) as err: |
| 1338 | return connectionErrHandler(args.json, "ConnectionError", err) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1339 | |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1340 | def bmcDumpList(host, args, session): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1341 | """ |
| 1342 | Lists the number of dump files on the bmc |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1343 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1344 | @param host: string, the hostname or IP address of the bmc |
| 1345 | @param args: contains additional arguments used by the collectServiceData sub command |
| 1346 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1347 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 1348 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1349 | url ='https://'+host+'/xyz/openbmc_project/dump/list' |
| 1350 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 1351 | r = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
Justin Thaler | 3a5771b | 2019-01-23 14:31:52 -0600 | [diff] [blame] | 1352 | dumpList = r.json() |
| 1353 | return dumpList |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1354 | except(requests.exceptions.Timeout): |
| 1355 | return connectionErrHandler(args.json, "Timeout", None) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1356 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1357 | except(requests.exceptions.ConnectionError) as err: |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1358 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 1359 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1360 | def bmcDumpDelete(host, args, session): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1361 | """ |
| 1362 | Deletes BMC dump files from the bmc |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1363 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1364 | @param host: string, the hostname or IP address of the bmc |
| 1365 | @param args: contains additional arguments used by the collectServiceData sub command |
| 1366 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1367 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1368 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1369 | dumpList = [] |
| 1370 | successList = [] |
| 1371 | failedList = [] |
| 1372 | if args.dumpNum is not None: |
| 1373 | if isinstance(args.dumpNum, list): |
| 1374 | dumpList = args.dumpNum |
| 1375 | else: |
| 1376 | dumpList.append(args.dumpNum) |
| 1377 | for dumpNum in dumpList: |
| 1378 | url ='https://'+host+'/xyz/openbmc_project/dump/entry/'+str(dumpNum)+'/action/Delete' |
| 1379 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 1380 | r = session.post(url, headers=jsonHeader, json = {"data": []}, verify=False, timeout=baseTimeout) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1381 | if r.status_code == 200: |
| 1382 | successList.append(str(dumpNum)) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1383 | else: |
| 1384 | failedList.append(str(dumpNum)) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1385 | except(requests.exceptions.Timeout): |
| 1386 | return connectionErrHandler(args.json, "Timeout", None) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1387 | except(requests.exceptions.ConnectionError) as err: |
| 1388 | return connectionErrHandler(args.json, "ConnectionError", err) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1389 | output = "Successfully deleted dumps: " + ', '.join(successList) |
| 1390 | if(len(failedList)>0): |
| 1391 | output+= '\nFailed to delete dumps: ' + ', '.join(failedList) |
| 1392 | return output |
| 1393 | else: |
| 1394 | return 'You must specify an entry number to delete' |
| 1395 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1396 | def bmcDumpDeleteAll(host, args, session): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1397 | """ |
| 1398 | Deletes All BMC dump files from the bmc |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1399 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1400 | @param host: string, the hostname or IP address of the bmc |
| 1401 | @param args: contains additional arguments used by the collectServiceData sub command |
| 1402 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1403 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1404 | """ |
| 1405 | dumpResp = bmcDumpList(host, args, session) |
| 1406 | if 'FQPSPIN0000M' in dumpResp or 'FQPSPIN0001M'in dumpResp: |
| 1407 | return dumpResp |
Justin Thaler | 3a5771b | 2019-01-23 14:31:52 -0600 | [diff] [blame] | 1408 | dumpList = dumpResp['data'] |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1409 | d = vars(args) |
| 1410 | dumpNums = [] |
| 1411 | for dump in dumpList: |
| 1412 | if '/xyz/openbmc_project/dump/internal/manager' not in dump: |
| 1413 | dumpNums.append(int(dump.strip().split('/')[-1])) |
| 1414 | d['dumpNum'] = dumpNums |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1415 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1416 | return bmcDumpDelete(host, args, session) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1417 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1418 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1419 | def bmcDumpCreate(host, args, session): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1420 | """ |
| 1421 | Creates a bmc dump file |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1422 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1423 | @param host: string, the hostname or IP address of the bmc |
| 1424 | @param args: contains additional arguments used by the collectServiceData sub command |
| 1425 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1426 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1427 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1428 | url = 'https://'+host+'/xyz/openbmc_project/dump/action/CreateDump' |
| 1429 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 1430 | r = session.post(url, headers=jsonHeader, json = {"data": []}, verify=False, timeout=baseTimeout) |
Matt Spinler | eae05b0 | 2019-01-24 12:59:34 -0600 | [diff] [blame] | 1431 | if(r.status_code == 200 and not args.json): |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1432 | return ('Dump successfully created') |
Justin Thaler | 3a5771b | 2019-01-23 14:31:52 -0600 | [diff] [blame] | 1433 | elif(args.json): |
| 1434 | return r.json() |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1435 | else: |
| 1436 | return ('Failed to create dump') |
| 1437 | except(requests.exceptions.Timeout): |
| 1438 | return connectionErrHandler(args.json, "Timeout", None) |
| 1439 | except(requests.exceptions.ConnectionError) as err: |
| 1440 | return connectionErrHandler(args.json, "ConnectionError", err) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1441 | |
| 1442 | |
Justin Thaler | 666cf34 | 2019-01-23 14:44:27 -0600 | [diff] [blame] | 1443 | def csdDumpInitiate(host, args, session): |
| 1444 | """ |
| 1445 | Starts the process of getting the current list of dumps then initiates the creation of one. |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1446 | |
Justin Thaler | 666cf34 | 2019-01-23 14:44:27 -0600 | [diff] [blame] | 1447 | @param host: string, the hostname or IP address of the bmc |
| 1448 | @param args: contains additional arguments used by the collectServiceData sub command |
| 1449 | @param session: the active session to use |
| 1450 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 1451 | """ |
| 1452 | errorInfo = "" |
| 1453 | dumpcount = 0 |
| 1454 | try: |
| 1455 | d = vars(args) |
| 1456 | d['json'] = True |
| 1457 | except Exception as e: |
| 1458 | errorInfo += "Failed to set the json flag to True \n Exception: {eInfo}\n".format(eInfo=e) |
| 1459 | |
| 1460 | try: |
| 1461 | for i in range(3): |
| 1462 | dumpInfo = bmcDumpList(host, args, session) |
| 1463 | if 'data' in dumpInfo: |
| 1464 | dumpcount = len(dumpInfo['data']) |
| 1465 | break |
| 1466 | else: |
| 1467 | errorInfo+= "Dump List Message returned: " + json.dumps(dumpInfo,indent=0, separators=(',', ':')).replace('\n','') +"\n" |
| 1468 | except Exception as e: |
| 1469 | errorInfo+= "Failed to collect the list of dumps.\nException: {eInfo}\n".format(eInfo=e) |
| 1470 | |
| 1471 | #Create a user initiated dump |
| 1472 | try: |
| 1473 | for i in range(3): |
| 1474 | dumpcreated = bmcDumpCreate(host, args, session) |
| 1475 | if 'message' in dumpcreated: |
| 1476 | if 'ok' in dumpcreated['message'].lower(): |
| 1477 | break |
| 1478 | else: |
| 1479 | errorInfo+= "Dump create message returned: " + json.dumps(dumpInfo,indent=0, separators=(',', ':')).replace('\n','') +"\n" |
| 1480 | else: |
| 1481 | errorInfo+= "Dump create message returned: " + json.dumps(dumpInfo,indent=0, separators=(',', ':')).replace('\n','') +"\n" |
| 1482 | except Exception as e: |
| 1483 | errorInfo+= "Dump create exception encountered: {eInfo}\n".format(eInfo=e) |
| 1484 | |
| 1485 | output = {} |
| 1486 | output['errors'] = errorInfo |
| 1487 | output['dumpcount'] = dumpcount |
| 1488 | return output |
| 1489 | |
| 1490 | def csdInventory(host, args,session, fileDir): |
| 1491 | """ |
| 1492 | Collects the BMC inventory, retrying if necessary |
| 1493 | |
| 1494 | @param host: string, the hostname or IP address of the bmc |
| 1495 | @param args: contains additional arguments used by the collectServiceData sub command |
| 1496 | @param session: the active session to use |
| 1497 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 1498 | @param fileDir: string representation of the path to use for putting files created |
| 1499 | """ |
| 1500 | errorInfo = "===========Inventory =============\n" |
| 1501 | output={} |
| 1502 | inventoryCollected = False |
| 1503 | try: |
| 1504 | for i in range(3): |
| 1505 | frulist = fruPrint(host, args, session) |
| 1506 | if 'Hardware' in frulist: |
| 1507 | inventoryCollected = True |
| 1508 | break |
| 1509 | else: |
| 1510 | errorInfo += json.dumps(frulist, sort_keys=True, indent=4, separators=(',', ': '), ensure_ascii=False) + '\n' |
| 1511 | except Exception as e: |
| 1512 | errorInfo += "Inventory collection exception: {eInfo}\n".format(eInfo=e) |
| 1513 | if inventoryCollected: |
| 1514 | try: |
| 1515 | with open(fileDir +os.sep+'inventory.txt', 'w') as f: |
| 1516 | f.write(json.dumps(frulist, sort_keys=True, indent=4, separators=(',', ': '), ensure_ascii=False) + '\n') |
| 1517 | print("Inventory collected and stored in " + fileDir + os.sep + "inventory.txt") |
| 1518 | output['fileLoc'] = fileDir+os.sep+'inventory.txt' |
| 1519 | except Exception as e: |
| 1520 | print("Failed to write inventory to file.") |
| 1521 | errorInfo += "Error writing inventory to the file. Exception: {eInfo}\n".format(eInfo=e) |
| 1522 | |
| 1523 | output['errors'] = errorInfo |
| 1524 | |
| 1525 | return output |
| 1526 | |
| 1527 | def csdSensors(host, args,session, fileDir): |
| 1528 | """ |
| 1529 | Collects the BMC sensor readings, retrying if necessary |
| 1530 | |
| 1531 | @param host: string, the hostname or IP address of the bmc |
| 1532 | @param args: contains additional arguments used by the collectServiceData sub command |
| 1533 | @param session: the active session to use |
| 1534 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 1535 | @param fileDir: string representation of the path to use for putting files created |
| 1536 | """ |
| 1537 | errorInfo = "===========Sensors =============\n" |
| 1538 | sensorsCollected = False |
| 1539 | output={} |
| 1540 | try: |
| 1541 | d = vars(args) |
| 1542 | d['json'] = False |
| 1543 | except Exception as e: |
| 1544 | errorInfo += "Failed to set the json flag to False \n Exception: {eInfo}\n".format(eInfo=e) |
| 1545 | |
| 1546 | try: |
| 1547 | for i in range(3): |
| 1548 | sensorReadings = sensor(host, args, session) |
| 1549 | if 'OCC0' in sensorReadings: |
| 1550 | sensorsCollected = True |
| 1551 | break |
| 1552 | else: |
| 1553 | errorInfo += sensorReadings |
| 1554 | except Exception as e: |
| 1555 | errorInfo += "Sensor reading collection exception: {eInfo}\n".format(eInfo=e) |
| 1556 | if sensorsCollected: |
| 1557 | try: |
| 1558 | with open(fileDir +os.sep+'sensorReadings.txt', 'w') as f: |
| 1559 | f.write(sensorReadings) |
| 1560 | print("Sensor readings collected and stored in " + fileDir + os.sep+ "sensorReadings.txt") |
| 1561 | output['fileLoc'] = fileDir+os.sep+'sensorReadings.txt' |
| 1562 | except Exception as e: |
| 1563 | print("Failed to write sensor readings to file system.") |
| 1564 | errorInfo += "Error writing sensor readings to the file. Exception: {eInfo}\n".format(eInfo=e) |
| 1565 | |
| 1566 | output['errors'] = errorInfo |
| 1567 | return output |
| 1568 | |
| 1569 | def csdLEDs(host,args, session, fileDir): |
| 1570 | """ |
| 1571 | Collects the BMC LED status, retrying if necessary |
| 1572 | |
| 1573 | @param host: string, the hostname or IP address of the bmc |
| 1574 | @param args: contains additional arguments used by the collectServiceData sub command |
| 1575 | @param session: the active session to use |
| 1576 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 1577 | @param fileDir: string representation of the path to use for putting files created |
| 1578 | """ |
| 1579 | errorInfo = "===========LEDs =============\n" |
| 1580 | ledsCollected = False |
| 1581 | output={} |
| 1582 | try: |
| 1583 | d = vars(args) |
| 1584 | d['json'] = True |
| 1585 | except Exception as e: |
| 1586 | errorInfo += "Failed to set the json flag to False \n Exception: {eInfo}\n".format(eInfo=e) |
| 1587 | try: |
| 1588 | url="https://"+host+"/xyz/openbmc_project/led/enumerate" |
| 1589 | httpHeader = {'Content-Type':'application/json'} |
| 1590 | for i in range(3): |
| 1591 | try: |
| 1592 | ledRes = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
| 1593 | if ledRes.status_code == 200: |
| 1594 | ledsCollected = True |
| 1595 | leds = ledRes.json()['data'] |
| 1596 | break |
| 1597 | else: |
| 1598 | errorInfo += ledRes.text |
| 1599 | except(requests.exceptions.Timeout): |
| 1600 | errorInfo+=json.dumps( connectionErrHandler(args.json, "Timeout", None), sort_keys=True, indent=4, separators=(',', ': '), ensure_ascii=False) + '\n' |
| 1601 | except(requests.exceptions.ConnectionError) as err: |
| 1602 | errorInfo += json.dumps(connectionErrHandler(args.json, "ConnectionError", err), sort_keys=True, indent=4, separators=(',', ': '), ensure_ascii=False) + '\n' |
| 1603 | except Exception as e: |
| 1604 | errorInfo += "LED status collection exception: {eInfo}\n".format(eInfo=e) |
| 1605 | |
| 1606 | if ledsCollected: |
| 1607 | try: |
| 1608 | with open(fileDir +os.sep+'ledStatus.txt', 'w') as f: |
| 1609 | f.write(json.dumps(leds, sort_keys=True, indent=4, separators=(',', ': '), ensure_ascii=False) + '\n') |
| 1610 | print("LED status collected and stored in " + fileDir + os.sep+ "ledStatus.txt") |
| 1611 | output['fileLoc'] = fileDir+os.sep+'ledStatus.txt' |
| 1612 | except Exception as e: |
| 1613 | print("Failed to write LED status to file system.") |
| 1614 | errorInfo += "Error writing LED status to the file. Exception: {eInfo}\n".format(eInfo=e) |
| 1615 | |
| 1616 | output['errors'] = errorInfo |
| 1617 | return output |
| 1618 | |
| 1619 | def csdSelShortList(host, args, session, fileDir): |
| 1620 | """ |
| 1621 | Collects the BMC log entries, retrying if necessary |
| 1622 | |
| 1623 | @param host: string, the hostname or IP address of the bmc |
| 1624 | @param args: contains additional arguments used by the collectServiceData sub command |
| 1625 | @param session: the active session to use |
| 1626 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 1627 | @param fileDir: string representation of the path to use for putting files created |
| 1628 | """ |
| 1629 | errorInfo = "===========SEL Short List =============\n" |
| 1630 | selsCollected = False |
| 1631 | output={} |
| 1632 | try: |
| 1633 | d = vars(args) |
| 1634 | d['json'] = False |
| 1635 | except Exception as e: |
| 1636 | errorInfo += "Failed to set the json flag to False \n Exception: {eInfo}\n".format(eInfo=e) |
| 1637 | |
| 1638 | try: |
| 1639 | for i in range(3): |
| 1640 | sels = selPrint(host,args,session) |
| 1641 | if '----Active Alerts----' in sels or 'No log entries found' in sels or '----Historical Alerts----' in sels: |
| 1642 | selsCollected = True |
| 1643 | break |
| 1644 | else: |
| 1645 | errorInfo += sels + '\n' |
| 1646 | except Exception as e: |
| 1647 | errorInfo += "SEL short list collection exception: {eInfo}\n".format(eInfo=e) |
| 1648 | |
| 1649 | if selsCollected: |
| 1650 | try: |
| 1651 | with open(fileDir +os.sep+'SELshortlist.txt', 'w') as f: |
| 1652 | f.write(sels) |
| 1653 | print("SEL short list collected and stored in " + fileDir + os.sep+ "SELshortlist.txt") |
| 1654 | output['fileLoc'] = fileDir+os.sep+'SELshortlist.txt' |
| 1655 | except Exception as e: |
| 1656 | print("Failed to write SEL short list to file system.") |
| 1657 | errorInfo += "Error writing SEL short list to the file. Exception: {eInfo}\n".format(eInfo=e) |
| 1658 | |
| 1659 | output['errors'] = errorInfo |
| 1660 | return output |
| 1661 | |
| 1662 | def csdParsedSels(host, args, session, fileDir): |
| 1663 | """ |
| 1664 | Collects the BMC log entries, retrying if necessary |
| 1665 | |
| 1666 | @param host: string, the hostname or IP address of the bmc |
| 1667 | @param args: contains additional arguments used by the collectServiceData sub command |
| 1668 | @param session: the active session to use |
| 1669 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 1670 | @param fileDir: string representation of the path to use for putting files created |
| 1671 | """ |
| 1672 | errorInfo = "===========SEL Parsed List =============\n" |
| 1673 | selsCollected = False |
| 1674 | output={} |
| 1675 | try: |
| 1676 | d = vars(args) |
| 1677 | d['json'] = True |
| 1678 | d['fullEsel'] = True |
| 1679 | except Exception as e: |
| 1680 | errorInfo += "Failed to set the json flag to True \n Exception: {eInfo}\n".format(eInfo=e) |
| 1681 | |
| 1682 | try: |
| 1683 | for i in range(3): |
| 1684 | parsedfullsels = json.loads(selPrint(host,args,session)) |
| 1685 | if 'numAlerts' in parsedfullsels: |
| 1686 | selsCollected = True |
| 1687 | break |
| 1688 | else: |
| 1689 | errorInfo += parsedfullsels + '\n' |
| 1690 | except Exception as e: |
| 1691 | errorInfo += "Parsed full SELs collection exception: {eInfo}\n".format(eInfo=e) |
| 1692 | |
| 1693 | if selsCollected: |
| 1694 | try: |
| 1695 | sortedSELs = sortSELs(parsedfullsels) |
| 1696 | with open(fileDir +os.sep+'parsedSELs.txt', 'w') as f: |
| 1697 | for log in sortedSELs[0]: |
| 1698 | esel = "" |
| 1699 | parsedfullsels[sortedSELs[1][str(log)]]['timestamp'] = datetime.datetime.fromtimestamp(int(parsedfullsels[sortedSELs[1][str(log)]]['timestamp']/1000)).strftime("%Y-%m-%d %H:%M:%S") |
| 1700 | if ('raweSEL' in parsedfullsels[sortedSELs[1][str(log)]] and args.devdebug): |
| 1701 | esel = parsedfullsels[sortedSELs[1][str(log)]]['raweSEL'] |
| 1702 | del parsedfullsels[sortedSELs[1][str(log)]]['raweSEL'] |
| 1703 | f.write(json.dumps(parsedfullsels[sortedSELs[1][str(log)]],sort_keys=True, indent=4, separators=(',', ': '))) |
| 1704 | if(args.devdebug and esel != ""): |
| 1705 | f.write(parseESEL(args, esel)) |
| 1706 | print("Parsed SELs collected and stored in " + fileDir + os.sep+ "parsedSELs.txt") |
| 1707 | output['fileLoc'] = fileDir+os.sep+'parsedSELs.txt' |
| 1708 | except Exception as e: |
| 1709 | print("Failed to write fully parsed SELs to file system.") |
| 1710 | errorInfo += "Error writing fully parsed SELs to the file. Exception: {eInfo}\n".format(eInfo=e) |
| 1711 | |
| 1712 | output['errors'] = errorInfo |
| 1713 | return output |
| 1714 | |
| 1715 | def csdFullEnumeration(host, args, session, fileDir): |
| 1716 | """ |
| 1717 | Collects a full enumeration of /xyz/openbmc_project/, retrying if necessary |
| 1718 | |
| 1719 | @param host: string, the hostname or IP address of the bmc |
| 1720 | @param args: contains additional arguments used by the collectServiceData sub command |
| 1721 | @param session: the active session to use |
| 1722 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 1723 | @param fileDir: string representation of the path to use for putting files created |
| 1724 | """ |
| 1725 | errorInfo = "===========BMC Full Enumeration =============\n" |
| 1726 | bmcFullCollected = False |
| 1727 | output={} |
| 1728 | try: |
| 1729 | d = vars(args) |
| 1730 | d['json'] = True |
| 1731 | except Exception as e: |
| 1732 | errorInfo += "Failed to set the json flag to False \n Exception: {eInfo}\n".format(eInfo=e) |
| 1733 | try: |
| 1734 | print("Attempting to get a full BMC enumeration") |
| 1735 | url="https://"+host+"/xyz/openbmc_project/enumerate" |
| 1736 | httpHeader = {'Content-Type':'application/json'} |
| 1737 | for i in range(3): |
| 1738 | try: |
| 1739 | bmcRes = session.get(url, headers=jsonHeader, verify=False, timeout=180) |
| 1740 | if bmcRes.status_code == 200: |
| 1741 | bmcFullCollected = True |
| 1742 | fullEnumeration = bmcRes.json() |
| 1743 | break |
| 1744 | else: |
| 1745 | errorInfo += bmcRes.text |
| 1746 | except(requests.exceptions.Timeout): |
| 1747 | errorInfo+=json.dumps( connectionErrHandler(args.json, "Timeout", None), sort_keys=True, indent=4, separators=(',', ': '), ensure_ascii=False) + '\n' |
| 1748 | except(requests.exceptions.ConnectionError) as err: |
| 1749 | errorInfo += json.dumps(connectionErrHandler(args.json, "ConnectionError", err), sort_keys=True, indent=4, separators=(',', ': '), ensure_ascii=False) + '\n' |
| 1750 | except Exception as e: |
| 1751 | errorInfo += "RAW BMC data collection exception: {eInfo}\n".format(eInfo=e) |
| 1752 | |
| 1753 | if bmcFullCollected: |
| 1754 | try: |
| 1755 | with open(fileDir +os.sep+'bmcFullRaw.txt', 'w') as f: |
| 1756 | f.write(json.dumps(fullEnumeration, sort_keys=True, indent=4, separators=(',', ': '), ensure_ascii=False) + '\n') |
| 1757 | print("RAW BMC data collected and saved into " + fileDir + os.sep+ "bmcFullRaw.txt") |
| 1758 | output['fileLoc'] = fileDir+os.sep+'bmcFullRaw.txt' |
| 1759 | except Exception as e: |
| 1760 | print("Failed to write RAW BMC data to file system.") |
| 1761 | errorInfo += "Error writing RAW BMC data collection to the file. Exception: {eInfo}\n".format(eInfo=e) |
| 1762 | |
| 1763 | output['errors'] = errorInfo |
| 1764 | return output |
| 1765 | |
| 1766 | def csdCollectAllDumps(host, args, session, fileDir): |
| 1767 | """ |
| 1768 | Collects all of the bmc dump files and stores them in fileDir |
| 1769 | |
| 1770 | @param host: string, the hostname or IP address of the bmc |
| 1771 | @param args: contains additional arguments used by the collectServiceData sub command |
| 1772 | @param session: the active session to use |
| 1773 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 1774 | @param fileDir: string representation of the path to use for putting files created |
| 1775 | """ |
| 1776 | |
| 1777 | errorInfo = "===========BMC Dump Collection =============\n" |
| 1778 | dumpListCollected = False |
| 1779 | output={} |
| 1780 | dumpList = {} |
| 1781 | try: |
| 1782 | d = vars(args) |
| 1783 | d['json'] = True |
| 1784 | d['dumpSaveLoc'] = fileDir |
| 1785 | except Exception as e: |
| 1786 | errorInfo += "Failed to set the json flag to True, or failed to set the dumpSave Location \n Exception: {eInfo}\n".format(eInfo=e) |
| 1787 | |
| 1788 | print('Collecting bmc dump files') |
| 1789 | |
| 1790 | try: |
| 1791 | for i in range(3): |
| 1792 | dumpResp = bmcDumpList(host, args, session) |
| 1793 | if 'message' in dumpResp: |
| 1794 | if 'ok' in dumpResp['message'].lower(): |
| 1795 | dumpList = dumpResp['data'] |
| 1796 | dumpListCollected = True |
| 1797 | break |
| 1798 | else: |
| 1799 | errorInfo += "Status was not OK when retrieving the list of dumps available. \n Response: \n{resp}\n".format(resp=dumpResp) |
| 1800 | else: |
| 1801 | errorInfo += "Invalid response received from the BMC while retrieving the list of dumps available.\n {resp}\n".format(resp=dumpResp) |
| 1802 | except Exception as e: |
| 1803 | errorInfo += "BMC dump list exception: {eInfo}\n".format(eInfo=e) |
| 1804 | |
| 1805 | if dumpListCollected: |
| 1806 | output['fileList'] = [] |
| 1807 | for dump in dumpList: |
| 1808 | try: |
| 1809 | if '/xyz/openbmc_project/dump/internal/manager' not in dump: |
| 1810 | d['dumpNum'] = int(dump.strip().split('/')[-1]) |
| 1811 | print('retrieving dump file ' + str(d['dumpNum'])) |
| 1812 | filename = bmcDumpRetrieve(host, args, session).split('Saved as ')[-1] |
| 1813 | output['fileList'].append(filename) |
| 1814 | except Exception as e: |
| 1815 | print("Unable to collect dump: {dumpInfo}".format(dumpInfo=dump)) |
| 1816 | errorInfo += "Exception collecting a bmc dump {dumpInfo}\n {eInfo}\n".format(dumpInfo=dump, eInfo=e) |
| 1817 | output['errors'] = errorInfo |
| 1818 | return output |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1819 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1820 | def collectServiceData(host, args, session): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1821 | """ |
| 1822 | Collects all data needed for service from the BMC |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1823 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1824 | @param host: string, the hostname or IP address of the bmc |
| 1825 | @param args: contains additional arguments used by the collectServiceData sub command |
| 1826 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1827 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1828 | """ |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1829 | |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 1830 | global toolVersion |
Justin Thaler | 666cf34 | 2019-01-23 14:44:27 -0600 | [diff] [blame] | 1831 | filelist = [] |
| 1832 | errorInfo = "" |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1833 | |
Justin Thaler | 666cf34 | 2019-01-23 14:44:27 -0600 | [diff] [blame] | 1834 | #get current number of bmc dumps and create a new bmc dump |
| 1835 | dumpInitdata = csdDumpInitiate(host, args, session) |
| 1836 | dumpcount = dumpInitdata['dumpcount'] |
| 1837 | errorInfo += dumpInitdata['errors'] |
| 1838 | #create the directory to put files |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1839 | try: |
| 1840 | args.silent = True |
Justin Thaler | cf1deae | 2018-05-25 19:35:21 -0500 | [diff] [blame] | 1841 | myDir = tempfile.gettempdir()+os.sep + host + "--" + datetime.datetime.now().strftime("%Y-%m-%d_%H.%M.%S") |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1842 | os.makedirs(myDir) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1843 | |
Justin Thaler | 666cf34 | 2019-01-23 14:44:27 -0600 | [diff] [blame] | 1844 | except Exception as e: |
| 1845 | print('Unable to create the temporary directory for data collection. Ensure sufficient privileges to create temporary directory. Aborting.') |
| 1846 | return("Python exception: {eInfo}".format(eInfo = e)) |
| 1847 | |
| 1848 | #Collect Inventory |
| 1849 | inventoryData = csdInventory(host, args, session, myDir) |
| 1850 | if 'fileLoc' in inventoryData: |
| 1851 | filelist.append(inventoryData['fileLoc']) |
| 1852 | errorInfo += inventoryData['errors'] |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1853 | #Read all the sensor and OCC status |
Justin Thaler | 666cf34 | 2019-01-23 14:44:27 -0600 | [diff] [blame] | 1854 | sensorData = csdSensors(host,args,session,myDir) |
| 1855 | if 'fileLoc' in sensorData: |
| 1856 | filelist.append(sensorData['fileLoc']) |
| 1857 | errorInfo += sensorData['errors'] |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1858 | #Collect all of the LEDs status |
Justin Thaler | 666cf34 | 2019-01-23 14:44:27 -0600 | [diff] [blame] | 1859 | ledStatus = csdLEDs(host, args, session, myDir) |
| 1860 | if 'fileLoc' in ledStatus: |
| 1861 | filelist.append(ledStatus['fileLoc']) |
| 1862 | errorInfo += ledStatus['errors'] |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1863 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1864 | #Collect the bmc logs |
Justin Thaler | 666cf34 | 2019-01-23 14:44:27 -0600 | [diff] [blame] | 1865 | selShort = csdSelShortList(host, args, session, myDir) |
| 1866 | if 'fileLoc' in selShort: |
| 1867 | filelist.append(selShort['fileLoc']) |
| 1868 | errorInfo += selShort['errors'] |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1869 | |
Justin Thaler | 666cf34 | 2019-01-23 14:44:27 -0600 | [diff] [blame] | 1870 | parsedSELs = csdParsedSels(host, args, session, myDir) |
| 1871 | if 'fileLoc' in parsedSELs: |
| 1872 | filelist.append(parsedSELs['fileLoc']) |
| 1873 | errorInfo += parsedSELs['errors'] |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1874 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1875 | #collect RAW bmc enumeration |
Justin Thaler | 666cf34 | 2019-01-23 14:44:27 -0600 | [diff] [blame] | 1876 | bmcRaw = csdFullEnumeration(host, args, session, myDir) |
| 1877 | if 'fileLoc' in bmcRaw: |
| 1878 | filelist.append(bmcRaw['fileLoc']) |
| 1879 | errorInfo += bmcRaw['errors'] |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1880 | |
Justin Thaler | 666cf34 | 2019-01-23 14:44:27 -0600 | [diff] [blame] | 1881 | #wait for new dump to finish being created |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1882 | waitingForNewDump = True |
| 1883 | count = 0; |
Justin Thaler | 666cf34 | 2019-01-23 14:44:27 -0600 | [diff] [blame] | 1884 | print("Waiting for new BMC dump to finish being created.") |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1885 | while(waitingForNewDump): |
Justin Thaler | 666cf34 | 2019-01-23 14:44:27 -0600 | [diff] [blame] | 1886 | dumpList = bmcDumpList(host, args, session)['data'] |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1887 | if len(dumpList) > dumpcount: |
| 1888 | waitingForNewDump = False |
| 1889 | break; |
| 1890 | elif(count>30): |
| 1891 | print("Timed out waiting for bmc to make a new dump file. Dump space may be full.") |
| 1892 | break; |
| 1893 | else: |
| 1894 | time.sleep(2) |
| 1895 | count += 1 |
Justin Thaler | 666cf34 | 2019-01-23 14:44:27 -0600 | [diff] [blame] | 1896 | |
| 1897 | #collect all of the dump files |
| 1898 | getBMCDumps = csdCollectAllDumps(host, args, session, myDir) |
| 1899 | if 'fileList' in getBMCDumps: |
| 1900 | filelist+= getBMCDumps['fileList'] |
| 1901 | errorInfo += getBMCDumps['errors'] |
| 1902 | |
| 1903 | #write the runtime errors to a file |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1904 | try: |
Justin Thaler | 666cf34 | 2019-01-23 14:44:27 -0600 | [diff] [blame] | 1905 | with open(myDir +os.sep+'openbmctoolRuntimeErrors.txt', 'w') as f: |
| 1906 | f.write(errorInfo) |
| 1907 | print("OpenBMC tool runtime errors collected and stored in " + myDir + os.sep+ "openbmctoolRuntimeErrors.txt") |
| 1908 | filelist.append(myDir+os.sep+'openbmctoolRuntimeErrors.txt') |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1909 | except Exception as e: |
Justin Thaler | 666cf34 | 2019-01-23 14:44:27 -0600 | [diff] [blame] | 1910 | print("Failed to write OpenBMC tool runtime errors to file system.") |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1911 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1912 | #create the zip file |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1913 | try: |
Justin Thaler | cf1deae | 2018-05-25 19:35:21 -0500 | [diff] [blame] | 1914 | filename = myDir.split(tempfile.gettempdir()+os.sep)[-1] + "_" + toolVersion + '_openbmc.zip' |
Justin Thaler | 666cf34 | 2019-01-23 14:44:27 -0600 | [diff] [blame] | 1915 | zf = zipfile.ZipFile(myDir+os.sep + filename, 'w') |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1916 | for myfile in filelist: |
| 1917 | zf.write(myfile, os.path.basename(myfile)) |
| 1918 | zf.close() |
Justin Thaler | 666cf34 | 2019-01-23 14:44:27 -0600 | [diff] [blame] | 1919 | print("Zip file with all collected data created and stored in: {fileInfo}".format(fileInfo=myDir+os.sep+filename)) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1920 | except Exception as e: |
| 1921 | print("Failed to create zip file with collected information") |
Justin Thaler | 666cf34 | 2019-01-23 14:44:27 -0600 | [diff] [blame] | 1922 | return "data collection finished" |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1923 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1924 | |
| 1925 | def healthCheck(host, args, session): |
| 1926 | """ |
| 1927 | runs a health check on the platform |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1928 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1929 | @param host: string, the hostname or IP address of the bmc |
| 1930 | @param args: contains additional arguments used by the bmc sub command |
| 1931 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1932 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 1933 | """ |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1934 | #check fru status and get as json to easily work through |
| 1935 | d = vars(args) |
| 1936 | useJson = d['json'] |
| 1937 | d['json'] = True |
| 1938 | d['verbose']= False |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1939 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1940 | frus = json.loads(fruStatus(host, args, session)) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1941 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1942 | hwStatus= "OK" |
| 1943 | performanceStatus = "OK" |
| 1944 | for key in frus: |
| 1945 | if frus[key]["Functional"] == "No" and frus[key]["Present"] == "Yes": |
| 1946 | hwStatus= "Degraded" |
Justin Thaler | fb9c81c | 2018-07-16 11:14:37 -0500 | [diff] [blame] | 1947 | if("power_supply" in key or "powersupply" in key): |
| 1948 | gpuCount =0 |
| 1949 | for comp in frus: |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1950 | if "gv100card" in comp: |
| 1951 | gpuCount +=1 |
| 1952 | if gpuCount > 4: |
| 1953 | hwStatus = "Critical" |
| 1954 | performanceStatus="Degraded" |
| 1955 | break; |
| 1956 | elif("fan" in key): |
| 1957 | hwStatus = "Degraded" |
| 1958 | else: |
| 1959 | performanceStatus = "Degraded" |
| 1960 | if useJson: |
| 1961 | output = {"Hardware Status": hwStatus, "Performance": performanceStatus} |
| 1962 | output = json.dumps(output, sort_keys=True, indent=4, separators=(',', ': '), ensure_ascii=False) |
| 1963 | else: |
| 1964 | output = ("Hardware Status: " + hwStatus + |
| 1965 | "\nPerformance: " +performanceStatus ) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1966 | |
| 1967 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1968 | #SW407886: Clear the duplicate entries |
| 1969 | #collect the dups |
| 1970 | d['devdebug'] = False |
| 1971 | sels = json.loads(selPrint(host, args, session)) |
| 1972 | logNums2Clr = [] |
| 1973 | oldestLogNum={"logNum": "bogus" ,"key" : ""} |
| 1974 | count = 0 |
| 1975 | if sels['numAlerts'] > 0: |
| 1976 | for key in sels: |
| 1977 | if "numAlerts" in key: |
| 1978 | continue |
| 1979 | try: |
| 1980 | if "slave@00:00/00:00:00:06/sbefifo1-dev0/occ1-dev0" in sels[key]['Message']: |
| 1981 | count += 1 |
| 1982 | if count > 1: |
| 1983 | #preserve first occurrence |
| 1984 | if sels[key]['timestamp'] < sels[oldestLogNum['key']]['timestamp']: |
| 1985 | oldestLogNum['key']=key |
| 1986 | oldestLogNum['logNum'] = sels[key]['logNum'] |
| 1987 | else: |
| 1988 | oldestLogNum['key']=key |
| 1989 | oldestLogNum['logNum'] = sels[key]['logNum'] |
| 1990 | logNums2Clr.append(sels[key]['logNum']) |
| 1991 | except KeyError: |
| 1992 | continue |
| 1993 | if(count >0): |
| 1994 | logNums2Clr.remove(oldestLogNum['logNum']) |
| 1995 | #delete the dups |
| 1996 | if count >1: |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1997 | data = "{\"data\": [] }" |
| 1998 | for logNum in logNums2Clr: |
| 1999 | url = "https://"+ host+ "/xyz/openbmc_project/logging/entry/"+logNum+"/action/Delete" |
| 2000 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 2001 | session.post(url, headers=jsonHeader, data=data, verify=False, timeout=baseTimeout) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2002 | except(requests.exceptions.Timeout): |
| 2003 | deleteFailed = True |
| 2004 | except(requests.exceptions.ConnectionError) as err: |
| 2005 | deleteFailed = True |
| 2006 | #End of defect resolve code |
| 2007 | d['json'] = useJson |
| 2008 | return output |
| 2009 | |
| 2010 | |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2011 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 2012 | def bmc(host, args, session): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2013 | """ |
| 2014 | handles various bmc level commands, currently bmc rebooting |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2015 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2016 | @param host: string, the hostname or IP address of the bmc |
| 2017 | @param args: contains additional arguments used by the bmc sub command |
| 2018 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2019 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 2020 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 2021 | if(args.type is not None): |
| 2022 | return bmcReset(host, args, session) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2023 | if(args.info): |
| 2024 | return "Not implemented at this time" |
| 2025 | |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2026 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2027 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 2028 | def bmcReset(host, args, session): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2029 | """ |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2030 | controls resetting the bmc. warm reset reboots the bmc, cold reset removes the configuration and reboots. |
| 2031 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2032 | @param host: string, the hostname or IP address of the bmc |
| 2033 | @param args: contains additional arguments used by the bmcReset sub command |
| 2034 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2035 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 2036 | """ |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 2037 | if checkFWactivation(host, args, session): |
| 2038 | return ("BMC reset control disabled during firmware activation") |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 2039 | if(args.type == "warm"): |
| 2040 | print("\nAttempting to reboot the BMC...:") |
| 2041 | url="https://"+host+"/xyz/openbmc_project/state/bmc0/attr/RequestedBMCTransition" |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2042 | data = '{"data":"xyz.openbmc_project.State.BMC.Transition.Reboot"}' |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 2043 | res = session.put(url, headers=jsonHeader, data=data, verify=False, timeout=baseTimeout) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 2044 | return res.text |
| 2045 | elif(args.type =="cold"): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2046 | print("\nAttempting to reboot the BMC...:") |
| 2047 | url="https://"+host+"/xyz/openbmc_project/state/bmc0/attr/RequestedBMCTransition" |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2048 | data = '{"data":"xyz.openbmc_project.State.BMC.Transition.Reboot"}' |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 2049 | res = session.put(url, headers=jsonHeader, data=data, verify=False, timeout=baseTimeout) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2050 | return res.text |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 2051 | else: |
| 2052 | return "invalid command" |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2053 | |
| 2054 | def gardClear(host, args, session): |
| 2055 | """ |
| 2056 | clears the gard records from the bmc |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2057 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2058 | @param host: string, the hostname or IP address of the bmc |
| 2059 | @param args: contains additional arguments used by the gardClear sub command |
| 2060 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2061 | """ |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2062 | url="https://"+host+"/org/open_power/control/gard/action/Reset" |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2063 | data = '{"data":[]}' |
| 2064 | try: |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2065 | |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 2066 | res = session.post(url, headers=jsonHeader, data=data, verify=False, timeout=baseTimeout) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2067 | if res.status_code == 404: |
| 2068 | return "Command not supported by this firmware version" |
| 2069 | else: |
| 2070 | return res.text |
| 2071 | except(requests.exceptions.Timeout): |
| 2072 | return connectionErrHandler(args.json, "Timeout", None) |
| 2073 | except(requests.exceptions.ConnectionError) as err: |
| 2074 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 2075 | |
| 2076 | def activateFWImage(host, args, session): |
| 2077 | """ |
| 2078 | activates a firmware image on the bmc |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2079 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2080 | @param host: string, the hostname or IP address of the bmc |
| 2081 | @param args: contains additional arguments used by the fwflash sub command |
| 2082 | @param session: the active session to use |
| 2083 | @param fwID: the unique ID of the fw image to activate |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2084 | """ |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2085 | fwID = args.imageID |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2086 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2087 | #determine the existing versions |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2088 | url="https://"+host+"/xyz/openbmc_project/software/enumerate" |
| 2089 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 2090 | resp = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2091 | except(requests.exceptions.Timeout): |
| 2092 | return connectionErrHandler(args.json, "Timeout", None) |
| 2093 | except(requests.exceptions.ConnectionError) as err: |
| 2094 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 2095 | existingSoftware = json.loads(resp.text)['data'] |
| 2096 | altVersionID = '' |
| 2097 | versionType = '' |
| 2098 | imageKey = '/xyz/openbmc_project/software/'+fwID |
| 2099 | if imageKey in existingSoftware: |
| 2100 | versionType = existingSoftware[imageKey]['Purpose'] |
| 2101 | for key in existingSoftware: |
| 2102 | if imageKey == key: |
| 2103 | continue |
| 2104 | if 'Purpose' in existingSoftware[key]: |
| 2105 | if versionType == existingSoftware[key]['Purpose']: |
| 2106 | altVersionID = key.split('/')[-1] |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2107 | |
| 2108 | |
| 2109 | |
| 2110 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2111 | url="https://"+host+"/xyz/openbmc_project/software/"+ fwID + "/attr/Priority" |
| 2112 | url1="https://"+host+"/xyz/openbmc_project/software/"+ altVersionID + "/attr/Priority" |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2113 | data = "{\"data\": 0}" |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2114 | data1 = "{\"data\": 1 }" |
| 2115 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 2116 | resp = session.put(url, headers=jsonHeader, data=data, verify=False, timeout=baseTimeout) |
| 2117 | resp1 = session.put(url1, headers=jsonHeader, data=data1, verify=False, timeout=baseTimeout) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2118 | except(requests.exceptions.Timeout): |
| 2119 | return connectionErrHandler(args.json, "Timeout", None) |
| 2120 | except(requests.exceptions.ConnectionError) as err: |
| 2121 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 2122 | if(not args.json): |
| 2123 | if resp.status_code == 200 and resp1.status_code == 200: |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 2124 | return 'Firmware flash and activation completed. Please reboot the bmc and then boot the host OS for the changes to take effect. ' |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2125 | else: |
| 2126 | return "Firmware activation failed." |
| 2127 | else: |
| 2128 | return resp.text + resp1.text |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 2129 | |
| 2130 | def activateStatus(host, args, session): |
| 2131 | if checkFWactivation(host, args, session): |
| 2132 | return("Firmware is currently being activated. Do not reboot the BMC or start the Host OS") |
| 2133 | else: |
| 2134 | return("No firmware activations are pending") |
| 2135 | |
| 2136 | def extractFWimage(path, imageType): |
| 2137 | """ |
| 2138 | extracts the bmc image and returns information about the package |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2139 | |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 2140 | @param path: the path and file name of the firmware image |
| 2141 | @param imageType: The type of image the user is trying to flash. Host or BMC |
| 2142 | @return: the image id associated with the package. returns an empty string on error. |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2143 | """ |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 2144 | f = tempfile.TemporaryFile() |
| 2145 | tmpDir = tempfile.gettempdir() |
| 2146 | newImageID = "" |
| 2147 | if os.path.exists(path): |
| 2148 | try: |
| 2149 | imageFile = tarfile.open(path,'r') |
| 2150 | contents = imageFile.getmembers() |
| 2151 | for tf in contents: |
| 2152 | if 'MANIFEST' in tf.name: |
| 2153 | imageFile.extract(tf.name, path=tmpDir) |
| 2154 | with open(tempfile.gettempdir() +os.sep+ tf.name, 'r') as imageInfo: |
| 2155 | for line in imageInfo: |
| 2156 | if 'purpose' in line: |
| 2157 | purpose = line.split('=')[1] |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2158 | if imageType not in purpose.split('.')[-1]: |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 2159 | print('The specified image is not for ' + imageType) |
| 2160 | print('Please try again with the image for ' + imageType) |
| 2161 | return "" |
| 2162 | if 'version' == line.split('=')[0]: |
| 2163 | version = line.split('=')[1].strip().encode('utf-8') |
| 2164 | m = hashlib.sha512() |
| 2165 | m.update(version) |
| 2166 | newImageID = m.hexdigest()[:8] |
| 2167 | break |
| 2168 | try: |
| 2169 | os.remove(tempfile.gettempdir() +os.sep+ tf.name) |
| 2170 | except OSError: |
| 2171 | pass |
| 2172 | return newImageID |
| 2173 | except tarfile.ExtractError as e: |
| 2174 | print('Unable to extract information from the firmware file.') |
| 2175 | print('Ensure you have write access to the directory: ' + tmpDir) |
| 2176 | return newImageID |
| 2177 | except tarfile.TarError as e: |
| 2178 | print('This is not a valid firmware file.') |
| 2179 | return newImageID |
| 2180 | print("This is not a valid firmware file.") |
| 2181 | return newImageID |
| 2182 | else: |
| 2183 | print('The filename and path provided are not valid.') |
| 2184 | return newImageID |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2185 | |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 2186 | def getAllFWImageIDs(fwInvDict): |
| 2187 | """ |
| 2188 | gets a list of all the firmware image IDs |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2189 | |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 2190 | @param fwInvDict: the dictionary to search for FW image IDs |
| 2191 | @return: list containing string representation of the found image ids |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2192 | """ |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 2193 | idList = [] |
| 2194 | for key in fwInvDict: |
| 2195 | if 'Version' in fwInvDict[key]: |
| 2196 | idList.append(key.split('/')[-1]) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2197 | return idList |
| 2198 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2199 | def fwFlash(host, args, session): |
| 2200 | """ |
| 2201 | updates the bmc firmware and pnor firmware |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2202 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2203 | @param host: string, the hostname or IP address of the bmc |
| 2204 | @param args: contains additional arguments used by the fwflash sub command |
| 2205 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2206 | """ |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 2207 | d = vars(args) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2208 | if(args.type == 'bmc'): |
| 2209 | purp = 'BMC' |
| 2210 | else: |
| 2211 | purp = 'Host' |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2212 | |
| 2213 | #check power state of the machine. No concurrent FW updates allowed |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 2214 | d['powcmd'] = 'status' |
| 2215 | powerstate = chassisPower(host, args, session) |
| 2216 | if 'Chassis Power State: On' in powerstate: |
| 2217 | return("Aborting firmware update. Host is powered on. Please turn off the host and try again.") |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2218 | |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 2219 | #determine the existing images on the bmc |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2220 | url="https://"+host+"/xyz/openbmc_project/software/enumerate" |
| 2221 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 2222 | resp = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2223 | except(requests.exceptions.Timeout): |
| 2224 | return connectionErrHandler(args.json, "Timeout", None) |
| 2225 | except(requests.exceptions.ConnectionError) as err: |
| 2226 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 2227 | oldsoftware = json.loads(resp.text)['data'] |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2228 | |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 2229 | #Extract the tar and get information from the manifest file |
| 2230 | newversionID = extractFWimage(args.fileloc, purp) |
| 2231 | if newversionID == "": |
| 2232 | return "Unable to verify FW image." |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2233 | |
| 2234 | |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 2235 | #check if the new image is already on the bmc |
| 2236 | if newversionID not in getAllFWImageIDs(oldsoftware): |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2237 | |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 2238 | #upload the file |
| 2239 | httpHeader = {'Content-Type':'application/octet-stream'} |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 2240 | httpHeader.update(xAuthHeader) |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 2241 | url="https://"+host+"/upload/image" |
| 2242 | data=open(args.fileloc,'rb').read() |
| 2243 | print("Uploading file to BMC") |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2244 | try: |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 2245 | resp = session.post(url, headers=httpHeader, data=data, verify=False) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2246 | except(requests.exceptions.Timeout): |
| 2247 | return connectionErrHandler(args.json, "Timeout", None) |
| 2248 | except(requests.exceptions.ConnectionError) as err: |
| 2249 | return connectionErrHandler(args.json, "ConnectionError", err) |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 2250 | if resp.status_code != 200: |
| 2251 | return "Failed to upload the file to the bmc" |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2252 | else: |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 2253 | print("Upload complete.") |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2254 | |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 2255 | #verify bmc processed the image |
| 2256 | software ={} |
| 2257 | for i in range(0, 5): |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 2258 | url="https://"+host+"/xyz/openbmc_project/software/enumerate" |
| 2259 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 2260 | resp = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 2261 | except(requests.exceptions.Timeout): |
| 2262 | return connectionErrHandler(args.json, "Timeout", None) |
| 2263 | except(requests.exceptions.ConnectionError) as err: |
| 2264 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 2265 | software = json.loads(resp.text)['data'] |
| 2266 | #check if bmc is done processing the new image |
| 2267 | if (newversionID in getAllFWImageIDs(software)): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2268 | break |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 2269 | else: |
| 2270 | time.sleep(15) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2271 | |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 2272 | #activate the new image |
| 2273 | print("Activating new image: "+newversionID) |
| 2274 | url="https://"+host+"/xyz/openbmc_project/software/"+ newversionID + "/attr/RequestedActivation" |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2275 | data = '{"data":"xyz.openbmc_project.Software.Activation.RequestedActivations.Active"}' |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 2276 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 2277 | resp = session.put(url, headers=jsonHeader, data=data, verify=False, timeout=baseTimeout) |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 2278 | except(requests.exceptions.Timeout): |
| 2279 | return connectionErrHandler(args.json, "Timeout", None) |
| 2280 | except(requests.exceptions.ConnectionError) as err: |
| 2281 | return connectionErrHandler(args.json, "ConnectionError", err) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2282 | |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 2283 | #wait for the activation to complete, timeout after ~1 hour |
| 2284 | i=0 |
| 2285 | while i < 360: |
| 2286 | url="https://"+host+"/xyz/openbmc_project/software/"+ newversionID |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2287 | data = '{"data":"xyz.openbmc_project.Software.Activation.RequestedActivations.Active"}' |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 2288 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 2289 | resp = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 2290 | except(requests.exceptions.Timeout): |
| 2291 | return connectionErrHandler(args.json, "Timeout", None) |
| 2292 | except(requests.exceptions.ConnectionError) as err: |
| 2293 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 2294 | fwInfo = json.loads(resp.text)['data'] |
| 2295 | if 'Activating' not in fwInfo['Activation'] and 'Activating' not in fwInfo['RequestedActivation']: |
| 2296 | print('') |
| 2297 | break |
| 2298 | else: |
| 2299 | sys.stdout.write('.') |
| 2300 | sys.stdout.flush() |
| 2301 | time.sleep(10) #check every 10 seconds |
| 2302 | return "Firmware flash and activation completed. Please reboot the bmc and then boot the host OS for the changes to take effect. " |
| 2303 | else: |
| 2304 | print("This image has been found on the bmc. Activating image: " + newversionID) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2305 | |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 2306 | d['imageID'] = newversionID |
| 2307 | return activateFWImage(host, args, session) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2308 | |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 2309 | def getFWInventoryAttributes(rawFWInvItem, ID): |
| 2310 | """ |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2311 | gets and lists all of the firmware in the system. |
| 2312 | |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 2313 | @return: returns a dictionary containing the image attributes |
| 2314 | """ |
| 2315 | reqActivation = rawFWInvItem["RequestedActivation"].split('.')[-1] |
| 2316 | pendingActivation = "" |
| 2317 | if reqActivation == "None": |
| 2318 | pendingActivation = "No" |
| 2319 | else: |
| 2320 | pendingActivation = "Yes" |
| 2321 | firmwareAttr = {ID: { |
| 2322 | "Purpose": rawFWInvItem["Purpose"].split('.')[-1], |
| 2323 | "Version": rawFWInvItem["Version"], |
| 2324 | "RequestedActivation": pendingActivation, |
| 2325 | "ID": ID}} |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2326 | |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 2327 | if "ExtendedVersion" in rawFWInvItem: |
| 2328 | firmwareAttr[ID]['ExtendedVersion'] = rawFWInvItem['ExtendedVersion'].split(',') |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2329 | else: |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 2330 | firmwareAttr[ID]['ExtendedVersion'] = "" |
| 2331 | return firmwareAttr |
| 2332 | |
| 2333 | def parseFWdata(firmwareDict): |
| 2334 | """ |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2335 | creates a dictionary with parsed firmware data |
| 2336 | |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 2337 | @return: returns a dictionary containing the image attributes |
| 2338 | """ |
| 2339 | firmwareInfoDict = {"Functional": {}, "Activated":{}, "NeedsActivated":{}} |
| 2340 | for key in firmwareDict['data']: |
| 2341 | #check for valid endpoint |
| 2342 | if "Purpose" in firmwareDict['data'][key]: |
| 2343 | id = key.split('/')[-1] |
| 2344 | if firmwareDict['data'][key]['Activation'].split('.')[-1] == "Active": |
| 2345 | fwActivated = True |
| 2346 | else: |
| 2347 | fwActivated = False |
Justin Thaler | cb68e06 | 2019-03-26 19:04:52 -0500 | [diff] [blame] | 2348 | if 'Priority' in firmwareDict['data'][key]: |
| 2349 | if firmwareDict['data'][key]['Priority'] == 0: |
| 2350 | firmwareInfoDict['Functional'].update(getFWInventoryAttributes(firmwareDict['data'][key], id)) |
| 2351 | elif firmwareDict['data'][key]['Priority'] >= 0 and fwActivated: |
| 2352 | firmwareInfoDict['Activated'].update(getFWInventoryAttributes(firmwareDict['data'][key], id)) |
| 2353 | else: |
| 2354 | firmwareInfoDict['NeedsActivated'].update(getFWInventoryAttributes(firmwareDict['data'][key], id)) |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 2355 | else: |
Justin Thaler | cb68e06 | 2019-03-26 19:04:52 -0500 | [diff] [blame] | 2356 | firmwareInfoDict['NeedsActivated'].update(getFWInventoryAttributes(firmwareDict['data'][key], id)) |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 2357 | emptySections = [] |
| 2358 | for key in firmwareInfoDict: |
| 2359 | if len(firmwareInfoDict[key])<=0: |
| 2360 | emptySections.append(key) |
| 2361 | for key in emptySections: |
| 2362 | del firmwareInfoDict[key] |
| 2363 | return firmwareInfoDict |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2364 | |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 2365 | def displayFWInvenory(firmwareInfoDict, args): |
| 2366 | """ |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2367 | gets and lists all of the firmware in the system. |
| 2368 | |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 2369 | @return: returns a string containing all of the firmware information |
| 2370 | """ |
| 2371 | output = "" |
| 2372 | if not args.json: |
| 2373 | for key in firmwareInfoDict: |
| 2374 | for subkey in firmwareInfoDict[key]: |
| 2375 | firmwareInfoDict[key][subkey]['ExtendedVersion'] = str(firmwareInfoDict[key][subkey]['ExtendedVersion']) |
| 2376 | if not args.verbose: |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2377 | output = "---Running Images---\n" |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 2378 | colNames = ["Purpose", "Version", "ID"] |
| 2379 | keylist = ["Purpose", "Version", "ID"] |
| 2380 | output += tableDisplay(keylist, colNames, firmwareInfoDict["Functional"]) |
| 2381 | if "Activated" in firmwareInfoDict: |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2382 | output += "\n---Available Images---\n" |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 2383 | output += tableDisplay(keylist, colNames, firmwareInfoDict["Activated"]) |
| 2384 | if "NeedsActivated" in firmwareInfoDict: |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2385 | output += "\n---Needs Activated Images---\n" |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 2386 | output += tableDisplay(keylist, colNames, firmwareInfoDict["NeedsActivated"]) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2387 | |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 2388 | else: |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2389 | output = "---Running Images---\n" |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 2390 | colNames = ["Purpose", "Version", "ID", "Pending Activation", "Extended Version"] |
| 2391 | keylist = ["Purpose", "Version", "ID", "RequestedActivation", "ExtendedVersion"] |
| 2392 | output += tableDisplay(keylist, colNames, firmwareInfoDict["Functional"]) |
| 2393 | if "Activated" in firmwareInfoDict: |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2394 | output += "\n---Available Images---\n" |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 2395 | output += tableDisplay(keylist, colNames, firmwareInfoDict["Activated"]) |
| 2396 | if "NeedsActivated" in firmwareInfoDict: |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2397 | output += "\n---Needs Activated Images---\n" |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 2398 | output += tableDisplay(keylist, colNames, firmwareInfoDict["NeedsActivated"]) |
| 2399 | return output |
| 2400 | else: |
| 2401 | return str(json.dumps(firmwareInfoDict, sort_keys=True, indent=4, separators=(',', ': '), ensure_ascii=False)) |
| 2402 | |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2403 | def firmwareList(host, args, session): |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 2404 | """ |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2405 | gets and lists all of the firmware in the system. |
| 2406 | |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 2407 | @return: returns a string containing all of the firmware information |
| 2408 | """ |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 2409 | url="https://{hostname}/xyz/openbmc_project/software/enumerate".format(hostname=host) |
| 2410 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 2411 | res = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 2412 | except(requests.exceptions.Timeout): |
| 2413 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 2414 | firmwareDict = json.loads(res.text) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2415 | |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 2416 | #sort the received information |
| 2417 | firmwareInfoDict = parseFWdata(firmwareDict) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2418 | |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 2419 | #display the information |
| 2420 | return displayFWInvenory(firmwareInfoDict, args) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2421 | |
| 2422 | |
Adriana Kobylak | 5af2fad | 2018-11-08 12:33:43 -0600 | [diff] [blame] | 2423 | def deleteFWVersion(host, args, session): |
| 2424 | """ |
| 2425 | deletes a firmware version on the BMC |
| 2426 | |
| 2427 | @param host: string, the hostname or IP address of the BMC |
| 2428 | @param args: contains additional arguments used by the fwflash sub command |
| 2429 | @param session: the active session to use |
| 2430 | @param fwID: the unique ID of the fw version to delete |
| 2431 | """ |
| 2432 | fwID = args.versionID |
| 2433 | |
| 2434 | print("Deleting version: "+fwID) |
| 2435 | url="https://"+host+"/xyz/openbmc_project/software/"+ fwID + "/action/Delete" |
Adriana Kobylak | 5af2fad | 2018-11-08 12:33:43 -0600 | [diff] [blame] | 2436 | data = "{\"data\": [] }" |
| 2437 | |
| 2438 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 2439 | res = session.post(url, headers=jsonHeader, data=data, verify=False, timeout=baseTimeout) |
Adriana Kobylak | 5af2fad | 2018-11-08 12:33:43 -0600 | [diff] [blame] | 2440 | except(requests.exceptions.Timeout): |
| 2441 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 2442 | if res.status_code == 200: |
| 2443 | return ('The firmware version has been deleted') |
| 2444 | else: |
| 2445 | return ('Unable to delete the specified firmware version') |
| 2446 | |
| 2447 | |
Deepak Kodihalli | 22d4df0 | 2018-09-18 06:52:43 -0500 | [diff] [blame] | 2448 | def restLogging(host, args, session): |
| 2449 | """ |
| 2450 | Called by the logging function. Turns REST API logging on/off. |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2451 | |
Deepak Kodihalli | 22d4df0 | 2018-09-18 06:52:43 -0500 | [diff] [blame] | 2452 | @param host: string, the hostname or IP address of the bmc |
| 2453 | @param args: contains additional arguments used by the logging sub command |
| 2454 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2455 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
Deepak Kodihalli | 22d4df0 | 2018-09-18 06:52:43 -0500 | [diff] [blame] | 2456 | """ |
Deepak Kodihalli | 22d4df0 | 2018-09-18 06:52:43 -0500 | [diff] [blame] | 2457 | url="https://"+host+"/xyz/openbmc_project/logging/rest_api_logs/attr/Enabled" |
Deepak Kodihalli | 22d4df0 | 2018-09-18 06:52:43 -0500 | [diff] [blame] | 2458 | |
| 2459 | if(args.rest_logging == 'on'): |
| 2460 | data = '{"data": 1}' |
| 2461 | elif(args.rest_logging == 'off'): |
| 2462 | data = '{"data": 0}' |
| 2463 | else: |
| 2464 | return "Invalid logging rest_api command" |
| 2465 | |
| 2466 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 2467 | res = session.put(url, headers=jsonHeader, data=data, verify=False, timeout=baseTimeout) |
Deepak Kodihalli | 22d4df0 | 2018-09-18 06:52:43 -0500 | [diff] [blame] | 2468 | except(requests.exceptions.Timeout): |
| 2469 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 2470 | return res.text |
| 2471 | |
| 2472 | |
Deepak Kodihalli | 02d5328 | 2018-09-18 06:53:31 -0500 | [diff] [blame] | 2473 | def remoteLogging(host, args, session): |
| 2474 | """ |
| 2475 | Called by the logging function. View config information for/disable remote logging (rsyslog). |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2476 | |
Deepak Kodihalli | 02d5328 | 2018-09-18 06:53:31 -0500 | [diff] [blame] | 2477 | @param host: string, the hostname or IP address of the bmc |
| 2478 | @param args: contains additional arguments used by the logging sub command |
| 2479 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2480 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
Deepak Kodihalli | 02d5328 | 2018-09-18 06:53:31 -0500 | [diff] [blame] | 2481 | """ |
| 2482 | |
| 2483 | url="https://"+host+"/xyz/openbmc_project/logging/config/remote" |
Deepak Kodihalli | 02d5328 | 2018-09-18 06:53:31 -0500 | [diff] [blame] | 2484 | |
| 2485 | try: |
| 2486 | if(args.remote_logging == 'view'): |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 2487 | res = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
Deepak Kodihalli | 02d5328 | 2018-09-18 06:53:31 -0500 | [diff] [blame] | 2488 | elif(args.remote_logging == 'disable'): |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 2489 | res = session.put(url + '/attr/Port', headers=jsonHeader, json = {"data": 0}, verify=False, timeout=baseTimeout) |
| 2490 | res = session.put(url + '/attr/Address', headers=jsonHeader, json = {"data": ""}, verify=False, timeout=baseTimeout) |
Deepak Kodihalli | 02d5328 | 2018-09-18 06:53:31 -0500 | [diff] [blame] | 2491 | else: |
| 2492 | return "Invalid logging remote_logging command" |
| 2493 | except(requests.exceptions.Timeout): |
| 2494 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 2495 | return res.text |
| 2496 | |
| 2497 | |
| 2498 | def remoteLoggingConfig(host, args, session): |
| 2499 | """ |
| 2500 | Called by the logging function. Configures remote logging (rsyslog). |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2501 | |
Deepak Kodihalli | 02d5328 | 2018-09-18 06:53:31 -0500 | [diff] [blame] | 2502 | @param host: string, the hostname or IP address of the bmc |
| 2503 | @param args: contains additional arguments used by the logging sub command |
| 2504 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2505 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
Deepak Kodihalli | 02d5328 | 2018-09-18 06:53:31 -0500 | [diff] [blame] | 2506 | """ |
| 2507 | |
| 2508 | url="https://"+host+"/xyz/openbmc_project/logging/config/remote" |
Deepak Kodihalli | 02d5328 | 2018-09-18 06:53:31 -0500 | [diff] [blame] | 2509 | |
| 2510 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 2511 | res = session.put(url + '/attr/Port', headers=jsonHeader, json = {"data": args.port}, verify=False, timeout=baseTimeout) |
| 2512 | res = session.put(url + '/attr/Address', headers=jsonHeader, json = {"data": args.address}, verify=False, timeout=baseTimeout) |
Deepak Kodihalli | 02d5328 | 2018-09-18 06:53:31 -0500 | [diff] [blame] | 2513 | except(requests.exceptions.Timeout): |
| 2514 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 2515 | return res.text |
| 2516 | |
Marri Devender Rao | 82590dc | 2019-06-06 04:54:22 -0500 | [diff] [blame] | 2517 | def redfishSupportPresent(host, session): |
| 2518 | url = "https://" + host + "/redfish/v1" |
| 2519 | try: |
| 2520 | resp = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
| 2521 | except(requests.exceptions.Timeout): |
| 2522 | return False |
| 2523 | except(requests.exceptions.ConnectionError) as err: |
| 2524 | return False |
| 2525 | if resp.status_code != 200: |
| 2526 | return False |
| 2527 | else: |
| 2528 | return True |
Ratan Gupta | 9166cd2 | 2018-10-01 18:09:40 +0530 | [diff] [blame] | 2529 | |
Dhruvaraj Subhashchandran | 64e7f6f | 2018-10-02 03:42:14 -0500 | [diff] [blame] | 2530 | def certificateUpdate(host, args, session): |
| 2531 | """ |
| 2532 | Called by certificate management function. update server/client/authority certificates |
| 2533 | Example: |
| 2534 | certificate update server https -f cert.pem |
| 2535 | certificate update authority ldap -f Root-CA.pem |
| 2536 | certificate update client ldap -f cert.pem |
| 2537 | @param host: string, the hostname or IP address of the bmc |
| 2538 | @param args: contains additional arguments used by the certificate update sub command |
| 2539 | @param session: the active session to use |
| 2540 | """ |
Dhruvaraj Subhashchandran | 64e7f6f | 2018-10-02 03:42:14 -0500 | [diff] [blame] | 2541 | httpHeader = {'Content-Type': 'application/octet-stream'} |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 2542 | httpHeader.update(xAuthHeader) |
Dhruvaraj Subhashchandran | 64e7f6f | 2018-10-02 03:42:14 -0500 | [diff] [blame] | 2543 | data = open(args.fileloc, 'rb').read() |
Dhruvaraj Subhashchandran | 64e7f6f | 2018-10-02 03:42:14 -0500 | [diff] [blame] | 2544 | try: |
Marri Devender Rao | 82590dc | 2019-06-06 04:54:22 -0500 | [diff] [blame] | 2545 | if redfishSupportPresent(host, session): |
| 2546 | url = ""; |
| 2547 | if(args.type.lower() == 'server'): |
| 2548 | url = "https://" + host + \ |
| 2549 | "/redfish/v1/Managers/bmc/NetworkProtocol/HTTPS/Certificates" |
| 2550 | elif(args.type.lower() == 'client'): |
| 2551 | url = "https://" + host + \ |
| 2552 | "/redfish/v1/AccountService/LDAP/Certificates" |
| 2553 | elif(args.type.lower() == 'authority'): |
| 2554 | url = "https://" + host + \ |
| 2555 | "/redfish/v1/Managers/bmc/Truststore/Certificates" |
| 2556 | else: |
| 2557 | return "Unsupported certificate type" |
| 2558 | resp = session.post(url, headers=httpHeader, data=data, |
| 2559 | verify=False) |
| 2560 | else: |
| 2561 | url = "https://" + host + "/xyz/openbmc_project/certs/" + \ |
| 2562 | args.type.lower() + "/" + args.service.lower() |
| 2563 | resp = session.put(url, headers=httpHeader, data=data, verify=False) |
Dhruvaraj Subhashchandran | 64e7f6f | 2018-10-02 03:42:14 -0500 | [diff] [blame] | 2564 | except(requests.exceptions.Timeout): |
| 2565 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 2566 | except(requests.exceptions.ConnectionError) as err: |
| 2567 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 2568 | if resp.status_code != 200: |
| 2569 | print(resp.text) |
| 2570 | return "Failed to update the certificate" |
| 2571 | else: |
Marri Devender Rao | 82590dc | 2019-06-06 04:54:22 -0500 | [diff] [blame] | 2572 | print("Update complete.") |
Dhruvaraj Subhashchandran | 64e7f6f | 2018-10-02 03:42:14 -0500 | [diff] [blame] | 2573 | |
| 2574 | def certificateDelete(host, args, session): |
| 2575 | """ |
| 2576 | Called by certificate management function to delete certificate |
| 2577 | Example: |
| 2578 | certificate delete server https |
| 2579 | certificate delete authority ldap |
| 2580 | certificate delete client ldap |
| 2581 | @param host: string, the hostname or IP address of the bmc |
| 2582 | @param args: contains additional arguments used by the certificate delete sub command |
| 2583 | @param session: the active session to use |
| 2584 | """ |
Marri Devender Rao | 77e7868 | 2019-07-17 03:18:35 -0500 | [diff] [blame] | 2585 | if redfishSupportPresent(host, session): |
| 2586 | return "Not supported, please use certificate replace instead"; |
Dhruvaraj Subhashchandran | 64e7f6f | 2018-10-02 03:42:14 -0500 | [diff] [blame] | 2587 | httpHeader = {'Content-Type': 'multipart/form-data'} |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 2588 | httpHeader.update(xAuthHeader) |
Dhruvaraj Subhashchandran | 64e7f6f | 2018-10-02 03:42:14 -0500 | [diff] [blame] | 2589 | url = "https://" + host + "/xyz/openbmc_project/certs/" + args.type.lower() + "/" + args.service.lower() |
| 2590 | print("Deleting certificate url=" + url) |
| 2591 | try: |
| 2592 | resp = session.delete(url, headers=httpHeader) |
| 2593 | except(requests.exceptions.Timeout): |
| 2594 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 2595 | except(requests.exceptions.ConnectionError) as err: |
| 2596 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 2597 | if resp.status_code != 200: |
| 2598 | print(resp.text) |
| 2599 | return "Failed to delete the certificate" |
| 2600 | else: |
Marri Devender Rao | 77e7868 | 2019-07-17 03:18:35 -0500 | [diff] [blame] | 2601 | print("Delete complete.") |
Deepak Kodihalli | 02d5328 | 2018-09-18 06:53:31 -0500 | [diff] [blame] | 2602 | |
Marri Devender Rao | dfe81ad | 2019-07-01 05:38:09 -0500 | [diff] [blame] | 2603 | def certificateReplace(host, args, session): |
| 2604 | """ |
| 2605 | Called by certificate management function. replace server/client/ |
| 2606 | authority certificates |
| 2607 | Example: |
| 2608 | certificate replace server https -f cert.pem |
| 2609 | certificate replace authority ldap -f Root-CA.pem |
| 2610 | certificate replace client ldap -f cert.pem |
| 2611 | @param host: string, the hostname or IP address of the bmc |
| 2612 | @param args: contains additional arguments used by the certificate |
| 2613 | replace sub command |
| 2614 | @param session: the active session to use |
| 2615 | """ |
| 2616 | cert = open(args.fileloc, 'rb').read() |
| 2617 | try: |
| 2618 | if redfishSupportPresent(host, session): |
| 2619 | httpHeader = {'Content-Type': 'application/json'} |
| 2620 | httpHeader.update(xAuthHeader) |
| 2621 | url = ""; |
| 2622 | if(args.type.lower() == 'server'): |
| 2623 | url = "/redfish/v1/Managers/bmc/NetworkProtocol/HTTPS/Certificates/1" |
| 2624 | elif(args.type.lower() == 'client'): |
| 2625 | url = "/redfish/v1/AccountService/LDAP/Certificates/1" |
| 2626 | elif(args.type.lower() == 'authority'): |
| 2627 | url = "/redfish/v1/Managers/bmc/Truststore/Certificates/1" |
| 2628 | replaceUrl = "https://" + host + \ |
| 2629 | "/redfish/v1/CertificateService/Actions/CertificateService.ReplaceCertificate" |
| 2630 | data ={"CertificateUri":{"@odata.id":url}, "CertificateType":"PEM", |
| 2631 | "CertificateString":cert} |
| 2632 | resp = session.post(replaceUrl, headers=httpHeader, json=data, verify=False) |
| 2633 | else: |
| 2634 | httpHeader = {'Content-Type': 'application/octet-stream'} |
| 2635 | httpHeader.update(xAuthHeader) |
| 2636 | url = "https://" + host + "/xyz/openbmc_project/certs/" + \ |
| 2637 | args.type.lower() + "/" + args.service.lower() |
| 2638 | resp = session.delete(url, headers=httpHeader) |
| 2639 | resp = session.put(url, headers=httpHeader, data=cert, verify=False) |
| 2640 | except(requests.exceptions.Timeout): |
| 2641 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 2642 | except(requests.exceptions.ConnectionError) as err: |
| 2643 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 2644 | if resp.status_code != 200: |
| 2645 | print(resp.text) |
| 2646 | return "Failed to replace the certificate" |
| 2647 | else: |
| 2648 | print("Replace complete.") |
| 2649 | return resp.text |
| 2650 | |
Marri Devender Rao | 3464640 | 2019-07-01 05:46:03 -0500 | [diff] [blame] | 2651 | def certificateDisplay(host, args, session): |
| 2652 | """ |
| 2653 | Called by certificate management function. display server/client/ |
| 2654 | authority certificates |
| 2655 | Example: |
| 2656 | certificate display server |
| 2657 | certificate display authority |
| 2658 | certificate display client |
| 2659 | @param host: string, the hostname or IP address of the bmc |
| 2660 | @param args: contains additional arguments used by the certificate |
| 2661 | display sub command |
| 2662 | @param session: the active session to use |
| 2663 | """ |
| 2664 | if not redfishSupportPresent(host, session): |
| 2665 | return "Not supported"; |
| 2666 | |
| 2667 | httpHeader = {'Content-Type': 'application/octet-stream'} |
| 2668 | httpHeader.update(xAuthHeader) |
| 2669 | if(args.type.lower() == 'server'): |
| 2670 | url = "https://" + host + \ |
| 2671 | "/redfish/v1/Managers/bmc/NetworkProtocol/HTTPS/Certificates/1" |
| 2672 | elif(args.type.lower() == 'client'): |
| 2673 | url = "https://" + host + \ |
| 2674 | "/redfish/v1/AccountService/LDAP/Certificates/1" |
| 2675 | elif(args.type.lower() == 'authority'): |
| 2676 | url = "https://" + host + \ |
| 2677 | "/redfish/v1/Managers/bmc/Truststore/Certificates/1" |
| 2678 | try: |
| 2679 | resp = session.get(url, headers=httpHeader, verify=False) |
| 2680 | except(requests.exceptions.Timeout): |
| 2681 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 2682 | except(requests.exceptions.ConnectionError) as err: |
| 2683 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 2684 | if resp.status_code != 200: |
| 2685 | print(resp.text) |
| 2686 | return "Failed to display the certificate" |
| 2687 | else: |
| 2688 | print("Display complete.") |
| 2689 | return resp.text |
| 2690 | |
Marri Devender Rao | a208ff8 | 2019-07-01 05:51:27 -0500 | [diff] [blame] | 2691 | def certificateList(host, args, session): |
| 2692 | """ |
| 2693 | Called by certificate management function. |
| 2694 | Example: |
| 2695 | certificate list |
| 2696 | @param host: string, the hostname or IP address of the bmc |
| 2697 | @param args: contains additional arguments used by the certificate |
| 2698 | list sub command |
| 2699 | @param session: the active session to use |
| 2700 | """ |
| 2701 | if not redfishSupportPresent(host, session): |
| 2702 | return "Not supported"; |
| 2703 | |
| 2704 | httpHeader = {'Content-Type': 'application/octet-stream'} |
| 2705 | httpHeader.update(xAuthHeader) |
| 2706 | url = "https://" + host + \ |
| 2707 | "/redfish/v1/CertificateService/CertificateLocations/" |
| 2708 | try: |
| 2709 | resp = session.get(url, headers=httpHeader, verify=False) |
| 2710 | except(requests.exceptions.Timeout): |
| 2711 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 2712 | except(requests.exceptions.ConnectionError) as err: |
| 2713 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 2714 | if resp.status_code != 200: |
| 2715 | print(resp.text) |
| 2716 | return "Failed to list certificates" |
| 2717 | else: |
| 2718 | print("List certificates complete.") |
| 2719 | return resp.text |
| 2720 | |
Marri Devender Rao | 3cdf8ae | 2019-07-01 06:01:40 -0500 | [diff] [blame] | 2721 | def certificateGenerateCSR(host, args, session): |
| 2722 | """ |
| 2723 | Called by certificate management function. Generate CSR for server/ |
| 2724 | client certificates |
| 2725 | Example: |
Marri Devender Rao | 88064f0 | 2019-08-19 09:00:30 -0500 | [diff] [blame^] | 2726 | certificate generatecsr server NJ w3.ibm.com US IBM IBM-UNIT NY EC 2048 prime256v1 cp abc.com an.com,bm.com gn sn un in |
| 2727 | certificate generatecsr client NJ w3.ibm.com US IBM IBM-UNIT NY EC 2048 prime256v1 cp abc.com an.com,bm.com gn sn un in |
Marri Devender Rao | 3cdf8ae | 2019-07-01 06:01:40 -0500 | [diff] [blame] | 2728 | @param host: string, the hostname or IP address of the bmc |
| 2729 | @param args: contains additional arguments used by the certificate replace sub command |
| 2730 | @param session: the active session to use |
| 2731 | """ |
| 2732 | if not redfishSupportPresent(host, session): |
| 2733 | return "Not supported"; |
| 2734 | |
| 2735 | httpHeader = {'Content-Type': 'application/octet-stream'} |
| 2736 | httpHeader.update(xAuthHeader) |
| 2737 | url = ""; |
| 2738 | if(args.type.lower() == 'server'): |
| 2739 | url = "/redfish/v1/Managers/bmc/NetworkProtocol/HTTPS/Certificates/" |
Marri Devender Rao | 88064f0 | 2019-08-19 09:00:30 -0500 | [diff] [blame^] | 2740 | usage_list = ["ServerAuthentication"] |
Marri Devender Rao | 3cdf8ae | 2019-07-01 06:01:40 -0500 | [diff] [blame] | 2741 | elif(args.type.lower() == 'client'): |
| 2742 | url = "/redfish/v1/AccountService/LDAP/Certificates/" |
Marri Devender Rao | 88064f0 | 2019-08-19 09:00:30 -0500 | [diff] [blame^] | 2743 | usage_list = ["ClientAuthentication"] |
Marri Devender Rao | 3cdf8ae | 2019-07-01 06:01:40 -0500 | [diff] [blame] | 2744 | elif(args.type.lower() == 'authority'): |
| 2745 | url = "/redfish/v1/Managers/bmc/Truststore/Certificates/" |
| 2746 | print("Generating CSR url=" + url) |
| 2747 | generateCSRUrl = "https://" + host + \ |
| 2748 | "/redfish/v1/CertificateService/Actions/CertificateService.GenerateCSR" |
| 2749 | try: |
Marri Devender Rao | 3cdf8ae | 2019-07-01 06:01:40 -0500 | [diff] [blame] | 2750 | alt_name_list = args.alternativeNames.split(",") |
| 2751 | data ={"CertificateCollection":{"@odata.id":url}, |
| 2752 | "CommonName":args.commonName, "City":args.city, |
| 2753 | "Country":args.country, "Organization":args.organization, |
| 2754 | "OrganizationalUnit":args.organizationUnit, "State":args.state, |
| 2755 | "KeyPairAlgorithm":args.keyPairAlgorithm, |
| 2756 | "KeyBitLength":int(args.keyBitLength), "KeyCurveId":args.keyCurveId, |
| 2757 | "AlternativeNames":alt_name_list, "ContactPerson":args.contactPerson, |
| 2758 | "Email":args.email, "GivenName":args.givenname, "Initials":args.initials, |
| 2759 | "KeyUsage":usage_list, "Surname":args.surname, |
| 2760 | "UnstructuredName":args.unstructuredname} |
| 2761 | resp = session.post(generateCSRUrl, headers=httpHeader, |
| 2762 | json=data, verify=False) |
| 2763 | except(requests.exceptions.Timeout): |
| 2764 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 2765 | except(requests.exceptions.ConnectionError) as err: |
| 2766 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 2767 | if resp.status_code != 200: |
| 2768 | print(resp.text) |
| 2769 | return "Failed to generate CSR" |
| 2770 | else: |
| 2771 | print("GenerateCSR complete.") |
| 2772 | return resp.text |
| 2773 | |
Ratan Gupta | 9166cd2 | 2018-10-01 18:09:40 +0530 | [diff] [blame] | 2774 | def enableLDAP(host, args, session): |
| 2775 | """ |
| 2776 | Called by the ldap function. Configures LDAP. |
| 2777 | |
| 2778 | @param host: string, the hostname or IP address of the bmc |
| 2779 | @param args: contains additional arguments used by the ldap subcommand |
| 2780 | @param session: the active session to use |
| 2781 | @param args.json: boolean, if this flag is set to true, the output will |
| 2782 | be provided in json format for programmatic consumption |
| 2783 | """ |
| 2784 | |
| 2785 | url='https://'+host+'/xyz/openbmc_project/user/ldap/action/CreateConfig' |
Ratan Gupta | 9166cd2 | 2018-10-01 18:09:40 +0530 | [diff] [blame] | 2786 | scope = { |
| 2787 | 'sub' : 'xyz.openbmc_project.User.Ldap.Create.SearchScope.sub', |
| 2788 | 'one' : 'xyz.openbmc_project.User.Ldap.Create.SearchScope.one', |
| 2789 | 'base': 'xyz.openbmc_project.User.Ldap.Create.SearchScope.base' |
| 2790 | } |
| 2791 | |
| 2792 | serverType = { |
| 2793 | 'ActiveDirectory' : 'xyz.openbmc_project.User.Ldap.Create.Type.ActiveDirectory', |
| 2794 | 'OpenLDAP' : 'xyz.openbmc_project.User.Ldap.Create.Type.OpenLdap' |
| 2795 | } |
| 2796 | |
| 2797 | data = {"data": [args.uri, args.bindDN, args.baseDN, args.bindPassword, scope[args.scope], serverType[args.serverType]]} |
| 2798 | |
| 2799 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 2800 | res = session.post(url, headers=jsonHeader, json=data, verify=False, timeout=baseTimeout) |
Ratan Gupta | 9166cd2 | 2018-10-01 18:09:40 +0530 | [diff] [blame] | 2801 | except(requests.exceptions.Timeout): |
| 2802 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 2803 | except(requests.exceptions.ConnectionError) as err: |
| 2804 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 2805 | |
| 2806 | return res.text |
| 2807 | |
| 2808 | |
| 2809 | def disableLDAP(host, args, session): |
| 2810 | """ |
| 2811 | Called by the ldap function. Deletes the LDAP Configuration. |
| 2812 | |
| 2813 | @param host: string, the hostname or IP address of the bmc |
| 2814 | @param args: contains additional arguments used by the ldap subcommand |
| 2815 | @param session: the active session to use |
| 2816 | @param args.json: boolean, if this flag is set to true, the output |
| 2817 | will be provided in json format for programmatic consumption |
| 2818 | """ |
| 2819 | |
| 2820 | url='https://'+host+'/xyz/openbmc_project/user/ldap/config/action/delete' |
Ratan Gupta | 9166cd2 | 2018-10-01 18:09:40 +0530 | [diff] [blame] | 2821 | data = {"data": []} |
| 2822 | |
| 2823 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 2824 | res = session.post(url, headers=jsonHeader, json=data, verify=False, timeout=baseTimeout) |
Ratan Gupta | 9166cd2 | 2018-10-01 18:09:40 +0530 | [diff] [blame] | 2825 | except(requests.exceptions.Timeout): |
| 2826 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 2827 | except(requests.exceptions.ConnectionError) as err: |
| 2828 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 2829 | |
| 2830 | return res.text |
| 2831 | |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 2832 | |
| 2833 | def enableDHCP(host, args, session): |
| 2834 | |
| 2835 | """ |
| 2836 | Called by the network function. Enables DHCP. |
| 2837 | |
| 2838 | @param host: string, the hostname or IP address of the bmc |
| 2839 | @param args: contains additional arguments used by the ldap subcommand |
| 2840 | args.json: boolean, if this flag is set to true, the output |
| 2841 | will be provided in json format for programmatic consumption |
| 2842 | @param session: the active session to use |
| 2843 | """ |
| 2844 | |
| 2845 | url = "https://"+host+"/xyz/openbmc_project/network/"+args.Interface+\ |
| 2846 | "/attr/DHCPEnabled" |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 2847 | data = "{\"data\": 1 }" |
| 2848 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 2849 | res = session.put(url, headers=jsonHeader, data=data, verify=False, |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 2850 | timeout=baseTimeout) |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 2851 | |
| 2852 | except(requests.exceptions.Timeout): |
| 2853 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 2854 | except(requests.exceptions.ConnectionError) as err: |
| 2855 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 2856 | if res.status_code == 403: |
| 2857 | return "The specified Interface"+"("+args.Interface+")"+\ |
| 2858 | " doesn't exist" |
| 2859 | |
| 2860 | return res.text |
| 2861 | |
| 2862 | |
| 2863 | def disableDHCP(host, args, session): |
| 2864 | """ |
| 2865 | Called by the network function. Disables DHCP. |
| 2866 | |
| 2867 | @param host: string, the hostname or IP address of the bmc |
| 2868 | @param args: contains additional arguments used by the ldap subcommand |
| 2869 | args.json: boolean, if this flag is set to true, the output |
| 2870 | will be provided in json format for programmatic consumption |
| 2871 | @param session: the active session to use |
| 2872 | """ |
| 2873 | |
| 2874 | url = "https://"+host+"/xyz/openbmc_project/network/"+args.Interface+\ |
| 2875 | "/attr/DHCPEnabled" |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 2876 | data = "{\"data\": 0 }" |
| 2877 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 2878 | res = session.put(url, headers=jsonHeader, data=data, verify=False, |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 2879 | timeout=baseTimeout) |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 2880 | except(requests.exceptions.Timeout): |
| 2881 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 2882 | except(requests.exceptions.ConnectionError) as err: |
| 2883 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 2884 | if res.status_code == 403: |
| 2885 | return "The specified Interface"+"("+args.Interface+")"+\ |
| 2886 | " doesn't exist" |
| 2887 | return res.text |
| 2888 | |
| 2889 | |
| 2890 | def getHostname(host, args, session): |
| 2891 | |
| 2892 | """ |
| 2893 | Called by the network function. Prints out the Hostname. |
| 2894 | |
| 2895 | @param host: string, the hostname or IP address of the bmc |
| 2896 | @param args: contains additional arguments used by the ldap subcommand |
| 2897 | args.json: boolean, if this flag is set to true, the output |
| 2898 | will be provided in json format for programmatic consumption |
| 2899 | @param session: the active session to use |
| 2900 | """ |
| 2901 | |
| 2902 | url = "https://"+host+"/xyz/openbmc_project/network/config/attr/HostName" |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 2903 | |
| 2904 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 2905 | res = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 2906 | except(requests.exceptions.Timeout): |
| 2907 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 2908 | except(requests.exceptions.ConnectionError) as err: |
| 2909 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 2910 | |
| 2911 | return res.text |
| 2912 | |
| 2913 | |
| 2914 | def setHostname(host, args, session): |
| 2915 | """ |
| 2916 | Called by the network function. Sets the Hostname. |
| 2917 | |
| 2918 | @param host: string, the hostname or IP address of the bmc |
| 2919 | @param args: contains additional arguments used by the ldap subcommand |
| 2920 | args.json: boolean, if this flag is set to true, the output |
| 2921 | will be provided in json format for programmatic consumption |
| 2922 | @param session: the active session to use |
| 2923 | """ |
| 2924 | |
| 2925 | url = "https://"+host+"/xyz/openbmc_project/network/config/attr/HostName" |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 2926 | |
| 2927 | data = {"data": args.HostName} |
| 2928 | |
| 2929 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 2930 | res = session.put(url, headers=jsonHeader, json=data, verify=False, |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 2931 | timeout=baseTimeout) |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 2932 | except(requests.exceptions.Timeout): |
| 2933 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 2934 | except(requests.exceptions.ConnectionError) as err: |
| 2935 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 2936 | |
| 2937 | return res.text |
| 2938 | |
| 2939 | |
| 2940 | def getDomainName(host, args, session): |
| 2941 | |
| 2942 | """ |
| 2943 | Called by the network function. Prints out the DomainName. |
| 2944 | |
| 2945 | @param host: string, the hostname or IP address of the bmc |
| 2946 | @param args: contains additional arguments used by the ldap subcommand |
| 2947 | args.json: boolean, if this flag is set to true, the output |
| 2948 | will be provided in json format for programmatic consumption |
| 2949 | @param session: the active session to use |
| 2950 | """ |
| 2951 | |
| 2952 | url = "https://"+host+"/xyz/openbmc_project/network/"+args.Interface+\ |
| 2953 | "/attr/DomainName" |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 2954 | |
| 2955 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 2956 | res = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 2957 | except(requests.exceptions.Timeout): |
| 2958 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 2959 | except(requests.exceptions.ConnectionError) as err: |
| 2960 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 2961 | if res.status_code == 404: |
| 2962 | return "The specified Interface"+"("+args.Interface+")"+\ |
| 2963 | " doesn't exist" |
| 2964 | |
| 2965 | return res.text |
| 2966 | |
| 2967 | |
| 2968 | def setDomainName(host, args, session): |
| 2969 | """ |
| 2970 | Called by the network function. Sets the DomainName. |
| 2971 | |
| 2972 | @param host: string, the hostname or IP address of the bmc |
| 2973 | @param args: contains additional arguments used by the ldap subcommand |
| 2974 | args.json: boolean, if this flag is set to true, the output |
| 2975 | will be provided in json format for programmatic consumption |
| 2976 | @param session: the active session to use |
| 2977 | """ |
| 2978 | |
| 2979 | url = "https://"+host+"/xyz/openbmc_project/network/"+args.Interface+\ |
| 2980 | "/attr/DomainName" |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 2981 | |
| 2982 | data = {"data": args.DomainName.split(",")} |
| 2983 | |
| 2984 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 2985 | res = session.put(url, headers=jsonHeader, json=data, verify=False, |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 2986 | timeout=baseTimeout) |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 2987 | except(requests.exceptions.Timeout): |
| 2988 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 2989 | except(requests.exceptions.ConnectionError) as err: |
| 2990 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 2991 | if res.status_code == 403: |
| 2992 | return "The specified Interface"+"("+args.Interface+")"+\ |
| 2993 | " doesn't exist" |
| 2994 | |
| 2995 | return res.text |
| 2996 | |
| 2997 | |
| 2998 | def getMACAddress(host, args, session): |
| 2999 | |
| 3000 | """ |
| 3001 | Called by the network function. Prints out the MACAddress. |
| 3002 | |
| 3003 | @param host: string, the hostname or IP address of the bmc |
| 3004 | @param args: contains additional arguments used by the ldap subcommand |
| 3005 | args.json: boolean, if this flag is set to true, the output |
| 3006 | will be provided in json format for programmatic consumption |
| 3007 | @param session: the active session to use |
| 3008 | """ |
| 3009 | |
| 3010 | url = "https://"+host+"/xyz/openbmc_project/network/"+args.Interface+\ |
| 3011 | "/attr/MACAddress" |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 3012 | |
| 3013 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 3014 | res = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 3015 | except(requests.exceptions.Timeout): |
| 3016 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3017 | except(requests.exceptions.ConnectionError) as err: |
| 3018 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 3019 | if res.status_code == 404: |
| 3020 | return "The specified Interface"+"("+args.Interface+")"+\ |
| 3021 | " doesn't exist" |
| 3022 | |
| 3023 | return res.text |
| 3024 | |
| 3025 | |
| 3026 | def setMACAddress(host, args, session): |
| 3027 | """ |
| 3028 | Called by the network function. Sets the MACAddress. |
| 3029 | |
| 3030 | @param host: string, the hostname or IP address of the bmc |
| 3031 | @param args: contains additional arguments used by the ldap subcommand |
| 3032 | args.json: boolean, if this flag is set to true, the output |
| 3033 | will be provided in json format for programmatic consumption |
| 3034 | @param session: the active session to use |
| 3035 | """ |
| 3036 | |
| 3037 | url = "https://"+host+"/xyz/openbmc_project/network/"+args.Interface+\ |
| 3038 | "/attr/MACAddress" |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 3039 | |
| 3040 | data = {"data": args.MACAddress} |
| 3041 | |
| 3042 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 3043 | res = session.put(url, headers=jsonHeader, json=data, verify=False, |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 3044 | timeout=baseTimeout) |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 3045 | except(requests.exceptions.Timeout): |
| 3046 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3047 | except(requests.exceptions.ConnectionError) as err: |
| 3048 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 3049 | if res.status_code == 403: |
| 3050 | return "The specified Interface"+"("+args.Interface+")"+\ |
| 3051 | " doesn't exist" |
| 3052 | |
| 3053 | return res.text |
| 3054 | |
| 3055 | |
| 3056 | def getDefaultGateway(host, args, session): |
| 3057 | |
| 3058 | """ |
| 3059 | Called by the network function. Prints out the DefaultGateway. |
| 3060 | |
| 3061 | @param host: string, the hostname or IP address of the bmc |
| 3062 | @param args: contains additional arguments used by the ldap subcommand |
| 3063 | args.json: boolean, if this flag is set to true, the output |
| 3064 | will be provided in json format for programmatic consumption |
| 3065 | @param session: the active session to use |
| 3066 | """ |
| 3067 | |
| 3068 | url = "https://"+host+"/xyz/openbmc_project/network/config/attr/DefaultGateway" |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 3069 | |
| 3070 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 3071 | res = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 3072 | except(requests.exceptions.Timeout): |
| 3073 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3074 | except(requests.exceptions.ConnectionError) as err: |
| 3075 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 3076 | if res.status_code == 404: |
| 3077 | return "Failed to get Default Gateway info!!" |
| 3078 | |
| 3079 | return res.text |
| 3080 | |
| 3081 | |
| 3082 | def setDefaultGateway(host, args, session): |
| 3083 | """ |
| 3084 | Called by the network function. Sets the DefaultGateway. |
| 3085 | |
| 3086 | @param host: string, the hostname or IP address of the bmc |
| 3087 | @param args: contains additional arguments used by the ldap subcommand |
| 3088 | args.json: boolean, if this flag is set to true, the output |
| 3089 | will be provided in json format for programmatic consumption |
| 3090 | @param session: the active session to use |
| 3091 | """ |
| 3092 | |
| 3093 | url = "https://"+host+"/xyz/openbmc_project/network/config/attr/DefaultGateway" |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 3094 | |
| 3095 | data = {"data": args.DefaultGW} |
| 3096 | |
| 3097 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 3098 | res = session.put(url, headers=jsonHeader, json=data, verify=False, |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 3099 | timeout=baseTimeout) |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 3100 | except(requests.exceptions.Timeout): |
| 3101 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3102 | except(requests.exceptions.ConnectionError) as err: |
| 3103 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 3104 | if res.status_code == 403: |
| 3105 | return "Failed to set Default Gateway!!" |
| 3106 | |
| 3107 | return res.text |
| 3108 | |
| 3109 | |
| 3110 | def viewNWConfig(host, args, session): |
| 3111 | """ |
| 3112 | Called by the ldap function. Prints out network configured properties |
| 3113 | |
| 3114 | @param host: string, the hostname or IP address of the bmc |
| 3115 | @param args: contains additional arguments used by the ldap subcommand |
| 3116 | args.json: boolean, if this flag is set to true, the output |
| 3117 | will be provided in json format for programmatic consumption |
| 3118 | @param session: the active session to use |
| 3119 | @return returns LDAP's configured properties. |
| 3120 | """ |
| 3121 | url = "https://"+host+"/xyz/openbmc_project/network/enumerate" |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 3122 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 3123 | res = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 3124 | except(requests.exceptions.Timeout): |
| 3125 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3126 | except(requests.exceptions.ConnectionError) as err: |
| 3127 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 3128 | except(requests.exceptions.RequestException) as err: |
| 3129 | return connectionErrHandler(args.json, "RequestException", err) |
| 3130 | if res.status_code == 404: |
| 3131 | return "LDAP server config has not been created" |
| 3132 | return res.text |
| 3133 | |
| 3134 | |
| 3135 | def getDNS(host, args, session): |
| 3136 | |
| 3137 | """ |
| 3138 | Called by the network function. Prints out DNS servers on the interface |
| 3139 | |
| 3140 | @param host: string, the hostname or IP address of the bmc |
| 3141 | @param args: contains additional arguments used by the ldap subcommand |
| 3142 | args.json: boolean, if this flag is set to true, the output |
| 3143 | will be provided in json format for programmatic consumption |
| 3144 | @param session: the active session to use |
| 3145 | """ |
| 3146 | |
| 3147 | url = "https://" + host + "/xyz/openbmc_project/network/" + args.Interface\ |
| 3148 | + "/attr/Nameservers" |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 3149 | |
| 3150 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 3151 | res = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 3152 | except(requests.exceptions.Timeout): |
| 3153 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3154 | except(requests.exceptions.ConnectionError) as err: |
| 3155 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 3156 | if res.status_code == 404: |
| 3157 | return "The specified Interface"+"("+args.Interface+")" + \ |
| 3158 | " doesn't exist" |
| 3159 | |
| 3160 | return res.text |
| 3161 | |
| 3162 | |
| 3163 | def setDNS(host, args, session): |
| 3164 | """ |
| 3165 | Called by the network function. Sets DNS servers on the interface. |
| 3166 | |
| 3167 | @param host: string, the hostname or IP address of the bmc |
| 3168 | @param args: contains additional arguments used by the ldap subcommand |
| 3169 | args.json: boolean, if this flag is set to true, the output |
| 3170 | will be provided in json format for programmatic consumption |
| 3171 | @param session: the active session to use |
| 3172 | """ |
| 3173 | |
| 3174 | url = "https://" + host + "/xyz/openbmc_project/network/" + args.Interface\ |
| 3175 | + "/attr/Nameservers" |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 3176 | |
| 3177 | data = {"data": args.DNSServers.split(",")} |
| 3178 | |
| 3179 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 3180 | res = session.put(url, headers=jsonHeader, json=data, verify=False, |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 3181 | timeout=baseTimeout) |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 3182 | except(requests.exceptions.Timeout): |
| 3183 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3184 | except(requests.exceptions.ConnectionError) as err: |
| 3185 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 3186 | if res.status_code == 403: |
| 3187 | return "The specified Interface"+"("+args.Interface+")" +\ |
| 3188 | " doesn't exist" |
| 3189 | |
| 3190 | return res.text |
| 3191 | |
| 3192 | |
| 3193 | def getNTP(host, args, session): |
| 3194 | |
| 3195 | """ |
| 3196 | Called by the network function. Prints out NTP servers on the interface |
| 3197 | |
| 3198 | @param host: string, the hostname or IP address of the bmc |
| 3199 | @param args: contains additional arguments used by the ldap subcommand |
| 3200 | args.json: boolean, if this flag is set to true, the output |
| 3201 | will be provided in json format for programmatic consumption |
| 3202 | @param session: the active session to use |
| 3203 | """ |
| 3204 | |
| 3205 | url = "https://" + host + "/xyz/openbmc_project/network/" + args.Interface\ |
| 3206 | + "/attr/NTPServers" |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 3207 | |
| 3208 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 3209 | res = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 3210 | except(requests.exceptions.Timeout): |
| 3211 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3212 | except(requests.exceptions.ConnectionError) as err: |
| 3213 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 3214 | if res.status_code == 404: |
| 3215 | return "The specified Interface"+"("+args.Interface+")" + \ |
| 3216 | " doesn't exist" |
| 3217 | |
| 3218 | return res.text |
| 3219 | |
| 3220 | |
| 3221 | def setNTP(host, args, session): |
| 3222 | """ |
| 3223 | Called by the network function. Sets NTP servers on the interface. |
| 3224 | |
| 3225 | @param host: string, the hostname or IP address of the bmc |
| 3226 | @param args: contains additional arguments used by the ldap subcommand |
| 3227 | args.json: boolean, if this flag is set to true, the output |
| 3228 | will be provided in json format for programmatic consumption |
| 3229 | @param session: the active session to use |
| 3230 | """ |
| 3231 | |
| 3232 | url = "https://" + host + "/xyz/openbmc_project/network/" + args.Interface\ |
| 3233 | + "/attr/NTPServers" |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 3234 | |
| 3235 | data = {"data": args.NTPServers.split(",")} |
| 3236 | |
| 3237 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 3238 | res = session.put(url, headers=jsonHeader, json=data, verify=False, |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 3239 | timeout=baseTimeout) |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 3240 | except(requests.exceptions.Timeout): |
| 3241 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3242 | except(requests.exceptions.ConnectionError) as err: |
| 3243 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 3244 | if res.status_code == 403: |
| 3245 | return "The specified Interface"+"("+args.Interface+")" +\ |
| 3246 | " doesn't exist" |
| 3247 | |
| 3248 | return res.text |
| 3249 | |
| 3250 | |
Nagaraju Goruganti | 97a2060 | 2018-11-16 03:06:08 -0600 | [diff] [blame] | 3251 | def addIP(host, args, session): |
| 3252 | """ |
| 3253 | Called by the network function. Configures IP address on given interface |
| 3254 | |
| 3255 | @param host: string, the hostname or IP address of the bmc |
| 3256 | @param args: contains additional arguments used by the ldap subcommand |
| 3257 | args.json: boolean, if this flag is set to true, the output |
| 3258 | will be provided in json format for programmatic consumption |
| 3259 | @param session: the active session to use |
| 3260 | """ |
| 3261 | |
| 3262 | url = "https://" + host + "/xyz/openbmc_project/network/" + args.Interface\ |
| 3263 | + "/action/IP" |
Nagaraju Goruganti | 97a2060 | 2018-11-16 03:06:08 -0600 | [diff] [blame] | 3264 | protocol = { |
| 3265 | 'ipv4': 'xyz.openbmc_project.Network.IP.Protocol.IPv4', |
| 3266 | 'ipv6': 'xyz.openbmc_project.Network.IP.Protocol.IPv6' |
| 3267 | } |
| 3268 | |
| 3269 | data = {"data": [protocol[args.type], args.address, int(args.prefixLength), |
| 3270 | args.gateway]} |
| 3271 | |
| 3272 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 3273 | res = session.post(url, headers=jsonHeader, json=data, verify=False, |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 3274 | timeout=baseTimeout) |
Nagaraju Goruganti | 97a2060 | 2018-11-16 03:06:08 -0600 | [diff] [blame] | 3275 | except(requests.exceptions.Timeout): |
| 3276 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3277 | except(requests.exceptions.ConnectionError) as err: |
| 3278 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 3279 | if res.status_code == 404: |
| 3280 | return "The specified Interface" + "(" + args.Interface + ")" +\ |
| 3281 | " doesn't exist" |
| 3282 | |
| 3283 | return res.text |
| 3284 | |
| 3285 | |
| 3286 | def getIP(host, args, session): |
| 3287 | """ |
| 3288 | Called by the network function. Prints out IP address of given interface |
| 3289 | |
| 3290 | @param host: string, the hostname or IP address of the bmc |
| 3291 | @param args: contains additional arguments used by the ldap subcommand |
| 3292 | args.json: boolean, if this flag is set to true, the output |
| 3293 | will be provided in json format for programmatic consumption |
| 3294 | @param session: the active session to use |
| 3295 | """ |
| 3296 | |
| 3297 | url = "https://" + host+"/xyz/openbmc_project/network/" + args.Interface +\ |
| 3298 | "/enumerate" |
Nagaraju Goruganti | 97a2060 | 2018-11-16 03:06:08 -0600 | [diff] [blame] | 3299 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 3300 | res = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
Nagaraju Goruganti | 97a2060 | 2018-11-16 03:06:08 -0600 | [diff] [blame] | 3301 | except(requests.exceptions.Timeout): |
| 3302 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3303 | except(requests.exceptions.ConnectionError) as err: |
| 3304 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 3305 | if res.status_code == 404: |
| 3306 | return "The specified Interface" + "(" + args.Interface + ")" +\ |
| 3307 | " doesn't exist" |
| 3308 | |
| 3309 | return res.text |
| 3310 | |
| 3311 | |
| 3312 | def deleteIP(host, args, session): |
| 3313 | """ |
| 3314 | Called by the network function. Deletes the IP address from given Interface |
| 3315 | |
| 3316 | @param host: string, the hostname or IP address of the bmc |
| 3317 | @param args: contains additional arguments used by the ldap subcommand |
| 3318 | @param session: the active session to use |
| 3319 | @param args.json: boolean, if this flag is set to true, the output |
| 3320 | will be provided in json format for programmatic consumption |
| 3321 | """ |
| 3322 | |
| 3323 | url = "https://"+host+"/xyz/openbmc_project/network/" + args.Interface+\ |
| 3324 | "/enumerate" |
Nagaraju Goruganti | 97a2060 | 2018-11-16 03:06:08 -0600 | [diff] [blame] | 3325 | data = {"data": []} |
| 3326 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 3327 | res = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
Nagaraju Goruganti | 97a2060 | 2018-11-16 03:06:08 -0600 | [diff] [blame] | 3328 | except(requests.exceptions.Timeout): |
| 3329 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3330 | except(requests.exceptions.ConnectionError) as err: |
| 3331 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 3332 | if res.status_code == 404: |
| 3333 | return "The specified Interface" + "(" + args.Interface + ")" +\ |
| 3334 | " doesn't exist" |
| 3335 | objDict = json.loads(res.text) |
| 3336 | if not objDict['data']: |
| 3337 | return "No object found for given address on given Interface" |
| 3338 | |
| 3339 | for obj in objDict['data']: |
Sunitha Harish | 0baf637 | 2019-07-31 03:59:03 -0500 | [diff] [blame] | 3340 | try: |
| 3341 | if args.address in objDict['data'][obj]['Address']: |
| 3342 | url = "https://"+host+obj+"/action/Delete" |
| 3343 | try: |
| 3344 | res = session.post(url, headers=jsonHeader, json=data, |
| 3345 | verify=False, timeout=baseTimeout) |
| 3346 | except(requests.exceptions.Timeout): |
| 3347 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3348 | except(requests.exceptions.ConnectionError) as err: |
| 3349 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 3350 | return res.text |
| 3351 | else: |
| 3352 | continue |
| 3353 | except KeyError: |
| 3354 | return "No object found for address " + args.address + \ |
| 3355 | " on Interface(" + args.Interface + ")" |
Nagaraju Goruganti | 97a2060 | 2018-11-16 03:06:08 -0600 | [diff] [blame] | 3356 | |
| 3357 | |
Nagaraju Goruganti | f21d43c | 2018-11-19 10:47:19 -0600 | [diff] [blame] | 3358 | def addVLAN(host, args, session): |
| 3359 | """ |
| 3360 | Called by the network function. Creates VLAN on given interface. |
| 3361 | |
| 3362 | @param host: string, the hostname or IP address of the bmc |
| 3363 | @param args: contains additional arguments used by the ldap subcommand |
| 3364 | args.json: boolean, if this flag is set to true, the output |
| 3365 | will be provided in json format for programmatic consumption |
| 3366 | @param session: the active session to use |
| 3367 | """ |
| 3368 | |
| 3369 | url = "https://" + host+"/xyz/openbmc_project/network/action/VLAN" |
Nagaraju Goruganti | f21d43c | 2018-11-19 10:47:19 -0600 | [diff] [blame] | 3370 | |
Sunitha Harish | 0baf637 | 2019-07-31 03:59:03 -0500 | [diff] [blame] | 3371 | data = {"data": [args.Interface,int(args.Identifier)]} |
Nagaraju Goruganti | f21d43c | 2018-11-19 10:47:19 -0600 | [diff] [blame] | 3372 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 3373 | res = session.post(url, headers=jsonHeader, json=data, verify=False, |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 3374 | timeout=baseTimeout) |
Nagaraju Goruganti | f21d43c | 2018-11-19 10:47:19 -0600 | [diff] [blame] | 3375 | except(requests.exceptions.Timeout): |
| 3376 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3377 | except(requests.exceptions.ConnectionError) as err: |
| 3378 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 3379 | if res.status_code == 400: |
Sunitha Harish | 0baf637 | 2019-07-31 03:59:03 -0500 | [diff] [blame] | 3380 | return "Adding VLAN to interface" + "(" + args.Interface + ")" +\ |
| 3381 | " failed" |
Nagaraju Goruganti | f21d43c | 2018-11-19 10:47:19 -0600 | [diff] [blame] | 3382 | |
| 3383 | return res.text |
| 3384 | |
| 3385 | |
| 3386 | def deleteVLAN(host, args, session): |
| 3387 | """ |
| 3388 | Called by the network function. Creates VLAN on given interface. |
| 3389 | |
| 3390 | @param host: string, the hostname or IP address of the bmc |
| 3391 | @param args: contains additional arguments used by the ldap subcommand |
| 3392 | args.json: boolean, if this flag is set to true, the output |
| 3393 | will be provided in json format for programmatic consumption |
| 3394 | @param session: the active session to use |
| 3395 | """ |
| 3396 | |
Sunitha Harish | 577a503 | 2019-08-08 06:27:40 -0500 | [diff] [blame] | 3397 | url = "https://" + host+"/xyz/openbmc_project/network/"+args.Interface+"/action/Delete" |
Nagaraju Goruganti | f21d43c | 2018-11-19 10:47:19 -0600 | [diff] [blame] | 3398 | data = {"data": []} |
| 3399 | |
| 3400 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 3401 | res = session.post(url, headers=jsonHeader, json=data, verify=False, timeout=baseTimeout) |
Nagaraju Goruganti | f21d43c | 2018-11-19 10:47:19 -0600 | [diff] [blame] | 3402 | except(requests.exceptions.Timeout): |
| 3403 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3404 | except(requests.exceptions.ConnectionError) as err: |
| 3405 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 3406 | if res.status_code == 404: |
Sunitha Harish | 577a503 | 2019-08-08 06:27:40 -0500 | [diff] [blame] | 3407 | return "The specified VLAN"+"("+args.Interface+")" +" doesn't exist" |
Nagaraju Goruganti | f21d43c | 2018-11-19 10:47:19 -0600 | [diff] [blame] | 3408 | |
| 3409 | return res.text |
| 3410 | |
| 3411 | |
| 3412 | def viewDHCPConfig(host, args, session): |
| 3413 | """ |
| 3414 | Called by the network function. Shows DHCP configured Properties. |
| 3415 | |
| 3416 | @param host: string, the hostname or IP address of the bmc |
| 3417 | @param args: contains additional arguments used by the ldap subcommand |
| 3418 | args.json: boolean, if this flag is set to true, the output |
| 3419 | will be provided in json format for programmatic consumption |
| 3420 | @param session: the active session to use |
| 3421 | """ |
| 3422 | |
| 3423 | url="https://"+host+"/xyz/openbmc_project/network/config/dhcp" |
Nagaraju Goruganti | f21d43c | 2018-11-19 10:47:19 -0600 | [diff] [blame] | 3424 | |
| 3425 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 3426 | res = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
Nagaraju Goruganti | f21d43c | 2018-11-19 10:47:19 -0600 | [diff] [blame] | 3427 | except(requests.exceptions.Timeout): |
| 3428 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3429 | except(requests.exceptions.ConnectionError) as err: |
| 3430 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 3431 | |
| 3432 | return res.text |
| 3433 | |
| 3434 | |
| 3435 | def configureDHCP(host, args, session): |
| 3436 | """ |
| 3437 | Called by the network function. Configures/updates DHCP Properties. |
| 3438 | |
| 3439 | @param host: string, the hostname or IP address of the bmc |
| 3440 | @param args: contains additional arguments used by the ldap subcommand |
| 3441 | args.json: boolean, if this flag is set to true, the output |
| 3442 | will be provided in json format for programmatic consumption |
| 3443 | @param session: the active session to use |
| 3444 | """ |
| 3445 | |
Nagaraju Goruganti | f21d43c | 2018-11-19 10:47:19 -0600 | [diff] [blame] | 3446 | |
| 3447 | try: |
| 3448 | url="https://"+host+"/xyz/openbmc_project/network/config/dhcp" |
| 3449 | if(args.DNSEnabled == True): |
| 3450 | data = '{"data": 1}' |
| 3451 | else: |
| 3452 | data = '{"data": 0}' |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 3453 | res = session.put(url + '/attr/DNSEnabled', headers=jsonHeader, |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 3454 | data=data, verify=False, timeout=baseTimeout) |
Nagaraju Goruganti | f21d43c | 2018-11-19 10:47:19 -0600 | [diff] [blame] | 3455 | if(args.HostNameEnabled == True): |
| 3456 | data = '{"data": 1}' |
| 3457 | else: |
| 3458 | data = '{"data": 0}' |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 3459 | res = session.put(url + '/attr/HostNameEnabled', headers=jsonHeader, |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 3460 | data=data, verify=False, timeout=baseTimeout) |
Nagaraju Goruganti | f21d43c | 2018-11-19 10:47:19 -0600 | [diff] [blame] | 3461 | if(args.NTPEnabled == True): |
| 3462 | data = '{"data": 1}' |
| 3463 | else: |
| 3464 | data = '{"data": 0}' |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 3465 | res = session.put(url + '/attr/NTPEnabled', headers=jsonHeader, |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 3466 | data=data, verify=False, timeout=baseTimeout) |
Nagaraju Goruganti | f21d43c | 2018-11-19 10:47:19 -0600 | [diff] [blame] | 3467 | if(args.SendHostNameEnabled == True): |
| 3468 | data = '{"data": 1}' |
| 3469 | else: |
| 3470 | data = '{"data": 0}' |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 3471 | res = session.put(url + '/attr/SendHostNameEnabled', headers=jsonHeader, |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 3472 | data=data, verify=False, timeout=baseTimeout) |
Nagaraju Goruganti | f21d43c | 2018-11-19 10:47:19 -0600 | [diff] [blame] | 3473 | except(requests.exceptions.Timeout): |
| 3474 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3475 | except(requests.exceptions.ConnectionError) as err: |
| 3476 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 3477 | |
| 3478 | return res.text |
| 3479 | |
| 3480 | |
| 3481 | def nwReset(host, args, session): |
| 3482 | |
| 3483 | """ |
| 3484 | Called by the network function. Resets networks setting to factory defaults. |
| 3485 | |
| 3486 | @param host: string, the hostname or IP address of the bmc |
| 3487 | @param args: contains additional arguments used by the ldap subcommand |
| 3488 | args.json: boolean, if this flag is set to true, the output |
| 3489 | will be provided in json format for programmatic consumption |
| 3490 | @param session: the active session to use |
| 3491 | """ |
| 3492 | |
| 3493 | url = "https://"+host+"/xyz/openbmc_project/network/action/Reset" |
Nagaraju Goruganti | f21d43c | 2018-11-19 10:47:19 -0600 | [diff] [blame] | 3494 | data = '{"data":[] }' |
| 3495 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 3496 | res = session.post(url, headers=jsonHeader, data=data, verify=False, |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 3497 | timeout=baseTimeout) |
Nagaraju Goruganti | f21d43c | 2018-11-19 10:47:19 -0600 | [diff] [blame] | 3498 | |
| 3499 | except(requests.exceptions.Timeout): |
| 3500 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3501 | except(requests.exceptions.ConnectionError) as err: |
| 3502 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 3503 | |
| 3504 | return res.text |
| 3505 | |
| 3506 | |
Ratan Gupta | feee637 | 2018-10-17 23:25:51 +0530 | [diff] [blame] | 3507 | def createPrivilegeMapping(host, args, session): |
| 3508 | """ |
| 3509 | Called by the ldap function. Creates the group and the privilege mapping. |
| 3510 | |
| 3511 | @param host: string, the hostname or IP address of the bmc |
| 3512 | @param args: contains additional arguments used by the ldap subcommand |
| 3513 | @param session: the active session to use |
| 3514 | @param args.json: boolean, if this flag is set to true, the output |
| 3515 | will be provided in json format for programmatic consumption |
| 3516 | """ |
| 3517 | |
| 3518 | url = 'https://'+host+'/xyz/openbmc_project/user/ldap/action/Create' |
Ratan Gupta | feee637 | 2018-10-17 23:25:51 +0530 | [diff] [blame] | 3519 | |
| 3520 | data = {"data": [args.groupName,args.privilege]} |
| 3521 | |
| 3522 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 3523 | res = session.post(url, headers=jsonHeader, json = data, verify=False, timeout=baseTimeout) |
Ratan Gupta | feee637 | 2018-10-17 23:25:51 +0530 | [diff] [blame] | 3524 | except(requests.exceptions.Timeout): |
| 3525 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3526 | except(requests.exceptions.ConnectionError) as err: |
| 3527 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 3528 | return res.text |
| 3529 | |
| 3530 | def listPrivilegeMapping(host, args, session): |
| 3531 | """ |
| 3532 | Called by the ldap function. Lists the group and the privilege mapping. |
| 3533 | |
| 3534 | @param host: string, the hostname or IP address of the bmc |
| 3535 | @param args: contains additional arguments used by the ldap subcommand |
| 3536 | @param session: the active session to use |
| 3537 | @param args.json: boolean, if this flag is set to true, the output |
| 3538 | will be provided in json format for programmatic consumption |
| 3539 | """ |
| 3540 | url = 'https://'+host+'/xyz/openbmc_project/user/ldap/enumerate' |
Ratan Gupta | feee637 | 2018-10-17 23:25:51 +0530 | [diff] [blame] | 3541 | data = {"data": []} |
| 3542 | |
| 3543 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 3544 | res = session.get(url, headers=jsonHeader, json = data, verify=False, timeout=baseTimeout) |
Ratan Gupta | feee637 | 2018-10-17 23:25:51 +0530 | [diff] [blame] | 3545 | except(requests.exceptions.Timeout): |
| 3546 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3547 | except(requests.exceptions.ConnectionError) as err: |
| 3548 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 3549 | return res.text |
| 3550 | |
| 3551 | def deletePrivilegeMapping(host, args, session): |
| 3552 | """ |
| 3553 | Called by the ldap function. Deletes the mapping associated with the group. |
| 3554 | |
| 3555 | @param host: string, the hostname or IP address of the bmc |
| 3556 | @param args: contains additional arguments used by the ldap subcommand |
| 3557 | @param session: the active session to use |
| 3558 | @param args.json: boolean, if this flag is set to true, the output |
| 3559 | will be provided in json format for programmatic consumption |
| 3560 | """ |
| 3561 | (ldapNameSpaceObjects) = listPrivilegeMapping(host, args, session) |
| 3562 | ldapNameSpaceObjects = json.loads(ldapNameSpaceObjects)["data"] |
| 3563 | path = '' |
| 3564 | |
| 3565 | # not interested in the config objet |
| 3566 | ldapNameSpaceObjects.pop('/xyz/openbmc_project/user/ldap/config', None) |
| 3567 | |
| 3568 | # search for the object having the mapping for the given group |
| 3569 | for key,value in ldapNameSpaceObjects.items(): |
| 3570 | if value['GroupName'] == args.groupName: |
| 3571 | path = key |
| 3572 | break |
| 3573 | |
| 3574 | if path == '': |
| 3575 | return "No privilege mapping found for this group." |
| 3576 | |
| 3577 | # delete the object |
| 3578 | url = 'https://'+host+path+'/action/delete' |
Ratan Gupta | feee637 | 2018-10-17 23:25:51 +0530 | [diff] [blame] | 3579 | data = {"data": []} |
| 3580 | |
| 3581 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 3582 | res = session.post(url, headers=jsonHeader, json = data, verify=False, timeout=baseTimeout) |
Ratan Gupta | feee637 | 2018-10-17 23:25:51 +0530 | [diff] [blame] | 3583 | except(requests.exceptions.Timeout): |
| 3584 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3585 | except(requests.exceptions.ConnectionError) as err: |
| 3586 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 3587 | return res.text |
Ratan Gupta | 9166cd2 | 2018-10-01 18:09:40 +0530 | [diff] [blame] | 3588 | |
Sivas SRR | 7883527 | 2018-11-27 05:27:19 -0600 | [diff] [blame] | 3589 | def deleteAllPrivilegeMapping(host, args, session): |
| 3590 | """ |
| 3591 | Called by the ldap function. Deletes all the privilege mapping and group defined. |
| 3592 | @param host: string, the hostname or IP address of the bmc |
| 3593 | @param args: contains additional arguments used by the ldap subcommand |
| 3594 | @param session: the active session to use |
| 3595 | @param args.json: boolean, if this flag is set to true, the output |
| 3596 | will be provided in json format for programmatic consumption |
| 3597 | """ |
| 3598 | ldapNameSpaceObjects = listPrivilegeMapping(host, args, session) |
| 3599 | ldapNameSpaceObjects = json.loads(ldapNameSpaceObjects)["data"] |
| 3600 | path = '' |
| 3601 | |
| 3602 | # Remove the config object. |
| 3603 | ldapNameSpaceObjects.pop('/xyz/openbmc_project/user/ldap/config', None) |
Sivas SRR | 7883527 | 2018-11-27 05:27:19 -0600 | [diff] [blame] | 3604 | data = {"data": []} |
| 3605 | |
| 3606 | try: |
| 3607 | # search for GroupName property and delete if it is available. |
| 3608 | for path in ldapNameSpaceObjects.keys(): |
| 3609 | # delete the object |
| 3610 | url = 'https://'+host+path+'/action/delete' |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 3611 | res = session.post(url, headers=jsonHeader, json = data, verify=False, timeout=baseTimeout) |
Sivas SRR | 7883527 | 2018-11-27 05:27:19 -0600 | [diff] [blame] | 3612 | except(requests.exceptions.Timeout): |
| 3613 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3614 | except(requests.exceptions.ConnectionError) as err: |
| 3615 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 3616 | return res.text |
| 3617 | |
Nagaraju Goruganti | 7d1fe17 | 2018-11-13 06:09:29 -0600 | [diff] [blame] | 3618 | def viewLDAPConfig(host, args, session): |
| 3619 | """ |
| 3620 | Called by the ldap function. Prints out LDAP's configured properties |
| 3621 | |
| 3622 | @param host: string, the hostname or IP address of the bmc |
| 3623 | @param args: contains additional arguments used by the ldap subcommand |
| 3624 | args.json: boolean, if this flag is set to true, the output |
| 3625 | will be provided in json format for programmatic consumption |
| 3626 | @param session: the active session to use |
| 3627 | @return returns LDAP's configured properties. |
| 3628 | """ |
| 3629 | url = "https://"+host+"/xyz/openbmc_project/user/ldap/config" |
Nagaraju Goruganti | 7d1fe17 | 2018-11-13 06:09:29 -0600 | [diff] [blame] | 3630 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 3631 | res = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
Nagaraju Goruganti | 7d1fe17 | 2018-11-13 06:09:29 -0600 | [diff] [blame] | 3632 | except(requests.exceptions.Timeout): |
| 3633 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3634 | except(requests.exceptions.ConnectionError) as err: |
| 3635 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 3636 | except(requests.exceptions.RequestException) as err: |
| 3637 | return connectionErrHandler(args.json, "RequestException", err) |
| 3638 | if res.status_code == 404: |
| 3639 | return "LDAP server config has not been created" |
| 3640 | return res.text |
| 3641 | |
Nagaraju Goruganti | f21d43c | 2018-11-19 10:47:19 -0600 | [diff] [blame] | 3642 | def str2bool(v): |
| 3643 | if v.lower() in ('yes', 'true', 't', 'y', '1'): |
| 3644 | return True |
| 3645 | elif v.lower() in ('no', 'false', 'f', 'n', '0'): |
| 3646 | return False |
| 3647 | else: |
| 3648 | raise argparse.ArgumentTypeError('Boolean value expected.') |
Nagaraju Goruganti | 7d1fe17 | 2018-11-13 06:09:29 -0600 | [diff] [blame] | 3649 | |
Matt Spinler | 7d426c2 | 2018-09-24 14:42:07 -0500 | [diff] [blame] | 3650 | def localUsers(host, args, session): |
| 3651 | """ |
| 3652 | Enables and disables local BMC users. |
| 3653 | |
| 3654 | @param host: string, the hostname or IP address of the bmc |
| 3655 | @param args: contains additional arguments used by the logging sub command |
| 3656 | @param session: the active session to use |
| 3657 | """ |
| 3658 | |
Matt Spinler | 7d426c2 | 2018-09-24 14:42:07 -0500 | [diff] [blame] | 3659 | url="https://{hostname}/xyz/openbmc_project/user/enumerate".format(hostname=host) |
| 3660 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 3661 | res = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
Matt Spinler | 7d426c2 | 2018-09-24 14:42:07 -0500 | [diff] [blame] | 3662 | except(requests.exceptions.Timeout): |
| 3663 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3664 | usersDict = json.loads(res.text) |
| 3665 | |
| 3666 | if not usersDict['data']: |
| 3667 | return "No users found" |
| 3668 | |
| 3669 | output = "" |
| 3670 | for user in usersDict['data']: |
Matt Spinler | 015adc2 | 2018-10-23 14:30:19 -0500 | [diff] [blame] | 3671 | |
| 3672 | # Skip LDAP and another non-local users |
| 3673 | if 'UserEnabled' not in usersDict['data'][user]: |
| 3674 | continue |
| 3675 | |
Matt Spinler | 7d426c2 | 2018-09-24 14:42:07 -0500 | [diff] [blame] | 3676 | name = user.split('/')[-1] |
| 3677 | url = "https://{hostname}{user}/attr/UserEnabled".format(hostname=host, user=user) |
| 3678 | |
| 3679 | if args.local_users == "queryenabled": |
| 3680 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 3681 | res = session.get(url, headers=jsonHeader,verify=False, timeout=baseTimeout) |
Matt Spinler | 7d426c2 | 2018-09-24 14:42:07 -0500 | [diff] [blame] | 3682 | except(requests.exceptions.Timeout): |
| 3683 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3684 | |
| 3685 | result = json.loads(res.text) |
| 3686 | output += ("User: {name} Enabled: {result}\n").format(name=name, result=result['data']) |
| 3687 | |
| 3688 | elif args.local_users in ["enableall", "disableall"]: |
| 3689 | action = "" |
| 3690 | if args.local_users == "enableall": |
| 3691 | data = '{"data": true}' |
| 3692 | action = "Enabling" |
| 3693 | else: |
| 3694 | data = '{"data": false}' |
| 3695 | action = "Disabling" |
| 3696 | |
| 3697 | output += "{action} {name}\n".format(action=action, name=name) |
| 3698 | |
| 3699 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 3700 | resp = session.put(url, headers=jsonHeader, data=data, verify=False, timeout=baseTimeout) |
Matt Spinler | 7d426c2 | 2018-09-24 14:42:07 -0500 | [diff] [blame] | 3701 | except(requests.exceptions.Timeout): |
| 3702 | return connectionErrHandler(args.json, "Timeout", None) |
| 3703 | except(requests.exceptions.ConnectionError) as err: |
| 3704 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 3705 | else: |
| 3706 | return "Invalid local users argument" |
| 3707 | |
| 3708 | return output |
| 3709 | |
Marri Devender Rao | 2c2a516 | 2018-11-05 08:57:11 -0600 | [diff] [blame] | 3710 | def setPassword(host, args, session): |
| 3711 | """ |
| 3712 | Set local user password |
| 3713 | @param host: string, the hostname or IP address of the bmc |
| 3714 | @param args: contains additional arguments used by the logging sub |
| 3715 | command |
| 3716 | @param session: the active session to use |
| 3717 | @param args.json: boolean, if this flag is set to true, the output |
| 3718 | will be provided in json format for programmatic consumption |
| 3719 | @return: Session object |
| 3720 | """ |
Marri Devender Rao | 2c2a516 | 2018-11-05 08:57:11 -0600 | [diff] [blame] | 3721 | try: |
Sunitha Harish | c99faba | 2019-07-19 06:55:22 -0500 | [diff] [blame] | 3722 | if(isRedfishSupport): |
| 3723 | url = "https://" + host + "/redfish/v1/AccountService/Accounts/"+ \ |
| 3724 | args.user |
| 3725 | data = {"Password":args.password} |
| 3726 | res = session.patch(url, headers=jsonHeader, json=data, |
| 3727 | verify=False, timeout=baseTimeout) |
| 3728 | else: |
| 3729 | url = "https://" + host + "/xyz/openbmc_project/user/" + args.user + \ |
| 3730 | "/action/SetPassword" |
| 3731 | res = session.post(url, headers=jsonHeader, |
Marri Devender Rao | 2c2a516 | 2018-11-05 08:57:11 -0600 | [diff] [blame] | 3732 | json={"data": [args.password]}, verify=False, |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 3733 | timeout=baseTimeout) |
Marri Devender Rao | 2c2a516 | 2018-11-05 08:57:11 -0600 | [diff] [blame] | 3734 | except(requests.exceptions.Timeout): |
| 3735 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3736 | except(requests.exceptions.ConnectionError) as err: |
| 3737 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 3738 | except(requests.exceptions.RequestException) as err: |
| 3739 | return connectionErrHandler(args.json, "RequestException", err) |
Sunitha Harish | c99faba | 2019-07-19 06:55:22 -0500 | [diff] [blame] | 3740 | return res.status_code |
Matthew Barth | 368e83c | 2019-02-01 13:48:25 -0600 | [diff] [blame] | 3741 | |
| 3742 | def getThermalZones(host, args, session): |
| 3743 | """ |
| 3744 | Get the available thermal control zones |
| 3745 | @param host: string, the hostname or IP address of the bmc |
| 3746 | @param args: contains additional arguments used to get the thermal |
| 3747 | control zones |
| 3748 | @param session: the active session to use |
| 3749 | @return: Session object |
| 3750 | """ |
| 3751 | url = "https://" + host + "/xyz/openbmc_project/control/thermal/enumerate" |
| 3752 | |
| 3753 | try: |
| 3754 | res = session.get(url, headers=jsonHeader, verify=False, timeout=30) |
| 3755 | except(requests.exceptions.Timeout): |
| 3756 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3757 | except(requests.exceptions.ConnectionError) as err: |
| 3758 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 3759 | except(requests.exceptions.RequestException) as err: |
| 3760 | return connectionErrHandler(args.json, "RequestException", err) |
| 3761 | |
| 3762 | if (res.status_code == 404): |
| 3763 | return "No thermal control zones found or system is in a" + \ |
| 3764 | " powered off state" |
| 3765 | |
| 3766 | zonesDict = json.loads(res.text) |
| 3767 | if not zonesDict['data']: |
| 3768 | return "No thermal control zones found" |
| 3769 | for zone in zonesDict['data']: |
| 3770 | z = ",".join(str(zone.split('/')[-1]) for zone in zonesDict['data']) |
| 3771 | |
| 3772 | return "Zones: [ " + z + " ]" |
| 3773 | |
| 3774 | |
| 3775 | def getThermalMode(host, args, session): |
| 3776 | """ |
| 3777 | Get thermal control mode |
| 3778 | @param host: string, the hostname or IP address of the bmc |
| 3779 | @param args: contains additional arguments used to get the thermal |
| 3780 | control mode |
| 3781 | @param session: the active session to use |
| 3782 | @param args.zone: the zone to get the mode on |
| 3783 | @return: Session object |
| 3784 | """ |
| 3785 | url = "https://" + host + "/xyz/openbmc_project/control/thermal/" + \ |
| 3786 | args.zone |
| 3787 | |
| 3788 | try: |
| 3789 | res = session.get(url, headers=jsonHeader, verify=False, timeout=30) |
| 3790 | except(requests.exceptions.Timeout): |
| 3791 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3792 | except(requests.exceptions.ConnectionError) as err: |
| 3793 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 3794 | except(requests.exceptions.RequestException) as err: |
| 3795 | return connectionErrHandler(args.json, "RequestException", err) |
| 3796 | |
| 3797 | if (res.status_code == 404): |
| 3798 | return "Thermal control zone(" + args.zone + ") not found or" + \ |
| 3799 | " system is in a powered off state" |
| 3800 | |
| 3801 | propsDict = json.loads(res.text) |
| 3802 | if not propsDict['data']: |
| 3803 | return "No thermal control properties found on zone(" + args.zone + ")" |
| 3804 | curMode = "Current" |
| 3805 | supModes = "Supported" |
| 3806 | result = "\n" |
| 3807 | for prop in propsDict['data']: |
| 3808 | if (prop.casefold() == curMode.casefold()): |
| 3809 | result += curMode + " Mode: " + propsDict['data'][curMode] + "\n" |
| 3810 | if (prop.casefold() == supModes.casefold()): |
| 3811 | s = ", ".join(str(sup) for sup in propsDict['data'][supModes]) |
| 3812 | result += supModes + " Modes: [ " + s + " ]\n" |
| 3813 | |
| 3814 | return result |
| 3815 | |
| 3816 | def setThermalMode(host, args, session): |
| 3817 | """ |
| 3818 | Set thermal control mode |
| 3819 | @param host: string, the hostname or IP address of the bmc |
| 3820 | @param args: contains additional arguments used for setting the thermal |
| 3821 | control mode |
| 3822 | @param session: the active session to use |
| 3823 | @param args.zone: the zone to set the mode on |
| 3824 | @param args.mode: the mode to enable |
| 3825 | @return: Session object |
| 3826 | """ |
| 3827 | url = "https://" + host + "/xyz/openbmc_project/control/thermal/" + \ |
| 3828 | args.zone + "/attr/Current" |
| 3829 | |
| 3830 | # Check args.mode against supported modes using `getThermalMode` output |
| 3831 | modes = getThermalMode(host, args, session) |
| 3832 | modes = os.linesep.join([m for m in modes.splitlines() if m]) |
| 3833 | modes = modes.replace("\n", ";").strip() |
| 3834 | modesDict = dict(m.split(': ') for m in modes.split(';')) |
| 3835 | sModes = ''.join(s for s in modesDict['Supported Modes'] if s not in '[ ]') |
| 3836 | if args.mode.casefold() not in \ |
| 3837 | (m.casefold() for m in sModes.split(',')) or not args.mode: |
| 3838 | result = ("Unsupported mode('" + args.mode + "') given, " + |
| 3839 | "select a supported mode: \n" + |
| 3840 | getThermalMode(host, args, session)) |
| 3841 | return result |
| 3842 | |
| 3843 | data = '{"data":"' + args.mode + '"}' |
| 3844 | try: |
| 3845 | res = session.get(url, headers=jsonHeader, verify=False, timeout=30) |
| 3846 | except(requests.exceptions.Timeout): |
| 3847 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3848 | except(requests.exceptions.ConnectionError) as err: |
| 3849 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 3850 | except(requests.exceptions.RequestException) as err: |
| 3851 | return connectionErrHandler(args.json, "RequestException", err) |
| 3852 | |
| 3853 | if (data and res.status_code != 404): |
| 3854 | try: |
| 3855 | res = session.put(url, headers=jsonHeader, |
| 3856 | data=data, verify=False, |
| 3857 | timeout=30) |
| 3858 | except(requests.exceptions.Timeout): |
| 3859 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3860 | except(requests.exceptions.ConnectionError) as err: |
| 3861 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 3862 | except(requests.exceptions.RequestException) as err: |
| 3863 | return connectionErrHandler(args.json, "RequestException", err) |
| 3864 | |
| 3865 | if res.status_code == 403: |
| 3866 | return "The specified thermal control zone(" + args.zone + ")" + \ |
| 3867 | " does not exist" |
| 3868 | |
| 3869 | return res.text |
| 3870 | else: |
| 3871 | return "Setting thermal control mode(" + args.mode + ")" + \ |
| 3872 | " not supported or operation not available(system powered off?)" |
| 3873 | |
| 3874 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 3875 | def createCommandParser(): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 3876 | """ |
| 3877 | creates the parser for the command line along with help for each command and subcommand |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 3878 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 3879 | @return: returns the parser for the command line |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 3880 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 3881 | parser = argparse.ArgumentParser(description='Process arguments') |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 3882 | parser.add_argument("-H", "--host", help='A hostname or IP for the BMC') |
| 3883 | parser.add_argument("-U", "--user", help='The username to login with') |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 3884 | group = parser.add_mutually_exclusive_group() |
| 3885 | group.add_argument("-A", "--askpw", action='store_true', help='prompt for password') |
| 3886 | group.add_argument("-P", "--PW", help='Provide the password in-line') |
Joseph Reynolds | a2d54c5 | 2019-06-11 22:02:57 -0500 | [diff] [blame] | 3887 | group.add_argument("-E", "--PWenvvar", action='store_true', help='Get password from envvar OPENBMCTOOL_PASSWORD') |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 3888 | parser.add_argument('-j', '--json', action='store_true', help='output json data only') |
| 3889 | parser.add_argument('-t', '--policyTableLoc', help='The location of the policy table to parse alerts') |
| 3890 | parser.add_argument('-c', '--CerFormat', action='store_true', help=argparse.SUPPRESS) |
| 3891 | parser.add_argument('-T', '--procTime', action='store_true', help= argparse.SUPPRESS) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 3892 | parser.add_argument('-V', '--version', action='store_true', help='Display the version number of the openbmctool') |
| 3893 | subparsers = parser.add_subparsers(title='subcommands', description='valid subcommands',help="sub-command help", dest='command') |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 3894 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 3895 | #fru command |
| 3896 | parser_inv = subparsers.add_parser("fru", help='Work with platform inventory') |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 3897 | inv_subparser = parser_inv.add_subparsers(title='subcommands', description='valid inventory actions', help="valid inventory actions", dest='command') |
Justin Thaler | 53bf2f1 | 2018-07-16 14:05:32 -0500 | [diff] [blame] | 3898 | inv_subparser.required = True |
| 3899 | #fru print |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 3900 | inv_print = inv_subparser.add_parser("print", help="prints out a list of all FRUs") |
| 3901 | inv_print.set_defaults(func=fruPrint) |
| 3902 | #fru list [0....n] |
| 3903 | inv_list = inv_subparser.add_parser("list", help="print out details on selected FRUs. Specifying no items will list the entire inventory") |
| 3904 | inv_list.add_argument('items', nargs='?', help="print out details on selected FRUs. Specifying no items will list the entire inventory") |
| 3905 | inv_list.set_defaults(func=fruList) |
| 3906 | #fru status |
| 3907 | inv_status = inv_subparser.add_parser("status", help="prints out the status of all FRUs") |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 3908 | inv_status.add_argument('-v', '--verbose', action='store_true', help='Verbose output') |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 3909 | inv_status.set_defaults(func=fruStatus) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 3910 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 3911 | #sensors command |
| 3912 | parser_sens = subparsers.add_parser("sensors", help="Work with platform sensors") |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 3913 | sens_subparser=parser_sens.add_subparsers(title='subcommands', description='valid sensor actions', help='valid sensor actions', dest='command') |
Justin Thaler | 53bf2f1 | 2018-07-16 14:05:32 -0500 | [diff] [blame] | 3914 | sens_subparser.required = True |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 3915 | #sensor print |
| 3916 | sens_print= sens_subparser.add_parser('print', help="prints out a list of all Sensors.") |
| 3917 | sens_print.set_defaults(func=sensor) |
| 3918 | #sensor list[0...n] |
| 3919 | sens_list=sens_subparser.add_parser("list", help="Lists all Sensors in the platform. Specify a sensor for full details. ") |
| 3920 | sens_list.add_argument("sensNum", nargs='?', help="The Sensor number to get full details on" ) |
| 3921 | sens_list.set_defaults(func=sensor) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 3922 | |
Matthew Barth | 368e83c | 2019-02-01 13:48:25 -0600 | [diff] [blame] | 3923 | #thermal control commands |
| 3924 | parser_therm = subparsers.add_parser("thermal", help="Work with thermal control parameters") |
| 3925 | therm_subparser=parser_therm.add_subparsers(title='subcommands', description='Thermal control actions to work with', help='Valid thermal control actions to work with', dest='command') |
| 3926 | #thermal control zones |
| 3927 | parser_thermZones = therm_subparser.add_parser("zones", help="Get a list of available thermal control zones") |
| 3928 | parser_thermZones.set_defaults(func=getThermalZones) |
| 3929 | #thermal control modes |
| 3930 | parser_thermMode = therm_subparser.add_parser("modes", help="Work with thermal control modes") |
| 3931 | thermMode_sub = parser_thermMode.add_subparsers(title='subactions', description='Work with thermal control modes', help="Work with thermal control modes") |
| 3932 | #get thermal control mode |
| 3933 | parser_getThermMode = thermMode_sub.add_parser("get", help="Get current and supported thermal control modes") |
| 3934 | parser_getThermMode.add_argument('-z', '--zone', required=True, help='Thermal zone to work with') |
| 3935 | parser_getThermMode.set_defaults(func=getThermalMode) |
| 3936 | #set thermal control mode |
| 3937 | parser_setThermMode = thermMode_sub.add_parser("set", help="Set the thermal control mode") |
| 3938 | parser_setThermMode.add_argument('-z', '--zone', required=True, help='Thermal zone to work with') |
| 3939 | parser_setThermMode.add_argument('-m', '--mode', required=True, help='The supported thermal control mode') |
| 3940 | parser_setThermMode.set_defaults(func=setThermalMode) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 3941 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 3942 | #sel command |
| 3943 | parser_sel = subparsers.add_parser("sel", help="Work with platform alerts") |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 3944 | sel_subparser = parser_sel.add_subparsers(title='subcommands', description='valid SEL actions', help = 'valid SEL actions', dest='command') |
Justin Thaler | 53bf2f1 | 2018-07-16 14:05:32 -0500 | [diff] [blame] | 3945 | sel_subparser.required = True |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 3946 | #sel print |
| 3947 | sel_print = sel_subparser.add_parser("print", help="prints out a list of all sels in a condensed list") |
| 3948 | sel_print.add_argument('-d', '--devdebug', action='store_true', help=argparse.SUPPRESS) |
| 3949 | sel_print.add_argument('-v', '--verbose', action='store_true', help="Changes the output to being very verbose") |
| 3950 | sel_print.add_argument('-f', '--fileloc', help='Parse a file instead of the BMC output') |
| 3951 | sel_print.set_defaults(func=selPrint) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 3952 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 3953 | #sel list |
| 3954 | sel_list = sel_subparser.add_parser("list", help="Lists all SELs in the platform. Specifying a specific number will pull all the details for that individual SEL") |
| 3955 | sel_list.add_argument("selNum", nargs='?', type=int, help="The SEL entry to get details on") |
| 3956 | sel_list.set_defaults(func=selList) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 3957 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 3958 | sel_get = sel_subparser.add_parser("get", help="Gets the verbose details of a specified SEL entry") |
| 3959 | sel_get.add_argument('selNum', type=int, help="the number of the SEL entry to get") |
| 3960 | sel_get.set_defaults(func=selList) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 3961 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 3962 | sel_clear = sel_subparser.add_parser("clear", help="Clears all entries from the SEL") |
| 3963 | sel_clear.set_defaults(func=selClear) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 3964 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 3965 | sel_setResolved = sel_subparser.add_parser("resolve", help="Sets the sel entry to resolved") |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 3966 | sel_setResolved.add_argument('-n', '--selNum', type=int, help="the number of the SEL entry to resolve") |
| 3967 | sel_ResolveAll_sub = sel_setResolved.add_subparsers(title='subcommands', description='valid subcommands',help="sub-command help", dest='command') |
| 3968 | sel_ResolveAll = sel_ResolveAll_sub.add_parser('all', help='Resolve all SEL entries') |
| 3969 | sel_ResolveAll.set_defaults(func=selResolveAll) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 3970 | sel_setResolved.set_defaults(func=selSetResolved) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 3971 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 3972 | parser_chassis = subparsers.add_parser("chassis", help="Work with chassis power and status") |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 3973 | chas_sub = parser_chassis.add_subparsers(title='subcommands', description='valid subcommands',help="sub-command help", dest='command') |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 3974 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 3975 | parser_chassis.add_argument('status', action='store_true', help='Returns the current status of the platform') |
| 3976 | parser_chassis.set_defaults(func=chassis) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 3977 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 3978 | parser_chasPower = chas_sub.add_parser("power", help="Turn the chassis on or off, check the power state") |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 3979 | parser_chasPower.add_argument('powcmd', choices=['on','softoff', 'hardoff', 'status'], help='The value for the power command. on, off, or status') |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 3980 | parser_chasPower.set_defaults(func=chassisPower) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 3981 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 3982 | #control the chassis identify led |
| 3983 | parser_chasIdent = chas_sub.add_parser("identify", help="Control the chassis identify led") |
| 3984 | parser_chasIdent.add_argument('identcmd', choices=['on', 'off', 'status'], help='The control option for the led: on, off, blink, status') |
| 3985 | parser_chasIdent.set_defaults(func=chassisIdent) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 3986 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 3987 | #collect service data |
| 3988 | parser_servData = subparsers.add_parser("collect_service_data", help="Collect all bmc data needed for service") |
| 3989 | parser_servData.add_argument('-d', '--devdebug', action='store_true', help=argparse.SUPPRESS) |
| 3990 | parser_servData.set_defaults(func=collectServiceData) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 3991 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 3992 | #system quick health check |
| 3993 | parser_healthChk = subparsers.add_parser("health_check", help="Work with platform sensors") |
| 3994 | parser_healthChk.set_defaults(func=healthCheck) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 3995 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 3996 | #work with bmc dumps |
| 3997 | parser_bmcdump = subparsers.add_parser("dump", help="Work with bmc dump files") |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 3998 | bmcDump_sub = parser_bmcdump.add_subparsers(title='subcommands', description='valid subcommands',help="sub-command help", dest='command') |
Justin Thaler | 53bf2f1 | 2018-07-16 14:05:32 -0500 | [diff] [blame] | 3999 | bmcDump_sub.required = True |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4000 | dump_Create = bmcDump_sub.add_parser('create', help="Create a bmc dump") |
| 4001 | dump_Create.set_defaults(func=bmcDumpCreate) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 4002 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4003 | dump_list = bmcDump_sub.add_parser('list', help="list all bmc dump files") |
| 4004 | dump_list.set_defaults(func=bmcDumpList) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 4005 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4006 | parserdumpdelete = bmcDump_sub.add_parser('delete', help="Delete bmc dump files") |
| 4007 | parserdumpdelete.add_argument("-n", "--dumpNum", nargs='*', type=int, help="The Dump entry to delete") |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4008 | parserdumpdelete.set_defaults(func=bmcDumpDelete) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 4009 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 4010 | bmcDumpDelsub = parserdumpdelete.add_subparsers(title='subcommands', description='valid subcommands',help="sub-command help", dest='command') |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4011 | deleteAllDumps = bmcDumpDelsub.add_parser('all', help='Delete all bmc dump files') |
| 4012 | deleteAllDumps.set_defaults(func=bmcDumpDeleteAll) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 4013 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4014 | parser_dumpretrieve = bmcDump_sub.add_parser('retrieve', help='Retrieve a dump file') |
| 4015 | parser_dumpretrieve.add_argument("dumpNum", type=int, help="The Dump entry to delete") |
| 4016 | parser_dumpretrieve.add_argument("-s", "--dumpSaveLoc", help="The location to save the bmc dump file") |
| 4017 | parser_dumpretrieve.set_defaults(func=bmcDumpRetrieve) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 4018 | |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 4019 | #bmc command for reseting the bmc |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4020 | parser_bmc = subparsers.add_parser('bmc', help="Work with the bmc") |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 4021 | bmc_sub = parser_bmc.add_subparsers(title='subcommands', description='valid subcommands',help="sub-command help", dest='command') |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4022 | parser_BMCReset = bmc_sub.add_parser('reset', help='Reset the bmc' ) |
| 4023 | parser_BMCReset.add_argument('type', choices=['warm','cold'], help="Warm: Reboot the BMC, Cold: CLEAR config and reboot bmc") |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4024 | parser_bmc.add_argument('info', action='store_true', help="Displays information about the BMC hardware, including device revision, firmware revision, IPMI version supported, manufacturer ID, and information on additional device support.") |
| 4025 | parser_bmc.set_defaults(func=bmc) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 4026 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4027 | #add alias to the bmc command |
| 4028 | parser_mc = subparsers.add_parser('mc', help="Work with the management controller") |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 4029 | mc_sub = parser_mc.add_subparsers(title='subcommands', description='valid subcommands',help="sub-command help", dest='command') |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4030 | parser_MCReset = mc_sub.add_parser('reset', help='Reset the bmc' ) |
| 4031 | parser_MCReset.add_argument('type', choices=['warm','cold'], help="Reboot the BMC") |
| 4032 | #parser_MCReset.add_argument('cold', action='store_true', help="Reboot the BMC and CLEAR the configuration") |
| 4033 | parser_mc.add_argument('info', action='store_true', help="Displays information about the BMC hardware, including device revision, firmware revision, IPMI version supported, manufacturer ID, and information on additional device support.") |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 4034 | parser_MCReset.set_defaults(func=bmcReset) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4035 | parser_mc.set_defaults(func=bmc) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 4036 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 4037 | #gard clear |
| 4038 | parser_gc = subparsers.add_parser("gardclear", help="Used to clear gard records") |
| 4039 | parser_gc.set_defaults(func=gardClear) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 4040 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 4041 | #firmware_flash |
| 4042 | parser_fw = subparsers.add_parser("firmware", help="Work with the system firmware") |
| 4043 | fwflash_subproc = parser_fw.add_subparsers(title='subcommands', description='valid firmware commands', help='sub-command help', dest='command') |
Justin Thaler | 53bf2f1 | 2018-07-16 14:05:32 -0500 | [diff] [blame] | 4044 | fwflash_subproc.required = True |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 4045 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 4046 | fwflash = fwflash_subproc.add_parser('flash', help="Flash the system firmware") |
| 4047 | fwflash.add_argument('type', choices=['bmc', 'pnor'], help="image type to flash") |
| 4048 | fwflash.add_argument('-f', '--fileloc', required=True, help="The absolute path to the firmware image") |
| 4049 | fwflash.set_defaults(func=fwFlash) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 4050 | |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 4051 | fwActivate = fwflash_subproc.add_parser('activate', help="Activate existing image on the bmc") |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 4052 | fwActivate.add_argument('imageID', help="The image ID to activate from the firmware list. Ex: 63c95399") |
| 4053 | fwActivate.set_defaults(func=activateFWImage) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 4054 | |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 4055 | fwActivateStatus = fwflash_subproc.add_parser('activation_status', help="Check Status of activations") |
| 4056 | fwActivateStatus.set_defaults(func=activateStatus) |
| 4057 | |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 4058 | fwList = fwflash_subproc.add_parser('list', help="List all of the installed firmware") |
| 4059 | fwList.add_argument('-v', '--verbose', action='store_true', help='Verbose output') |
| 4060 | fwList.set_defaults(func=firmwareList) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 4061 | |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 4062 | fwprint = fwflash_subproc.add_parser('print', help="List all of the installed firmware") |
| 4063 | fwprint.add_argument('-v', '--verbose', action='store_true', help='Verbose output') |
| 4064 | fwprint.set_defaults(func=firmwareList) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 4065 | |
Adriana Kobylak | 5af2fad | 2018-11-08 12:33:43 -0600 | [diff] [blame] | 4066 | fwDelete = fwflash_subproc.add_parser('delete', help="Delete an existing firmware version") |
| 4067 | fwDelete.add_argument('versionID', help="The version ID to delete from the firmware list. Ex: 63c95399") |
| 4068 | fwDelete.set_defaults(func=deleteFWVersion) |
| 4069 | |
Deepak Kodihalli | 22d4df0 | 2018-09-18 06:52:43 -0500 | [diff] [blame] | 4070 | #logging |
| 4071 | parser_logging = subparsers.add_parser("logging", help="logging controls") |
| 4072 | logging_sub = parser_logging.add_subparsers(title='subcommands', description='valid subcommands',help="sub-command help", dest='command') |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 4073 | |
Deepak Kodihalli | 22d4df0 | 2018-09-18 06:52:43 -0500 | [diff] [blame] | 4074 | #turn rest api logging on/off |
| 4075 | parser_rest_logging = logging_sub.add_parser("rest_api", help="turn rest api logging on/off") |
| 4076 | parser_rest_logging.add_argument('rest_logging', choices=['on', 'off'], help='The control option for rest logging: on, off') |
| 4077 | parser_rest_logging.set_defaults(func=restLogging) |
Deepak Kodihalli | 02d5328 | 2018-09-18 06:53:31 -0500 | [diff] [blame] | 4078 | |
| 4079 | #remote logging |
| 4080 | parser_remote_logging = logging_sub.add_parser("remote_logging", help="Remote logging (rsyslog) commands") |
| 4081 | parser_remote_logging.add_argument('remote_logging', choices=['view', 'disable'], help='Remote logging (rsyslog) commands') |
| 4082 | parser_remote_logging.set_defaults(func=remoteLogging) |
| 4083 | |
| 4084 | #configure remote logging |
| 4085 | parser_remote_logging_config = logging_sub.add_parser("remote_logging_config", help="Configure remote logging (rsyslog)") |
| 4086 | parser_remote_logging_config.add_argument("-a", "--address", required=True, help="Set IP address of rsyslog server") |
| 4087 | parser_remote_logging_config.add_argument("-p", "--port", required=True, type=int, help="Set Port of rsyslog server") |
| 4088 | parser_remote_logging_config.set_defaults(func=remoteLoggingConfig) |
Dhruvaraj Subhashchandran | 64e7f6f | 2018-10-02 03:42:14 -0500 | [diff] [blame] | 4089 | |
| 4090 | #certificate management |
| 4091 | parser_cert = subparsers.add_parser("certificate", help="Certificate management") |
| 4092 | certMgmt_subproc = parser_cert.add_subparsers(title='subcommands', description='valid certificate commands', help='sub-command help', dest='command') |
| 4093 | |
| 4094 | certUpdate = certMgmt_subproc.add_parser('update', help="Update the certificate") |
| 4095 | certUpdate.add_argument('type', choices=['server', 'client', 'authority'], help="certificate type to update") |
| 4096 | certUpdate.add_argument('service', choices=['https', 'ldap'], help="Service to update") |
| 4097 | certUpdate.add_argument('-f', '--fileloc', required=True, help="The absolute path to the certificate file") |
| 4098 | certUpdate.set_defaults(func=certificateUpdate) |
| 4099 | |
| 4100 | certDelete = certMgmt_subproc.add_parser('delete', help="Delete the certificate") |
| 4101 | certDelete.add_argument('type', choices=['server', 'client', 'authority'], help="certificate type to delete") |
| 4102 | certDelete.add_argument('service', choices=['https', 'ldap'], help="Service to delete the certificate") |
| 4103 | certDelete.set_defaults(func=certificateDelete) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 4104 | |
Marri Devender Rao | dfe81ad | 2019-07-01 05:38:09 -0500 | [diff] [blame] | 4105 | certReplace = certMgmt_subproc.add_parser('replace', |
| 4106 | help="Replace the certificate") |
| 4107 | certReplace.add_argument('type', choices=['server', 'client', 'authority'], |
| 4108 | help="certificate type to replace") |
| 4109 | certReplace.add_argument('service', choices=['https', 'ldap'], |
| 4110 | help="Service to replace the certificate") |
| 4111 | certReplace.add_argument('-f', '--fileloc', required=True, |
| 4112 | help="The absolute path to the certificate file") |
| 4113 | certReplace.set_defaults(func=certificateReplace) |
| 4114 | |
Marri Devender Rao | 3464640 | 2019-07-01 05:46:03 -0500 | [diff] [blame] | 4115 | certDisplay = certMgmt_subproc.add_parser('display', |
| 4116 | help="Print the certificate") |
| 4117 | certDisplay.add_argument('type', choices=['server', 'client', 'authority'], |
| 4118 | help="certificate type to display") |
| 4119 | certDisplay.set_defaults(func=certificateDisplay) |
| 4120 | |
Marri Devender Rao | a208ff8 | 2019-07-01 05:51:27 -0500 | [diff] [blame] | 4121 | certList = certMgmt_subproc.add_parser('list', |
| 4122 | help="Certificate list") |
| 4123 | certList.set_defaults(func=certificateList) |
| 4124 | |
Marri Devender Rao | 3cdf8ae | 2019-07-01 06:01:40 -0500 | [diff] [blame] | 4125 | certGenerateCSR = certMgmt_subproc.add_parser('generatecsr', help="Generate CSR") |
| 4126 | certGenerateCSR.add_argument('type', choices=['server', 'client', 'authority'], |
| 4127 | help="Generate CSR") |
| 4128 | certGenerateCSR.add_argument('city', |
| 4129 | help="The city or locality of the organization making the request") |
| 4130 | certGenerateCSR.add_argument('commonName', |
| 4131 | help="The fully qualified domain name of the component that is being secured.") |
| 4132 | certGenerateCSR.add_argument('country', |
| 4133 | help="The country of the organization making the request") |
| 4134 | certGenerateCSR.add_argument('organization', |
| 4135 | help="The name of the organization making the request.") |
| 4136 | certGenerateCSR.add_argument('organizationUnit', |
| 4137 | help="The name of the unit or division of the organization making the request.") |
| 4138 | certGenerateCSR.add_argument('state', |
| 4139 | help="The state, province, or region of the organization making the request.") |
| 4140 | certGenerateCSR.add_argument('keyPairAlgorithm', choices=['RSA', 'EC'], |
| 4141 | help="The type of key pair for use with signing algorithms.") |
| 4142 | certGenerateCSR.add_argument('keyBitLength', choices=['2048'], |
| 4143 | help="The length of the key in bits, if needed based on the value of the 'KeyPairAlgorithm' parameter.") |
| 4144 | certGenerateCSR.add_argument('keyCurveId', |
| 4145 | help="The curve ID to be used with the key, if needed based on the value of the 'KeyPairAlgorithm' parameter.") |
| 4146 | certGenerateCSR.add_argument('contactPerson', |
| 4147 | help="The name of the user making the request") |
| 4148 | certGenerateCSR.add_argument('email', |
| 4149 | help="The email address of the contact within the organization") |
| 4150 | certGenerateCSR.add_argument('alternativeNames', |
| 4151 | help="Additional hostnames of the component that is being secured") |
| 4152 | certGenerateCSR.add_argument('givenname', |
| 4153 | help="The given name of the user making the request") |
| 4154 | certGenerateCSR.add_argument('surname', |
| 4155 | help="The surname of the user making the request") |
| 4156 | certGenerateCSR.add_argument('unstructuredname', |
| 4157 | help="he unstructured name of the subject") |
| 4158 | certGenerateCSR.add_argument('initials', |
| 4159 | help="The initials of the user making the request") |
Marri Devender Rao | 3cdf8ae | 2019-07-01 06:01:40 -0500 | [diff] [blame] | 4160 | certGenerateCSR.set_defaults(func=certificateGenerateCSR) |
| 4161 | |
Matt Spinler | 7d426c2 | 2018-09-24 14:42:07 -0500 | [diff] [blame] | 4162 | # local users |
| 4163 | parser_users = subparsers.add_parser("local_users", help="Work with local users") |
| 4164 | parser_users.add_argument('local_users', choices=['disableall','enableall', 'queryenabled'], help="Disable, enable or query local user accounts") |
| 4165 | parser_users.add_argument('-v', '--verbose', action='store_true', help='Verbose output') |
| 4166 | parser_users.set_defaults(func=localUsers) |
| 4167 | |
Ratan Gupta | 9166cd2 | 2018-10-01 18:09:40 +0530 | [diff] [blame] | 4168 | #LDAP |
| 4169 | parser_ldap = subparsers.add_parser("ldap", help="LDAP controls") |
| 4170 | ldap_sub = parser_ldap.add_subparsers(title='subcommands', description='valid subcommands',help="sub-command help", dest='command') |
| 4171 | |
| 4172 | #configure and enable LDAP |
| 4173 | parser_ldap_config = ldap_sub.add_parser("enable", help="Configure and enables the LDAP") |
| 4174 | parser_ldap_config.add_argument("-a", "--uri", required=True, help="Set LDAP server URI") |
| 4175 | parser_ldap_config.add_argument("-B", "--bindDN", required=True, help="Set the bind DN of the LDAP server") |
| 4176 | parser_ldap_config.add_argument("-b", "--baseDN", required=True, help="Set the base DN of the LDAP server") |
| 4177 | parser_ldap_config.add_argument("-p", "--bindPassword", required=True, help="Set the bind password of the LDAP server") |
| 4178 | parser_ldap_config.add_argument("-S", "--scope", choices=['sub','one', 'base'], |
| 4179 | help='Specifies the search scope:subtree, one level or base object.') |
| 4180 | parser_ldap_config.add_argument("-t", "--serverType", choices=['ActiveDirectory','OpenLDAP'], |
| 4181 | help='Specifies the configured server is ActiveDirectory(AD) or OpenLdap') |
| 4182 | parser_ldap_config.set_defaults(func=enableLDAP) |
| 4183 | |
| 4184 | # disable LDAP |
| 4185 | parser_disable_ldap = ldap_sub.add_parser("disable", help="disables the LDAP") |
| 4186 | parser_disable_ldap.set_defaults(func=disableLDAP) |
Nagaraju Goruganti | 7d1fe17 | 2018-11-13 06:09:29 -0600 | [diff] [blame] | 4187 | # view-config |
| 4188 | parser_ldap_config = \ |
| 4189 | ldap_sub.add_parser("view-config", help="prints out a list of all \ |
| 4190 | LDAPS's configured properties") |
| 4191 | parser_ldap_config.set_defaults(func=viewLDAPConfig) |
Ratan Gupta | 9166cd2 | 2018-10-01 18:09:40 +0530 | [diff] [blame] | 4192 | |
Ratan Gupta | feee637 | 2018-10-17 23:25:51 +0530 | [diff] [blame] | 4193 | #create group privilege mapping |
| 4194 | parser_ldap_mapper = ldap_sub.add_parser("privilege-mapper", help="LDAP group privilege controls") |
| 4195 | parser_ldap_mapper_sub = parser_ldap_mapper.add_subparsers(title='subcommands', description='valid subcommands', |
| 4196 | help="sub-command help", dest='command') |
| 4197 | |
| 4198 | parser_ldap_mapper_create = parser_ldap_mapper_sub.add_parser("create", help="Create mapping of ldap group and privilege") |
| 4199 | parser_ldap_mapper_create.add_argument("-g","--groupName",required=True,help="Group Name") |
| 4200 | parser_ldap_mapper_create.add_argument("-p","--privilege",choices=['priv-admin','priv-user'],required=True,help="Privilege") |
| 4201 | parser_ldap_mapper_create.set_defaults(func=createPrivilegeMapping) |
| 4202 | |
| 4203 | #list group privilege mapping |
| 4204 | parser_ldap_mapper_list = parser_ldap_mapper_sub.add_parser("list",help="List privilege mapping") |
| 4205 | parser_ldap_mapper_list.set_defaults(func=listPrivilegeMapping) |
| 4206 | |
| 4207 | #delete group privilege mapping |
| 4208 | parser_ldap_mapper_delete = parser_ldap_mapper_sub.add_parser("delete",help="Delete privilege mapping") |
| 4209 | parser_ldap_mapper_delete.add_argument("-g","--groupName",required=True,help="Group Name") |
| 4210 | parser_ldap_mapper_delete.set_defaults(func=deletePrivilegeMapping) |
| 4211 | |
Sivas SRR | 7883527 | 2018-11-27 05:27:19 -0600 | [diff] [blame] | 4212 | #deleteAll group privilege mapping |
| 4213 | parser_ldap_mapper_delete = parser_ldap_mapper_sub.add_parser("purge",help="Delete All privilege mapping") |
| 4214 | parser_ldap_mapper_delete.set_defaults(func=deleteAllPrivilegeMapping) |
| 4215 | |
Marri Devender Rao | 2c2a516 | 2018-11-05 08:57:11 -0600 | [diff] [blame] | 4216 | # set local user password |
| 4217 | parser_set_password = subparsers.add_parser("set_password", |
| 4218 | help="Set password of local user") |
| 4219 | parser_set_password.add_argument( "-p", "--password", required=True, |
| 4220 | help="Password of local user") |
| 4221 | parser_set_password.set_defaults(func=setPassword) |
| 4222 | |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 4223 | # network |
| 4224 | parser_nw = subparsers.add_parser("network", help="network controls") |
| 4225 | nw_sub = parser_nw.add_subparsers(title='subcommands', |
| 4226 | description='valid subcommands', |
| 4227 | help="sub-command help", |
| 4228 | dest='command') |
| 4229 | |
| 4230 | # enable DHCP |
| 4231 | parser_enable_dhcp = nw_sub.add_parser("enableDHCP", |
| 4232 | help="enables the DHCP on given " |
| 4233 | "Interface") |
| 4234 | parser_enable_dhcp.add_argument("-I", "--Interface", required=True, |
Nagaraju Goruganti | 97a2060 | 2018-11-16 03:06:08 -0600 | [diff] [blame] | 4235 | help="Name of the ethernet interface(it can" |
| 4236 | "be obtained by the " |
| 4237 | "command:network view-config)" |
| 4238 | "Ex: eth0 or eth1 or VLAN(VLAN=eth0_50 etc)") |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 4239 | parser_enable_dhcp.set_defaults(func=enableDHCP) |
| 4240 | |
| 4241 | # disable DHCP |
| 4242 | parser_disable_dhcp = nw_sub.add_parser("disableDHCP", |
| 4243 | help="disables the DHCP on given " |
| 4244 | "Interface") |
| 4245 | parser_disable_dhcp.add_argument("-I", "--Interface", required=True, |
Nagaraju Goruganti | 97a2060 | 2018-11-16 03:06:08 -0600 | [diff] [blame] | 4246 | help="Name of the ethernet interface(it can" |
| 4247 | "be obtained by the " |
| 4248 | "command:network view-config)" |
| 4249 | "Ex: eth0 or eth1 or VLAN(VLAN=eth0_50 etc)") |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 4250 | parser_disable_dhcp.set_defaults(func=disableDHCP) |
| 4251 | |
| 4252 | # get HostName |
| 4253 | parser_gethostname = nw_sub.add_parser("getHostName", |
| 4254 | help="prints out HostName") |
| 4255 | parser_gethostname.set_defaults(func=getHostname) |
| 4256 | |
| 4257 | # set HostName |
| 4258 | parser_sethostname = nw_sub.add_parser("setHostName", help="sets HostName") |
| 4259 | parser_sethostname.add_argument("-H", "--HostName", required=True, |
| 4260 | help="A HostName for the BMC") |
| 4261 | parser_sethostname.set_defaults(func=setHostname) |
| 4262 | |
| 4263 | # get domainname |
| 4264 | parser_getdomainname = nw_sub.add_parser("getDomainName", |
| 4265 | help="prints out DomainName of " |
| 4266 | "given Interface") |
| 4267 | parser_getdomainname.add_argument("-I", "--Interface", required=True, |
Nagaraju Goruganti | 97a2060 | 2018-11-16 03:06:08 -0600 | [diff] [blame] | 4268 | help="Name of the ethernet interface(it " |
| 4269 | "can be obtained by the " |
| 4270 | "command:network view-config)" |
| 4271 | "Ex: eth0 or eth1 or VLAN(VLAN=eth0_50 etc)") |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 4272 | parser_getdomainname.set_defaults(func=getDomainName) |
| 4273 | |
| 4274 | # set domainname |
| 4275 | parser_setdomainname = nw_sub.add_parser("setDomainName", |
| 4276 | help="sets DomainName of given " |
| 4277 | "Interface") |
| 4278 | parser_setdomainname.add_argument("-D", "--DomainName", required=True, |
| 4279 | help="Ex: DomainName=Domain1,Domain2,...") |
| 4280 | parser_setdomainname.add_argument("-I", "--Interface", required=True, |
Nagaraju Goruganti | 97a2060 | 2018-11-16 03:06:08 -0600 | [diff] [blame] | 4281 | help="Name of the ethernet interface(it " |
| 4282 | "can be obtained by the " |
| 4283 | "command:network view-config)" |
| 4284 | "Ex: eth0 or eth1 or VLAN(VLAN=eth0_50 etc)") |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 4285 | parser_setdomainname.set_defaults(func=setDomainName) |
| 4286 | |
| 4287 | # get MACAddress |
| 4288 | parser_getmacaddress = nw_sub.add_parser("getMACAddress", |
| 4289 | help="prints out MACAddress the " |
| 4290 | "given Interface") |
| 4291 | parser_getmacaddress.add_argument("-I", "--Interface", required=True, |
Nagaraju Goruganti | 97a2060 | 2018-11-16 03:06:08 -0600 | [diff] [blame] | 4292 | help="Name of the ethernet interface(it " |
| 4293 | "can be obtained by the " |
| 4294 | "command:network view-config)" |
| 4295 | "Ex: eth0 or eth1 or VLAN(VLAN=eth0_50 etc)") |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 4296 | parser_getmacaddress.set_defaults(func=getMACAddress) |
| 4297 | |
| 4298 | # set MACAddress |
| 4299 | parser_setmacaddress = nw_sub.add_parser("setMACAddress", |
| 4300 | help="sets MACAddress") |
| 4301 | parser_setmacaddress.add_argument("-MA", "--MACAddress", required=True, |
| 4302 | help="A MACAddress for the given " |
| 4303 | "Interface") |
| 4304 | parser_setmacaddress.add_argument("-I", "--Interface", required=True, |
Nagaraju Goruganti | 97a2060 | 2018-11-16 03:06:08 -0600 | [diff] [blame] | 4305 | help="Name of the ethernet interface(it can" |
| 4306 | "be obtained by the " |
| 4307 | "command:network view-config)" |
| 4308 | "Ex: eth0 or eth1 or VLAN(VLAN=eth0_50 etc)") |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 4309 | parser_setmacaddress.set_defaults(func=setMACAddress) |
| 4310 | |
| 4311 | # get DefaultGW |
| 4312 | parser_getdefaultgw = nw_sub.add_parser("getDefaultGW", |
| 4313 | help="prints out DefaultGateway " |
| 4314 | "the BMC") |
| 4315 | parser_getdefaultgw.set_defaults(func=getDefaultGateway) |
| 4316 | |
| 4317 | # set DefaultGW |
| 4318 | parser_setdefaultgw = nw_sub.add_parser("setDefaultGW", |
| 4319 | help="sets DefaultGW") |
| 4320 | parser_setdefaultgw.add_argument("-GW", "--DefaultGW", required=True, |
| 4321 | help="A DefaultGateway for the BMC") |
| 4322 | parser_setdefaultgw.set_defaults(func=setDefaultGateway) |
| 4323 | |
| 4324 | # view network Config |
| 4325 | parser_ldap_config = nw_sub.add_parser("view-config", help="prints out a " |
| 4326 | "list of all network's configured " |
| 4327 | "properties") |
| 4328 | parser_ldap_config.set_defaults(func=viewNWConfig) |
| 4329 | |
| 4330 | # get DNS |
| 4331 | parser_getDNS = nw_sub.add_parser("getDNS", |
| 4332 | help="prints out DNS servers on the " |
| 4333 | "given interface") |
| 4334 | parser_getDNS.add_argument("-I", "--Interface", required=True, |
Nagaraju Goruganti | 97a2060 | 2018-11-16 03:06:08 -0600 | [diff] [blame] | 4335 | help="Name of the ethernet interface(it can" |
| 4336 | "be obtained by the " |
| 4337 | "command:network view-config)" |
| 4338 | "Ex: eth0 or eth1 or VLAN(VLAN=eth0_50 etc)") |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 4339 | parser_getDNS.set_defaults(func=getDNS) |
| 4340 | |
| 4341 | # set DNS |
| 4342 | parser_setDNS = nw_sub.add_parser("setDNS", |
| 4343 | help="sets DNS servers on the given " |
| 4344 | "interface") |
| 4345 | parser_setDNS.add_argument("-d", "--DNSServers", required=True, |
| 4346 | help="Ex: DNSSERVERS=DNS1,DNS2,...") |
| 4347 | parser_setDNS.add_argument("-I", "--Interface", required=True, |
Nagaraju Goruganti | 97a2060 | 2018-11-16 03:06:08 -0600 | [diff] [blame] | 4348 | help="Name of the ethernet interface(it can" |
| 4349 | "be obtained by the " |
| 4350 | "command:network view-config)" |
| 4351 | "Ex: eth0 or eth1 or VLAN(VLAN=eth0_50 etc)") |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 4352 | parser_setDNS.set_defaults(func=setDNS) |
| 4353 | |
| 4354 | # get NTP |
| 4355 | parser_getNTP = nw_sub.add_parser("getNTP", |
| 4356 | help="prints out NTP servers on the " |
| 4357 | "given interface") |
| 4358 | parser_getNTP.add_argument("-I", "--Interface", required=True, |
Nagaraju Goruganti | 97a2060 | 2018-11-16 03:06:08 -0600 | [diff] [blame] | 4359 | help="Name of the ethernet interface(it can" |
| 4360 | "be obtained by the " |
| 4361 | "command:network view-config)" |
| 4362 | "Ex: eth0 or eth1 or VLAN(VLAN=eth0_50 etc)") |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 4363 | parser_getNTP.set_defaults(func=getNTP) |
| 4364 | |
| 4365 | # set NTP |
| 4366 | parser_setNTP = nw_sub.add_parser("setNTP", |
| 4367 | help="sets NTP servers on the given " |
| 4368 | "interface") |
| 4369 | parser_setNTP.add_argument("-N", "--NTPServers", required=True, |
| 4370 | help="Ex: NTPSERVERS=NTP1,NTP2,...") |
| 4371 | parser_setNTP.add_argument("-I", "--Interface", required=True, |
Nagaraju Goruganti | 97a2060 | 2018-11-16 03:06:08 -0600 | [diff] [blame] | 4372 | help="Name of the ethernet interface(it can" |
| 4373 | "be obtained by the " |
| 4374 | "command:network view-config)" |
| 4375 | "Ex: eth0 or eth1 or VLAN(VLAN=eth0_50 etc)") |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 4376 | parser_setNTP.set_defaults(func=setNTP) |
| 4377 | |
Nagaraju Goruganti | 97a2060 | 2018-11-16 03:06:08 -0600 | [diff] [blame] | 4378 | # configure IP |
| 4379 | parser_ip_config = nw_sub.add_parser("addIP", help="Sets IP address to" |
| 4380 | "given interface") |
| 4381 | parser_ip_config.add_argument("-a", "--address", required=True, |
| 4382 | help="IP address of given interface") |
| 4383 | parser_ip_config.add_argument("-gw", "--gateway", required=False, default='', |
| 4384 | help="The gateway for given interface") |
| 4385 | parser_ip_config.add_argument("-l", "--prefixLength", required=True, |
| 4386 | help="The prefixLength of IP address") |
Sunitha Harish | 0baf637 | 2019-07-31 03:59:03 -0500 | [diff] [blame] | 4387 | parser_ip_config.add_argument("-p", "--type", required=True, |
| 4388 | choices=['ipv4', 'ipv6'], |
Nagaraju Goruganti | 97a2060 | 2018-11-16 03:06:08 -0600 | [diff] [blame] | 4389 | help="The protocol type of the given" |
| 4390 | "IP address") |
| 4391 | parser_ip_config.add_argument("-I", "--Interface", required=True, |
| 4392 | help="Name of the ethernet interface(it can" |
| 4393 | "be obtained by the " |
| 4394 | "command:network view-config)" |
| 4395 | "Ex: eth0 or eth1 or VLAN(VLAN=eth0_50 etc)") |
| 4396 | parser_ip_config.set_defaults(func=addIP) |
| 4397 | |
| 4398 | # getIP |
| 4399 | parser_getIP = nw_sub.add_parser("getIP", help="prints out IP address" |
| 4400 | "of given interface") |
| 4401 | parser_getIP.add_argument("-I", "--Interface", required=True, |
| 4402 | help="Name of the ethernet interface(it can" |
| 4403 | "be obtained by the command:network view-config)" |
| 4404 | "Ex: eth0 or eth1 or VLAN(VLAN=eth0_50 etc)") |
| 4405 | parser_getIP.set_defaults(func=getIP) |
| 4406 | |
| 4407 | # rmIP |
| 4408 | parser_rmIP = nw_sub.add_parser("rmIP", help="deletes IP address" |
| 4409 | "of given interface") |
| 4410 | parser_rmIP.add_argument("-a", "--address", required=True, |
| 4411 | help="IP address to remove form given Interface") |
| 4412 | parser_rmIP.add_argument("-I", "--Interface", required=True, |
| 4413 | help="Name of the ethernet interface(it can" |
| 4414 | "be obtained by the command:network view-config)" |
| 4415 | "Ex: eth0 or eth1 or VLAN(VLAN=eth0_50 etc)") |
| 4416 | parser_rmIP.set_defaults(func=deleteIP) |
| 4417 | |
Nagaraju Goruganti | f21d43c | 2018-11-19 10:47:19 -0600 | [diff] [blame] | 4418 | # add VLAN |
| 4419 | parser_create_vlan = nw_sub.add_parser("addVLAN", help="enables VLAN " |
| 4420 | "on given interface with given " |
| 4421 | "VLAN Identifier") |
| 4422 | parser_create_vlan.add_argument("-I", "--Interface", required=True, |
| 4423 | choices=['eth0', 'eth1'], |
| 4424 | help="Name of the ethernet interface") |
| 4425 | parser_create_vlan.add_argument("-n", "--Identifier", required=True, |
| 4426 | help="VLAN Identifier") |
| 4427 | parser_create_vlan.set_defaults(func=addVLAN) |
| 4428 | |
| 4429 | # delete VLAN |
| 4430 | parser_delete_vlan = nw_sub.add_parser("deleteVLAN", help="disables VLAN " |
| 4431 | "on given interface with given " |
| 4432 | "VLAN Identifier") |
| 4433 | parser_delete_vlan.add_argument("-I", "--Interface", required=True, |
| 4434 | help="Name of the ethernet interface(it can" |
| 4435 | "be obtained by the " |
| 4436 | "command:network view-config)" |
| 4437 | "Ex: eth0 or eth1 or VLAN(VLAN=eth0_50 etc)") |
| 4438 | parser_delete_vlan.set_defaults(func=deleteVLAN) |
| 4439 | |
| 4440 | # viewDHCPConfig |
| 4441 | parser_viewDHCPConfig = nw_sub.add_parser("viewDHCPConfig", |
| 4442 | help="Shows DHCP configured " |
| 4443 | "Properties") |
| 4444 | parser_viewDHCPConfig.set_defaults(func=viewDHCPConfig) |
| 4445 | |
| 4446 | # configureDHCP |
| 4447 | parser_configDHCP = nw_sub.add_parser("configureDHCP", |
| 4448 | help="Configures/updates DHCP " |
| 4449 | "Properties") |
| 4450 | parser_configDHCP.add_argument("-d", "--DNSEnabled", type=str2bool, |
| 4451 | required=True, help="Sets DNSEnabled property") |
| 4452 | parser_configDHCP.add_argument("-n", "--HostNameEnabled", type=str2bool, |
| 4453 | required=True, |
| 4454 | help="Sets HostNameEnabled property") |
| 4455 | parser_configDHCP.add_argument("-t", "--NTPEnabled", type=str2bool, |
| 4456 | required=True, |
| 4457 | help="Sets NTPEnabled property") |
| 4458 | parser_configDHCP.add_argument("-s", "--SendHostNameEnabled", type=str2bool, |
| 4459 | required=True, |
| 4460 | help="Sets SendHostNameEnabled property") |
| 4461 | parser_configDHCP.set_defaults(func=configureDHCP) |
| 4462 | |
| 4463 | # network factory reset |
| 4464 | parser_nw_reset = nw_sub.add_parser("nwReset", |
| 4465 | help="Resets networks setting to " |
| 4466 | "factory defaults. " |
| 4467 | "note:Reset settings will be applied " |
| 4468 | "after BMC reboot") |
| 4469 | parser_nw_reset.set_defaults(func=nwReset) |
| 4470 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4471 | return parser |
| 4472 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4473 | def main(argv=None): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 4474 | """ |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 4475 | main function for running the command line utility as a sub application |
| 4476 | """ |
| 4477 | global toolVersion |
Marri Devender Rao | 82590dc | 2019-06-06 04:54:22 -0500 | [diff] [blame] | 4478 | toolVersion = "1.15" |
Sunitha Harish | c99faba | 2019-07-19 06:55:22 -0500 | [diff] [blame] | 4479 | global isRedfishSupport |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4480 | parser = createCommandParser() |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4481 | args = parser.parse_args(argv) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 4482 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4483 | totTimeStart = int(round(time.time()*1000)) |
| 4484 | |
| 4485 | if(sys.version_info < (3,0)): |
| 4486 | urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) |
| 4487 | if sys.version_info >= (3,0): |
| 4488 | requests.packages.urllib3.disable_warnings(requests.packages.urllib3.exceptions.InsecureRequestWarning) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 4489 | if (args.version): |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 4490 | print("Version: "+ toolVersion) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 4491 | sys.exit(0) |
| 4492 | if (hasattr(args, 'fileloc') and args.fileloc is not None and 'print' in args.command): |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4493 | mysess = None |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 4494 | print(selPrint('N/A', args, mysess)) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 4495 | else: |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 4496 | if(hasattr(args, 'host') and hasattr(args,'user')): |
| 4497 | if (args.askpw): |
| 4498 | pw = getpass.getpass() |
| 4499 | elif(args.PW is not None): |
| 4500 | pw = args.PW |
Joseph Reynolds | a2d54c5 | 2019-06-11 22:02:57 -0500 | [diff] [blame] | 4501 | elif(args.PWenvvar): |
| 4502 | pw = os.environ['OPENBMCTOOL_PASSWORD'] |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 4503 | else: |
| 4504 | print("You must specify a password") |
| 4505 | sys.exit() |
| 4506 | logintimeStart = int(round(time.time()*1000)) |
| 4507 | mysess = login(args.host, args.user, pw, args.json) |
Sunitha Harish | 336cda2 | 2019-07-23 02:02:52 -0500 | [diff] [blame] | 4508 | if(mysess == None): |
| 4509 | print("Login Failed!") |
| 4510 | sys.exit() |
Justin Thaler | a9415b4 | 2018-05-25 19:40:13 -0500 | [diff] [blame] | 4511 | if(sys.version_info < (3,0)): |
| 4512 | if isinstance(mysess, basestring): |
| 4513 | print(mysess) |
| 4514 | sys.exit(1) |
| 4515 | elif sys.version_info >= (3,0): |
| 4516 | if isinstance(mysess, str): |
| 4517 | print(mysess) |
| 4518 | sys.exit(1) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 4519 | logintimeStop = int(round(time.time()*1000)) |
Sunitha Harish | c99faba | 2019-07-19 06:55:22 -0500 | [diff] [blame] | 4520 | isRedfishSupport = redfishSupportPresent(args.host,mysess) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 4521 | commandTimeStart = int(round(time.time()*1000)) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 4522 | output = args.func(args.host, args, mysess) |
| 4523 | commandTimeStop = int(round(time.time()*1000)) |
Justin Thaler | 761484a | 2019-03-26 19:20:23 -0500 | [diff] [blame] | 4524 | if isinstance(output, dict): |
| 4525 | print(json.dumps(output, sort_keys=True, indent=4, separators=(',', ': '), ensure_ascii=False)) |
| 4526 | else: |
| 4527 | print(output) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 4528 | if (mysess is not None): |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 4529 | logout(args.host, args.user, pw, mysess, args.json) |
| 4530 | if(args.procTime): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 4531 | print("Total time: " + str(int(round(time.time()*1000))- totTimeStart)) |
| 4532 | print("loginTime: " + str(logintimeStop - logintimeStart)) |
| 4533 | print("command Time: " + str(commandTimeStop - commandTimeStart)) |
| 4534 | else: |
Joseph Reynolds | a2d54c5 | 2019-06-11 22:02:57 -0500 | [diff] [blame] | 4535 | print("usage:\n" |
| 4536 | " OPENBMCTOOL_PASSWORD=secret # if using -E\n" |
| 4537 | " openbmctool.py [-h] -H HOST -U USER {-A | -P PW | -E} [-j]\n" + |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 4538 | "\t[-t POLICYTABLELOC] [-V]\n" + |
Deepak Kodihalli | 22d4df0 | 2018-09-18 06:52:43 -0500 | [diff] [blame] | 4539 | "\t{fru,sensors,sel,chassis,collect_service_data, \ |
| 4540 | health_check,dump,bmc,mc,gardclear,firmware,logging}\n" + |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 4541 | "\t...\n" + |
| 4542 | "openbmctool.py: error: the following arguments are required: -H/--host, -U/--user") |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 4543 | sys.exit() |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4544 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4545 | if __name__ == '__main__': |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 4546 | """ |
| 4547 | main function when called from the command line |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 4548 | |
| 4549 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4550 | import sys |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 4551 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4552 | isTTY = sys.stdout.isatty() |
| 4553 | assert sys.version_info >= (2,7) |
| 4554 | main() |