Пример #1
0
    def create(self, user_token, log_file):
        basicConfig(filename=log_file,
                    level=DEBUG,
                    format='%(asctime)s %(levelname)-8s %(message)s',
                    datefmt='%Y-%m-%d %H:%M:%S')

        lf = open(log_file, 'a+')

        time_now = strftime('%Y-%m-%dT%H:%M:%S-00', gmtime(int(time())))

        data = {
            "name": self.name,
            "description": self.description + time_now,
            "point_of_contact": self.point_of_contact,
            "response_play": self.response_play,
            self.relationship: self.linked_services
        }
        try:
            lf.write(
                "\n[%s] [%s]: ===============Creating Business Services=================="
                % (datetime.now(), 'Info'))
            lf.write("\n[%s] [%s]: Payload before POST: %s" %
                     (datetime.now(), 'Info', data))

            session = APISession(user_token)
            provision_biz_service = session.rpost(
                "/business_services", json={'business_service': data})
            info("===== The provisioned business service was: =====")

            # print(provision_biz_service.json())
            self.payload = provision_biz_service
            self.id = self.payload["id"]

            lf.write("\n[%s] [%s]: Metrics value: %s" %
                     (datetime.now(), 'Info', self.payload))
            # print("BIZ PAYLOAD:", self.payload)
            lf.write(
                "\n[%s] [%s]: =================== BUSINESS SERVICE CREATED ===================="
                % (datetime.now(), 'Info'))
        except PDClientError as e:
            lf.write("\n[%s] [%s]: %s" % (datetime.now(), 'Error', e.response))
            lf.write("\n[%s] [%s]: %s" %
                     (datetime.now(), 'Error', e.response.url))
            lf.write("\n[%s] [%s]: %s" %
                     (datetime.now(), 'Error', e.response.text))
            lf.write("\n[%s] [%s]: %s" %
                     (datetime.now(), 'Error', print_exc()))

        lf.close()
Пример #2
0
def main():

    api_token = os.getenv('API_TOKEN')
    assignee_user_id = os.getenv('ASSIGNEE_USER_ID')
    cf_build_id = os.getenv('CF_BUILD_ID')
    cf_build_url = os.getenv('CF_BUILD_URL')
    event_source = os.getenv('EVENT_SOURCE')
    event_summary = os.getenv('EVENT_SUMMARY')
    from_email = os.getenv('FROM_EMAIL')
    service_id = os.getenv('SERVICE_ID')
    title = os.getenv('TITLE')
    pagerduty_type = os.getenv('PAGERDUTY_ALERT_TYPE')

    if pagerduty_type == 'incident':
        session = APISession(api_token, default_from=from_email)

        payload = {
            'type': 'incident',
            'title': '{}'.format(title),
            'service': {
                'id': '{}'.format(service_id), 
                'type': 'service_reference'
            },
            'assignments': [
                {
                    'assignee': {
                        'id': '{}'.format(assignee_user_id), 
                        'type': 'user_reference'
                    }
                }
            ],
        }
        
        pd_incident = session.rpost('incidents', json=payload)
    
    elif pagerduty_type == 'change_event':

        session = ChangeEventsAPISession(api_token)

        pd_change_event = session.submit(
            summary='{}'.format(event_summary), 
            source='{}'.format(event_source), 
            custom_details={"Build ID":'{}'.format(cf_build_id)},
            links=[{'href':'{}'.format(cf_build_url)}]
        )
Пример #3
0
    def create(self, api_token, log_file):
        """ (this, str, str) -> NoneType
        Given API token key and path to the log file Create a PagerDuty Service associated with the API token

        """

        # Logfile for writing service API logs
        lf = open(log_file, 'a+')

        data = {"name": self.name, "description": self.description, "status": self.status,
                "escalation_policy": {"id": self.escalation_policy, "type": "escalation_policy_reference"},
                "alert_creation": self.alert_creation, "alert_grouping": self.alert_grouping}

        if self.time_grouping:
            data["alert_grouping_timeout"] = self.time_grouping

            lf.write("\n[%s] [%s] ===== Provisioning Service =====" % (datetime.now(), 'Info'))
            lf.write("\n[%s] [%s] Payload before provisioning: %s" % (datetime.now(), 'Info', data))
        try:
            session = APISession(api_token)
            provision_service = session.rpost("/services", json={'service': data})

            lf.write("\n[%s] [%s] Provisioned Service: %s" % (datetime.now(), 'Info:', provision_service['name']))

            self.provisioned_name = provision_service['name']
            self.id = provision_service['id']
            self.html_url = provision_service['html_url']
            self.payload = provision_service

            lf.write("\n[%s] [%s] =================== SERVICE CREATED ====================" % (datetime.now(), 'Info'))

        except PDClientError as e:
            lf.write("\n[%s] [%s]: %s" % (datetime.now(), 'Error', e.response))
            lf.write("\n[%s] [%s]: %s" % (datetime.now(), 'Error', e.response.url))
            lf.write("\n[%s] [%s]: %s" % (datetime.now(), 'Error', e.response.text))
            lf.write("\n[%s] [%s]: %s" % (datetime.now(), 'Error', print_exc()))

        lf.close()