コード例 #1
0
class PollerStatus(Resource):

    api_utils = ApiUtils()
    db_utils = DatabaseUtils()

    snmp_poller_schema = SnmpPollerSchema

    def get(self, table_name=None):
        if table_name is None:
            return {'message': 'No table name', 'type': 'TableError'}, 422

        table_name = table_name.replace(' ', '')
        table_not_exists = self.db_utils.check_table_if_not_exist(table_name)

        if not table_not_exists:
            poller_tables = self.db_utils.model_session(SnmpPoller)
            tables = self.db_utils.filter_with_paginate(poller_tables,
                                                        SnmpPoller,
                                                        id=None,
                                                        args=None)
            schema_option = self.api_utils.schema_options(
                self.snmp_poller_schema, 'table_name')
            tables_result = SnmpPollerSchema(**schema_option).dump(tables)
            all_poller_tables = [val['table_name'] for val in tables_result]

            if table_name in all_poller_tables:
                args = self.api_utils.optional_parameters()
                result = data_view(args, table_name)

                all_dates = []
                for poller in result:
                    all_dates.append(str(poller['datetime']))
                    del poller['datetime']

                timestamp = sorted(
                    all_dates,
                    key=lambda d: datetime.strptime(d, '%Y-%m-%d %H:%M:%S.%f'),
                    reverse=True)[0]

                up = sum(poller['status'] == '1' for poller in result)
                down = sum(poller['status'] == '0' for poller in result)

                status = {'up': up, 'down': down, 'timestamp': timestamp}

                return status
            else:
                return {
                    'message': 'Invalid table name.',
                    'type': 'TableError'
                }, 422
        else:
            return {
                'message': 'Table does not exist.',
                'type': 'TableError'
            }, 422
コード例 #2
0
class BlacklistApi(Resource):

    api_utils = ApiUtils()
    db_utils = DatabaseUtils()

    def post(self):
        args = self.api_utils.parameters_without_model(poller_config='append')

        for poller_element in args['poller_config']:
            poller_config = eval(poller_element)

        return args
コード例 #3
0
class CategoryApi(Resource):

    api_utils = ApiUtils()
    db_utils = DatabaseUtils()

    main_model = Categories
    main_schema = CategoriesSchema

    def get(self, id=None):
        category = db.session.query(self.main_model)
        if id is not None:
            category = category.filter(self.main_model.id == id)
        category.all()
        category_result = self.main_schema(many=True).dump(category)
        return {'data': category_result}, 200
コード例 #4
0
class NetworkDiscovery(Resource):

    api_utils = ApiUtils()
    db_utils = DatabaseUtils()

    module_name = 'Network Discovery'

    def post(self):
        args = self.api_utils.parameters_without_model(config='append')
        if args["config"]:
            try:
                config = eval(args["config"][0])
                config = NetworkDiscoverySchema().load(config)
                scanner = NetworkDiscoveryScan(config['ip_address'],
                                               config['subnet'],
                                               config['community_string'])
                result = scanner.run()
                if result:
                    return result
                else:
                    return {
                        'lists': [],
                        'type': 'Info',
                        'message': 'No SNMP Response'
                    }, 200
            except ValidationError as value_error:
                logger.log("Validation encountered on Network Discovery: %s" %
                           (str(value_error)),
                           log_type='ERROR')
                return {
                    'lists': 'Payload error',
                    'type': 'ValidationError',
                    'message': 'Validation errors in your request'
                }, 422
            except Exception as err:
                logger.log("Error encountered on Network Discovery : %s" %
                           str(err),
                           log_type='ERROR')
                return {
                    'type': 'ValidationError',
                    'message': 'Invalid Payload'
                }, 422
        else:
            return {
                'lists': "No payload",
                'type': 'ValidationError',
                'message': 'Validation errors in your request'
            }, 422
