def test(resource_identifier): """test a resource""" resource = Resource.query.filter_by(identifier=resource_identifier).first() if resource is None: flash(gettext('Resource not found'), 'danger') return redirect(request.referrer) from healthcheck import run_test_resource result = run_test_resource( resource) if request.method == 'GET': if result.message == 'Skipped': msg = gettext('INFO') flash('%s: %s' % (msg, result.message), 'info') elif result.message not in ['OK', None, 'None']: msg = gettext('ERROR') flash('%s: %s' % (msg, result.message), 'danger') else: flash(gettext('Resource tested successfully'), 'success') return redirect(url_for('get_resource_by_id', lang=g.current_lang, identifier=resource_identifier)) elif request.method == 'POST': return jsonify(result.get_report())
def test(resource_identifier): """test a resource""" resource = Resource.query.filter_by(identifier=resource_identifier).first() if resource is None: flash(gettext('Resource not found'), 'danger') return redirect(request.referrer) from healthcheck import run_test_resource result = run_test_resource(resource) if request.method == 'GET': if result.message == 'Skipped': msg = gettext('INFO') flash('%s: %s' % (msg, result.message), 'info') elif result.message not in ['OK', None, 'None']: msg = gettext('ERROR') flash('%s: %s' % (msg, result.message), 'danger') else: flash(gettext('Resource tested successfully'), 'success') return redirect( url_for('get_resource_by_id', lang=g.current_lang, identifier=resource_identifier)) elif request.method == 'POST': return jsonify(result.get_report())
def add(): if not g.user.is_authenticated(): return render_template('add.html') if request.method == 'GET': return render_template('add.html') resource_type = request.form['resource_type'] url = request.form['url'].strip() resource = Resource.query.filter_by(resource_type=resource_type, url=url).first() if resource is not None: flash('service already registered (%s, %s)' % (resource_type, url), 'danger') if 'resource_type' in request.args: rtype = request.args.get('resource_type') return redirect(url_for('add', resource_type=rtype)) return redirect(url_for('add')) [title, success, response_time, message, start_time] = run_test_resource(resource_type, url) resource_to_add = Resource(current_user, resource_type, title, url) run_to_add = Run(resource_to_add, success, response_time, message, start_time) DB.session.add(resource_to_add) DB.session.add(run_to_add) try: DB.session.commit() flash('service registered (%s, %s)' % (resource_type, url), 'success') except Exception, err: DB.session.rollback() flash(str(err), 'danger')
def add(): if not g.user.is_authenticated(): return render_template('add.html') if request.method == 'GET': return render_template('add.html') resource_type = request.form['resource_type'] url = request.form['url'].strip() resource = Resource.query.filter_by(resource_type=resource_type, url=url).first() if resource is not None: flash('service already registered (%s, %s)' % (resource_type, url), 'danger') if 'resource_type' in request.args: rtype = request.args.get('resource_type') return redirect(url_for('add', resource_type=rtype)) return redirect(url_for('add')) [title, success, response_time, message, start_time] = run_test_resource( resource_type, url) resource_to_add = Resource(current_user, resource_type, title, url) run_to_add = Run(resource_to_add, success, response_time, message, start_time) DB.session.add(resource_to_add) DB.session.add(run_to_add) try: DB.session.commit() flash('service registered (%s, %s)' % (resource_type, url), 'success') except Exception, err: DB.session.rollback() flash(str(err), 'danger')
def testRunResoures(self): # Do the whole healthcheck for all Resources for now resources = Resource.query.all() for resource in resources: result = run_test_resource(resource) print('resource: %s result=%s' % (resource.url, result.success)) run = Run(resource, result) print('Adding Run: success=%s, response_time=%ss\n' % (str(run.success), run.response_time)) self.db.session.add(run) self.db.session.commit() self.db.session.close() # Verify resources = Resource.query.all() for resource in resources: # Each Resource should have one Run self.assertEqual(resource.runs.count(), 1, 'RunCount should be 1 for %s' % resource.url) self.assertEqual( resource.runs[0].success, True, 'Run should be success for %s report=%s' % (resource.url, str(resource.runs[0])))
def add(): """add resource""" if not g.user.is_authenticated(): return render_template('add.html') if request.method == 'GET': return render_template('add.html') tag_list = [] resource_type = request.form['resource_type'] tags = request.form.getlist('tags') url = request.form['url'].strip() resource = Resource.query.filter_by(resource_type=resource_type, url=url).first() if resource is not None: msg = gettext('Service already registered') flash('%s (%s, %s)' % (msg, resource_type, url), 'danger') if 'resource_type' in request.args: rtype = request.args.get('resource_type') return redirect(url_for('add', lang=g.current_lang, resource_type=rtype)) return redirect(url_for('add', lang=g.current_lang)) [title, success, response_time, message, start_time] = run_test_resource( APP.config, resource_type, url) if not success: flash(message, 'danger') return redirect(url_for('add', lang=g.current_lang, resource_type=resource_type)) if tags: for tag in tags: tag_found = False for tag_obj in Tag.query.all(): if tag == tag_obj.name: # use existing tag_found = True tag_list.append(tag_obj) if not tag_found: # add new tag_list.append(Tag(name=tag)) resource_to_add = Resource(current_user, resource_type, title, url, tags=tag_list) run_to_add = Run(resource_to_add, success, response_time, message, start_time) DB.session.add(resource_to_add) DB.session.add(run_to_add) try: DB.session.commit() msg = gettext('Service registered') flash('%s (%s, %s)' % (msg, resource_type, url), 'success') except Exception as err: DB.session.rollback() flash(str(err), 'danger') return redirect(url_for('home', lang=g.current_lang))
def test(resource_identifier): """test a resource""" resource = Resource.query.filter_by(identifier=resource_identifier).first() if resource is None: flash('resource not found', 'danger') return redirect(request.referrer) [title, success, response_time, message, start_time] = run_test_resource(resource.resource_type, resource.url) if message not in ['OK', None, 'None']: flash('ERROR: %s' % message, 'danger') else: flash('Resource tested successfully', 'success') return redirect( url_for('get_resource_by_id', identifier=resource_identifier))
def test(resource_identifier): """test a resource""" resource = Resource.query.filter_by(identifier=resource_identifier).first() if resource is None: flash(gettext('Resource not found'), 'danger') return redirect(request.referrer) [title, success, response_time, message, start_time] = run_test_resource( APP.config, resource.resource_type, resource.url) if message not in ['OK', None, 'None']: msg = gettext('ERROR') flash('%s: %s' % (msg, message), 'danger') else: flash(gettext('Resource tested successfully'), 'success') return redirect(url_for('get_resource_by_id', lang=g.current_lang, identifier=resource_identifier))
def test(resource_identifier): resource = Resource.query.filter_by(identifier=resource_identifier).first() if resource is None: flash('resource not found', 'danger') return redirect(request.referrer) [title, success, response_time, message, start_time] = run_test_resource( resource.resource_type, resource.url) run_to_add = Run(resource, success, response_time, message, start_time) if message not in ['OK', None, 'None']: flash('ERROR: %s' % message, 'danger') else: flash('Resource tested successfully', 'success') DB.session.add(run_to_add) try: DB.session.commit() except Exception, err: DB.session.rollback() flash(str(err), 'danger')
def test(resource_identifier): resource = Resource.query.filter_by(identifier=resource_identifier).first() if resource is None: flash('resource not found', 'danger') return redirect(request.referrer) [title, success, response_time, message, start_time] = run_test_resource(resource.resource_type, resource.url) run_to_add = Run(resource, success, response_time, message, start_time) if message not in ['OK', None, 'None']: flash('ERROR: %s' % message, 'danger') else: flash('Resource tested successfully', 'success') DB.session.add(run_to_add) try: DB.session.commit() except Exception, err: DB.session.rollback() flash(str(err), 'danger')
def add(): """add resource""" if not g.user.is_authenticated(): return render_template('add.html') if request.method == 'GET': return render_template('add.html') resource_type = request.form['resource_type'] tags = request.form.getlist('tags') url = request.form['url'].strip() resources_to_add = [] from healthcheck import sniff_test_resource, run_test_resource sniffed_resources = sniff_test_resource(CONFIG, resource_type, url) if not sniffed_resources: msg = gettext("No resources detected") LOGGER.exception() flash(msg, 'danger') for ( resource_type, resource_url, title, success, response_time, message, start_time, resource_tags, ) in sniffed_resources: # sniffed_resources may return list of resource # types different from initial one # so we need to test each row separately resource = Resource.query.filter_by(resource_type=resource_type, url=url).first() if resource is not None: msg = gettext('Service already registered') flash('%s (%s, %s)' % (msg, resource_type, url), 'danger') if len(sniffed_resources) == 1 and 'resource_type' in request.args: return redirect(url_for('add', lang=g.current_lang)) tags_to_add = [] for tag in chain(tags, resource_tags): tag_obj = tag if not isinstance(tag, Tag): tag_obj = Tag.query.filter_by(name=tag).first() if tag_obj is None: tag_obj = Tag(name=tag) tags_to_add.append(tag_obj) resource_to_add = Resource(current_user, resource_type, title, resource_url, tags=tags_to_add) resources_to_add.append(resource_to_add) probe_to_add = None checks_to_add = [] # Always add a default Probe and Check(s) # from the GHC_PROBE_DEFAULTS conf if resource_type in CONFIG['GHC_PROBE_DEFAULTS']: resource_settings = CONFIG['GHC_PROBE_DEFAULTS'][resource_type] probe_class = resource_settings['probe_class'] if probe_class: # Add the default Probe probe_obj = Factory.create_obj(probe_class) probe_to_add = ProbeVars( resource_to_add, probe_class, probe_obj.get_default_parameter_values()) # Add optional default (parameterized) # Checks to add to this Probe checks_info = probe_obj.get_checks_info() checks_param_info = probe_obj.get_plugin_vars()['CHECKS_AVAIL'] for check_class in checks_info: check_param_info = checks_param_info[check_class] if 'default' in checks_info[check_class]: if checks_info[check_class]['default']: # Filter out params for Check with fixed values param_defs = check_param_info['PARAM_DEFS'] param_vals = {} for param in param_defs: if param_defs[param]['value']: param_vals[param] = \ param_defs[param]['value'] check_vars = CheckVars(probe_to_add, check_class, param_vals) checks_to_add.append(check_vars) result = run_test_resource(resource_to_add) run_to_add = Run(resource_to_add, result) DB.session.add(resource_to_add) # prepopulate notifications for current user resource_to_add.set_recipients('email', [g.user.email]) if probe_to_add: DB.session.add(probe_to_add) for check_to_add in checks_to_add: DB.session.add(check_to_add) DB.session.add(run_to_add) try: DB.session.commit() msg = gettext('Services registered') flash('%s (%s, %s)' % (msg, resource_type, url), 'success') except Exception as err: DB.session.rollback() flash(str(err), 'danger') return redirect(url_for('home', lang=g.current_lang)) if len(resources_to_add) == 1: return edit_resource(resources_to_add[0].identifier) return redirect(url_for('home', lang=g.current_lang))
print('Testing %s %s' % (resource.resource_type, resource.url)) if not resource.active: print('Resource is not active. Skipping') continue # Get the status of the last run, # assume success if there is none last_run_success = True last_run = resource.last_run if last_run: last_run_success = last_run.success # Run test result = run_test_resource(resource) run1 = Run(resource, result) print('Adding Run: success=%s, response_time=%ss\n' % (str(run1.success), run1.response_time)) DB.session.add(run1) # commit or rollback each run to avoid long-lived transactions # see https://github.com/geopython/GeoHealthCheck/issues/14 db_commit() if APP.config['GHC_NOTIFICATIONS']: # Attempt notification try:
email2 = raw_input('Enter your email again: ').strip() if email1 != email2: raise ValueError('Emails must match') user_to_add = User(username, password1, email1, role='admin') DB.session.add(user_to_add) elif sys.argv[1] == 'drop': print('Dropping database objects') DB.drop_all() elif sys.argv[1] == 'run': print('Running health check tests') from healthcheck import run_test_resource for res in Resource.query.all(): # run all tests print('Testing %s %s' % (res.resource_type, res.url)) last_run_success = res.last_run.success run_to_add = run_test_resource(res.resource_type, res.url) run1 = Run(res, run_to_add[1], run_to_add[2], run_to_add[3], run_to_add[4]) print('Adding run') DB.session.add(run1) if APP.config['GHC_NOTIFICATIONS']: notify(APP.config, res, run1, last_run_success) elif sys.argv[1] == 'flush': print('Flushing runs older than %d days' % APP.config['GHC_RETENTION_DAYS']) for run1 in Run.query.all(): here_and_now = datetime.utcnow()
def add(): """add resource""" if not g.user.is_authenticated(): return render_template('add.html') if request.method == 'GET': return render_template('add.html') resource_type = request.form['resource_type'] tags = request.form.getlist('tags') url = request.form['url'].strip() resources_to_add = [] sniffed_resources = sniff_test_resource(CONFIG, resource_type, url) if not sniffed_resources: msg = gettext("No resources detected") LOGGER.exception() flash(msg, 'danger') for (resource_type, resource_url, title, success, response_time, message, start_time, resource_tags,) in sniffed_resources: # sniffed_resources may return list of resource # types different from initial one # so we need to test each row separately resource = Resource.query.filter_by(resource_type=resource_type, url=url).first() if resource is not None: msg = gettext('Service already registered') flash('%s (%s, %s)' % (msg, resource_type, url), 'danger') if len(sniffed_resources) == 1 and 'resource_type' in request.args: return redirect(url_for('add', lang=g.current_lang)) tags_to_add = [] for tag in chain(tags, resource_tags): tag_obj = tag if not isinstance(tag, Tag): tag_obj = Tag.query.filter_by(name=tag).first() if tag_obj is None: tag_obj = Tag(name=tag) tags_to_add.append(tag_obj) resource_to_add = Resource(current_user, resource_type, title, resource_url, tags=tags_to_add) resources_to_add.append(resource_to_add) probe_to_add = None checks_to_add = [] # Always add a default Probe and Check(s) # from the GHC_PROBE_DEFAULTS conf if resource_type in CONFIG['GHC_PROBE_DEFAULTS']: resource_settings = CONFIG['GHC_PROBE_DEFAULTS'][resource_type] probe_class = resource_settings['probe_class'] if probe_class: # Add the default Probe probe_obj = Factory.create_obj(probe_class) probe_to_add = ProbeVars( resource_to_add, probe_class, probe_obj.get_default_parameter_values()) # Add optional default (parameterized) # Checks to add to this Probe checks_info = probe_obj.get_checks_info() checks_param_info = probe_obj.get_plugin_vars()['CHECKS_AVAIL'] for check_class in checks_info: check_param_info = checks_param_info[check_class] if 'default' in checks_info[check_class]: if checks_info[check_class]['default']: # Filter out params for Check with fixed values param_defs = check_param_info['PARAM_DEFS'] param_vals = {} for param in param_defs: if param_defs[param]['value']: param_vals[param] =\ param_defs[param]['value'] check_vars = CheckVars( probe_to_add, check_class, param_vals) checks_to_add.append(check_vars) result = run_test_resource(resource_to_add) run_to_add = Run(resource_to_add, result) DB.session.add(resource_to_add) # prepopulate notifications for current user resource_to_add.set_recipients('email', [g.user.email]) if probe_to_add: DB.session.add(probe_to_add) for check_to_add in checks_to_add: DB.session.add(check_to_add) DB.session.add(run_to_add) try: DB.session.commit() msg = gettext('Services registered') flash('%s (%s, %s)' % (msg, resource_type, url), 'success') except Exception as err: DB.session.rollback() flash(str(err), 'danger') return redirect(url_for('home', lang=g.current_lang)) if len(resources_to_add) == 1: return edit_resource(resources_to_add[0].identifier) return redirect(url_for('home', lang=g.current_lang))
elif sys.argv[1] == 'run': print('START - Running health check tests on %s' % datetime.utcnow().isoformat()) from healthcheck import run_test_resource for res in Resource.query.all(): # run all tests print('Testing %s %s' % (res.resource_type, res.url)) # Get the status of the last run, # assume success if there is none last_run_success = True last_run = res.last_run if last_run: last_run_success = last_run.success # Run test run_to_add = run_test_resource( APP.config, res.resource_type, res.url) run1 = Run(res, run_to_add[1], run_to_add[2], run_to_add[3], run_to_add[4]) print('Adding Run: success=%s, response_time=%ss\n' % (str(run1.success), run1.response_time)) DB.session.add(run1) # commit or rollback each run to avoid long-lived transactions # see https://github.com/geopython/GeoHealthCheck/issues/14 db_commit() if APP.config['GHC_NOTIFICATIONS']: # Attempt notification try: notify(APP.config, res, run1, last_run_success)
DB.session.add(user_to_add) try: DB.session.commit() except Exception, err: DB.session.rollback() msg = str(err) print(msg) elif sys.argv[1] == 'drop': print('Dropping database objects') DB.drop_all() elif sys.argv[1] == 'run': print('Running health check tests') from healthcheck import run_test_resource for res in Resource.query.all(): # run all tests print('Testing %s %s' % (res.resource_type, res.url)) run_to_add = run_test_resource(res.resource_type, res.url) run1 = Run(res, run_to_add[1], run_to_add[2], run_to_add[3], run_to_add[4]) print('Adding run') DB.session.add(run1) try: DB.session.commit() except Exception, err: DB.session.rollback() msg = str(err) print(msg) elif sys.argv[1] == 'flush': print('Flushing runs older than %d days' % config.GHC_RETENTION_DAYS) for run1 in Run.query.all(): here_and_now = datetime.utcnow()
def add(): """add resource""" if not g.user.is_authenticated(): return render_template('add.html') if request.method == 'GET': return render_template('add.html') tag_list = [] resource_type = request.form['resource_type'] tags = request.form.getlist('tags') url = request.form['url'].strip() resource = Resource.query.filter_by(resource_type=resource_type, url=url).first() if resource is not None: msg = gettext('Service already registered') flash('%s (%s, %s)' % (msg, resource_type, url), 'danger') if 'resource_type' in request.args: rtype = request.args.get('resource_type') return redirect( url_for('add', lang=g.current_lang, resource_type=rtype)) return redirect(url_for('add', lang=g.current_lang)) [title, success, response_time, message, start_time] = sniff_test_resource(APP.config, resource_type, url) if not success: flash(message, 'danger') return redirect( url_for('add', lang=g.current_lang, resource_type=resource_type)) if tags: for tag in tags: tag_found = False for tag_obj in Tag.query.all(): if tag == tag_obj.name: # use existing tag_found = True tag_list.append(tag_obj) if not tag_found: # add new tag_list.append(Tag(name=tag)) resource_to_add = Resource(current_user, resource_type, title, url, tags=tag_list) probe_to_add = None checks_to_add = [] # Always add a default Probe and Check(s) from the GHC_PROBE_DEFAULTS conf if resource_type in APP.config['GHC_PROBE_DEFAULTS']: resource_settings = APP.config['GHC_PROBE_DEFAULTS'][resource_type] probe_class = resource_settings['probe_class'] if probe_class: # Add the default Probe probe_obj = Factory.create_obj(probe_class) probe_to_add = ProbeVars(resource_to_add, probe_class, probe_obj.get_default_parameter_values()) # Add optional default (parameterized) Checks to add to this Probe checks_info = probe_obj.get_checks_info() checks_param_info = probe_obj.get_plugin_vars()['CHECKS_AVAIL'] for check_class in checks_info: check_param_info = checks_param_info[check_class] if 'default' in checks_info[check_class]: if checks_info[check_class]['default']: # Filter out params for Check with fixed values param_defs = check_param_info['PARAM_DEFS'] param_vals = {} for param in param_defs: if param_defs[param]['value']: param_vals[param] = param_defs[param]['value'] check_vars = CheckVars(probe_to_add, check_class, param_vals) checks_to_add.append(check_vars) result = run_test_resource(resource_to_add) run_to_add = Run(resource_to_add, result) DB.session.add(resource_to_add) if probe_to_add: DB.session.add(probe_to_add) for check_to_add in checks_to_add: DB.session.add(check_to_add) DB.session.add(run_to_add) try: DB.session.commit() msg = gettext('Service registered') flash('%s (%s, %s)' % (msg, resource_type, url), 'success') except Exception as err: DB.session.rollback() flash(str(err), 'danger') return redirect(url_for('home', lang=g.current_lang)) else: return edit_resource(resource_to_add.identifier)