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