Esempio n. 1
0
 def __init__(self, pid_directory, id, module_name, table_name, logger):
     self.pid_directory = pid_directory
     self.id = id
     self.module_name = module_name.replace(' ', '_').lower()
     self.table_name = table_name
     self.logger = logger
     self.conn = DatabaseUtil(os.environ.get("DB_CONN"),
                              os.environ.get("DB_USER"),
                              os.environ.get("DB_PASSWORD"),
                              os.environ.get("DB_NAME"), self.logger)
    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()
Esempio n. 3
0
 def __init__(self, poller_id=0):
     self.poller_id = poller_id
     # self.conn = DatabaseUtil(os.environ.get("DB_CONN"), os.environ.get("DB_USER"), os.environ.get("DB_PASSWORD"), os.environ.get("DB_NAME"))
     try:
         self.conn = DatabaseUtil(os.environ.get("DB_CONN"),
                                  os.environ.get("DB_USER"),
                                  os.environ.get("DB_PASSWORD"),
                                  os.environ.get("DB_NAME"))
         conn = self.conn.get_connection(interval=60)
         conn.close()
     except Exception as err:
         print(err)
         sys.exit()
     self.logger = None
     self.pid_util = None
     atexit.register(self.exit_handler)
Esempio n. 4
0
 def __init__(self, table_name=None,orig_table_name=None,fields=[], default=[]):
     self.orig_table_name = orig_table_name
     self.table = table_name
     self.fields = fields
     self.default = default
     self.db_util = DatabaseUtil(os.environ.get("DB_CONN"), os.environ.get("DB_USER"), 
         os.environ.get("DB_PASSWORD"), os.environ.get("DB_NAME"))
    def __init__(self,
                 logs_directory=None,
                 module_id=None,
                 module_name=None,
                 table_name=None,
                 use_db=True):
        self.logs_directory = logs_directory
        self.module_name = module_name
        self.table_name = table_name
        self.module_id = module_id
        self.use_db = use_db

        try:
            self.conn = DatabaseUtil(os.environ.get("DB_CONN"),
                                     os.environ.get("DB_USER"),
                                     os.environ.get("DB_PASSWORD"),
                                     os.environ.get("DB_NAME"))
        except Exception as err:
            print(err)
            exit(0)
 def authentication(self):
     try:
         if self.args['type'] == "API":
             if self.auth['authentication_type'] == 'token':
                 url = "{0}{1}".format(self.auth['host'],
                                       self.auth['token_url'])
                 auth = requests.post(url,
                                      auth=HTTPBasicAuth(
                                          username=self.auth['username'],
                                          password=self.auth['password']),
                                      verify=False)
                 return auth.json()
             else:
                 return None
         elif self.args['type'] == "Database":
             conn = DatabaseUtil(self.auth['host'], self.auth['username'],
                                 self.auth['password'],
                                 self.auth['db_name'])
             test_conn = conn.get_connection(api=True)
             return conn
     except Exception as err:
         raise ValueError('Cannot connect to source {0} - {1}'.format(
             self.args['type'], self.auth['host']))
Esempio n. 7
0
class PollPreview:
    def __init__(self,prev_config):
        self.ip_list = prev_config['ip_list']
        self.community_string = prev_config['community_string']
        self.oid_list = prev_config['oid_list']
        self.conn = DatabaseUtil(os.environ.get("DB_CONN"), os.environ.get("DB_USER"), os.environ.get("DB_PASSWORD"), os.environ.get("DB_NAME"))

    def get_oid_prev(self,brand):
        oid_list = {"oid_list" :[]}
        oid_inner = dict()
        for oid_key in self.oid_list:
            sql_query = sql_utils.sql_templates["oid_prev"].value
            oid_raw = self.conn.jinja_select_query(sql_query, {'oid_key': oid_key,'brand': brand})[0]
            oid_inner[oid_raw['oid_key']] = oid_raw['oid']
        oid_list['oid_list'] = [oid_inner]
        return oid_list

    def run(self):
        main_data =list()
        try:
            for prev_info in self.ip_list:
                ip_address = prev_info['ip_address']
                brand = prev_info['brand']
                for_mdd = self.get_oid_prev(brand)
                mdd_runner = main_device_details(ip_address , for_mdd , self.community_string)
                mdd_output = mdd_runner.run()
                mdd_output["ip_address"] = ip_address
                main_data.append(mdd_output)
            return main_data
        except Exception as err:
            print('-----------------------')
            print(err)
            raise ValueError(err)
          

