def run(self,ts,data,args): outlist=[] fmt=args['format'] try: etpl = string.Template(fmt) #e = etpl.safe_substitute(v) if data is None: return None if isinstance(data,six.string_types): ts.stop() return None # list of dicts if isinstance(data,list): for row in data: rowstr = etpl.safe_substitute(row) outlist.append(rowstr) return outlist # dict if isinstance(data,dict): dstr = etpl.safe_substitute(data) return dstr except TypeError as e: ts.oc.log.error(u'exception in {}: {}'.format(self.code, e)) ts.oc.log.error("format:"+str(fmt)) ts.oc.log.error("data:"+str(data)) return None
def run(self,ts,data,args): regex = re.compile(args['search']) if args['dest']: dest = args['dest'] else: dest = args['field'] if isinstance(data, six.string_types): return regex.sub(args['replace'],data) if isinstance(data,list): outlist=[] for row in data: if isinstance(row, six.string_types): outlist.append(regex.sub(args['replace'],row)) if isinstance(row, dict): d = row.copy() if args['field'] in row: d[dest] = regex.sub(args['replace'],row[args['field']]) outlist.append(d) return outlist if isinstance(data, dict): if args['field'] in data: data[dest] = regex.sub(args['replace'],data[args['field']]) return data ts.oc.log.error("unreachable code in {}, bad data type: {}".format(self.code, type(data))) ts.stop() return None
def run(self, ts, data, args): path = args['path'] # check if path in whitelist if ts.oc.tpconf_enabled: if not '*' in self.tpconf['read']: if not path in self.tpconf['read']: # error! cannot read this file! tpconfline = 'Maybe try --tpconf {}:read={}'.format( self.code, path) ts.stop( "File {} is not allowed to read. Whitelist read files: {}. Maybe try: {} ?" .format(path, self.tpconf['read'], tpconfline)) return None try: fh = open(path, encoding='utf-8', errors='surrogateescape') except IOError as e: ts.oc.log.error('Cannot read file \'{}\': {}'.format(path, e)) ts.stop() return None return fh
def run(self,ts,data,args): try: if isinstance(data,list): if int(args['n'])==1: return data[0] else: return data[:int(args['n'])] else: ts.stop() return None except IndexError: ts.stop() return None return None
def run(self,ts,data,args): chars = args['chars'] if isinstance(data, six.string_types): return data.strip(chars) # assume list, but catch exceptions! outlist=[] try: for l in data: outlist.append(l.strip(chars)) return outlist except: ts.oc.log.error('{}: Unexpected data ({}): {}'.format(self.code, type(data), str(data)[:100])) ts.stop() return None
def run(self,ts,data,args): out = dict() try: count = int(args['count']) timeout = int(args['timeout']) r = pyping.ping(hostname = args["host"], timeout = timeout, count=count) except Exception as e: ts.stop(e) return ts.details = "{} ({}) rtt: {}/{}/{} lost: {}".format(r.destination, r.destination_ip, r.min_rtt, r.avg_rtt, r.max_rtt, r.packet_lost) for aname in ['destination', 'destination_ip', 'min_rtt', 'avg_rtt', 'max_rtt','packet_lost']: out[aname] = getattr(r,aname) return out
def run(self, ts, data, args): self.ts = ts if not isinstance(data, list): data = [] if args['path']: try: data.extend(self.getrec(args['path'], args, 0)) except (IOError, OSError, PermissionError, FileNotFoundError) as e: self.ts.oc.log.debug("exception: {}".format(e)) ts.stop() return None else: self.ts.oc.log.error( 'DIR requires path, e.g. DIR path=/var/log or DIR path=/var/log/mail.log' ) return data
def run(self,ts,data,args): nfork=0 for row in data: if isinstance(row,dict): newrow = row.copy() else: newrow = row newts = ts.fork(ts.iname,ts.route,ts.method).run(newrow) ts.stop() return None # if isinstance(row,dict): # d = ts.getvars() # d.update(row) # elif isinstance(row,six.string_types): # d = ts.getvars() # d['_str']=row # d['_nfork']=nfork # newname = args['name'].format(**d) # etpl = string.Template(args['name']) # newname = etpl.safe_substitute(d) # newts = ts.fork(newname,ts.route,ts.method) # newts = ts.fork(ts.iname,ts.route,ts.method) # newts.run(row) # nfork+=1 # ts.stop() return None
def run(self,ts,data,args): out = dict() url = args['url'] if not self.validate_url(ts, url): ts.oc.error('URL {} not validated against {}'.format(url, self.tpconf['url'])) ts.stop('Not allowed this url. Set tpconf GETURL:url={}'.format(url)) return None if args['redirect'] == '1': redir = True else: redir = False if args['verify'] == '1': verify = True else: verify = False try: timeout = int(args['timeout']) except ValueError: ts.oc.error('Cannot parse timeout value {}'.format(repr(args['timeout']))) ts.stop('Bad timeout value') return None if args['user'] and args['pass']: auth = (args['user'], args['pass']) else: auth = None try: r = requests.get(url, allow_redirects = redir, verify = verify, auth = auth, timeout = timeout) except requests.exceptions.RequestException as e: ts.stop('GETURL exception {} {}'.format(type(e), e)) return None out['elapsed'] = r.elapsed.total_seconds() for field in ['status_code', 'text']: out[field] = getattr(r,field, None) for header in r.headers: out['h_'+header] = r.headers[header] return out
def run(self, ts, data, args): def mysuid(user): # override basic env variables os.environ['HOME'] = user.pw_dir os.environ['SHELL'] = user.pw_shell os.setuid(user.pw_uid) callarg = shlex.split(args['prog']) # is this program allowed? if ts.oc.tpconf_enabled: if not ('*' in self.tpconf['safebin'] or callarg[0] in self.tpconf['safebin']): ts.stop( '{} is not in safebin: {}. Maybe try --tpconf {}:safebin={}' .format(callarg[0], str(self.tpconf['safebin']), self.code, callarg[0])) #ts.stop() return None try: user = pwd.getpwnam(self.tpconf['user']) except KeyError: ts.oc.log.error('no such user {}'.format(self.tpconf['user'])) ts.stop() return None if os.geteuid() != 0: # not root! if user.pw_uid != os.geteuid(): ts.oc.log.error( 'Current user {} (uid:{}) is not root and cannot swith to user \'{}\'' .format(getpass.getuser(), os.geteuid(), self.tpconf['user'])) ts.stop() return try: p = subprocess.Popen(callarg, stdout=subprocess.PIPE, stderr=subprocess.PIPE, preexec_fn=lambda: mysuid(user)) except OSError as e: # report problem ts.oc.log.error( 'Cannot run {} ({}). May be {} is not installed?'.format( str(callarg), e, callarg[0])) ts.stop() return None p.wait() res = p.communicate() out = dict() out['stdout'] = res[0].decode('utf8') out['stderr'] = res[1].decode('utf8') out['code'] = p.returncode #if p.returncode or res[1]: # ts.oc.log.error('error code: {}, stderr: {}'.format(p.returncode, res[1])) # ts.stop() # return None #return res[0].split('\n') return out