def login(self): try: NXAPITransport.init(target_url=self.target_url, username=self.username, password=self.password) NXAPITransport.cli('! Testing if device is online') except Exception as e: self._is_online = False e = str(e).lower() if 'timed out' in e: self.error_online = ('Connection timed out!! Make sure that ' 'URL provided ({0}) is accurate' '').format(self.target_url) elif 'unauthorized' in e: self.error_online = ( 'Unauthorized!! Make sure that username ' '({0}) and/or password provided are accurate' '').format(self.username) elif 'target machine actively refused it' in e: self.error_online = ('NXAPI TCP Port not open!! Make sure ' '"feature nxapi" is configured on ' 'target Nexus switch') else: self.error_online = e else: self._is_online = True return self._is_online
def login(self): try: NXAPITransport.init(target_url=self.target_url, username=self.username, password=self.password) NXAPITransport.cli('! Testing if device is online') except Exception as e: self._is_online = False e = str(e).lower() if 'timed out' in e: self.error_online = ('Connection timed out!! Make sure that ' 'URL provided ({0}) is accurate' '').format(self.target_url) elif 'unauthorized' in e: self.error_online = ('Unauthorized!! Make sure that username ' '({0}) and/or password provided are accurate' '').format(self.username) elif 'target machine actively refused it' in e: self.error_online = ('NXAPI TCP Port not open!! Make sure ' '"feature nxapi" is configured on ' 'target Nexus switch') else: self.error_online = e else: self._is_online = True return self._is_online
def get_dirstats(self): ''' @return: dict d with keys matching exactly as attributes of dashboardperdevice.models.DirStats ''' l = list() modules_dirs = [['sup-active', 'bootflash'], ['sup-standby', 'bootflash']] if self._is_online: for module, dirpath in modules_dirs: cmd = 'dir {0}://{1}'.format(dirpath, module) o = NXAPITransport.cli(cmd) d = dict() try: d['used'] = re.search('(\d+ bytes) used', o).group(1) d['free'] = re.search('(\d+ bytes) free', o).group(1) d['total'] = re.search('(\d+ bytes) total', o).group(1) used = float(d['used'].rstrip(' bytes')) total = float(d['total'].rstrip(' bytes')) used_percent = (used / total) * 100 d['used_percent'] = '{0}%'.format(int(used_percent)) except: d['used'] = d['free'] = d['total'] = d[ 'used_percent'] = 'NA' d['module'] = module d['dirpath'] = dirpath l.append(d) return l
def get_dirstats(self): ''' @return: dict d with keys matching exactly as attributes of dashboardperdevice.models.DirStats ''' l = list() modules_dirs = [['sup-active', 'bootflash'], ['sup-standby', 'bootflash']] if self._is_online: for module, dirpath in modules_dirs: cmd = 'dir {0}://{1}'.format(dirpath, module) o = NXAPITransport.cli(cmd) d = dict() try: d['used'] = re.search('(\d+ bytes) used', o).group(1) d['free'] = re.search('(\d+ bytes) free', o).group(1) d['total'] = re.search('(\d+ bytes) total', o).group(1) used = float(d['used'].rstrip(' bytes')) total = float(d['total'].rstrip(' bytes')) used_percent = (used/total)*100 d['used_percent'] = '{0}%'.format(int(used_percent)) except: d['used'] = d['free'] = d['total'] = d['used_percent'] = 'NA' d['module'] = module d['dirpath'] = dirpath l.append(d) return l
def get_osinfo(self): ''' @return: dict d with keys: 'osdevicename', 'osplatform', 'ostime', 'osuptime' ''' d = {'osdevicename': '', 'osplatform': '', 'ostime': '', 'osuptime': ''} if self._is_online: version = VERSION_OUT_FRMT o = json.loads(NXAPITransport.clid('show version')) version = version.format(chassis=o['chassis_id'], image=o['kick_file_name']) osuptime = ('days: {0}, hrs: {1}, mins: {2}, secs: {3}' ''.format(o['kern_uptm_days'], o['kern_uptm_hrs'], o['kern_uptm_mins'], o['kern_uptm_secs'])) devicename = o['host_name'] try: o = json.loads(NXAPITransport.clid('show clock')) except cmd_exec_error: # N3K issue o['simple_time'] = NXAPITransport.cli('show clock') ostime = o["simple_time"] d['osplatform'] = version d['osdevicename'] = devicename d['ostime'] = ostime d['osuptime'] = osuptime return d