# prev_config = {
#     'ip_list' : [{'ip_address' : '192.168.1.1' , 'brand' : 'cisco'}],
#     'community_string' : 'trends-ro',
#     'oid_list' : ['cpu' , 'memory']
# }
# sample = PollPreview(prev_config)
# abcd = sample.run()
# print(abcd)
Esempio n. 8
0
 def __init__(self,prev_config):
     self.ip_list = prev_config['ip_list']
     self.community_string = prev_config['community_string']
     self.oid_list = prev_config['oid_list']
     self.conn = DatabaseUtil(os.environ.get("DB_CONN"), os.environ.get("DB_USER"), os.environ.get("DB_PASSWORD"), os.environ.get("DB_NAME"))
Esempio n. 9
0
class start_polling:
    def __init__(self, poller_id=0):
        self.poller_id = poller_id
        # self.conn = DatabaseUtil(os.environ.get("DB_CONN"), os.environ.get("DB_USER"), os.environ.get("DB_PASSWORD"), os.environ.get("DB_NAME"))
        try:
            self.conn = DatabaseUtil(os.environ.get("DB_CONN"),
                                     os.environ.get("DB_USER"),
                                     os.environ.get("DB_PASSWORD"),
                                     os.environ.get("DB_NAME"))
            conn = self.conn.get_connection(interval=60)
            conn.close()
        except Exception as err:
            print(err)
            sys.exit()
        self.logger = None
        self.pid_util = None
        atexit.register(self.exit_handler)

    def poller_event(self, status):
        status = int(status)
        try:
            self.conn.jinja_update_query(
                sql_utils.sql_templates["update_event"].value, {
                    "status": status,
                    "id": self.poller_id
                })
            if status is not 1:
                os.kill(os.getpid(), signal.SIGINT)
        except Exception as err:
            self.logger.log(
                'An error has occured while updating poller status: {0}'.
                format(err), 'critical')
            sys.exit(1)

    def get_poller_config(self):
        sql_query = sql_utils.sql_templates["poller_config"].value
        return self.conn.jinja_select_query(sql_query,
                                            {'poll_id': self.poller_id})

    def get_selected_oid(self, id, brand):
        oid_list = {"oid_list": []}
        oid_inner = dict()
        sql_query = sql_utils.sql_templates["oid_config"].value
        oid_raw = self.conn.jinja_select_query(sql_query, {
            'poll_id': id,
            'brand': brand
        })
        for key, value in enumerate(oid_raw):
            oid_inner[value['oid_key']] = value['oid']
        oid_list['oid_list'] = [oid_inner]
        return oid_list

    def get_ip_address(self, table):
        ip_list = list()
        raw_list = self.conn.select_query(
            """SELECT ip_address from {0}""".format(table))
        for key, items in enumerate(raw_list):
            ip_list.append(items["ip_address"])
        return ip_list

    def insert_update(self, device_detail, table_name):
        ip_address = device_detail['ip_address']
        del device_detail['ip_address']

        placeholder = ', '.join('?' * len(device_detail))
        update_util = sql_utils.sql_templates["poll_update_up"].value

        update_query = update_util.format(
            table_name,
            ', '.join("{0}='{1}'".format(value, device_detail[value])
                      for key, value in enumerate(device_detail)), ip_address,
            self.poller_id)
        self.conn.update_query(update_query)

    def exit_handler(self):
        if self.pid_util:
            if str(self.pid_util.read_pid()) == str(os.getpid()):
                try:
                    self.pid_util.delete_pid()
                except Exception as err:
                    self.logger.log(err, 'critical')

    def signal_handler(self, signum, frame):
        self.exit_handler()
        if self.logger:
            self.logger.log('Service stopped.')
        sys.exit(1)

    def create_directory(self, directory):
        if not os.path.exists(os.path.join(os.getcwd(), directory)):
            os.mkdir(os.path.join(os.getcwd(), directory))
            print(directory, 'Directory created.')

    def run(self):
        self.create_directory(logs_directory)
        self.create_directory(pid_directory)
        poller_config = self.get_poller_config()

        if poller_config:
            poller_config = poller_config[0]
            subnet = poller_config["subnet"]
            community_string = poller_config["community_string"]
            interval = poller_config["interval"]
            table_name = poller_config["table_name"]
            poll_name = poller_config["poll_name"]
            self.logger = Logger(logs_directory, self.poller_id, table_name,
                                 'snmp_poller_logs')
            self.pid_util = ProcessIdUtil(pid_directory, self.poller_id,
                                          table_name, 'snmp_poller', None)
            self.logger.config_logging()

            if self.pid_util.is_process_running():
                self.logger.log('Already running.')
                sys.exit(1)
            else:
                self.logger.log("[{0}] : Poller running...".format(poll_name))
                self.poller_event(1)
                ip_list = self.get_ip_address(table_name)
                try:
                    self.pid_util.create_pid()
                    self.pid_util.save_pid()
                except Exception as err:
                    self.logger.log(err, 'critical')
                    sys.exit(1)
                runner = True

                while runner:
                    # start
                    for ip_address in ip_list:
                        mdi_runner = main_device_info(ip_address,
                                                      community_string)
                        mdi_output = mdi_runner.run()
                        if mdi_output["is_valid"]:
                            try:
                                mdi_data = mdi_output["main_info"]
                                for_mdd = self.get_selected_oid(
                                    self.poller_id, mdi_data['brand'])
                                mdd_runner = main_device_details(
                                    mdi_data["ip_address"], for_mdd,
                                    community_string)
                                mdd_output = mdd_runner.run()
                                mdd_output["ip_address"] = mdi_data[
                                    "ip_address"]
                                self.insert_update(mdd_output, table_name)
                            except Exception as err:
                                self.logger.log(
                                    "[{0}] : Stopped due to an error : {1}".
                                    format(poll_name, err))
                                self.poller_event('-1')
                        else:
                            update_util = sql_utils.sql_templates[
                                "poll_update_down"].value
                            update_query = update_util.format(
                                table_name, ip_address)
                            self.conn.update_query(update_query)
                            self.logger.log(
                                "[{0}] : No SNMP response for {1}".format(
                                    poll_name, ip_address))
                        time.sleep(2)
                    #end
                time.sleep(interval)
        else:
            print('Poller does not exist.')
