<s> import json <EOL> import eventlet <EOL> eventlet . monkey_patch ( ) <EOL> from eventlet . green import urllib2 <EOL> from flask import request , abort , render_template , session , redirect <EOL> from bson import ObjectId <EOL> from time import time <EOL> from random import randint <EOL> from stalkerweb . auth import is_valid_login , login_required , remove_user <EOL> from stalkerweb . stutils import jsonify , genPrimaryKey64 <EOL> from stalkerweb import app , rc , rdb <EOL> from flask . ext . wtf import Form <EOL> from wtforms import TextField , PasswordField , BooleanField <EOL> from wtforms . validators import Required <EOL> from werkzeug . contrib . cache import RedisCache <EOL> import rethinkdb as r <EOL> from rethinkdb . errors import RqlDriverError , RqlRuntimeError <EOL> import logging <EOL> VALID_STATES = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> cache = RedisCache ( host = app . config [ '<STR_LIT>' ] , port = app . config [ '<STR_LIT>' ] , default_timeout = app . config [ '<STR_LIT>' ] ) <EOL> logger = logging . getLogger ( app . config [ '<STR_LIT>' ] ) <EOL> class SignInForm ( Form ) : <EOL> username = TextField ( validators = [ Required ( ) ] ) <EOL> password = PasswordField ( validators = [ Required ( ) ] ) <EOL> remember_me = BooleanField ( ) <EOL> def _get_local_metrics ( ) : <EOL> metrics = { } <EOL> mkeys = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> try : <EOL> values = rc . mget ( mkeys ) <EOL> for k in mkeys : <EOL> metrics [ k ] = int ( values [ mkeys . index ( k ) ] ) <EOL> return metrics <EOL> except Exception as err : <EOL> logger . exception ( '<STR_LIT>' ) <EOL> return None <EOL> def _get_remote_checks ( clusterid , state ) : <EOL> endpoints = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> target = app . config [ '<STR_LIT>' ] [ clusterid ] [ '<STR_LIT:host>' ] + endpoints [ state ] <EOL> headers = { '<STR_LIT>' : app . config [ '<STR_LIT>' ] [ clusterid ] [ '<STR_LIT:key>' ] } <EOL> try : <EOL> req = urllib2 . Request ( target , headers = headers ) <EOL> res = urllib2 . urlopen ( req , timeout = app . config [ '<STR_LIT>' ] ) <EOL> return json . loads ( res . read ( ) ) <EOL> except Exception as err : <EOL> logger . exception ( "<STR_LIT>" % ( clusterid , err ) ) <EOL> return None <EOL> def _get_remote_stats ( clusterid ) : <EOL> target = app . config [ '<STR_LIT>' ] [ clusterid ] [ '<STR_LIT:host>' ] + '<STR_LIT>' <EOL> headers = { '<STR_LIT>' : app . config [ '<STR_LIT>' ] [ clusterid ] [ '<STR_LIT:key>' ] } <EOL> try : <EOL> req = urllib2 . Request ( target , headers = headers ) <EOL> res = urllib2 . urlopen ( req , timeout = app . config [ '<STR_LIT>' ] ) <EOL> return json . loads ( res . read ( ) ) [ clusterid ] <EOL> except Exception as err : <EOL> logger . exception ( "<STR_LIT>" % ( clusterid , err ) ) <EOL> return None <EOL> def _get_users_theme ( username ) : <EOL> q = list ( r . table ( "<STR_LIT>" ) . filter ( { "<STR_LIT:username>" : username } ) . pluck ( { "<STR_LIT>" : True } ) . run ( rdb . conn ) ) [ <NUM_LIT:0> ] <EOL> return q . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def _rand_start ( ) : <EOL> """<STR_LIT>""" <EOL> return time ( ) + randint ( <NUM_LIT:1> , <NUM_LIT> ) <EOL> def _valid_registration ( content ) : <EOL> fields = [ ( '<STR_LIT>' , basestring ) , ( '<STR_LIT>' , dict ) , ( '<STR_LIT>' , list ) ] <EOL> for field in fields : <EOL> if field [ <NUM_LIT:0> ] in content : <EOL> if not isinstance ( content [ field [ <NUM_LIT:0> ] ] , field [ <NUM_LIT:1> ] ) : <EOL> return False <EOL> else : <EOL> return False <EOL> for check in content [ '<STR_LIT>' ] : <EOL> if not isinstance ( content [ '<STR_LIT>' ] [ check ] , dict ) : <EOL> return False <EOL> if '<STR_LIT>' not in content [ '<STR_LIT>' ] [ check ] : <EOL> return False <EOL> if not isinstance ( content [ '<STR_LIT>' ] [ check ] [ '<STR_LIT>' ] , int ) : <EOL> return False <EOL> if not isinstance ( content [ '<STR_LIT>' ] [ check ] [ '<STR_LIT>' ] , int ) : <EOL> return False <EOL> if '<STR_LIT:args>' not in content [ '<STR_LIT>' ] [ check ] : <EOL> return False <EOL> if '<STR_LIT>' in content [ '<STR_LIT>' ] [ check ] : <EOL> if not isinstance ( content [ '<STR_LIT>' ] [ check ] [ '<STR_LIT>' ] , int ) : <EOL> return False <EOL> if not isinstance ( content [ '<STR_LIT>' ] [ check ] [ '<STR_LIT:args>' ] , basestring ) : <EOL> return False <EOL> return True <EOL> @ app . route ( "<STR_LIT>" , methods = [ '<STR_LIT:POST>' , '<STR_LIT>' ] ) <EOL> def register ( ) : <EOL> if request . headers . get ( '<STR_LIT>' ) != app . config [ '<STR_LIT>' ] : <EOL> abort ( <NUM_LIT> ) <EOL> if not request . json : <EOL> abort ( <NUM_LIT> ) <EOL> if not _valid_registration ( request . json ) : <EOL> abort ( <NUM_LIT> ) <EOL> hid = request . json [ '<STR_LIT>' ] <EOL> checks = request . json [ '<STR_LIT>' ] <EOL> roles = request . json [ '<STR_LIT>' ] <EOL> ip_addr = request . json . get ( '<STR_LIT>' , request . remote_addr ) <EOL> if ip_addr == '<STR_LIT>' : <EOL> ip_addr = request . remote_addr <EOL> try : <EOL> pkey = genPrimaryKey64 ( "<STR_LIT>" % ( hid , ip_addr ) ) <EOL> q = r . table ( "<STR_LIT>" ) . insert ( { <EOL> "<STR_LIT:id>" : pkey , <EOL> "<STR_LIT>" : hid , <EOL> "<STR_LIT>" : ip_addr , <EOL> "<STR_LIT>" : checks , <EOL> "<STR_LIT>" : roles <EOL> } , conflict = "<STR_LIT>" ) . run ( rdb . conn ) <EOL> q = r . table ( "<STR_LIT>" ) . filter ( { "<STR_LIT>" : hid } ) . delete ( ) . run ( rdb . conn ) <EOL> bulk_load = [ ] <EOL> for i in checks : <EOL> bulk_load . append ( { '<STR_LIT:id>' : genPrimaryKey64 ( "<STR_LIT>" % ( hid , ip_addr , i ) ) , <EOL> '<STR_LIT>' : hid , '<STR_LIT>' : ip_addr , <EOL> '<STR_LIT>' : i , '<STR_LIT>' : <NUM_LIT:0> , '<STR_LIT>' : _rand_start ( ) , <EOL> '<STR_LIT>' : checks [ i ] [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : checks [ i ] [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT:status>' : None , '<STR_LIT>' : False , <EOL> '<STR_LIT>' : False , '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : checks [ i ] . get ( '<STR_LIT>' , <NUM_LIT:1> ) } ) <EOL> q = r . table ( "<STR_LIT>" ) . insert ( bulk_load , conflict = "<STR_LIT>" ) . run ( rdb . conn ) <EOL> except Exception as err : <EOL> logger . error ( err ) <EOL> return jsonify ( { '<STR_LIT:status>' : '<STR_LIT>' , '<STR_LIT:error>' : str ( err ) } ) , <NUM_LIT> <EOL> return jsonify ( { '<STR_LIT:status>' : '<STR_LIT>' } ) <EOL> @ app . route ( "<STR_LIT>" , defaults = { '<STR_LIT:username>' : None } ) <EOL> @ app . route ( "<STR_LIT>" , methods = [ '<STR_LIT:GET>' , '<STR_LIT:POST>' , '<STR_LIT>' ] ) <EOL> @ login_required <EOL> def users ( username ) : <EOL> if not username : <EOL> if request . method == '<STR_LIT>' : <EOL> abort ( <NUM_LIT> ) <EOL> if session . get ( '<STR_LIT:username>' , False ) : <EOL> username = session . get ( '<STR_LIT:username>' ) <EOL> else : <EOL> abort ( <NUM_LIT> ) <EOL> if request . method == '<STR_LIT:GET>' : <EOL> q = list ( r . table ( "<STR_LIT>" ) . filter ( { "<STR_LIT:username>" : username } ) . without ( "<STR_LIT>" ) . run ( rdb . conn ) ) [ <NUM_LIT:0> ] <EOL> if q : <EOL> if '<STR_LIT>' not in q : <EOL> q [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> return jsonify ( q ) <EOL> else : <EOL> abort ( <NUM_LIT> ) <EOL> elif request . method == '<STR_LIT>' : <EOL> return jsonify ( { '<STR_LIT:success>' : remove_user ( username ) } ) <EOL> elif request . method == '<STR_LIT:POST>' : <EOL> fields = { } <EOL> if not request . json : <EOL> abort ( <NUM_LIT> ) <EOL> if '<STR_LIT>' in request . json : <EOL> if request . json [ '<STR_LIT>' ] in app . config [ '<STR_LIT>' ] : <EOL> fields [ '<STR_LIT>' ] = request . json [ '<STR_LIT>' ] <EOL> else : <EOL> abort ( <NUM_LIT> ) <EOL> if '<STR_LIT:email>' in request . json : <EOL> fields [ '<STR_LIT:email>' ] = request . json [ '<STR_LIT:email>' ] <EOL> q = r . table ( "<STR_LIT>" ) . filter ( { "<STR_LIT:username>" : username } ) . update ( fields ) . run ( rdb . conn ) <EOL> if q [ "<STR_LIT>" ] != <NUM_LIT:0> : <EOL> if session and '<STR_LIT>' in fields : <EOL> session [ '<STR_LIT>' ] = fields [ '<STR_LIT>' ] <EOL> return jsonify ( { '<STR_LIT:success>' : True } ) <EOL> else : <EOL> return jsonify ( { '<STR_LIT:success>' : False } ) <EOL> @ app . route ( "<STR_LIT>" , defaults = { '<STR_LIT:host>' : None } ) <EOL> @ app . route ( "<STR_LIT>" , methods = [ '<STR_LIT:GET>' , '<STR_LIT>' ] ) <EOL> @ login_required <EOL> def hosts ( host ) : <EOL> """<STR_LIT>""" <EOL> if not host : <EOL> q = list ( r . table ( "<STR_LIT>" ) . without ( "<STR_LIT:id>" ) . run ( rdb . conn ) ) <EOL> if q : <EOL> q = { '<STR_LIT>' : q } <EOL> else : <EOL> if request . method == '<STR_LIT>' : <EOL> q = r . table ( "<STR_LIT>" ) . filter ( ( r . row [ "<STR_LIT>" ] == host ) | ( r . row [ "<STR_LIT>" ] == host ) ) . delete ( ) . run ( rdb . conn ) <EOL> q = r . table ( "<STR_LIT>" ) . filter ( ( r . row [ "<STR_LIT>" ] == host ) | ( r . row [ "<STR_LIT>" ] == host ) ) . delete ( ) . run ( rdb . conn ) <EOL> return jsonify ( { '<STR_LIT:success>' : True } ) <EOL> else : <EOL> q = list ( r . table ( "<STR_LIT>" ) . filter ( ( r . row [ "<STR_LIT>" ] == host ) | ( r . row [ "<STR_LIT>" ] == host ) ) . without ( "<STR_LIT:id>" ) . run ( rdb . conn ) ) <EOL> if q : <EOL> return jsonify ( q [ <NUM_LIT:0> ] ) <EOL> else : <EOL> abort ( <NUM_LIT> ) <EOL> @ app . route ( "<STR_LIT>" , defaults = { '<STR_LIT:host>' : None } ) <EOL> @ app . route ( "<STR_LIT>" ) <EOL> @ login_required <EOL> def checks ( host ) : <EOL> """<STR_LIT>""" <EOL> if not host : <EOL> q = list ( r . table ( "<STR_LIT>" ) . run ( rdb . conn ) ) <EOL> else : <EOL> q = list ( r . table ( "<STR_LIT>" ) . filter ( ( r . row [ "<STR_LIT>" ] == host ) | ( r . row [ "<STR_LIT>" ] == host ) ) . run ( rdb . conn ) ) <EOL> if q : <EOL> for check in q : <EOL> check [ '<STR_LIT:id>' ] = str ( check [ '<STR_LIT:id>' ] ) <EOL> return jsonify ( { '<STR_LIT>' : q } ) <EOL> else : <EOL> abort ( <NUM_LIT> ) <EOL> @ app . route ( '<STR_LIT>' , methods = [ '<STR_LIT:GET>' , '<STR_LIT>' ] ) <EOL> @ login_required <EOL> def checks_by_id ( checkid ) : <EOL> """<STR_LIT>""" <EOL> if request . method == '<STR_LIT:GET>' : <EOL> check = r . table ( "<STR_LIT>" ) . get ( checkid ) . run ( rdb . conn ) <EOL> if check : <EOL> check [ '<STR_LIT:id>' ] = str ( check [ '<STR_LIT:id>' ] ) <EOL> return jsonify ( { '<STR_LIT>' : check } ) <EOL> else : <EOL> abort ( <NUM_LIT> ) <EOL> elif request . method == '<STR_LIT>' : <EOL> q = r . table ( "<STR_LIT>" ) . get ( checkid ) . delete ( ) . run ( rdb . conn ) <EOL> if q [ "<STR_LIT>" ] == <NUM_LIT:1> : <EOL> return jsonify ( { '<STR_LIT:success>' : True } ) <EOL> else : <EOL> return jsonify ( { '<STR_LIT:success>' : False } ) <EOL> @ app . route ( '<STR_LIT>' , methods = [ '<STR_LIT:GET>' , '<STR_LIT:POST>' , '<STR_LIT>' ] ) <EOL> @ login_required <EOL> def check_owner ( checkid ) : <EOL> """<STR_LIT>""" <EOL> if request . method == '<STR_LIT:GET>' : <EOL> check = r . table ( "<STR_LIT>" ) . get ( checkid ) . run ( rdb . conn ) <EOL> if check : <EOL> check [ '<STR_LIT:id>' ] = str ( check [ '<STR_LIT:id>' ] ) <EOL> return jsonify ( { '<STR_LIT>' : check } ) <EOL> else : <EOL> abort ( <NUM_LIT> ) <EOL> elif request . method == '<STR_LIT:POST>' : <EOL> if not request . json : <EOL> abort ( <NUM_LIT> ) <EOL> try : <EOL> if request . json . get ( '<STR_LIT>' ) : <EOL> q = r . table ( "<STR_LIT>" ) . get ( checkid ) . update ( { "<STR_LIT>" : str ( request . json [ "<STR_LIT>" ] ) } ) . run ( rdb . conn ) <EOL> else : <EOL> abort ( <NUM_LIT> ) <EOL> if q [ "<STR_LIT>" ] != <NUM_LIT:0> : <EOL> return jsonify ( { '<STR_LIT:success>' : True } ) <EOL> else : <EOL> abort ( <NUM_LIT> ) <EOL> except Exception as err : <EOL> logger . error ( err ) <EOL> abort ( <NUM_LIT> ) <EOL> elif request . method == '<STR_LIT>' : <EOL> try : <EOL> q = r . table ( "<STR_LIT>" ) . get ( checkid ) . update ( { "<STR_LIT>" : "<STR_LIT>" } ) . run ( rdb . conn ) <EOL> if q [ "<STR_LIT>" ] != <NUM_LIT:0> : <EOL> return jsonify ( { '<STR_LIT:success>' : True } ) <EOL> else : <EOL> abort ( <NUM_LIT> ) <EOL> except Exception as err : <EOL> logger . error ( err ) <EOL> abort ( <NUM_LIT> ) <EOL> @ app . route ( '<STR_LIT>' , methods = [ '<STR_LIT:GET>' , '<STR_LIT:POST>' ] ) <EOL> @ login_required <EOL> def check_next ( checkid ) : <EOL> """<STR_LIT>""" <EOL> if request . method == '<STR_LIT:GET>' : <EOL> check = r . table ( "<STR_LIT>" ) . get ( checkid ) . run ( rdb . conn ) <EOL> if check : <EOL> check [ '<STR_LIT:id>' ] = str ( check [ '<STR_LIT:id>' ] ) <EOL> return jsonify ( { '<STR_LIT>' : check } ) <EOL> else : <EOL> abort ( <NUM_LIT> ) <EOL> elif request . method == '<STR_LIT:POST>' : <EOL> if not request . json : <EOL> abort ( <NUM_LIT> ) <EOL> try : <EOL> if not request . json . get ( '<STR_LIT>' ) : <EOL> abort ( <NUM_LIT> ) <EOL> if request . json . get ( '<STR_LIT>' ) == '<STR_LIT>' : <EOL> q = r . table ( "<STR_LIT>" ) . get ( checkid ) . update ( { "<STR_LIT>" : time ( ) - <NUM_LIT:1> } ) . run ( rdb . conn ) <EOL> else : <EOL> q = r . table ( "<STR_LIT>" ) . get ( checkid ) . update ( { "<STR_LIT>" : int ( request . json [ "<STR_LIT>" ] ) } ) . run ( rdb . conn ) <EOL> if q [ "<STR_LIT>" ] != <NUM_LIT:0> : <EOL> return jsonify ( { '<STR_LIT:success>' : True } ) <EOL> else : <EOL> abort ( <NUM_LIT> ) <EOL> except Exception as err : <EOL> logger . error ( err ) <EOL> abort ( <NUM_LIT> ) <EOL> @ app . route ( '<STR_LIT>' , methods = [ '<STR_LIT:GET>' , '<STR_LIT:POST>' ] ) <EOL> @ login_required <EOL> def check_suspended ( checkid ) : <EOL> """<STR_LIT>""" <EOL> if request . method == '<STR_LIT:GET>' : <EOL> check = r . table ( "<STR_LIT>" ) . get ( checkid ) . run ( rdb . conn ) <EOL> if check : <EOL> check [ '<STR_LIT:id>' ] = str ( check [ '<STR_LIT:id>' ] ) <EOL> return jsonify ( { '<STR_LIT>' : check } ) <EOL> else : <EOL> abort ( <NUM_LIT> ) <EOL> elif request . method == '<STR_LIT:POST>' : <EOL> if not request . json : <EOL> abort ( <NUM_LIT> ) <EOL> try : <EOL> if not request . json . get ( '<STR_LIT>' ) : <EOL> abort ( <NUM_LIT> ) <EOL> if request . json . get ( '<STR_LIT>' ) is True : <EOL> q = r . table ( "<STR_LIT>" ) . get ( checkid ) . update ( { "<STR_LIT>" : True } ) . run ( rdb . conn ) <EOL> elif request . json . get ( '<STR_LIT>' ) is False : <EOL> q = r . table ( "<STR_LIT>" ) . get ( checkid ) . update ( { "<STR_LIT>" : False } ) . run ( rdb . conn ) <EOL> else : <EOL> abort ( <NUM_LIT> ) <EOL> if q [ '<STR_LIT>' ] != <NUM_LIT:0> : <EOL> return jsonify ( { '<STR_LIT:success>' : True } ) <EOL> else : <EOL> abort ( <NUM_LIT> ) <EOL> except Exception as err : <EOL> logger . error ( err ) <EOL> abort ( <NUM_LIT> ) <EOL> @ app . route ( '<STR_LIT>' ) <EOL> @ login_required <EOL> def check_state ( state ) : <EOL> """<STR_LIT>""" <EOL> if state == '<STR_LIT>' : <EOL> q = list ( r . table ( "<STR_LIT>" ) . get_all ( False , index = "<STR_LIT:status>" ) . run ( rdb . conn ) ) <EOL> if q : <EOL> return jsonify ( { '<STR_LIT>' : q } ) <EOL> else : <EOL> return jsonify ( { '<STR_LIT>' : [ ] } ) <EOL> elif state == '<STR_LIT>' : <EOL> q = list ( r . table ( "<STR_LIT>" ) . get_all ( True , index = "<STR_LIT>" ) . run ( rdb . conn ) ) <EOL> if q : <EOL> return jsonify ( { '<STR_LIT>' : q } ) <EOL> else : <EOL> return jsonify ( { '<STR_LIT>' : [ ] } ) <EOL> elif state == '<STR_LIT>' : <EOL> q = list ( r . table ( "<STR_LIT>" ) . get_all ( True , index = "<STR_LIT>" ) . run ( rdb . conn ) ) <EOL> if q : <EOL> return jsonify ( { '<STR_LIT>' : q } ) <EOL> else : <EOL> return jsonify ( { '<STR_LIT>' : [ ] } ) <EOL> elif state == '<STR_LIT>' : <EOL> q = list ( r . table ( "<STR_LIT>" ) . get_all ( True , index = "<STR_LIT>" ) . run ( rdb . conn ) ) <EOL> if q : <EOL> return jsonify ( { '<STR_LIT>' : q } ) <EOL> else : <EOL> return jsonify ( { '<STR_LIT>' : [ ] } ) <EOL> else : <EOL> abort ( <NUM_LIT> ) <EOL> @ app . route ( '<STR_LIT>' , methods = [ '<STR_LIT:GET>' ] ) <EOL> @ login_required <EOL> def state_log_by_check ( hostname , checkname ) : <EOL> """<STR_LIT>""" <EOL> if request . method == '<STR_LIT:GET>' : <EOL> try : <EOL> limit = request . args . get ( '<STR_LIT>' , <NUM_LIT:10> , type = int ) <EOL> except ValueError : <EOL> abort ( <NUM_LIT> ) <EOL> log = list ( r . table ( "<STR_LIT>" ) . filter ( { "<STR_LIT>" : hostname , "<STR_LIT>" : checkname } ) . order_by ( r . desc ( "<STR_LIT>" ) ) . limit ( limit ) . run ( rdb . conn ) ) <EOL> if log : <EOL> return jsonify ( { '<STR_LIT>' : sorted ( log , key = lambda k : k [ '<STR_LIT>' ] ) } ) <EOL> else : <EOL> abort ( <NUM_LIT> ) <EOL> else : <EOL> abort ( <NUM_LIT> ) <EOL> @ app . route ( '<STR_LIT>' , methods = [ '<STR_LIT:GET>' , '<STR_LIT:POST>' ] ) <EOL> @ login_required <EOL> def list_notes ( hostname ) : <EOL> """<STR_LIT>""" <EOL> if request . method == '<STR_LIT:GET>' : <EOL> try : <EOL> limit = request . args . get ( '<STR_LIT>' , <NUM_LIT:50> , type = int ) <EOL> except ValueError : <EOL> abort ( <NUM_LIT> ) <EOL> notes = list ( r . table ( "<STR_LIT>" ) . filter ( { "<STR_LIT>" : hostname } ) . order_by ( r . desc ( "<STR_LIT>" ) ) . limit ( limit ) . run ( rdb . conn ) ) <EOL> if notes : <EOL> return jsonify ( { '<STR_LIT>' : sorted ( notes , key = lambda k : k [ '<STR_LIT>' ] ) } ) <EOL> else : <EOL> abort ( <NUM_LIT> ) <EOL> elif request . method == '<STR_LIT:POST>' : <EOL> if not request . json : <EOL> abort ( <NUM_LIT> ) <EOL> if not request . json . get ( "<STR_LIT:user>" ) or not request . json . get ( "<STR_LIT>" ) : <EOL> abort ( <NUM_LIT> ) <EOL> if not r . table ( "<STR_LIT>" ) . get_all ( hostname , index = "<STR_LIT>" ) . run ( rdb . conn ) : <EOL> abort ( <NUM_LIT> ) <EOL> alerting = [ x [ "<STR_LIT>" ] for x in r . table ( "<STR_LIT>" ) . filter ( { "<STR_LIT>" : hostname , "<STR_LIT:status>" : False } ) . run ( rdb . conn ) ] <EOL> q = r . table ( "<STR_LIT>" ) . insert ( { '<STR_LIT>' : hostname , '<STR_LIT:user>' : request . json . get ( "<STR_LIT:user>" ) , <EOL> '<STR_LIT>' : request . json . get ( "<STR_LIT>" ) , '<STR_LIT>' : time ( ) , '<STR_LIT>' : alerting } ) . run ( rdb . conn ) <EOL> if q [ "<STR_LIT>" ] == <NUM_LIT:1> : <EOL> return jsonify ( { '<STR_LIT:success>' : True } ) <EOL> else : <EOL> logger . error ( q ) <EOL> abort ( <NUM_LIT> ) <EOL> else : <EOL> abort ( <NUM_LIT> ) <EOL> @ app . route ( '<STR_LIT>' ) <EOL> @ login_required <EOL> def global_clusters ( ) : <EOL> """<STR_LIT>""" <EOL> return jsonify ( { '<STR_LIT>' : app . config [ '<STR_LIT>' ] } ) <EOL> @ app . route ( '<STR_LIT>' ) <EOL> @ login_required <EOL> def global_check_state ( clusterid , state ) : <EOL> """<STR_LIT>""" <EOL> if clusterid not in app . config [ '<STR_LIT>' ] : <EOL> abort ( <NUM_LIT> ) <EOL> if state in VALID_STATES : <EOL> ckey = '<STR_LIT>' % ( clusterid , state ) <EOL> cached = cache . get ( ckey ) <EOL> if cached : <EOL> return jsonify ( { clusterid : cached } ) <EOL> else : <EOL> q = _get_remote_checks ( clusterid , state ) <EOL> if q : <EOL> cache . set ( ckey , q ) <EOL> return jsonify ( { clusterid : q } ) <EOL> else : <EOL> abort ( <NUM_LIT> ) <EOL> else : <EOL> abort ( <NUM_LIT> ) <EOL> @ app . route ( '<STR_LIT>' , defaults = { '<STR_LIT>' : None } ) <EOL> @ app . route ( '<STR_LIT>' ) <EOL> @ login_required <EOL> def stalker_stats ( clusterid ) : <EOL> """<STR_LIT>""" <EOL> default = { '<STR_LIT>' : None , '<STR_LIT>' : None , '<STR_LIT>' : None , <EOL> '<STR_LIT>' : None , '<STR_LIT>' : None , '<STR_LIT>' : None } <EOL> if not clusterid : <EOL> q = _get_local_metrics ( ) <EOL> if q : <EOL> return jsonify ( { app . config [ '<STR_LIT>' ] : q } ) <EOL> else : <EOL> return jsonify ( { app . config [ '<STR_LIT>' ] : default } ) <EOL> else : <EOL> if clusterid in app . config [ '<STR_LIT>' ] : <EOL> ckey = '<STR_LIT>' % ( clusterid , '<STR_LIT>' ) <EOL> cached = cache . get ( ckey ) <EOL> if cached : <EOL> return jsonify ( { clusterid : cached } ) <EOL> else : <EOL> q = _get_remote_stats ( clusterid ) <EOL> if q : <EOL> cache . set ( ckey , q ) <EOL> return jsonify ( { clusterid : q } ) <EOL> else : <EOL> return jsonify ( { clusterid : default } ) <EOL> elif clusterid == '<STR_LIT:all>' : <EOL> q = { } <EOL> q [ app . config [ '<STR_LIT>' ] ] = _get_local_metrics ( ) <EOL> for cid in app . config [ '<STR_LIT>' ] . keys ( ) : <EOL> ckey = '<STR_LIT>' % ( cid , '<STR_LIT>' ) <EOL> cached = cache . get ( ckey ) <EOL> if cached : <EOL> q [ cid ] = cached <EOL> else : <EOL> q [ cid ] = _get_remote_stats ( cid ) <EOL> if not q [ cid ] : <EOL> q [ cid ] = default <EOL> cache . set ( ckey , q [ cid ] ) <EOL> return jsonify ( { '<STR_LIT:all>' : q } ) <EOL> else : <EOL> abort ( <NUM_LIT> ) <EOL> @ app . route ( '<STR_LIT>' ) <EOL> @ login_required <EOL> def findhost ( ) : <EOL> """<STR_LIT>""" <EOL> if not request . args . get ( '<STR_LIT:q>' ) : <EOL> abort ( <NUM_LIT> ) <EOL> result = [ ] <EOL> for i in r . table ( "<STR_LIT>" ) . filter ( <EOL> r . row [ "<STR_LIT>" ] . match ( "<STR_LIT>" % request . args . get ( '<STR_LIT:q>' ) ) | r . row [ "<STR_LIT>" ] . match ( "<STR_LIT>" % request . args . get ( '<STR_LIT:q>' ) ) <EOL> ) . pluck ( { "<STR_LIT>" : True , "<STR_LIT>" : True } ) . run ( rdb . conn ) : <EOL> if i [ '<STR_LIT>' ] . startswith ( request . args . get ( '<STR_LIT:q>' ) ) : <EOL> result . append ( i [ '<STR_LIT>' ] ) <EOL> else : <EOL> result . append ( i [ '<STR_LIT>' ] ) <EOL> return "<STR_LIT:U+002C>" . join ( result ) <EOL> @ app . route ( '<STR_LIT:/>' ) <EOL> @ login_required <EOL> def index ( ) : <EOL> return render_template ( '<STR_LIT>' , state = '<STR_LIT>' ) <EOL> @ app . route ( '<STR_LIT>' , defaults = { '<STR_LIT:state>' : None } ) <EOL> @ app . route ( '<STR_LIT>' ) <EOL> @ login_required <EOL> def view_states ( state ) : <EOL> if state : <EOL> if state in VALID_STATES : <EOL> return render_template ( '<STR_LIT>' , state = state ) <EOL> else : <EOL> abort ( <NUM_LIT> ) <EOL> else : <EOL> return render_template ( '<STR_LIT>' , state = '<STR_LIT>' ) <EOL> @ app . route ( '<STR_LIT>' ) <EOL> @ login_required <EOL> def view_checks ( ) : <EOL> return render_template ( '<STR_LIT>' ) <EOL> @ app . route ( '<STR_LIT>' , defaults = { '<STR_LIT>' : None } ) <EOL> @ app . route ( '<STR_LIT>' ) <EOL> @ login_required <EOL> def view_single_host ( hostname ) : <EOL> if request . args . get ( '<STR_LIT>' ) : <EOL> hostname = request . args . get ( '<STR_LIT>' ) <EOL> if not hostname : <EOL> abort ( <NUM_LIT> ) <EOL> else : <EOL> return render_template ( '<STR_LIT>' , target = hostname ) <EOL> @ app . route ( '<STR_LIT>' ) <EOL> @ login_required <EOL> def view_user ( username ) : <EOL> return render_template ( '<STR_LIT>' ) <EOL> @ app . route ( '<STR_LIT>' , defaults = { '<STR_LIT:state>' : None } ) <EOL> @ app . route ( '<STR_LIT>' ) <EOL> @ login_required <EOL> def view_global ( state ) : <EOL> """<STR_LIT>""" <EOL> if state : <EOL> if state in VALID_STATES : <EOL> return render_template ( '<STR_LIT>' , state = state ) <EOL> else : <EOL> abort ( <NUM_LIT> ) <EOL> else : <EOL> return render_template ( '<STR_LIT>' , state = '<STR_LIT>' ) <EOL> @ app . route ( '<STR_LIT>' ) <EOL> def signout ( ) : <EOL> """<STR_LIT>""" <EOL> session . pop ( '<STR_LIT>' , None ) <EOL> return render_template ( '<STR_LIT>' ) <EOL> @ app . route ( '<STR_LIT>' , methods = [ "<STR_LIT:GET>" , "<STR_LIT:POST>" ] ) <EOL> def signin ( ) : <EOL> """<STR_LIT>""" <EOL> form = SignInForm ( ) <EOL> if form . validate_on_submit ( ) : <EOL> username = form . username . data . strip ( ) <EOL> password = form . password . data . strip ( ) <EOL> if is_valid_login ( username , password ) : <EOL> session [ '<STR_LIT>' ] = _get_users_theme ( username ) <EOL> session [ '<STR_LIT>' ] = True <EOL> session [ '<STR_LIT:username>' ] = username <EOL> if form . remember_me . data : <EOL> session . permanent = True <EOL> else : <EOL> session . permanent = False <EOL> if request . args . get ( '<STR_LIT>' ) == '<STR_LIT>' : <EOL> return redirect ( "<STR_LIT:/>" ) <EOL> else : <EOL> return redirect ( request . args . get ( '<STR_LIT>' ) or request . referrer or "<STR_LIT:/>" ) <EOL> else : <EOL> return render_template ( '<STR_LIT>' , form = form , error = "<STR_LIT>" ) <EOL> return render_template ( '<STR_LIT>' , form = form , error = None ) <EOL> @ app . errorhandler ( <NUM_LIT> ) <EOL> def page_not_found ( e ) : <EOL> return render_template ( '<STR_LIT>' ) , <NUM_LIT> <EOL> @ app . route ( '<STR_LIT>' , methods = [ '<STR_LIT:GET>' ] ) <EOL> @ login_required <EOL> def help ( ) : <EOL> """<STR_LIT>""" <EOL> func_list = { } <EOL> for rule in app . url_map . iter_rules ( ) : <EOL> if rule . endpoint != '<STR_LIT>' : <EOL> func_list [ rule . rule ] = app . view_functions [ rule . endpoint ] . __doc__ <EOL> return jsonify ( func_list ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> debug = True <EOL> app . run ( host = '<STR_LIT>' , debug = debug ) </s>
