def getStatsData(self, viewObject=None, viewName='Flow Statistics', csvFile=None, csvEnableFileTimestamp=False, displayStats=True, silentMode=False, ignoreError=False): """ Description For IxNetwork version >= 8.50. Get stats by the statistic name or get stats by providing a view object handle. This method get stats using /api/v1/sessions/{id}/ixnetwork/statistics/view/{id}/data to get attributes columnCaptions, pageValues and totalPages. This method uses new API starting in version 8.50. Parameters csvFile = None or . None will not create a CSV file. Provide a .csv to record all stats to a CSV file. Example: getStats(sessionUrl, csvFile='Flow_Statistics.csv') csvEnableFileTimestamp = True or False. If True, timestamp will be appended to the filename. displayStats: True or False. True=Display stats. ignoreError: True or False. Returns None if viewName is not found. viewObject: The view object: http://{apiServerIp:port}/api/v1/sessions/2/ixnetwork/statistics/view/13 A view object handle could be obtained by calling getViewObject(). viewName options (Not case sensitive): NOTE: Not all statistics are listed here. You could get the statistic viewName directly from the IxNetwork GUI in the statistics. 'Port Statistics' 'Tx-Rx Frame Rate Statistics' 'Port CPU Statistics' 'Global Protocol Statistics' 'Protocols Summary' 'Port Summary' 'BGP Peer Per Port' 'OSPFv2-RTR Drill Down' 'OSPFv2-RTR Per Port' 'IPv4 Drill Down' 'L2-L3 Test Summary Statistics' 'Flow Statistics' 'Traffic Item Statistics' 'IGMP Host Drill Down' 'IGMP Host Per Port' 'IPv6 Drill Down' 'MLD Host Drill Down' 'MLD Host Per Port' 'PIMv6 IF Drill Down' 'PIMv6 IF Per Port' 'Flow View' Note: Not all of the viewNames are listed here. You have to get the exact names from the IxNetwork GUI in statistics based on your protocol(s). Return a dictionary of all the stats: statDict[rowNumber][columnName] == statValue Get stats on row 2 for 'Tx Frames' = statDict[2]['Tx Frames'] """ if viewObject == None: breakFlag = 0 counterStop = 30 for counter in range(1, counterStop+1): viewList = self.ixnObj.get('%s/%s/%s' % (self.ixnObj.sessionUrl, 'statistics', 'view'), silentMode=silentMode) views = ['%s/%s/%s/%s' % (self.ixnObj.sessionUrl, 'statistics', 'view', str(i['id'])) for i in viewList.json()] if silentMode is False: self.ixnObj.logInfo('\ngetStats: Searching for viewObj for viewName: {0}'.format( viewName), timestamp=False) for view in views: #print('\nview:', view) response = self.ixnObj.get('%s' % view, silentMode=True) captionMatch = re.match(viewName, response.json()['caption'], re.I) if captionMatch: # viewObj: sessionUrl + /statistics/view/11' viewObject = view breakFlag = 1 break if breakFlag == 1: break if counter < counterStop: self.ixnObj.logInfo('\nGetting statview [{0}] is not ready. Waiting {1}/{2} seconds.'.format( viewName, counter, counterStop), timestamp=False) time.sleep(1) continue if counter == counterStop: if viewObject == None and ignoreError == False: raise IxNetRestApiException("viewObj wasn't found for viewName: {0}".format(viewName)) if viewObject == None and ignoreError == True: return None if silentMode is False: self.ixnObj.logInfo('\n[{0}] viewObj is: {1}'.format(viewName, viewObject)) counterStop = 30 for counter in range(0, counterStop+1): response = self.ixnObj.get(viewObject+'/data', silentMode=silentMode) totalPages = response.json()['totalPages'] #self.ixnObj.logInfo('totalPages: {0}'.format(totalPages), timestamp=False) if totalPages == 'null' and counter < counterStop: self.ixnObj.logInfo('\nGetting total pages is not ready yet. Waiting {0}/{1} seconds'.format( counter, counterStop), timestamp=False) time.sleep(1) continue if totalPages != 'null' and counter < counterStop: break if counter == counterStop: raise IxNetRestApiException('getStats failed: Getting total pages') if csvFile != None: import csv csvFileName = csvFile.replace(' ', '_') if csvEnableFileTimestamp: import datetime timestamp = datetime.datetime.now().strftime('%H%M%S') if '.' in csvFileName: csvFileNameTemp = csvFileName.split('.')[0] csvFileNameExtension = csvFileName.split('.')[1] csvFileName = csvFileNameTemp+'_'+timestamp+'.'+csvFileNameExtension else: csvFileName = csvFileName+'_'+timestamp csvFile = open(csvFileName, 'w') csvWriteObj = csv.writer(csvFile) flowNumber = 1 statDict = {} getColumnCaptionFlag = 0 for pageNumber in range(1,totalPages+1): self.ixnObj.patch(viewObject+'/data', data={'currentPage': pageNumber}, silentMode=silentMode) counterStop = 30 for counter in range(1, counterStop+1): response = self.ixnObj.get(viewObject+'/data', silentMode=silentMode) if counter < counterStop: if response.json()['columnCaptions'] == [] or response.json()['pageValues'] == []: self.ixnObj.logInfo('[{0}] stat values not ready yet. Wait {1}/{2} seconds.'.format( viewName, counter, counterStop)) time.sleep(1) continue if response.json()['columnCaptions'] != [] or response.json()['pageValues'] != []: break if counter == counterStop: raise IxNetRestApiException('IxNetwork API server failed to provide stats') # Get the stat column names one time only if getColumnCaptionFlag == 0: getColumnCaptionFlag = 1 columnList = response.json()['columnCaptions'] if csvFile != None: csvWriteObj.writerow(columnList) statValueList = response.json()['pageValues'] for statValue in statValueList: if csvFile != None: csvWriteObj.writerow(statValue[0]) if displayStats: self.ixnObj.logInfo('\nRow: %d' % flowNumber, timestamp=False) statDict[flowNumber] = {} index = 0 for statValue in statValue[0]: statName = columnList[index] statDict[flowNumber].update({statName: statValue}) if displayStats: self.ixnObj.logInfo('\t%s: %s' % (statName, statValue), timestamp=False) index += 1 flowNumber += 1 if csvFile != None: csvFile.close() return statDict