def load_config(self, command): """ Check that config file exists :param command: Command's name :type command: str :return: schema, configuration """ conffile = join(self.commanddir, '{0}.json'.format(command)) schema_id = 'monitoringplugin.{0}'.format(command) if not exists(conffile): raise self.CheckError( 'Impossible to find command: {0}'.format(command)) # Load configuration and validate it with open(conffile) as f: try: conf = json.load(f) schema = cschema.get(schema_id) if not cschema.validate(conf, schema_id): raise ValueError('Impossible to validate configuration') except ValueError as err: raise self.CheckError('Invalid command {0}: {1}'.format( command, err)) except cschema.NoSchemaError: raise self.CheckError( 'Cannot validate command {0}: {1}'.format(command, err)) return schema, conf
def send_event(event, url=None): if ws.enable_crossdomain_send_events and url is not None: payload = { 'event': json.dumps(event) } response = requests.post(url, data=payload) if response.status_code != 200: api_response = json.loads(response.text) return (api_response['data'], api_response['total']) else: return HTTPError(response.status_code, response.text) else: events = ensure_iterable(event) exchange = ws.amqp.exchange_name_events for event in events: if schema.validate(event, 'cevent'): sname = 'cevent.{0}'.format(event['event_type']) if schema.validate(event, sname): if event['event_type'] == 'eue': sname = 'cevent.eue.{0}'.format( event['type_message'] ) if not schema.validate(event, sname): continue rk = '{0}.{1}.{2}.{3}.{4}'.format( u'{0}'.format(event['connector']), u'{0}'.format(event['connector_name']), u'{0}'.format(event['event_type']), u'{0}'.format(event['source_type']), u'{0}'.format(event['component']) ) if event['source_type'] == 'resource': rk = '{0}.{1}'.format(rk, event['resource']) ws.amqp.publish(event, rk, exchange) return events
def send_event(event, url=None): if ws.enable_crossdomain_send_events and url is not None: payload = {"event": json.dumps(event)} response = requests.post(url, data=payload) if response.status_code != 200: api_response = json.loads(response.text) return (api_response["data"], api_response["total"]) else: return HTTPError(response.status_code, response.text) else: events = ensure_iterable(event) exchange = ws.amqp.exchange_name_events for event in events: if schema.validate(event, "cevent"): sname = "cevent.{0}".format(event["event_type"]) if schema.validate(event, sname): if event["event_type"] == "eue": sname = "cevent.eue.{0}".format(event["type_message"]) if not schema.validate(event, sname): continue rk = "{0}.{1}.{2}.{3}.{4}".format( event["connector"], event["connector_name"], event["event_type"], event["source_type"], event["component"], ) if event["source_type"] == "resource": rk = "{0}.{1}".format(rk, event["resource"]) ws.amqp.publish(event, rk, exchange) return events
def load_config(self, command): """ Check that config file exists :param command: Command's name :type command: str :return: schema, configuration """ conffile = join(self.commanddir, '{0}.json'.format(command)) schema_id = 'monitoringplugin.{0}'.format(command) if not exists(conffile): raise self.CheckError( 'Impossible to find command: {0}'.format(command) ) # Load configuration and validate it with open(conffile) as f: try: conf = json.load(f) schema = cschema.get(schema_id) if not cschema.validate(conf, schema_id): raise ValueError('Impossible to validate configuration') except ValueError as err: raise self.CheckError( 'Invalid command {0}: {1}'.format(command, err) ) except cschema.NoSchemaError: raise self.CheckError( 'Cannot validate command {0}: {1}'.format(command, err) ) return schema, conf
def work(self, job, *args, **kwargs): if schema.validate(job, 'crecord.job'): self.do_job(job) else: self.logger.error(u'Invalid job: {0}'.format(job))