예제 #1
0
 def generate(self):
     logger = util.setup_logger(INFO)
     env = self.env.lower() if self.env else 'production'
     conf = util.getstanza('getsnow', env)
     # Proxy not currently used in this version
     # proxy_conf = util.getstanza('getsnow', 'global')
     # proxies = util.setproxy(conf, proxy_conf)
     username = conf['user']
     password = conf['password']
     url = conf['url']
     value_replacements = conf['value_replacements']
     user_name = self.user_name.split(',')
     daysAgo = int(self.daysAgo) if self.daysAgo else 30
     snowuser = snow(url, username, password)
     snowuser.replacementsdict(value_replacements)
     user_info = snowuser.getsysid('sys_user', 'user_name', user_name, mapto='user_name')
     for record in user_info[1]:
         record = snowuser.updaterecord(record, sourcetype='snow:user')
         record['_raw'] = util.tojson(record)
         yield record
     exuded = snowuser.filterbuilder('assigned_to', user_info[0])
     url = snowuser.reqencode([exuded], table='alm_asset')
     for record in snowuser.getrecords(url):
         record = snowuser.updaterecord(record, sourcetype='snow:asset')
         record['_raw'] = util.tojson(record)
         yield record
     exuded = snowuser.filterbuilder('opened_by', user_info[0])
     url = snowuser.reqencode([exuded], days=daysAgo)
     for record in snowuser.getrecords(url):
         record = snowuser.updaterecord(record, sourcetype='snow:incident')
         record['_raw'] = util.tojson(record)
         yield record
예제 #2
0
 def generate(self):
     env = self.env.lower() if self.env else 'production'
     conf = util.getstanza('getsnow', env)
     #proxy_conf = util.getstanza('getsnow', 'global')
     #proxies = util.setproxy(conf, proxy_conf)
     username = conf['user']
     password = conf['password']
     url = conf['url']
     value_replacements = conf['value_replacements']
     daysAgo = self.daysAgo
     daysBy = self.daysAgo
     filters = self.filters
     table = self.table
     limit = self.limit
     bfilter = {}
     filters = filters.split(',') if filters else []
     exuded = []
     for x in filters:
         k, v = x.split('=')
         if k in bfilter:
             bfilter[k].append(v)
         else:
             bfilter[k] = []
             bfilter[k].append(v)
     snownow = snow(url, username, password)
     snownow.replacementsdict(value_replacements)
     for k, v in bfilter.iteritems():
         exuded.append(snownow.filterbuilder(k, v))
     url = snownow.reqencode(exuded, table=table, timeby=daysBy, days=daysAgo)
     for record in snownow.getrecords(url, limit):
         record = snownow.updaterecord(record, sourcetype='snow', lookup=True)
         record['_raw'] = util.tojson(record)
         yield record
예제 #3
0
 def getroles(self, info):
     num, data = access.roledict()
     if num == -1:
         return "-1"
     namelist = ['roleid', 'rolename']
     data = util.tojson(data, num, namelist)
     print(data)
     return data
예제 #4
0
 def getuser(self, info):
     num, data = access.userdict()
     if num == -1:
         return "-1"
     namelist = ['userid', 'account', 'date', 'email', 'roles']
     data = util.tojson(data, num, namelist)
     print(data)
     return data
예제 #5
0
 def getmineresult(self, info):
     info = json.loads(info)
     num, data = access.minedata(info["account"])
     if num == -1:
         return json.dumps({"wrong": "wrong"})
     datajson = util.tojson(
         data, num, ['result_id', 'deviceid', 'userid', 'starttime'])
     print(datajson)
     return datajson
예제 #6
0
 def getconditionresult(self, info):
     try:
         info = json.loads(info)
         print(info)
         num, data = access.conditiondata(info)
         if num == -1:
             raise Exception("something wrong")
         datajson = util.tojson(
             data, num,
             ['result_id', 'account', 'deviceid', 'userid', 'starttime'])
         print("successs")
         return datajson
     except:
         print("nnnnnnnn")
         return json.dumps({"wrong": "wrong"})
