def poll_process(self, cmd, stdout, stderr): proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) while True: try: fds = [proc.stdout.fileno(), proc.stderr.fileno()] ret = select.select(fds, [], []) for fd in ret[0]: if fd == proc.stdout.fileno(): read = proc.stdout.readline() #stdout.append(read) if fd == proc.stderr.fileno(): read = proc.stderr.readline() self.log.error("stderr" + read) if proc.poll() != None: self.log.warning("poll break") break line = proc.stdout.readline() #logging.debug(' '.join(cmd)) if line.startswith('CNTR'): #self.log.debug(line) stdout.put(parse_line(line)) except Exception as e: #self.log.debug(str(e)) print(e) pass
def popen(self, *args): """ make cmd command and popen it """ cmd = [ 'sflowtool', ] cmd.extend(args) # cmd.extend(self.hosts) self.log.debug(' '.join(cmd)) # get both stdout and stderr return subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
def probe(self): codec = munge.get_codec('yaml')() msg = {} msg['data'] = [] msg['ts'] = (datetime.datetime.utcnow() - datetime.datetime(1970, 1, 1)).total_seconds() # FIXME use poll for host in self.hosts: args = shlex.split(self.command.format(host=host)) proc = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) # TODO poll, timeout, maybe from parent process for better control? with proc.stdout: #msg['data'].append(proc.stdout.read()) msg['data'].append(codec.load(proc.stdout)) return msg