コード例 #5
0
class OidApi(Resource):
    

    api_utils = ApiUtils()
    db_utils = DatabaseUtils()

    main_model = OidList
    main_schema = OidListSchema


    def get(self, id=None):
        oid_list = db.session.query(self.main_model.oid_key).distinct(self.main_model.oid_key)
        if id is not None:
            oid_list = oid_list.filter(self.main_model.id==id)
        print(oid_list)
        oid_list.all()
        oid_result = self.main_schema(many=True).dump(oid_list)
        return {'data': oid_result}, 200
コード例 #6
0
class PollerData(Resource):

    api_utils = ApiUtils()
    db_utils = DatabaseUtils()
    poller_schema = SnmpPollerSchema

    def get(self, table_name=None):
        if table_name is None:
            return {'message': 'No table name', 'type': 'TableError'}, 422

        table_name = table_name.replace(' ', '')
        table_not_exists = self.db_utils.check_table_if_not_exist(table_name)

        if not table_not_exists:
            poller_tables = self.db_utils.model_session(SnmpPoller)
            tables = self.db_utils.filter_with_paginate(poller_tables,
                                                        SnmpPoller,
                                                        id=None,
                                                        args=None)
            schema_option = self.api_utils.schema_options(
                self.poller_schema, 'table_name')
            tables_result = SnmpPollerSchema(**schema_option).dump(tables)
            all_poller_tables = [val['table_name'] for val in tables_result]

            if table_name in all_poller_tables:
                args = self.api_utils.optional_parameters()
                result = data_view(args, table_name)
                return jsonify(result)
            else:
                return {
                    'message': 'Invalid table name.',
                    'type': 'TableError'
                }, 422
        else:
            return {
                'message': 'Table does not exist.',
                'type': 'TableError'
            }, 422
コード例 #7
0
from backend_api.app.models.snmp_poller import SnmpPoller
from backend_api.app.models.snmp_poller_schema import SnmpPollerSchema
from backend_api.app.common.api_utils import ApiUtils
from backend_api.app.common.db_utils import DatabaseUtils
from backend_api.app.common.service_manager import ServiceManager
import sys


api_utils = ApiUtils()
db_utils = DatabaseUtils()


if 'no_service' not in sys.argv:
    poll_service = ServiceManager(module_name='poller')
    poller_service = db_utils.select_with_filter(SnmpPoller, SnmpPollerSchema, {'status': 1})
    # poll_service.restart_service(poller_service, 'poller.exe')
    poll_service.restart_service(poller_service, 'poller.py')
コード例 #8
0
class DataPollingApi(Resource):

    api_utils = ApiUtils()
    db_utils = DatabaseUtils()

    module_name = 'Polling Data Preview'

    def post(self):
        args = self.api_utils.parameters_without_model(config='append')

        try:
            config = args['config'][0].replace('\'', '\"')

            if len(config) == 0:
                raise ValueError(
                    ErrorObject(type="Config cannot be blank",
                                message="ConfigError").to_json())

            config = json.loads(config)
            config_preview = {
                'ip_list': config['ip_list'],
                'community_string': config['community_string'],
                'oid_list': config['oid_list']
            }

            status = config['ip_list'][0] if config['ip_list'] else ""

            if self.api_utils.check_list_has_empty_string(
                    config_preview['ip_list']) or isinstance(status, str):
                raise ValueError("Invalid IP list value")

            for ip in config_preview['ip_list']:
                IpListSchema().load(ip)

            if not isinstance(config_preview['oid_list'], list):
                logger.log(
                    "Validation encountered on polling data preview: %s" %
                    ('Invalid oid list values'),
                    log_type='ERROR')
                return {
                    'lists': 'Invalid oid list',
                    'type': 'ValidationError',
                    'message': 'Validation errors in your request'
                }, 422
            poll_preview = PollPreview(config_preview)
            result = poll_preview.run()

            return result
        except ValidationError as value_error:
            logger.log("Validation encountered on polling data preview: %s" %
                       (str(value_error)),
                       log_type='ERROR')
            return {
                'lists': value_error.messages,
                'type': 'ValidationError',
                'message': 'Validation errors in your request'
            }, 422
        except Exception as err:
            logger.log("Error encountered on polling data preview: %s" %
                       str(err),
                       log_type='ERROR')
            return {'type': 'ExceptionType', 'message': str(err)}, 422