예제 #7
0
 def generate(self):
     env = self.env.lower() if self.env else 'production'
     conf = util.getstanza('getsnow', env)
     # Proxy not currently used in this version
     # proxy_conf = util.getstanza('getsnow', 'global')
     # proxies = util.setproxy(conf, proxy_conf)
     username = conf['user']
     password = conf['password']
     url = conf['url']
     value_replacements = conf['value_replacements']
     daysAgo = int(self.daysAgo) if self.daysAgo else 30
     snowuser = snow(url, username, password)
     snowuser.replacementsdict(value_replacements)
     url = snowuser.reqencode([],table='cmdb_ci_outage', days=daysAgo)
     for record in snowuser.getrecords(url):
         record = snowuser.updaterecord(record, sourcetype='snow:outage')
         record['_raw'] = util.tojson(record)
         yield record
예제 #8
0
 def generate(self):
     logger = util.setup_logger(INFO)
     env = self.env.lower() if self.env else 'production'
     conf = util.getstanza('getsnow', env)
     #proxy_conf = util.getstanza('getsnow', 'global')
     username = conf['user']
     password = conf['password']
     active = self.active
     user_name = self.user_name.split(',') if self.user_name else []
     assigment_group = self.assignment_group.split(',') if self.assignment_group else []
     daysAgo = int(self.daysAgo) if self.daysAgo else None
     limit = self.limit
     daysBy = self.daysBy if self.daysBy else 'opened_at'
     filterBy = self.filterBy if self.filterBy else 'assigned_to'
     url = conf['url']
     value_replacements = conf['value_replacements']
     if active:
         try:
             active = active.strip()
             active = active[0].upper() + active[1:].lower()
             active = ast.literal_eval(active)
         except:
             active = True
     if limit:
         try:
             limit = int(limit)
         except:
             limit = 10000
     snowincident = snow(url, username, password)
     snowincident.replacementsdict(value_replacements)
     user_info = snowincident.getsysid('sys_user', 'user_name', user_name, mapto='user_name')[0]
     group_info = snowincident.getsysid('sys_user_group', 'name', assigment_group, mapto='name')[0]
     exuded1 = snowincident.filterbuilder(filterBy, user_info)
     exuded2 = snowincident.filterbuilder('assignment_group', group_info)
     url = snowincident.reqencode([exuded1, exuded2], table='incident', active=active, timeby=daysBy, days=daysAgo)
     for record in snowincident.getrecords(url, limit=limit):
         record = snowincident.updaterecord(record, sourcetype='snow:incident')
         record['_raw'] = util.tojson(record)
         yield record
예제 #9
0
    def add_document(self, doc):
        self.load_termgenerator()
        config = self.config
        termgenerator = self.termgenerator

        xpdoc = xapian.Document()
        termgenerator.set_document(xpdoc)

        try:
            # Index fields with prefixes.
            for prefix, fields in self.field_prefix.items():
                for field in fields:
                    try:
                        termgenerator.index_text(doc[field], 1, prefix)
                    except KeyError:
                        continue

            # Index fields without prefixes for general search.
            for field in self.indexingField:
                try:
                    termgenerator.index_text(doc[field])
                    logger.debug("indexed: %s: %s", field, doc[field])
                except KeyError:
                    # If the key can't be found, just ignore it
                    continue
                termgenerator.increase_termpos()

            for i, (field, isnumber) in enumerate(self.sortingField.items()):
                if isnumber:
                    value = xapian.sortable_serialise(float(doc[field]))
                else:
                    f = doc[field]
                    if isinstance(f, unicode):
                        value = f.encode("utf-8")
                    else:
                        value = str(f)
                xpdoc.add_value(i, value)

            idfield = config.get("config", "id")
            ver = self.get_version()
            if self.storingField is None:
                data = doc
            else:
                data = {k: doc[k] for k in self.storingField}
                if ver >= 1.09:
                    data[idfield] = doc[idfield]
                else:
                    data["_id"] = doc[idfield]
            xpdoc.set_data(util.tojson(data))
            logger.debug("data is: %s", data)

            idterm = u"Q" + unicode(doc[idfield])
            if len(idterm) > 240:
                raise AWIPRequestInvalid(
                    'value for id field "%s" too long; please redesign your document structure' % idfield
                )
            xpdoc.add_boolean_term(idterm)
            self.db.replace_document(idterm, xpdoc)

        except KeyError, e:
            raise AWIPRequestInvalid('invalid document: missing key "%s"' % e.args[0])
