def update_email(): form = UpdateEmailForm() if request.method == 'POST': if form.validate_on_submit(): if not verify_password(flask_login.current_user.id, form.password.data): flash('Current password is invalid', 'danger') return redirect(url_for('main.account')) if not form.email.data == form.email_verify.data: flash('Email addresses did not match', 'danger') return redirect(url_for('main.account')) try: current_user = Users.query.filter_by(username=flask_login.current_user.id).first() current_user.email = form.email.data db.session.commit() flash('Email successfully updated', 'success') except Exception as exc: log.error('Failed to update email address: {}'.format(exc)) flash('Failed to update email address', 'danger') else: for dummy, errors in form.errors.items(): for error in errors: flash(error, 'danger') return redirect(url_for('main.account'))
def update_password(): form = UpdatePasswordForm() if request.method == 'POST': if form.validate_on_submit(): if not verify_password(flask_login.current_user.id, form.current_password.data): flash('Current password is invalid', 'danger') return redirect(url_for('main.account')) if not form.new_password.data == form.verify_password.data: flash('Passwords did not match', 'danger') return redirect(url_for('main.account')) if test_password(form.new_password.data): reqs = form.new_password.description flash('Password did not meet {}'.format(reqs), 'danger') return redirect(url_for('main.account')) try: current_user = Users.query.filter_by(username=flask_login.current_user.id).first() current_user.password = sha256_crypt.hash(form.new_password.data) db.session.commit() flash('Password successfully updated', 'success') except Exception as exc: log.error('Failed to update user password: {}'.format(exc)) flash('Failed to update password', 'danger') else: for dummy, errors in form.errors.items(): for error in errors: flash(error, 'danger') return redirect(url_for('main.account'))
def send_smtp_message(recipient, subject, message): '''Send SMTP message''' current_smtp = json.loads(get_smtp_config()) if not json.loads(get_smtp_configured())['smtp_configured']: log.error('Attempting to send SMTP message but SMTP not configured.') return msg = MIMEText(message, 'html') msg['Subject'] = subject msg['From'] = current_smtp['smtp_sender'] try: server = smtplib.SMTP(current_smtp['smtp_server'], current_smtp['smtp_port'], timeout=10) except Exception as exc: log.error('Failed to start SMTP server: {}'.format(exc)) raise exc # Secure the connection server.starttls() # Send ehlo server.ehlo() server.set_debuglevel(False) # Send message server.sendmail(current_smtp['smtp_sender'], recipient, msg.as_string()) server.quit()
def add_hosts(): '''Add Hosts Page''' form = AddHostsForm() if request.method == 'GET': return render_template('addHosts.html', form=form) elif request.method == 'POST': if form.validate_on_submit(): pool = ThreadPool(config['Max_Threads']) threads = [] for ip_address in form.ip_address.data.split('\n'): ip_address = ip_address.strip() validator = IPAddress(ipv4=True) if not validator.check_ipv4(ip_address): flash('{} Is not a valid IP address'.format(ip_address), 'danger') continue if Hosts.query.filter_by(ip_address=ip_address).first(): flash('IP Address {} already exists.'.format(ip_address), 'danger') continue threads.append( pool.apply_async(_add_hosts_threaded, (ip_address, ))) pool.close() pool.join() for thread in threads: new_host = thread.get() try: # add the new host to the database db.session.add(new_host) flash( u'Successfully added {} ({})'.format( new_host.ip_address, new_host.hostname), 'success') except Exception as exc: flash(u'Failed to add {}'.format(new_host.ip_address), 'danger') log.error( 'Failed to add {} to database. Exception: {}'.format( new_host, exc)) continue db.session.commit() else: for dummy, errors in form.errors.items(): for error in errors: flash(error, 'danger') return redirect(url_for('hosts.add_hosts'))
def _host_status_alerts_threaded(): with app.app_context(): alerts_enabled = json.loads(get_alerts_enabled())['alerts_enabled'] smtp_configured = json.loads(get_smtp_configured())['smtp_configured'] alerts = HostAlerts.query.filter_by(alert_cleared=False).all() for alert in alerts: alert.alert_cleared = True if smtp_configured and alerts_enabled: pool = ThreadPool(config['Max_Threads']) threads = [] message = '' for alert in alerts: threads.append( pool.apply_async(_get_alert_status_message, (alert,)) ) pool.close() pool.join() for thread in threads: message += thread.get() if message: recipients = ';'.join(x['email'] for x in Schemas.users(many=True).dump(Users.query.filter_by(alerts_enabled=True))) try: send_smtp_message( recipient=recipients, subject='IPMON - Host Status Change Alert', message=message ) except Exception as exc: log.error('Failed to send host status change alert email: {}'.format(exc)) db.session.commit()