def ptmongo(dashboard, panel): log.warn('[' + dashboard['name'] + ']' + panel['title'] + ' Splunk Transmitting...') namespace = dashboards.mergestr(dashboard['name'], panel['title'], panel['ptype']) splunk = Splunk() db = DB(namespace) while True: try: job = splunk.service.jobs.create(panel['query']) time.sleep(3) results = [ u for u in splunk.results.ResultsReader(splunk.service.jobs[ job.sid].preview(**{"count": 100})) ] for r in results: db.col.update(r, r, upsert=True) socketio.emit('status', {'on': True}, namespace='/' + namespace) job.cancel() except Exception as e: log.warn('[' + dashboard['name'] + '][' + panel['title'] + ']' + str(e)) socketio.emit('status', {'on': False}, namespace='/' + namespace) time.sleep(2)
def __make_tree(self): self.__start_time = datetime.now() self.__event_start_tree() chunk = math.floor(self.__scan_items_count / 10) log.warn(chunk) chunk_group = 0 for item in self.__scan_items: path = item[0] ntype = item[1] item_array = path.split(os.sep) item_name = item_array[-1] item_path = item_array[:-1] parent_node = self.tree.get_node_tree_path(item_path) if ntype == 0: node = self.tree.create_node_dir(parent_node, item_name) elif ntype == 1: node = self.tree.create_node_file(parent_node, item_name) chunk_group += 1 if chunk_group == chunk: chunk_group = 0 self.__progress += 10 self.__event_progress(self.__progress) end_time = datetime.now() qqq = end_time - self.__start_time self.__event_finish_tree(qqq.seconds)
def watchdog(): log.warn('[LAUNCH] Monitoring dashboards') [ Process(target=watch_dashboard, name=dashboard['name'], args=(dashboard, )).start() for dashboard in dashboards.get() ]
def connect(self): while True: try: self.col = pymongo.MongoClient(mng['host'], int( mng['port'])).splunk[self.collection] break except Exception: log.warn('[ERROR] Can`t connect to MongoDB') time.sleep(2)
def watch_dashboard(dashboard): log.warn('[' + dashboard['name'] + '] Watching...') [ Process(target=ptmongo, args=(dashboard, panel)).start() for panel in dashboards.parse(dashboard) if panel['query'] != 'empty' ] [ Process(target=mngtopan, args=(dashboard, panel)).start() for panel in dashboards.parse(dashboard) if panel['query'] != 'empty' ]
def connect(self): while True: try: self.service = client.connect(host=spc['host'], port=spc['port'], username=spc['user'], password=spc['password']) self.results = results self.ns = client.namespace(sharing='user', owner='wall', app='splunkb_templates') break except Exception: log.warn('[ERROR] Can`t connect to Splunk') time.sleep(2)
def compare(): for dashboard in get(): log.warn( parse(dashboard) )
def dev_webhook(): if app.config['MAIN_CHAT'] == '': log.warn('Init bot first by "/init" command') return abort(403) if request.headers.get('content-type') == 'application/json': jData = request.get_json() ''' Field "message" contains data. Main field, without it bot don't send anything. ''' if 'message' in jData.keys(): data = jData['message'] ''' Field "to" contains destination chat. It can be set to "MAIN" or "DEV" for chats from .env. Default is "DEV". ''' SEND_TO = app.config['DEV_CHAT'] if 'to' in jData.keys(): send_to = jData['to'] if send_to == 'MAIN': SEND_TO = app.config['MAIN_CHAT'] elif send_to == 'DEV': SEND_TO = app.config['DEV_CHAT'] else: SEND_TO = send_to ''' Field "id" contains service name. If service is muted than message will be skipped. ''' if 'id' in jData.keys(): id = jData['id'] add(id) if is_muted(id): return "OK" ''' Type can be "markdown/html/plain/file". Default type - "plain". ''' if 'type' in jData.keys(): type = jData['type'] try: if type == 'markdown': bot.send_message(SEND_TO, data, parse_mode='Markdown') return "OK" elif type == 'html': bot.send_message(SEND_TO, data, parse_mode='HTML') return "OK" elif type == 'plain': bot.send_message(SEND_TO, data) return "OK" elif type == 'file': file = open('/home/app/bot/files/' + data, 'rb') bot.send_document(SEND_TO, file, timeout=60) return "OK" except Exception: bot.send_message(SEND_TO, data) return "OK" else: bot.send_message(SEND_TO, data) return "OK" else: log.error("Wrong json data") abort(403) else: log.error("Wrong content type") abort(403)
def mngtopan(dashboard, panel): log.warn('[' + dashboard['name'] + ']' + panel['title'] + ' Mongo Transmitting...') namespace = dashboards.mergestr(dashboard['name'], panel['title'], panel['ptype']) db = DB(namespace)