コード例 #9
0
class PollerService(Resource):

    db_utils = DatabaseUtils()
    api_utils = ApiUtils()
    service = ServiceManager(module_name='poller')

    main_model = SnmpPoller
    main_schema = SnmpPollerSchema

    def get(self, id=None, log='logs', level=None):
        args = self.api_utils.optional_parameters()
        try:
            if id is None:
                logger.log(
                    "Encountered error on poller service: ID is invalid",
                    log_type='ERROR')
                return {
                    'message': "ID doesn't exist",
                    "type": "ReferenceError"
                }, 422
            logs = db.session.query(SnmpPollerLogs).filter(
                SnmpPollerLogs.snmp_poller_id == id)
            if level is not None:
                logs = logs.filter(SnmpPollerLogs.log_level == level)
                logger.log(
                    "Retrieving data from poller logs with filter : level %s" %
                    (level))

            if args['start'] != 1 or args['limit'] != 0:
                logs = logs.order_by(SnmpPollerLogs.id.asc()).paginate(
                    args['start'], args['limit'], False).items
                logger.log(
                    "Retrieving data from poller logs with paginate : start %s and limit %s"
                    % (args['start'], args['limit']))
            else:
                logs = logs.order_by(SnmpPollerLogs.id.asc()).all()
                logger.log("Retrieving data from poller logs. Status OK 200")

            logs_data = SnmpPollerLogsSchema(many=True).dump(logs)
            return {'data': logs_data}, 200
        except Exception as err:
            pass
        finally:
            db.session.close()
            db.engine.dispose()

    def put(self, id=None):
        try:
            get_poller_service = self.db_utils.select_with_filter(
                self.main_model, self.main_schema, {'id': id})

            if (id is None) or (len(get_poller_service) == 0):
                logger.log(
                    "Encountered error on poller service: ID is invalid",
                    log_type='ERROR')
                return {
                    'message': "ID doesn't exist",
                    "type": "ReferenceError"
                }, 422

            # check_service = self.service.check_service(pid=get_poller_service[0]['pid'], file_name='poller.exe')
            check_service = self.service.check_service(
                pid=get_poller_service[0]['pid'], file_name='poller.py')
            if 'image name' in str(check_service.decode("utf-8")).lower():
                logger.log("Encountered error on poller service: \
                     poller service is already running",
                           log_type='ERROR')
                return {
                    'message': "poller service is already running",
                    "type": "ServiceError"
                }, 422

            else:
                # self.service.start_service(id, 'poller.exe')
                self.service.start_service(id, 'poller.py')

            logger.log(
                "Starting the Service : (%s) Successfully running the service"
                % (id))
            return {'message': 'Successfully running the service'}, 200

        except Exception as err:
            logger.log("Encountered error on poller service : %s" % (str(err)))
            return eval(str(err)), 422

    def delete(self, id=None):
        try:
            if id is None:
                logger.log(
                    "Encountered error on poller service: ID doesn't exist",
                    log_type='ERROR')
                return {
                    'message': "ID doesn't exist",
                    "type": "ReferenceError"
                }, 422
            get_pid = self.db_utils.select_with_filter(self.main_model,
                                                       self.main_schema,
                                                       {'id': id})
            process = self.service.stop_service(pid=get_pid[0]['pid'])
            self.db_utils.update_data(SnmpPoller, {'id': id}, {
                'status': 0,
                'pid': 0
            },
                                      commit=True)
            logger.log("Stopped the poller service : (%s) %s" % (id, process))
            return {'message': process}
        except Exception as err:
            logger.log("Encountered error on poller service :(%s) %s" %
                       (id, err),
                       log_type='ERROR')
            return eval(str(err)), 422
