def main(): config = Config(BASE_DIR + '/configuration/') sqlite = Sqlite(BASE_DIR + '/storage/harvester.db', config.XMLconfiguration) pandadb = PandaDB(BASE_DIR + '/settings.ini') es = Es(BASE_DIR + '/settings.ini') metrics = pandadb.get_db_metrics() dictHarvesterHosts, dictHarvesterInstnaces = es.get_last_submittedtime() sqlite.instances_availability(dictHarvesterInstnaces, metrics) instances = sqlite.get_instances() for instance in instances: for harvesterhost in instances[instance]: if harvesterhost != 'none': availability = instances[instance][harvesterhost]['availability'] notificated = instances[instance][harvesterhost]['notificated'] contacts = instances[instance][harvesterhost]['contacts'] text = instances[instance][harvesterhost]['errorsdesc'] if (availability == 0 or availability == 10) and notificated == 0: email = Notifications(text=text, subject='Service issues on {0} {1}'.format(instance, harvesterhost), to=contacts) email.send_notification_email() sqlite.update_field('notificated', 1, instance, harvesterhost) email = {} elif availability == 100 and notificated == 1: sqlite.update_field('notificated', 0, instance, harvesterhost) host = harvesterhost.split('.')[0] doc = ServiceDocument('harv_{0}_{1}'.format(instance, host), availability=availability, contact=','.join(contacts), availabilitydesc="PandaHarvester instance:{0}".format(instance), availabilityinfo="{0}".format(text)) XSLSPublisher.send(doc) doc = {}
def publish(cls, metrics): """Publish service metrics to CERN XSLS service.""" api_url = current_app.config.get('METRICS_XSLS_API_URL') service_id = current_app.config.get('METRICS_XSLS_SERVICE_ID') if api_url is None: raise RuntimeError("METRICS_XSLS_API_URL must be set.") if service_id is None: raise RuntimeError("METRICS_XSLS_SERVICE_ID must be set.") # Create service document. doc = ServiceDocument( service_id, contact=current_app.config.get('METRICS_XSLS_EMAIL'), webpage=current_app.config.get('METRICS_XSLS_WEBPAGE'), ) for obj in metrics: doc.add_numericvalue(obj.metric, obj.value) # Compute availability try: avail_imp = current_app.config.get( 'METRICS_XSLS_AVAILABILITY') if avail_imp: avail_func = import_string(avail_imp) doc.availability = avail_func(doc) except Exception: current_app.logger.exception("Could not compute availability") resp = XSLSPublisher.send(doc, api_url=api_url) if resp.status_code != 200: raise RuntimeError("%s did not accept service XML (status %s)" % ( resp.content, resp.status_code), resp.content)
def test_xslspublisher_send(): """.""" httpretty.register_uri(httpretty.POST, EXAMPLE_URL, body="", status=200) dt = datetime(2015, 1, 1, 0, 0, 0) doc = ServiceDocument('myid', timestamp=dt) resp = XSLSPublisher.send(doc, api_url=EXAMPLE_URL) assert resp.status_code == 200 assert httpretty.last_request().body.decode('utf-8') == \ '<serviceupdate xmlns="http://sls.cern.ch/SLS/XML/update">' \ '<id>myid</id><availability>100</availability>' \ '<timestamp>2015-01-01T00:00:00</timestamp></serviceupdate>'
def test_xslspublisher_send(): """.""" httpretty.register_uri(httpretty.POST, EXAMPLE_URL, body="", status=200) dt = datetime(2015, 1, 1, 0, 0, 0) doc = ServiceDocument('myid', timestamp=dt) resp = XSLSPublisher.send(doc, api_url=EXAMPLE_URL) assert resp.status_code == 200 # Parse multipart/form-data (not supported by HTTPretty) content_type = httpretty.last_request().headers['Content-Type'].split(';') content_type = [x.strip() for x in content_type] key, boundary = content_type[1].split("=") data = cgi.parse_multipart(httpretty.last_request().rfile, {'boundary': boundary.encode('utf-8')}) assert 'file' in data assert data['file'][0].decode('utf-8') == \ '<serviceupdate xmlns="http://sls.cern.ch/SLS/XML/update">' \ '<id>myid</id><status>available</status>' \ '<timestamp>2015-01-01T00:00:00</timestamp></serviceupdate>'