class Logger():
    def __init__(self,
                 logs_directory=None,
                 module_id=None,
                 module_name=None,
                 table_name=None,
                 use_db=True):
        self.logs_directory = logs_directory
        self.module_name = module_name
        self.table_name = table_name
        self.module_id = module_id
        self.use_db = use_db

        try:
            self.conn = DatabaseUtil(os.environ.get("DB_CONN"),
                                     os.environ.get("DB_USER"),
                                     os.environ.get("DB_PASSWORD"),
                                     os.environ.get("DB_NAME"))
        except Exception as err:
            print(err)
            exit(0)

    def create_directory(self):
        if not os.path.exists(os.path.join(os.getcwd(), self.logs_directory)):
            os.mkdir(os.path.join(os.getcwd(), self.logs_directory))

    def config_logging(self, use_datetime=False):
        module_name = self.module_name.replace(' ', '_').lower()
        now = datetime.datetime.now()
        if use_datetime:
            module_name = module_name + '_' + now.strftime("%Y%m%d")
        log_handler = TimedRotatingFileHandler(os.path.join(
            os.getcwd(), self.logs_directory, module_name + '.log'),
                                               when='midnight')
        log_formatter = logging.Formatter(
            '%(asctime)s - %(levelname)s - %(message)s', '%Y-%m-%d %H:%M:%S')
        log_handler.setFormatter(log_formatter)
        log_handler.suffix = '%Y%m%d'
        self.logger = logging.getLogger('logger')
        self.logger.addHandler(log_handler)
        self.logger.setLevel(logging.INFO)

    def log(self, description, log_type='INFO'):
        log = ""
        if self.use_db:
            log = '{0} ({1}) : {2}'.format(self.module_name, self.module_id,
                                           description)
            print(log)
        else:
            log = '{0} : {1}'.format(self.module_name, description)

        log_type = str(log_type).upper()

        if log_type == 'CRITICAL':
            self.logger.critical(log)
        elif log_type == 'WARNING':
            self.logger.warning(log)
        elif log_type == 'ERROR':
            self.logger.error(log)
        elif log_type == 'DEBUG':
            self.logger.debug(log)
        else:
            self.logger.info(log)

        log = log.replace('\'', '\"')

        if self.use_db:
            query_string = 'INSERT INTO {0} values ({1}, \'{2}\', \'{3}\', DEFAULT)'.format(
                self.table_name, self.module_id, log_type, log)
            try:
                self.conn.insert_query(query_string)
            except Exception as err:
                log = '{0} ({1}) : {2}'.format(self.module_name,
                                               self.module_id, err)
                print(log)