コード例 #10
0
class SnmpPollerApi(Resource):

    api_utils = ApiUtils()
    db_utils = DatabaseUtils()
    db_util_raw = DatabaseUtil()

    main_model = SnmpPoller
    main_schema = SnmpPollerSchema

    service = ServiceManager(module_name='poller')

    module_name = 'SNMP Poller'

    def get(self, id=None, show=None, status=None):
        args = self.api_utils.optional_parameters()
        try:
            poller_session = self.db_utils.model_session(self.main_model)
            if id is not None:
                poller_session = poller_session.filter(
                    self.main_model.id == id)
            poller_session = poller_session.order_by(
                self.main_model.id.desc()).all()

            schema_option = self.api_utils.schema_options(
                self.main_schema, args['columns'])
            poller_result = self.main_schema(
                **schema_option).dump(poller_session)
            poller_result = self.api_utils.get_paginated_list(
                poller_result, args['start'], args['limit'])

            for poller_element in poller_result:
                if 'blacklist' in args['include']:
                    blacklist = self.db_utils.select_with_filter(
                        Blacklist, BlacklistSchema,
                        {'snmp_poller_id': poller_element['id']})
                    poller_element['blacklist'] = blacklist

                if 'selected_oid' in args['include']:
                    selected_oid = self.db_utils.select_with_filter(
                        SelectedOid, SelectedOidSchema,
                        {'snmp_poller_id': poller_element['id']})
                    poller_element['selected_oid'] = selected_oid

                if 'selected_ips' in args['include']:
                    conn = DatabaseUtil(os.environ.get("DB_CONN"),
                                        os.environ.get("DB_USER"),
                                        os.environ.get("DB_PASSWORD"),
                                        os.environ.get("DB_NAME"))
                    query_string = "Select {0}_id,ip_address,system_description,system_name,brand from {0}".format(
                        poller_element["table_name"])
                    result = conn.select_query(query_string)
                    poller_element['selected_ip'] = result

            return {'data': poller_result}, 200
        except Exception as err:
            logger.log("Error encountered on Poller : %s" % str(err),
                       log_type='ERROR')
            return eval(str(err)), 422
        finally:
            db.session.close()
            db.engine.dispose()

    def post(self):
        conn = DatabaseUtil(os.environ.get("DB_CONN"),
                            os.environ.get("DB_USER"),
                            os.environ.get("DB_PASSWORD"),
                            os.environ.get("DB_NAME"))
        args = self.api_utils.parameters(self.main_model(),
                                         blacklist="append",
                                         selected_oid="append",
                                         ip_list="append")
        list_of_ids = {}
        try:
            blacklist = list(eval(items) for items in list(args['blacklist']))
            selected_oid = list(filter(None, args['selected_oid']))
            ip_list = list(eval(items) for items in list(args['ip_list']))
            del args['ip_list']
            del args['blacklist']
            del args['selected_oid']

            SnmpPollerSchema().load(args)
            if (selected_oid and ip_list) and self.api_utils.ip_validator(
                    ip_list) and self.api_utils.ip_validator(
                        blacklist) and self.api_utils.oid_validator(
                            conn, selected_oid):
                poller_data = self.main_schema().load(args)
                poller_result = self.db_utils.insert_data(self.module_name,
                                                          self.main_model,
                                                          poller_data,
                                                          commit=True)
                list_of_ids['snmp_poller'] = poller_result

                if blacklist is not None:
                    for blacklist_element in blacklist:
                        blacklist_obj = {}
                        blacklist_obj['ip_address'] = blacklist_element[
                            'ip_address']
                        blacklist_obj['snmp_poller_id'] = poller_result
                        blacklist_obj[
                            'system_description'] = blacklist_element[
                                'system_description']
                        blacklist_obj['system_name'] = blacklist_element[
                            'system_name']
                        blacklist_obj['brand'] = blacklist_element['brand']
                        blacklist_data = BlacklistSchema().load(blacklist_obj)
                        blacklist_result = self.db_utils.insert_data(
                            self.module_name,
                            Blacklist,
                            blacklist_data,
                            commit=True)
                        list_of_ids['blacklist'] = blacklist_result

                if selected_oid is not None:
                    for selected_oid_element in selected_oid:
                        selected_oid_obj = {}
                        selected_oid_obj['oid_key'] = selected_oid_element
                        selected_oid_obj['snmp_poller_id'] = poller_result
                        selected_oid_data = SelectedOidSchema().load(
                            selected_oid_obj)
                        selected_oid_result = self.db_utils.insert_data(
                            self.module_name,
                            SelectedOid,
                            selected_oid_data,
                            commit=True)
                        list_of_ids['selected_oid'] = selected_oid_result
                oid_raw = conn.select_query('Select oid_key from oid_list')
                oid_main = [oid['oid_key'] for oid in oid_raw]
                oid_main.extend([
                    'ip_address', 'status', 'brand', 'system_description',
                    'system_name'
                ])
                SchemaBuilder(args['table_name'],
                              fields=oid_main).create_table(default_date=True)
                SchemaBuilder().create_data_retention(args['table_name'])

                conn = DatabaseUtil(os.environ.get("DB_CONN"),
                                    os.environ.get("DB_USER"),
                                    os.environ.get("DB_PASSWORD"),
                                    os.environ.get("DB_NAME"))
                query_string = 'INSERT INTO {0} (ip_address,system_description,brand,system_name) values ({1})'.format(
                    args['table_name'],
                    '%(ip_address)s,%(system_description)s,%(brand)s ,%(system_name)s'
                )
                conn.insert_many_query(query_string, ip_list)

                args['snmp_poller_id'] = poller_result
                logger.log("Inserted data to SNMP Poller. Status CREATED 201")
                return {
                    'message': 'Successfully added.',
                    'snmp_poller_payload': args
                }, 201
            else:
                return {
                    'message': 'Payload verification failed.',
                    'snmp_poller_payload': args
                }, 422

        except ValidationError as value_error:
            logger.log("Validation encountered on SNMP poller table: %s" %
                       (str(value_error)),
                       log_type='ERROR')
            self.db_utils.data_rollback(list_of_ids)
            return {
                'lists': value_error.messages,
                'type': 'ValidationError',
                'message': 'Validation errors in your request'
            }, 422
        except Exception as err:
            logger.log("Error encountered on SNMP poller : %s" % str(err),
                       log_type='ERROR')
            self.db_utils.data_rollback(list_of_ids)
            return eval(str(err)), 422

    # edit

    def put(self, id=None):
        args = self.api_utils.parameters(self.main_model(),
                                         blacklist="append",
                                         selected_oid="append",
                                         ip_list="append")
        poller_id = id
        conn = DatabaseUtil(os.environ.get("DB_CONN"),
                            os.environ.get("DB_USER"),
                            os.environ.get("DB_PASSWORD"),
                            os.environ.get("DB_NAME"))
        poll_data = conn.select_query(
            'Select status from snmp_poller where id = {0}'.format(poller_id))
        if not poll_data:
            return {
                'message': "ID doesn't exist",
                "type": "ReferenceError"
            }, 422

        poll_status = poll_data[0]['status']
        list_of_ids = {}

        if poll_status != 1:
            try:
                blacklist = list(
                    eval(items) for items in list(args['blacklist']))
                selected_oid = list(filter(None, args['selected_oid']))
                ip_list = list(eval(items) for items in list(args['ip_list']))
                del args['ip_list']
                del args['blacklist']
                del args['selected_oid']
                SnmpPollerSchema().load(args)
                if (selected_oid and ip_list) and self.api_utils.ip_validator(
                        ip_list) and self.api_utils.ip_validator(
                            blacklist) and self.api_utils.oid_validator(
                                conn, selected_oid):
                    if blacklist is not None:
                        self.db_utils.delete_data_using_id(
                            Blacklist, {'snmp_poller_id': poller_id})
                        for blacklist_element in blacklist:
                            blacklist_obj = {}
                            blacklist_obj['ip_address'] = blacklist_element[
                                'ip_address']
                            blacklist_obj[
                                'system_description'] = blacklist_element[
                                    'system_description']
                            blacklist_obj['system_name'] = blacklist_element[
                                'system_name']
                            blacklist_obj['brand'] = blacklist_element['brand']
                            blacklist_obj['snmp_poller_id'] = poller_id
                            blacklist_data = BlacklistSchema().load(
                                blacklist_obj)
                            blacklist_result = self.db_utils.insert_data(
                                self.module_name,
                                Blacklist,
                                blacklist_data,
                                commit=True)
                            list_of_ids['blacklist'] = blacklist_result

                    if selected_oid is not None:
                        self.db_utils.delete_data_using_id(
                            SelectedOid, {'snmp_poller_id': poller_id})
                        for selected_oid_element in selected_oid:
                            selected_oid_obj = {}
                            selected_oid_obj['oid_key'] = selected_oid_element
                            selected_oid_obj['snmp_poller_id'] = poller_id
                            selected_oid_data = SelectedOidSchema().load(
                                selected_oid_obj)
                            selected_oid_result = self.db_utils.insert_data(
                                self.module_name,
                                SelectedOid,
                                selected_oid_data,
                                commit=True)
                            list_of_ids['selected_oid'] = selected_oid_result

                    if ip_list is not None:
                        table_name = conn.select_query(
                            'Select table_name from snmp_poller where id = %s '
                            % (poller_id))[0]
                        new_table_name = table_name["table_name"]
                        conn.truncate_table('Truncate table %s ' %
                                            (new_table_name))
                        query_string = 'INSERT INTO {0} (ip_address,system_description,brand,system_name) values ({1})'.format(
                            new_table_name,
                            '%(ip_address)s,%(system_description)s,%(brand)s ,%(system_name)s'
                        )
                        conn.insert_many_query(query_string, ip_list)

                    del args['table_name']
                    args['status'] = 0
                    args['pid'] = 0

                    self.db_utils.update_data(self.main_model,
                                              {'id': poller_id},
                                              args,
                                              commit=True)
                    args['snmp_poller_id'] = poller_id
                    logger.log("Update SNMP Poller. Status Succesful 200")
                    return {
                        'message': 'Update Successful.',
                        'snmp_poller_payload': args
                    }, 200

                else:
                    return {
                        'message': 'Payload verification failed.',
                        'snmp_poller_payload': args
                    }, 422

            except ValidationError as value_error:
                logger.log("Validation encountered on SNMP poller table: %s" %
                           (str(value_error)),
                           log_type='ERROR')
                self.db_utils.data_rollback(list_of_ids)
                return {
                    'lists': value_error.messages,
                    'type': 'ValidationError',
                    'message': 'Validation errors in your request'
                }, 422
            except Exception as err:
                logger.log("Error encountered on SNMP poller : %s" % str(err),
                           log_type='ERROR')
                self.db_utils.data_rollback(list_of_ids)
                return eval(str(err)), 422
        else:
            return {'message': "Update unsuccessfull. Poller is running."}, 422

    def delete(self, id=None):
        try:

            get_table_name = None
            poller = self.db_utils.select_with_filter(self.main_model,
                                                      self.main_schema,
                                                      {'id': id})

            if poller:
                # poller_tasklist = self.service.check_service(poller[0]['pid'], 'poller.exe')
                poller_tasklist = self.service.check_service(
                    poller[0]['pid'], 'poller.py')
                if 'image name' in str(
                        poller_tasklist.decode("utf-8")).lower():
                    raise ValueError(
                        ErrorObject(type="ServiceError",
                                    message="Poller is running.").to_json())

                delete_status = self.db_utils.delete_data_using_id(
                    SnmpPoller, {'id': id})

                get_table_name = poller[0]['table_name']
                self.db_utils.drop_table(get_table_name)
                logger.log("Deleting the data on SNMP Poller, id : %s" % (id))
                return {'message': "Successfully deleted."}, 200
            else:
                logger.log("Encountered error on poller: ID doesn't exist",
                           log_type='ERROR')
                return {
                    'message': "ID doesn't exist",
                    "type": "ReferenceError"
                }, 422
        except Exception as err:
            logger.log("Error encountered on SNMP Poller : %s" % str(err),
                       log_type='ERROR')
            return eval(str(err)), 422