Justin Thaler | b8807ce | 2018-05-25 19:16:20 -0500 | [diff] [blame] | 1 | #!/usr/bin/python3 |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2 | """ |
Joseph Reynolds | a2d54c5 | 2019-06-11 22:02:57 -0500 | [diff] [blame] | 3 | Copyright 2017,2019 IBM Corporation |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 5 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | you may not use this file except in compliance with the License. |
| 7 | You may obtain a copy of the License at |
| 8 | |
| 9 | http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | |
| 11 | Unless required by applicable law or agreed to in writing, software |
| 12 | distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | See the License for the specific language governing permissions and |
| 15 | limitations under the License. |
| 16 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 17 | import argparse |
| 18 | import requests |
| 19 | import getpass |
| 20 | import json |
| 21 | import os |
| 22 | import urllib3 |
| 23 | import time, datetime |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 24 | import binascii |
| 25 | import subprocess |
| 26 | import platform |
| 27 | import zipfile |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 28 | import tarfile |
| 29 | import tempfile |
| 30 | import hashlib |
Justin Thaler | a6b5df7 | 2018-07-16 11:10:07 -0500 | [diff] [blame] | 31 | import re |
Justin Thaler | 24d4efa | 2018-11-08 22:48:10 -0600 | [diff] [blame] | 32 | import uuid |
Ravi Teja | d8be0b4 | 2020-03-18 14:31:46 -0500 | [diff] [blame] | 33 | import ssl |
| 34 | import socket |
| 35 | import select |
| 36 | import http.client |
| 37 | from subprocess import check_output |
Justin Thaler | b425667 | 2020-04-07 19:38:26 -0500 | [diff] [blame] | 38 | import traceback |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 39 | |
Ravi Teja | d8be0b4 | 2020-03-18 14:31:46 -0500 | [diff] [blame] | 40 | |
| 41 | MAX_NBD_PACKET_SIZE = 131088 |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 42 | jsonHeader = {'Content-Type' : 'application/json'} |
| 43 | xAuthHeader = {} |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 44 | baseTimeout = 60 |
RAJESWARAN THILLAIGOVINDAN | 87f087b | 2019-05-08 04:15:26 -0500 | [diff] [blame] | 45 | serverTypeMap = { |
| 46 | 'ActiveDirectory' : 'active_directory', |
| 47 | 'OpenLDAP' : 'openldap' |
| 48 | } |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 49 | |
Ravi Teja | d8be0b4 | 2020-03-18 14:31:46 -0500 | [diff] [blame] | 50 | class NBDPipe: |
| 51 | |
| 52 | def openHTTPSocket(self,args): |
| 53 | |
| 54 | try: |
| 55 | _create_unverified_https_context = ssl._create_unverified_context |
| 56 | except AttributeError: |
| 57 | # Legacy Python that doesn't verify HTTPS certificates by default |
| 58 | pass |
| 59 | else: |
| 60 | # Handle target environment that doesn't support HTTPS verification |
| 61 | ssl._create_default_https_context = _create_unverified_https_context |
| 62 | |
| 63 | |
| 64 | token = gettoken(args) |
| 65 | self.conn = http.client.HTTPSConnection(args.host,port=443) |
| 66 | URI = "/redfish/v1/Systems/system/LogServices/SystemDump/Entries/"+str(args.dumpNum)+"/Actions/Oem/OpenBmc/LogEntry.DownloadLog" |
| 67 | self.conn.request("POST",URI, headers={"X-Auth-Token":token}) |
| 68 | |
| 69 | def openTCPSocket(self): |
| 70 | # Create a TCP/IP socket |
| 71 | self.tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 72 | # Connect the socket to the port where the server is listening |
| 73 | server_address = ('localhost', 1043) |
| 74 | self.tcp.connect(server_address) |
| 75 | |
| 76 | def waitformessage(self): |
| 77 | inputs = [self.conn.sock,self.tcp] |
| 78 | outputs = [] |
| 79 | message_queues = {} |
| 80 | while True: |
| 81 | readable, writable, exceptional = select.select( |
| 82 | inputs, outputs, inputs) |
| 83 | |
| 84 | for s in readable: |
| 85 | if s is self.conn.sock: |
| 86 | |
| 87 | data = self.conn.sock.recv(MAX_NBD_PACKET_SIZE) |
| 88 | print("<<HTTP") |
Ravi Teja | d8be0b4 | 2020-03-18 14:31:46 -0500 | [diff] [blame] | 89 | if data: |
| 90 | self.tcp.send(data) |
| 91 | else: |
| 92 | print ("BMC Closed the connection") |
| 93 | self.conn.close() |
| 94 | self.tcp.close() |
| 95 | sys.exit(1) |
| 96 | elif s is self.tcp: |
| 97 | data = self.tcp.recv(MAX_NBD_PACKET_SIZE) |
| 98 | print(">>TCP") |
Ravi Teja | d8be0b4 | 2020-03-18 14:31:46 -0500 | [diff] [blame] | 99 | if data: |
| 100 | self.conn.sock.send(data) |
| 101 | else: |
| 102 | print("NBD server closed the connection") |
| 103 | self.conn.sock.close() |
| 104 | self.tcp.close() |
| 105 | sys.exit(1) |
| 106 | for s in exceptional: |
| 107 | inputs.remove(s) |
| 108 | print("Exceptional closing the socket") |
| 109 | s.close() |
| 110 | |
| 111 | def getsize(host,args,session): |
| 112 | url = "https://"+host+"/redfish/v1/Systems/system/LogServices/SystemDump/Entries/"+str(args.dumpNum) |
Ravi Teja | d8be0b4 | 2020-03-18 14:31:46 -0500 | [diff] [blame] | 113 | try: |
| 114 | resp = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
| 115 | if resp.status_code==200: |
| 116 | size = resp.json()["Oem"]["OpenBmc"]['SizeInB'] |
Ravi Teja | d8be0b4 | 2020-03-18 14:31:46 -0500 | [diff] [blame] | 117 | return size |
| 118 | else: |
| 119 | return "Failed get Size" |
| 120 | except(requests.exceptions.Timeout): |
| 121 | return connectionErrHandler(args.json, "Timeout", None) |
| 122 | |
| 123 | except(requests.exceptions.ConnectionError) as err: |
| 124 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 125 | |
| 126 | def gettoken(args): |
| 127 | mysess = requests.session() |
| 128 | resp = mysess.post('https://'+args.host+'/login', headers=jsonHeader,json={"data":[args.user,args.PW]},verify=False) |
| 129 | if resp.status_code == 200: |
| 130 | cookie = resp.headers['Set-Cookie'] |
| 131 | match = re.search('SESSION=(\w+);', cookie) |
| 132 | return match.group(1) |
| 133 | |
| 134 | |
| 135 | |
| 136 | def get_pid(name): |
| 137 | try: |
| 138 | pid = map(int, check_output(["pidof", "-s",name])) |
| 139 | except Exception: |
| 140 | pid = 0 |
| 141 | |
| 142 | return pid |
| 143 | |
| 144 | def findThisProcess( process_name ): |
| 145 | ps = subprocess.Popen("ps -eaf | grep "+process_name, shell=True, stdout=subprocess.PIPE) |
| 146 | output = ps.stdout.read() |
| 147 | ps.stdout.close() |
| 148 | ps.wait() |
| 149 | pid = get_pid(process_name) |
Ravi Teja | d8be0b4 | 2020-03-18 14:31:46 -0500 | [diff] [blame] | 150 | return output |
| 151 | |
| 152 | def isThisProcessRunning( process_name ): |
| 153 | pid = get_pid(process_name) |
| 154 | if (pid == 0 ): |
| 155 | return False |
| 156 | else: |
| 157 | return True |
| 158 | |
| 159 | def NBDSetup(host,args,session): |
| 160 | user=os.getenv("SUDO_USER") |
| 161 | if user is None: |
| 162 | path = os.getcwd() |
| 163 | nbdServerPath = path + "/nbd-server" |
Ravi Teja | d8be0b4 | 2020-03-18 14:31:46 -0500 | [diff] [blame] | 164 | if not os.path.exists(nbdServerPath): |
| 165 | print("Error: this program did not run as sudo!\nplease copy nbd-server to current directory and run script again") |
| 166 | exit() |
| 167 | |
| 168 | if isThisProcessRunning('nbd-server') == True: |
| 169 | print("nbd-server already Running! killing the nbd-server") |
| 170 | os.system('killall nbd-server') |
| 171 | |
| 172 | if (args.dumpSaveLoc is not None): |
| 173 | if(os.path.exists(args.dumpSaveLoc)): |
| 174 | print("Error: File already exists.") |
| 175 | exit() |
| 176 | |
| 177 | fp= open(args.dumpSaveLoc,"w") |
| 178 | sizeInBytes = getsize(host,args,session) |
| 179 | #Round off size to mutiples of 1024 |
| 180 | size = int(sizeInBytes) |
Ravi Teja | d8be0b4 | 2020-03-18 14:31:46 -0500 | [diff] [blame] | 181 | mod = size % 1024 |
| 182 | if mod : |
| 183 | roundoff = 1024 - mod |
| 184 | size = size + roundoff |
| 185 | |
| 186 | cmd = 'chmod 777 ' + args.dumpSaveLoc |
| 187 | os.system(cmd) |
| 188 | |
| 189 | #Run truncate to create file with given size |
| 190 | cmd = 'truncate -s ' + str(size) + ' '+ args.dumpSaveLoc |
| 191 | os.system(cmd) |
| 192 | |
| 193 | if user is None: |
| 194 | cmd = './nbd-server 1043 '+ args.dumpSaveLoc |
| 195 | else: |
| 196 | cmd = 'nbd-server 1043 '+ args.dumpSaveLoc |
| 197 | os.system(cmd) |
| 198 | |
| 199 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 200 | def hilight(textToColor, color, bold): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 201 | """ |
| 202 | Used to add highlights to various text for displaying in a terminal |
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 | @param textToColor: string, the text to be colored |
| 205 | @param color: string, used to color the text red or green |
| 206 | @param bold: boolean, used to bold the textToColor |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 207 | @return: Buffered reader containing the modified string. |
| 208 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 209 | if(sys.platform.__contains__("win")): |
| 210 | if(color == "red"): |
| 211 | os.system('color 04') |
| 212 | elif(color == "green"): |
| 213 | os.system('color 02') |
| 214 | else: |
| 215 | os.system('color') #reset to default |
| 216 | return textToColor |
| 217 | else: |
| 218 | attr = [] |
| 219 | if(color == "red"): |
| 220 | attr.append('31') |
| 221 | elif(color == "green"): |
| 222 | attr.append('32') |
| 223 | else: |
| 224 | attr.append('0') |
| 225 | if bold: |
| 226 | attr.append('1') |
| 227 | else: |
| 228 | attr.append('0') |
| 229 | return '\x1b[%sm%s\x1b[0m' % (';'.join(attr),textToColor) |
| 230 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 231 | def connectionErrHandler(jsonFormat, errorStr, err): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 232 | """ |
| 233 | Error handler various connection errors to bmcs |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 234 | |
| 235 | @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] | 236 | @param errorStr: string, used to color the text red or green |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 237 | @param err: string, the text from the exception |
| 238 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 239 | if errorStr == "Timeout": |
| 240 | if not jsonFormat: |
| 241 | return("FQPSPIN0000M: Connection timed out. Ensure you have network connectivity to the bmc") |
| 242 | else: |
Justin Thaler | 115bca7 | 2018-05-25 19:29:08 -0500 | [diff] [blame] | 243 | conerror = {} |
| 244 | conerror['CommonEventID'] = 'FQPSPIN0000M' |
| 245 | conerror['sensor']="N/A" |
| 246 | conerror['state']="N/A" |
| 247 | conerror['additionalDetails'] = "N/A" |
| 248 | conerror['Message']="Connection timed out. Ensure you have network connectivity to the BMC" |
| 249 | 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." |
| 250 | conerror['Serviceable']="Yes" |
| 251 | conerror['CallHomeCandidate']= "No" |
| 252 | conerror['Severity'] = "Critical" |
| 253 | conerror['EventType'] = "Communication Failure/Timeout" |
| 254 | conerror['VMMigrationFlag'] = "Yes" |
| 255 | conerror["AffectedSubsystem"] = "Interconnect (Networking)" |
| 256 | conerror["timestamp"] = str(int(time.time())) |
| 257 | conerror["UserAction"] = "Verify network connectivity between the two systems and the bmc is functional." |
| 258 | eventdict = {} |
| 259 | eventdict['event0'] = conerror |
| 260 | eventdict['numAlerts'] = '1' |
Justin Thaler | 115bca7 | 2018-05-25 19:29:08 -0500 | [diff] [blame] | 261 | 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] | 262 | return(errorMessageStr) |
| 263 | elif errorStr == "ConnectionError": |
| 264 | if not jsonFormat: |
| 265 | return("FQPSPIN0001M: " + str(err)) |
| 266 | else: |
Justin Thaler | 115bca7 | 2018-05-25 19:29:08 -0500 | [diff] [blame] | 267 | conerror = {} |
| 268 | conerror['CommonEventID'] = 'FQPSPIN0001M' |
| 269 | conerror['sensor']="N/A" |
| 270 | conerror['state']="N/A" |
| 271 | conerror['additionalDetails'] = str(err) |
| 272 | conerror['Message']="Connection Error. View additional details for more information" |
| 273 | conerror['LengthyDescription'] = "A connection error to the specified BMC occurred and additional details are provided. Review these details to resolve the issue." |
| 274 | conerror['Serviceable']="Yes" |
| 275 | conerror['CallHomeCandidate']= "No" |
| 276 | conerror['Severity'] = "Critical" |
| 277 | conerror['EventType'] = "Communication Failure/Timeout" |
| 278 | conerror['VMMigrationFlag'] = "Yes" |
| 279 | conerror["AffectedSubsystem"] = "Interconnect (Networking)" |
| 280 | conerror["timestamp"] = str(int(time.time())) |
| 281 | conerror["UserAction"] = "Correct the issue highlighted in additional details and try again" |
| 282 | eventdict = {} |
| 283 | eventdict['event0'] = conerror |
| 284 | eventdict['numAlerts'] = '1' |
Justin Thaler | 115bca7 | 2018-05-25 19:29:08 -0500 | [diff] [blame] | 285 | 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] | 286 | return(errorMessageStr) |
Justin Thaler | 115bca7 | 2018-05-25 19:29:08 -0500 | [diff] [blame] | 287 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 288 | else: |
| 289 | return("Unknown Error: "+ str(err)) |
| 290 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 291 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 292 | def setColWidth(keylist, numCols, dictForOutput, colNames): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 293 | """ |
| 294 | Sets the output width of the columns to display |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 295 | |
| 296 | @param keylist: list, list of strings representing the keys for the dictForOutput |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 297 | @param numcols: the total number of columns in the final output |
| 298 | @param dictForOutput: dictionary, contains the information to print to the screen |
| 299 | @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] | 300 | @return: A list of the column widths for each respective column. |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 301 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 302 | colWidths = [] |
| 303 | for x in range(0, numCols): |
| 304 | colWidths.append(0) |
| 305 | for key in dictForOutput: |
| 306 | for x in range(0, numCols): |
| 307 | colWidths[x] = max(colWidths[x], len(str(dictForOutput[key][keylist[x]]))) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 308 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 309 | for x in range(0, numCols): |
| 310 | colWidths[x] = max(colWidths[x], len(colNames[x])) +2 |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 311 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 312 | return colWidths |
| 313 | |
| 314 | def loadPolicyTable(pathToPolicyTable): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 315 | """ |
| 316 | loads a json based policy table into a dictionary |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 317 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 318 | @param value: boolean, the value to convert |
| 319 | @return: A string of "Yes" or "No" |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 320 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 321 | policyTable = {} |
| 322 | if(os.path.exists(pathToPolicyTable)): |
| 323 | with open(pathToPolicyTable, 'r') as stream: |
| 324 | try: |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 325 | contents =json.load(stream) |
| 326 | policyTable = contents['events'] |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 327 | except Exception as err: |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 328 | print(err) |
| 329 | return policyTable |
| 330 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 331 | |
| 332 | def boolToString(value): |
| 333 | """ |
| 334 | converts a boolean value to a human readable string value |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 335 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 336 | @param value: boolean, the value to convert |
| 337 | @return: A string of "Yes" or "No" |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 338 | """ |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 339 | if(value): |
| 340 | return "Yes" |
| 341 | else: |
| 342 | return "No" |
| 343 | |
Justin Thaler | a6b5df7 | 2018-07-16 11:10:07 -0500 | [diff] [blame] | 344 | def stringToInt(text): |
| 345 | """ |
| 346 | 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] | 347 | |
Justin Thaler | a6b5df7 | 2018-07-16 11:10:07 -0500 | [diff] [blame] | 348 | @param text: the string to try to convert to an integer |
| 349 | """ |
| 350 | if text.isdigit(): |
| 351 | return int(text) |
| 352 | else: |
| 353 | return text |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 354 | |
Justin Thaler | a6b5df7 | 2018-07-16 11:10:07 -0500 | [diff] [blame] | 355 | def naturalSort(text): |
| 356 | """ |
| 357 | provides a way to naturally sort a list |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 358 | |
Justin Thaler | a6b5df7 | 2018-07-16 11:10:07 -0500 | [diff] [blame] | 359 | @param text: the key to convert for sorting |
| 360 | @return list containing the broken up string parts by integers and strings |
| 361 | """ |
| 362 | stringPartList = [] |
| 363 | for c in re.split('(\d+)', text): |
| 364 | stringPartList.append(stringToInt(c)) |
| 365 | return stringPartList |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 366 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 367 | def tableDisplay(keylist, colNames, output): |
| 368 | """ |
| 369 | Logs into the BMC and creates a session |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 370 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 371 | @param keylist: list, keys for the output dictionary, ordered by colNames |
| 372 | @param colNames: Names for the Table of the columns |
| 373 | @param output: The dictionary of data to display |
| 374 | @return: Session object |
| 375 | """ |
| 376 | colWidth = setColWidth(keylist, len(colNames), output, colNames) |
| 377 | row = "" |
| 378 | outputText = "" |
| 379 | for i in range(len(colNames)): |
| 380 | if (i != 0): row = row + "| " |
| 381 | row = row + colNames[i].ljust(colWidth[i]) |
| 382 | outputText += row + "\n" |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 383 | |
Justin Thaler | a6b5df7 | 2018-07-16 11:10:07 -0500 | [diff] [blame] | 384 | output_keys = list(output.keys()) |
| 385 | output_keys.sort(key=naturalSort) |
| 386 | for key in output_keys: |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 387 | row = "" |
Justin Thaler | 8fe0c73 | 2018-07-24 14:32:35 -0500 | [diff] [blame] | 388 | for i in range(len(keylist)): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 389 | if (i != 0): row = row + "| " |
| 390 | row = row + output[key][keylist[i]].ljust(colWidth[i]) |
| 391 | outputText += row + "\n" |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 392 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 393 | return outputText |
| 394 | |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 395 | def checkFWactivation(host, args, session): |
| 396 | """ |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 397 | Checks the software inventory for an image that is being activated. |
| 398 | |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 399 | @return: True if an image is being activated, false is no activations are happening |
| 400 | """ |
| 401 | url="https://"+host+"/xyz/openbmc_project/software/enumerate" |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 402 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 403 | resp = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 404 | except(requests.exceptions.Timeout): |
| 405 | print(connectionErrHandler(args.json, "Timeout", None)) |
| 406 | return(True) |
| 407 | except(requests.exceptions.ConnectionError) as err: |
| 408 | print( connectionErrHandler(args.json, "ConnectionError", err)) |
| 409 | return True |
Justin Thaler | 3a5771b | 2019-01-23 14:31:52 -0600 | [diff] [blame] | 410 | fwInfo = resp.json()['data'] |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 411 | for key in fwInfo: |
| 412 | if 'Activation' in fwInfo[key]: |
| 413 | if 'Activating' in fwInfo[key]['Activation'] or 'Activating' in fwInfo[key]['RequestedActivation']: |
| 414 | return True |
| 415 | return False |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 416 | |
Joy Onyerikwu | 182c3a3 | 2019-10-15 08:33:59 -0500 | [diff] [blame] | 417 | def login(host, username, pw,jsonFormat, allowExpiredPassword): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 418 | """ |
| 419 | Logs into the BMC and creates a session |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 420 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 421 | @param host: string, the hostname or IP address of the bmc to log into |
| 422 | @param username: The user name for the bmc to log into |
| 423 | @param pw: The password for the BMC to log into |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 424 | @param jsonFormat: boolean, flag that will only allow relevant data from user command to be display. This function becomes silent when set to true. |
Joy Onyerikwu | 182c3a3 | 2019-10-15 08:33:59 -0500 | [diff] [blame] | 425 | @param allowExpiredPassword: true, if the requested operation should |
| 426 | be allowed when the password is expired |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 427 | @return: Session object |
| 428 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 429 | if(jsonFormat==False): |
| 430 | print("Attempting login...") |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 431 | mysess = requests.session() |
| 432 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 433 | r = mysess.post('https://'+host+'/login', headers=jsonHeader, json = {"data": [username, pw]}, verify=False, timeout=baseTimeout) |
Sunitha Harish | 336cda2 | 2019-07-23 02:02:52 -0500 | [diff] [blame] | 434 | if r.status_code == 200: |
| 435 | cookie = r.headers['Set-Cookie'] |
| 436 | match = re.search('SESSION=(\w+);', cookie) |
| 437 | if match: |
| 438 | xAuthHeader['X-Auth-Token'] = match.group(1) |
| 439 | jsonHeader.update(xAuthHeader) |
| 440 | loginMessage = json.loads(r.text) |
| 441 | if (loginMessage['status'] != "ok"): |
| 442 | print(loginMessage["data"]["description"].encode('utf-8')) |
| 443 | sys.exit(1) |
Joy Onyerikwu | 182c3a3 | 2019-10-15 08:33:59 -0500 | [diff] [blame] | 444 | if (('extendedMessage' in r.json()) and |
| 445 | ('The password for this account must be changed' in r.json()['extendedMessage'])): |
| 446 | if not allowExpiredPassword: |
| 447 | print("The password for this system has expired and must be changed"+ |
| 448 | "\nsee openbmctool.py set_password --help") |
| 449 | logout(host, username, pw, mysess, jsonFormat) |
| 450 | sys.exit(1) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 451 | # if(sys.version_info < (3,0)): |
| 452 | # urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) |
| 453 | # if sys.version_info >= (3,0): |
| 454 | # requests.packages.urllib3.disable_warnings(requests.packages.urllib3.exceptions.InsecureRequestWarning) |
Sunitha Harish | 336cda2 | 2019-07-23 02:02:52 -0500 | [diff] [blame] | 455 | return mysess |
| 456 | else: |
| 457 | return None |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 458 | except(requests.exceptions.Timeout): |
Justin Thaler | 115bca7 | 2018-05-25 19:29:08 -0500 | [diff] [blame] | 459 | return (connectionErrHandler(jsonFormat, "Timeout", None)) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 460 | except(requests.exceptions.ConnectionError) as err: |
Justin Thaler | 115bca7 | 2018-05-25 19:29:08 -0500 | [diff] [blame] | 461 | return (connectionErrHandler(jsonFormat, "ConnectionError", err)) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 462 | |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 463 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 464 | def logout(host, username, pw, session, jsonFormat): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 465 | """ |
| 466 | Logs out of the bmc and terminates the session |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 467 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 468 | @param host: string, the hostname or IP address of the bmc to log out of |
| 469 | @param username: The user name for the bmc to log out of |
| 470 | @param pw: The password for the BMC to log out of |
| 471 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 472 | @param jsonFormat: boolean, flag that will only allow relevant data from user command to be display. This function becomes silent when set to true. |
| 473 | """ |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 474 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 475 | r = session.post('https://'+host+'/logout', headers=jsonHeader,json = {"data": [username, pw]}, verify=False, timeout=baseTimeout) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 476 | except(requests.exceptions.Timeout): |
| 477 | print(connectionErrHandler(jsonFormat, "Timeout", None)) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 478 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 479 | if(jsonFormat==False): |
Matt Spinler | eae05b0 | 2019-01-24 12:59:34 -0600 | [diff] [blame] | 480 | if r.status_code == 200: |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 481 | print('User ' +username + ' has been logged out') |
| 482 | |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 483 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 484 | def fru(host, args, session): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 485 | """ |
| 486 | prints out the system inventory. deprecated see fruPrint and fruList |
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 | @param host: string, the hostname or IP address of the bmc |
| 489 | @param args: contains additional arguments used by the fru sub command |
| 490 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 491 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 492 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 493 | #url="https://"+host+"/org/openbmc/inventory/system/chassis/enumerate" |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 494 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 495 | #print(url) |
| 496 | #res = session.get(url, headers=httpHeader, verify=False) |
| 497 | #print(res.text) |
| 498 | #sample = res.text |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 499 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 500 | #inv_list = json.loads(sample)["data"] |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 501 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 502 | url="https://"+host+"/xyz/openbmc_project/inventory/enumerate" |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 503 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 504 | res = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 505 | except(requests.exceptions.Timeout): |
| 506 | return(connectionErrHandler(args.json, "Timeout", None)) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 507 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 508 | sample = res.text |
| 509 | # inv_list.update(json.loads(sample)["data"]) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 510 | # |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 511 | # #determine column width's |
| 512 | # colNames = ["FRU Name", "FRU Type", "Has Fault", "Is FRU", "Present", "Version"] |
| 513 | # 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] | 514 | # |
| 515 | # 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] | 516 | # "Present".ljust(colWidths[4]) + "Version".ljust(colWidths[5])) |
| 517 | # format the output |
| 518 | # for key in sorted(inv_list.keys()): |
| 519 | # keyParts = key.split("/") |
| 520 | # isFRU = "True" if (inv_list[key]["is_fru"]==1) else "False" |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 521 | # |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 522 | # fruEntry = (keyParts[len(keyParts) - 1].ljust(colWidths[0]) + inv_list[key]["fru_type"].ljust(colWidths[1])+ |
| 523 | # inv_list[key]["fault"].ljust(colWidths[2])+isFRU.ljust(colWidths[3])+ |
| 524 | # inv_list[key]["present"].ljust(colWidths[4])+ inv_list[key]["version"].ljust(colWidths[5])) |
| 525 | # if(isTTY): |
| 526 | # if(inv_list[key]["is_fru"] == 1): |
| 527 | # color = "green" |
| 528 | # bold = True |
| 529 | # else: |
| 530 | # color='black' |
| 531 | # bold = False |
| 532 | # fruEntry = hilight(fruEntry, color, bold) |
| 533 | # print (fruEntry) |
| 534 | return sample |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 535 | |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 536 | def fruPrint(host, args, session): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 537 | """ |
| 538 | prints out all inventory |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 539 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 540 | @param host: string, the hostname or IP address of the bmc |
| 541 | @param args: contains additional arguments used by the fru sub command |
| 542 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 543 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 544 | @return returns the total fru list. |
| 545 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 546 | url="https://"+host+"/xyz/openbmc_project/inventory/enumerate" |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 547 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 548 | res = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 549 | except(requests.exceptions.Timeout): |
| 550 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 551 | |
Justin Thaler | 3a5771b | 2019-01-23 14:31:52 -0600 | [diff] [blame] | 552 | frulist={} |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 553 | # print(res.text) |
Justin Thaler | 3a5771b | 2019-01-23 14:31:52 -0600 | [diff] [blame] | 554 | if res.status_code==200: |
| 555 | frulist['Hardware'] = res.json()['data'] |
| 556 | else: |
| 557 | if not args.json: |
| 558 | return "Error retrieving the system inventory. BMC message: {msg}".format(msg=res.json()['message']) |
| 559 | else: |
| 560 | return res.json() |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 561 | url="https://"+host+"/xyz/openbmc_project/software/enumerate" |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 562 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 563 | res = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 564 | except(requests.exceptions.Timeout): |
| 565 | return(connectionErrHandler(args.json, "Timeout", None)) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 566 | # print(res.text) |
Justin Thaler | 3a5771b | 2019-01-23 14:31:52 -0600 | [diff] [blame] | 567 | if res.status_code==200: |
| 568 | frulist['Software'] = res.json()['data'] |
| 569 | else: |
| 570 | if not args.json(): |
| 571 | return "Error retrieving the system inventory. BMC message: {msg}".format(msg=res.json()['message']) |
| 572 | else: |
| 573 | return res.json() |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 574 | return frulist |
| 575 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 576 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 577 | def fruList(host, args, session): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 578 | """ |
| 579 | prints out all inventory or only a specific specified item |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 580 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 581 | @param host: string, the hostname or IP address of the bmc |
| 582 | @param args: contains additional arguments used by the fru sub command |
| 583 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 584 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 585 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 586 | if(args.items==True): |
| 587 | return fruPrint(host, args, session) |
| 588 | else: |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 589 | return fruPrint(host, args, session) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 590 | |
| 591 | |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 592 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 593 | def fruStatus(host, args, session): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 594 | """ |
| 595 | prints out the status of all FRUs |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 596 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 597 | @param host: string, the hostname or IP address of the bmc |
| 598 | @param args: contains additional arguments used by the fru sub command |
| 599 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 600 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 601 | """ |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 602 | url="https://"+host+"/xyz/openbmc_project/inventory/enumerate" |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 603 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 604 | res = session.get(url, headers=jsonHeader, verify=False) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 605 | except(requests.exceptions.Timeout): |
| 606 | return(connectionErrHandler(args.json, "Timeout", None)) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 607 | # print(res.text) |
Justin Thaler | 3a5771b | 2019-01-23 14:31:52 -0600 | [diff] [blame] | 608 | frulist = res.json()['data'] |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 609 | frus = {} |
| 610 | for key in frulist: |
| 611 | component = frulist[key] |
| 612 | isFru = False |
| 613 | present = False |
| 614 | func = False |
| 615 | hasSels = False |
| 616 | keyPieces = key.split('/') |
| 617 | fruName = keyPieces[-1] |
| 618 | if 'core' in fruName: #associate cores to cpus |
| 619 | fruName = keyPieces[-2] + '-' + keyPieces[-1] |
| 620 | if 'Functional' in component: |
| 621 | if('Present' in component): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 622 | if 'FieldReplaceable' in component: |
| 623 | if component['FieldReplaceable'] == 1: |
| 624 | isFru = True |
| 625 | if "fan" in fruName: |
| 626 | isFru = True; |
| 627 | if component['Present'] == 1: |
| 628 | present = True |
| 629 | if component['Functional'] == 1: |
| 630 | func = True |
| 631 | if ((key + "/fault") in frulist): |
| 632 | hasSels = True; |
| 633 | if args.verbose: |
| 634 | if hasSels: |
| 635 | loglist = [] |
| 636 | faults = frulist[key+"/fault"]['endpoints'] |
| 637 | for item in faults: |
| 638 | loglist.append(item.split('/')[-1]) |
| 639 | frus[fruName] = {"compName": fruName, "Functional": boolToString(func), "Present":boolToString(present), "IsFru": boolToString(isFru), "selList": ', '.join(loglist).strip() } |
| 640 | else: |
| 641 | frus[fruName] = {"compName": fruName, "Functional": boolToString(func), "Present":boolToString(present), "IsFru": boolToString(isFru), "selList": "None" } |
| 642 | else: |
| 643 | 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] | 644 | elif "power_supply" in fruName or "powersupply" in fruName: |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 645 | if component['Present'] ==1: |
| 646 | present = True |
| 647 | isFru = True |
| 648 | if ((key + "/fault") in frulist): |
| 649 | hasSels = True; |
| 650 | if args.verbose: |
| 651 | if hasSels: |
| 652 | loglist = [] |
| 653 | faults = frulist[key+"/fault"]['endpoints'] |
Obihörnchen | ff8035f | 2018-12-05 21:07:37 +0100 | [diff] [blame] | 654 | for item in faults: |
| 655 | loglist.append(item.split('/')[-1]) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 656 | frus[fruName] = {"compName": fruName, "Functional": "No", "Present":boolToString(present), "IsFru": boolToString(isFru), "selList": ', '.join(loglist).strip() } |
| 657 | else: |
| 658 | frus[fruName] = {"compName": fruName, "Functional": "Yes", "Present":boolToString(present), "IsFru": boolToString(isFru), "selList": "None" } |
| 659 | else: |
| 660 | frus[fruName] = {"compName": fruName, "Functional": boolToString(not hasSels), "Present":boolToString(present), "IsFru": boolToString(isFru), "hasSEL": boolToString(hasSels) } |
| 661 | if not args.json: |
| 662 | if not args.verbose: |
| 663 | colNames = ["Component", "Is a FRU", "Present", "Functional", "Has Logs"] |
| 664 | keylist = ["compName", "IsFru", "Present", "Functional", "hasSEL"] |
| 665 | else: |
| 666 | colNames = ["Component", "Is a FRU", "Present", "Functional", "Assoc. Log Number(s)"] |
| 667 | keylist = ["compName", "IsFru", "Present", "Functional", "selList"] |
| 668 | return tableDisplay(keylist, colNames, frus) |
| 669 | else: |
| 670 | 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] | 671 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 672 | def sensor(host, args, session): |
| 673 | """ |
| 674 | prints out all sensors |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 675 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 676 | @param host: string, the hostname or IP address of the bmc |
| 677 | @param args: contains additional arguments used by the sensor sub command |
| 678 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 679 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 680 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 681 | url="https://"+host+"/xyz/openbmc_project/sensors/enumerate" |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 682 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 683 | res = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 684 | except(requests.exceptions.Timeout): |
| 685 | return(connectionErrHandler(args.json, "Timeout", None)) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 686 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 687 | #Get OCC status |
| 688 | url="https://"+host+"/org/open_power/control/enumerate" |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 689 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 690 | occres = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 691 | except(requests.exceptions.Timeout): |
| 692 | return(connectionErrHandler(args.json, "Timeout", None)) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 693 | if not args.json: |
| 694 | colNames = ['sensor', 'type', 'units', 'value', 'target'] |
Justin Thaler | 3a5771b | 2019-01-23 14:31:52 -0600 | [diff] [blame] | 695 | sensors = res.json()["data"] |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 696 | output = {} |
| 697 | for key in sensors: |
| 698 | senDict = {} |
| 699 | keyparts = key.split("/") |
Matt Spinler | 596ef74 | 2019-09-20 08:54:36 -0500 | [diff] [blame] | 700 | |
| 701 | # Associations like the following also show up here: |
| 702 | # /xyz/openbmc_project/sensors/<type>/<name>/<assoc-name> |
| 703 | # Skip them. |
| 704 | # Note: keyparts[0] = '' which is why there are 7 segments. |
| 705 | if len(keyparts) > 6: |
| 706 | continue |
| 707 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 708 | senDict['sensorName'] = keyparts[-1] |
| 709 | senDict['type'] = keyparts[-2] |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 710 | try: |
| 711 | senDict['units'] = sensors[key]['Unit'].split('.')[-1] |
| 712 | except KeyError: |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 713 | senDict['units'] = "N/A" |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 714 | if('Scale' in sensors[key]): |
| 715 | scale = 10 ** sensors[key]['Scale'] |
| 716 | else: |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 717 | scale = 1 |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 718 | try: |
| 719 | senDict['value'] = str(sensors[key]['Value'] * scale) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 720 | except KeyError: |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 721 | if 'value' in sensors[key]: |
| 722 | senDict['value'] = sensors[key]['value'] |
| 723 | else: |
| 724 | senDict['value'] = "N/A" |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 725 | if 'Target' in sensors[key]: |
| 726 | senDict['target'] = str(sensors[key]['Target']) |
| 727 | else: |
| 728 | senDict['target'] = 'N/A' |
| 729 | output[senDict['sensorName']] = senDict |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 730 | |
Justin Thaler | 3a5771b | 2019-01-23 14:31:52 -0600 | [diff] [blame] | 731 | occstatus = occres.json()["data"] |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 732 | if '/org/open_power/control/occ0' in occstatus: |
| 733 | occ0 = occstatus["/org/open_power/control/occ0"]['OccActive'] |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 734 | if occ0 == 1: |
| 735 | occ0 = 'Active' |
| 736 | else: |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 737 | occ0 = 'Inactive' |
| 738 | output['OCC0'] = {'sensorName':'OCC0', 'type': 'Discrete', 'units': 'N/A', 'value': occ0, 'target': 'Active'} |
| 739 | occ1 = occstatus["/org/open_power/control/occ1"]['OccActive'] |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 740 | if occ1 == 1: |
| 741 | occ1 = 'Active' |
| 742 | else: |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 743 | occ1 = 'Inactive' |
| 744 | output['OCC1'] = {'sensorName':'OCC1', 'type': 'Discrete', 'units': 'N/A', 'value': occ0, 'target': 'Active'} |
| 745 | else: |
| 746 | output['OCC0'] = {'sensorName':'OCC0', 'type': 'Discrete', 'units': 'N/A', 'value': 'Inactive', 'target': 'Inactive'} |
| 747 | output['OCC1'] = {'sensorName':'OCC1', 'type': 'Discrete', 'units': 'N/A', 'value': 'Inactive', 'target': 'Inactive'} |
| 748 | keylist = ['sensorName', 'type', 'units', 'value', 'target'] |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 749 | |
| 750 | return tableDisplay(keylist, colNames, output) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 751 | else: |
| 752 | return res.text + occres.text |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 753 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 754 | def sel(host, args, session): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 755 | """ |
| 756 | prints out the bmc alerts |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 757 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 758 | @param host: string, the hostname or IP address of the bmc |
| 759 | @param args: contains additional arguments used by the sel sub command |
| 760 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 761 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 762 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 763 | |
| 764 | url="https://"+host+"/xyz/openbmc_project/logging/entry/enumerate" |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 765 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 766 | res = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 767 | except(requests.exceptions.Timeout): |
| 768 | return(connectionErrHandler(args.json, "Timeout", None)) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 769 | return res.text |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 770 | |
| 771 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 772 | def parseESEL(args, eselRAW): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 773 | """ |
| 774 | parses the esel data and gets predetermined search terms |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 775 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 776 | @param eselRAW: string, the raw esel string from the bmc |
| 777 | @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] | 778 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 779 | eselParts = {} |
| 780 | esel_bin = binascii.unhexlify(''.join(eselRAW.split()[16:])) |
| 781 | #search terms contains the search term as the key and the return dictionary key as it's value |
| 782 | searchTerms = { 'Signature Description':'signatureDescription', 'devdesc':'devdesc', |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 783 | 'Callout type': 'calloutType', 'Procedure':'procedure', 'Sensor Type': 'sensorType'} |
Justin Thaler | 24d4efa | 2018-11-08 22:48:10 -0600 | [diff] [blame] | 784 | uniqueID = str(uuid.uuid4()) |
| 785 | eselBinPath = tempfile.gettempdir() + os.sep + uniqueID + 'esel.bin' |
Justin Thaler | cf1deae | 2018-05-25 19:35:21 -0500 | [diff] [blame] | 786 | with open(eselBinPath, 'wb') as f: |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 787 | f.write(esel_bin) |
| 788 | errlPath = "" |
| 789 | #use the right errl file for the machine architecture |
| 790 | arch = platform.machine() |
| 791 | if(arch =='x86_64' or arch =='AMD64'): |
| 792 | if os.path.exists('/opt/ibm/ras/bin/x86_64/errl'): |
| 793 | errlPath = '/opt/ibm/ras/bin/x86_64/errl' |
| 794 | elif os.path.exists('errl/x86_64/errl'): |
| 795 | errlPath = 'errl/x86_64/errl' |
| 796 | else: |
| 797 | errlPath = 'x86_64/errl' |
| 798 | elif (platform.machine()=='ppc64le'): |
| 799 | if os.path.exists('/opt/ibm/ras/bin/ppc64le/errl'): |
| 800 | errlPath = '/opt/ibm/ras/bin/ppc64le/errl' |
| 801 | elif os.path.exists('errl/ppc64le/errl'): |
| 802 | errlPath = 'errl/ppc64le/errl' |
| 803 | else: |
| 804 | errlPath = 'ppc64le/errl' |
| 805 | else: |
| 806 | print("machine architecture not supported for parsing eSELs") |
| 807 | return eselParts |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 808 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 809 | if(os.path.exists(errlPath)): |
Justin Thaler | cf1deae | 2018-05-25 19:35:21 -0500 | [diff] [blame] | 810 | output= subprocess.check_output([errlPath, '-d', '--file='+eselBinPath]).decode('utf-8') |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 811 | # output = proc.communicate()[0] |
| 812 | lines = output.split('\n') |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 813 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 814 | if(hasattr(args, 'fullEsel')): |
| 815 | return output |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 816 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 817 | for i in range(0, len(lines)): |
| 818 | lineParts = lines[i].split(':') |
| 819 | if(len(lineParts)>1): #ignore multi lines, output formatting lines, and other information |
| 820 | for term in searchTerms: |
| 821 | if(term in lineParts[0]): |
| 822 | temp = lines[i][lines[i].find(':')+1:].strip()[:-1].strip() |
| 823 | if lines[i+1].find(':') != -1: |
| 824 | if (len(lines[i+1].split(':')[0][1:].strip())==0): |
| 825 | while(len(lines[i][:lines[i].find(':')].strip())>2): |
Justin Thaler | 4303042 | 2018-11-08 22:50:21 -0600 | [diff] [blame] | 826 | #has multiple lines, process and update line counter |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 827 | if((i+1) <= len(lines)): |
| 828 | i+=1 |
| 829 | else: |
| 830 | i=i-1 |
| 831 | break |
Justin Thaler | 4303042 | 2018-11-08 22:50:21 -0600 | [diff] [blame] | 832 | #Append the content from the next line removing the pretty display characters |
| 833 | #Finds the first colon then starts 2 characters after, then removes all whitespace |
| 834 | 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] | 835 | if(searchTerms[term] in eselParts): |
| 836 | eselParts[searchTerms[term]] = eselParts[searchTerms[term]] + ", " + temp |
| 837 | else: |
| 838 | eselParts[searchTerms[term]] = temp |
Justin Thaler | cf1deae | 2018-05-25 19:35:21 -0500 | [diff] [blame] | 839 | os.remove(eselBinPath) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 840 | else: |
| 841 | print("errl file cannot be found") |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 842 | |
| 843 | return eselParts |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 844 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 845 | |
Matt Spinler | 02d0dff | 2018-08-29 13:19:25 -0500 | [diff] [blame] | 846 | def getESELSeverity(esel): |
| 847 | """ |
| 848 | Finds the severity type in an eSEL from the User Header section. |
| 849 | @param esel - the eSEL data |
| 850 | @return severity - e.g. 'Critical' |
| 851 | """ |
| 852 | |
| 853 | # everything but 1 and 2 are Critical |
| 854 | # '1': 'recovered', |
| 855 | # '2': 'predictive', |
| 856 | # '4': 'unrecoverable', |
| 857 | # '5': 'critical', |
| 858 | # '6': 'diagnostic', |
| 859 | # '7': 'symptom' |
| 860 | severities = { |
| 861 | '1': 'Informational', |
| 862 | '2': 'Warning' |
| 863 | } |
| 864 | |
| 865 | try: |
| 866 | headerPosition = esel.index('55 48') # 'UH' |
| 867 | # The severity is the last byte in the 8 byte section (a byte is ' bb') |
| 868 | severity = esel[headerPosition:headerPosition+32].split(' ')[-1] |
| 869 | type = severity[0] |
| 870 | except ValueError: |
| 871 | print("Could not find severity value in UH section in eSEL") |
| 872 | type = 'x'; |
| 873 | |
| 874 | return severities.get(type, 'Critical') |
| 875 | |
| 876 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 877 | def sortSELs(events): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 878 | """ |
| 879 | sorts the sels by timestamp, then log entry number |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 880 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 881 | @param events: Dictionary containing events |
| 882 | @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] | 883 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 884 | logNumList = [] |
| 885 | timestampList = [] |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 886 | eventKeyDict = {} |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 887 | eventsWithTimestamp = {} |
| 888 | logNum2events = {} |
| 889 | for key in events: |
| 890 | if key == 'numAlerts': continue |
| 891 | if 'callout' in key: continue |
| 892 | timestamp = (events[key]['timestamp']) |
| 893 | if timestamp not in timestampList: |
| 894 | eventsWithTimestamp[timestamp] = [events[key]['logNum']] |
| 895 | else: |
| 896 | eventsWithTimestamp[timestamp].append(events[key]['logNum']) |
| 897 | #map logNumbers to the event dictionary keys |
| 898 | eventKeyDict[str(events[key]['logNum'])] = key |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 899 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 900 | timestampList = list(eventsWithTimestamp.keys()) |
| 901 | timestampList.sort() |
| 902 | for ts in timestampList: |
| 903 | if len(eventsWithTimestamp[ts]) > 1: |
| 904 | tmplist = eventsWithTimestamp[ts] |
| 905 | tmplist.sort() |
| 906 | logNumList = logNumList + tmplist |
| 907 | else: |
| 908 | logNumList = logNumList + eventsWithTimestamp[ts] |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 909 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 910 | return [logNumList, eventKeyDict] |
| 911 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 912 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 913 | def parseAlerts(policyTable, selEntries, args): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 914 | """ |
| 915 | parses alerts in the IBM CER format, using an IBM policy Table |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 916 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 917 | @param policyTable: dictionary, the policy table entries |
| 918 | @param selEntries: dictionary, the alerts retrieved from the bmc |
| 919 | @return: A dictionary of the parsed entries, in chronological order |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 920 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 921 | eventDict = {} |
| 922 | eventNum ="" |
| 923 | count = 0 |
| 924 | esel = "" |
| 925 | eselParts = {} |
| 926 | i2cdevice= "" |
Matt Spinler | 02d0dff | 2018-08-29 13:19:25 -0500 | [diff] [blame] | 927 | eselSeverity = None |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 928 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 929 | 'prepare and sort the event entries' |
Justin Thaler | 667f87c | 2020-04-06 16:13:12 -0500 | [diff] [blame] | 930 | sels = {} |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 931 | for key in selEntries: |
Justin Thaler | 667f87c | 2020-04-06 16:13:12 -0500 | [diff] [blame] | 932 | if '/xyz/openbmc_project/logging/entry/' not in key: continue |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 933 | if 'callout' not in key: |
Justin Thaler | 667f87c | 2020-04-06 16:13:12 -0500 | [diff] [blame] | 934 | sels[key] = selEntries[key] |
| 935 | sels[key]['logNum'] = key.split('/')[-1] |
| 936 | sels[key]['timestamp'] = selEntries[key]['Timestamp'] |
| 937 | sortedEntries = sortSELs(sels) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 938 | logNumList = sortedEntries[0] |
| 939 | eventKeyDict = sortedEntries[1] |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 940 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 941 | for logNum in logNumList: |
| 942 | key = eventKeyDict[logNum] |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 943 | hasEsel=False |
| 944 | i2creadFail = False |
| 945 | if 'callout' in key: |
| 946 | continue |
| 947 | else: |
| 948 | messageID = str(selEntries[key]['Message']) |
| 949 | addDataPiece = selEntries[key]['AdditionalData'] |
| 950 | calloutIndex = 0 |
| 951 | calloutFound = False |
| 952 | for i in range(len(addDataPiece)): |
| 953 | if("CALLOUT_INVENTORY_PATH" in addDataPiece[i]): |
| 954 | calloutIndex = i |
| 955 | calloutFound = True |
| 956 | fruCallout = str(addDataPiece[calloutIndex]).split('=')[1] |
| 957 | if("CALLOUT_DEVICE_PATH" in addDataPiece[i]): |
| 958 | i2creadFail = True |
Matt Spinler | d178a47 | 2018-08-31 09:48:52 -0500 | [diff] [blame] | 959 | |
| 960 | fruCallout = str(addDataPiece[calloutIndex]).split('=')[1] |
| 961 | |
| 962 | # Fall back to "I2C"/"FSI" if dev path isn't in policy table |
| 963 | if (messageID + '||' + fruCallout) not in policyTable: |
| 964 | i2cdevice = str(addDataPiece[i]).strip().split('=')[1] |
| 965 | i2cdevice = '/'.join(i2cdevice.split('/')[-4:]) |
| 966 | if 'fsi' in str(addDataPiece[calloutIndex]).split('=')[1]: |
| 967 | fruCallout = 'FSI' |
| 968 | else: |
| 969 | fruCallout = 'I2C' |
Justin Thaler | e34c43a | 2018-05-25 19:37:55 -0500 | [diff] [blame] | 970 | calloutFound = True |
| 971 | if("CALLOUT_GPIO_NUM" in addDataPiece[i]): |
| 972 | if not calloutFound: |
| 973 | fruCallout = 'GPIO' |
| 974 | calloutFound = True |
| 975 | if("CALLOUT_IIC_BUS" in addDataPiece[i]): |
| 976 | if not calloutFound: |
| 977 | fruCallout = "I2C" |
| 978 | calloutFound = True |
| 979 | if("CALLOUT_IPMI_SENSOR_NUM" in addDataPiece[i]): |
| 980 | if not calloutFound: |
| 981 | fruCallout = "IPMI" |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 982 | calloutFound = True |
| 983 | if("ESEL" in addDataPiece[i]): |
| 984 | esel = str(addDataPiece[i]).strip().split('=')[1] |
Matt Spinler | 02d0dff | 2018-08-29 13:19:25 -0500 | [diff] [blame] | 985 | eselSeverity = getESELSeverity(esel) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 986 | if args.devdebug: |
| 987 | eselParts = parseESEL(args, esel) |
| 988 | hasEsel=True |
| 989 | if("GPU" in addDataPiece[i]): |
| 990 | fruCallout = '/xyz/openbmc_project/inventory/system/chassis/motherboard/gpu' + str(addDataPiece[i]).strip()[-1] |
| 991 | calloutFound = True |
| 992 | if("PROCEDURE" in addDataPiece[i]): |
| 993 | fruCallout = str(hex(int(str(addDataPiece[i]).split('=')[1])))[2:] |
| 994 | calloutFound = True |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 995 | if("RAIL_NAME" in addDataPiece[i]): |
| 996 | calloutFound=True |
| 997 | fruCallout = str(addDataPiece[i]).split('=')[1].strip() |
| 998 | if("INPUT_NAME" in addDataPiece[i]): |
| 999 | calloutFound=True |
| 1000 | fruCallout = str(addDataPiece[i]).split('=')[1].strip() |
| 1001 | if("SENSOR_TYPE" in addDataPiece[i]): |
| 1002 | calloutFound=True |
| 1003 | fruCallout = str(addDataPiece[i]).split('=')[1].strip() |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1004 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1005 | if(calloutFound): |
Justin Thaler | 667f87c | 2020-04-06 16:13:12 -0500 | [diff] [blame] | 1006 | if fruCallout.strip() != "": |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 1007 | policyKey = messageID +"||" + fruCallout |
Matt Spinler | 02d0dff | 2018-08-29 13:19:25 -0500 | [diff] [blame] | 1008 | |
| 1009 | # Also use the severity for hostboot errors |
| 1010 | if eselSeverity and messageID == 'org.open_power.Host.Error.Event': |
| 1011 | policyKey += '||' + eselSeverity |
| 1012 | |
| 1013 | # if not in the table, fall back to the original key |
| 1014 | if policyKey not in policyTable: |
| 1015 | policyKey = policyKey.replace('||'+eselSeverity, '') |
| 1016 | |
Justin Thaler | e34c43a | 2018-05-25 19:37:55 -0500 | [diff] [blame] | 1017 | if policyKey not in policyTable: |
| 1018 | policyKey = messageID |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 1019 | else: |
| 1020 | policyKey = messageID |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1021 | else: |
| 1022 | policyKey = messageID |
| 1023 | event = {} |
| 1024 | eventNum = str(count) |
| 1025 | if policyKey in policyTable: |
| 1026 | for pkey in policyTable[policyKey]: |
| 1027 | if(type(policyTable[policyKey][pkey])== bool): |
| 1028 | event[pkey] = boolToString(policyTable[policyKey][pkey]) |
| 1029 | else: |
| 1030 | if (i2creadFail and pkey == 'Message'): |
| 1031 | event[pkey] = policyTable[policyKey][pkey] + ' ' +i2cdevice |
| 1032 | else: |
| 1033 | event[pkey] = policyTable[policyKey][pkey] |
| 1034 | event['timestamp'] = selEntries[key]['Timestamp'] |
| 1035 | event['resolved'] = bool(selEntries[key]['Resolved']) |
| 1036 | if(hasEsel): |
| 1037 | if args.devdebug: |
| 1038 | event['eselParts'] = eselParts |
| 1039 | event['raweSEL'] = esel |
| 1040 | event['logNum'] = key.split('/')[-1] |
| 1041 | eventDict['event' + eventNum] = event |
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 | else: |
| 1044 | severity = str(selEntries[key]['Severity']).split('.')[-1] |
| 1045 | if severity == 'Error': |
| 1046 | severity = 'Critical' |
| 1047 | eventDict['event'+eventNum] = {} |
| 1048 | eventDict['event' + eventNum]['error'] = "error: Not found in policy table: " + policyKey |
| 1049 | eventDict['event' + eventNum]['timestamp'] = selEntries[key]['Timestamp'] |
| 1050 | eventDict['event' + eventNum]['Severity'] = severity |
| 1051 | if(hasEsel): |
| 1052 | if args.devdebug: |
| 1053 | eventDict['event' +eventNum]['eselParts'] = eselParts |
| 1054 | eventDict['event' +eventNum]['raweSEL'] = esel |
| 1055 | eventDict['event' +eventNum]['logNum'] = key.split('/')[-1] |
| 1056 | eventDict['event' +eventNum]['resolved'] = bool(selEntries[key]['Resolved']) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1057 | count += 1 |
| 1058 | return eventDict |
| 1059 | |
| 1060 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1061 | def selDisplay(events, args): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1062 | """ |
| 1063 | displays alerts in human readable format |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1064 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1065 | @param events: Dictionary containing events |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1066 | @return: |
| 1067 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1068 | activeAlerts = [] |
| 1069 | historyAlerts = [] |
| 1070 | sortedEntries = sortSELs(events) |
| 1071 | logNumList = sortedEntries[0] |
| 1072 | eventKeyDict = sortedEntries[1] |
| 1073 | keylist = ['Entry', 'ID', 'Timestamp', 'Serviceable', 'Severity','Message'] |
| 1074 | if(args.devdebug): |
| 1075 | colNames = ['Entry', 'ID', 'Timestamp', 'Serviceable', 'Severity','Message', 'eSEL contents'] |
| 1076 | keylist.append('eSEL') |
| 1077 | else: |
| 1078 | colNames = ['Entry', 'ID', 'Timestamp', 'Serviceable', 'Severity', 'Message'] |
| 1079 | for log in logNumList: |
| 1080 | selDict = {} |
| 1081 | alert = events[eventKeyDict[str(log)]] |
| 1082 | if('error' in alert): |
| 1083 | selDict['Entry'] = alert['logNum'] |
| 1084 | selDict['ID'] = 'Unknown' |
| 1085 | selDict['Timestamp'] = datetime.datetime.fromtimestamp(int(alert['timestamp']/1000)).strftime("%Y-%m-%d %H:%M:%S") |
| 1086 | msg = alert['error'] |
| 1087 | polMsg = msg.split("policy table:")[0] |
| 1088 | msg = msg.split("policy table:")[1] |
| 1089 | msgPieces = msg.split("||") |
| 1090 | err = msgPieces[0] |
| 1091 | if(err.find("org.open_power.")!=-1): |
| 1092 | err = err.split("org.open_power.")[1] |
| 1093 | elif(err.find("xyz.openbmc_project.")!=-1): |
| 1094 | err = err.split("xyz.openbmc_project.")[1] |
| 1095 | else: |
| 1096 | err = msgPieces[0] |
| 1097 | callout = "" |
| 1098 | if len(msgPieces) >1: |
| 1099 | callout = msgPieces[1] |
| 1100 | if(callout.find("/org/open_power/")!=-1): |
| 1101 | callout = callout.split("/org/open_power/")[1] |
| 1102 | elif(callout.find("/xyz/openbmc_project/")!=-1): |
| 1103 | callout = callout.split("/xyz/openbmc_project/")[1] |
| 1104 | else: |
| 1105 | callout = msgPieces[1] |
| 1106 | selDict['Message'] = polMsg +"policy table: "+ err + "||" + callout |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1107 | selDict['Serviceable'] = 'Unknown' |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1108 | selDict['Severity'] = alert['Severity'] |
| 1109 | else: |
| 1110 | selDict['Entry'] = alert['logNum'] |
| 1111 | selDict['ID'] = alert['CommonEventID'] |
| 1112 | 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] | 1113 | selDict['Message'] = alert['Message'] |
| 1114 | selDict['Serviceable'] = alert['Serviceable'] |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1115 | selDict['Severity'] = alert['Severity'] |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1116 | |
| 1117 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1118 | eselOrder = ['refCode','signatureDescription', 'eselType', 'devdesc', 'calloutType', 'procedure'] |
| 1119 | if ('eselParts' in alert and args.devdebug): |
| 1120 | eselOutput = "" |
| 1121 | for item in eselOrder: |
| 1122 | if item in alert['eselParts']: |
| 1123 | eselOutput = eselOutput + item + ": " + alert['eselParts'][item] + " | " |
| 1124 | selDict['eSEL'] = eselOutput |
| 1125 | else: |
| 1126 | if args.devdebug: |
| 1127 | selDict['eSEL'] = "None" |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1128 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1129 | if not alert['resolved']: |
| 1130 | activeAlerts.append(selDict) |
| 1131 | else: |
| 1132 | historyAlerts.append(selDict) |
| 1133 | mergedOutput = activeAlerts + historyAlerts |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1134 | colWidth = setColWidth(keylist, len(colNames), dict(enumerate(mergedOutput)), colNames) |
| 1135 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1136 | output = "" |
| 1137 | if(len(activeAlerts)>0): |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1138 | row = "" |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1139 | output +="----Active Alerts----\n" |
| 1140 | for i in range(0, len(colNames)): |
| 1141 | if i!=0: row =row + "| " |
| 1142 | row = row + colNames[i].ljust(colWidth[i]) |
| 1143 | output += row + "\n" |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1144 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1145 | for i in range(0,len(activeAlerts)): |
| 1146 | row = "" |
| 1147 | for j in range(len(activeAlerts[i])): |
| 1148 | if (j != 0): row = row + "| " |
| 1149 | row = row + activeAlerts[i][keylist[j]].ljust(colWidth[j]) |
| 1150 | output += row + "\n" |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1151 | |
| 1152 | if(len(historyAlerts)>0): |
| 1153 | row = "" |
| 1154 | output+= "----Historical Alerts----\n" |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1155 | for i in range(len(colNames)): |
| 1156 | if i!=0: row =row + "| " |
| 1157 | row = row + colNames[i].ljust(colWidth[i]) |
| 1158 | output += row + "\n" |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1159 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1160 | for i in range(0, len(historyAlerts)): |
| 1161 | row = "" |
| 1162 | for j in range(len(historyAlerts[i])): |
| 1163 | if (j != 0): row = row + "| " |
| 1164 | row = row + historyAlerts[i][keylist[j]].ljust(colWidth[j]) |
| 1165 | output += row + "\n" |
| 1166 | # print(events[eventKeyDict[str(log)]]) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1167 | return output |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1168 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1169 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1170 | def selPrint(host, args, session): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1171 | """ |
| 1172 | prints out all bmc alerts |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1173 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1174 | @param host: string, the hostname or IP address of the bmc |
| 1175 | @param args: contains additional arguments used by the fru sub command |
| 1176 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1177 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 1178 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1179 | if(args.policyTableLoc is None): |
| 1180 | if os.path.exists('policyTable.json'): |
| 1181 | ptableLoc = "policyTable.json" |
| 1182 | elif os.path.exists('/opt/ibm/ras/lib/policyTable.json'): |
| 1183 | ptableLoc = '/opt/ibm/ras/lib/policyTable.json' |
| 1184 | else: |
| 1185 | ptableLoc = 'lib/policyTable.json' |
| 1186 | else: |
| 1187 | ptableLoc = args.policyTableLoc |
| 1188 | policyTable = loadPolicyTable(ptableLoc) |
| 1189 | rawselEntries = "" |
| 1190 | if(hasattr(args, 'fileloc') and args.fileloc is not None): |
| 1191 | if os.path.exists(args.fileloc): |
| 1192 | with open(args.fileloc, 'r') as selFile: |
| 1193 | selLines = selFile.readlines() |
| 1194 | rawselEntries = ''.join(selLines) |
| 1195 | else: |
| 1196 | print("Error: File not found") |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1197 | sys.exit(1) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1198 | else: |
| 1199 | rawselEntries = sel(host, args, session) |
| 1200 | loadFailed = False |
| 1201 | try: |
| 1202 | selEntries = json.loads(rawselEntries) |
| 1203 | except ValueError: |
| 1204 | loadFailed = True |
| 1205 | if loadFailed: |
| 1206 | cleanSels = json.dumps(rawselEntries).replace('\\n', '') |
| 1207 | #need to load json twice as original content was string escaped a second time |
| 1208 | selEntries = json.loads(json.loads(cleanSels)) |
| 1209 | selEntries = selEntries['data'] |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1210 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1211 | if 'description' in selEntries: |
| 1212 | if(args.json): |
| 1213 | return("{\n\t\"numAlerts\": 0\n}") |
| 1214 | else: |
| 1215 | return("No log entries found") |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1216 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1217 | else: |
| 1218 | if(len(policyTable)>0): |
| 1219 | events = parseAlerts(policyTable, selEntries, args) |
| 1220 | if(args.json): |
| 1221 | events["numAlerts"] = len(events) |
| 1222 | retValue = str(json.dumps(events, sort_keys=True, indent=4, separators=(',', ': '), ensure_ascii=False)) |
| 1223 | return retValue |
| 1224 | elif(hasattr(args, 'fullSel')): |
| 1225 | return events |
| 1226 | else: |
| 1227 | #get log numbers to order event entries sequentially |
| 1228 | return selDisplay(events, args) |
| 1229 | else: |
| 1230 | if(args.json): |
| 1231 | return selEntries |
| 1232 | else: |
| 1233 | print("error: Policy Table not found.") |
| 1234 | return selEntries |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1235 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1236 | def selList(host, args, session): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1237 | """ |
| 1238 | 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] | 1239 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1240 | @param host: string, the hostname or IP address of the bmc |
| 1241 | @param args: contains additional arguments used by the fru sub command |
| 1242 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1243 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 1244 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1245 | return(sel(host, args, session)) |
| 1246 | |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1247 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1248 | def selClear(host, args, session): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1249 | """ |
| 1250 | clears all alerts |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1251 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1252 | @param host: string, the hostname or IP address of the bmc |
| 1253 | @param args: contains additional arguments used by the fru sub command |
| 1254 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1255 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 1256 | """ |
Matt Spinler | 47b13e9 | 2019-01-04 14:58:53 -0600 | [diff] [blame] | 1257 | url="https://"+host+"/xyz/openbmc_project/logging/action/DeleteAll" |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1258 | data = "{\"data\": [] }" |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1259 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1260 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 1261 | res = session.post(url, headers=jsonHeader, data=data, verify=False, timeout=baseTimeout) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1262 | except(requests.exceptions.Timeout): |
| 1263 | return(connectionErrHandler(args.json, "Timeout", None)) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1264 | if res.status_code == 200: |
| 1265 | return "The Alert Log has been cleared. Please allow a few minutes for the action to complete." |
| 1266 | else: |
| 1267 | print("Unable to clear the logs, trying to clear 1 at a time") |
| 1268 | sels = json.loads(sel(host, args, session))['data'] |
| 1269 | for key in sels: |
| 1270 | if 'callout' not in key: |
| 1271 | logNum = key.split('/')[-1] |
| 1272 | url = "https://"+ host+ "/xyz/openbmc_project/logging/entry/"+logNum+"/action/Delete" |
| 1273 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 1274 | session.post(url, headers=jsonHeader, data=data, verify=False, timeout=baseTimeout) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1275 | except(requests.exceptions.Timeout): |
| 1276 | return connectionErrHandler(args.json, "Timeout", None) |
| 1277 | sys.exit(1) |
| 1278 | except(requests.exceptions.ConnectionError) as err: |
| 1279 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 1280 | sys.exit(1) |
| 1281 | return ('Sel clearing complete') |
| 1282 | |
| 1283 | def selSetResolved(host, args, session): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1284 | """ |
| 1285 | sets a sel entry to resolved |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1286 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1287 | @param host: string, the hostname or IP address of the bmc |
| 1288 | @param args: contains additional arguments used by the fru sub command |
| 1289 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1290 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 1291 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1292 | url="https://"+host+"/xyz/openbmc_project/logging/entry/" + str(args.selNum) + "/attr/Resolved" |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1293 | data = "{\"data\": 1 }" |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1294 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 1295 | res = session.put(url, headers=jsonHeader, data=data, verify=False, timeout=baseTimeout) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1296 | except(requests.exceptions.Timeout): |
| 1297 | return(connectionErrHandler(args.json, "Timeout", None)) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1298 | if res.status_code == 200: |
| 1299 | return "Sel entry "+ str(args.selNum) +" is now set to resolved" |
| 1300 | else: |
| 1301 | return "Unable to set the alert to resolved" |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1302 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1303 | def selResolveAll(host, args, session): |
| 1304 | """ |
| 1305 | sets a sel entry to resolved |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1306 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1307 | @param host: string, the hostname or IP address of the bmc |
| 1308 | @param args: contains additional arguments used by the fru sub command |
| 1309 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1310 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 1311 | """ |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1312 | rawselEntries = sel(host, args, session) |
| 1313 | loadFailed = False |
| 1314 | try: |
| 1315 | selEntries = json.loads(rawselEntries) |
| 1316 | except ValueError: |
| 1317 | loadFailed = True |
| 1318 | if loadFailed: |
| 1319 | cleanSels = json.dumps(rawselEntries).replace('\\n', '') |
| 1320 | #need to load json twice as original content was string escaped a second time |
| 1321 | selEntries = json.loads(json.loads(cleanSels)) |
| 1322 | selEntries = selEntries['data'] |
| 1323 | |
| 1324 | if 'description' in selEntries: |
| 1325 | if(args.json): |
| 1326 | return("{\n\t\"selsResolved\": 0\n}") |
| 1327 | else: |
| 1328 | return("No log entries found") |
| 1329 | else: |
| 1330 | d = vars(args) |
| 1331 | successlist = [] |
| 1332 | failedlist = [] |
| 1333 | for key in selEntries: |
| 1334 | if 'callout' not in key: |
| 1335 | d['selNum'] = key.split('/')[-1] |
| 1336 | resolved = selSetResolved(host,args,session) |
| 1337 | if 'Sel entry' in resolved: |
| 1338 | successlist.append(d['selNum']) |
| 1339 | else: |
| 1340 | failedlist.append(d['selNum']) |
| 1341 | output = "" |
| 1342 | successlist.sort() |
| 1343 | failedlist.sort() |
| 1344 | if len(successlist)>0: |
| 1345 | output = "Successfully resolved: " +', '.join(successlist) +"\n" |
| 1346 | if len(failedlist)>0: |
| 1347 | output += "Failed to resolve: " + ', '.join(failedlist) + "\n" |
| 1348 | return output |
| 1349 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1350 | def chassisPower(host, args, session): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1351 | """ |
| 1352 | 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] | 1353 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1354 | @param host: string, the hostname or IP address of the bmc |
| 1355 | @param args: contains additional arguments used by the fru sub command |
| 1356 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1357 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 1358 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1359 | if(args.powcmd == 'on'): |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 1360 | if checkFWactivation(host, args, session): |
| 1361 | return ("Chassis Power control disabled during firmware activation") |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1362 | print("Attempting to Power on...:") |
| 1363 | url="https://"+host+"/xyz/openbmc_project/state/host0/attr/RequestedHostTransition" |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1364 | data = '{"data":"xyz.openbmc_project.State.Host.Transition.On"}' |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1365 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 1366 | res = session.put(url, headers=jsonHeader, data=data, verify=False, timeout=baseTimeout) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1367 | except(requests.exceptions.Timeout): |
| 1368 | return(connectionErrHandler(args.json, "Timeout", None)) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1369 | return res.text |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1370 | elif(args.powcmd == 'softoff'): |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 1371 | if checkFWactivation(host, args, session): |
| 1372 | return ("Chassis Power control disabled during firmware activation") |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1373 | print("Attempting to Power off gracefully...:") |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1374 | url="https://"+host+"/xyz/openbmc_project/state/host0/attr/RequestedHostTransition" |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1375 | data = '{"data":"xyz.openbmc_project.State.Host.Transition.Off"}' |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1376 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 1377 | res = session.put(url, headers=jsonHeader, data=data, verify=False, timeout=baseTimeout) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1378 | except(requests.exceptions.Timeout): |
| 1379 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 1380 | return res.text |
| 1381 | elif(args.powcmd == 'hardoff'): |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 1382 | if checkFWactivation(host, args, session): |
| 1383 | return ("Chassis Power control disabled during firmware activation") |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1384 | print("Attempting to Power off immediately...:") |
| 1385 | url="https://"+host+"/xyz/openbmc_project/state/chassis0/attr/RequestedPowerTransition" |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1386 | data = '{"data":"xyz.openbmc_project.State.Chassis.Transition.Off"}' |
| 1387 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 1388 | res = session.put(url, headers=jsonHeader, data=data, verify=False, timeout=baseTimeout) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1389 | except(requests.exceptions.Timeout): |
| 1390 | return(connectionErrHandler(args.json, "Timeout", None)) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1391 | return res.text |
| 1392 | elif(args.powcmd == 'status'): |
| 1393 | url="https://"+host+"/xyz/openbmc_project/state/chassis0/attr/CurrentPowerState" |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1394 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 1395 | res = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1396 | except(requests.exceptions.Timeout): |
| 1397 | return(connectionErrHandler(args.json, "Timeout", None)) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1398 | chassisState = json.loads(res.text)['data'].split('.')[-1] |
| 1399 | url="https://"+host+"/xyz/openbmc_project/state/host0/attr/CurrentHostState" |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1400 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 1401 | res = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1402 | except(requests.exceptions.Timeout): |
| 1403 | return(connectionErrHandler(args.json, "Timeout", None)) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1404 | hostState = json.loads(res.text)['data'].split('.')[-1] |
| 1405 | url="https://"+host+"/xyz/openbmc_project/state/bmc0/attr/CurrentBMCState" |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1406 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 1407 | res = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1408 | except(requests.exceptions.Timeout): |
| 1409 | return(connectionErrHandler(args.json, "Timeout", None)) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1410 | bmcState = json.loads(res.text)['data'].split('.')[-1] |
| 1411 | if(args.json): |
| 1412 | outDict = {"Chassis Power State" : chassisState, "Host Power State" : hostState, "BMC Power State":bmcState} |
| 1413 | return json.dumps(outDict, sort_keys=True, indent=4, separators=(',', ': '), ensure_ascii=False) |
| 1414 | else: |
| 1415 | return "Chassis Power State: " +chassisState + "\nHost Power State: " + hostState + "\nBMC Power State: " + bmcState |
| 1416 | else: |
| 1417 | return "Invalid chassis power command" |
| 1418 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1419 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1420 | def chassisIdent(host, args, session): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1421 | """ |
| 1422 | 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] | 1423 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1424 | @param host: string, the hostname or IP address of the bmc |
| 1425 | @param args: contains additional arguments used by the fru sub command |
| 1426 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1427 | @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] | 1428 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1429 | if(args.identcmd == 'on'): |
| 1430 | print("Attempting to turn identify light on...:") |
| 1431 | url="https://"+host+"/xyz/openbmc_project/led/groups/enclosure_identify/attr/Asserted" |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1432 | data = '{"data":true}' |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1433 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 1434 | res = session.put(url, headers=jsonHeader, data=data, verify=False, timeout=baseTimeout) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1435 | except(requests.exceptions.Timeout): |
| 1436 | return(connectionErrHandler(args.json, "Timeout", None)) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1437 | return res.text |
| 1438 | elif(args.identcmd == 'off'): |
| 1439 | print("Attempting to turn identify light off...:") |
| 1440 | url="https://"+host+"/xyz/openbmc_project/led/groups/enclosure_identify/attr/Asserted" |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1441 | data = '{"data":false}' |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1442 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 1443 | res = session.put(url, headers=jsonHeader, data=data, verify=False, timeout=baseTimeout) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1444 | except(requests.exceptions.Timeout): |
| 1445 | return(connectionErrHandler(args.json, "Timeout", None)) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1446 | return res.text |
| 1447 | elif(args.identcmd == 'status'): |
| 1448 | url="https://"+host+"/xyz/openbmc_project/led/groups/enclosure_identify" |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1449 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 1450 | res = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1451 | except(requests.exceptions.Timeout): |
| 1452 | return(connectionErrHandler(args.json, "Timeout", None)) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1453 | status = json.loads(res.text)['data'] |
| 1454 | if(args.json): |
| 1455 | return status |
| 1456 | else: |
| 1457 | if status['Asserted'] == 0: |
| 1458 | return "Identify light is off" |
| 1459 | else: |
| 1460 | return "Identify light is blinking" |
| 1461 | else: |
| 1462 | return "Invalid chassis identify command" |
| 1463 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1464 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1465 | def chassis(host, args, session): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1466 | """ |
| 1467 | controls the different chassis commands |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1468 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1469 | @param host: string, the hostname or IP address of the bmc |
| 1470 | @param args: contains additional arguments used by the fru sub command |
| 1471 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1472 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 1473 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1474 | if(hasattr(args, 'powcmd')): |
| 1475 | result = chassisPower(host,args,session) |
| 1476 | elif(hasattr(args, 'identcmd')): |
| 1477 | result = chassisIdent(host, args, session) |
| 1478 | else: |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 1479 | return "This feature is not yet implemented" |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1480 | return result |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1481 | |
Ravi Teja | d8be0b4 | 2020-03-18 14:31:46 -0500 | [diff] [blame] | 1482 | def dumpRetrieve(host, args, session): |
| 1483 | """ |
| 1484 | Downloads dump of given dump type |
| 1485 | |
| 1486 | @param host: string, the hostname or IP address of the bmc |
| 1487 | @param args: contains additional arguments used by the collectServiceData sub command |
| 1488 | @param session: the active session to use |
| 1489 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 1490 | """ |
| 1491 | dumpType = args.dumpType |
| 1492 | if (args.dumpType=="SystemDump"): |
| 1493 | dumpResp=systemDumpRetrieve(host,args,session) |
| 1494 | elif(args.dumpType=="bmc"): |
| 1495 | dumpResp=bmcDumpRetrieve(host,args,session) |
| 1496 | return dumpResp |
| 1497 | |
| 1498 | def dumpList(host, args, session): |
| 1499 | """ |
| 1500 | Lists dump of the given dump type |
| 1501 | |
| 1502 | @param host: string, the hostname or IP address of the bmc |
| 1503 | @param args: contains additional arguments used by the collectServiceData sub command |
| 1504 | @param session: the active session to use |
| 1505 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 1506 | """ |
| 1507 | if (args.dumpType=="SystemDump"): |
| 1508 | dumpResp=systemDumpList(host,args,session) |
| 1509 | elif(args.dumpType=="bmc"): |
| 1510 | dumpResp=bmcDumpList(host,args,session) |
| 1511 | return dumpResp |
| 1512 | |
| 1513 | def dumpDelete(host, args, session): |
| 1514 | """ |
| 1515 | Deletes dump of the given dump type |
| 1516 | |
| 1517 | @param host: string, the hostname or IP address of the bmc |
| 1518 | @param args: contains additional arguments used by the collectServiceData sub command |
| 1519 | @param session: the active session to use |
| 1520 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 1521 | """ |
| 1522 | if (args.dumpType=="SystemDump"): |
| 1523 | dumpResp=systemDumpDelete(host,args,session) |
| 1524 | elif(args.dumpType=="bmc"): |
| 1525 | dumpResp=bmcDumpDelete(host,args,session) |
| 1526 | return dumpResp |
| 1527 | |
| 1528 | def dumpDeleteAll(host, args, session): |
| 1529 | """ |
| 1530 | Deletes all dumps of the given dump type |
| 1531 | |
| 1532 | @param host: string, the hostname or IP address of the bmc |
| 1533 | @param args: contains additional arguments used by the collectServiceData sub command |
| 1534 | @param session: the active session to use |
| 1535 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 1536 | """ |
| 1537 | if (args.dumpType=="SystemDump"): |
| 1538 | dumpResp=systemDumpDeleteAll(host,args,session) |
| 1539 | elif(args.dumpType=="bmc"): |
| 1540 | dumpResp=bmcDumpDeleteAll(host,args,session) |
| 1541 | return dumpResp |
| 1542 | |
| 1543 | def dumpCreate(host, args, session): |
| 1544 | """ |
| 1545 | Creates dump for the given dump type |
| 1546 | |
| 1547 | @param host: string, the hostname or IP address of the bmc |
| 1548 | @param args: contains additional arguments used by the collectServiceData sub command |
| 1549 | @param session: the active session to use |
| 1550 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 1551 | """ |
| 1552 | if (args.dumpType=="SystemDump"): |
| 1553 | dumpResp=systemDumpCreate(host,args,session) |
| 1554 | elif(args.dumpType=="bmc"): |
Justin Thaler | 0a3e169 | 2020-04-07 19:10:40 -0500 | [diff] [blame] | 1555 | dumpResp=bmcDumpCreate(host,args,session) |
Ravi Teja | d8be0b4 | 2020-03-18 14:31:46 -0500 | [diff] [blame] | 1556 | return dumpResp |
| 1557 | |
| 1558 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1559 | def bmcDumpRetrieve(host, args, session): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1560 | """ |
| 1561 | Downloads a dump file from the bmc |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1562 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1563 | @param host: string, the hostname or IP address of the bmc |
| 1564 | @param args: contains additional arguments used by the collectServiceData sub command |
| 1565 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1566 | @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] | 1567 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1568 | dumpNum = args.dumpNum |
| 1569 | if (args.dumpSaveLoc is not None): |
| 1570 | saveLoc = args.dumpSaveLoc |
| 1571 | else: |
Justin Thaler | cf1deae | 2018-05-25 19:35:21 -0500 | [diff] [blame] | 1572 | saveLoc = tempfile.gettempdir() |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1573 | url ='https://'+host+'/download/dump/' + str(dumpNum) |
| 1574 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 1575 | r = session.get(url, headers=jsonHeader, stream=True, verify=False, timeout=baseTimeout) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1576 | if (args.dumpSaveLoc is not None): |
| 1577 | if os.path.exists(saveLoc): |
| 1578 | if saveLoc[-1] != os.path.sep: |
| 1579 | saveLoc = saveLoc + os.path.sep |
| 1580 | filename = saveLoc + host+'-dump' + str(dumpNum) + '.tar.xz' |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1581 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1582 | else: |
| 1583 | return 'Invalid save location specified' |
| 1584 | else: |
Justin Thaler | cf1deae | 2018-05-25 19:35:21 -0500 | [diff] [blame] | 1585 | filename = tempfile.gettempdir()+os.sep + host+'-dump' + str(dumpNum) + '.tar.xz' |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1586 | |
| 1587 | with open(filename, 'wb') as f: |
| 1588 | for chunk in r.iter_content(chunk_size =1024): |
| 1589 | if chunk: |
| 1590 | f.write(chunk) |
| 1591 | return 'Saved as ' + filename |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1592 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1593 | except(requests.exceptions.Timeout): |
| 1594 | return connectionErrHandler(args.json, "Timeout", None) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1595 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1596 | except(requests.exceptions.ConnectionError) as err: |
| 1597 | return connectionErrHandler(args.json, "ConnectionError", err) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1598 | |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1599 | def bmcDumpList(host, args, session): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1600 | """ |
| 1601 | Lists the number of dump files on the bmc |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1602 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1603 | @param host: string, the hostname or IP address of the bmc |
| 1604 | @param args: contains additional arguments used by the collectServiceData sub command |
| 1605 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1606 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 1607 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1608 | url ='https://'+host+'/xyz/openbmc_project/dump/list' |
| 1609 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 1610 | r = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
Justin Thaler | 3a5771b | 2019-01-23 14:31:52 -0600 | [diff] [blame] | 1611 | dumpList = r.json() |
Justin Thaler | 3b3c658 | 2020-04-07 19:17:36 -0500 | [diff] [blame] | 1612 | formattedList = [] |
| 1613 | #remove items that aren't dump entries 'entry, internal, manager endpoints' |
| 1614 | if 'data' in dumpList: |
| 1615 | for entry in dumpList['data']: |
| 1616 | if 'entry' in entry: |
| 1617 | if entry.split('/')[-1].isnumeric(): |
| 1618 | formattedList.append(entry) |
| 1619 | dumpList['data']= formattedList |
Justin Thaler | 3a5771b | 2019-01-23 14:31:52 -0600 | [diff] [blame] | 1620 | return dumpList |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1621 | except(requests.exceptions.Timeout): |
| 1622 | return connectionErrHandler(args.json, "Timeout", None) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1623 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1624 | except(requests.exceptions.ConnectionError) as err: |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1625 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 1626 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1627 | def bmcDumpDelete(host, args, session): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1628 | """ |
| 1629 | Deletes BMC dump files from the bmc |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1630 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1631 | @param host: string, the hostname or IP address of the bmc |
| 1632 | @param args: contains additional arguments used by the collectServiceData sub command |
| 1633 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1634 | @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] | 1635 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1636 | dumpList = [] |
| 1637 | successList = [] |
| 1638 | failedList = [] |
| 1639 | if args.dumpNum is not None: |
| 1640 | if isinstance(args.dumpNum, list): |
| 1641 | dumpList = args.dumpNum |
| 1642 | else: |
| 1643 | dumpList.append(args.dumpNum) |
| 1644 | for dumpNum in dumpList: |
| 1645 | url ='https://'+host+'/xyz/openbmc_project/dump/entry/'+str(dumpNum)+'/action/Delete' |
| 1646 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 1647 | r = session.post(url, headers=jsonHeader, json = {"data": []}, verify=False, timeout=baseTimeout) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1648 | if r.status_code == 200: |
| 1649 | successList.append(str(dumpNum)) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1650 | else: |
| 1651 | failedList.append(str(dumpNum)) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1652 | except(requests.exceptions.Timeout): |
| 1653 | return connectionErrHandler(args.json, "Timeout", None) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1654 | except(requests.exceptions.ConnectionError) as err: |
| 1655 | return connectionErrHandler(args.json, "ConnectionError", err) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1656 | output = "Successfully deleted dumps: " + ', '.join(successList) |
| 1657 | if(len(failedList)>0): |
| 1658 | output+= '\nFailed to delete dumps: ' + ', '.join(failedList) |
| 1659 | return output |
| 1660 | else: |
| 1661 | return 'You must specify an entry number to delete' |
| 1662 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1663 | def bmcDumpDeleteAll(host, args, session): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1664 | """ |
| 1665 | Deletes All BMC dump files from the bmc |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1666 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1667 | @param host: string, the hostname or IP address of the bmc |
| 1668 | @param args: contains additional arguments used by the collectServiceData sub command |
| 1669 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1670 | @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] | 1671 | """ |
| 1672 | dumpResp = bmcDumpList(host, args, session) |
| 1673 | if 'FQPSPIN0000M' in dumpResp or 'FQPSPIN0001M'in dumpResp: |
| 1674 | return dumpResp |
Justin Thaler | 3a5771b | 2019-01-23 14:31:52 -0600 | [diff] [blame] | 1675 | dumpList = dumpResp['data'] |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1676 | d = vars(args) |
| 1677 | dumpNums = [] |
| 1678 | for dump in dumpList: |
Alvin Wang | 28bd09d | 2019-10-28 13:23:58 +0800 | [diff] [blame] | 1679 | dumpNum = dump.strip().split('/')[-1] |
| 1680 | if dumpNum.isdigit(): |
| 1681 | dumpNums.append(int(dumpNum)) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1682 | d['dumpNum'] = dumpNums |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1683 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1684 | return bmcDumpDelete(host, args, session) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1685 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1686 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1687 | def bmcDumpCreate(host, args, session): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1688 | """ |
| 1689 | Creates a bmc dump file |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1690 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1691 | @param host: string, the hostname or IP address of the bmc |
| 1692 | @param args: contains additional arguments used by the collectServiceData sub command |
| 1693 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1694 | @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] | 1695 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1696 | url = 'https://'+host+'/xyz/openbmc_project/dump/action/CreateDump' |
| 1697 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 1698 | r = session.post(url, headers=jsonHeader, json = {"data": []}, verify=False, timeout=baseTimeout) |
Justin Thaler | 2f59aea | 2020-04-07 19:27:01 -0500 | [diff] [blame] | 1699 | info = r.json() |
Matt Spinler | eae05b0 | 2019-01-24 12:59:34 -0600 | [diff] [blame] | 1700 | if(r.status_code == 200 and not args.json): |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1701 | return ('Dump successfully created') |
Justin Thaler | 3a5771b | 2019-01-23 14:31:52 -0600 | [diff] [blame] | 1702 | elif(args.json): |
Justin Thaler | 2f59aea | 2020-04-07 19:27:01 -0500 | [diff] [blame] | 1703 | return info |
| 1704 | elif 'data' in info: |
| 1705 | if 'QuotaExceeded' in info['data']['description']: |
| 1706 | return 'BMC dump space is full. Please delete at least one existing dump entry and try again.' |
| 1707 | else: |
| 1708 | return "Failed to create a BMC dump. BMC Response:\n {resp}".format(resp=info) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1709 | else: |
Justin Thaler | 2f59aea | 2020-04-07 19:27:01 -0500 | [diff] [blame] | 1710 | return "Failed to create a BMC dump. BMC Response:\n {resp}".format(resp=info) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1711 | except(requests.exceptions.Timeout): |
| 1712 | return connectionErrHandler(args.json, "Timeout", None) |
| 1713 | except(requests.exceptions.ConnectionError) as err: |
| 1714 | return connectionErrHandler(args.json, "ConnectionError", err) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1715 | |
Justin Thaler | 2f59aea | 2020-04-07 19:27:01 -0500 | [diff] [blame] | 1716 | |
Ravi Teja | d8be0b4 | 2020-03-18 14:31:46 -0500 | [diff] [blame] | 1717 | def systemDumpRetrieve(host, args, session): |
| 1718 | """ |
| 1719 | Downloads system dump |
| 1720 | |
| 1721 | @param host: string, the hostname or IP address of the bmc |
| 1722 | @param args: contains additional arguments used by the collectServiceData sub command |
| 1723 | @param session: the active session to use |
| 1724 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 1725 | """ |
| 1726 | NBDSetup(host,args,session) |
| 1727 | pipe = NBDPipe() |
| 1728 | pipe.openHTTPSocket(args) |
| 1729 | pipe.openTCPSocket() |
| 1730 | pipe.waitformessage() |
| 1731 | |
| 1732 | def systemDumpList(host, args, session): |
| 1733 | """ |
| 1734 | Lists system dumps |
| 1735 | |
| 1736 | @param host: string, the hostname or IP address of the bmc |
| 1737 | @param args: contains additional arguments used by the collectServiceData sub command |
| 1738 | @param session: the active session to use |
| 1739 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 1740 | """ |
| 1741 | print("in systemDumpList") |
| 1742 | url = "https://"+host+"/redfish/v1/Systems/system/LogServices/"+args.dumpType+"/Entries" |
| 1743 | try: |
| 1744 | r = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
| 1745 | dumpList = r.json() |
| 1746 | return dumpList |
| 1747 | except(requests.exceptions.Timeout): |
| 1748 | return connectionErrHandler(args.json, "Timeout", None) |
| 1749 | |
| 1750 | except(requests.exceptions.ConnectionError) as err: |
| 1751 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 1752 | |
| 1753 | |
| 1754 | def systemDumpDelete(host, args, session): |
| 1755 | """ |
| 1756 | Deletes system dump |
| 1757 | |
| 1758 | @param host: string, the hostname or IP address of the bmc |
| 1759 | @param args: contains additional arguments used by the collectServiceData sub command |
| 1760 | @param session: the active session to use |
| 1761 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 1762 | """ |
| 1763 | dumpList = [] |
| 1764 | successList = [] |
| 1765 | failedList = [] |
| 1766 | if args.dumpNum is not None: |
| 1767 | if isinstance(args.dumpNum, list): |
| 1768 | dumpList = args.dumpNum |
| 1769 | else: |
| 1770 | dumpList.append(args.dumpNum) |
| 1771 | for dumpNum in dumpList: |
| 1772 | url = 'https://'+host+'/redfish/v1/Systems/system/LogServices/'+args.dumpType+'/Entries/'+ str(dumpNum) |
| 1773 | try: |
| 1774 | r = session.delete(url, headers=jsonHeader, json = {"data": []}, verify=False, timeout=baseTimeout) |
| 1775 | if r.status_code == 200: |
| 1776 | successList.append(str(dumpNum)) |
| 1777 | else: |
| 1778 | failedList.append(str(dumpNum)) |
| 1779 | except(requests.exceptions.Timeout): |
| 1780 | return connectionErrHandler(args.json, "Timeout", None) |
| 1781 | except(requests.exceptions.ConnectionError) as err: |
| 1782 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 1783 | output = "Successfully deleted dumps: " + ', '.join(successList) |
| 1784 | if(len(failedList)>0): |
| 1785 | output+= '\nFailed to delete dumps: ' + ', '.join(failedList) |
| 1786 | return output |
| 1787 | else: |
| 1788 | return 'You must specify an entry number to delete' |
| 1789 | |
| 1790 | def systemDumpDeleteAll(host, args, session): |
| 1791 | """ |
| 1792 | Deletes All system dumps |
| 1793 | |
| 1794 | @param host: string, the hostname or IP address of the bmc |
| 1795 | @param args: contains additional arguments used by the collectServiceData sub command |
| 1796 | @param session: the active session to use |
| 1797 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 1798 | """ |
| 1799 | url = 'https://'+host+'/redfish/v1/Systems/system/LogServices/'+args.dumpType+'/Actions/LogService.ClearLog' |
| 1800 | try: |
| 1801 | r = session.post(url, headers=jsonHeader, json = {"data": []}, verify=False, timeout=baseTimeout) |
| 1802 | if(r.status_code == 200 and not args.json): |
| 1803 | return ('Dumps successfully cleared') |
| 1804 | elif(args.json): |
| 1805 | return r.json() |
| 1806 | else: |
| 1807 | return ('Failed to clear dumps') |
| 1808 | except(requests.exceptions.Timeout): |
| 1809 | return connectionErrHandler(args.json, "Timeout", None) |
| 1810 | except(requests.exceptions.ConnectionError) as err: |
| 1811 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 1812 | |
| 1813 | def systemDumpCreate(host, args, session): |
| 1814 | """ |
| 1815 | Creates a system dump |
| 1816 | |
| 1817 | @param host: string, the hostname or IP address of the bmc |
| 1818 | @param args: contains additional arguments used by the collectServiceData sub command |
| 1819 | @param session: the active session to use |
| 1820 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 1821 | """ |
| 1822 | url = 'https://'+host+'/redfish/v1/Systems/system/LogServices/'+args.dumpType+'/Actions/Oem/Openbmc/LogService.CreateLog' |
| 1823 | try: |
| 1824 | r = session.post(url, headers=jsonHeader, json = {"data": []}, verify=False, timeout=baseTimeout) |
| 1825 | if(r.status_code == 200 and not args.json): |
| 1826 | return ('Dump successfully created') |
| 1827 | elif(args.json): |
| 1828 | return r.json() |
| 1829 | else: |
| 1830 | return ('Failed to create dump') |
| 1831 | except(requests.exceptions.Timeout): |
| 1832 | return connectionErrHandler(args.json, "Timeout", None) |
| 1833 | except(requests.exceptions.ConnectionError) as err: |
| 1834 | return connectionErrHandler(args.json, "ConnectionError", err) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1835 | |
Justin Thaler | 666cf34 | 2019-01-23 14:44:27 -0600 | [diff] [blame] | 1836 | def csdDumpInitiate(host, args, session): |
| 1837 | """ |
| 1838 | Starts the process of getting the current list of dumps then initiates the creation of one. |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1839 | |
Justin Thaler | 666cf34 | 2019-01-23 14:44:27 -0600 | [diff] [blame] | 1840 | @param host: string, the hostname or IP address of the bmc |
| 1841 | @param args: contains additional arguments used by the collectServiceData sub command |
| 1842 | @param session: the active session to use |
| 1843 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 1844 | """ |
| 1845 | errorInfo = "" |
| 1846 | dumpcount = 0 |
| 1847 | try: |
| 1848 | d = vars(args) |
| 1849 | d['json'] = True |
| 1850 | except Exception as e: |
| 1851 | errorInfo += "Failed to set the json flag to True \n Exception: {eInfo}\n".format(eInfo=e) |
Justin Thaler | b425667 | 2020-04-07 19:38:26 -0500 | [diff] [blame] | 1852 | exc_type, exc_obj, exc_tb = sys.exc_info() |
| 1853 | fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] |
| 1854 | errorInfo += "Exception: Error: {err}, Details: {etype}, {fname}, {lineno}\n".format(err=e, etype=exc_type, fname=fname, lineno=exc_tb.tb_lineno) |
| 1855 | errorInfo += traceback.format_exc() |
Justin Thaler | 666cf34 | 2019-01-23 14:44:27 -0600 | [diff] [blame] | 1856 | |
| 1857 | try: |
| 1858 | for i in range(3): |
| 1859 | dumpInfo = bmcDumpList(host, args, session) |
| 1860 | if 'data' in dumpInfo: |
| 1861 | dumpcount = len(dumpInfo['data']) |
| 1862 | break |
| 1863 | else: |
| 1864 | errorInfo+= "Dump List Message returned: " + json.dumps(dumpInfo,indent=0, separators=(',', ':')).replace('\n','') +"\n" |
| 1865 | except Exception as e: |
| 1866 | errorInfo+= "Failed to collect the list of dumps.\nException: {eInfo}\n".format(eInfo=e) |
Justin Thaler | b425667 | 2020-04-07 19:38:26 -0500 | [diff] [blame] | 1867 | exc_type, exc_obj, exc_tb = sys.exc_info() |
| 1868 | fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] |
| 1869 | errorInfo += "Exception: Error: {err}, Details: {etype}, {fname}, {lineno}\n".format(err=e, etype=exc_type, fname=fname, lineno=exc_tb.tb_lineno) |
| 1870 | errorInfo += traceback.format_exc() |
Justin Thaler | 666cf34 | 2019-01-23 14:44:27 -0600 | [diff] [blame] | 1871 | |
| 1872 | #Create a user initiated dump |
Justin Thaler | b425667 | 2020-04-07 19:38:26 -0500 | [diff] [blame] | 1873 | dumpFailure = True |
Justin Thaler | 666cf34 | 2019-01-23 14:44:27 -0600 | [diff] [blame] | 1874 | try: |
| 1875 | for i in range(3): |
| 1876 | dumpcreated = bmcDumpCreate(host, args, session) |
| 1877 | if 'message' in dumpcreated: |
| 1878 | if 'ok' in dumpcreated['message'].lower(): |
Justin Thaler | b425667 | 2020-04-07 19:38:26 -0500 | [diff] [blame] | 1879 | dumpFailure = False |
Justin Thaler | 666cf34 | 2019-01-23 14:44:27 -0600 | [diff] [blame] | 1880 | break |
Justin Thaler | b425667 | 2020-04-07 19:38:26 -0500 | [diff] [blame] | 1881 | elif 'data' in dumpcreated: |
| 1882 | if 'QuotaExceeded' in dumpcreated['data']['description']: |
| 1883 | print('Not enough dump space on the BMC to create a new dump. Please delete the oldest entry (lowest number) and rerun the collect_service_data command.') |
| 1884 | errorInfo+='Dump Space is full. No new dump was created with this collection' |
| 1885 | break |
| 1886 | else: |
| 1887 | errorInfo+= "Dump create message returned: " + json.dumps(dumpcreated,indent=0, separators=(',', ':')).replace('\n','') +"\n" |
Justin Thaler | 666cf34 | 2019-01-23 14:44:27 -0600 | [diff] [blame] | 1888 | else: |
Justin Thaler | b425667 | 2020-04-07 19:38:26 -0500 | [diff] [blame] | 1889 | errorInfo+= "Dump create message returned: " + json.dumps(dumpcreated,indent=0, separators=(',', ':')).replace('\n','') +"\n" |
Justin Thaler | 666cf34 | 2019-01-23 14:44:27 -0600 | [diff] [blame] | 1890 | else: |
Justin Thaler | b425667 | 2020-04-07 19:38:26 -0500 | [diff] [blame] | 1891 | errorInfo+= "Dump create message returned: " + json.dumps(dumpcreated,indent=0, separators=(',', ':')).replace('\n','') +"\n" |
Justin Thaler | 666cf34 | 2019-01-23 14:44:27 -0600 | [diff] [blame] | 1892 | except Exception as e: |
| 1893 | errorInfo+= "Dump create exception encountered: {eInfo}\n".format(eInfo=e) |
Justin Thaler | b425667 | 2020-04-07 19:38:26 -0500 | [diff] [blame] | 1894 | exc_type, exc_obj, exc_tb = sys.exc_info() |
| 1895 | fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] |
| 1896 | errorInfo += "Exception: Error: {err}, Details: {etype}, {fname}, {lineno}\n".format(err=e, etype=exc_type, fname=fname, lineno=exc_tb.tb_lineno) |
| 1897 | errorInfo += traceback.format_exc() |
Justin Thaler | 666cf34 | 2019-01-23 14:44:27 -0600 | [diff] [blame] | 1898 | |
| 1899 | output = {} |
| 1900 | output['errors'] = errorInfo |
| 1901 | output['dumpcount'] = dumpcount |
Justin Thaler | b425667 | 2020-04-07 19:38:26 -0500 | [diff] [blame] | 1902 | if dumpFailure: output['dumpFailure'] = True |
Justin Thaler | 666cf34 | 2019-01-23 14:44:27 -0600 | [diff] [blame] | 1903 | return output |
| 1904 | |
| 1905 | def csdInventory(host, args,session, fileDir): |
| 1906 | """ |
| 1907 | Collects the BMC inventory, retrying if necessary |
| 1908 | |
| 1909 | @param host: string, the hostname or IP address of the bmc |
| 1910 | @param args: contains additional arguments used by the collectServiceData sub command |
| 1911 | @param session: the active session to use |
| 1912 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 1913 | @param fileDir: string representation of the path to use for putting files created |
| 1914 | """ |
| 1915 | errorInfo = "===========Inventory =============\n" |
| 1916 | output={} |
| 1917 | inventoryCollected = False |
| 1918 | try: |
| 1919 | for i in range(3): |
| 1920 | frulist = fruPrint(host, args, session) |
| 1921 | if 'Hardware' in frulist: |
| 1922 | inventoryCollected = True |
| 1923 | break |
| 1924 | else: |
| 1925 | errorInfo += json.dumps(frulist, sort_keys=True, indent=4, separators=(',', ': '), ensure_ascii=False) + '\n' |
| 1926 | except Exception as e: |
| 1927 | errorInfo += "Inventory collection exception: {eInfo}\n".format(eInfo=e) |
Justin Thaler | b425667 | 2020-04-07 19:38:26 -0500 | [diff] [blame] | 1928 | exc_type, exc_obj, exc_tb = sys.exc_info() |
| 1929 | fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] |
| 1930 | errorInfo += "Exception: Error: {err}, Details: {etype}, {fname}, {lineno}\n".format(err=e, etype=exc_type, fname=fname, lineno=exc_tb.tb_lineno) |
| 1931 | errorInfo += traceback.format_exc() |
Justin Thaler | 666cf34 | 2019-01-23 14:44:27 -0600 | [diff] [blame] | 1932 | if inventoryCollected: |
| 1933 | try: |
| 1934 | with open(fileDir +os.sep+'inventory.txt', 'w') as f: |
| 1935 | f.write(json.dumps(frulist, sort_keys=True, indent=4, separators=(',', ': '), ensure_ascii=False) + '\n') |
| 1936 | print("Inventory collected and stored in " + fileDir + os.sep + "inventory.txt") |
| 1937 | output['fileLoc'] = fileDir+os.sep+'inventory.txt' |
| 1938 | except Exception as e: |
| 1939 | print("Failed to write inventory to file.") |
| 1940 | errorInfo += "Error writing inventory to the file. Exception: {eInfo}\n".format(eInfo=e) |
Justin Thaler | b425667 | 2020-04-07 19:38:26 -0500 | [diff] [blame] | 1941 | exc_type, exc_obj, exc_tb = sys.exc_info() |
| 1942 | fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] |
| 1943 | errorInfo += "Exception: Error: {err}, Details: {etype}, {fname}, {lineno}\n".format(err=e, etype=exc_type, fname=fname, lineno=exc_tb.tb_lineno) |
| 1944 | errorInfo += traceback.format_exc() |
Justin Thaler | 666cf34 | 2019-01-23 14:44:27 -0600 | [diff] [blame] | 1945 | |
| 1946 | output['errors'] = errorInfo |
| 1947 | |
| 1948 | return output |
| 1949 | |
| 1950 | def csdSensors(host, args,session, fileDir): |
| 1951 | """ |
| 1952 | Collects the BMC sensor readings, retrying if necessary |
| 1953 | |
| 1954 | @param host: string, the hostname or IP address of the bmc |
| 1955 | @param args: contains additional arguments used by the collectServiceData sub command |
| 1956 | @param session: the active session to use |
| 1957 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 1958 | @param fileDir: string representation of the path to use for putting files created |
| 1959 | """ |
| 1960 | errorInfo = "===========Sensors =============\n" |
| 1961 | sensorsCollected = False |
| 1962 | output={} |
| 1963 | try: |
| 1964 | d = vars(args) |
| 1965 | d['json'] = False |
| 1966 | except Exception as e: |
| 1967 | errorInfo += "Failed to set the json flag to False \n Exception: {eInfo}\n".format(eInfo=e) |
Justin Thaler | b425667 | 2020-04-07 19:38:26 -0500 | [diff] [blame] | 1968 | exc_type, exc_obj, exc_tb = sys.exc_info() |
| 1969 | fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] |
| 1970 | errorInfo += "Exception: Error: {err}, Details: {etype}, {fname}, {lineno}\n".format(err=e, etype=exc_type, fname=fname, lineno=exc_tb.tb_lineno) |
| 1971 | errorInfo += traceback.format_exc() |
Justin Thaler | 666cf34 | 2019-01-23 14:44:27 -0600 | [diff] [blame] | 1972 | |
| 1973 | try: |
| 1974 | for i in range(3): |
| 1975 | sensorReadings = sensor(host, args, session) |
| 1976 | if 'OCC0' in sensorReadings: |
| 1977 | sensorsCollected = True |
| 1978 | break |
| 1979 | else: |
| 1980 | errorInfo += sensorReadings |
| 1981 | except Exception as e: |
| 1982 | errorInfo += "Sensor reading collection exception: {eInfo}\n".format(eInfo=e) |
Justin Thaler | b425667 | 2020-04-07 19:38:26 -0500 | [diff] [blame] | 1983 | exc_type, exc_obj, exc_tb = sys.exc_info() |
| 1984 | fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] |
| 1985 | errorInfo += "Exception: Error: {err}, Details: {etype}, {fname}, {lineno}\n".format(err=e, etype=exc_type, fname=fname, lineno=exc_tb.tb_lineno) |
| 1986 | errorInfo += traceback.format_exc() |
Justin Thaler | 666cf34 | 2019-01-23 14:44:27 -0600 | [diff] [blame] | 1987 | if sensorsCollected: |
| 1988 | try: |
| 1989 | with open(fileDir +os.sep+'sensorReadings.txt', 'w') as f: |
| 1990 | f.write(sensorReadings) |
| 1991 | print("Sensor readings collected and stored in " + fileDir + os.sep+ "sensorReadings.txt") |
| 1992 | output['fileLoc'] = fileDir+os.sep+'sensorReadings.txt' |
| 1993 | except Exception as e: |
| 1994 | print("Failed to write sensor readings to file system.") |
| 1995 | errorInfo += "Error writing sensor readings to the file. Exception: {eInfo}\n".format(eInfo=e) |
Justin Thaler | b425667 | 2020-04-07 19:38:26 -0500 | [diff] [blame] | 1996 | exc_type, exc_obj, exc_tb = sys.exc_info() |
| 1997 | fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] |
| 1998 | errorInfo += "Exception: Error: {err}, Details: {etype}, {fname}, {lineno}\n".format(err=e, etype=exc_type, fname=fname, lineno=exc_tb.tb_lineno) |
| 1999 | errorInfo += traceback.format_exc() |
Justin Thaler | 666cf34 | 2019-01-23 14:44:27 -0600 | [diff] [blame] | 2000 | |
| 2001 | output['errors'] = errorInfo |
| 2002 | return output |
| 2003 | |
| 2004 | def csdLEDs(host,args, session, fileDir): |
| 2005 | """ |
| 2006 | Collects the BMC LED status, retrying if necessary |
| 2007 | |
| 2008 | @param host: string, the hostname or IP address of the bmc |
| 2009 | @param args: contains additional arguments used by the collectServiceData sub command |
| 2010 | @param session: the active session to use |
| 2011 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 2012 | @param fileDir: string representation of the path to use for putting files created |
| 2013 | """ |
| 2014 | errorInfo = "===========LEDs =============\n" |
| 2015 | ledsCollected = False |
| 2016 | output={} |
| 2017 | try: |
| 2018 | d = vars(args) |
| 2019 | d['json'] = True |
| 2020 | except Exception as e: |
| 2021 | errorInfo += "Failed to set the json flag to False \n Exception: {eInfo}\n".format(eInfo=e) |
Justin Thaler | b425667 | 2020-04-07 19:38:26 -0500 | [diff] [blame] | 2022 | exc_type, exc_obj, exc_tb = sys.exc_info() |
| 2023 | fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] |
| 2024 | errorInfo += "Exception: Error: {err}, Details: {etype}, {fname}, {lineno}\n".format(err=e, etype=exc_type, fname=fname, lineno=exc_tb.tb_lineno) |
| 2025 | errorInfo += traceback.format_exc() |
Justin Thaler | 666cf34 | 2019-01-23 14:44:27 -0600 | [diff] [blame] | 2026 | try: |
| 2027 | url="https://"+host+"/xyz/openbmc_project/led/enumerate" |
| 2028 | httpHeader = {'Content-Type':'application/json'} |
| 2029 | for i in range(3): |
| 2030 | try: |
| 2031 | ledRes = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
| 2032 | if ledRes.status_code == 200: |
| 2033 | ledsCollected = True |
| 2034 | leds = ledRes.json()['data'] |
| 2035 | break |
| 2036 | else: |
| 2037 | errorInfo += ledRes.text |
| 2038 | except(requests.exceptions.Timeout): |
| 2039 | errorInfo+=json.dumps( connectionErrHandler(args.json, "Timeout", None), sort_keys=True, indent=4, separators=(',', ': '), ensure_ascii=False) + '\n' |
| 2040 | except(requests.exceptions.ConnectionError) as err: |
| 2041 | errorInfo += json.dumps(connectionErrHandler(args.json, "ConnectionError", err), sort_keys=True, indent=4, separators=(',', ': '), ensure_ascii=False) + '\n' |
Justin Thaler | b425667 | 2020-04-07 19:38:26 -0500 | [diff] [blame] | 2042 | exc_type, exc_obj, exc_tb = sys.exc_info() |
| 2043 | fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] |
| 2044 | errorInfo += "Exception: Error: {err}, Details: {etype}, {fname}, {lineno}\n".format(err=e, etype=exc_type, fname=fname, lineno=exc_tb.tb_lineno) |
| 2045 | errorInfo += traceback.format_exc() |
Justin Thaler | 666cf34 | 2019-01-23 14:44:27 -0600 | [diff] [blame] | 2046 | except Exception as e: |
| 2047 | errorInfo += "LED status collection exception: {eInfo}\n".format(eInfo=e) |
Justin Thaler | b425667 | 2020-04-07 19:38:26 -0500 | [diff] [blame] | 2048 | exc_type, exc_obj, exc_tb = sys.exc_info() |
| 2049 | fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] |
| 2050 | errorInfo += "Exception: Error: {err}, Details: {etype}, {fname}, {lineno}\n".format(err=e, etype=exc_type, fname=fname, lineno=exc_tb.tb_lineno) |
| 2051 | errorInfo += traceback.format_exc() |
Justin Thaler | 666cf34 | 2019-01-23 14:44:27 -0600 | [diff] [blame] | 2052 | |
| 2053 | if ledsCollected: |
| 2054 | try: |
| 2055 | with open(fileDir +os.sep+'ledStatus.txt', 'w') as f: |
| 2056 | f.write(json.dumps(leds, sort_keys=True, indent=4, separators=(',', ': '), ensure_ascii=False) + '\n') |
| 2057 | print("LED status collected and stored in " + fileDir + os.sep+ "ledStatus.txt") |
| 2058 | output['fileLoc'] = fileDir+os.sep+'ledStatus.txt' |
| 2059 | except Exception as e: |
| 2060 | print("Failed to write LED status to file system.") |
| 2061 | errorInfo += "Error writing LED status to the file. Exception: {eInfo}\n".format(eInfo=e) |
Justin Thaler | b425667 | 2020-04-07 19:38:26 -0500 | [diff] [blame] | 2062 | exc_type, exc_obj, exc_tb = sys.exc_info() |
| 2063 | fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] |
| 2064 | errorInfo += "Exception: Error: {err}, Details: {etype}, {fname}, {lineno}\n".format(err=e, etype=exc_type, fname=fname, lineno=exc_tb.tb_lineno) |
| 2065 | errorInfo += traceback.format_exc() |
Justin Thaler | 666cf34 | 2019-01-23 14:44:27 -0600 | [diff] [blame] | 2066 | |
| 2067 | output['errors'] = errorInfo |
| 2068 | return output |
| 2069 | |
| 2070 | def csdSelShortList(host, args, session, fileDir): |
| 2071 | """ |
| 2072 | Collects the BMC log entries, retrying if necessary |
| 2073 | |
| 2074 | @param host: string, the hostname or IP address of the bmc |
| 2075 | @param args: contains additional arguments used by the collectServiceData sub command |
| 2076 | @param session: the active session to use |
| 2077 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 2078 | @param fileDir: string representation of the path to use for putting files created |
| 2079 | """ |
| 2080 | errorInfo = "===========SEL Short List =============\n" |
| 2081 | selsCollected = False |
| 2082 | output={} |
| 2083 | try: |
| 2084 | d = vars(args) |
| 2085 | d['json'] = False |
| 2086 | except Exception as e: |
| 2087 | errorInfo += "Failed to set the json flag to False \n Exception: {eInfo}\n".format(eInfo=e) |
Justin Thaler | b425667 | 2020-04-07 19:38:26 -0500 | [diff] [blame] | 2088 | exc_type, exc_obj, exc_tb = sys.exc_info() |
| 2089 | fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] |
| 2090 | errorInfo += "Exception: Error: {err}, Details: {etype}, {fname}, {lineno}\n".format(err=e, etype=exc_type, fname=fname, lineno=exc_tb.tb_lineno) |
| 2091 | errorInfo += traceback.format_exc() |
Justin Thaler | 666cf34 | 2019-01-23 14:44:27 -0600 | [diff] [blame] | 2092 | |
| 2093 | try: |
| 2094 | for i in range(3): |
| 2095 | sels = selPrint(host,args,session) |
| 2096 | if '----Active Alerts----' in sels or 'No log entries found' in sels or '----Historical Alerts----' in sels: |
| 2097 | selsCollected = True |
| 2098 | break |
| 2099 | else: |
| 2100 | errorInfo += sels + '\n' |
| 2101 | except Exception as e: |
| 2102 | errorInfo += "SEL short list collection exception: {eInfo}\n".format(eInfo=e) |
Justin Thaler | b425667 | 2020-04-07 19:38:26 -0500 | [diff] [blame] | 2103 | exc_type, exc_obj, exc_tb = sys.exc_info() |
| 2104 | fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] |
| 2105 | errorInfo += "Exception: Error: {err}, Details: {etype}, {fname}, {lineno}\n".format(err=e, etype=exc_type, fname=fname, lineno=exc_tb.tb_lineno) |
| 2106 | errorInfo += traceback.format_exc() |
Justin Thaler | 666cf34 | 2019-01-23 14:44:27 -0600 | [diff] [blame] | 2107 | |
| 2108 | if selsCollected: |
| 2109 | try: |
| 2110 | with open(fileDir +os.sep+'SELshortlist.txt', 'w') as f: |
| 2111 | f.write(sels) |
| 2112 | print("SEL short list collected and stored in " + fileDir + os.sep+ "SELshortlist.txt") |
| 2113 | output['fileLoc'] = fileDir+os.sep+'SELshortlist.txt' |
| 2114 | except Exception as e: |
| 2115 | print("Failed to write SEL short list to file system.") |
| 2116 | errorInfo += "Error writing SEL short list to the file. Exception: {eInfo}\n".format(eInfo=e) |
Justin Thaler | b425667 | 2020-04-07 19:38:26 -0500 | [diff] [blame] | 2117 | exc_type, exc_obj, exc_tb = sys.exc_info() |
| 2118 | fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] |
| 2119 | errorInfo += "Exception: Error: {err}, Details: {etype}, {fname}, {lineno}\n".format(err=e, etype=exc_type, fname=fname, lineno=exc_tb.tb_lineno) |
| 2120 | errorInfo += traceback.format_exc() |
Justin Thaler | 666cf34 | 2019-01-23 14:44:27 -0600 | [diff] [blame] | 2121 | |
| 2122 | output['errors'] = errorInfo |
| 2123 | return output |
| 2124 | |
| 2125 | def csdParsedSels(host, args, session, fileDir): |
| 2126 | """ |
| 2127 | Collects the BMC log entries, retrying if necessary |
| 2128 | |
| 2129 | @param host: string, the hostname or IP address of the bmc |
| 2130 | @param args: contains additional arguments used by the collectServiceData sub command |
| 2131 | @param session: the active session to use |
| 2132 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 2133 | @param fileDir: string representation of the path to use for putting files created |
| 2134 | """ |
| 2135 | errorInfo = "===========SEL Parsed List =============\n" |
| 2136 | selsCollected = False |
| 2137 | output={} |
| 2138 | try: |
| 2139 | d = vars(args) |
| 2140 | d['json'] = True |
| 2141 | d['fullEsel'] = True |
| 2142 | except Exception as e: |
| 2143 | errorInfo += "Failed to set the json flag to True \n Exception: {eInfo}\n".format(eInfo=e) |
Justin Thaler | b425667 | 2020-04-07 19:38:26 -0500 | [diff] [blame] | 2144 | exc_type, exc_obj, exc_tb = sys.exc_info() |
| 2145 | fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] |
| 2146 | errorInfo += "Exception: Error: {err}, Details: {etype}, {fname}, {lineno}\n".format(err=e, etype=exc_type, fname=fname, lineno=exc_tb.tb_lineno) |
| 2147 | errorInfo += traceback.format_exc() |
Justin Thaler | 666cf34 | 2019-01-23 14:44:27 -0600 | [diff] [blame] | 2148 | |
| 2149 | try: |
| 2150 | for i in range(3): |
| 2151 | parsedfullsels = json.loads(selPrint(host,args,session)) |
| 2152 | if 'numAlerts' in parsedfullsels: |
| 2153 | selsCollected = True |
| 2154 | break |
| 2155 | else: |
| 2156 | errorInfo += parsedfullsels + '\n' |
| 2157 | except Exception as e: |
| 2158 | errorInfo += "Parsed full SELs collection exception: {eInfo}\n".format(eInfo=e) |
Justin Thaler | b425667 | 2020-04-07 19:38:26 -0500 | [diff] [blame] | 2159 | exc_type, exc_obj, exc_tb = sys.exc_info() |
| 2160 | fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] |
| 2161 | errorInfo += "Exception: Error: {err}, Details: {etype}, {fname}, {lineno}\n".format(err=e, etype=exc_type, fname=fname, lineno=exc_tb.tb_lineno) |
| 2162 | errorInfo += traceback.format_exc() |
Justin Thaler | 666cf34 | 2019-01-23 14:44:27 -0600 | [diff] [blame] | 2163 | |
| 2164 | if selsCollected: |
| 2165 | try: |
| 2166 | sortedSELs = sortSELs(parsedfullsels) |
| 2167 | with open(fileDir +os.sep+'parsedSELs.txt', 'w') as f: |
| 2168 | for log in sortedSELs[0]: |
| 2169 | esel = "" |
| 2170 | parsedfullsels[sortedSELs[1][str(log)]]['timestamp'] = datetime.datetime.fromtimestamp(int(parsedfullsels[sortedSELs[1][str(log)]]['timestamp']/1000)).strftime("%Y-%m-%d %H:%M:%S") |
| 2171 | if ('raweSEL' in parsedfullsels[sortedSELs[1][str(log)]] and args.devdebug): |
| 2172 | esel = parsedfullsels[sortedSELs[1][str(log)]]['raweSEL'] |
| 2173 | del parsedfullsels[sortedSELs[1][str(log)]]['raweSEL'] |
| 2174 | f.write(json.dumps(parsedfullsels[sortedSELs[1][str(log)]],sort_keys=True, indent=4, separators=(',', ': '))) |
| 2175 | if(args.devdebug and esel != ""): |
| 2176 | f.write(parseESEL(args, esel)) |
| 2177 | print("Parsed SELs collected and stored in " + fileDir + os.sep+ "parsedSELs.txt") |
| 2178 | output['fileLoc'] = fileDir+os.sep+'parsedSELs.txt' |
| 2179 | except Exception as e: |
| 2180 | print("Failed to write fully parsed SELs to file system.") |
| 2181 | errorInfo += "Error writing fully parsed SELs to the file. Exception: {eInfo}\n".format(eInfo=e) |
Justin Thaler | b425667 | 2020-04-07 19:38:26 -0500 | [diff] [blame] | 2182 | exc_type, exc_obj, exc_tb = sys.exc_info() |
| 2183 | fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] |
| 2184 | errorInfo += "Exception: Error: {err}, Details: {etype}, {fname}, {lineno}\n".format(err=e, etype=exc_type, fname=fname, lineno=exc_tb.tb_lineno) |
| 2185 | errorInfo += traceback.format_exc() |
Justin Thaler | 666cf34 | 2019-01-23 14:44:27 -0600 | [diff] [blame] | 2186 | |
| 2187 | output['errors'] = errorInfo |
| 2188 | return output |
| 2189 | |
| 2190 | def csdFullEnumeration(host, args, session, fileDir): |
| 2191 | """ |
| 2192 | Collects a full enumeration of /xyz/openbmc_project/, retrying if necessary |
| 2193 | |
| 2194 | @param host: string, the hostname or IP address of the bmc |
| 2195 | @param args: contains additional arguments used by the collectServiceData sub command |
| 2196 | @param session: the active session to use |
| 2197 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 2198 | @param fileDir: string representation of the path to use for putting files created |
| 2199 | """ |
| 2200 | errorInfo = "===========BMC Full Enumeration =============\n" |
| 2201 | bmcFullCollected = False |
| 2202 | output={} |
| 2203 | try: |
| 2204 | d = vars(args) |
| 2205 | d['json'] = True |
| 2206 | except Exception as e: |
| 2207 | errorInfo += "Failed to set the json flag to False \n Exception: {eInfo}\n".format(eInfo=e) |
Justin Thaler | b425667 | 2020-04-07 19:38:26 -0500 | [diff] [blame] | 2208 | exc_type, exc_obj, exc_tb = sys.exc_info() |
| 2209 | fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] |
| 2210 | errorInfo += "Exception: Error: {err}, Details: {etype}, {fname}, {lineno}\n".format(err=e, etype=exc_type, fname=fname, lineno=exc_tb.tb_lineno) |
| 2211 | errorInfo += traceback.format_exc() |
Justin Thaler | 666cf34 | 2019-01-23 14:44:27 -0600 | [diff] [blame] | 2212 | try: |
| 2213 | print("Attempting to get a full BMC enumeration") |
| 2214 | url="https://"+host+"/xyz/openbmc_project/enumerate" |
| 2215 | httpHeader = {'Content-Type':'application/json'} |
| 2216 | for i in range(3): |
| 2217 | try: |
| 2218 | bmcRes = session.get(url, headers=jsonHeader, verify=False, timeout=180) |
| 2219 | if bmcRes.status_code == 200: |
| 2220 | bmcFullCollected = True |
| 2221 | fullEnumeration = bmcRes.json() |
| 2222 | break |
| 2223 | else: |
| 2224 | errorInfo += bmcRes.text |
| 2225 | except(requests.exceptions.Timeout): |
| 2226 | errorInfo+=json.dumps( connectionErrHandler(args.json, "Timeout", None), sort_keys=True, indent=4, separators=(',', ': '), ensure_ascii=False) + '\n' |
| 2227 | except(requests.exceptions.ConnectionError) as err: |
| 2228 | errorInfo += json.dumps(connectionErrHandler(args.json, "ConnectionError", err), sort_keys=True, indent=4, separators=(',', ': '), ensure_ascii=False) + '\n' |
Justin Thaler | b425667 | 2020-04-07 19:38:26 -0500 | [diff] [blame] | 2229 | exc_type, exc_obj, exc_tb = sys.exc_info() |
| 2230 | fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] |
| 2231 | errorInfo += "Exception: Error: {err}, Details: {etype}, {fname}, {lineno}\n".format(err=e, etype=exc_type, fname=fname, lineno=exc_tb.tb_lineno) |
| 2232 | errorInfo += traceback.format_exc() |
Justin Thaler | 666cf34 | 2019-01-23 14:44:27 -0600 | [diff] [blame] | 2233 | except Exception as e: |
| 2234 | errorInfo += "RAW BMC data collection exception: {eInfo}\n".format(eInfo=e) |
Justin Thaler | b425667 | 2020-04-07 19:38:26 -0500 | [diff] [blame] | 2235 | exc_type, exc_obj, exc_tb = sys.exc_info() |
| 2236 | fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] |
| 2237 | errorInfo += "Exception: Error: {err}, Details: {etype}, {fname}, {lineno}\n".format(err=e, etype=exc_type, fname=fname, lineno=exc_tb.tb_lineno) |
| 2238 | errorInfo += traceback.format_exc() |
Justin Thaler | 666cf34 | 2019-01-23 14:44:27 -0600 | [diff] [blame] | 2239 | |
| 2240 | if bmcFullCollected: |
| 2241 | try: |
| 2242 | with open(fileDir +os.sep+'bmcFullRaw.txt', 'w') as f: |
| 2243 | f.write(json.dumps(fullEnumeration, sort_keys=True, indent=4, separators=(',', ': '), ensure_ascii=False) + '\n') |
| 2244 | print("RAW BMC data collected and saved into " + fileDir + os.sep+ "bmcFullRaw.txt") |
| 2245 | output['fileLoc'] = fileDir+os.sep+'bmcFullRaw.txt' |
| 2246 | except Exception as e: |
| 2247 | print("Failed to write RAW BMC data to file system.") |
| 2248 | errorInfo += "Error writing RAW BMC data collection to the file. Exception: {eInfo}\n".format(eInfo=e) |
Justin Thaler | b425667 | 2020-04-07 19:38:26 -0500 | [diff] [blame] | 2249 | exc_type, exc_obj, exc_tb = sys.exc_info() |
| 2250 | fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] |
| 2251 | errorInfo += "Exception: Error: {err}, Details: {etype}, {fname}, {lineno}\n".format(err=e, etype=exc_type, fname=fname, lineno=exc_tb.tb_lineno) |
| 2252 | errorInfo += traceback.format_exc() |
Justin Thaler | 666cf34 | 2019-01-23 14:44:27 -0600 | [diff] [blame] | 2253 | |
| 2254 | output['errors'] = errorInfo |
| 2255 | return output |
| 2256 | |
| 2257 | def csdCollectAllDumps(host, args, session, fileDir): |
| 2258 | """ |
| 2259 | Collects all of the bmc dump files and stores them in fileDir |
| 2260 | |
| 2261 | @param host: string, the hostname or IP address of the bmc |
| 2262 | @param args: contains additional arguments used by the collectServiceData sub command |
| 2263 | @param session: the active session to use |
| 2264 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 2265 | @param fileDir: string representation of the path to use for putting files created |
| 2266 | """ |
| 2267 | |
| 2268 | errorInfo = "===========BMC Dump Collection =============\n" |
| 2269 | dumpListCollected = False |
| 2270 | output={} |
| 2271 | dumpList = {} |
| 2272 | try: |
| 2273 | d = vars(args) |
| 2274 | d['json'] = True |
| 2275 | d['dumpSaveLoc'] = fileDir |
| 2276 | except Exception as e: |
| 2277 | errorInfo += "Failed to set the json flag to True, or failed to set the dumpSave Location \n Exception: {eInfo}\n".format(eInfo=e) |
Justin Thaler | b425667 | 2020-04-07 19:38:26 -0500 | [diff] [blame] | 2278 | exc_type, exc_obj, exc_tb = sys.exc_info() |
| 2279 | fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] |
| 2280 | errorInfo += "Exception: Error: {err}, Details: {etype}, {fname}, {lineno}\n".format(err=e, etype=exc_type, fname=fname, lineno=exc_tb.tb_lineno) |
| 2281 | errorInfo += traceback.format_exc() |
Justin Thaler | 666cf34 | 2019-01-23 14:44:27 -0600 | [diff] [blame] | 2282 | |
| 2283 | print('Collecting bmc dump files') |
| 2284 | |
| 2285 | try: |
| 2286 | for i in range(3): |
| 2287 | dumpResp = bmcDumpList(host, args, session) |
| 2288 | if 'message' in dumpResp: |
| 2289 | if 'ok' in dumpResp['message'].lower(): |
| 2290 | dumpList = dumpResp['data'] |
| 2291 | dumpListCollected = True |
| 2292 | break |
| 2293 | else: |
| 2294 | errorInfo += "Status was not OK when retrieving the list of dumps available. \n Response: \n{resp}\n".format(resp=dumpResp) |
| 2295 | else: |
| 2296 | errorInfo += "Invalid response received from the BMC while retrieving the list of dumps available.\n {resp}\n".format(resp=dumpResp) |
| 2297 | except Exception as e: |
| 2298 | errorInfo += "BMC dump list exception: {eInfo}\n".format(eInfo=e) |
Justin Thaler | b425667 | 2020-04-07 19:38:26 -0500 | [diff] [blame] | 2299 | exc_type, exc_obj, exc_tb = sys.exc_info() |
| 2300 | fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] |
| 2301 | errorInfo += "Exception: Error: {err}, Details: {etype}, {fname}, {lineno}\n".format(err=e, etype=exc_type, fname=fname, lineno=exc_tb.tb_lineno) |
| 2302 | errorInfo += traceback.format_exc() |
Justin Thaler | 666cf34 | 2019-01-23 14:44:27 -0600 | [diff] [blame] | 2303 | |
| 2304 | if dumpListCollected: |
| 2305 | output['fileList'] = [] |
| 2306 | for dump in dumpList: |
| 2307 | try: |
| 2308 | if '/xyz/openbmc_project/dump/internal/manager' not in dump: |
| 2309 | d['dumpNum'] = int(dump.strip().split('/')[-1]) |
| 2310 | print('retrieving dump file ' + str(d['dumpNum'])) |
| 2311 | filename = bmcDumpRetrieve(host, args, session).split('Saved as ')[-1] |
| 2312 | output['fileList'].append(filename) |
| 2313 | except Exception as e: |
| 2314 | print("Unable to collect dump: {dumpInfo}".format(dumpInfo=dump)) |
| 2315 | errorInfo += "Exception collecting a bmc dump {dumpInfo}\n {eInfo}\n".format(dumpInfo=dump, eInfo=e) |
Justin Thaler | b425667 | 2020-04-07 19:38:26 -0500 | [diff] [blame] | 2316 | exc_type, exc_obj, exc_tb = sys.exc_info() |
| 2317 | fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] |
| 2318 | errorInfo += "Exception: Error: {err}, Details: {etype}, {fname}, {lineno}\n".format(err=e, etype=exc_type, fname=fname, lineno=exc_tb.tb_lineno) |
| 2319 | errorInfo += traceback.format_exc() |
Justin Thaler | 666cf34 | 2019-01-23 14:44:27 -0600 | [diff] [blame] | 2320 | output['errors'] = errorInfo |
| 2321 | return output |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2322 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 2323 | def collectServiceData(host, args, session): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2324 | """ |
| 2325 | Collects all data needed for service from the BMC |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2326 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2327 | @param host: string, the hostname or IP address of the bmc |
| 2328 | @param args: contains additional arguments used by the collectServiceData sub command |
| 2329 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2330 | @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] | 2331 | """ |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2332 | |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 2333 | global toolVersion |
Justin Thaler | 666cf34 | 2019-01-23 14:44:27 -0600 | [diff] [blame] | 2334 | filelist = [] |
| 2335 | errorInfo = "" |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2336 | |
Justin Thaler | 666cf34 | 2019-01-23 14:44:27 -0600 | [diff] [blame] | 2337 | #get current number of bmc dumps and create a new bmc dump |
| 2338 | dumpInitdata = csdDumpInitiate(host, args, session) |
Justin Thaler | b425667 | 2020-04-07 19:38:26 -0500 | [diff] [blame] | 2339 | if 'dumpFailure' in dumpInitdata: |
| 2340 | return 'Collect service data is stopping due to not being able to create a new dump. No service data was collected.' |
Justin Thaler | 666cf34 | 2019-01-23 14:44:27 -0600 | [diff] [blame] | 2341 | dumpcount = dumpInitdata['dumpcount'] |
| 2342 | errorInfo += dumpInitdata['errors'] |
| 2343 | #create the directory to put files |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 2344 | try: |
| 2345 | args.silent = True |
Justin Thaler | cf1deae | 2018-05-25 19:35:21 -0500 | [diff] [blame] | 2346 | 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] | 2347 | os.makedirs(myDir) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2348 | |
Justin Thaler | 666cf34 | 2019-01-23 14:44:27 -0600 | [diff] [blame] | 2349 | except Exception as e: |
| 2350 | print('Unable to create the temporary directory for data collection. Ensure sufficient privileges to create temporary directory. Aborting.') |
Justin Thaler | b425667 | 2020-04-07 19:38:26 -0500 | [diff] [blame] | 2351 | exc_type, exc_obj, exc_tb = sys.exc_info() |
| 2352 | fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] |
| 2353 | errorInfo += "Exception: Error: {err}, Details: {etype}, {fname}, {lineno}\n".format(err=e, etype=exc_type, fname=fname, lineno=exc_tb.tb_lineno) |
| 2354 | errorInfo += traceback.format_exc() |
Justin Thaler | 666cf34 | 2019-01-23 14:44:27 -0600 | [diff] [blame] | 2355 | return("Python exception: {eInfo}".format(eInfo = e)) |
| 2356 | |
| 2357 | #Collect Inventory |
| 2358 | inventoryData = csdInventory(host, args, session, myDir) |
| 2359 | if 'fileLoc' in inventoryData: |
| 2360 | filelist.append(inventoryData['fileLoc']) |
| 2361 | errorInfo += inventoryData['errors'] |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 2362 | #Read all the sensor and OCC status |
Justin Thaler | 666cf34 | 2019-01-23 14:44:27 -0600 | [diff] [blame] | 2363 | sensorData = csdSensors(host,args,session,myDir) |
| 2364 | if 'fileLoc' in sensorData: |
| 2365 | filelist.append(sensorData['fileLoc']) |
| 2366 | errorInfo += sensorData['errors'] |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 2367 | #Collect all of the LEDs status |
Justin Thaler | 666cf34 | 2019-01-23 14:44:27 -0600 | [diff] [blame] | 2368 | ledStatus = csdLEDs(host, args, session, myDir) |
| 2369 | if 'fileLoc' in ledStatus: |
| 2370 | filelist.append(ledStatus['fileLoc']) |
| 2371 | errorInfo += ledStatus['errors'] |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2372 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 2373 | #Collect the bmc logs |
Justin Thaler | 666cf34 | 2019-01-23 14:44:27 -0600 | [diff] [blame] | 2374 | selShort = csdSelShortList(host, args, session, myDir) |
| 2375 | if 'fileLoc' in selShort: |
| 2376 | filelist.append(selShort['fileLoc']) |
| 2377 | errorInfo += selShort['errors'] |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2378 | |
Justin Thaler | 666cf34 | 2019-01-23 14:44:27 -0600 | [diff] [blame] | 2379 | parsedSELs = csdParsedSels(host, args, session, myDir) |
| 2380 | if 'fileLoc' in parsedSELs: |
| 2381 | filelist.append(parsedSELs['fileLoc']) |
| 2382 | errorInfo += parsedSELs['errors'] |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2383 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 2384 | #collect RAW bmc enumeration |
Justin Thaler | 666cf34 | 2019-01-23 14:44:27 -0600 | [diff] [blame] | 2385 | bmcRaw = csdFullEnumeration(host, args, session, myDir) |
| 2386 | if 'fileLoc' in bmcRaw: |
| 2387 | filelist.append(bmcRaw['fileLoc']) |
| 2388 | errorInfo += bmcRaw['errors'] |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2389 | |
Justin Thaler | 666cf34 | 2019-01-23 14:44:27 -0600 | [diff] [blame] | 2390 | #wait for new dump to finish being created |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 2391 | waitingForNewDump = True |
| 2392 | count = 0; |
Justin Thaler | b425667 | 2020-04-07 19:38:26 -0500 | [diff] [blame] | 2393 | print("Waiting for new BMC dump to finish being created. Wait time could be up to 5 minutes") |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2394 | while(waitingForNewDump): |
Justin Thaler | 666cf34 | 2019-01-23 14:44:27 -0600 | [diff] [blame] | 2395 | dumpList = bmcDumpList(host, args, session)['data'] |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 2396 | if len(dumpList) > dumpcount: |
| 2397 | waitingForNewDump = False |
| 2398 | break; |
Justin Thaler | b425667 | 2020-04-07 19:38:26 -0500 | [diff] [blame] | 2399 | elif(count>150): |
| 2400 | print("Timed out waiting for bmc to make a new dump file. Continuing without it.") |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 2401 | break; |
| 2402 | else: |
| 2403 | time.sleep(2) |
| 2404 | count += 1 |
Justin Thaler | 666cf34 | 2019-01-23 14:44:27 -0600 | [diff] [blame] | 2405 | |
| 2406 | #collect all of the dump files |
| 2407 | getBMCDumps = csdCollectAllDumps(host, args, session, myDir) |
| 2408 | if 'fileList' in getBMCDumps: |
| 2409 | filelist+= getBMCDumps['fileList'] |
| 2410 | errorInfo += getBMCDumps['errors'] |
| 2411 | |
| 2412 | #write the runtime errors to a file |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2413 | try: |
Justin Thaler | 666cf34 | 2019-01-23 14:44:27 -0600 | [diff] [blame] | 2414 | with open(myDir +os.sep+'openbmctoolRuntimeErrors.txt', 'w') as f: |
| 2415 | f.write(errorInfo) |
| 2416 | print("OpenBMC tool runtime errors collected and stored in " + myDir + os.sep+ "openbmctoolRuntimeErrors.txt") |
| 2417 | filelist.append(myDir+os.sep+'openbmctoolRuntimeErrors.txt') |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 2418 | except Exception as e: |
Justin Thaler | 666cf34 | 2019-01-23 14:44:27 -0600 | [diff] [blame] | 2419 | print("Failed to write OpenBMC tool runtime errors to file system.") |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2420 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 2421 | #create the zip file |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2422 | try: |
Justin Thaler | cf1deae | 2018-05-25 19:35:21 -0500 | [diff] [blame] | 2423 | filename = myDir.split(tempfile.gettempdir()+os.sep)[-1] + "_" + toolVersion + '_openbmc.zip' |
Justin Thaler | 666cf34 | 2019-01-23 14:44:27 -0600 | [diff] [blame] | 2424 | zf = zipfile.ZipFile(myDir+os.sep + filename, 'w') |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 2425 | for myfile in filelist: |
| 2426 | zf.write(myfile, os.path.basename(myfile)) |
| 2427 | zf.close() |
Justin Thaler | 666cf34 | 2019-01-23 14:44:27 -0600 | [diff] [blame] | 2428 | print("Zip file with all collected data created and stored in: {fileInfo}".format(fileInfo=myDir+os.sep+filename)) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 2429 | except Exception as e: |
| 2430 | print("Failed to create zip file with collected information") |
Justin Thaler | 666cf34 | 2019-01-23 14:44:27 -0600 | [diff] [blame] | 2431 | return "data collection finished" |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 2432 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2433 | |
| 2434 | def healthCheck(host, args, session): |
| 2435 | """ |
| 2436 | runs a health check on the platform |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2437 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2438 | @param host: string, the hostname or IP address of the bmc |
| 2439 | @param args: contains additional arguments used by the bmc sub command |
| 2440 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2441 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 2442 | """ |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2443 | #check fru status and get as json to easily work through |
| 2444 | d = vars(args) |
| 2445 | useJson = d['json'] |
| 2446 | d['json'] = True |
| 2447 | d['verbose']= False |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2448 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2449 | frus = json.loads(fruStatus(host, args, session)) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2450 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2451 | hwStatus= "OK" |
| 2452 | performanceStatus = "OK" |
| 2453 | for key in frus: |
| 2454 | if frus[key]["Functional"] == "No" and frus[key]["Present"] == "Yes": |
| 2455 | hwStatus= "Degraded" |
Justin Thaler | fb9c81c | 2018-07-16 11:14:37 -0500 | [diff] [blame] | 2456 | if("power_supply" in key or "powersupply" in key): |
| 2457 | gpuCount =0 |
| 2458 | for comp in frus: |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2459 | if "gv100card" in comp: |
| 2460 | gpuCount +=1 |
| 2461 | if gpuCount > 4: |
| 2462 | hwStatus = "Critical" |
| 2463 | performanceStatus="Degraded" |
| 2464 | break; |
| 2465 | elif("fan" in key): |
| 2466 | hwStatus = "Degraded" |
| 2467 | else: |
| 2468 | performanceStatus = "Degraded" |
| 2469 | if useJson: |
| 2470 | output = {"Hardware Status": hwStatus, "Performance": performanceStatus} |
| 2471 | output = json.dumps(output, sort_keys=True, indent=4, separators=(',', ': '), ensure_ascii=False) |
| 2472 | else: |
| 2473 | output = ("Hardware Status: " + hwStatus + |
| 2474 | "\nPerformance: " +performanceStatus ) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2475 | |
| 2476 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2477 | #SW407886: Clear the duplicate entries |
| 2478 | #collect the dups |
| 2479 | d['devdebug'] = False |
| 2480 | sels = json.loads(selPrint(host, args, session)) |
| 2481 | logNums2Clr = [] |
| 2482 | oldestLogNum={"logNum": "bogus" ,"key" : ""} |
| 2483 | count = 0 |
| 2484 | if sels['numAlerts'] > 0: |
| 2485 | for key in sels: |
| 2486 | if "numAlerts" in key: |
| 2487 | continue |
| 2488 | try: |
| 2489 | if "slave@00:00/00:00:00:06/sbefifo1-dev0/occ1-dev0" in sels[key]['Message']: |
| 2490 | count += 1 |
| 2491 | if count > 1: |
| 2492 | #preserve first occurrence |
| 2493 | if sels[key]['timestamp'] < sels[oldestLogNum['key']]['timestamp']: |
| 2494 | oldestLogNum['key']=key |
| 2495 | oldestLogNum['logNum'] = sels[key]['logNum'] |
| 2496 | else: |
| 2497 | oldestLogNum['key']=key |
| 2498 | oldestLogNum['logNum'] = sels[key]['logNum'] |
| 2499 | logNums2Clr.append(sels[key]['logNum']) |
| 2500 | except KeyError: |
| 2501 | continue |
| 2502 | if(count >0): |
| 2503 | logNums2Clr.remove(oldestLogNum['logNum']) |
| 2504 | #delete the dups |
| 2505 | if count >1: |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2506 | data = "{\"data\": [] }" |
| 2507 | for logNum in logNums2Clr: |
| 2508 | url = "https://"+ host+ "/xyz/openbmc_project/logging/entry/"+logNum+"/action/Delete" |
| 2509 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 2510 | session.post(url, headers=jsonHeader, data=data, verify=False, timeout=baseTimeout) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2511 | except(requests.exceptions.Timeout): |
| 2512 | deleteFailed = True |
| 2513 | except(requests.exceptions.ConnectionError) as err: |
| 2514 | deleteFailed = True |
| 2515 | #End of defect resolve code |
| 2516 | d['json'] = useJson |
| 2517 | return output |
| 2518 | |
| 2519 | |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2520 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 2521 | def bmc(host, args, session): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2522 | """ |
| 2523 | handles various bmc level commands, currently bmc rebooting |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2524 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2525 | @param host: string, the hostname or IP address of the bmc |
| 2526 | @param args: contains additional arguments used by the bmc sub command |
| 2527 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2528 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 2529 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 2530 | if(args.type is not None): |
| 2531 | return bmcReset(host, args, session) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2532 | if(args.info): |
| 2533 | return "Not implemented at this time" |
| 2534 | |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2535 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2536 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 2537 | def bmcReset(host, args, session): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2538 | """ |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2539 | controls resetting the bmc. warm reset reboots the bmc, cold reset removes the configuration and reboots. |
| 2540 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2541 | @param host: string, the hostname or IP address of the bmc |
| 2542 | @param args: contains additional arguments used by the bmcReset sub command |
| 2543 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2544 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 2545 | """ |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 2546 | if checkFWactivation(host, args, session): |
| 2547 | return ("BMC reset control disabled during firmware activation") |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 2548 | if(args.type == "warm"): |
| 2549 | print("\nAttempting to reboot the BMC...:") |
| 2550 | url="https://"+host+"/xyz/openbmc_project/state/bmc0/attr/RequestedBMCTransition" |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2551 | data = '{"data":"xyz.openbmc_project.State.BMC.Transition.Reboot"}' |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 2552 | res = session.put(url, headers=jsonHeader, data=data, verify=False, timeout=baseTimeout) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 2553 | return res.text |
| 2554 | elif(args.type =="cold"): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2555 | print("\nAttempting to reboot the BMC...:") |
| 2556 | url="https://"+host+"/xyz/openbmc_project/state/bmc0/attr/RequestedBMCTransition" |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2557 | data = '{"data":"xyz.openbmc_project.State.BMC.Transition.Reboot"}' |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 2558 | res = session.put(url, headers=jsonHeader, data=data, verify=False, timeout=baseTimeout) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2559 | return res.text |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 2560 | else: |
| 2561 | return "invalid command" |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2562 | |
| 2563 | def gardClear(host, args, session): |
| 2564 | """ |
| 2565 | clears the gard records from the bmc |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2566 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2567 | @param host: string, the hostname or IP address of the bmc |
| 2568 | @param args: contains additional arguments used by the gardClear sub command |
| 2569 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2570 | """ |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2571 | url="https://"+host+"/org/open_power/control/gard/action/Reset" |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2572 | data = '{"data":[]}' |
| 2573 | try: |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2574 | |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 2575 | res = session.post(url, headers=jsonHeader, data=data, verify=False, timeout=baseTimeout) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2576 | if res.status_code == 404: |
| 2577 | return "Command not supported by this firmware version" |
| 2578 | else: |
| 2579 | return res.text |
| 2580 | except(requests.exceptions.Timeout): |
| 2581 | return connectionErrHandler(args.json, "Timeout", None) |
| 2582 | except(requests.exceptions.ConnectionError) as err: |
| 2583 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 2584 | |
| 2585 | def activateFWImage(host, args, session): |
| 2586 | """ |
| 2587 | activates a firmware image on the bmc |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2588 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2589 | @param host: string, the hostname or IP address of the bmc |
| 2590 | @param args: contains additional arguments used by the fwflash sub command |
| 2591 | @param session: the active session to use |
| 2592 | @param fwID: the unique ID of the fw image to activate |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2593 | """ |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2594 | fwID = args.imageID |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2595 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2596 | #determine the existing versions |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2597 | url="https://"+host+"/xyz/openbmc_project/software/enumerate" |
| 2598 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 2599 | resp = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2600 | except(requests.exceptions.Timeout): |
| 2601 | return connectionErrHandler(args.json, "Timeout", None) |
| 2602 | except(requests.exceptions.ConnectionError) as err: |
| 2603 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 2604 | existingSoftware = json.loads(resp.text)['data'] |
| 2605 | altVersionID = '' |
| 2606 | versionType = '' |
| 2607 | imageKey = '/xyz/openbmc_project/software/'+fwID |
| 2608 | if imageKey in existingSoftware: |
| 2609 | versionType = existingSoftware[imageKey]['Purpose'] |
| 2610 | for key in existingSoftware: |
| 2611 | if imageKey == key: |
| 2612 | continue |
| 2613 | if 'Purpose' in existingSoftware[key]: |
| 2614 | if versionType == existingSoftware[key]['Purpose']: |
| 2615 | altVersionID = key.split('/')[-1] |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2616 | |
| 2617 | |
| 2618 | |
| 2619 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2620 | url="https://"+host+"/xyz/openbmc_project/software/"+ fwID + "/attr/Priority" |
| 2621 | url1="https://"+host+"/xyz/openbmc_project/software/"+ altVersionID + "/attr/Priority" |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2622 | data = "{\"data\": 0}" |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2623 | data1 = "{\"data\": 1 }" |
| 2624 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 2625 | resp = session.put(url, headers=jsonHeader, data=data, verify=False, timeout=baseTimeout) |
| 2626 | resp1 = session.put(url1, headers=jsonHeader, data=data1, verify=False, timeout=baseTimeout) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2627 | except(requests.exceptions.Timeout): |
| 2628 | return connectionErrHandler(args.json, "Timeout", None) |
| 2629 | except(requests.exceptions.ConnectionError) as err: |
| 2630 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 2631 | if(not args.json): |
| 2632 | if resp.status_code == 200 and resp1.status_code == 200: |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 2633 | 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] | 2634 | else: |
| 2635 | return "Firmware activation failed." |
| 2636 | else: |
| 2637 | return resp.text + resp1.text |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 2638 | |
| 2639 | def activateStatus(host, args, session): |
| 2640 | if checkFWactivation(host, args, session): |
| 2641 | return("Firmware is currently being activated. Do not reboot the BMC or start the Host OS") |
| 2642 | else: |
| 2643 | return("No firmware activations are pending") |
| 2644 | |
| 2645 | def extractFWimage(path, imageType): |
| 2646 | """ |
| 2647 | extracts the bmc image and returns information about the package |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2648 | |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 2649 | @param path: the path and file name of the firmware image |
| 2650 | @param imageType: The type of image the user is trying to flash. Host or BMC |
| 2651 | @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] | 2652 | """ |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 2653 | f = tempfile.TemporaryFile() |
| 2654 | tmpDir = tempfile.gettempdir() |
| 2655 | newImageID = "" |
| 2656 | if os.path.exists(path): |
| 2657 | try: |
| 2658 | imageFile = tarfile.open(path,'r') |
| 2659 | contents = imageFile.getmembers() |
| 2660 | for tf in contents: |
| 2661 | if 'MANIFEST' in tf.name: |
| 2662 | imageFile.extract(tf.name, path=tmpDir) |
| 2663 | with open(tempfile.gettempdir() +os.sep+ tf.name, 'r') as imageInfo: |
| 2664 | for line in imageInfo: |
| 2665 | if 'purpose' in line: |
| 2666 | purpose = line.split('=')[1] |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2667 | if imageType not in purpose.split('.')[-1]: |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 2668 | print('The specified image is not for ' + imageType) |
| 2669 | print('Please try again with the image for ' + imageType) |
| 2670 | return "" |
| 2671 | if 'version' == line.split('=')[0]: |
| 2672 | version = line.split('=')[1].strip().encode('utf-8') |
| 2673 | m = hashlib.sha512() |
| 2674 | m.update(version) |
| 2675 | newImageID = m.hexdigest()[:8] |
| 2676 | break |
| 2677 | try: |
| 2678 | os.remove(tempfile.gettempdir() +os.sep+ tf.name) |
| 2679 | except OSError: |
| 2680 | pass |
| 2681 | return newImageID |
| 2682 | except tarfile.ExtractError as e: |
| 2683 | print('Unable to extract information from the firmware file.') |
| 2684 | print('Ensure you have write access to the directory: ' + tmpDir) |
| 2685 | return newImageID |
| 2686 | except tarfile.TarError as e: |
| 2687 | print('This is not a valid firmware file.') |
| 2688 | return newImageID |
| 2689 | print("This is not a valid firmware file.") |
| 2690 | return newImageID |
| 2691 | else: |
| 2692 | print('The filename and path provided are not valid.') |
| 2693 | return newImageID |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2694 | |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 2695 | def getAllFWImageIDs(fwInvDict): |
| 2696 | """ |
| 2697 | gets a list of all the firmware image IDs |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2698 | |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 2699 | @param fwInvDict: the dictionary to search for FW image IDs |
| 2700 | @return: list containing string representation of the found image ids |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2701 | """ |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 2702 | idList = [] |
| 2703 | for key in fwInvDict: |
| 2704 | if 'Version' in fwInvDict[key]: |
| 2705 | idList.append(key.split('/')[-1]) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2706 | return idList |
| 2707 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2708 | def fwFlash(host, args, session): |
| 2709 | """ |
| 2710 | updates the bmc firmware and pnor firmware |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2711 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2712 | @param host: string, the hostname or IP address of the bmc |
| 2713 | @param args: contains additional arguments used by the fwflash sub command |
| 2714 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2715 | """ |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 2716 | d = vars(args) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2717 | if(args.type == 'bmc'): |
| 2718 | purp = 'BMC' |
| 2719 | else: |
| 2720 | purp = 'Host' |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2721 | |
| 2722 | #check power state of the machine. No concurrent FW updates allowed |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 2723 | d['powcmd'] = 'status' |
| 2724 | powerstate = chassisPower(host, args, session) |
| 2725 | if 'Chassis Power State: On' in powerstate: |
| 2726 | 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] | 2727 | |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 2728 | #determine the existing images on the bmc |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2729 | url="https://"+host+"/xyz/openbmc_project/software/enumerate" |
| 2730 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 2731 | resp = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2732 | except(requests.exceptions.Timeout): |
| 2733 | return connectionErrHandler(args.json, "Timeout", None) |
| 2734 | except(requests.exceptions.ConnectionError) as err: |
| 2735 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 2736 | oldsoftware = json.loads(resp.text)['data'] |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2737 | |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 2738 | #Extract the tar and get information from the manifest file |
| 2739 | newversionID = extractFWimage(args.fileloc, purp) |
| 2740 | if newversionID == "": |
| 2741 | return "Unable to verify FW image." |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2742 | |
| 2743 | |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 2744 | #check if the new image is already on the bmc |
| 2745 | if newversionID not in getAllFWImageIDs(oldsoftware): |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2746 | |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 2747 | #upload the file |
| 2748 | httpHeader = {'Content-Type':'application/octet-stream'} |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 2749 | httpHeader.update(xAuthHeader) |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 2750 | url="https://"+host+"/upload/image" |
| 2751 | data=open(args.fileloc,'rb').read() |
| 2752 | print("Uploading file to BMC") |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2753 | try: |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 2754 | resp = session.post(url, headers=httpHeader, data=data, verify=False) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2755 | except(requests.exceptions.Timeout): |
| 2756 | return connectionErrHandler(args.json, "Timeout", None) |
| 2757 | except(requests.exceptions.ConnectionError) as err: |
| 2758 | return connectionErrHandler(args.json, "ConnectionError", err) |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 2759 | if resp.status_code != 200: |
| 2760 | return "Failed to upload the file to the bmc" |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2761 | else: |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 2762 | print("Upload complete.") |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2763 | |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 2764 | #verify bmc processed the image |
| 2765 | software ={} |
| 2766 | for i in range(0, 5): |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 2767 | url="https://"+host+"/xyz/openbmc_project/software/enumerate" |
| 2768 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 2769 | resp = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 2770 | except(requests.exceptions.Timeout): |
| 2771 | return connectionErrHandler(args.json, "Timeout", None) |
| 2772 | except(requests.exceptions.ConnectionError) as err: |
| 2773 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 2774 | software = json.loads(resp.text)['data'] |
| 2775 | #check if bmc is done processing the new image |
| 2776 | if (newversionID in getAllFWImageIDs(software)): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2777 | break |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 2778 | else: |
| 2779 | time.sleep(15) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2780 | |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 2781 | #activate the new image |
| 2782 | print("Activating new image: "+newversionID) |
| 2783 | url="https://"+host+"/xyz/openbmc_project/software/"+ newversionID + "/attr/RequestedActivation" |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2784 | data = '{"data":"xyz.openbmc_project.Software.Activation.RequestedActivations.Active"}' |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 2785 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 2786 | resp = session.put(url, headers=jsonHeader, data=data, verify=False, timeout=baseTimeout) |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 2787 | except(requests.exceptions.Timeout): |
| 2788 | return connectionErrHandler(args.json, "Timeout", None) |
| 2789 | except(requests.exceptions.ConnectionError) as err: |
| 2790 | return connectionErrHandler(args.json, "ConnectionError", err) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2791 | |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 2792 | #wait for the activation to complete, timeout after ~1 hour |
| 2793 | i=0 |
| 2794 | while i < 360: |
| 2795 | url="https://"+host+"/xyz/openbmc_project/software/"+ newversionID |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2796 | data = '{"data":"xyz.openbmc_project.Software.Activation.RequestedActivations.Active"}' |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 2797 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 2798 | resp = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 2799 | except(requests.exceptions.Timeout): |
| 2800 | return connectionErrHandler(args.json, "Timeout", None) |
| 2801 | except(requests.exceptions.ConnectionError) as err: |
| 2802 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 2803 | fwInfo = json.loads(resp.text)['data'] |
| 2804 | if 'Activating' not in fwInfo['Activation'] and 'Activating' not in fwInfo['RequestedActivation']: |
| 2805 | print('') |
| 2806 | break |
| 2807 | else: |
| 2808 | sys.stdout.write('.') |
| 2809 | sys.stdout.flush() |
| 2810 | time.sleep(10) #check every 10 seconds |
| 2811 | return "Firmware flash and activation completed. Please reboot the bmc and then boot the host OS for the changes to take effect. " |
| 2812 | else: |
| 2813 | print("This image has been found on the bmc. Activating image: " + newversionID) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2814 | |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 2815 | d['imageID'] = newversionID |
| 2816 | return activateFWImage(host, args, session) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2817 | |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 2818 | def getFWInventoryAttributes(rawFWInvItem, ID): |
| 2819 | """ |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2820 | gets and lists all of the firmware in the system. |
| 2821 | |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 2822 | @return: returns a dictionary containing the image attributes |
| 2823 | """ |
| 2824 | reqActivation = rawFWInvItem["RequestedActivation"].split('.')[-1] |
| 2825 | pendingActivation = "" |
| 2826 | if reqActivation == "None": |
| 2827 | pendingActivation = "No" |
| 2828 | else: |
| 2829 | pendingActivation = "Yes" |
| 2830 | firmwareAttr = {ID: { |
| 2831 | "Purpose": rawFWInvItem["Purpose"].split('.')[-1], |
| 2832 | "Version": rawFWInvItem["Version"], |
| 2833 | "RequestedActivation": pendingActivation, |
| 2834 | "ID": ID}} |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2835 | |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 2836 | if "ExtendedVersion" in rawFWInvItem: |
| 2837 | firmwareAttr[ID]['ExtendedVersion'] = rawFWInvItem['ExtendedVersion'].split(',') |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2838 | else: |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 2839 | firmwareAttr[ID]['ExtendedVersion'] = "" |
| 2840 | return firmwareAttr |
| 2841 | |
| 2842 | def parseFWdata(firmwareDict): |
| 2843 | """ |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2844 | creates a dictionary with parsed firmware data |
| 2845 | |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 2846 | @return: returns a dictionary containing the image attributes |
| 2847 | """ |
| 2848 | firmwareInfoDict = {"Functional": {}, "Activated":{}, "NeedsActivated":{}} |
| 2849 | for key in firmwareDict['data']: |
| 2850 | #check for valid endpoint |
| 2851 | if "Purpose" in firmwareDict['data'][key]: |
| 2852 | id = key.split('/')[-1] |
| 2853 | if firmwareDict['data'][key]['Activation'].split('.')[-1] == "Active": |
| 2854 | fwActivated = True |
| 2855 | else: |
| 2856 | fwActivated = False |
Justin Thaler | cb68e06 | 2019-03-26 19:04:52 -0500 | [diff] [blame] | 2857 | if 'Priority' in firmwareDict['data'][key]: |
| 2858 | if firmwareDict['data'][key]['Priority'] == 0: |
| 2859 | firmwareInfoDict['Functional'].update(getFWInventoryAttributes(firmwareDict['data'][key], id)) |
| 2860 | elif firmwareDict['data'][key]['Priority'] >= 0 and fwActivated: |
| 2861 | firmwareInfoDict['Activated'].update(getFWInventoryAttributes(firmwareDict['data'][key], id)) |
| 2862 | else: |
| 2863 | firmwareInfoDict['NeedsActivated'].update(getFWInventoryAttributes(firmwareDict['data'][key], id)) |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 2864 | else: |
Justin Thaler | cb68e06 | 2019-03-26 19:04:52 -0500 | [diff] [blame] | 2865 | firmwareInfoDict['NeedsActivated'].update(getFWInventoryAttributes(firmwareDict['data'][key], id)) |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 2866 | emptySections = [] |
| 2867 | for key in firmwareInfoDict: |
| 2868 | if len(firmwareInfoDict[key])<=0: |
| 2869 | emptySections.append(key) |
| 2870 | for key in emptySections: |
| 2871 | del firmwareInfoDict[key] |
| 2872 | return firmwareInfoDict |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2873 | |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 2874 | def displayFWInvenory(firmwareInfoDict, args): |
| 2875 | """ |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2876 | gets and lists all of the firmware in the system. |
| 2877 | |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 2878 | @return: returns a string containing all of the firmware information |
| 2879 | """ |
| 2880 | output = "" |
| 2881 | if not args.json: |
| 2882 | for key in firmwareInfoDict: |
| 2883 | for subkey in firmwareInfoDict[key]: |
| 2884 | firmwareInfoDict[key][subkey]['ExtendedVersion'] = str(firmwareInfoDict[key][subkey]['ExtendedVersion']) |
| 2885 | if not args.verbose: |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2886 | output = "---Running Images---\n" |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 2887 | colNames = ["Purpose", "Version", "ID"] |
| 2888 | keylist = ["Purpose", "Version", "ID"] |
| 2889 | output += tableDisplay(keylist, colNames, firmwareInfoDict["Functional"]) |
| 2890 | if "Activated" in firmwareInfoDict: |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2891 | output += "\n---Available Images---\n" |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 2892 | output += tableDisplay(keylist, colNames, firmwareInfoDict["Activated"]) |
| 2893 | if "NeedsActivated" in firmwareInfoDict: |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2894 | output += "\n---Needs Activated Images---\n" |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 2895 | output += tableDisplay(keylist, colNames, firmwareInfoDict["NeedsActivated"]) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2896 | |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 2897 | else: |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2898 | output = "---Running Images---\n" |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 2899 | colNames = ["Purpose", "Version", "ID", "Pending Activation", "Extended Version"] |
| 2900 | keylist = ["Purpose", "Version", "ID", "RequestedActivation", "ExtendedVersion"] |
| 2901 | output += tableDisplay(keylist, colNames, firmwareInfoDict["Functional"]) |
| 2902 | if "Activated" in firmwareInfoDict: |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2903 | output += "\n---Available Images---\n" |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 2904 | output += tableDisplay(keylist, colNames, firmwareInfoDict["Activated"]) |
| 2905 | if "NeedsActivated" in firmwareInfoDict: |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2906 | output += "\n---Needs Activated Images---\n" |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 2907 | output += tableDisplay(keylist, colNames, firmwareInfoDict["NeedsActivated"]) |
| 2908 | return output |
| 2909 | else: |
| 2910 | return str(json.dumps(firmwareInfoDict, sort_keys=True, indent=4, separators=(',', ': '), ensure_ascii=False)) |
| 2911 | |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2912 | def firmwareList(host, args, session): |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 2913 | """ |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2914 | gets and lists all of the firmware in the system. |
| 2915 | |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 2916 | @return: returns a string containing all of the firmware information |
| 2917 | """ |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 2918 | url="https://{hostname}/xyz/openbmc_project/software/enumerate".format(hostname=host) |
| 2919 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 2920 | res = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 2921 | except(requests.exceptions.Timeout): |
| 2922 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 2923 | firmwareDict = json.loads(res.text) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2924 | |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 2925 | #sort the received information |
| 2926 | firmwareInfoDict = parseFWdata(firmwareDict) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2927 | |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 2928 | #display the information |
| 2929 | return displayFWInvenory(firmwareInfoDict, args) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2930 | |
| 2931 | |
Adriana Kobylak | 5af2fad | 2018-11-08 12:33:43 -0600 | [diff] [blame] | 2932 | def deleteFWVersion(host, args, session): |
| 2933 | """ |
| 2934 | deletes a firmware version on the BMC |
| 2935 | |
| 2936 | @param host: string, the hostname or IP address of the BMC |
| 2937 | @param args: contains additional arguments used by the fwflash sub command |
| 2938 | @param session: the active session to use |
| 2939 | @param fwID: the unique ID of the fw version to delete |
| 2940 | """ |
| 2941 | fwID = args.versionID |
| 2942 | |
| 2943 | print("Deleting version: "+fwID) |
| 2944 | url="https://"+host+"/xyz/openbmc_project/software/"+ fwID + "/action/Delete" |
Adriana Kobylak | 5af2fad | 2018-11-08 12:33:43 -0600 | [diff] [blame] | 2945 | data = "{\"data\": [] }" |
| 2946 | |
| 2947 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 2948 | res = session.post(url, headers=jsonHeader, data=data, verify=False, timeout=baseTimeout) |
Adriana Kobylak | 5af2fad | 2018-11-08 12:33:43 -0600 | [diff] [blame] | 2949 | except(requests.exceptions.Timeout): |
| 2950 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 2951 | if res.status_code == 200: |
| 2952 | return ('The firmware version has been deleted') |
| 2953 | else: |
| 2954 | return ('Unable to delete the specified firmware version') |
| 2955 | |
| 2956 | |
Deepak Kodihalli | 22d4df0 | 2018-09-18 06:52:43 -0500 | [diff] [blame] | 2957 | def restLogging(host, args, session): |
| 2958 | """ |
| 2959 | Called by the logging function. Turns REST API logging on/off. |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2960 | |
Deepak Kodihalli | 22d4df0 | 2018-09-18 06:52:43 -0500 | [diff] [blame] | 2961 | @param host: string, the hostname or IP address of the bmc |
| 2962 | @param args: contains additional arguments used by the logging sub command |
| 2963 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2964 | @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] | 2965 | """ |
Deepak Kodihalli | 22d4df0 | 2018-09-18 06:52:43 -0500 | [diff] [blame] | 2966 | url="https://"+host+"/xyz/openbmc_project/logging/rest_api_logs/attr/Enabled" |
Deepak Kodihalli | 22d4df0 | 2018-09-18 06:52:43 -0500 | [diff] [blame] | 2967 | |
| 2968 | if(args.rest_logging == 'on'): |
| 2969 | data = '{"data": 1}' |
| 2970 | elif(args.rest_logging == 'off'): |
| 2971 | data = '{"data": 0}' |
| 2972 | else: |
| 2973 | return "Invalid logging rest_api command" |
| 2974 | |
| 2975 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 2976 | res = session.put(url, headers=jsonHeader, data=data, verify=False, timeout=baseTimeout) |
Deepak Kodihalli | 22d4df0 | 2018-09-18 06:52:43 -0500 | [diff] [blame] | 2977 | except(requests.exceptions.Timeout): |
| 2978 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 2979 | return res.text |
| 2980 | |
| 2981 | |
Deepak Kodihalli | 02d5328 | 2018-09-18 06:53:31 -0500 | [diff] [blame] | 2982 | def remoteLogging(host, args, session): |
| 2983 | """ |
| 2984 | 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] | 2985 | |
Deepak Kodihalli | 02d5328 | 2018-09-18 06:53:31 -0500 | [diff] [blame] | 2986 | @param host: string, the hostname or IP address of the bmc |
| 2987 | @param args: contains additional arguments used by the logging sub command |
| 2988 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2989 | @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] | 2990 | """ |
| 2991 | |
| 2992 | url="https://"+host+"/xyz/openbmc_project/logging/config/remote" |
Deepak Kodihalli | 02d5328 | 2018-09-18 06:53:31 -0500 | [diff] [blame] | 2993 | |
| 2994 | try: |
| 2995 | if(args.remote_logging == 'view'): |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 2996 | res = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
Deepak Kodihalli | 02d5328 | 2018-09-18 06:53:31 -0500 | [diff] [blame] | 2997 | elif(args.remote_logging == 'disable'): |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 2998 | res = session.put(url + '/attr/Port', headers=jsonHeader, json = {"data": 0}, verify=False, timeout=baseTimeout) |
| 2999 | res = session.put(url + '/attr/Address', headers=jsonHeader, json = {"data": ""}, verify=False, timeout=baseTimeout) |
Deepak Kodihalli | 02d5328 | 2018-09-18 06:53:31 -0500 | [diff] [blame] | 3000 | else: |
| 3001 | return "Invalid logging remote_logging command" |
| 3002 | except(requests.exceptions.Timeout): |
| 3003 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3004 | return res.text |
| 3005 | |
| 3006 | |
| 3007 | def remoteLoggingConfig(host, args, session): |
| 3008 | """ |
| 3009 | Called by the logging function. Configures remote logging (rsyslog). |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 3010 | |
Deepak Kodihalli | 02d5328 | 2018-09-18 06:53:31 -0500 | [diff] [blame] | 3011 | @param host: string, the hostname or IP address of the bmc |
| 3012 | @param args: contains additional arguments used by the logging sub command |
| 3013 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 3014 | @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] | 3015 | """ |
| 3016 | |
| 3017 | url="https://"+host+"/xyz/openbmc_project/logging/config/remote" |
Deepak Kodihalli | 02d5328 | 2018-09-18 06:53:31 -0500 | [diff] [blame] | 3018 | |
| 3019 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 3020 | res = session.put(url + '/attr/Port', headers=jsonHeader, json = {"data": args.port}, verify=False, timeout=baseTimeout) |
| 3021 | res = session.put(url + '/attr/Address', headers=jsonHeader, json = {"data": args.address}, verify=False, timeout=baseTimeout) |
Deepak Kodihalli | 02d5328 | 2018-09-18 06:53:31 -0500 | [diff] [blame] | 3022 | except(requests.exceptions.Timeout): |
| 3023 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3024 | return res.text |
| 3025 | |
Marri Devender Rao | 82590dc | 2019-06-06 04:54:22 -0500 | [diff] [blame] | 3026 | def redfishSupportPresent(host, session): |
| 3027 | url = "https://" + host + "/redfish/v1" |
| 3028 | try: |
| 3029 | resp = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
| 3030 | except(requests.exceptions.Timeout): |
| 3031 | return False |
| 3032 | except(requests.exceptions.ConnectionError) as err: |
| 3033 | return False |
| 3034 | if resp.status_code != 200: |
| 3035 | return False |
| 3036 | else: |
| 3037 | return True |
Ratan Gupta | 9166cd2 | 2018-10-01 18:09:40 +0530 | [diff] [blame] | 3038 | |
Dhruvaraj Subhashchandran | 64e7f6f | 2018-10-02 03:42:14 -0500 | [diff] [blame] | 3039 | def certificateUpdate(host, args, session): |
| 3040 | """ |
| 3041 | Called by certificate management function. update server/client/authority certificates |
| 3042 | Example: |
| 3043 | certificate update server https -f cert.pem |
| 3044 | certificate update authority ldap -f Root-CA.pem |
| 3045 | certificate update client ldap -f cert.pem |
| 3046 | @param host: string, the hostname or IP address of the bmc |
| 3047 | @param args: contains additional arguments used by the certificate update sub command |
| 3048 | @param session: the active session to use |
| 3049 | """ |
Dhruvaraj Subhashchandran | 64e7f6f | 2018-10-02 03:42:14 -0500 | [diff] [blame] | 3050 | httpHeader = {'Content-Type': 'application/octet-stream'} |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 3051 | httpHeader.update(xAuthHeader) |
Brad Bishop | 5da038f | 2020-07-10 14:21:43 -0400 | [diff] [blame] | 3052 | data = open(args.fileloc, 'r').read() |
Dhruvaraj Subhashchandran | 64e7f6f | 2018-10-02 03:42:14 -0500 | [diff] [blame] | 3053 | try: |
Marri Devender Rao | 82590dc | 2019-06-06 04:54:22 -0500 | [diff] [blame] | 3054 | if redfishSupportPresent(host, session): |
Marri Devender Rao | 62db08a | 2019-08-22 03:16:02 -0500 | [diff] [blame] | 3055 | if(args.type.lower() == 'server' and args.service.lower() != "https"): |
| 3056 | return "Invalid service type" |
| 3057 | if(args.type.lower() == 'client' and args.service.lower() != "ldap"): |
| 3058 | return "Invalid service type" |
| 3059 | if(args.type.lower() == 'authority' and args.service.lower() != "ldap"): |
| 3060 | return "Invalid service type" |
Marri Devender Rao | 82590dc | 2019-06-06 04:54:22 -0500 | [diff] [blame] | 3061 | url = ""; |
| 3062 | if(args.type.lower() == 'server'): |
| 3063 | url = "https://" + host + \ |
| 3064 | "/redfish/v1/Managers/bmc/NetworkProtocol/HTTPS/Certificates" |
| 3065 | elif(args.type.lower() == 'client'): |
| 3066 | url = "https://" + host + \ |
| 3067 | "/redfish/v1/AccountService/LDAP/Certificates" |
| 3068 | elif(args.type.lower() == 'authority'): |
| 3069 | url = "https://" + host + \ |
| 3070 | "/redfish/v1/Managers/bmc/Truststore/Certificates" |
| 3071 | else: |
| 3072 | return "Unsupported certificate type" |
| 3073 | resp = session.post(url, headers=httpHeader, data=data, |
| 3074 | verify=False) |
| 3075 | else: |
| 3076 | url = "https://" + host + "/xyz/openbmc_project/certs/" + \ |
| 3077 | args.type.lower() + "/" + args.service.lower() |
| 3078 | resp = session.put(url, headers=httpHeader, data=data, verify=False) |
Dhruvaraj Subhashchandran | 64e7f6f | 2018-10-02 03:42:14 -0500 | [diff] [blame] | 3079 | except(requests.exceptions.Timeout): |
| 3080 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3081 | except(requests.exceptions.ConnectionError) as err: |
| 3082 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 3083 | if resp.status_code != 200: |
| 3084 | print(resp.text) |
| 3085 | return "Failed to update the certificate" |
| 3086 | else: |
Marri Devender Rao | 82590dc | 2019-06-06 04:54:22 -0500 | [diff] [blame] | 3087 | print("Update complete.") |
Dhruvaraj Subhashchandran | 64e7f6f | 2018-10-02 03:42:14 -0500 | [diff] [blame] | 3088 | |
| 3089 | def certificateDelete(host, args, session): |
| 3090 | """ |
| 3091 | Called by certificate management function to delete certificate |
| 3092 | Example: |
| 3093 | certificate delete server https |
| 3094 | certificate delete authority ldap |
| 3095 | certificate delete client ldap |
| 3096 | @param host: string, the hostname or IP address of the bmc |
| 3097 | @param args: contains additional arguments used by the certificate delete sub command |
| 3098 | @param session: the active session to use |
| 3099 | """ |
Marri Devender Rao | 77e7868 | 2019-07-17 03:18:35 -0500 | [diff] [blame] | 3100 | if redfishSupportPresent(host, session): |
| 3101 | return "Not supported, please use certificate replace instead"; |
Dhruvaraj Subhashchandran | 64e7f6f | 2018-10-02 03:42:14 -0500 | [diff] [blame] | 3102 | httpHeader = {'Content-Type': 'multipart/form-data'} |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 3103 | httpHeader.update(xAuthHeader) |
Dhruvaraj Subhashchandran | 64e7f6f | 2018-10-02 03:42:14 -0500 | [diff] [blame] | 3104 | url = "https://" + host + "/xyz/openbmc_project/certs/" + args.type.lower() + "/" + args.service.lower() |
| 3105 | print("Deleting certificate url=" + url) |
| 3106 | try: |
| 3107 | resp = session.delete(url, headers=httpHeader) |
| 3108 | except(requests.exceptions.Timeout): |
| 3109 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3110 | except(requests.exceptions.ConnectionError) as err: |
| 3111 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 3112 | if resp.status_code != 200: |
| 3113 | print(resp.text) |
| 3114 | return "Failed to delete the certificate" |
| 3115 | else: |
Marri Devender Rao | 77e7868 | 2019-07-17 03:18:35 -0500 | [diff] [blame] | 3116 | print("Delete complete.") |
Deepak Kodihalli | 02d5328 | 2018-09-18 06:53:31 -0500 | [diff] [blame] | 3117 | |
Marri Devender Rao | dfe81ad | 2019-07-01 05:38:09 -0500 | [diff] [blame] | 3118 | def certificateReplace(host, args, session): |
| 3119 | """ |
| 3120 | Called by certificate management function. replace server/client/ |
| 3121 | authority certificates |
| 3122 | Example: |
| 3123 | certificate replace server https -f cert.pem |
| 3124 | certificate replace authority ldap -f Root-CA.pem |
| 3125 | certificate replace client ldap -f cert.pem |
| 3126 | @param host: string, the hostname or IP address of the bmc |
| 3127 | @param args: contains additional arguments used by the certificate |
| 3128 | replace sub command |
| 3129 | @param session: the active session to use |
| 3130 | """ |
Brad Bishop | 5da038f | 2020-07-10 14:21:43 -0400 | [diff] [blame] | 3131 | cert = open(args.fileloc, 'r').read() |
Marri Devender Rao | dfe81ad | 2019-07-01 05:38:09 -0500 | [diff] [blame] | 3132 | try: |
| 3133 | if redfishSupportPresent(host, session): |
| 3134 | httpHeader = {'Content-Type': 'application/json'} |
| 3135 | httpHeader.update(xAuthHeader) |
| 3136 | url = ""; |
Marri Devender Rao | 62db08a | 2019-08-22 03:16:02 -0500 | [diff] [blame] | 3137 | if(args.type.lower() == 'server' and args.service.lower() != "https"): |
| 3138 | return "Invalid service type" |
| 3139 | if(args.type.lower() == 'client' and args.service.lower() != "ldap"): |
| 3140 | return "Invalid service type" |
| 3141 | if(args.type.lower() == 'authority' and args.service.lower() != "ldap"): |
| 3142 | return "Invalid service type" |
Marri Devender Rao | dfe81ad | 2019-07-01 05:38:09 -0500 | [diff] [blame] | 3143 | if(args.type.lower() == 'server'): |
| 3144 | url = "/redfish/v1/Managers/bmc/NetworkProtocol/HTTPS/Certificates/1" |
| 3145 | elif(args.type.lower() == 'client'): |
| 3146 | url = "/redfish/v1/AccountService/LDAP/Certificates/1" |
| 3147 | elif(args.type.lower() == 'authority'): |
| 3148 | url = "/redfish/v1/Managers/bmc/Truststore/Certificates/1" |
| 3149 | replaceUrl = "https://" + host + \ |
| 3150 | "/redfish/v1/CertificateService/Actions/CertificateService.ReplaceCertificate" |
| 3151 | data ={"CertificateUri":{"@odata.id":url}, "CertificateType":"PEM", |
| 3152 | "CertificateString":cert} |
| 3153 | resp = session.post(replaceUrl, headers=httpHeader, json=data, verify=False) |
| 3154 | else: |
| 3155 | httpHeader = {'Content-Type': 'application/octet-stream'} |
| 3156 | httpHeader.update(xAuthHeader) |
| 3157 | url = "https://" + host + "/xyz/openbmc_project/certs/" + \ |
| 3158 | args.type.lower() + "/" + args.service.lower() |
| 3159 | resp = session.delete(url, headers=httpHeader) |
| 3160 | resp = session.put(url, headers=httpHeader, data=cert, verify=False) |
| 3161 | except(requests.exceptions.Timeout): |
| 3162 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3163 | except(requests.exceptions.ConnectionError) as err: |
| 3164 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 3165 | if resp.status_code != 200: |
| 3166 | print(resp.text) |
| 3167 | return "Failed to replace the certificate" |
| 3168 | else: |
| 3169 | print("Replace complete.") |
| 3170 | return resp.text |
| 3171 | |
Marri Devender Rao | 3464640 | 2019-07-01 05:46:03 -0500 | [diff] [blame] | 3172 | def certificateDisplay(host, args, session): |
| 3173 | """ |
| 3174 | Called by certificate management function. display server/client/ |
| 3175 | authority certificates |
| 3176 | Example: |
| 3177 | certificate display server |
| 3178 | certificate display authority |
| 3179 | certificate display client |
| 3180 | @param host: string, the hostname or IP address of the bmc |
| 3181 | @param args: contains additional arguments used by the certificate |
| 3182 | display sub command |
| 3183 | @param session: the active session to use |
| 3184 | """ |
| 3185 | if not redfishSupportPresent(host, session): |
| 3186 | return "Not supported"; |
| 3187 | |
| 3188 | httpHeader = {'Content-Type': 'application/octet-stream'} |
| 3189 | httpHeader.update(xAuthHeader) |
| 3190 | if(args.type.lower() == 'server'): |
| 3191 | url = "https://" + host + \ |
| 3192 | "/redfish/v1/Managers/bmc/NetworkProtocol/HTTPS/Certificates/1" |
| 3193 | elif(args.type.lower() == 'client'): |
| 3194 | url = "https://" + host + \ |
| 3195 | "/redfish/v1/AccountService/LDAP/Certificates/1" |
| 3196 | elif(args.type.lower() == 'authority'): |
| 3197 | url = "https://" + host + \ |
| 3198 | "/redfish/v1/Managers/bmc/Truststore/Certificates/1" |
| 3199 | try: |
| 3200 | resp = session.get(url, headers=httpHeader, verify=False) |
| 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 | if resp.status_code != 200: |
| 3206 | print(resp.text) |
| 3207 | return "Failed to display the certificate" |
| 3208 | else: |
| 3209 | print("Display complete.") |
| 3210 | return resp.text |
| 3211 | |
Marri Devender Rao | a208ff8 | 2019-07-01 05:51:27 -0500 | [diff] [blame] | 3212 | def certificateList(host, args, session): |
| 3213 | """ |
| 3214 | Called by certificate management function. |
| 3215 | Example: |
| 3216 | certificate list |
| 3217 | @param host: string, the hostname or IP address of the bmc |
| 3218 | @param args: contains additional arguments used by the certificate |
| 3219 | list sub command |
| 3220 | @param session: the active session to use |
| 3221 | """ |
| 3222 | if not redfishSupportPresent(host, session): |
| 3223 | return "Not supported"; |
| 3224 | |
| 3225 | httpHeader = {'Content-Type': 'application/octet-stream'} |
| 3226 | httpHeader.update(xAuthHeader) |
| 3227 | url = "https://" + host + \ |
| 3228 | "/redfish/v1/CertificateService/CertificateLocations/" |
| 3229 | try: |
| 3230 | resp = session.get(url, headers=httpHeader, verify=False) |
| 3231 | except(requests.exceptions.Timeout): |
| 3232 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3233 | except(requests.exceptions.ConnectionError) as err: |
| 3234 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 3235 | if resp.status_code != 200: |
| 3236 | print(resp.text) |
| 3237 | return "Failed to list certificates" |
| 3238 | else: |
| 3239 | print("List certificates complete.") |
| 3240 | return resp.text |
| 3241 | |
Marri Devender Rao | 3cdf8ae | 2019-07-01 06:01:40 -0500 | [diff] [blame] | 3242 | def certificateGenerateCSR(host, args, session): |
| 3243 | """ |
| 3244 | Called by certificate management function. Generate CSR for server/ |
| 3245 | client certificates |
| 3246 | Example: |
Marri Devender Rao | df0e1a4 | 2019-09-09 08:18:27 -0500 | [diff] [blame] | 3247 | certificate generatecsr server NJ w3.ibm.com US IBM IBM-UNIT NY EC prime256v1 cp abc.com an.com,bm.com gn sn un in |
| 3248 | certificate generatecsr client NJ w3.ibm.com US IBM IBM-UNIT NY EC prime256v1 cp abc.com an.com,bm.com gn sn un in |
Marri Devender Rao | 3cdf8ae | 2019-07-01 06:01:40 -0500 | [diff] [blame] | 3249 | @param host: string, the hostname or IP address of the bmc |
| 3250 | @param args: contains additional arguments used by the certificate replace sub command |
| 3251 | @param session: the active session to use |
| 3252 | """ |
| 3253 | if not redfishSupportPresent(host, session): |
| 3254 | return "Not supported"; |
| 3255 | |
| 3256 | httpHeader = {'Content-Type': 'application/octet-stream'} |
| 3257 | httpHeader.update(xAuthHeader) |
| 3258 | url = ""; |
| 3259 | if(args.type.lower() == 'server'): |
| 3260 | url = "/redfish/v1/Managers/bmc/NetworkProtocol/HTTPS/Certificates/" |
Marri Devender Rao | 88064f0 | 2019-08-19 09:00:30 -0500 | [diff] [blame] | 3261 | usage_list = ["ServerAuthentication"] |
Marri Devender Rao | 3cdf8ae | 2019-07-01 06:01:40 -0500 | [diff] [blame] | 3262 | elif(args.type.lower() == 'client'): |
| 3263 | url = "/redfish/v1/AccountService/LDAP/Certificates/" |
Marri Devender Rao | 88064f0 | 2019-08-19 09:00:30 -0500 | [diff] [blame] | 3264 | usage_list = ["ClientAuthentication"] |
Marri Devender Rao | 3cdf8ae | 2019-07-01 06:01:40 -0500 | [diff] [blame] | 3265 | elif(args.type.lower() == 'authority'): |
| 3266 | url = "/redfish/v1/Managers/bmc/Truststore/Certificates/" |
| 3267 | print("Generating CSR url=" + url) |
| 3268 | generateCSRUrl = "https://" + host + \ |
| 3269 | "/redfish/v1/CertificateService/Actions/CertificateService.GenerateCSR" |
| 3270 | try: |
Marri Devender Rao | 3cdf8ae | 2019-07-01 06:01:40 -0500 | [diff] [blame] | 3271 | alt_name_list = args.alternativeNames.split(",") |
| 3272 | data ={"CertificateCollection":{"@odata.id":url}, |
| 3273 | "CommonName":args.commonName, "City":args.city, |
| 3274 | "Country":args.country, "Organization":args.organization, |
| 3275 | "OrganizationalUnit":args.organizationUnit, "State":args.state, |
Marri Devender Rao | df0e1a4 | 2019-09-09 08:18:27 -0500 | [diff] [blame] | 3276 | "KeyPairAlgorithm":args.keyPairAlgorithm, "KeyCurveId":args.keyCurveId, |
Marri Devender Rao | 3cdf8ae | 2019-07-01 06:01:40 -0500 | [diff] [blame] | 3277 | "AlternativeNames":alt_name_list, "ContactPerson":args.contactPerson, |
| 3278 | "Email":args.email, "GivenName":args.givenname, "Initials":args.initials, |
| 3279 | "KeyUsage":usage_list, "Surname":args.surname, |
| 3280 | "UnstructuredName":args.unstructuredname} |
| 3281 | resp = session.post(generateCSRUrl, headers=httpHeader, |
| 3282 | json=data, verify=False) |
| 3283 | except(requests.exceptions.Timeout): |
| 3284 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3285 | except(requests.exceptions.ConnectionError) as err: |
| 3286 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 3287 | if resp.status_code != 200: |
| 3288 | print(resp.text) |
| 3289 | return "Failed to generate CSR" |
| 3290 | else: |
| 3291 | print("GenerateCSR complete.") |
| 3292 | return resp.text |
| 3293 | |
RAJESWARAN THILLAIGOVINDAN | 87f087b | 2019-05-08 04:15:26 -0500 | [diff] [blame] | 3294 | def enableLDAPConfig(host, args, session): |
Ratan Gupta | 9166cd2 | 2018-10-01 18:09:40 +0530 | [diff] [blame] | 3295 | """ |
| 3296 | Called by the ldap function. Configures LDAP. |
| 3297 | |
| 3298 | @param host: string, the hostname or IP address of the bmc |
| 3299 | @param args: contains additional arguments used by the ldap subcommand |
| 3300 | @param session: the active session to use |
| 3301 | @param args.json: boolean, if this flag is set to true, the output will |
| 3302 | be provided in json format for programmatic consumption |
| 3303 | """ |
| 3304 | |
RAJESWARAN THILLAIGOVINDAN | 87f087b | 2019-05-08 04:15:26 -0500 | [diff] [blame] | 3305 | if(isRedfishSupport): |
| 3306 | return enableLDAP(host, args, session) |
| 3307 | else: |
| 3308 | return enableLegacyLDAP(host, args, session) |
| 3309 | |
| 3310 | def enableLegacyLDAP(host, args, session): |
| 3311 | """ |
| 3312 | Called by the ldap function. Configures LDAP on Lagecy systems. |
| 3313 | |
| 3314 | @param host: string, the hostname or IP address of the bmc |
| 3315 | @param args: contains additional arguments used by the ldap subcommand |
| 3316 | @param session: the active session to use |
| 3317 | @param args.json: boolean, if this flag is set to true, the output will |
| 3318 | be provided in json format for programmatic consumption |
| 3319 | """ |
| 3320 | |
Ratan Gupta | 9166cd2 | 2018-10-01 18:09:40 +0530 | [diff] [blame] | 3321 | url='https://'+host+'/xyz/openbmc_project/user/ldap/action/CreateConfig' |
Ratan Gupta | 9166cd2 | 2018-10-01 18:09:40 +0530 | [diff] [blame] | 3322 | scope = { |
| 3323 | 'sub' : 'xyz.openbmc_project.User.Ldap.Create.SearchScope.sub', |
| 3324 | 'one' : 'xyz.openbmc_project.User.Ldap.Create.SearchScope.one', |
| 3325 | 'base': 'xyz.openbmc_project.User.Ldap.Create.SearchScope.base' |
| 3326 | } |
| 3327 | |
| 3328 | serverType = { |
RAJESWARAN THILLAIGOVINDAN | 87f087b | 2019-05-08 04:15:26 -0500 | [diff] [blame] | 3329 | 'ActiveDirectory' : 'xyz.openbmc_project.User.Ldap.Create.Type.ActiveDirectory', |
| 3330 | 'OpenLDAP' : 'xyz.openbmc_project.User.Ldap.Create.Type.OpenLdap' |
Ratan Gupta | 9166cd2 | 2018-10-01 18:09:40 +0530 | [diff] [blame] | 3331 | } |
| 3332 | |
| 3333 | data = {"data": [args.uri, args.bindDN, args.baseDN, args.bindPassword, scope[args.scope], serverType[args.serverType]]} |
| 3334 | |
| 3335 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 3336 | res = session.post(url, headers=jsonHeader, json=data, verify=False, timeout=baseTimeout) |
Ratan Gupta | 9166cd2 | 2018-10-01 18:09:40 +0530 | [diff] [blame] | 3337 | except(requests.exceptions.Timeout): |
| 3338 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3339 | except(requests.exceptions.ConnectionError) as err: |
| 3340 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 3341 | |
| 3342 | return res.text |
| 3343 | |
RAJESWARAN THILLAIGOVINDAN | 87f087b | 2019-05-08 04:15:26 -0500 | [diff] [blame] | 3344 | def enableLDAP(host, args, session): |
| 3345 | """ |
| 3346 | Called by the ldap function. Configures LDAP for systems with latest user-manager design changes |
| 3347 | |
| 3348 | @param host: string, the hostname or IP address of the bmc |
| 3349 | @param args: contains additional arguments used by the ldap subcommand |
| 3350 | @param session: the active session to use |
| 3351 | @param args.json: boolean, if this flag is set to true, the output will |
| 3352 | be provided in json format for programmatic consumption |
| 3353 | """ |
| 3354 | |
| 3355 | scope = { |
| 3356 | 'sub' : 'xyz.openbmc_project.User.Ldap.Config.SearchScope.sub', |
| 3357 | 'one' : 'xyz.openbmc_project.User.Ldap.Config.SearchScope.one', |
| 3358 | 'base': 'xyz.openbmc_project.User.Ldap.Config.SearchScope.base' |
| 3359 | } |
| 3360 | |
| 3361 | serverType = { |
| 3362 | 'ActiveDirectory' : 'xyz.openbmc_project.User.Ldap.Config.Type.ActiveDirectory', |
| 3363 | 'OpenLDAP' : 'xyz.openbmc_project.User.Ldap.Config.Type.OpenLdap' |
| 3364 | } |
| 3365 | |
| 3366 | url = "https://"+host+"/xyz/openbmc_project/user/ldap/" |
| 3367 | |
| 3368 | serverTypeEnabled = getLDAPTypeEnabled(host,session) |
| 3369 | serverTypeToBeEnabled = args.serverType |
| 3370 | |
| 3371 | #If the given LDAP type is already enabled, then return |
| 3372 | if (serverTypeToBeEnabled == serverTypeEnabled): |
| 3373 | return("Server type " + serverTypeToBeEnabled + " is already enabled...") |
| 3374 | |
| 3375 | try: |
| 3376 | |
| 3377 | # Copy the role map from the currently enabled LDAP server type |
| 3378 | # to the newly enabled server type |
| 3379 | # Disable the currently enabled LDAP server type. Unless |
| 3380 | # it is disabled, we cannot enable a new LDAP server type |
| 3381 | if (serverTypeEnabled is not None): |
| 3382 | |
| 3383 | if (serverTypeToBeEnabled != serverTypeEnabled): |
| 3384 | res = syncRoleMap(host,args,session,serverTypeEnabled,serverTypeToBeEnabled) |
| 3385 | |
| 3386 | data = "{\"data\": 0 }" |
| 3387 | res = session.put(url + serverTypeMap[serverTypeEnabled] + '/attr/Enabled', headers=jsonHeader, data=data, verify=False, timeout=baseTimeout) |
| 3388 | |
| 3389 | data = {"data": args.baseDN} |
| 3390 | res = session.put(url + serverTypeMap[serverTypeToBeEnabled] + '/attr/LDAPBaseDN', headers=jsonHeader, json=data, verify=False, timeout=baseTimeout) |
| 3391 | if (res.status_code != requests.codes.ok): |
| 3392 | print("Updates to the property LDAPBaseDN failed...") |
| 3393 | return(res.text) |
| 3394 | |
| 3395 | data = {"data": args.bindDN} |
| 3396 | res = session.put(url + serverTypeMap[serverTypeToBeEnabled] + '/attr/LDAPBindDN', headers=jsonHeader, json=data, verify=False, timeout=baseTimeout) |
| 3397 | if (res.status_code != requests.codes.ok): |
| 3398 | print("Updates to the property LDAPBindDN failed...") |
| 3399 | return(res.text) |
| 3400 | |
| 3401 | data = {"data": args.bindPassword} |
| 3402 | res = session.put(url + serverTypeMap[serverTypeToBeEnabled] + '/attr/LDAPBindDNPassword', headers=jsonHeader, json=data, verify=False, timeout=baseTimeout) |
| 3403 | if (res.status_code != requests.codes.ok): |
| 3404 | print("Updates to the property LDAPBindDNPassword failed...") |
| 3405 | return(res.text) |
| 3406 | |
| 3407 | data = {"data": scope[args.scope]} |
| 3408 | res = session.put(url + serverTypeMap[serverTypeToBeEnabled] + '/attr/LDAPSearchScope', headers=jsonHeader, json=data, verify=False, timeout=baseTimeout) |
| 3409 | if (res.status_code != requests.codes.ok): |
| 3410 | print("Updates to the property LDAPSearchScope failed...") |
| 3411 | return(res.text) |
| 3412 | |
| 3413 | data = {"data": args.uri} |
| 3414 | res = session.put(url + serverTypeMap[serverTypeToBeEnabled] + '/attr/LDAPServerURI', headers=jsonHeader, json=data, verify=False, timeout=baseTimeout) |
| 3415 | if (res.status_code != requests.codes.ok): |
| 3416 | print("Updates to the property LDAPServerURI failed...") |
| 3417 | return(res.text) |
| 3418 | |
| 3419 | data = {"data": args.groupAttrName} |
| 3420 | res = session.put(url + serverTypeMap[serverTypeToBeEnabled] + '/attr/GroupNameAttribute', headers=jsonHeader, json=data, verify=False, timeout=baseTimeout) |
| 3421 | if (res.status_code != requests.codes.ok): |
| 3422 | print("Updates to the property GroupNameAttribute failed...") |
| 3423 | return(res.text) |
| 3424 | |
| 3425 | data = {"data": args.userAttrName} |
| 3426 | res = session.put(url + serverTypeMap[serverTypeToBeEnabled] + '/attr/UserNameAttribute', headers=jsonHeader, json=data, verify=False, timeout=baseTimeout) |
| 3427 | if (res.status_code != requests.codes.ok): |
| 3428 | print("Updates to the property UserNameAttribute failed...") |
| 3429 | return(res.text) |
| 3430 | |
| 3431 | #After updating the properties, enable the new server type |
| 3432 | data = "{\"data\": 1 }" |
| 3433 | res = session.put(url + serverTypeMap[serverTypeToBeEnabled] + '/attr/Enabled', headers=jsonHeader, data=data, verify=False, timeout=baseTimeout) |
| 3434 | |
| 3435 | except(requests.exceptions.Timeout): |
| 3436 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3437 | except(requests.exceptions.ConnectionError) as err: |
| 3438 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 3439 | return res.text |
Ratan Gupta | 9166cd2 | 2018-10-01 18:09:40 +0530 | [diff] [blame] | 3440 | |
| 3441 | def disableLDAP(host, args, session): |
| 3442 | """ |
| 3443 | Called by the ldap function. Deletes the LDAP Configuration. |
| 3444 | |
| 3445 | @param host: string, the hostname or IP address of the bmc |
| 3446 | @param args: contains additional arguments used by the ldap subcommand |
| 3447 | @param session: the active session to use |
| 3448 | @param args.json: boolean, if this flag is set to true, the output |
| 3449 | will be provided in json format for programmatic consumption |
| 3450 | """ |
| 3451 | |
Ratan Gupta | 9166cd2 | 2018-10-01 18:09:40 +0530 | [diff] [blame] | 3452 | try: |
RAJESWARAN THILLAIGOVINDAN | 87f087b | 2019-05-08 04:15:26 -0500 | [diff] [blame] | 3453 | if (isRedfishSupport) : |
| 3454 | |
| 3455 | url = "https://"+host+"/xyz/openbmc_project/user/ldap/" |
| 3456 | |
| 3457 | serverTypeEnabled = getLDAPTypeEnabled(host,session) |
| 3458 | |
| 3459 | if (serverTypeEnabled is not None): |
| 3460 | #To keep the role map in sync, |
| 3461 | #If the server type being disabled has role map, then |
| 3462 | # - copy the role map to the other server type(s) |
| 3463 | for serverType in serverTypeMap.keys(): |
| 3464 | if (serverType != serverTypeEnabled): |
| 3465 | res = syncRoleMap(host,args,session,serverTypeEnabled,serverType) |
| 3466 | |
| 3467 | #Disable the currently enabled LDAP server type |
| 3468 | data = "{\"data\": 0 }" |
| 3469 | res = session.put(url + serverTypeMap[serverTypeEnabled] + '/attr/Enabled', headers=jsonHeader, data=data, verify=False, timeout=baseTimeout) |
| 3470 | |
| 3471 | else: |
| 3472 | return("LDAP server has not been enabled...") |
| 3473 | |
| 3474 | else : |
| 3475 | url='https://'+host+'/xyz/openbmc_project/user/ldap/config/action/delete' |
| 3476 | data = {"data": []} |
| 3477 | res = session.post(url, headers=jsonHeader, json=data, verify=False, timeout=baseTimeout) |
| 3478 | |
Ratan Gupta | 9166cd2 | 2018-10-01 18:09:40 +0530 | [diff] [blame] | 3479 | except(requests.exceptions.Timeout): |
| 3480 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3481 | except(requests.exceptions.ConnectionError) as err: |
| 3482 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 3483 | |
| 3484 | return res.text |
| 3485 | |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 3486 | def enableDHCP(host, args, session): |
| 3487 | |
| 3488 | """ |
| 3489 | Called by the network function. Enables DHCP. |
| 3490 | |
| 3491 | @param host: string, the hostname or IP address of the bmc |
| 3492 | @param args: contains additional arguments used by the ldap subcommand |
| 3493 | args.json: boolean, if this flag is set to true, the output |
| 3494 | will be provided in json format for programmatic consumption |
| 3495 | @param session: the active session to use |
| 3496 | """ |
| 3497 | |
| 3498 | url = "https://"+host+"/xyz/openbmc_project/network/"+args.Interface+\ |
| 3499 | "/attr/DHCPEnabled" |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 3500 | data = "{\"data\": 1 }" |
| 3501 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 3502 | res = session.put(url, headers=jsonHeader, data=data, verify=False, |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 3503 | timeout=baseTimeout) |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 3504 | |
| 3505 | except(requests.exceptions.Timeout): |
| 3506 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3507 | except(requests.exceptions.ConnectionError) as err: |
| 3508 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 3509 | if res.status_code == 403: |
| 3510 | return "The specified Interface"+"("+args.Interface+")"+\ |
| 3511 | " doesn't exist" |
| 3512 | |
| 3513 | return res.text |
| 3514 | |
| 3515 | |
| 3516 | def disableDHCP(host, args, session): |
| 3517 | """ |
| 3518 | Called by the network function. Disables DHCP. |
| 3519 | |
| 3520 | @param host: string, the hostname or IP address of the bmc |
| 3521 | @param args: contains additional arguments used by the ldap subcommand |
| 3522 | args.json: boolean, if this flag is set to true, the output |
| 3523 | will be provided in json format for programmatic consumption |
| 3524 | @param session: the active session to use |
| 3525 | """ |
| 3526 | |
| 3527 | url = "https://"+host+"/xyz/openbmc_project/network/"+args.Interface+\ |
| 3528 | "/attr/DHCPEnabled" |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 3529 | data = "{\"data\": 0 }" |
| 3530 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 3531 | res = session.put(url, headers=jsonHeader, data=data, verify=False, |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 3532 | timeout=baseTimeout) |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 3533 | except(requests.exceptions.Timeout): |
| 3534 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3535 | except(requests.exceptions.ConnectionError) as err: |
| 3536 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 3537 | if res.status_code == 403: |
| 3538 | return "The specified Interface"+"("+args.Interface+")"+\ |
| 3539 | " doesn't exist" |
| 3540 | return res.text |
| 3541 | |
| 3542 | |
| 3543 | def getHostname(host, args, session): |
| 3544 | |
| 3545 | """ |
| 3546 | Called by the network function. Prints out the Hostname. |
| 3547 | |
| 3548 | @param host: string, the hostname or IP address of the bmc |
| 3549 | @param args: contains additional arguments used by the ldap subcommand |
| 3550 | args.json: boolean, if this flag is set to true, the output |
| 3551 | will be provided in json format for programmatic consumption |
| 3552 | @param session: the active session to use |
| 3553 | """ |
| 3554 | |
| 3555 | url = "https://"+host+"/xyz/openbmc_project/network/config/attr/HostName" |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 3556 | |
| 3557 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 3558 | res = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 3559 | except(requests.exceptions.Timeout): |
| 3560 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3561 | except(requests.exceptions.ConnectionError) as err: |
| 3562 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 3563 | |
| 3564 | return res.text |
| 3565 | |
| 3566 | |
| 3567 | def setHostname(host, args, session): |
| 3568 | """ |
| 3569 | Called by the network function. Sets the Hostname. |
| 3570 | |
| 3571 | @param host: string, the hostname or IP address of the bmc |
| 3572 | @param args: contains additional arguments used by the ldap subcommand |
| 3573 | args.json: boolean, if this flag is set to true, the output |
| 3574 | will be provided in json format for programmatic consumption |
| 3575 | @param session: the active session to use |
| 3576 | """ |
| 3577 | |
| 3578 | url = "https://"+host+"/xyz/openbmc_project/network/config/attr/HostName" |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 3579 | |
| 3580 | data = {"data": args.HostName} |
| 3581 | |
| 3582 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 3583 | res = session.put(url, headers=jsonHeader, json=data, verify=False, |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 3584 | timeout=baseTimeout) |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 3585 | except(requests.exceptions.Timeout): |
| 3586 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3587 | except(requests.exceptions.ConnectionError) as err: |
| 3588 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 3589 | |
| 3590 | return res.text |
| 3591 | |
| 3592 | |
| 3593 | def getDomainName(host, args, session): |
| 3594 | |
| 3595 | """ |
| 3596 | Called by the network function. Prints out the DomainName. |
| 3597 | |
| 3598 | @param host: string, the hostname or IP address of the bmc |
| 3599 | @param args: contains additional arguments used by the ldap subcommand |
| 3600 | args.json: boolean, if this flag is set to true, the output |
| 3601 | will be provided in json format for programmatic consumption |
| 3602 | @param session: the active session to use |
| 3603 | """ |
| 3604 | |
| 3605 | url = "https://"+host+"/xyz/openbmc_project/network/"+args.Interface+\ |
| 3606 | "/attr/DomainName" |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 3607 | |
| 3608 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 3609 | res = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 3610 | except(requests.exceptions.Timeout): |
| 3611 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3612 | except(requests.exceptions.ConnectionError) as err: |
| 3613 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 3614 | if res.status_code == 404: |
Sunitha Harish | f9bb2fa | 2019-09-19 01:48:00 -0500 | [diff] [blame] | 3615 | return "The DomainName is not configured on Interface"+"("+args.Interface+")" |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 3616 | |
| 3617 | return res.text |
| 3618 | |
| 3619 | |
| 3620 | def setDomainName(host, args, session): |
| 3621 | """ |
| 3622 | Called by the network function. Sets the DomainName. |
| 3623 | |
| 3624 | @param host: string, the hostname or IP address of the bmc |
| 3625 | @param args: contains additional arguments used by the ldap subcommand |
| 3626 | args.json: boolean, if this flag is set to true, the output |
| 3627 | will be provided in json format for programmatic consumption |
| 3628 | @param session: the active session to use |
| 3629 | """ |
| 3630 | |
| 3631 | url = "https://"+host+"/xyz/openbmc_project/network/"+args.Interface+\ |
| 3632 | "/attr/DomainName" |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 3633 | |
| 3634 | data = {"data": args.DomainName.split(",")} |
| 3635 | |
| 3636 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 3637 | res = session.put(url, headers=jsonHeader, json=data, verify=False, |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 3638 | timeout=baseTimeout) |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 3639 | except(requests.exceptions.Timeout): |
| 3640 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3641 | except(requests.exceptions.ConnectionError) as err: |
| 3642 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 3643 | if res.status_code == 403: |
| 3644 | return "The specified Interface"+"("+args.Interface+")"+\ |
| 3645 | " doesn't exist" |
| 3646 | |
| 3647 | return res.text |
| 3648 | |
| 3649 | |
| 3650 | def getMACAddress(host, args, session): |
| 3651 | |
| 3652 | """ |
| 3653 | Called by the network function. Prints out the MACAddress. |
| 3654 | |
| 3655 | @param host: string, the hostname or IP address of the bmc |
| 3656 | @param args: contains additional arguments used by the ldap subcommand |
| 3657 | args.json: boolean, if this flag is set to true, the output |
| 3658 | will be provided in json format for programmatic consumption |
| 3659 | @param session: the active session to use |
| 3660 | """ |
| 3661 | |
| 3662 | url = "https://"+host+"/xyz/openbmc_project/network/"+args.Interface+\ |
| 3663 | "/attr/MACAddress" |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 3664 | |
| 3665 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 3666 | res = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 3667 | except(requests.exceptions.Timeout): |
| 3668 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3669 | except(requests.exceptions.ConnectionError) as err: |
| 3670 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 3671 | if res.status_code == 404: |
| 3672 | return "The specified Interface"+"("+args.Interface+")"+\ |
| 3673 | " doesn't exist" |
| 3674 | |
| 3675 | return res.text |
| 3676 | |
| 3677 | |
| 3678 | def setMACAddress(host, args, session): |
| 3679 | """ |
| 3680 | Called by the network function. Sets the MACAddress. |
| 3681 | |
| 3682 | @param host: string, the hostname or IP address of the bmc |
| 3683 | @param args: contains additional arguments used by the ldap subcommand |
| 3684 | args.json: boolean, if this flag is set to true, the output |
| 3685 | will be provided in json format for programmatic consumption |
| 3686 | @param session: the active session to use |
| 3687 | """ |
| 3688 | |
| 3689 | url = "https://"+host+"/xyz/openbmc_project/network/"+args.Interface+\ |
| 3690 | "/attr/MACAddress" |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 3691 | |
| 3692 | data = {"data": args.MACAddress} |
| 3693 | |
| 3694 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 3695 | res = session.put(url, headers=jsonHeader, json=data, verify=False, |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 3696 | timeout=baseTimeout) |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 3697 | except(requests.exceptions.Timeout): |
| 3698 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3699 | except(requests.exceptions.ConnectionError) as err: |
| 3700 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 3701 | if res.status_code == 403: |
| 3702 | return "The specified Interface"+"("+args.Interface+")"+\ |
| 3703 | " doesn't exist" |
| 3704 | |
| 3705 | return res.text |
| 3706 | |
| 3707 | |
| 3708 | def getDefaultGateway(host, args, session): |
| 3709 | |
| 3710 | """ |
| 3711 | Called by the network function. Prints out the DefaultGateway. |
| 3712 | |
| 3713 | @param host: string, the hostname or IP address of the bmc |
| 3714 | @param args: contains additional arguments used by the ldap subcommand |
| 3715 | args.json: boolean, if this flag is set to true, the output |
| 3716 | will be provided in json format for programmatic consumption |
| 3717 | @param session: the active session to use |
| 3718 | """ |
| 3719 | |
| 3720 | url = "https://"+host+"/xyz/openbmc_project/network/config/attr/DefaultGateway" |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 3721 | |
| 3722 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 3723 | res = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 3724 | except(requests.exceptions.Timeout): |
| 3725 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3726 | except(requests.exceptions.ConnectionError) as err: |
| 3727 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 3728 | if res.status_code == 404: |
| 3729 | return "Failed to get Default Gateway info!!" |
| 3730 | |
| 3731 | return res.text |
| 3732 | |
| 3733 | |
| 3734 | def setDefaultGateway(host, args, session): |
| 3735 | """ |
| 3736 | Called by the network function. Sets the DefaultGateway. |
| 3737 | |
| 3738 | @param host: string, the hostname or IP address of the bmc |
| 3739 | @param args: contains additional arguments used by the ldap subcommand |
| 3740 | args.json: boolean, if this flag is set to true, the output |
| 3741 | will be provided in json format for programmatic consumption |
| 3742 | @param session: the active session to use |
| 3743 | """ |
| 3744 | |
| 3745 | url = "https://"+host+"/xyz/openbmc_project/network/config/attr/DefaultGateway" |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 3746 | |
| 3747 | data = {"data": args.DefaultGW} |
| 3748 | |
| 3749 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 3750 | res = session.put(url, headers=jsonHeader, json=data, verify=False, |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 3751 | timeout=baseTimeout) |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 3752 | except(requests.exceptions.Timeout): |
| 3753 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3754 | except(requests.exceptions.ConnectionError) as err: |
| 3755 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 3756 | if res.status_code == 403: |
| 3757 | return "Failed to set Default Gateway!!" |
| 3758 | |
| 3759 | return res.text |
| 3760 | |
| 3761 | |
| 3762 | def viewNWConfig(host, args, session): |
| 3763 | """ |
| 3764 | Called by the ldap function. Prints out network configured properties |
| 3765 | |
| 3766 | @param host: string, the hostname or IP address of the bmc |
| 3767 | @param args: contains additional arguments used by the ldap subcommand |
| 3768 | args.json: boolean, if this flag is set to true, the output |
| 3769 | will be provided in json format for programmatic consumption |
| 3770 | @param session: the active session to use |
| 3771 | @return returns LDAP's configured properties. |
| 3772 | """ |
| 3773 | url = "https://"+host+"/xyz/openbmc_project/network/enumerate" |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 3774 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 3775 | res = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 3776 | except(requests.exceptions.Timeout): |
| 3777 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3778 | except(requests.exceptions.ConnectionError) as err: |
| 3779 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 3780 | except(requests.exceptions.RequestException) as err: |
| 3781 | return connectionErrHandler(args.json, "RequestException", err) |
| 3782 | if res.status_code == 404: |
| 3783 | return "LDAP server config has not been created" |
| 3784 | return res.text |
| 3785 | |
| 3786 | |
| 3787 | def getDNS(host, args, session): |
| 3788 | |
| 3789 | """ |
| 3790 | Called by the network function. Prints out DNS servers on the interface |
| 3791 | |
| 3792 | @param host: string, the hostname or IP address of the bmc |
| 3793 | @param args: contains additional arguments used by the ldap subcommand |
| 3794 | args.json: boolean, if this flag is set to true, the output |
| 3795 | will be provided in json format for programmatic consumption |
| 3796 | @param session: the active session to use |
| 3797 | """ |
| 3798 | |
| 3799 | url = "https://" + host + "/xyz/openbmc_project/network/" + args.Interface\ |
| 3800 | + "/attr/Nameservers" |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 3801 | |
| 3802 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 3803 | res = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 3804 | except(requests.exceptions.Timeout): |
| 3805 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3806 | except(requests.exceptions.ConnectionError) as err: |
| 3807 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 3808 | if res.status_code == 404: |
Sunitha Harish | f9bb2fa | 2019-09-19 01:48:00 -0500 | [diff] [blame] | 3809 | return "The NameServer is not configured on Interface"+"("+args.Interface+")" |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 3810 | |
| 3811 | return res.text |
| 3812 | |
| 3813 | |
| 3814 | def setDNS(host, args, session): |
| 3815 | """ |
| 3816 | Called by the network function. Sets DNS servers on the interface. |
| 3817 | |
| 3818 | @param host: string, the hostname or IP address of the bmc |
| 3819 | @param args: contains additional arguments used by the ldap subcommand |
| 3820 | args.json: boolean, if this flag is set to true, the output |
| 3821 | will be provided in json format for programmatic consumption |
| 3822 | @param session: the active session to use |
| 3823 | """ |
| 3824 | |
| 3825 | url = "https://" + host + "/xyz/openbmc_project/network/" + args.Interface\ |
| 3826 | + "/attr/Nameservers" |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 3827 | |
| 3828 | data = {"data": args.DNSServers.split(",")} |
| 3829 | |
| 3830 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 3831 | res = session.put(url, headers=jsonHeader, json=data, verify=False, |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 3832 | timeout=baseTimeout) |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 3833 | except(requests.exceptions.Timeout): |
| 3834 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3835 | except(requests.exceptions.ConnectionError) as err: |
| 3836 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 3837 | if res.status_code == 403: |
| 3838 | return "The specified Interface"+"("+args.Interface+")" +\ |
| 3839 | " doesn't exist" |
| 3840 | |
| 3841 | return res.text |
| 3842 | |
| 3843 | |
| 3844 | def getNTP(host, args, session): |
| 3845 | |
| 3846 | """ |
| 3847 | Called by the network function. Prints out NTP servers on the interface |
| 3848 | |
| 3849 | @param host: string, the hostname or IP address of the bmc |
| 3850 | @param args: contains additional arguments used by the ldap subcommand |
| 3851 | args.json: boolean, if this flag is set to true, the output |
| 3852 | will be provided in json format for programmatic consumption |
| 3853 | @param session: the active session to use |
| 3854 | """ |
| 3855 | |
| 3856 | url = "https://" + host + "/xyz/openbmc_project/network/" + args.Interface\ |
| 3857 | + "/attr/NTPServers" |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 3858 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 3859 | res = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 3860 | except(requests.exceptions.Timeout): |
| 3861 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3862 | except(requests.exceptions.ConnectionError) as err: |
| 3863 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 3864 | if res.status_code == 404: |
Sunitha Harish | f9bb2fa | 2019-09-19 01:48:00 -0500 | [diff] [blame] | 3865 | return "The NTPServer is not configured on Interface"+"("+args.Interface+")" |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 3866 | |
| 3867 | return res.text |
| 3868 | |
| 3869 | |
| 3870 | def setNTP(host, args, session): |
| 3871 | """ |
| 3872 | Called by the network function. Sets NTP servers on the interface. |
| 3873 | |
| 3874 | @param host: string, the hostname or IP address of the bmc |
| 3875 | @param args: contains additional arguments used by the ldap subcommand |
| 3876 | args.json: boolean, if this flag is set to true, the output |
| 3877 | will be provided in json format for programmatic consumption |
| 3878 | @param session: the active session to use |
| 3879 | """ |
| 3880 | |
| 3881 | url = "https://" + host + "/xyz/openbmc_project/network/" + args.Interface\ |
| 3882 | + "/attr/NTPServers" |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 3883 | |
| 3884 | data = {"data": args.NTPServers.split(",")} |
| 3885 | |
| 3886 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 3887 | res = session.put(url, headers=jsonHeader, json=data, verify=False, |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 3888 | timeout=baseTimeout) |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 3889 | except(requests.exceptions.Timeout): |
| 3890 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3891 | except(requests.exceptions.ConnectionError) as err: |
| 3892 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 3893 | if res.status_code == 403: |
| 3894 | return "The specified Interface"+"("+args.Interface+")" +\ |
| 3895 | " doesn't exist" |
| 3896 | |
| 3897 | return res.text |
| 3898 | |
| 3899 | |
Nagaraju Goruganti | 97a2060 | 2018-11-16 03:06:08 -0600 | [diff] [blame] | 3900 | def addIP(host, args, session): |
| 3901 | """ |
| 3902 | Called by the network function. Configures IP address on given interface |
| 3903 | |
| 3904 | @param host: string, the hostname or IP address of the bmc |
| 3905 | @param args: contains additional arguments used by the ldap subcommand |
| 3906 | args.json: boolean, if this flag is set to true, the output |
| 3907 | will be provided in json format for programmatic consumption |
| 3908 | @param session: the active session to use |
| 3909 | """ |
| 3910 | |
| 3911 | url = "https://" + host + "/xyz/openbmc_project/network/" + args.Interface\ |
| 3912 | + "/action/IP" |
Nagaraju Goruganti | 97a2060 | 2018-11-16 03:06:08 -0600 | [diff] [blame] | 3913 | protocol = { |
| 3914 | 'ipv4': 'xyz.openbmc_project.Network.IP.Protocol.IPv4', |
| 3915 | 'ipv6': 'xyz.openbmc_project.Network.IP.Protocol.IPv6' |
| 3916 | } |
| 3917 | |
| 3918 | data = {"data": [protocol[args.type], args.address, int(args.prefixLength), |
| 3919 | args.gateway]} |
| 3920 | |
| 3921 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 3922 | res = session.post(url, headers=jsonHeader, json=data, verify=False, |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 3923 | timeout=baseTimeout) |
Nagaraju Goruganti | 97a2060 | 2018-11-16 03:06:08 -0600 | [diff] [blame] | 3924 | except(requests.exceptions.Timeout): |
| 3925 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3926 | except(requests.exceptions.ConnectionError) as err: |
| 3927 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 3928 | if res.status_code == 404: |
| 3929 | return "The specified Interface" + "(" + args.Interface + ")" +\ |
| 3930 | " doesn't exist" |
| 3931 | |
| 3932 | return res.text |
| 3933 | |
| 3934 | |
| 3935 | def getIP(host, args, session): |
| 3936 | """ |
| 3937 | Called by the network function. Prints out IP address of given interface |
| 3938 | |
| 3939 | @param host: string, the hostname or IP address of the bmc |
| 3940 | @param args: contains additional arguments used by the ldap subcommand |
| 3941 | args.json: boolean, if this flag is set to true, the output |
| 3942 | will be provided in json format for programmatic consumption |
| 3943 | @param session: the active session to use |
| 3944 | """ |
| 3945 | |
| 3946 | url = "https://" + host+"/xyz/openbmc_project/network/" + args.Interface +\ |
| 3947 | "/enumerate" |
Nagaraju Goruganti | 97a2060 | 2018-11-16 03:06:08 -0600 | [diff] [blame] | 3948 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 3949 | res = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
Nagaraju Goruganti | 97a2060 | 2018-11-16 03:06:08 -0600 | [diff] [blame] | 3950 | except(requests.exceptions.Timeout): |
| 3951 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3952 | except(requests.exceptions.ConnectionError) as err: |
| 3953 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 3954 | if res.status_code == 404: |
| 3955 | return "The specified Interface" + "(" + args.Interface + ")" +\ |
| 3956 | " doesn't exist" |
| 3957 | |
| 3958 | return res.text |
| 3959 | |
| 3960 | |
| 3961 | def deleteIP(host, args, session): |
| 3962 | """ |
| 3963 | Called by the network function. Deletes the IP address from given Interface |
| 3964 | |
| 3965 | @param host: string, the hostname or IP address of the bmc |
| 3966 | @param args: contains additional arguments used by the ldap subcommand |
| 3967 | @param session: the active session to use |
| 3968 | @param args.json: boolean, if this flag is set to true, the output |
| 3969 | will be provided in json format for programmatic consumption |
| 3970 | """ |
| 3971 | |
| 3972 | url = "https://"+host+"/xyz/openbmc_project/network/" + args.Interface+\ |
| 3973 | "/enumerate" |
Nagaraju Goruganti | 97a2060 | 2018-11-16 03:06:08 -0600 | [diff] [blame] | 3974 | data = {"data": []} |
| 3975 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 3976 | res = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
Nagaraju Goruganti | 97a2060 | 2018-11-16 03:06:08 -0600 | [diff] [blame] | 3977 | except(requests.exceptions.Timeout): |
| 3978 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3979 | except(requests.exceptions.ConnectionError) as err: |
| 3980 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 3981 | if res.status_code == 404: |
| 3982 | return "The specified Interface" + "(" + args.Interface + ")" +\ |
| 3983 | " doesn't exist" |
| 3984 | objDict = json.loads(res.text) |
| 3985 | if not objDict['data']: |
| 3986 | return "No object found for given address on given Interface" |
Nagaraju Goruganti | 97a2060 | 2018-11-16 03:06:08 -0600 | [diff] [blame] | 3987 | for obj in objDict['data']: |
Sunitha Harish | 0baf637 | 2019-07-31 03:59:03 -0500 | [diff] [blame] | 3988 | try: |
| 3989 | if args.address in objDict['data'][obj]['Address']: |
| 3990 | url = "https://"+host+obj+"/action/Delete" |
| 3991 | try: |
| 3992 | res = session.post(url, headers=jsonHeader, json=data, |
| 3993 | verify=False, timeout=baseTimeout) |
| 3994 | except(requests.exceptions.Timeout): |
| 3995 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3996 | except(requests.exceptions.ConnectionError) as err: |
| 3997 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 3998 | return res.text |
| 3999 | else: |
| 4000 | continue |
| 4001 | except KeyError: |
Sunitha Harish | f9bb2fa | 2019-09-19 01:48:00 -0500 | [diff] [blame] | 4002 | continue |
| 4003 | return "No object found for address " + args.address + \ |
| 4004 | " on Interface(" + args.Interface + ")" |
Nagaraju Goruganti | 97a2060 | 2018-11-16 03:06:08 -0600 | [diff] [blame] | 4005 | |
| 4006 | |
Nagaraju Goruganti | f21d43c | 2018-11-19 10:47:19 -0600 | [diff] [blame] | 4007 | def addVLAN(host, args, session): |
| 4008 | """ |
| 4009 | Called by the network function. Creates VLAN on given interface. |
| 4010 | |
| 4011 | @param host: string, the hostname or IP address of the bmc |
| 4012 | @param args: contains additional arguments used by the ldap subcommand |
| 4013 | args.json: boolean, if this flag is set to true, the output |
| 4014 | will be provided in json format for programmatic consumption |
| 4015 | @param session: the active session to use |
| 4016 | """ |
| 4017 | |
| 4018 | url = "https://" + host+"/xyz/openbmc_project/network/action/VLAN" |
Nagaraju Goruganti | f21d43c | 2018-11-19 10:47:19 -0600 | [diff] [blame] | 4019 | |
Sunitha Harish | 0baf637 | 2019-07-31 03:59:03 -0500 | [diff] [blame] | 4020 | data = {"data": [args.Interface,int(args.Identifier)]} |
Nagaraju Goruganti | f21d43c | 2018-11-19 10:47:19 -0600 | [diff] [blame] | 4021 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 4022 | res = session.post(url, headers=jsonHeader, json=data, verify=False, |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 4023 | timeout=baseTimeout) |
Nagaraju Goruganti | f21d43c | 2018-11-19 10:47:19 -0600 | [diff] [blame] | 4024 | except(requests.exceptions.Timeout): |
| 4025 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 4026 | except(requests.exceptions.ConnectionError) as err: |
| 4027 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 4028 | if res.status_code == 400: |
Sunitha Harish | 0baf637 | 2019-07-31 03:59:03 -0500 | [diff] [blame] | 4029 | return "Adding VLAN to interface" + "(" + args.Interface + ")" +\ |
| 4030 | " failed" |
Nagaraju Goruganti | f21d43c | 2018-11-19 10:47:19 -0600 | [diff] [blame] | 4031 | |
| 4032 | return res.text |
| 4033 | |
| 4034 | |
| 4035 | def deleteVLAN(host, args, session): |
| 4036 | """ |
| 4037 | Called by the network function. Creates VLAN on given interface. |
| 4038 | |
| 4039 | @param host: string, the hostname or IP address of the bmc |
| 4040 | @param args: contains additional arguments used by the ldap subcommand |
| 4041 | args.json: boolean, if this flag is set to true, the output |
| 4042 | will be provided in json format for programmatic consumption |
| 4043 | @param session: the active session to use |
| 4044 | """ |
| 4045 | |
Sunitha Harish | 577a503 | 2019-08-08 06:27:40 -0500 | [diff] [blame] | 4046 | url = "https://" + host+"/xyz/openbmc_project/network/"+args.Interface+"/action/Delete" |
Nagaraju Goruganti | f21d43c | 2018-11-19 10:47:19 -0600 | [diff] [blame] | 4047 | data = {"data": []} |
| 4048 | |
| 4049 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 4050 | res = session.post(url, headers=jsonHeader, json=data, verify=False, timeout=baseTimeout) |
Nagaraju Goruganti | f21d43c | 2018-11-19 10:47:19 -0600 | [diff] [blame] | 4051 | except(requests.exceptions.Timeout): |
| 4052 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 4053 | except(requests.exceptions.ConnectionError) as err: |
| 4054 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 4055 | if res.status_code == 404: |
Sunitha Harish | 577a503 | 2019-08-08 06:27:40 -0500 | [diff] [blame] | 4056 | return "The specified VLAN"+"("+args.Interface+")" +" doesn't exist" |
Nagaraju Goruganti | f21d43c | 2018-11-19 10:47:19 -0600 | [diff] [blame] | 4057 | |
| 4058 | return res.text |
| 4059 | |
| 4060 | |
| 4061 | def viewDHCPConfig(host, args, session): |
| 4062 | """ |
| 4063 | Called by the network function. Shows DHCP configured Properties. |
| 4064 | |
| 4065 | @param host: string, the hostname or IP address of the bmc |
| 4066 | @param args: contains additional arguments used by the ldap subcommand |
| 4067 | args.json: boolean, if this flag is set to true, the output |
| 4068 | will be provided in json format for programmatic consumption |
| 4069 | @param session: the active session to use |
| 4070 | """ |
| 4071 | |
| 4072 | url="https://"+host+"/xyz/openbmc_project/network/config/dhcp" |
Nagaraju Goruganti | f21d43c | 2018-11-19 10:47:19 -0600 | [diff] [blame] | 4073 | |
| 4074 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 4075 | res = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
Nagaraju Goruganti | f21d43c | 2018-11-19 10:47:19 -0600 | [diff] [blame] | 4076 | except(requests.exceptions.Timeout): |
| 4077 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 4078 | except(requests.exceptions.ConnectionError) as err: |
| 4079 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 4080 | |
| 4081 | return res.text |
| 4082 | |
| 4083 | |
| 4084 | def configureDHCP(host, args, session): |
| 4085 | """ |
| 4086 | Called by the network function. Configures/updates DHCP Properties. |
| 4087 | |
| 4088 | @param host: string, the hostname or IP address of the bmc |
| 4089 | @param args: contains additional arguments used by the ldap subcommand |
| 4090 | args.json: boolean, if this flag is set to true, the output |
| 4091 | will be provided in json format for programmatic consumption |
| 4092 | @param session: the active session to use |
| 4093 | """ |
| 4094 | |
Nagaraju Goruganti | f21d43c | 2018-11-19 10:47:19 -0600 | [diff] [blame] | 4095 | |
| 4096 | try: |
| 4097 | url="https://"+host+"/xyz/openbmc_project/network/config/dhcp" |
| 4098 | if(args.DNSEnabled == True): |
| 4099 | data = '{"data": 1}' |
| 4100 | else: |
| 4101 | data = '{"data": 0}' |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 4102 | res = session.put(url + '/attr/DNSEnabled', headers=jsonHeader, |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 4103 | data=data, verify=False, timeout=baseTimeout) |
Nagaraju Goruganti | f21d43c | 2018-11-19 10:47:19 -0600 | [diff] [blame] | 4104 | if(args.HostNameEnabled == True): |
| 4105 | data = '{"data": 1}' |
| 4106 | else: |
| 4107 | data = '{"data": 0}' |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 4108 | res = session.put(url + '/attr/HostNameEnabled', headers=jsonHeader, |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 4109 | data=data, verify=False, timeout=baseTimeout) |
Nagaraju Goruganti | f21d43c | 2018-11-19 10:47:19 -0600 | [diff] [blame] | 4110 | if(args.NTPEnabled == True): |
| 4111 | data = '{"data": 1}' |
| 4112 | else: |
| 4113 | data = '{"data": 0}' |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 4114 | res = session.put(url + '/attr/NTPEnabled', headers=jsonHeader, |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 4115 | data=data, verify=False, timeout=baseTimeout) |
Nagaraju Goruganti | f21d43c | 2018-11-19 10:47:19 -0600 | [diff] [blame] | 4116 | if(args.SendHostNameEnabled == True): |
| 4117 | data = '{"data": 1}' |
| 4118 | else: |
| 4119 | data = '{"data": 0}' |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 4120 | res = session.put(url + '/attr/SendHostNameEnabled', headers=jsonHeader, |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 4121 | data=data, verify=False, timeout=baseTimeout) |
Nagaraju Goruganti | f21d43c | 2018-11-19 10:47:19 -0600 | [diff] [blame] | 4122 | except(requests.exceptions.Timeout): |
| 4123 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 4124 | except(requests.exceptions.ConnectionError) as err: |
| 4125 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 4126 | |
| 4127 | return res.text |
| 4128 | |
| 4129 | |
| 4130 | def nwReset(host, args, session): |
| 4131 | |
| 4132 | """ |
| 4133 | Called by the network function. Resets networks setting to factory defaults. |
| 4134 | |
| 4135 | @param host: string, the hostname or IP address of the bmc |
| 4136 | @param args: contains additional arguments used by the ldap subcommand |
| 4137 | args.json: boolean, if this flag is set to true, the output |
| 4138 | will be provided in json format for programmatic consumption |
| 4139 | @param session: the active session to use |
| 4140 | """ |
| 4141 | |
| 4142 | url = "https://"+host+"/xyz/openbmc_project/network/action/Reset" |
Nagaraju Goruganti | f21d43c | 2018-11-19 10:47:19 -0600 | [diff] [blame] | 4143 | data = '{"data":[] }' |
| 4144 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 4145 | res = session.post(url, headers=jsonHeader, data=data, verify=False, |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 4146 | timeout=baseTimeout) |
Nagaraju Goruganti | f21d43c | 2018-11-19 10:47:19 -0600 | [diff] [blame] | 4147 | |
| 4148 | except(requests.exceptions.Timeout): |
| 4149 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 4150 | except(requests.exceptions.ConnectionError) as err: |
| 4151 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 4152 | |
| 4153 | return res.text |
| 4154 | |
RAJESWARAN THILLAIGOVINDAN | 87f087b | 2019-05-08 04:15:26 -0500 | [diff] [blame] | 4155 | def getLDAPTypeEnabled(host,session): |
| 4156 | |
| 4157 | """ |
| 4158 | Called by LDAP related functions to find the LDAP server type that has been enabled. |
| 4159 | Returns None if LDAP has not been configured. |
| 4160 | |
| 4161 | @param host: string, the hostname or IP address of the bmc |
| 4162 | @param session: the active session to use |
| 4163 | """ |
| 4164 | |
| 4165 | enabled = False |
| 4166 | url = 'https://'+host+'/xyz/openbmc_project/user/ldap/' |
| 4167 | for key,value in serverTypeMap.items(): |
| 4168 | data = {"data": []} |
| 4169 | try: |
| 4170 | res = session.get(url + value + '/attr/Enabled', headers=jsonHeader, json=data, verify=False, timeout=baseTimeout) |
| 4171 | except(requests.exceptions.Timeout): |
| 4172 | print(connectionErrHandler(args.json, "Timeout", None)) |
| 4173 | return |
| 4174 | except(requests.exceptions.ConnectionError) as err: |
| 4175 | print(connectionErrHandler(args.json, "ConnectionError", err)) |
| 4176 | return |
| 4177 | |
| 4178 | enabled = res.json()['data'] |
| 4179 | if (enabled): |
| 4180 | return key |
| 4181 | |
| 4182 | def syncRoleMap(host,args,session,fromServerType,toServerType): |
| 4183 | |
| 4184 | """ |
| 4185 | Called by LDAP related functions to sync the role maps |
| 4186 | Returns False if LDAP has not been configured. |
| 4187 | |
| 4188 | @param host: string, the hostname or IP address of the bmc |
| 4189 | @param session: the active session to use |
| 4190 | @param fromServerType : Server type whose role map has to be copied |
| 4191 | @param toServerType : Server type to which role map has to be copied |
| 4192 | """ |
| 4193 | |
| 4194 | url = "https://"+host+"/xyz/openbmc_project/user/ldap/" |
| 4195 | |
| 4196 | try: |
| 4197 | #Note: If the fromServerType has no role map, then |
| 4198 | #the toServerType will not have any role map. |
| 4199 | |
| 4200 | #delete the privilege mapping from the toServerType and |
| 4201 | #then copy the privilege mapping from fromServerType to |
| 4202 | #toServerType. |
| 4203 | args.serverType = toServerType |
| 4204 | res = deleteAllPrivilegeMapping(host, args, session) |
| 4205 | |
| 4206 | data = {"data": []} |
| 4207 | res = session.get(url + serverTypeMap[fromServerType] + '/role_map/enumerate', headers=jsonHeader, json=data, verify=False, timeout=baseTimeout) |
| 4208 | #Previously enabled server type has no role map |
| 4209 | if (res.status_code != requests.codes.ok): |
| 4210 | |
| 4211 | #fromServerType has no role map; So, no need to copy |
| 4212 | #role map to toServerType. |
| 4213 | return |
| 4214 | |
| 4215 | objDict = json.loads(res.text) |
| 4216 | dataDict = objDict['data'] |
| 4217 | for key,value in dataDict.items(): |
| 4218 | data = {"data": [value["GroupName"], value["Privilege"]]} |
| 4219 | res = session.post(url + serverTypeMap[toServerType] + '/action/Create', headers=jsonHeader, json = data, verify=False, timeout=baseTimeout) |
| 4220 | |
| 4221 | except(requests.exceptions.Timeout): |
| 4222 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 4223 | except(requests.exceptions.ConnectionError) as err: |
| 4224 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 4225 | return res.text |
| 4226 | |
Nagaraju Goruganti | f21d43c | 2018-11-19 10:47:19 -0600 | [diff] [blame] | 4227 | |
Ratan Gupta | feee637 | 2018-10-17 23:25:51 +0530 | [diff] [blame] | 4228 | def createPrivilegeMapping(host, args, session): |
| 4229 | """ |
| 4230 | Called by the ldap function. Creates the group and the privilege mapping. |
| 4231 | |
| 4232 | @param host: string, the hostname or IP address of the bmc |
| 4233 | @param args: contains additional arguments used by the ldap subcommand |
| 4234 | @param session: the active session to use |
| 4235 | @param args.json: boolean, if this flag is set to true, the output |
| 4236 | will be provided in json format for programmatic consumption |
| 4237 | """ |
| 4238 | |
Ratan Gupta | feee637 | 2018-10-17 23:25:51 +0530 | [diff] [blame] | 4239 | try: |
RAJESWARAN THILLAIGOVINDAN | 87f087b | 2019-05-08 04:15:26 -0500 | [diff] [blame] | 4240 | if (isRedfishSupport): |
| 4241 | url = 'https://'+host+'/xyz/openbmc_project/user/ldap/' |
| 4242 | |
| 4243 | #To maintain the interface compatibility between op930 and op940, the server type has been made |
| 4244 | #optional. If the server type is not specified, then create the role-mapper for the currently |
| 4245 | #enabled server type. |
| 4246 | serverType = args.serverType |
| 4247 | if (serverType is None): |
| 4248 | serverType = getLDAPTypeEnabled(host,session) |
| 4249 | if (serverType is None): |
| 4250 | return("LDAP server has not been enabled. Please specify LDAP serverType to proceed further...") |
| 4251 | |
| 4252 | data = {"data": [args.groupName,args.privilege]} |
| 4253 | res = session.post(url + serverTypeMap[serverType] + '/action/Create', headers=jsonHeader, json = data, verify=False, timeout=baseTimeout) |
| 4254 | |
| 4255 | else: |
| 4256 | url = 'https://'+host+'/xyz/openbmc_project/user/ldap/action/Create' |
| 4257 | data = {"data": [args.groupName,args.privilege]} |
| 4258 | res = session.post(url, headers=jsonHeader, json = data, verify=False, timeout=baseTimeout) |
| 4259 | |
Ratan Gupta | feee637 | 2018-10-17 23:25:51 +0530 | [diff] [blame] | 4260 | except(requests.exceptions.Timeout): |
| 4261 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 4262 | except(requests.exceptions.ConnectionError) as err: |
| 4263 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 4264 | return res.text |
| 4265 | |
| 4266 | def listPrivilegeMapping(host, args, session): |
| 4267 | """ |
| 4268 | Called by the ldap function. Lists the group and the privilege mapping. |
| 4269 | |
| 4270 | @param host: string, the hostname or IP address of the bmc |
| 4271 | @param args: contains additional arguments used by the ldap subcommand |
| 4272 | @param session: the active session to use |
| 4273 | @param args.json: boolean, if this flag is set to true, the output |
| 4274 | will be provided in json format for programmatic consumption |
| 4275 | """ |
RAJESWARAN THILLAIGOVINDAN | 87f087b | 2019-05-08 04:15:26 -0500 | [diff] [blame] | 4276 | |
| 4277 | if (isRedfishSupport): |
| 4278 | serverType = args.serverType |
| 4279 | if (serverType is None): |
| 4280 | serverType = getLDAPTypeEnabled(host,session) |
| 4281 | if (serverType is None): |
| 4282 | return("LDAP has not been enabled. Please specify LDAP serverType to proceed further...") |
| 4283 | |
| 4284 | url = 'https://'+host+'/xyz/openbmc_project/user/ldap/'+serverTypeMap[serverType]+'/role_map/enumerate' |
| 4285 | |
| 4286 | else: |
| 4287 | url = 'https://'+host+'/xyz/openbmc_project/user/ldap/enumerate' |
| 4288 | |
Ratan Gupta | feee637 | 2018-10-17 23:25:51 +0530 | [diff] [blame] | 4289 | data = {"data": []} |
| 4290 | |
| 4291 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 4292 | res = session.get(url, headers=jsonHeader, json = data, verify=False, timeout=baseTimeout) |
Ratan Gupta | feee637 | 2018-10-17 23:25:51 +0530 | [diff] [blame] | 4293 | except(requests.exceptions.Timeout): |
| 4294 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 4295 | except(requests.exceptions.ConnectionError) as err: |
| 4296 | return connectionErrHandler(args.json, "ConnectionError", err) |
RAJESWARAN THILLAIGOVINDAN | 87f087b | 2019-05-08 04:15:26 -0500 | [diff] [blame] | 4297 | |
Ratan Gupta | feee637 | 2018-10-17 23:25:51 +0530 | [diff] [blame] | 4298 | return res.text |
| 4299 | |
| 4300 | def deletePrivilegeMapping(host, args, session): |
| 4301 | """ |
| 4302 | Called by the ldap function. Deletes the mapping associated with the group. |
| 4303 | |
| 4304 | @param host: string, the hostname or IP address of the bmc |
| 4305 | @param args: contains additional arguments used by the ldap subcommand |
| 4306 | @param session: the active session to use |
| 4307 | @param args.json: boolean, if this flag is set to true, the output |
| 4308 | will be provided in json format for programmatic consumption |
| 4309 | """ |
RAJESWARAN THILLAIGOVINDAN | 87f087b | 2019-05-08 04:15:26 -0500 | [diff] [blame] | 4310 | |
| 4311 | ldapNameSpaceObjects = listPrivilegeMapping(host, args, session) |
Ratan Gupta | feee637 | 2018-10-17 23:25:51 +0530 | [diff] [blame] | 4312 | ldapNameSpaceObjects = json.loads(ldapNameSpaceObjects)["data"] |
| 4313 | path = '' |
Ratan Gupta | feee637 | 2018-10-17 23:25:51 +0530 | [diff] [blame] | 4314 | data = {"data": []} |
| 4315 | |
RAJESWARAN THILLAIGOVINDAN | 87f087b | 2019-05-08 04:15:26 -0500 | [diff] [blame] | 4316 | if (isRedfishSupport): |
| 4317 | if (args.serverType is None): |
| 4318 | serverType = getLDAPTypeEnabled(host,session) |
| 4319 | if (serverType is None): |
| 4320 | return("LDAP has not been enabled. Please specify LDAP serverType to proceed further...") |
| 4321 | # search for the object having the mapping for the given group |
| 4322 | for key,value in ldapNameSpaceObjects.items(): |
| 4323 | if value['GroupName'] == args.groupName: |
| 4324 | path = key |
| 4325 | break |
| 4326 | |
| 4327 | if path == '': |
| 4328 | return "No privilege mapping found for this group." |
| 4329 | |
| 4330 | # delete the object |
| 4331 | url = 'https://'+host+path+'/action/Delete' |
| 4332 | |
| 4333 | else: |
| 4334 | # not interested in the config objet |
| 4335 | ldapNameSpaceObjects.pop('/xyz/openbmc_project/user/ldap/config', None) |
| 4336 | |
| 4337 | # search for the object having the mapping for the given group |
| 4338 | for key,value in ldapNameSpaceObjects.items(): |
| 4339 | if value['GroupName'] == args.groupName: |
| 4340 | path = key |
| 4341 | break |
| 4342 | |
| 4343 | if path == '': |
| 4344 | return "No privilege mapping found for this group." |
| 4345 | |
| 4346 | # delete the object |
| 4347 | url = 'https://'+host+path+'/action/delete' |
| 4348 | |
Ratan Gupta | feee637 | 2018-10-17 23:25:51 +0530 | [diff] [blame] | 4349 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 4350 | res = session.post(url, headers=jsonHeader, json = data, verify=False, timeout=baseTimeout) |
Ratan Gupta | feee637 | 2018-10-17 23:25:51 +0530 | [diff] [blame] | 4351 | except(requests.exceptions.Timeout): |
| 4352 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 4353 | except(requests.exceptions.ConnectionError) as err: |
| 4354 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 4355 | return res.text |
Ratan Gupta | 9166cd2 | 2018-10-01 18:09:40 +0530 | [diff] [blame] | 4356 | |
Sivas SRR | 7883527 | 2018-11-27 05:27:19 -0600 | [diff] [blame] | 4357 | def deleteAllPrivilegeMapping(host, args, session): |
| 4358 | """ |
| 4359 | Called by the ldap function. Deletes all the privilege mapping and group defined. |
| 4360 | @param host: string, the hostname or IP address of the bmc |
| 4361 | @param args: contains additional arguments used by the ldap subcommand |
| 4362 | @param session: the active session to use |
| 4363 | @param args.json: boolean, if this flag is set to true, the output |
| 4364 | will be provided in json format for programmatic consumption |
| 4365 | """ |
RAJESWARAN THILLAIGOVINDAN | 87f087b | 2019-05-08 04:15:26 -0500 | [diff] [blame] | 4366 | |
Sivas SRR | 7883527 | 2018-11-27 05:27:19 -0600 | [diff] [blame] | 4367 | ldapNameSpaceObjects = listPrivilegeMapping(host, args, session) |
| 4368 | ldapNameSpaceObjects = json.loads(ldapNameSpaceObjects)["data"] |
| 4369 | path = '' |
Sivas SRR | 7883527 | 2018-11-27 05:27:19 -0600 | [diff] [blame] | 4370 | data = {"data": []} |
| 4371 | |
RAJESWARAN THILLAIGOVINDAN | 87f087b | 2019-05-08 04:15:26 -0500 | [diff] [blame] | 4372 | if (isRedfishSupport): |
| 4373 | if (args.serverType is None): |
| 4374 | serverType = getLDAPTypeEnabled(host,session) |
| 4375 | if (serverType is None): |
| 4376 | return("LDAP has not been enabled. Please specify LDAP serverType to proceed further...") |
| 4377 | |
| 4378 | else: |
| 4379 | # Remove the config object. |
| 4380 | ldapNameSpaceObjects.pop('/xyz/openbmc_project/user/ldap/config', None) |
| 4381 | |
Sivas SRR | 7883527 | 2018-11-27 05:27:19 -0600 | [diff] [blame] | 4382 | try: |
| 4383 | # search for GroupName property and delete if it is available. |
| 4384 | for path in ldapNameSpaceObjects.keys(): |
| 4385 | # delete the object |
RAJESWARAN THILLAIGOVINDAN | 87f087b | 2019-05-08 04:15:26 -0500 | [diff] [blame] | 4386 | url = 'https://'+host+path+'/action/Delete' |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 4387 | res = session.post(url, headers=jsonHeader, json = data, verify=False, timeout=baseTimeout) |
RAJESWARAN THILLAIGOVINDAN | 87f087b | 2019-05-08 04:15:26 -0500 | [diff] [blame] | 4388 | |
Sivas SRR | 7883527 | 2018-11-27 05:27:19 -0600 | [diff] [blame] | 4389 | except(requests.exceptions.Timeout): |
| 4390 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 4391 | except(requests.exceptions.ConnectionError) as err: |
| 4392 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 4393 | return res.text |
| 4394 | |
Nagaraju Goruganti | 7d1fe17 | 2018-11-13 06:09:29 -0600 | [diff] [blame] | 4395 | def viewLDAPConfig(host, args, session): |
| 4396 | """ |
RAJESWARAN THILLAIGOVINDAN | 87f087b | 2019-05-08 04:15:26 -0500 | [diff] [blame] | 4397 | Called by the ldap function. Prints out active LDAP configuration properties |
Nagaraju Goruganti | 7d1fe17 | 2018-11-13 06:09:29 -0600 | [diff] [blame] | 4398 | |
| 4399 | @param host: string, the hostname or IP address of the bmc |
| 4400 | @param args: contains additional arguments used by the ldap subcommand |
| 4401 | args.json: boolean, if this flag is set to true, the output |
| 4402 | will be provided in json format for programmatic consumption |
| 4403 | @param session: the active session to use |
| 4404 | @return returns LDAP's configured properties. |
| 4405 | """ |
RAJESWARAN THILLAIGOVINDAN | 87f087b | 2019-05-08 04:15:26 -0500 | [diff] [blame] | 4406 | |
Nagaraju Goruganti | 7d1fe17 | 2018-11-13 06:09:29 -0600 | [diff] [blame] | 4407 | try: |
RAJESWARAN THILLAIGOVINDAN | 87f087b | 2019-05-08 04:15:26 -0500 | [diff] [blame] | 4408 | if (isRedfishSupport): |
| 4409 | |
| 4410 | url = "https://"+host+"/xyz/openbmc_project/user/ldap/" |
| 4411 | |
| 4412 | serverTypeEnabled = getLDAPTypeEnabled(host,session) |
| 4413 | |
| 4414 | if (serverTypeEnabled is not None): |
| 4415 | data = {"data": []} |
| 4416 | res = session.get(url + serverTypeMap[serverTypeEnabled], headers=jsonHeader, json=data, verify=False, timeout=baseTimeout) |
| 4417 | else: |
| 4418 | return("LDAP server has not been enabled...") |
| 4419 | |
| 4420 | else : |
| 4421 | url = "https://"+host+"/xyz/openbmc_project/user/ldap/config" |
| 4422 | res = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
| 4423 | |
Nagaraju Goruganti | 7d1fe17 | 2018-11-13 06:09:29 -0600 | [diff] [blame] | 4424 | except(requests.exceptions.Timeout): |
| 4425 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 4426 | except(requests.exceptions.ConnectionError) as err: |
| 4427 | return connectionErrHandler(args.json, "ConnectionError", err) |
Nagaraju Goruganti | 7d1fe17 | 2018-11-13 06:09:29 -0600 | [diff] [blame] | 4428 | if res.status_code == 404: |
| 4429 | return "LDAP server config has not been created" |
| 4430 | return res.text |
| 4431 | |
Nagaraju Goruganti | f21d43c | 2018-11-19 10:47:19 -0600 | [diff] [blame] | 4432 | def str2bool(v): |
| 4433 | if v.lower() in ('yes', 'true', 't', 'y', '1'): |
| 4434 | return True |
| 4435 | elif v.lower() in ('no', 'false', 'f', 'n', '0'): |
| 4436 | return False |
| 4437 | else: |
| 4438 | raise argparse.ArgumentTypeError('Boolean value expected.') |
Nagaraju Goruganti | 7d1fe17 | 2018-11-13 06:09:29 -0600 | [diff] [blame] | 4439 | |
Matt Spinler | 7d426c2 | 2018-09-24 14:42:07 -0500 | [diff] [blame] | 4440 | def localUsers(host, args, session): |
| 4441 | """ |
| 4442 | Enables and disables local BMC users. |
| 4443 | |
| 4444 | @param host: string, the hostname or IP address of the bmc |
| 4445 | @param args: contains additional arguments used by the logging sub command |
| 4446 | @param session: the active session to use |
| 4447 | """ |
| 4448 | |
Matt Spinler | 7d426c2 | 2018-09-24 14:42:07 -0500 | [diff] [blame] | 4449 | url="https://{hostname}/xyz/openbmc_project/user/enumerate".format(hostname=host) |
| 4450 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 4451 | res = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
Matt Spinler | 7d426c2 | 2018-09-24 14:42:07 -0500 | [diff] [blame] | 4452 | except(requests.exceptions.Timeout): |
| 4453 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 4454 | usersDict = json.loads(res.text) |
| 4455 | |
| 4456 | if not usersDict['data']: |
| 4457 | return "No users found" |
| 4458 | |
| 4459 | output = "" |
| 4460 | for user in usersDict['data']: |
Matt Spinler | 015adc2 | 2018-10-23 14:30:19 -0500 | [diff] [blame] | 4461 | |
| 4462 | # Skip LDAP and another non-local users |
| 4463 | if 'UserEnabled' not in usersDict['data'][user]: |
| 4464 | continue |
| 4465 | |
Matt Spinler | 7d426c2 | 2018-09-24 14:42:07 -0500 | [diff] [blame] | 4466 | name = user.split('/')[-1] |
| 4467 | url = "https://{hostname}{user}/attr/UserEnabled".format(hostname=host, user=user) |
| 4468 | |
| 4469 | if args.local_users == "queryenabled": |
| 4470 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 4471 | res = session.get(url, headers=jsonHeader,verify=False, timeout=baseTimeout) |
Matt Spinler | 7d426c2 | 2018-09-24 14:42:07 -0500 | [diff] [blame] | 4472 | except(requests.exceptions.Timeout): |
| 4473 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 4474 | |
| 4475 | result = json.loads(res.text) |
| 4476 | output += ("User: {name} Enabled: {result}\n").format(name=name, result=result['data']) |
| 4477 | |
| 4478 | elif args.local_users in ["enableall", "disableall"]: |
| 4479 | action = "" |
| 4480 | if args.local_users == "enableall": |
| 4481 | data = '{"data": true}' |
| 4482 | action = "Enabling" |
| 4483 | else: |
| 4484 | data = '{"data": false}' |
| 4485 | action = "Disabling" |
| 4486 | |
| 4487 | output += "{action} {name}\n".format(action=action, name=name) |
| 4488 | |
| 4489 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 4490 | resp = session.put(url, headers=jsonHeader, data=data, verify=False, timeout=baseTimeout) |
Matt Spinler | 7d426c2 | 2018-09-24 14:42:07 -0500 | [diff] [blame] | 4491 | except(requests.exceptions.Timeout): |
| 4492 | return connectionErrHandler(args.json, "Timeout", None) |
| 4493 | except(requests.exceptions.ConnectionError) as err: |
| 4494 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 4495 | else: |
| 4496 | return "Invalid local users argument" |
| 4497 | |
| 4498 | return output |
| 4499 | |
Marri Devender Rao | 2c2a516 | 2018-11-05 08:57:11 -0600 | [diff] [blame] | 4500 | def setPassword(host, args, session): |
| 4501 | """ |
| 4502 | Set local user password |
| 4503 | @param host: string, the hostname or IP address of the bmc |
| 4504 | @param args: contains additional arguments used by the logging sub |
| 4505 | command |
| 4506 | @param session: the active session to use |
| 4507 | @param args.json: boolean, if this flag is set to true, the output |
| 4508 | will be provided in json format for programmatic consumption |
| 4509 | @return: Session object |
| 4510 | """ |
Marri Devender Rao | 2c2a516 | 2018-11-05 08:57:11 -0600 | [diff] [blame] | 4511 | try: |
Sunitha Harish | c99faba | 2019-07-19 06:55:22 -0500 | [diff] [blame] | 4512 | if(isRedfishSupport): |
| 4513 | url = "https://" + host + "/redfish/v1/AccountService/Accounts/"+ \ |
| 4514 | args.user |
| 4515 | data = {"Password":args.password} |
| 4516 | res = session.patch(url, headers=jsonHeader, json=data, |
| 4517 | verify=False, timeout=baseTimeout) |
| 4518 | else: |
| 4519 | url = "https://" + host + "/xyz/openbmc_project/user/" + args.user + \ |
| 4520 | "/action/SetPassword" |
| 4521 | res = session.post(url, headers=jsonHeader, |
Marri Devender Rao | 2c2a516 | 2018-11-05 08:57:11 -0600 | [diff] [blame] | 4522 | json={"data": [args.password]}, verify=False, |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 4523 | timeout=baseTimeout) |
Marri Devender Rao | 2c2a516 | 2018-11-05 08:57:11 -0600 | [diff] [blame] | 4524 | except(requests.exceptions.Timeout): |
| 4525 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 4526 | except(requests.exceptions.ConnectionError) as err: |
| 4527 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 4528 | except(requests.exceptions.RequestException) as err: |
| 4529 | return connectionErrHandler(args.json, "RequestException", err) |
Sunitha Harish | c99faba | 2019-07-19 06:55:22 -0500 | [diff] [blame] | 4530 | return res.status_code |
Matthew Barth | 368e83c | 2019-02-01 13:48:25 -0600 | [diff] [blame] | 4531 | |
| 4532 | def getThermalZones(host, args, session): |
| 4533 | """ |
| 4534 | Get the available thermal control zones |
| 4535 | @param host: string, the hostname or IP address of the bmc |
| 4536 | @param args: contains additional arguments used to get the thermal |
| 4537 | control zones |
| 4538 | @param session: the active session to use |
| 4539 | @return: Session object |
| 4540 | """ |
| 4541 | url = "https://" + host + "/xyz/openbmc_project/control/thermal/enumerate" |
| 4542 | |
| 4543 | try: |
| 4544 | res = session.get(url, headers=jsonHeader, verify=False, timeout=30) |
| 4545 | except(requests.exceptions.Timeout): |
| 4546 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 4547 | except(requests.exceptions.ConnectionError) as err: |
| 4548 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 4549 | except(requests.exceptions.RequestException) as err: |
| 4550 | return connectionErrHandler(args.json, "RequestException", err) |
| 4551 | |
| 4552 | if (res.status_code == 404): |
Matthew Barth | 0939e82 | 2019-09-27 15:47:50 -0500 | [diff] [blame] | 4553 | return "No thermal control zones found" |
Matthew Barth | 368e83c | 2019-02-01 13:48:25 -0600 | [diff] [blame] | 4554 | |
| 4555 | zonesDict = json.loads(res.text) |
| 4556 | if not zonesDict['data']: |
| 4557 | return "No thermal control zones found" |
| 4558 | for zone in zonesDict['data']: |
| 4559 | z = ",".join(str(zone.split('/')[-1]) for zone in zonesDict['data']) |
| 4560 | |
| 4561 | return "Zones: [ " + z + " ]" |
| 4562 | |
| 4563 | |
| 4564 | def getThermalMode(host, args, session): |
| 4565 | """ |
| 4566 | Get thermal control mode |
| 4567 | @param host: string, the hostname or IP address of the bmc |
| 4568 | @param args: contains additional arguments used to get the thermal |
| 4569 | control mode |
| 4570 | @param session: the active session to use |
| 4571 | @param args.zone: the zone to get the mode on |
| 4572 | @return: Session object |
| 4573 | """ |
| 4574 | url = "https://" + host + "/xyz/openbmc_project/control/thermal/" + \ |
| 4575 | args.zone |
| 4576 | |
| 4577 | try: |
| 4578 | res = session.get(url, headers=jsonHeader, verify=False, timeout=30) |
| 4579 | except(requests.exceptions.Timeout): |
| 4580 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 4581 | except(requests.exceptions.ConnectionError) as err: |
| 4582 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 4583 | except(requests.exceptions.RequestException) as err: |
| 4584 | return connectionErrHandler(args.json, "RequestException", err) |
| 4585 | |
| 4586 | if (res.status_code == 404): |
Matthew Barth | 0939e82 | 2019-09-27 15:47:50 -0500 | [diff] [blame] | 4587 | return "Thermal control zone(" + args.zone + ") not found" |
Matthew Barth | 368e83c | 2019-02-01 13:48:25 -0600 | [diff] [blame] | 4588 | |
| 4589 | propsDict = json.loads(res.text) |
| 4590 | if not propsDict['data']: |
| 4591 | return "No thermal control properties found on zone(" + args.zone + ")" |
| 4592 | curMode = "Current" |
| 4593 | supModes = "Supported" |
| 4594 | result = "\n" |
| 4595 | for prop in propsDict['data']: |
| 4596 | if (prop.casefold() == curMode.casefold()): |
| 4597 | result += curMode + " Mode: " + propsDict['data'][curMode] + "\n" |
| 4598 | if (prop.casefold() == supModes.casefold()): |
| 4599 | s = ", ".join(str(sup) for sup in propsDict['data'][supModes]) |
| 4600 | result += supModes + " Modes: [ " + s + " ]\n" |
| 4601 | |
| 4602 | return result |
| 4603 | |
| 4604 | def setThermalMode(host, args, session): |
| 4605 | """ |
| 4606 | Set thermal control mode |
| 4607 | @param host: string, the hostname or IP address of the bmc |
| 4608 | @param args: contains additional arguments used for setting the thermal |
| 4609 | control mode |
| 4610 | @param session: the active session to use |
| 4611 | @param args.zone: the zone to set the mode on |
| 4612 | @param args.mode: the mode to enable |
| 4613 | @return: Session object |
| 4614 | """ |
| 4615 | url = "https://" + host + "/xyz/openbmc_project/control/thermal/" + \ |
| 4616 | args.zone + "/attr/Current" |
| 4617 | |
| 4618 | # Check args.mode against supported modes using `getThermalMode` output |
| 4619 | modes = getThermalMode(host, args, session) |
| 4620 | modes = os.linesep.join([m for m in modes.splitlines() if m]) |
| 4621 | modes = modes.replace("\n", ";").strip() |
| 4622 | modesDict = dict(m.split(': ') for m in modes.split(';')) |
| 4623 | sModes = ''.join(s for s in modesDict['Supported Modes'] if s not in '[ ]') |
| 4624 | if args.mode.casefold() not in \ |
| 4625 | (m.casefold() for m in sModes.split(',')) or not args.mode: |
| 4626 | result = ("Unsupported mode('" + args.mode + "') given, " + |
| 4627 | "select a supported mode: \n" + |
| 4628 | getThermalMode(host, args, session)) |
| 4629 | return result |
| 4630 | |
| 4631 | data = '{"data":"' + args.mode + '"}' |
| 4632 | try: |
| 4633 | res = session.get(url, headers=jsonHeader, verify=False, timeout=30) |
| 4634 | except(requests.exceptions.Timeout): |
| 4635 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 4636 | except(requests.exceptions.ConnectionError) as err: |
| 4637 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 4638 | except(requests.exceptions.RequestException) as err: |
| 4639 | return connectionErrHandler(args.json, "RequestException", err) |
| 4640 | |
| 4641 | if (data and res.status_code != 404): |
| 4642 | try: |
| 4643 | res = session.put(url, headers=jsonHeader, |
| 4644 | data=data, verify=False, |
| 4645 | timeout=30) |
| 4646 | except(requests.exceptions.Timeout): |
| 4647 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 4648 | except(requests.exceptions.ConnectionError) as err: |
| 4649 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 4650 | except(requests.exceptions.RequestException) as err: |
| 4651 | return connectionErrHandler(args.json, "RequestException", err) |
| 4652 | |
| 4653 | if res.status_code == 403: |
| 4654 | return "The specified thermal control zone(" + args.zone + ")" + \ |
| 4655 | " does not exist" |
| 4656 | |
| 4657 | return res.text |
| 4658 | else: |
| 4659 | return "Setting thermal control mode(" + args.mode + ")" + \ |
Matthew Barth | 0939e82 | 2019-09-27 15:47:50 -0500 | [diff] [blame] | 4660 | " not supported or operation not available" |
Matthew Barth | 368e83c | 2019-02-01 13:48:25 -0600 | [diff] [blame] | 4661 | |
| 4662 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4663 | def createCommandParser(): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 4664 | """ |
| 4665 | 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] | 4666 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 4667 | @return: returns the parser for the command line |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 4668 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4669 | parser = argparse.ArgumentParser(description='Process arguments') |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 4670 | parser.add_argument("-H", "--host", help='A hostname or IP for the BMC') |
| 4671 | parser.add_argument("-U", "--user", help='The username to login with') |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4672 | group = parser.add_mutually_exclusive_group() |
| 4673 | group.add_argument("-A", "--askpw", action='store_true', help='prompt for password') |
| 4674 | group.add_argument("-P", "--PW", help='Provide the password in-line') |
Joseph Reynolds | a2d54c5 | 2019-06-11 22:02:57 -0500 | [diff] [blame] | 4675 | group.add_argument("-E", "--PWenvvar", action='store_true', help='Get password from envvar OPENBMCTOOL_PASSWORD') |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4676 | parser.add_argument('-j', '--json', action='store_true', help='output json data only') |
| 4677 | parser.add_argument('-t', '--policyTableLoc', help='The location of the policy table to parse alerts') |
| 4678 | parser.add_argument('-c', '--CerFormat', action='store_true', help=argparse.SUPPRESS) |
| 4679 | parser.add_argument('-T', '--procTime', action='store_true', help= argparse.SUPPRESS) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 4680 | parser.add_argument('-V', '--version', action='store_true', help='Display the version number of the openbmctool') |
| 4681 | 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] | 4682 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4683 | #fru command |
| 4684 | parser_inv = subparsers.add_parser("fru", help='Work with platform inventory') |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 4685 | 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] | 4686 | inv_subparser.required = True |
| 4687 | #fru print |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4688 | inv_print = inv_subparser.add_parser("print", help="prints out a list of all FRUs") |
| 4689 | inv_print.set_defaults(func=fruPrint) |
| 4690 | #fru list [0....n] |
| 4691 | inv_list = inv_subparser.add_parser("list", help="print out details on selected FRUs. Specifying no items will list the entire inventory") |
| 4692 | inv_list.add_argument('items', nargs='?', help="print out details on selected FRUs. Specifying no items will list the entire inventory") |
| 4693 | inv_list.set_defaults(func=fruList) |
| 4694 | #fru status |
| 4695 | 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] | 4696 | inv_status.add_argument('-v', '--verbose', action='store_true', help='Verbose output') |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4697 | inv_status.set_defaults(func=fruStatus) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 4698 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4699 | #sensors command |
| 4700 | parser_sens = subparsers.add_parser("sensors", help="Work with platform sensors") |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 4701 | 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] | 4702 | sens_subparser.required = True |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4703 | #sensor print |
| 4704 | sens_print= sens_subparser.add_parser('print', help="prints out a list of all Sensors.") |
| 4705 | sens_print.set_defaults(func=sensor) |
| 4706 | #sensor list[0...n] |
| 4707 | sens_list=sens_subparser.add_parser("list", help="Lists all Sensors in the platform. Specify a sensor for full details. ") |
| 4708 | sens_list.add_argument("sensNum", nargs='?', help="The Sensor number to get full details on" ) |
| 4709 | sens_list.set_defaults(func=sensor) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 4710 | |
Matthew Barth | 368e83c | 2019-02-01 13:48:25 -0600 | [diff] [blame] | 4711 | #thermal control commands |
| 4712 | parser_therm = subparsers.add_parser("thermal", help="Work with thermal control parameters") |
| 4713 | therm_subparser=parser_therm.add_subparsers(title='subcommands', description='Thermal control actions to work with', help='Valid thermal control actions to work with', dest='command') |
| 4714 | #thermal control zones |
| 4715 | parser_thermZones = therm_subparser.add_parser("zones", help="Get a list of available thermal control zones") |
| 4716 | parser_thermZones.set_defaults(func=getThermalZones) |
| 4717 | #thermal control modes |
| 4718 | parser_thermMode = therm_subparser.add_parser("modes", help="Work with thermal control modes") |
| 4719 | thermMode_sub = parser_thermMode.add_subparsers(title='subactions', description='Work with thermal control modes', help="Work with thermal control modes") |
| 4720 | #get thermal control mode |
| 4721 | parser_getThermMode = thermMode_sub.add_parser("get", help="Get current and supported thermal control modes") |
| 4722 | parser_getThermMode.add_argument('-z', '--zone', required=True, help='Thermal zone to work with') |
| 4723 | parser_getThermMode.set_defaults(func=getThermalMode) |
| 4724 | #set thermal control mode |
| 4725 | parser_setThermMode = thermMode_sub.add_parser("set", help="Set the thermal control mode") |
| 4726 | parser_setThermMode.add_argument('-z', '--zone', required=True, help='Thermal zone to work with') |
| 4727 | parser_setThermMode.add_argument('-m', '--mode', required=True, help='The supported thermal control mode') |
| 4728 | parser_setThermMode.set_defaults(func=setThermalMode) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 4729 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4730 | #sel command |
| 4731 | parser_sel = subparsers.add_parser("sel", help="Work with platform alerts") |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 4732 | 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] | 4733 | sel_subparser.required = True |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4734 | #sel print |
| 4735 | sel_print = sel_subparser.add_parser("print", help="prints out a list of all sels in a condensed list") |
| 4736 | sel_print.add_argument('-d', '--devdebug', action='store_true', help=argparse.SUPPRESS) |
| 4737 | sel_print.add_argument('-v', '--verbose', action='store_true', help="Changes the output to being very verbose") |
| 4738 | sel_print.add_argument('-f', '--fileloc', help='Parse a file instead of the BMC output') |
| 4739 | sel_print.set_defaults(func=selPrint) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 4740 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4741 | #sel list |
| 4742 | 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") |
| 4743 | sel_list.add_argument("selNum", nargs='?', type=int, help="The SEL entry to get details on") |
| 4744 | sel_list.set_defaults(func=selList) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 4745 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4746 | sel_get = sel_subparser.add_parser("get", help="Gets the verbose details of a specified SEL entry") |
| 4747 | sel_get.add_argument('selNum', type=int, help="the number of the SEL entry to get") |
| 4748 | sel_get.set_defaults(func=selList) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 4749 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4750 | sel_clear = sel_subparser.add_parser("clear", help="Clears all entries from the SEL") |
| 4751 | sel_clear.set_defaults(func=selClear) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 4752 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4753 | 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] | 4754 | sel_setResolved.add_argument('-n', '--selNum', type=int, help="the number of the SEL entry to resolve") |
| 4755 | sel_ResolveAll_sub = sel_setResolved.add_subparsers(title='subcommands', description='valid subcommands',help="sub-command help", dest='command') |
| 4756 | sel_ResolveAll = sel_ResolveAll_sub.add_parser('all', help='Resolve all SEL entries') |
| 4757 | sel_ResolveAll.set_defaults(func=selResolveAll) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4758 | sel_setResolved.set_defaults(func=selSetResolved) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 4759 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4760 | 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] | 4761 | 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] | 4762 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4763 | parser_chassis.add_argument('status', action='store_true', help='Returns the current status of the platform') |
| 4764 | parser_chassis.set_defaults(func=chassis) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 4765 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4766 | 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] | 4767 | 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] | 4768 | parser_chasPower.set_defaults(func=chassisPower) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 4769 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4770 | #control the chassis identify led |
| 4771 | parser_chasIdent = chas_sub.add_parser("identify", help="Control the chassis identify led") |
| 4772 | parser_chasIdent.add_argument('identcmd', choices=['on', 'off', 'status'], help='The control option for the led: on, off, blink, status') |
| 4773 | parser_chasIdent.set_defaults(func=chassisIdent) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 4774 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4775 | #collect service data |
| 4776 | parser_servData = subparsers.add_parser("collect_service_data", help="Collect all bmc data needed for service") |
| 4777 | parser_servData.add_argument('-d', '--devdebug', action='store_true', help=argparse.SUPPRESS) |
| 4778 | parser_servData.set_defaults(func=collectServiceData) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 4779 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 4780 | #system quick health check |
| 4781 | parser_healthChk = subparsers.add_parser("health_check", help="Work with platform sensors") |
| 4782 | parser_healthChk.set_defaults(func=healthCheck) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 4783 | |
Ravi Teja | d8be0b4 | 2020-03-18 14:31:46 -0500 | [diff] [blame] | 4784 | #work with dumps |
| 4785 | parser_bmcdump = subparsers.add_parser("dump", help="Work with dumps") |
| 4786 | parser_bmcdump.add_argument("-t", "--dumpType", default='bmc', choices=['bmc','SystemDump'],help="Type of dump") |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 4787 | 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] | 4788 | bmcDump_sub.required = True |
Ravi Teja | d8be0b4 | 2020-03-18 14:31:46 -0500 | [diff] [blame] | 4789 | dump_Create = bmcDump_sub.add_parser('create', help="Create a dump of given type") |
| 4790 | dump_Create.set_defaults(func=dumpCreate) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 4791 | |
Ravi Teja | d8be0b4 | 2020-03-18 14:31:46 -0500 | [diff] [blame] | 4792 | dump_list = bmcDump_sub.add_parser('list', help="list all dumps") |
| 4793 | dump_list.set_defaults(func=dumpList) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 4794 | |
Ravi Teja | d8be0b4 | 2020-03-18 14:31:46 -0500 | [diff] [blame] | 4795 | parserdumpdelete = bmcDump_sub.add_parser('delete', help="Delete dump") |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4796 | parserdumpdelete.add_argument("-n", "--dumpNum", nargs='*', type=int, help="The Dump entry to delete") |
Ravi Teja | d8be0b4 | 2020-03-18 14:31:46 -0500 | [diff] [blame] | 4797 | parserdumpdelete.set_defaults(func=dumpDelete) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 4798 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 4799 | bmcDumpDelsub = parserdumpdelete.add_subparsers(title='subcommands', description='valid subcommands',help="sub-command help", dest='command') |
Ravi Teja | d8be0b4 | 2020-03-18 14:31:46 -0500 | [diff] [blame] | 4800 | deleteAllDumps = bmcDumpDelsub.add_parser('all', help='Delete all dumps') |
| 4801 | deleteAllDumps.set_defaults(func=dumpDeleteAll) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 4802 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4803 | parser_dumpretrieve = bmcDump_sub.add_parser('retrieve', help='Retrieve a dump file') |
Ravi Teja | d8be0b4 | 2020-03-18 14:31:46 -0500 | [diff] [blame] | 4804 | parser_dumpretrieve.add_argument("-n,", "--dumpNum", help="The Dump entry to retrieve") |
| 4805 | parser_dumpretrieve.add_argument("-s", "--dumpSaveLoc", help="The location to save the bmc dump file or file path for system dump") |
| 4806 | parser_dumpretrieve.set_defaults(func=dumpRetrieve) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 4807 | |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 4808 | #bmc command for reseting the bmc |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4809 | parser_bmc = subparsers.add_parser('bmc', help="Work with the bmc") |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 4810 | 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] | 4811 | parser_BMCReset = bmc_sub.add_parser('reset', help='Reset the bmc' ) |
| 4812 | 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] | 4813 | 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.") |
| 4814 | parser_bmc.set_defaults(func=bmc) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 4815 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4816 | #add alias to the bmc command |
| 4817 | parser_mc = subparsers.add_parser('mc', help="Work with the management controller") |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 4818 | 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] | 4819 | parser_MCReset = mc_sub.add_parser('reset', help='Reset the bmc' ) |
| 4820 | parser_MCReset.add_argument('type', choices=['warm','cold'], help="Reboot the BMC") |
| 4821 | #parser_MCReset.add_argument('cold', action='store_true', help="Reboot the BMC and CLEAR the configuration") |
| 4822 | 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] | 4823 | parser_MCReset.set_defaults(func=bmcReset) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4824 | parser_mc.set_defaults(func=bmc) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 4825 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 4826 | #gard clear |
| 4827 | parser_gc = subparsers.add_parser("gardclear", help="Used to clear gard records") |
| 4828 | parser_gc.set_defaults(func=gardClear) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 4829 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 4830 | #firmware_flash |
| 4831 | parser_fw = subparsers.add_parser("firmware", help="Work with the system firmware") |
| 4832 | 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] | 4833 | fwflash_subproc.required = True |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 4834 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 4835 | fwflash = fwflash_subproc.add_parser('flash', help="Flash the system firmware") |
| 4836 | fwflash.add_argument('type', choices=['bmc', 'pnor'], help="image type to flash") |
| 4837 | fwflash.add_argument('-f', '--fileloc', required=True, help="The absolute path to the firmware image") |
| 4838 | fwflash.set_defaults(func=fwFlash) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 4839 | |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 4840 | 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] | 4841 | fwActivate.add_argument('imageID', help="The image ID to activate from the firmware list. Ex: 63c95399") |
| 4842 | fwActivate.set_defaults(func=activateFWImage) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 4843 | |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 4844 | fwActivateStatus = fwflash_subproc.add_parser('activation_status', help="Check Status of activations") |
| 4845 | fwActivateStatus.set_defaults(func=activateStatus) |
| 4846 | |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 4847 | fwList = fwflash_subproc.add_parser('list', help="List all of the installed firmware") |
| 4848 | fwList.add_argument('-v', '--verbose', action='store_true', help='Verbose output') |
| 4849 | fwList.set_defaults(func=firmwareList) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 4850 | |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 4851 | fwprint = fwflash_subproc.add_parser('print', help="List all of the installed firmware") |
| 4852 | fwprint.add_argument('-v', '--verbose', action='store_true', help='Verbose output') |
| 4853 | fwprint.set_defaults(func=firmwareList) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 4854 | |
Adriana Kobylak | 5af2fad | 2018-11-08 12:33:43 -0600 | [diff] [blame] | 4855 | fwDelete = fwflash_subproc.add_parser('delete', help="Delete an existing firmware version") |
| 4856 | fwDelete.add_argument('versionID', help="The version ID to delete from the firmware list. Ex: 63c95399") |
| 4857 | fwDelete.set_defaults(func=deleteFWVersion) |
| 4858 | |
Deepak Kodihalli | 22d4df0 | 2018-09-18 06:52:43 -0500 | [diff] [blame] | 4859 | #logging |
| 4860 | parser_logging = subparsers.add_parser("logging", help="logging controls") |
| 4861 | 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] | 4862 | |
Deepak Kodihalli | 22d4df0 | 2018-09-18 06:52:43 -0500 | [diff] [blame] | 4863 | #turn rest api logging on/off |
| 4864 | parser_rest_logging = logging_sub.add_parser("rest_api", help="turn rest api logging on/off") |
| 4865 | parser_rest_logging.add_argument('rest_logging', choices=['on', 'off'], help='The control option for rest logging: on, off') |
| 4866 | parser_rest_logging.set_defaults(func=restLogging) |
Deepak Kodihalli | 02d5328 | 2018-09-18 06:53:31 -0500 | [diff] [blame] | 4867 | |
| 4868 | #remote logging |
| 4869 | parser_remote_logging = logging_sub.add_parser("remote_logging", help="Remote logging (rsyslog) commands") |
| 4870 | parser_remote_logging.add_argument('remote_logging', choices=['view', 'disable'], help='Remote logging (rsyslog) commands') |
| 4871 | parser_remote_logging.set_defaults(func=remoteLogging) |
| 4872 | |
| 4873 | #configure remote logging |
| 4874 | parser_remote_logging_config = logging_sub.add_parser("remote_logging_config", help="Configure remote logging (rsyslog)") |
| 4875 | parser_remote_logging_config.add_argument("-a", "--address", required=True, help="Set IP address of rsyslog server") |
| 4876 | parser_remote_logging_config.add_argument("-p", "--port", required=True, type=int, help="Set Port of rsyslog server") |
| 4877 | parser_remote_logging_config.set_defaults(func=remoteLoggingConfig) |
Dhruvaraj Subhashchandran | 64e7f6f | 2018-10-02 03:42:14 -0500 | [diff] [blame] | 4878 | |
| 4879 | #certificate management |
| 4880 | parser_cert = subparsers.add_parser("certificate", help="Certificate management") |
| 4881 | certMgmt_subproc = parser_cert.add_subparsers(title='subcommands', description='valid certificate commands', help='sub-command help', dest='command') |
| 4882 | |
| 4883 | certUpdate = certMgmt_subproc.add_parser('update', help="Update the certificate") |
| 4884 | certUpdate.add_argument('type', choices=['server', 'client', 'authority'], help="certificate type to update") |
| 4885 | certUpdate.add_argument('service', choices=['https', 'ldap'], help="Service to update") |
| 4886 | certUpdate.add_argument('-f', '--fileloc', required=True, help="The absolute path to the certificate file") |
| 4887 | certUpdate.set_defaults(func=certificateUpdate) |
| 4888 | |
| 4889 | certDelete = certMgmt_subproc.add_parser('delete', help="Delete the certificate") |
| 4890 | certDelete.add_argument('type', choices=['server', 'client', 'authority'], help="certificate type to delete") |
| 4891 | certDelete.add_argument('service', choices=['https', 'ldap'], help="Service to delete the certificate") |
| 4892 | certDelete.set_defaults(func=certificateDelete) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 4893 | |
Marri Devender Rao | dfe81ad | 2019-07-01 05:38:09 -0500 | [diff] [blame] | 4894 | certReplace = certMgmt_subproc.add_parser('replace', |
| 4895 | help="Replace the certificate") |
| 4896 | certReplace.add_argument('type', choices=['server', 'client', 'authority'], |
| 4897 | help="certificate type to replace") |
| 4898 | certReplace.add_argument('service', choices=['https', 'ldap'], |
| 4899 | help="Service to replace the certificate") |
| 4900 | certReplace.add_argument('-f', '--fileloc', required=True, |
| 4901 | help="The absolute path to the certificate file") |
| 4902 | certReplace.set_defaults(func=certificateReplace) |
| 4903 | |
Marri Devender Rao | 3464640 | 2019-07-01 05:46:03 -0500 | [diff] [blame] | 4904 | certDisplay = certMgmt_subproc.add_parser('display', |
| 4905 | help="Print the certificate") |
| 4906 | certDisplay.add_argument('type', choices=['server', 'client', 'authority'], |
| 4907 | help="certificate type to display") |
| 4908 | certDisplay.set_defaults(func=certificateDisplay) |
| 4909 | |
Marri Devender Rao | a208ff8 | 2019-07-01 05:51:27 -0500 | [diff] [blame] | 4910 | certList = certMgmt_subproc.add_parser('list', |
| 4911 | help="Certificate list") |
| 4912 | certList.set_defaults(func=certificateList) |
| 4913 | |
Marri Devender Rao | 3cdf8ae | 2019-07-01 06:01:40 -0500 | [diff] [blame] | 4914 | certGenerateCSR = certMgmt_subproc.add_parser('generatecsr', help="Generate CSR") |
| 4915 | certGenerateCSR.add_argument('type', choices=['server', 'client', 'authority'], |
| 4916 | help="Generate CSR") |
| 4917 | certGenerateCSR.add_argument('city', |
| 4918 | help="The city or locality of the organization making the request") |
| 4919 | certGenerateCSR.add_argument('commonName', |
| 4920 | help="The fully qualified domain name of the component that is being secured.") |
| 4921 | certGenerateCSR.add_argument('country', |
| 4922 | help="The country of the organization making the request") |
| 4923 | certGenerateCSR.add_argument('organization', |
| 4924 | help="The name of the organization making the request.") |
| 4925 | certGenerateCSR.add_argument('organizationUnit', |
| 4926 | help="The name of the unit or division of the organization making the request.") |
| 4927 | certGenerateCSR.add_argument('state', |
| 4928 | help="The state, province, or region of the organization making the request.") |
| 4929 | certGenerateCSR.add_argument('keyPairAlgorithm', choices=['RSA', 'EC'], |
| 4930 | help="The type of key pair for use with signing algorithms.") |
Marri Devender Rao | 3cdf8ae | 2019-07-01 06:01:40 -0500 | [diff] [blame] | 4931 | certGenerateCSR.add_argument('keyCurveId', |
| 4932 | help="The curve ID to be used with the key, if needed based on the value of the 'KeyPairAlgorithm' parameter.") |
| 4933 | certGenerateCSR.add_argument('contactPerson', |
| 4934 | help="The name of the user making the request") |
| 4935 | certGenerateCSR.add_argument('email', |
| 4936 | help="The email address of the contact within the organization") |
| 4937 | certGenerateCSR.add_argument('alternativeNames', |
| 4938 | help="Additional hostnames of the component that is being secured") |
| 4939 | certGenerateCSR.add_argument('givenname', |
| 4940 | help="The given name of the user making the request") |
| 4941 | certGenerateCSR.add_argument('surname', |
| 4942 | help="The surname of the user making the request") |
| 4943 | certGenerateCSR.add_argument('unstructuredname', |
| 4944 | help="he unstructured name of the subject") |
| 4945 | certGenerateCSR.add_argument('initials', |
| 4946 | help="The initials of the user making the request") |
Marri Devender Rao | 3cdf8ae | 2019-07-01 06:01:40 -0500 | [diff] [blame] | 4947 | certGenerateCSR.set_defaults(func=certificateGenerateCSR) |
| 4948 | |
Matt Spinler | 7d426c2 | 2018-09-24 14:42:07 -0500 | [diff] [blame] | 4949 | # local users |
| 4950 | parser_users = subparsers.add_parser("local_users", help="Work with local users") |
| 4951 | parser_users.add_argument('local_users', choices=['disableall','enableall', 'queryenabled'], help="Disable, enable or query local user accounts") |
| 4952 | parser_users.add_argument('-v', '--verbose', action='store_true', help='Verbose output') |
| 4953 | parser_users.set_defaults(func=localUsers) |
| 4954 | |
Ratan Gupta | 9166cd2 | 2018-10-01 18:09:40 +0530 | [diff] [blame] | 4955 | #LDAP |
| 4956 | parser_ldap = subparsers.add_parser("ldap", help="LDAP controls") |
| 4957 | ldap_sub = parser_ldap.add_subparsers(title='subcommands', description='valid subcommands',help="sub-command help", dest='command') |
| 4958 | |
| 4959 | #configure and enable LDAP |
| 4960 | parser_ldap_config = ldap_sub.add_parser("enable", help="Configure and enables the LDAP") |
| 4961 | parser_ldap_config.add_argument("-a", "--uri", required=True, help="Set LDAP server URI") |
| 4962 | parser_ldap_config.add_argument("-B", "--bindDN", required=True, help="Set the bind DN of the LDAP server") |
| 4963 | parser_ldap_config.add_argument("-b", "--baseDN", required=True, help="Set the base DN of the LDAP server") |
| 4964 | parser_ldap_config.add_argument("-p", "--bindPassword", required=True, help="Set the bind password of the LDAP server") |
| 4965 | parser_ldap_config.add_argument("-S", "--scope", choices=['sub','one', 'base'], |
| 4966 | help='Specifies the search scope:subtree, one level or base object.') |
RAJESWARAN THILLAIGOVINDAN | 87f087b | 2019-05-08 04:15:26 -0500 | [diff] [blame] | 4967 | parser_ldap_config.add_argument("-t", "--serverType", required=True, choices=['ActiveDirectory','OpenLDAP'], |
Ratan Gupta | 9166cd2 | 2018-10-01 18:09:40 +0530 | [diff] [blame] | 4968 | help='Specifies the configured server is ActiveDirectory(AD) or OpenLdap') |
RAJESWARAN THILLAIGOVINDAN | 87f087b | 2019-05-08 04:15:26 -0500 | [diff] [blame] | 4969 | parser_ldap_config.add_argument("-g","--groupAttrName", required=False, default='', help="Group Attribute Name") |
| 4970 | parser_ldap_config.add_argument("-u","--userAttrName", required=False, default='', help="User Attribute Name") |
| 4971 | parser_ldap_config.set_defaults(func=enableLDAPConfig) |
Ratan Gupta | 9166cd2 | 2018-10-01 18:09:40 +0530 | [diff] [blame] | 4972 | |
| 4973 | # disable LDAP |
| 4974 | parser_disable_ldap = ldap_sub.add_parser("disable", help="disables the LDAP") |
| 4975 | parser_disable_ldap.set_defaults(func=disableLDAP) |
Nagaraju Goruganti | 7d1fe17 | 2018-11-13 06:09:29 -0600 | [diff] [blame] | 4976 | # view-config |
| 4977 | parser_ldap_config = \ |
| 4978 | ldap_sub.add_parser("view-config", help="prints out a list of all \ |
| 4979 | LDAPS's configured properties") |
| 4980 | parser_ldap_config.set_defaults(func=viewLDAPConfig) |
Ratan Gupta | 9166cd2 | 2018-10-01 18:09:40 +0530 | [diff] [blame] | 4981 | |
Ratan Gupta | feee637 | 2018-10-17 23:25:51 +0530 | [diff] [blame] | 4982 | #create group privilege mapping |
| 4983 | parser_ldap_mapper = ldap_sub.add_parser("privilege-mapper", help="LDAP group privilege controls") |
| 4984 | parser_ldap_mapper_sub = parser_ldap_mapper.add_subparsers(title='subcommands', description='valid subcommands', |
| 4985 | help="sub-command help", dest='command') |
| 4986 | |
| 4987 | parser_ldap_mapper_create = parser_ldap_mapper_sub.add_parser("create", help="Create mapping of ldap group and privilege") |
RAJESWARAN THILLAIGOVINDAN | 87f087b | 2019-05-08 04:15:26 -0500 | [diff] [blame] | 4988 | parser_ldap_mapper_create.add_argument("-t", "--serverType", choices=['ActiveDirectory','OpenLDAP'], |
| 4989 | help='Specifies the configured server is ActiveDirectory(AD) or OpenLdap') |
Ratan Gupta | feee637 | 2018-10-17 23:25:51 +0530 | [diff] [blame] | 4990 | parser_ldap_mapper_create.add_argument("-g","--groupName",required=True,help="Group Name") |
RAJESWARAN THILLAIGOVINDAN | 87f087b | 2019-05-08 04:15:26 -0500 | [diff] [blame] | 4991 | parser_ldap_mapper_create.add_argument("-p","--privilege",choices=['priv-admin','priv-operator','priv-user','priv-callback'],required=True,help="Privilege") |
Ratan Gupta | feee637 | 2018-10-17 23:25:51 +0530 | [diff] [blame] | 4992 | parser_ldap_mapper_create.set_defaults(func=createPrivilegeMapping) |
| 4993 | |
| 4994 | #list group privilege mapping |
| 4995 | parser_ldap_mapper_list = parser_ldap_mapper_sub.add_parser("list",help="List privilege mapping") |
RAJESWARAN THILLAIGOVINDAN | 87f087b | 2019-05-08 04:15:26 -0500 | [diff] [blame] | 4996 | parser_ldap_mapper_list.add_argument("-t", "--serverType", choices=['ActiveDirectory','OpenLDAP'], |
| 4997 | help='Specifies the configured server is ActiveDirectory(AD) or OpenLdap') |
Ratan Gupta | feee637 | 2018-10-17 23:25:51 +0530 | [diff] [blame] | 4998 | parser_ldap_mapper_list.set_defaults(func=listPrivilegeMapping) |
| 4999 | |
| 5000 | #delete group privilege mapping |
| 5001 | parser_ldap_mapper_delete = parser_ldap_mapper_sub.add_parser("delete",help="Delete privilege mapping") |
RAJESWARAN THILLAIGOVINDAN | 87f087b | 2019-05-08 04:15:26 -0500 | [diff] [blame] | 5002 | parser_ldap_mapper_delete.add_argument("-t", "--serverType", choices=['ActiveDirectory','OpenLDAP'], |
| 5003 | help='Specifies the configured server is ActiveDirectory(AD) or OpenLdap') |
Ratan Gupta | feee637 | 2018-10-17 23:25:51 +0530 | [diff] [blame] | 5004 | parser_ldap_mapper_delete.add_argument("-g","--groupName",required=True,help="Group Name") |
| 5005 | parser_ldap_mapper_delete.set_defaults(func=deletePrivilegeMapping) |
| 5006 | |
Sivas SRR | 7883527 | 2018-11-27 05:27:19 -0600 | [diff] [blame] | 5007 | #deleteAll group privilege mapping |
| 5008 | parser_ldap_mapper_delete = parser_ldap_mapper_sub.add_parser("purge",help="Delete All privilege mapping") |
RAJESWARAN THILLAIGOVINDAN | 87f087b | 2019-05-08 04:15:26 -0500 | [diff] [blame] | 5009 | parser_ldap_mapper_delete.add_argument("-t", "--serverType", choices=['ActiveDirectory','OpenLDAP'], |
| 5010 | help='Specifies the configured server is ActiveDirectory(AD) or OpenLdap') |
Sivas SRR | 7883527 | 2018-11-27 05:27:19 -0600 | [diff] [blame] | 5011 | parser_ldap_mapper_delete.set_defaults(func=deleteAllPrivilegeMapping) |
| 5012 | |
Marri Devender Rao | 2c2a516 | 2018-11-05 08:57:11 -0600 | [diff] [blame] | 5013 | # set local user password |
| 5014 | parser_set_password = subparsers.add_parser("set_password", |
| 5015 | help="Set password of local user") |
| 5016 | parser_set_password.add_argument( "-p", "--password", required=True, |
| 5017 | help="Password of local user") |
| 5018 | parser_set_password.set_defaults(func=setPassword) |
| 5019 | |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 5020 | # network |
| 5021 | parser_nw = subparsers.add_parser("network", help="network controls") |
| 5022 | nw_sub = parser_nw.add_subparsers(title='subcommands', |
| 5023 | description='valid subcommands', |
| 5024 | help="sub-command help", |
| 5025 | dest='command') |
| 5026 | |
| 5027 | # enable DHCP |
| 5028 | parser_enable_dhcp = nw_sub.add_parser("enableDHCP", |
| 5029 | help="enables the DHCP on given " |
| 5030 | "Interface") |
| 5031 | parser_enable_dhcp.add_argument("-I", "--Interface", required=True, |
Nagaraju Goruganti | 97a2060 | 2018-11-16 03:06:08 -0600 | [diff] [blame] | 5032 | help="Name of the ethernet interface(it can" |
| 5033 | "be obtained by the " |
| 5034 | "command:network view-config)" |
| 5035 | "Ex: eth0 or eth1 or VLAN(VLAN=eth0_50 etc)") |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 5036 | parser_enable_dhcp.set_defaults(func=enableDHCP) |
| 5037 | |
| 5038 | # disable DHCP |
| 5039 | parser_disable_dhcp = nw_sub.add_parser("disableDHCP", |
| 5040 | help="disables the DHCP on given " |
| 5041 | "Interface") |
| 5042 | parser_disable_dhcp.add_argument("-I", "--Interface", required=True, |
Nagaraju Goruganti | 97a2060 | 2018-11-16 03:06:08 -0600 | [diff] [blame] | 5043 | help="Name of the ethernet interface(it can" |
| 5044 | "be obtained by the " |
| 5045 | "command:network view-config)" |
| 5046 | "Ex: eth0 or eth1 or VLAN(VLAN=eth0_50 etc)") |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 5047 | parser_disable_dhcp.set_defaults(func=disableDHCP) |
| 5048 | |
| 5049 | # get HostName |
| 5050 | parser_gethostname = nw_sub.add_parser("getHostName", |
| 5051 | help="prints out HostName") |
| 5052 | parser_gethostname.set_defaults(func=getHostname) |
| 5053 | |
| 5054 | # set HostName |
| 5055 | parser_sethostname = nw_sub.add_parser("setHostName", help="sets HostName") |
| 5056 | parser_sethostname.add_argument("-H", "--HostName", required=True, |
| 5057 | help="A HostName for the BMC") |
| 5058 | parser_sethostname.set_defaults(func=setHostname) |
| 5059 | |
| 5060 | # get domainname |
| 5061 | parser_getdomainname = nw_sub.add_parser("getDomainName", |
| 5062 | help="prints out DomainName of " |
| 5063 | "given Interface") |
| 5064 | parser_getdomainname.add_argument("-I", "--Interface", required=True, |
Nagaraju Goruganti | 97a2060 | 2018-11-16 03:06:08 -0600 | [diff] [blame] | 5065 | help="Name of the ethernet interface(it " |
| 5066 | "can be obtained by the " |
| 5067 | "command:network view-config)" |
| 5068 | "Ex: eth0 or eth1 or VLAN(VLAN=eth0_50 etc)") |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 5069 | parser_getdomainname.set_defaults(func=getDomainName) |
| 5070 | |
| 5071 | # set domainname |
| 5072 | parser_setdomainname = nw_sub.add_parser("setDomainName", |
| 5073 | help="sets DomainName of given " |
| 5074 | "Interface") |
| 5075 | parser_setdomainname.add_argument("-D", "--DomainName", required=True, |
| 5076 | help="Ex: DomainName=Domain1,Domain2,...") |
| 5077 | parser_setdomainname.add_argument("-I", "--Interface", required=True, |
Nagaraju Goruganti | 97a2060 | 2018-11-16 03:06:08 -0600 | [diff] [blame] | 5078 | help="Name of the ethernet interface(it " |
| 5079 | "can be obtained by the " |
| 5080 | "command:network view-config)" |
| 5081 | "Ex: eth0 or eth1 or VLAN(VLAN=eth0_50 etc)") |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 5082 | parser_setdomainname.set_defaults(func=setDomainName) |
| 5083 | |
| 5084 | # get MACAddress |
| 5085 | parser_getmacaddress = nw_sub.add_parser("getMACAddress", |
| 5086 | help="prints out MACAddress the " |
| 5087 | "given Interface") |
| 5088 | parser_getmacaddress.add_argument("-I", "--Interface", required=True, |
Nagaraju Goruganti | 97a2060 | 2018-11-16 03:06:08 -0600 | [diff] [blame] | 5089 | help="Name of the ethernet interface(it " |
| 5090 | "can be obtained by the " |
| 5091 | "command:network view-config)" |
| 5092 | "Ex: eth0 or eth1 or VLAN(VLAN=eth0_50 etc)") |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 5093 | parser_getmacaddress.set_defaults(func=getMACAddress) |
| 5094 | |
| 5095 | # set MACAddress |
| 5096 | parser_setmacaddress = nw_sub.add_parser("setMACAddress", |
| 5097 | help="sets MACAddress") |
| 5098 | parser_setmacaddress.add_argument("-MA", "--MACAddress", required=True, |
| 5099 | help="A MACAddress for the given " |
| 5100 | "Interface") |
| 5101 | parser_setmacaddress.add_argument("-I", "--Interface", required=True, |
Nagaraju Goruganti | 97a2060 | 2018-11-16 03:06:08 -0600 | [diff] [blame] | 5102 | help="Name of the ethernet interface(it can" |
| 5103 | "be obtained by the " |
| 5104 | "command:network view-config)" |
| 5105 | "Ex: eth0 or eth1 or VLAN(VLAN=eth0_50 etc)") |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 5106 | parser_setmacaddress.set_defaults(func=setMACAddress) |
| 5107 | |
| 5108 | # get DefaultGW |
| 5109 | parser_getdefaultgw = nw_sub.add_parser("getDefaultGW", |
| 5110 | help="prints out DefaultGateway " |
| 5111 | "the BMC") |
| 5112 | parser_getdefaultgw.set_defaults(func=getDefaultGateway) |
| 5113 | |
| 5114 | # set DefaultGW |
| 5115 | parser_setdefaultgw = nw_sub.add_parser("setDefaultGW", |
| 5116 | help="sets DefaultGW") |
| 5117 | parser_setdefaultgw.add_argument("-GW", "--DefaultGW", required=True, |
| 5118 | help="A DefaultGateway for the BMC") |
| 5119 | parser_setdefaultgw.set_defaults(func=setDefaultGateway) |
| 5120 | |
| 5121 | # view network Config |
| 5122 | parser_ldap_config = nw_sub.add_parser("view-config", help="prints out a " |
| 5123 | "list of all network's configured " |
| 5124 | "properties") |
| 5125 | parser_ldap_config.set_defaults(func=viewNWConfig) |
| 5126 | |
| 5127 | # get DNS |
| 5128 | parser_getDNS = nw_sub.add_parser("getDNS", |
| 5129 | help="prints out DNS servers on the " |
| 5130 | "given interface") |
| 5131 | parser_getDNS.add_argument("-I", "--Interface", required=True, |
Nagaraju Goruganti | 97a2060 | 2018-11-16 03:06:08 -0600 | [diff] [blame] | 5132 | help="Name of the ethernet interface(it can" |
| 5133 | "be obtained by the " |
| 5134 | "command:network view-config)" |
| 5135 | "Ex: eth0 or eth1 or VLAN(VLAN=eth0_50 etc)") |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 5136 | parser_getDNS.set_defaults(func=getDNS) |
| 5137 | |
| 5138 | # set DNS |
| 5139 | parser_setDNS = nw_sub.add_parser("setDNS", |
| 5140 | help="sets DNS servers on the given " |
| 5141 | "interface") |
| 5142 | parser_setDNS.add_argument("-d", "--DNSServers", required=True, |
| 5143 | help="Ex: DNSSERVERS=DNS1,DNS2,...") |
| 5144 | parser_setDNS.add_argument("-I", "--Interface", required=True, |
Nagaraju Goruganti | 97a2060 | 2018-11-16 03:06:08 -0600 | [diff] [blame] | 5145 | help="Name of the ethernet interface(it can" |
| 5146 | "be obtained by the " |
| 5147 | "command:network view-config)" |
| 5148 | "Ex: eth0 or eth1 or VLAN(VLAN=eth0_50 etc)") |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 5149 | parser_setDNS.set_defaults(func=setDNS) |
| 5150 | |
| 5151 | # get NTP |
| 5152 | parser_getNTP = nw_sub.add_parser("getNTP", |
| 5153 | help="prints out NTP servers on the " |
| 5154 | "given interface") |
| 5155 | parser_getNTP.add_argument("-I", "--Interface", required=True, |
Nagaraju Goruganti | 97a2060 | 2018-11-16 03:06:08 -0600 | [diff] [blame] | 5156 | help="Name of the ethernet interface(it can" |
| 5157 | "be obtained by the " |
| 5158 | "command:network view-config)" |
| 5159 | "Ex: eth0 or eth1 or VLAN(VLAN=eth0_50 etc)") |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 5160 | parser_getNTP.set_defaults(func=getNTP) |
| 5161 | |
| 5162 | # set NTP |
| 5163 | parser_setNTP = nw_sub.add_parser("setNTP", |
| 5164 | help="sets NTP servers on the given " |
| 5165 | "interface") |
| 5166 | parser_setNTP.add_argument("-N", "--NTPServers", required=True, |
| 5167 | help="Ex: NTPSERVERS=NTP1,NTP2,...") |
| 5168 | parser_setNTP.add_argument("-I", "--Interface", required=True, |
Nagaraju Goruganti | 97a2060 | 2018-11-16 03:06:08 -0600 | [diff] [blame] | 5169 | help="Name of the ethernet interface(it can" |
| 5170 | "be obtained by the " |
| 5171 | "command:network view-config)" |
| 5172 | "Ex: eth0 or eth1 or VLAN(VLAN=eth0_50 etc)") |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 5173 | parser_setNTP.set_defaults(func=setNTP) |
| 5174 | |
Nagaraju Goruganti | 97a2060 | 2018-11-16 03:06:08 -0600 | [diff] [blame] | 5175 | # configure IP |
| 5176 | parser_ip_config = nw_sub.add_parser("addIP", help="Sets IP address to" |
| 5177 | "given interface") |
| 5178 | parser_ip_config.add_argument("-a", "--address", required=True, |
| 5179 | help="IP address of given interface") |
| 5180 | parser_ip_config.add_argument("-gw", "--gateway", required=False, default='', |
| 5181 | help="The gateway for given interface") |
| 5182 | parser_ip_config.add_argument("-l", "--prefixLength", required=True, |
| 5183 | help="The prefixLength of IP address") |
Sunitha Harish | 0baf637 | 2019-07-31 03:59:03 -0500 | [diff] [blame] | 5184 | parser_ip_config.add_argument("-p", "--type", required=True, |
| 5185 | choices=['ipv4', 'ipv6'], |
Nagaraju Goruganti | 97a2060 | 2018-11-16 03:06:08 -0600 | [diff] [blame] | 5186 | help="The protocol type of the given" |
| 5187 | "IP address") |
| 5188 | parser_ip_config.add_argument("-I", "--Interface", required=True, |
| 5189 | help="Name of the ethernet interface(it can" |
| 5190 | "be obtained by the " |
| 5191 | "command:network view-config)" |
| 5192 | "Ex: eth0 or eth1 or VLAN(VLAN=eth0_50 etc)") |
| 5193 | parser_ip_config.set_defaults(func=addIP) |
| 5194 | |
| 5195 | # getIP |
| 5196 | parser_getIP = nw_sub.add_parser("getIP", help="prints out IP address" |
| 5197 | "of given interface") |
| 5198 | parser_getIP.add_argument("-I", "--Interface", required=True, |
| 5199 | help="Name of the ethernet interface(it can" |
| 5200 | "be obtained by the command:network view-config)" |
| 5201 | "Ex: eth0 or eth1 or VLAN(VLAN=eth0_50 etc)") |
| 5202 | parser_getIP.set_defaults(func=getIP) |
| 5203 | |
| 5204 | # rmIP |
| 5205 | parser_rmIP = nw_sub.add_parser("rmIP", help="deletes IP address" |
| 5206 | "of given interface") |
| 5207 | parser_rmIP.add_argument("-a", "--address", required=True, |
| 5208 | help="IP address to remove form given Interface") |
| 5209 | parser_rmIP.add_argument("-I", "--Interface", required=True, |
| 5210 | help="Name of the ethernet interface(it can" |
| 5211 | "be obtained by the command:network view-config)" |
| 5212 | "Ex: eth0 or eth1 or VLAN(VLAN=eth0_50 etc)") |
| 5213 | parser_rmIP.set_defaults(func=deleteIP) |
| 5214 | |
Nagaraju Goruganti | f21d43c | 2018-11-19 10:47:19 -0600 | [diff] [blame] | 5215 | # add VLAN |
| 5216 | parser_create_vlan = nw_sub.add_parser("addVLAN", help="enables VLAN " |
| 5217 | "on given interface with given " |
| 5218 | "VLAN Identifier") |
| 5219 | parser_create_vlan.add_argument("-I", "--Interface", required=True, |
| 5220 | choices=['eth0', 'eth1'], |
| 5221 | help="Name of the ethernet interface") |
| 5222 | parser_create_vlan.add_argument("-n", "--Identifier", required=True, |
| 5223 | help="VLAN Identifier") |
| 5224 | parser_create_vlan.set_defaults(func=addVLAN) |
| 5225 | |
| 5226 | # delete VLAN |
| 5227 | parser_delete_vlan = nw_sub.add_parser("deleteVLAN", help="disables VLAN " |
| 5228 | "on given interface with given " |
| 5229 | "VLAN Identifier") |
| 5230 | parser_delete_vlan.add_argument("-I", "--Interface", required=True, |
| 5231 | help="Name of the ethernet interface(it can" |
| 5232 | "be obtained by the " |
| 5233 | "command:network view-config)" |
| 5234 | "Ex: eth0 or eth1 or VLAN(VLAN=eth0_50 etc)") |
| 5235 | parser_delete_vlan.set_defaults(func=deleteVLAN) |
| 5236 | |
| 5237 | # viewDHCPConfig |
| 5238 | parser_viewDHCPConfig = nw_sub.add_parser("viewDHCPConfig", |
| 5239 | help="Shows DHCP configured " |
| 5240 | "Properties") |
| 5241 | parser_viewDHCPConfig.set_defaults(func=viewDHCPConfig) |
| 5242 | |
| 5243 | # configureDHCP |
| 5244 | parser_configDHCP = nw_sub.add_parser("configureDHCP", |
| 5245 | help="Configures/updates DHCP " |
| 5246 | "Properties") |
| 5247 | parser_configDHCP.add_argument("-d", "--DNSEnabled", type=str2bool, |
| 5248 | required=True, help="Sets DNSEnabled property") |
| 5249 | parser_configDHCP.add_argument("-n", "--HostNameEnabled", type=str2bool, |
| 5250 | required=True, |
| 5251 | help="Sets HostNameEnabled property") |
| 5252 | parser_configDHCP.add_argument("-t", "--NTPEnabled", type=str2bool, |
| 5253 | required=True, |
| 5254 | help="Sets NTPEnabled property") |
| 5255 | parser_configDHCP.add_argument("-s", "--SendHostNameEnabled", type=str2bool, |
| 5256 | required=True, |
| 5257 | help="Sets SendHostNameEnabled property") |
| 5258 | parser_configDHCP.set_defaults(func=configureDHCP) |
| 5259 | |
| 5260 | # network factory reset |
| 5261 | parser_nw_reset = nw_sub.add_parser("nwReset", |
| 5262 | help="Resets networks setting to " |
| 5263 | "factory defaults. " |
| 5264 | "note:Reset settings will be applied " |
| 5265 | "after BMC reboot") |
| 5266 | parser_nw_reset.set_defaults(func=nwReset) |
| 5267 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 5268 | return parser |
| 5269 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 5270 | def main(argv=None): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 5271 | """ |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 5272 | main function for running the command line utility as a sub application |
| 5273 | """ |
| 5274 | global toolVersion |
Justin Thaler | c42e4e5 | 2020-05-13 12:04:24 -0500 | [diff] [blame] | 5275 | toolVersion = "1.19" |
Sunitha Harish | c99faba | 2019-07-19 06:55:22 -0500 | [diff] [blame] | 5276 | global isRedfishSupport |
RAJESWARAN THILLAIGOVINDAN | 87f087b | 2019-05-08 04:15:26 -0500 | [diff] [blame] | 5277 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 5278 | parser = createCommandParser() |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 5279 | args = parser.parse_args(argv) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 5280 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 5281 | totTimeStart = int(round(time.time()*1000)) |
| 5282 | |
| 5283 | if(sys.version_info < (3,0)): |
| 5284 | urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) |
| 5285 | if sys.version_info >= (3,0): |
| 5286 | requests.packages.urllib3.disable_warnings(requests.packages.urllib3.exceptions.InsecureRequestWarning) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 5287 | if (args.version): |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 5288 | print("Version: "+ toolVersion) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 5289 | sys.exit(0) |
| 5290 | 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] | 5291 | mysess = None |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 5292 | print(selPrint('N/A', args, mysess)) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 5293 | else: |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 5294 | if(hasattr(args, 'host') and hasattr(args,'user')): |
| 5295 | if (args.askpw): |
| 5296 | pw = getpass.getpass() |
| 5297 | elif(args.PW is not None): |
| 5298 | pw = args.PW |
Joseph Reynolds | a2d54c5 | 2019-06-11 22:02:57 -0500 | [diff] [blame] | 5299 | elif(args.PWenvvar): |
| 5300 | pw = os.environ['OPENBMCTOOL_PASSWORD'] |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 5301 | else: |
| 5302 | print("You must specify a password") |
| 5303 | sys.exit() |
| 5304 | logintimeStart = int(round(time.time()*1000)) |
Joy Onyerikwu | 182c3a3 | 2019-10-15 08:33:59 -0500 | [diff] [blame] | 5305 | mysess = login(args.host, args.user, pw, args.json, |
| 5306 | args.command == 'set_password') |
Sunitha Harish | 336cda2 | 2019-07-23 02:02:52 -0500 | [diff] [blame] | 5307 | if(mysess == None): |
| 5308 | print("Login Failed!") |
| 5309 | sys.exit() |
Justin Thaler | a9415b4 | 2018-05-25 19:40:13 -0500 | [diff] [blame] | 5310 | if(sys.version_info < (3,0)): |
| 5311 | if isinstance(mysess, basestring): |
| 5312 | print(mysess) |
| 5313 | sys.exit(1) |
| 5314 | elif sys.version_info >= (3,0): |
| 5315 | if isinstance(mysess, str): |
| 5316 | print(mysess) |
| 5317 | sys.exit(1) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 5318 | logintimeStop = int(round(time.time()*1000)) |
Sunitha Harish | c99faba | 2019-07-19 06:55:22 -0500 | [diff] [blame] | 5319 | isRedfishSupport = redfishSupportPresent(args.host,mysess) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 5320 | commandTimeStart = int(round(time.time()*1000)) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 5321 | output = args.func(args.host, args, mysess) |
| 5322 | commandTimeStop = int(round(time.time()*1000)) |
Justin Thaler | 761484a | 2019-03-26 19:20:23 -0500 | [diff] [blame] | 5323 | if isinstance(output, dict): |
| 5324 | print(json.dumps(output, sort_keys=True, indent=4, separators=(',', ': '), ensure_ascii=False)) |
| 5325 | else: |
| 5326 | print(output) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 5327 | if (mysess is not None): |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 5328 | logout(args.host, args.user, pw, mysess, args.json) |
| 5329 | if(args.procTime): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 5330 | print("Total time: " + str(int(round(time.time()*1000))- totTimeStart)) |
| 5331 | print("loginTime: " + str(logintimeStop - logintimeStart)) |
| 5332 | print("command Time: " + str(commandTimeStop - commandTimeStart)) |
| 5333 | else: |
Joseph Reynolds | a2d54c5 | 2019-06-11 22:02:57 -0500 | [diff] [blame] | 5334 | print("usage:\n" |
| 5335 | " OPENBMCTOOL_PASSWORD=secret # if using -E\n" |
| 5336 | " openbmctool.py [-h] -H HOST -U USER {-A | -P PW | -E} [-j]\n" + |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 5337 | "\t[-t POLICYTABLELOC] [-V]\n" + |
Deepak Kodihalli | 22d4df0 | 2018-09-18 06:52:43 -0500 | [diff] [blame] | 5338 | "\t{fru,sensors,sel,chassis,collect_service_data, \ |
| 5339 | health_check,dump,bmc,mc,gardclear,firmware,logging}\n" + |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 5340 | "\t...\n" + |
| 5341 | "openbmctool.py: error: the following arguments are required: -H/--host, -U/--user") |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 5342 | sys.exit() |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 5343 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 5344 | if __name__ == '__main__': |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 5345 | """ |
| 5346 | main function when called from the command line |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 5347 | |
| 5348 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 5349 | import sys |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 5350 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 5351 | isTTY = sys.stdout.isatty() |
| 5352 | assert sys.version_info >= (2,7) |
| 5353 | main() |