<s> from django . db import models <EOL> from django . conf import settings <EOL> from django . core . exceptions import ValidationError <EOL> from polymorphic import PolymorphicModel <EOL> from django . db . models import F <EOL> from django . core . urlresolvers import reverse <EOL> from django . contrib . auth . models import User <EOL> from celery . exceptions import SoftTimeLimitExceeded <EOL> from . jenkins import get_job_status <EOL> from . alert import ( <EOL> send_alert , <EOL> send_alert_update , <EOL> AlertPlugin , <EOL> AlertPluginUserData , <EOL> update_alert_plugins <EOL> ) <EOL> from . calendar import get_events <EOL> from . graphite import parse_metric <EOL> from . graphite import get_data <EOL> from . tasks import update_service , update_instance <EOL> from datetime import datetime , timedelta <EOL> from django . utils import timezone <EOL> import json <EOL> import re <EOL> import time <EOL> import os <EOL> import subprocess <EOL> import itertools <EOL> import requests <EOL> from celery . utils . log import get_task_logger <EOL> RAW_DATA_LIMIT = <NUM_LIT> <EOL> logger = get_task_logger ( __name__ ) <EOL> CHECK_TYPES = ( <EOL> ( '<STR_LIT:>>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT:<>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ) <EOL> def serialize_recent_results ( recent_results ) : <EOL> if not recent_results : <EOL> return '<STR_LIT>' <EOL> def result_to_value ( result ) : <EOL> if result . succeeded : <EOL> return '<STR_LIT:1>' <EOL> else : <EOL> return '<STR_LIT>' <EOL> vals = [ result_to_value ( r ) for r in recent_results ] <EOL> vals . reverse ( ) <EOL> return '<STR_LIT:U+002C>' . join ( vals ) <EOL> def calculate_debounced_passing ( recent_results , debounce = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> if not recent_results : <EOL> return True <EOL> debounce_window = recent_results [ : debounce + <NUM_LIT:1> ] <EOL> for r in debounce_window : <EOL> if r . succeeded : <EOL> return True <EOL> return False <EOL> class CheckGroupMixin ( models . Model ) : <EOL> class Meta : <EOL> abstract = True <EOL> PASSING_STATUS = '<STR_LIT>' <EOL> WARNING_STATUS = '<STR_LIT>' <EOL> ERROR_STATUS = '<STR_LIT>' <EOL> CRITICAL_STATUS = '<STR_LIT>' <EOL> CALCULATED_PASSING_STATUS = '<STR_LIT>' <EOL> CALCULATED_INTERMITTENT_STATUS = '<STR_LIT>' <EOL> CALCULATED_FAILING_STATUS = '<STR_LIT>' <EOL> STATUSES = ( <EOL> ( CALCULATED_PASSING_STATUS , CALCULATED_PASSING_STATUS ) , <EOL> ( CALCULATED_INTERMITTENT_STATUS , CALCULATED_INTERMITTENT_STATUS ) , <EOL> ( CALCULATED_FAILING_STATUS , CALCULATED_FAILING_STATUS ) , <EOL> ) <EOL> IMPORTANCES = ( <EOL> ( WARNING_STATUS , '<STR_LIT>' ) , <EOL> ( ERROR_STATUS , '<STR_LIT>' ) , <EOL> ( CRITICAL_STATUS , '<STR_LIT>' ) , <EOL> ) <EOL> name = models . TextField ( ) <EOL> users_to_notify = models . ManyToManyField ( <EOL> User , <EOL> blank = True , <EOL> help_text = '<STR_LIT>' , <EOL> ) <EOL> alerts_enabled = models . BooleanField ( <EOL> default = True , <EOL> help_text = '<STR_LIT>' , <EOL> ) <EOL> status_checks = models . ManyToManyField ( <EOL> '<STR_LIT>' , <EOL> blank = True , <EOL> help_text = '<STR_LIT>' , <EOL> ) <EOL> last_alert_sent = models . DateTimeField ( <EOL> null = True , <EOL> blank = True , <EOL> ) <EOL> alerts = models . ManyToManyField ( <EOL> '<STR_LIT>' , <EOL> blank = True , <EOL> help_text = '<STR_LIT>' <EOL> ) <EOL> email_alert = models . BooleanField ( default = False ) <EOL> hipchat_alert = models . BooleanField ( default = True ) <EOL> sms_alert = models . BooleanField ( default = False ) <EOL> telephone_alert = models . BooleanField ( <EOL> default = False , <EOL> help_text = '<STR_LIT>' , <EOL> ) <EOL> overall_status = models . TextField ( default = PASSING_STATUS ) <EOL> old_overall_status = models . TextField ( default = PASSING_STATUS ) <EOL> hackpad_id = models . TextField ( <EOL> null = True , <EOL> blank = True , <EOL> verbose_name = '<STR_LIT>' , <EOL> help_text = '<STR_LIT>' <EOL> ) <EOL> def __unicode__ ( self ) : <EOL> return self . name <EOL> def most_severe ( self , check_list ) : <EOL> failures = [ c . importance for c in check_list ] <EOL> if self . CRITICAL_STATUS in failures : <EOL> return self . CRITICAL_STATUS <EOL> if self . ERROR_STATUS in failures : <EOL> return self . ERROR_STATUS <EOL> if self . WARNING_STATUS in failures : <EOL> return self . WARNING_STATUS <EOL> return self . PASSING_STATUS <EOL> @ property <EOL> def is_critical ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . old_overall_status != self . CRITICAL_STATUS and self . overall_status == self . CRITICAL_STATUS : <EOL> return True <EOL> return False <EOL> def alert ( self ) : <EOL> if not self . alerts_enabled : <EOL> return <EOL> if self . overall_status != self . PASSING_STATUS : <EOL> if self . overall_status == self . WARNING_STATUS : <EOL> if self . last_alert_sent and ( timezone . now ( ) - timedelta ( minutes = settings . NOTIFICATION_INTERVAL ) ) < self . last_alert_sent : <EOL> return <EOL> elif self . overall_status in ( self . CRITICAL_STATUS , self . ERROR_STATUS ) : <EOL> if self . last_alert_sent and ( timezone . now ( ) - timedelta ( minutes = settings . ALERT_INTERVAL ) ) < self . last_alert_sent : <EOL> return <EOL> self . last_alert_sent = timezone . now ( ) <EOL> else : <EOL> self . last_alert_sent = None <EOL> self . save ( ) <EOL> if self . unexpired_acknowledgement ( ) : <EOL> send_alert_update ( self , duty_officers = get_duty_officers ( ) ) <EOL> else : <EOL> self . snapshot . did_send_alert = True <EOL> self . snapshot . save ( ) <EOL> send_alert ( self , duty_officers = get_duty_officers ( ) ) <EOL> def unexpired_acknowledgements ( self ) : <EOL> acknowledgements = self . alertacknowledgement_set . all ( ) . filter ( <EOL> time__gte = timezone . now ( ) - timedelta ( minutes = settings . ACKNOWLEDGEMENT_EXPIRY ) , <EOL> cancelled_time__isnull = True , <EOL> ) . order_by ( '<STR_LIT>' ) <EOL> return acknowledgements <EOL> def acknowledge_alert ( self , user ) : <EOL> if self . unexpired_acknowledgements ( ) : <EOL> return None <EOL> acknowledgement = AlertAcknowledgement . objects . create ( <EOL> user = user , <EOL> time = timezone . now ( ) , <EOL> service = self , <EOL> ) <EOL> def remove_acknowledgement ( self , user ) : <EOL> self . unexpired_acknowledgements ( ) . update ( <EOL> cancelled_time = timezone . now ( ) , <EOL> cancelled_user = user , <EOL> ) <EOL> def unexpired_acknowledgement ( self ) : <EOL> try : <EOL> return self . unexpired_acknowledgements ( ) [ <NUM_LIT:0> ] <EOL> except : <EOL> return None <EOL> @ property <EOL> def recent_snapshots ( self ) : <EOL> snapshots = self . snapshots . filter ( <EOL> time__gt = ( timezone . now ( ) - timedelta ( minutes = <NUM_LIT> * <NUM_LIT> ) ) ) <EOL> snapshots = list ( snapshots . values ( ) ) <EOL> for s in snapshots : <EOL> s [ '<STR_LIT:time>' ] = time . mktime ( s [ '<STR_LIT:time>' ] . timetuple ( ) ) <EOL> return snapshots <EOL> def graphite_status_checks ( self ) : <EOL> return self . status_checks . filter ( polymorphic_ctype__model = '<STR_LIT>' ) <EOL> def http_status_checks ( self ) : <EOL> return self . status_checks . filter ( polymorphic_ctype__model = '<STR_LIT>' ) <EOL> def jenkins_status_checks ( self ) : <EOL> return self . status_checks . filter ( polymorphic_ctype__model = '<STR_LIT>' ) <EOL> def active_graphite_status_checks ( self ) : <EOL> return self . graphite_status_checks ( ) . filter ( active = True ) <EOL> def active_http_status_checks ( self ) : <EOL> return self . http_status_checks ( ) . filter ( active = True ) <EOL> def active_jenkins_status_checks ( self ) : <EOL> return self . jenkins_status_checks ( ) . filter ( active = True ) <EOL> def active_status_checks ( self ) : <EOL> return self . status_checks . filter ( active = True ) <EOL> def inactive_status_checks ( self ) : <EOL> return self . status_checks . filter ( active = False ) <EOL> def all_passing_checks ( self ) : <EOL> return self . active_status_checks ( ) . filter ( calculated_status = self . CALCULATED_PASSING_STATUS ) <EOL> def all_failing_checks ( self ) : <EOL> return self . active_status_checks ( ) . exclude ( calculated_status = self . CALCULATED_PASSING_STATUS ) <EOL> class Service ( CheckGroupMixin ) : <EOL> def update_status ( self ) : <EOL> self . old_overall_status = self . overall_status <EOL> status_checks_failed_count = self . all_failing_checks ( ) . count ( ) <EOL> self . overall_status = self . most_severe ( self . all_failing_checks ( ) ) <EOL> self . snapshot = ServiceStatusSnapshot ( <EOL> service = self , <EOL> num_checks_active = self . active_status_checks ( ) . count ( ) , <EOL> num_checks_passing = self . active_status_checks ( <EOL> ) . count ( ) - status_checks_failed_count , <EOL> num_checks_failing = status_checks_failed_count , <EOL> overall_status = self . overall_status , <EOL> time = timezone . now ( ) , <EOL> ) <EOL> self . snapshot . save ( ) <EOL> self . save ( ) <EOL> if not ( self . overall_status == Service . PASSING_STATUS and self . old_overall_status == Service . PASSING_STATUS ) : <EOL> self . alert ( ) <EOL> instances = models . ManyToManyField ( <EOL> '<STR_LIT>' , <EOL> blank = True , <EOL> help_text = '<STR_LIT>' , <EOL> ) <EOL> url = models . TextField ( <EOL> blank = True , <EOL> help_text = "<STR_LIT>" <EOL> ) <EOL> class Meta : <EOL> ordering = [ '<STR_LIT:name>' ] <EOL> class Instance ( CheckGroupMixin ) : <EOL> def duplicate ( self ) : <EOL> checks = self . status_checks . all ( ) <EOL> new_instance = self <EOL> new_instance . pk = None <EOL> new_instance . id = None <EOL> new_instance . name = u"<STR_LIT>" % self . name <EOL> new_instance . save ( ) <EOL> for check in checks : <EOL> check . duplicate ( inst_set = ( new_instance , ) , serv_set = ( ) ) <EOL> return new_instance . pk <EOL> def update_status ( self ) : <EOL> self . old_overall_status = self . overall_status <EOL> status_checks_failed_count = self . all_failing_checks ( ) . count ( ) <EOL> self . overall_status = self . most_severe ( self . all_failing_checks ( ) ) <EOL> self . snapshot = InstanceStatusSnapshot ( <EOL> instance = self , <EOL> num_checks_active = self . active_status_checks ( ) . count ( ) , <EOL> num_checks_passing = self . active_status_checks ( <EOL> ) . count ( ) - status_checks_failed_count , <EOL> num_checks_failing = status_checks_failed_count , <EOL> overall_status = self . overall_status , <EOL> time = timezone . now ( ) , <EOL> ) <EOL> self . snapshot . save ( ) <EOL> self . save ( ) <EOL> class Meta : <EOL> ordering = [ '<STR_LIT:name>' ] <EOL> address = models . TextField ( <EOL> blank = True , <EOL> help_text = "<STR_LIT>" <EOL> ) <EOL> def icmp_status_checks ( self ) : <EOL> return self . status_checks . filter ( polymorphic_ctype__model = '<STR_LIT>' ) <EOL> def active_icmp_status_checks ( self ) : <EOL> return self . icmp_status_checks ( ) . filter ( active = True ) <EOL> def delete ( self , * args , ** kwargs ) : <EOL> self . icmp_status_checks ( ) . delete ( ) <EOL> return super ( Instance , self ) . delete ( * args , ** kwargs ) <EOL> class Snapshot ( models . Model ) : <EOL> class Meta : <EOL> abstract = True <EOL> time = models . DateTimeField ( db_index = True ) <EOL> num_checks_active = models . IntegerField ( default = <NUM_LIT:0> ) <EOL> num_checks_passing = models . IntegerField ( default = <NUM_LIT:0> ) <EOL> num_checks_failing = models . IntegerField ( default = <NUM_LIT:0> ) <EOL> overall_status = models . TextField ( default = Service . PASSING_STATUS ) <EOL> did_send_alert = models . IntegerField ( default = False ) <EOL> class ServiceStatusSnapshot ( Snapshot ) : <EOL> service = models . ForeignKey ( Service , related_name = '<STR_LIT>' ) <EOL> def __unicode__ ( self ) : <EOL> return u"<STR_LIT>" % ( self . service . name , self . overall_status ) <EOL> class InstanceStatusSnapshot ( Snapshot ) : <EOL> instance = models . ForeignKey ( Instance , related_name = '<STR_LIT>' ) <EOL> def __unicode__ ( self ) : <EOL> return u"<STR_LIT>" % ( self . instance . name , self . overall_status ) <EOL> class StatusCheck ( PolymorphicModel ) : <EOL> """<STR_LIT>""" <EOL> name = models . TextField ( ) <EOL> active = models . BooleanField ( <EOL> default = True , <EOL> help_text = '<STR_LIT>' , <EOL> ) <EOL> importance = models . CharField ( <EOL> max_length = <NUM_LIT:30> , <EOL> choices = Service . IMPORTANCES , <EOL> default = Service . ERROR_STATUS , <EOL> help_text = '<STR_LIT>' <EOL> ) <EOL> frequency = models . IntegerField ( <EOL> default = <NUM_LIT:5> , <EOL> help_text = '<STR_LIT>' , <EOL> ) <EOL> debounce = models . IntegerField ( <EOL> default = <NUM_LIT:0> , <EOL> null = True , <EOL> help_text = '<STR_LIT>' <EOL> ) <EOL> created_by = models . ForeignKey ( User , null = True ) <EOL> calculated_status = models . CharField ( <EOL> max_length = <NUM_LIT:50> , choices = Service . STATUSES , default = Service . CALCULATED_PASSING_STATUS , blank = True ) <EOL> last_run = models . DateTimeField ( null = True ) <EOL> cached_health = models . TextField ( editable = False , null = True ) <EOL> metric = models . TextField ( <EOL> null = True , <EOL> help_text = '<STR_LIT>' , <EOL> ) <EOL> check_type = models . CharField ( <EOL> choices = CHECK_TYPES , <EOL> max_length = <NUM_LIT:100> , <EOL> null = True , <EOL> ) <EOL> value = models . TextField ( <EOL> null = True , <EOL> help_text = '<STR_LIT>' , <EOL> ) <EOL> expected_num_hosts = models . IntegerField ( <EOL> default = <NUM_LIT:0> , <EOL> null = True , <EOL> help_text = '<STR_LIT>' , <EOL> ) <EOL> allowed_num_failures = models . IntegerField ( <EOL> default = <NUM_LIT:0> , <EOL> null = True , <EOL> help_text = '<STR_LIT>' , <EOL> ) <EOL> endpoint = models . TextField ( <EOL> null = True , <EOL> help_text = '<STR_LIT>' , <EOL> ) <EOL> username = models . TextField ( <EOL> blank = True , <EOL> null = True , <EOL> help_text = '<STR_LIT>' , <EOL> ) <EOL> password = models . TextField ( <EOL> blank = True , <EOL> null = True , <EOL> help_text = '<STR_LIT>' , <EOL> ) <EOL> text_match = models . TextField ( <EOL> blank = True , <EOL> null = True , <EOL> help_text = '<STR_LIT>' , <EOL> ) <EOL> status_code = models . TextField ( <EOL> default = <NUM_LIT:200> , <EOL> null = True , <EOL> help_text = '<STR_LIT>' <EOL> ) <EOL> timeout = models . IntegerField ( <EOL> default = <NUM_LIT:30> , <EOL> null = True , <EOL> help_text = '<STR_LIT>' , <EOL> ) <EOL> verify_ssl_certificate = models . BooleanField ( <EOL> default = True , <EOL> help_text = '<STR_LIT>' , <EOL> ) <EOL> max_queued_build_time = models . IntegerField ( <EOL> null = True , <EOL> blank = True , <EOL> help_text = '<STR_LIT>' , <EOL> ) <EOL> class Meta ( PolymorphicModel . Meta ) : <EOL> ordering = [ '<STR_LIT:name>' ] <EOL> def __unicode__ ( self ) : <EOL> return self . name <EOL> def recent_results ( self ) : <EOL> return StatusCheckResult . objects . filter ( check = self ) . order_by ( '<STR_LIT>' ) . defer ( '<STR_LIT>' ) [ : <NUM_LIT:10> ] <EOL> def last_result ( self ) : <EOL> try : <EOL> return StatusCheckResult . objects . filter ( check = self ) . order_by ( '<STR_LIT>' ) . defer ( '<STR_LIT>' ) [ <NUM_LIT:0> ] <EOL> except : <EOL> return None <EOL> def run ( self ) : <EOL> start = timezone . now ( ) <EOL> try : <EOL> result = self . _run ( ) <EOL> except SoftTimeLimitExceeded as e : <EOL> result = StatusCheckResult ( check = self ) <EOL> result . error = u'<STR_LIT>' <EOL> result . succeeded = False <EOL> except Exception as e : <EOL> result = StatusCheckResult ( check = self ) <EOL> logger . error ( u"<STR_LIT>" % ( e . message , ) ) <EOL> result . error = u'<STR_LIT>' % ( e . message , ) <EOL> result . succeeded = False <EOL> finish = timezone . now ( ) <EOL> result . time = start <EOL> result . time_complete = finish <EOL> result . save ( ) <EOL> self . last_run = finish <EOL> self . save ( ) <EOL> def _run ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( '<STR_LIT>' ) <EOL> def save ( self , * args , ** kwargs ) : <EOL> if self . last_run : <EOL> recent_results = list ( self . recent_results ( ) ) <EOL> if calculate_debounced_passing ( recent_results , self . debounce ) : <EOL> self . calculated_status = Service . CALCULATED_PASSING_STATUS <EOL> else : <EOL> self . calculated_status = Service . CALCULATED_FAILING_STATUS <EOL> self . cached_health = serialize_recent_results ( recent_results ) <EOL> try : <EOL> updated = StatusCheck . objects . get ( pk = self . pk ) <EOL> except StatusCheck . DoesNotExist as e : <EOL> logger . error ( '<STR_LIT>' % self . pk ) <EOL> return <EOL> else : <EOL> self . cached_health = '<STR_LIT>' <EOL> self . calculated_status = Service . CALCULATED_PASSING_STATUS <EOL> ret = super ( StatusCheck , self ) . save ( * args , ** kwargs ) <EOL> self . update_related_services ( ) <EOL> self . update_related_instances ( ) <EOL> return ret <EOL> def duplicate ( self , inst_set = ( ) , serv_set = ( ) ) : <EOL> new_check = self <EOL> new_check . pk = None <EOL> new_check . id = None <EOL> new_check . last_run = None <EOL> new_check . save ( ) <EOL> for linked in list ( inst_set ) + list ( serv_set ) : <EOL> linked . status_checks . add ( new_check ) <EOL> return new_check . pk <EOL> def update_related_services ( self ) : <EOL> services = self . service_set . all ( ) <EOL> for service in services : <EOL> update_service . delay ( service . id ) <EOL> def update_related_instances ( self ) : <EOL> instances = self . instance_set . all ( ) <EOL> for instance in instances : <EOL> update_instance . delay ( instance . id ) <EOL> class ICMPStatusCheck ( StatusCheck ) : <EOL> class Meta ( StatusCheck . Meta ) : <EOL> proxy = True <EOL> @ property <EOL> def check_category ( self ) : <EOL> return "<STR_LIT>" <EOL> def _run ( self ) : <EOL> result = StatusCheckResult ( check = self ) <EOL> instances = self . instance_set . all ( ) <EOL> target = self . instance_set . get ( ) . address <EOL> ping_process = subprocess . Popen ( "<STR_LIT>" + target , stdout = subprocess . PIPE , stderr = subprocess . STDOUT , shell = True ) <EOL> response = ping_process . wait ( ) <EOL> if response == <NUM_LIT:0> : <EOL> result . succeeded = True <EOL> else : <EOL> output = ping_process . stdout . read ( ) <EOL> result . succeeded = False <EOL> result . error = output <EOL> return result <EOL> def minimize_targets ( targets ) : <EOL> split = [ target . split ( "<STR_LIT:.>" ) for target in targets ] <EOL> prefix_nodes_in_common = <NUM_LIT:0> <EOL> for i , nodes in enumerate ( itertools . izip ( * split ) ) : <EOL> if any ( node != nodes [ <NUM_LIT:0> ] for node in nodes ) : <EOL> prefix_nodes_in_common = i <EOL> break <EOL> split = [ nodes [ prefix_nodes_in_common : ] for nodes in split ] <EOL> suffix_nodes_in_common = <NUM_LIT:0> <EOL> for i , nodes in enumerate ( reversed ( zip ( * split ) ) ) : <EOL> if any ( node != nodes [ <NUM_LIT:0> ] for node in nodes ) : <EOL> suffix_nodes_in_common = i <EOL> break <EOL> if suffix_nodes_in_common : <EOL> split = [ nodes [ : - suffix_nodes_in_common ] for nodes in split ] <EOL> return [ "<STR_LIT:.>" . join ( nodes ) for nodes in split ] <EOL> class GraphiteStatusCheck ( StatusCheck ) : <EOL> class Meta ( StatusCheck . Meta ) : <EOL> proxy = True <EOL> @ property <EOL> def check_category ( self ) : <EOL> return "<STR_LIT>" <EOL> def format_error_message ( self , failures , actual_hosts , hosts_by_target ) : <EOL> if actual_hosts < self . expected_num_hosts : <EOL> return "<STR_LIT>" % ( <EOL> actual_hosts , self . expected_num_hosts ) <EOL> elif actual_hosts > <NUM_LIT:1> : <EOL> threshold = float ( self . value ) <EOL> failures_by_host = [ "<STR_LIT>" % ( <EOL> hosts_by_target [ target ] , value , self . check_type , threshold ) <EOL> for target , value in failures ] <EOL> return "<STR_LIT:U+002CU+0020>" . join ( failures_by_host ) <EOL> else : <EOL> target , value = failures [ <NUM_LIT:0> ] <EOL> return "<STR_LIT>" % ( value , self . check_type , float ( self . value ) ) <EOL> def _run ( self ) : <EOL> result = StatusCheckResult ( check = self ) <EOL> failures = [ ] <EOL> graphite_output = parse_metric ( self . metric , mins_to_check = self . frequency ) <EOL> try : <EOL> result . raw_data = json . dumps ( graphite_output [ '<STR_LIT>' ] ) <EOL> except : <EOL> result . raw_data = graphite_output [ '<STR_LIT>' ] <EOL> if graphite_output [ "<STR_LIT:error>" ] : <EOL> result . succeeded = False <EOL> result . error = graphite_output [ "<STR_LIT:error>" ] <EOL> return result <EOL> if graphite_output [ '<STR_LIT>' ] > <NUM_LIT:0> : <EOL> result . average_value = graphite_output [ '<STR_LIT>' ] <EOL> for s in graphite_output [ '<STR_LIT>' ] : <EOL> if not s [ "<STR_LIT>" ] : <EOL> continue <EOL> failure_value = None <EOL> if self . check_type == '<STR_LIT:<>' : <EOL> if float ( s [ '<STR_LIT>' ] ) < float ( self . value ) : <EOL> failure_value = s [ '<STR_LIT>' ] <EOL> elif self . check_type == '<STR_LIT>' : <EOL> if float ( s [ '<STR_LIT>' ] ) <= float ( self . value ) : <EOL> failure_value = s [ '<STR_LIT>' ] <EOL> elif self . check_type == '<STR_LIT:>>' : <EOL> if float ( s [ '<STR_LIT>' ] ) > float ( self . value ) : <EOL> failure_value = s [ '<STR_LIT>' ] <EOL> elif self . check_type == '<STR_LIT>' : <EOL> if float ( s [ '<STR_LIT>' ] ) >= float ( self . value ) : <EOL> failure_value = s [ '<STR_LIT>' ] <EOL> elif self . check_type == '<STR_LIT>' : <EOL> if float ( self . value ) in s [ '<STR_LIT>' ] : <EOL> failure_value = float ( self . value ) <EOL> else : <EOL> raise Exception ( u'<STR_LIT>' % <EOL> self . check_type ) <EOL> if not failure_value is None : <EOL> failures . append ( ( s [ "<STR_LIT:target>" ] , failure_value ) ) <EOL> if len ( failures ) > self . allowed_num_failures : <EOL> result . succeeded = False <EOL> elif graphite_output [ '<STR_LIT>' ] < self . expected_num_hosts : <EOL> result . succeeded = False <EOL> else : <EOL> result . succeeded = True <EOL> if not result . succeeded : <EOL> targets = [ s [ "<STR_LIT:target>" ] for s in graphite_output [ "<STR_LIT>" ] ] <EOL> hosts = minimize_targets ( targets ) <EOL> hosts_by_target = dict ( zip ( targets , hosts ) ) <EOL> result . error = self . format_error_message ( <EOL> failures , <EOL> graphite_output [ '<STR_LIT>' ] , <EOL> hosts_by_target , <EOL> ) <EOL> return result <EOL> class HttpStatusCheck ( StatusCheck ) : <EOL> class Meta ( StatusCheck . Meta ) : <EOL> proxy = True <EOL> @ property <EOL> def check_category ( self ) : <EOL> return "<STR_LIT>" <EOL> def _run ( self ) : <EOL> result = StatusCheckResult ( check = self ) <EOL> auth = None <EOL> if self . username or self . password : <EOL> auth = ( self . username , self . password ) <EOL> try : <EOL> resp = requests . get ( <EOL> self . endpoint , <EOL> timeout = self . timeout , <EOL> verify = self . verify_ssl_certificate , <EOL> auth = auth , <EOL> headers = { <EOL> "<STR_LIT>" : settings . HTTP_USER_AGENT , <EOL> } , <EOL> ) <EOL> except requests . RequestException as e : <EOL> result . error = u'<STR_LIT>' % ( e . message , ) <EOL> result . succeeded = False <EOL> else : <EOL> if self . status_code and resp . status_code != int ( self . status_code ) : <EOL> result . error = u'<STR_LIT>' % ( <EOL> resp . status_code , int ( self . status_code ) ) <EOL> result . succeeded = False <EOL> result . raw_data = resp . content <EOL> elif self . text_match : <EOL> if not re . search ( self . text_match , resp . content ) : <EOL> result . error = u'<STR_LIT>' % self . text_match <EOL> result . raw_data = resp . content <EOL> result . succeeded = False <EOL> else : <EOL> result . succeeded = True <EOL> else : <EOL> result . succeeded = True <EOL> return result <EOL> class JenkinsStatusCheck ( StatusCheck ) : <EOL> class Meta ( StatusCheck . Meta ) : <EOL> proxy = True <EOL> @ property <EOL> def check_category ( self ) : <EOL> return "<STR_LIT>" <EOL> @ property <EOL> def failing_short_status ( self ) : <EOL> return '<STR_LIT>' <EOL> def _run ( self ) : <EOL> result = StatusCheckResult ( check = self ) <EOL> try : <EOL> status = get_job_status ( self . name ) <EOL> active = status [ '<STR_LIT>' ] <EOL> result . job_number = status [ '<STR_LIT>' ] <EOL> if status [ '<STR_LIT>' ] == <NUM_LIT> : <EOL> result . error = u'<STR_LIT>' % self . name <EOL> result . succeeded = False <EOL> return result <EOL> elif status [ '<STR_LIT>' ] > <NUM_LIT> : <EOL> raise Exception ( u'<STR_LIT>' % status [ '<STR_LIT>' ] ) <EOL> except Exception as e : <EOL> result . error = u'<STR_LIT>' % e . message <EOL> result . succeeded = True <EOL> return result <EOL> if not active : <EOL> result . error = u'<STR_LIT>' % self . name <EOL> result . succeeded = False <EOL> else : <EOL> if self . max_queued_build_time and status [ '<STR_LIT>' ] : <EOL> if status [ '<STR_LIT>' ] > self . max_queued_build_time * <NUM_LIT> : <EOL> result . succeeded = False <EOL> result . error = u'<STR_LIT>' % ( <EOL> self . name , <EOL> int ( status [ '<STR_LIT>' ] ) , <EOL> self . max_queued_build_time , <EOL> ) <EOL> else : <EOL> result . succeeded = status [ '<STR_LIT>' ] <EOL> else : <EOL> result . succeeded = status [ '<STR_LIT>' ] <EOL> if not status [ '<STR_LIT>' ] : <EOL> if result . error : <EOL> result . error += u'<STR_LIT>' % self . name <EOL> else : <EOL> result . error = u'<STR_LIT>' % self . name <EOL> result . raw_data = status <EOL> return result <EOL> class StatusCheckResult ( models . Model ) : <EOL> """<STR_LIT>""" <EOL> check = models . ForeignKey ( StatusCheck ) <EOL> time = models . DateTimeField ( null = False , db_index = True ) <EOL> time_complete = models . DateTimeField ( null = True , db_index = True ) <EOL> raw_data = models . TextField ( null = True ) <EOL> succeeded = models . BooleanField ( default = False ) <EOL> error = models . TextField ( null = True ) <EOL> job_number = models . PositiveIntegerField ( null = True ) <EOL> class Meta : <EOL> ordering = [ '<STR_LIT>' ] <EOL> index_together = ( ( '<STR_LIT>' , '<STR_LIT>' ) , ) <EOL> def __unicode__ ( self ) : <EOL> return '<STR_LIT>' % ( self . status , self . check . name , self . time ) <EOL> @ property <EOL> def status ( self ) : <EOL> if self . succeeded : <EOL> return '<STR_LIT>' <EOL> else : <EOL> return '<STR_LIT>' <EOL> @ property <EOL> def took ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> diff = self . time_complete - self . time <EOL> return ( diff . microseconds + ( diff . seconds + diff . days * <NUM_LIT> * <NUM_LIT> ) * <NUM_LIT:10> ** <NUM_LIT:6> ) / <NUM_LIT:1000> <EOL> except : <EOL> return None <EOL> @ property <EOL> def short_error ( self ) : <EOL> snippet_len = <NUM_LIT:30> <EOL> if len ( self . error ) > snippet_len : <EOL> return u"<STR_LIT>" % self . error [ : snippet_len - <NUM_LIT:3> ] <EOL> else : <EOL> return self . error <EOL> def save ( self , * args , ** kwargs ) : <EOL> if isinstance ( self . raw_data , basestring ) : <EOL> self . raw_data = self . raw_data [ : RAW_DATA_LIMIT ] <EOL> return super ( StatusCheckResult , self ) . save ( * args , ** kwargs ) <EOL> class AlertAcknowledgement ( models . Model ) : <EOL> time = models . DateTimeField ( ) <EOL> user = models . ForeignKey ( User ) <EOL> service = models . ForeignKey ( Service ) <EOL> cancelled_time = models . DateTimeField ( null = True , blank = True ) <EOL> cancelled_user = models . ForeignKey ( <EOL> User , <EOL> null = True , <EOL> blank = True , <EOL> related_name = '<STR_LIT>' <EOL> ) <EOL> def unexpired ( self ) : <EOL> return self . expires ( ) > timezone . now ( ) <EOL> def expires ( self ) : <EOL> return self . time + timedelta ( minutes = settings . ACKNOWLEDGEMENT_EXPIRY ) <EOL> class UserProfile ( models . Model ) : <EOL> user = models . OneToOneField ( User , related_name = '<STR_LIT>' ) <EOL> def user_data ( self ) : <EOL> for user_data_subclass in AlertPluginUserData . __subclasses__ ( ) : <EOL> user_data = user_data_subclass . objects . get_or_create ( user = self , title = user_data_subclass . name ) <EOL> return AlertPluginUserData . objects . filter ( user = self ) <EOL> def __unicode__ ( self ) : <EOL> return '<STR_LIT>' % self . user . username <EOL> def save ( self , * args , ** kwargs ) : <EOL> if self . fallback_alert_user : <EOL> profiles = UserProfile . objects . exclude ( id = self . id ) <EOL> profiles . update ( fallback_alert_user = False ) <EOL> return super ( UserProfile , self ) . save ( * args , ** kwargs ) <EOL> @ property <EOL> def prefixed_mobile_number ( self ) : <EOL> return '<STR_LIT>' % self . mobile_number <EOL> mobile_number = models . CharField ( max_length = <NUM_LIT:20> , blank = True , default = '<STR_LIT>' ) <EOL> hipchat_alias = models . CharField ( max_length = <NUM_LIT:50> , blank = True , default = '<STR_LIT>' ) <EOL> fallback_alert_user = models . BooleanField ( default = False ) <EOL> class Shift ( models . Model ) : <EOL> start = models . DateTimeField ( ) <EOL> end = models . DateTimeField ( ) <EOL> user = models . ForeignKey ( User ) <EOL> uid = models . TextField ( ) <EOL> deleted = models . BooleanField ( default = False ) <EOL> def __unicode__ ( self ) : <EOL> deleted = '<STR_LIT>' <EOL> if self . deleted : <EOL> deleted = '<STR_LIT>' <EOL> return "<STR_LIT>" % ( self . user . username , self . start , self . end , deleted ) <EOL> def get_duty_officers ( at_time = None ) : <EOL> """<STR_LIT>""" <EOL> duty_officers = [ ] <EOL> if not at_time : <EOL> at_time = timezone . now ( ) <EOL> current_shifts = Shift . objects . filter ( <EOL> deleted = False , <EOL> start__lt = at_time , <EOL> end__gt = at_time , <EOL> ) <EOL> if current_shifts : <EOL> duty_officers = [ shift . user for shift in current_shifts ] <EOL> return duty_officers <EOL> else : <EOL> try : <EOL> u = UserProfile . objects . get ( fallback_alert_user = True ) <EOL> return [ u . user ] <EOL> except UserProfile . DoesNotExist : <EOL> return [ ] <EOL> def update_shifts ( ) : <EOL> events = get_events ( ) <EOL> users = User . objects . filter ( is_active = True ) <EOL> user_lookup = { } <EOL> for u in users : <EOL> user_lookup [ u . username . lower ( ) ] = u <EOL> future_shifts = Shift . objects . filter ( start__gt = timezone . now ( ) ) <EOL> future_shifts . update ( deleted = True ) <EOL> for event in events : <EOL> e = event [ '<STR_LIT>' ] . lower ( ) . strip ( ) <EOL> if e in user_lookup : <EOL> user = user_lookup [ e ] <EOL> try : <EOL> s = Shift . objects . get ( uid = event [ '<STR_LIT>' ] ) <EOL> except Shift . DoesNotExist : <EOL> s = Shift ( uid = event [ '<STR_LIT>' ] ) <EOL> s . start = event [ '<STR_LIT:start>' ] <EOL> s . end = event [ '<STR_LIT:end>' ] <EOL> s . user = user <EOL> s . deleted = False <EOL> s . save ( ) </s>