예제 #10
0
    def generate(self):
        # Parse and set arguments
        logger = util.setup_logger(INFO)

        # get config
        env = self.env.lower() if self.env else 'production'
        conf = util.getstanza('getsnow', env)
        proxy_conf = util.getstanza('getsnow', 'global')
        proxies = util.setproxy(conf, proxy_conf)
        username = conf['user']
        password = conf['password']
        url = conf['url']

        user_query = '%s/api/now/table/%s?sysparm_query=user_name=%s' % (url, 'sys_user', self.user_name)

        response = util.request(user_query,
                                username=username,
                                password=password,
                                headers={'Accept': 'application/json'}
                                )

        if response['code'] == 200:
            records = json.loads(response['msg'])
            # for each event creating dic object for yield
            for record in records['result']:
                record['_time'] = time.mktime(datetime.datetime.strptime(record['sys_created_on'], "%Y-%m-%d %H:%M:%S").timetuple())
                record['url'] = url
                if record['manager']['link']:
                    response = util.request(record['manager']['link'],
                                            username=username,
                                            password=password,
                                            headers={'Accept': 'application/json'}
                                            )
                    manager = json.loads(response['msg'])['result']
                    record['manager'] = manager['name']
                    record['manager_email'] = manager['email']
                    record['manager_phone'] = manager['phone']
                if record['u_office']['link']:
                    response = util.request(record['u_office']['link'],
                                            username=username,
                                            password=password,
                                            headers={'Accept': 'application/json'}
                                            )
                    office = json.loads(response['msg'])['result']
                    record['office_number'] = office['u_office_number']
                if record['location']['link']:
                    response = util.request(record['location']['link'],
                                            username=username,
                                            password=password,
                                            headers={'Accept': 'application/json'}
                                            )
                    location = json.loads(response['msg'])['result']
                    record['office_name'] = location['full_name']
                if record['department']['link']:
                    response = util.request(record['department']['link'],
                                            username=username,
                                            password=password,
                                            headers={'Accept': 'application/json'}
                                            )
                    department = json.loads(response['msg'])['result']
                    record['department'] = department['name']

                # removing unnecessary keys
                record.pop('sys_domain', None)
                record.pop('u_office', None)
                record.pop('company', None)
                record.pop('u_organization_group', None)
                record.pop('u_title', None)
                record.pop('ldap_server', None)
                record.pop('cost_center', None)
                user_sysid = record['sys_id']
                record['sourcetype'] = 'snow:user'
                record['source'] = user_query
                # adding _raw to record
                record['_raw'] = util.tojson(record)
                record['_time'] = time.mktime(datetime.datetime.strptime(record['sys_created_on'], "%Y-%m-%d %H:%M:%S").timetuple())

                #yielding record
                yield record

                # building query string incidents
                time_range = 'opened_at>=javascript:gs.daysAgo(%s)^' % self.daysAgo if self.daysAgo else ''
                incident_query = '%s/api/now/table/%s?sysparm_query=%sopened_by=%s' % (url, 'incident', time_range, user_sysid)
                response = util.request(incident_query,
                                        username=username,
                                        password=password,
                                        headers={'Accept': 'application/json'}
                                        )

                if response['code'] == 200:
                    incidents = json.loads(response['msg'])['result']

                    # replacing all sys_id with user_names
                    for incident in incidents:
                        incident = keyreplace(incident, 'closed_by', 'user_name', username, password)
                        incident = keyreplace(incident, 'opened_by', 'user_name', username, password)
                        incident = keyreplace(incident, 'assigned_to', 'user_name', username, password)
                        incident = keyreplace(incident, 'resolved_by', 'user_name', username, password)
                        incident = keyreplace(incident, 'caller_id', 'user_name', username, password)
                        incident = keyreplace(incident, 'u_opened_for', 'user_name', username, password)
                        incident = keyreplace(incident, 'assignment_group', 'name', username, password)
                        incident['source'] = incident_query
                        incident['sourcetype'] = 'snow:incident'
                        incident['_time'] = time.mktime(datetime.datetime.strptime(incident['sys_created_on'], "%Y-%m-%d %H:%M:%S").timetuple())
                        incident['_raw'] = util.tojson(incident)

                        # removing unnecessary keys
                        incident.pop('company', None)
                        incident.pop('location', None)

                        # yield incident record
                        yield incident
                else:
                    try:
                    # If not 200 status_code showing error message in Splunk UI
                        record = util.dictexpand(response)
                        record['url'] = url
                        record['_raw'] = util.tojson(response)
                    except Exception as e:
                        record = dict()
                        record['url'] = url
                        record['error'] = e
                        record['_raw'] = util.tojson(response)
                        yield record

                asset_query = '%s/api/now/table/%s?sysparm_query=assigned_to=%s' % (url, 'alm_asset', user_sysid)
                response = util.request(asset_query,
                                        username=username,
                                        password=password,
                                        headers={'Accept': 'application/json'}
                                        )
                if response['code'] == 200:
                    assets = json.loads(response['msg'])['result']

                    for asset in assets:
                        asset.pop('support_group', None)
                        asset.pop('department', None)
                        asset.pop('model', None)
                        asset.pop('ci', None)
                        asset.pop('company', None)
                        asset.pop('location', None)
                        asset.pop('model_category', None)
                        asset.pop('cost_center', None)
                        asset.pop('sys_domain', None)
                        asset['source'] = asset_query
                        asset['_time'] = time.mktime(datetime.datetime.strptime(asset['sys_created_on'], "%Y-%m-%d %H:%M:%S").timetuple())
                        asset['sourcetype'] = 'snow:asset'
                        asset['_raw'] = util.tojson(asset)
                        yield asset

                else:
                    try:
                        # If not 200 status_code showing error message in Splunk UI
                        record = util.dictexpand(response)
                        record['url'] = url
                        record['_raw'] = util.tojson(response)
                    except Exception as e:
                        record = dict()
                        record['url'] = url
                        record['error'] = e
                        record['_raw'] = util.tojson(response)
                        yield record

        else:
            try:
                # If not 200 status_code showing error message in Splunk UI
                record = util.dictexpand(response)
                record['url'] = url
                record['_raw'] = util.tojson(response)
            except Exception as e:
                record = dict()
                record['url'] = url
                record['error'] = e
                record['_raw'] = util.tojson(response)
            yield record
    def generate(self):
        logger = util.setup_logger(INFO)
        try:
            default_conf = util.getstanza('hipchat', 'default')
            local_conf = util.getstanza('hipchat', 'hipchat')
            proxy_conf = util.setproxy(local_conf, default_conf)
            hipchat_url = local_conf[
                'url'] if 'url' in local_conf else default_conf['url']
            auth_token = local_conf[
                'authToken'] if 'authToken' in local_conf else default_conf[
                    'autToken']
            timeout = local_conf[
                'timeout'] if 'timeout' in local_conf else default_conf[
                    'timeout']
        finally:
            logger.info(
                'Unable to parse Config File. Check if hipchat.conf exists')
            raise Exception(
                "Unable to parse Config File. Check if hipchat.conf exists")
            exit()

        headers = dict(Authorization='Bearer {0}'.format(auth_token))
        data = dict(message=None, message_format='text')
        hipchat_room_url = '{0}/v2/room/{1}/notification'.format(
            hipchat_url, self.room)
        hipchat_room_list_url = '{0}/v2/room'.format(hipchat_url)

        if not self.listrooms:
            for record in records:
                message = None
                for key, value in record:
                    message = message.join('{0}={1} '.format(key, value))
                response = util.request(hipchat_room_url,
                                        data=data,
                                        headers=headers,
                                        timeout=timeout,
                                        proxy=proxy_conf)
                logger.info(
                    'sending notification room={0} status_code={1} response={2}'
                    .format(self.room, response['code'], response['msg']))
                record['status_code'] = response['code']
                record['response'] = response['msg']
                record['_raw'] = util.tojson(response)
                yield record
        else:
            while hipchat_room_list_url:
                response = util.request(hipchat_room_list_url,
                                        headers=headers,
                                        timeout=timeout,
                                        proxy=proxy_conf)
                if response['code'] == 200:
                    room_list = json.loads(response['msg'])
                    hipchat_room_list_url = room_list['links'][
                        'next'] if 'next' in room_list['links'] else None
                    for room in room_list['items']:
                        room_info = dict()
                        room_info['room_id'] = room['id']
                        room_info['room_name'] = room['id']
                        room_info['_raw'] = util.tojson(room_info)
                        yield room_info
                else:
                    yield response
        exit()