Esempio n. 11
0
class ProcessIdUtil():
    def __init__(self, pid_directory, id, module_name, table_name, logger):
        self.pid_directory = pid_directory
        self.id = id
        self.module_name = module_name.replace(' ', '_').lower()
        self.table_name = table_name
        self.logger = logger
        self.conn = DatabaseUtil(os.environ.get("DB_CONN"),
                                 os.environ.get("DB_USER"),
                                 os.environ.get("DB_PASSWORD"),
                                 os.environ.get("DB_NAME"), self.logger)

    def create_directory(self):
        if not os.path.exists(os.path.join(os.getcwd(), self.pid_directory)):
            os.mkdir(os.path.join(os.getcwd(), self.pid_directory))
            print(self.pid_directory, 'created.')

    def create_pid(self):
        try:
            with open(
                    os.path.join(os.getcwd(), self.pid_directory,
                                 str(self.module_name) + '.pid'), 'w') as file:
                file.write(str(os.getpid()))
        except Exception as err:
            raise ValueError("Error encountered in creating PID: %s" % (err))

    def read_pid(self):
        try:
            f = open(
                os.path.join(os.getcwd(), self.pid_directory,
                             str(self.module_name) + '.pid'), 'r')
            contents = f.read()
            f.close()
            return contents
        except FileNotFoundError:
            return False

    def delete_pid(self):
        try:
            os.remove(
                os.path.join(os.getcwd(), self.pid_directory,
                             str(self.module_name) + '.pid'))
        except Exception as err:
            raise ValueError("Error encountered in deleting PID: %s" % (err))

    def save_pid(self, pid=None):
        try:
            if pid:
                query_string = 'UPDATE {0} SET pid = {1} WHERE id = {2}'.format(
                    self.table_name, pid, self.id)
            else:
                query_string = 'UPDATE {0} SET pid = {1} WHERE id = {2}'.format(
                    self.table_name, os.getpid(), self.id)
            self.conn.insert_query(query_string)
        except Exception as err:
            raise ValueError("Error encountered in creating PID: %s" % (err))

    def get_pid(self):
        try:
            query_string = 'SELECT pid FROM {0} WHERE id = {1}'.format(
                self.table_name, self.id)
            pid = self.conn.select_query(query_string)
            return pid[0]['pid']
        except Exception as err:
            raise ValueError("Error encountered in getting PID: %s" % (err))

    def is_pid_running(self, pid):
        # services = ['worker.exe', 'aggregate.exe','visionapi.exe']
        services = ['worker.py', 'aggregate.py', 'VisionAPI.py', 'poller.py']
        # check if pid exists
        if psutil.pid_exists(pid):
            process = psutil.Process(pid)
            if process.name() == 'python.exe' and (
                    process.cmdline()[1] and process.cmdline()[1] in services):
                return True

        # if pid does not exist, check if command exists
        for process in psutil.process_iter():
            try:
                if len(process.cmdline()) == 3:
                    if process.name() == 'python.exe' and (
                            process.cmdline()[1] and process.cmdline()[1]
                            in services) and process.cmdline()[2] == self.id:
                        if process.pid != os.getpid():
                            self.save_pid(process.pid)
                            return True
            except (psutil.NoSuchProcess, psutil.AccessDenied,
                    psutil.ZombieProcess):
                pass

        return False

    def is_process_running(self):
        db_pid = self.get_pid()
        db_pid_running = self.is_pid_running(db_pid)

        if self.read_pid():
            file_pid = int(self.read_pid())
            file_pid_running = self.is_pid_running(file_pid)

            if file_pid_running and db_pid_running:
                return True
        else:
            if db_pid_running:
                return True

        return False

    def is_process_running_without_db(self):

        if self.read_pid():
            file_pid = int(self.read_pid())
            file_pid_running = self.is_pid_running(file_pid)
            if file_pid_running:
                return True
        return False
    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
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
    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