Index: /branches/amp_3_7_2/src/webui/webui/htdocs/new/src/cm/lib/communication.py
===================================================================
--- /branches/amp_3_7_2/src/webui/webui/htdocs/new/src/cm/lib/communication.py	(revision 2805)
+++ /branches/amp_3_7_2/src/webui/webui/htdocs/new/src/cm/lib/communication.py	(working copy)
@@ -98,7 +98,7 @@
         curl.setopt(pycurl.POSTFIELDS, params)
         # curl.setopt(pycurl.COOKIEFILE, "ComposerUISESS=f1f713c9e000f5d3f280adbd124df4f5; Path=/; Max-Age=86400")
     if save_cookie:
-        curl.setopt(pycurl.VERBOSE, 1L) #DEBUG MODE
+        curl.setopt(pycurl.VERBOSE, 1) #DEBUG MODE
         curl.setopt(pycurl.HEADER, 1) #don't write http header to response
         curl.setopt(pycurl.HTTPHEADER,  ['Accept: application/json', 'Authorization: %s'%authorization, 'Expect:'])
         curl.setopt(pycurl.URL, "http://%s:%d%s"%(str(ip), int(restapi_port), url))
@@ -122,10 +122,10 @@
                     'status': status,
                     'body': buffer.getvalue(),
                 }
-            curl.close()
-        except Exception, e:
+        except Exception as e:
             raise NameError(str(e))
         finally:
+            curl.close()
             if lock:
                 lock.release()
         return data
@@ -133,7 +133,7 @@
         if proto == 'https':
             curl.setopt(pycurl.SSL_VERIFYPEER, 0)
             curl.setopt(pycurl.SSL_VERIFYHOST, 0)
-            curl.setopt(pycurl.VERBOSE, 1L) #DEBUG MODE
+            curl.setopt(pycurl.VERBOSE, 1) #DEBUG MODE
             curl.setopt(pycurl.HEADER, 0) #don't write http header to response
             curl.setopt(pycurl.HTTPHEADER,  ['Accept: application/json', 'Authorization: %s'%authorization, 'Expect:'])
             curl.setopt(pycurl.URL, "https://%s:%d%s"%(str(ip), int(restapi_port), url))
@@ -149,17 +149,17 @@
                     #'reason': response.reason,
                     'body': buffer.getvalue(),
                 }
-                curl.close()
-            except Exception, e:
+            except Exception as e:
                 raise NameError(str(e))
             finally:
+                curl.close()
                 if lock:
                     lock.release()
         else:
 
             # curl.setopt(pycurl.SSL_VERIFYPEER, 0)
             # curl.setopt(pycurl.SSL_VERIFYHOST, 0)
-            curl.setopt(pycurl.VERBOSE, 1L) #DEBUG MODE
+            curl.setopt(pycurl.VERBOSE, 1) #DEBUG MODE
             curl.setopt(pycurl.HEADER, 0) #don't write http header to response
             curl.setopt(pycurl.HTTPHEADER,  ['Accept: application/json', 'Authorization: %s'%authorization, 'Expect:'])
             curl_url = "http://%s:%d%s"%(str(ip), int(restapi_port), url)
@@ -175,10 +175,10 @@
                     'status': curl.getinfo(pycurl.RESPONSE_CODE),
                     'body': buffer.getvalue(),
                 }
-                curl.close()
-            except Exception, e:
+            except Exception as e:
                 raise NameError(str(e))
             finally:
+                curl.close()
                 if lock:
                     lock.release()
         return data
@@ -195,7 +195,7 @@
     if proto == 'https':
         curl.setopt(pycurl.SSL_VERIFYPEER, 0)
         curl.setopt(pycurl.SSL_VERIFYHOST, 0)
-        curl.setopt(pycurl.VERBOSE, 1L) #DEBUG MODE
+        curl.setopt(pycurl.VERBOSE, 1) #DEBUG MODE
         curl.setopt(pycurl.HEADER, 0)
         curl.setopt(pycurl.HTTPHEADER,  ['Content-Type: text/xml', 'Expect:'])
         curl.setopt(pycurl.URL, "https://%s:%d%s"%(str(ip), int(xmlrpc_port), url))
@@ -207,11 +207,12 @@
                 #'reason': response.reason,
                 'body': buffer.getvalue(),
             }
-            curl.close()
-        except Exception, e:
+        except Exception as e:
             raise NameError(str(e))
+        finally:
+            curl.close()
     else:
-        curl.setopt(pycurl.VERBOSE, 1L) #DEBUG MODE
+        curl.setopt(pycurl.VERBOSE, 1) #DEBUG MODE
         curl.setopt(pycurl.HEADER, 0)
         curl.setopt(pycurl.HTTPHEADER,  ['Content-Type: text/xml', 'Expect:'])
         curl_url = "http://%s:%d%s"%(str(ip), int(xmlrpc_port), url)
@@ -223,9 +224,10 @@
                 'status': curl.getinfo(pycurl.RESPONSE_CODE),
                 'body': buffer.getvalue(),
             }
-            curl.close()
-        except Exception, e:
+        except Exception as e:
             raise NameError(str(e))
+        finally:
+            curl.close()
     return data
 
 def send_http_request(method, url, data, format, proto, ip, port, timeout=20):
@@ -246,7 +248,7 @@
             'reason': response.reason,
             'body': response.read(),
         }
-    except Exception, e:
+    except Exception as e:
         raise e
     finally:
         if httpClient:
@@ -260,7 +262,7 @@
         ip = "[%s]" % ip
     buffer = StringIO()
     curl = pycurl.Curl()
-    curl.setopt(pycurl.VERBOSE, 1L) #DEBUG MODE
+    curl.setopt(pycurl.VERBOSE, 1) #DEBUG MODE
     curl.setopt(pycurl.HTTPHEADER,  ['Accept: application/json', 'Authorization: %s'%authorization, 'Expect: '])
     curl.setopt(pycurl.CONNECTTIMEOUT, 5)
     if method == 'POST':
@@ -275,21 +277,23 @@
         curl.setopt(pycurl.WRITEFUNCTION, buffer.write)
         if cookie:
             curl.setopt(pycurl.COOKIE, cookie)
-        curl.perform()
-        status = curl.getinfo(pycurl.RESPONSE_CODE)
-        if status == 200:
-            cookie = buffer.getvalue().split("Set-Cookie: ")[1].split("\r\n")[0]
-            data = {
-                'status': status,
-                'body': buffer.getvalue().split("\r\n\r\n")[1],
-                'cookie': cookie,
-            }
-        else:
-            data = {
-                'status': status,
-                'body': buffer.getvalue(),
-            }
-        curl.close()
+        try:
+            curl.perform()
+            status = curl.getinfo(pycurl.RESPONSE_CODE)
+            if status == 200:
+                cookie = buffer.getvalue().split("Set-Cookie: ")[1].split("\r\n")[0]
+                data = {
+                    'status': status,
+                    'body': buffer.getvalue().split("\r\n\r\n")[1],
+                    'cookie': cookie,
+                }
+            else:
+                data = {
+                    'status': status,
+                    'body': buffer.getvalue(),
+                }
+        finally:
+            curl.close()
         return data
     else:
         curl.setopt(pycurl.HEADER, 0) #don't write http header to response
@@ -302,10 +306,12 @@
         curl.setopt(pycurl.WRITEFUNCTION, buffer.write)
         if cookie:
             curl.setopt(pycurl.COOKIE, cookie)
-        curl.perform()
-        data = {
-            'status': curl.getinfo(pycurl.RESPONSE_CODE),
-            'body': buffer.getvalue(),
-        }
-        curl.close()
+        try:
+            curl.perform()
+            data = {
+                'status': curl.getinfo(pycurl.RESPONSE_CODE),
+                'body': buffer.getvalue(),
+            }
+        finally:
+            curl.close()
         return data
\ No newline at end of file
Index: /branches/amp_3_7_2/src/webui/webui/htdocs/new/src/cm/lib/libbasic_operation.py
===================================================================
--- /branches/amp_3_7_2/src/webui/webui/htdocs/new/src/cm/lib/libbasic_operation.py	(revision 2805)
+++ /branches/amp_3_7_2/src/webui/webui/htdocs/new/src/cm/lib/libbasic_operation.py	(working copy)
@@ -233,10 +233,14 @@
 
 
 def get_rest_info_from_device(device_name):
-    db = DB.get_connected_db()
-    fetchall_sql = '''SELECT ip_address, protocol, restapi_port, restapi_username, restapi_password, type, name, enable_password FROM device where name='%s' ''' % device_name
-    data = db.fetchall(fetchall_sql)
-    db.close()
+    db = None
+    try:
+        db = DB.get_connected_db()
+        fetchall_sql = '''SELECT ip_address, protocol, restapi_port, restapi_username, restapi_password, type, name, enable_password FROM device where name='%s' ''' % device_name
+        data = db.fetchall(fetchall_sql)
+    finally:
+        if db:
+            db.close()
     if not data:
         return [None,]
     key = ['ip_address', 'protocol', 'restapi_port', 'restapi_username', 'restapi_password', 'type', 'name', 'enable_password']
@@ -312,9 +316,13 @@
                 username = "system"
         s_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
         insert_sql = "insert into operation_log(username, client_ip, time, level, module, operation) values('%s', '%s', '%s', '%s', '%s', '%s')" % (username, ip, s_time, level, module, operation)
-        db = DB.get_connected_db()
-        db.execute_sql(insert_sql)
-        db.close()
+        db = None
+        try:
+            db = DB.get_connected_db()
+            db.execute_sql(insert_sql)
+        finally:
+            if db:
+                db.close()
         log_msg = "%s level=%s, username=%s, client_ip=%s, module=%s, message=\"%s\"" %(s_time, level, username, ip, module, operation)
         send_log_to_hosts(log_msg)
 
@@ -327,17 +335,25 @@
         result_msg = "success" if result["result"] else "failed, %s" % json.dumps(result["msg"])
         operation = "Process RESTful API request: %s, result: %s" % (request_msg, result_msg)
         insert_sql = "insert into operation_log(username, client_ip, time, level, module, operation) values('%s', '%s', '%s', '%s', '%s', '%s')" % (username, ip, s_time, level, "API", operation)
-        db = DB.get_connected_db()
-        db.execute_sql(insert_sql)
-        db.close()
+        db = None
+        try:
+            db = DB.get_connected_db()
+            db.execute_sql(insert_sql)
+        finally:
+            if db:
+                db.close()
         log_msg = "%s level=%s, username=%s, client_ip=%s, module=API, message=\"%s\"" %(s_time, level, username, ip, operation)
         send_log_to_hosts(log_msg)
 
 def query_all_default_tmpkey():
-    db = DB.get_connected_db()
-    fetchall_sql = "SELECT key, default_value FROM config_template"
-    sql_res = db.fetchall(fetchall_sql)
-    db.close()
+    db = None
+    try:
+        db = DB.get_connected_db()
+        fetchall_sql = "SELECT key, default_value FROM config_template"
+        sql_res = db.fetchall(fetchall_sql)
+    finally:
+        if db:
+            db.close()
     key = ["key", "default_value"]
     result = [dict(zip(key, each)) for each in sql_res]
 
@@ -345,32 +361,42 @@
 
 def insert_default_tmpkey(device_ip):
     default_tmpkey_list = query_all_default_tmpkey()
-    db = DB.get_connected_db()
+    db = None
+    try:
+        db = DB.get_connected_db()
 
-    for each in default_tmpkey_list:
-        insert_sql = "INSERT INTO device_config_template(device_id, key, value) VALUES('%s', '%s', '%s')" % (device_ip, each["key"], each["default_value"])
-        db.execute_sql(insert_sql)
-    db.close()
+        for each in default_tmpkey_list:
+            insert_sql = "INSERT INTO device_config_template(device_id, key, value) VALUES('%s', '%s', '%s')" % (device_ip, each["key"], each["default_value"])
+            db.execute_sql(insert_sql)
+    finally:
+        if db:
+            db.close()
 
     return
 
 def delete_all_tmpkey(device_ip):
+    db = None
     try:
         db = DB.get_connected_db()
         delete_sql = "DELETE FROM device_config_template WHERE device_ip = '%s'" % device_ip
         db.execute_sql(delete_sql)
-        db.close()
     except:
+        pass
+    finally:
         if db:
             db.close()
 
     return
 
 def get_template_content(file_path, device_ip):
-    db = DB.get_connected_db()
-    fetchall_sql = "SELECT key, value FROM device_config_template WHERE device_id = '%s'" % device_ip
-    sql_res = db.fetchall(fetchall_sql)
-    db.close()
+    db = None
+    try:
+        db = DB.get_connected_db()
+        fetchall_sql = "SELECT key, value FROM device_config_template WHERE device_id = '%s'" % device_ip
+        sql_res = db.fetchall(fetchall_sql)
+    finally:
+        if db:
+            db.close()
     key = ["key", "value"]
     result = [dict(zip(key, each)) for each in sql_res]
 
Index: /branches/amp_3_7_2/src/webui/webui/htdocs/new/src/cm/lib/task_scheduler.py
===================================================================
--- /branches/amp_3_7_2/src/webui/webui/htdocs/new/src/cm/lib/task_scheduler.py	(revision 2805)
+++ /branches/amp_3_7_2/src/webui/webui/htdocs/new/src/cm/lib/task_scheduler.py	(working copy)
@@ -1,29 +1,30 @@
-#from cm.lib.sqlite_db import DB
-from cm.lib.postgres_db import DB
-from hive.imports.model import *
-from hive.utils import andebug, get_device_type, standard_model_type
-from apscheduler.schedulers.background import BackgroundScheduler
-from apscheduler.events import *
-from cm.conf import NETWORK_CARD
-from djproject.settings import FILE_UPLOAD_DIR, CONFIG_UPLOAD_STORE_DIR
-import hashlib, json, time, logging, logging.handlers, threading, re
-import os, shutil, requests, datetime, difflib
-from hive.model.query import mark_expire_all
-from hive.model.loading import get_model
-from cm.lib.communication import send_https_rest_request, modify_url, call_restapi
-from cm.lib.libbasic_operation import send_command_to_device, get_rest_info_from_device, get_ip_address, md5sum, send_xmlrpc_to_device
-import hashlib, uuid, tarfile
-from cm.lib.parse_configfile import parse_vsite_from_configfile, DEFAULT_CONFIG_FILE_PATH, parse_vsite_config_from_configfile, parse_diff_config, TarFiles
-from hive.notification import send_notification
 from cm.lib.libmonitor import Decision, SSLCertDecision
 from cm.lib.libconfig import Backup_AG, Backup_APV, Recover_AG, Recover_APV, Backup_Custom, Apply_AG
 #from cm.models.device_mgmt.device import get_rest_info_from_device
 #from cm.models.configuration.update import update_updatelist
 from djproject.an_settings import *
 import socket
-from cm.lib.libbasic_operation import oper_log, send_cli_to_device
+import logging
+import time
+import datetime
+import json
+import os
+import hashlib
+import shutil
+import requests
+import re
+from apscheduler.schedulers.background import BackgroundScheduler
+from multiprocessing.pool import ThreadPool
+from cm.lib.libbasic_operation import oper_log, send_cli_to_device, send_xmlrpc_to_device, get_rest_info_from_device, get_device_type, get_ip_address
+from cm.lib.postgres_db import DB
+from hive.model.loading import get_model
+from hive.model.query import mark_expire_all
+from hive.notification import send_notification
+from cm.lib.communication import send_https_rest_request, modify_url
+from hive.model.legacycli import cli_parse, RegexParser, MATCHONE
 
 logger = logging.getLogger('hive.debug')
+device_check_pool = ThreadPool(processes=20)
 
 def append_job_description(name, description):
     db = DB.get_connected_db()
@@ -742,13 +743,17 @@
         config_file.close()
 
         create_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
-        db = DB.get_connected_db()
-        insert_sql = "INSERT INTO file_list(name, create_time, modify_time, type, device_type, device_id) values('%s', '%s', '%s', '%s', '%s', '%s') on conflict (name) do update set modify_time=excluded.modify_time" % (file_name, create_time, create_time, 'device', device_info[0]['type'], device_info[0]['id'])
-        #delete_sql = "DELETE FROM file_list where name='%s' " % file_name
-        db = DB.get_connected_db()
-        #db.execute_sql(delete_sql)
-        db.execute_sql(insert_sql)
-        db.close()
+        create_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
+        db = None
+        try:
+            db = DB.get_connected_db()
+            insert_sql = "INSERT INTO file_list(name, create_time, modify_time, type, device_type, device_id) values('%s', '%s', '%s', '%s', '%s', '%s') on conflict (name) do update set modify_time=excluded.modify_time" % (file_name, create_time, create_time, 'device', device_info[0]['type'], device_info[0]['id'])
+            db.execute_sql(insert_sql)
+        except Exception:
+            pass
+        finally:
+            if db:
+                db.close()
   
     if device_info[0]['type'].lower() in ADC_TYPE_LIST:
         return
@@ -1976,193 +1981,193 @@
     db.execute_sql(update_sql)
     db.close()
 
-class DeviceStatusThread(threading.Thread):
-    def __init__(self, data):
-        super(DeviceStatusThread, self).__init__()
-        self.data = data
-
-    def run(self):
-        # begin_time = time.time()
-        failed_time = 3
-        while failed_time:
-            if self.data['protocol'] == 'xmlrpc':
-                response_data = send_cli_to_device(self.data, "show version", mode='enable')
-                if response_data['message'] == 'success':
-                    set_device_status(self.data['id'], 'connected')
-                    set_device_version(self.data['id'], response_data['data'])
-                    break
-                else:
-                    failed_time -= 1
-                    logger.error('Get device<%s>(%s) version failed. %s' % (self.data['name'], self.data['ip'], response_data['data']))
+def check_one_device_status(data):
+    # begin_time = time.time()
+    failed_time = 3
+    while failed_time:
+        if data['protocol'] == 'xmlrpc':
+            response_data = send_cli_to_device(data, "show version", mode='enable')
+            if response_data['message'] == 'success':
+                set_device_status(data['id'], 'connected')
+                set_device_version(data['id'], response_data['data'])
+                break
             else:
+                failed_time -= 1
+                logger.error('Get device<%s>(%s) version failed. %s' % (data['name'], data['ip'], response_data['data']))
+                time.sleep(2)
+        else:
+            try:
+                new_url = modify_url('/rest/device_type/system/SystemInfo/version', data['type'])
+                rest_response_data = send_https_rest_request('GET', new_url, '', data['ip'], data['restapi_port'], data['restapi_username'], data['restapi_password'])
+            except Exception, e:
+                failed_time -= 1
+                logger.error('Get device<%s>(%s) version failed. %s' % (data['name'], data['ip'], str(e)))
+                time.sleep(2)
+            else:
+                if rest_response_data['status'] == 200 and data['connection'] != 'connected':
+                    set_device_status(data['id'], 'connected')
                 try:
-                    new_url = modify_url('/rest/device_type/system/SystemInfo/version', self.data['type'])
-                    rest_response_data = send_https_rest_request('GET', new_url, '', self.data['ip'], self.data['restapi_port'], self.data['restapi_username'], self.data['restapi_password'])
+                    rest_body = json.loads(rest_response_data['body'])
+                    set_device_version(data['id'], rest_body['SystemInfo']['version'])
                 except Exception, e:
-                    failed_time -= 1
-                    logger.error('Get device<%s>(%s) version failed. %s' % (self.data['name'], self.data['ip'], str(e)))
-                else:
-                    if rest_response_data['status'] == 200 and self.data['connection'] != 'connected':
-                        set_device_status(self.data['id'], 'connected')
+                    logger.error('Get device<%s>(%s) version failed.' % (data['name'], data['ip']))
+                break
+    if not failed_time:
+        if data['connection'] != 'unconnected':
+            set_device_status(data['id'], 'unconnected')
+    else:
+        if os.path.exists(os.path.join(extension_path, "monitoring")):
+            if data['protocol'] == 'xmlrpc' or "nsae" in data['type'].lower() or "netopti" in data['type'].lower():
+                response_data = send_cli_to_device(data, "show snmp", mode='enable')
+                if response_data['message'] == 'success':
                     try:
-                        rest_body = json.loads(rest_response_data['body'])
-                        set_device_version(self.data['id'], rest_body['SystemInfo']['version'])
+                        result = cli_parse(response_data['data'], [
+                            RegexParser('snmp (?P<enable_snmp>on|off)', MATCHONE),
+                            RegexParser('snmp on (?P<snmp_version>default|v3)', MATCHONE),
+                            RegexParser('snmp ipcontrol (?P<ipcontrol>on|off)', MATCHONE),
+                            RegexParser('snmp community "?(?P<community>.*?)"?\n', MATCHONE),
+                            RegexParser('snmp contact "?(?P<contact>.*?)"?\n', MATCHONE),
+                            RegexParser('snmp location "?(?P<location>.*?)"?\n', MATCHONE),
+                            RegexParser('snmp v3user (?P<v3user>.*?)\n', MATCHONE)])
+                        if result[1]:
+                            snmp_version = 'default' if result[1]['snmp_version'] == 'default' else 'v3'
+                        else:
+                            snmp_version = 'default'
+                        snmp_general = {
+                            "snmp_version": snmp_version,
+                            "snmp_enable": True if result[0]['enable_snmp'] == 'on' else False,
+                            "community": result[3]['community']
+                        }
+                        if result[6]:
+                            snmp_general["v3user"] = result[6]['v3user'].replace('\"', '')
+                        set_device_snmp(data['id'], json.dumps(snmp_general))
                     except Exception, e:
-                        logger.error('Get device<%s>(%s) version failed.' % (self.data['name'], self.data['ip']))
-                    break
-        if not failed_time:
-            if self.data['connection'] != 'unconnected':
-                set_device_status(self.data['id'], 'unconnected')
-        else:
-            if os.path.exists(os.path.join(extension_path, "monitoring")):
-                if self.data['protocol'] == 'xmlrpc' or "nsae" in self.data['type'].lower() or "netopti" in self.data['type'].lower():
-                    response_data = send_cli_to_device(self.data, "show snmp", mode='enable')
-                    if response_data['message'] == 'success':
-                        try:
-                            result = cli_parse(response_data['data'], [
-                                RegexParser('snmp (?P<enable_snmp>on|off)', MATCHONE),
-                                RegexParser('snmp on (?P<snmp_version>default|v3)', MATCHONE),
-                                RegexParser('snmp ipcontrol (?P<ipcontrol>on|off)', MATCHONE),
-                                RegexParser('snmp community "?(?P<community>.*?)"?\n', MATCHONE),
-                                RegexParser('snmp contact "?(?P<contact>.*?)"?\n', MATCHONE),
-                                RegexParser('snmp location "?(?P<location>.*?)"?\n', MATCHONE),
-                                RegexParser('snmp v3user (?P<v3user>.*?)\n', MATCHONE)])
-                            if result[1]:
-                                snmp_version = 'default' if result[1]['snmp_version'] == 'default' else 'v3'
-                            else:
-                                snmp_version = 'default'
-                            snmp_general = {
-                                "snmp_version": snmp_version,
-                                "snmp_enable": True if result[0]['enable_snmp'] == 'on' else False,
-                                "community": result[3]['community']
-                            }
-                            if result[6]:
-                                snmp_general["v3user"] = result[6]['v3user'].replace('\"', '')
-                            set_device_snmp(self.data['id'], json.dumps(snmp_general))
-                        except Exception, e:
-                            logger.error('Set device<%s>(%s) snmp failed. %s' % (self.data['name'], self.data['ip'], str(e)))
-                    else:
-                        logger.error('Get device<%s>(%s) SNMPGeneral failed. %s' % (self.data['name'], self.data['ip'], str(e)))
+                        logger.error('Set device<%s>(%s) snmp failed. %s' % (data['name'], data['ip'], str(e)))
                 else:
-                    if self.data['type'].lower() in ADC_TYPE_LIST + WAF_TYPE_LIST:
-                        try:
-                            new_url = modify_url('/rest/device_type/admintools/snmp/SNMPGeneral/general', self.data['type'])
-                            rest_response_data = send_https_rest_request('GET', new_url, '', self.data['ip'], self.data['restapi_port'], self.data['restapi_username'], self.data['restapi_password'])
-                        except Exception, e:
-                            logger.error('Get device<%s>(%s) SNMPGeneral failed. %s' % (self.data['name'], self.data['ip'], str(e)))
-                        else:
-                            if rest_response_data['status'] == 200:
-                                rest_body = json.loads(rest_response_data['body'])
-                                try:
-                                    if 'SNMPGeneral' in rest_body:
-                                        snmp_general = {}
-                                        if 'snmp_version' in rest_body['SNMPGeneral']:
-                                            snmp_general['snmp_version'] = rest_body['SNMPGeneral']['snmp_version']
-                                        if 'enable_snmp' in rest_body['SNMPGeneral']:
-                                            snmp_general['snmp_enable'] = rest_body['SNMPGeneral']['enable_snmp']
-                                        if 'snmp_enable' in rest_body['SNMPGeneral']:
-                                            snmp_general['snmp_enable'] = rest_body['SNMPGeneral']['snmp_enable']
-                                        if 'community' in rest_body['SNMPGeneral']:
-                                            snmp_general['community'] = rest_body['SNMPGeneral']['community']
-                                        if 'v3_user' in rest_body['SNMPGeneral']:
-                                            for user in rest_body['SNMPGeneral']['v3_user']:
-                                                if 'username' in user and 'auth_password' in user and 'level' in user:
-                                                    v3user = user['username']+' '+user['auth_password']+' '+user['level']
-                                                    if 'priv_password' in user:
-                                                        v3user += ' '+user['priv_password']
-                                                    snmp_general['v3user'] = v3user
-                                                break
-                                        set_device_snmp(self.data['id'], json.dumps(snmp_general))
-                                except Exception, e:
-                                    logger.error('Set device<%s>(%s) snmp failed. %s' % (self.data['name'], self.data['ip'], str(e)))
-                    elif self.data['type'].lower() in VPN_TYPE_LIST:
-                        # Failed to query the snmp general info from FieldGroup `general` in current AG versions.
-                        # It should add more judgment when the bug is fixed in new AG version. But the old version
-                        # will always query the snmp info follow below.
-                        snmp_general = {}
-                        try:
-                            new_url = modify_url('/rest/device_type/admintools/snmp/SNMPGeneral/snmp_version', self.data['type'])
-                            rest_response_data = send_https_rest_request('GET', new_url, '', self.data['ip'], self.data['restapi_port'], self.data['restapi_username'], self.data['restapi_password'])
-                        except Exception, e:
-                            logger.error('Get device<%s>(%s) SNMP version failed. %s' % (self.data['name'], self.data['ip'], str(e)))
-                        else:
-                            if rest_response_data['status'] == 200:
-                                rest_body = json.loads(rest_response_data['body'])
-                                try:
-                                    if 'SNMPGeneral' in rest_body:
-                                        if 'snmp_version' in rest_body['SNMPGeneral']:
-                                            snmp_general['snmp_version'] = rest_body['SNMPGeneral']['snmp_version']
-                                except Exception, e:
-                                    logger.error('Set device<%s>(%s) snmp version failed. %s' % (self.data['name'], self.data['ip'], str(e)))
+                    logger.error('Get device<%s>(%s) SNMPGeneral failed. %s' % (data['name'], data['ip'], str(e)))
+            else:
+                if data['type'].lower() in ADC_TYPE_LIST + WAF_TYPE_LIST:
+                    try:
+                        new_url = modify_url('/rest/device_type/admintools/snmp/SNMPGeneral/general', data['type'])
+                        rest_response_data = send_https_rest_request('GET', new_url, '', data['ip'], data['restapi_port'], data['restapi_username'], data['restapi_password'])
+                    except Exception, e:
+                        logger.error('Get device<%s>(%s) SNMPGeneral failed. %s' % (data['name'], data['ip'], str(e)))
+                    else:
+                        if rest_response_data['status'] == 200:
+                            rest_body = json.loads(rest_response_data['body'])
+                            try:
+                                if 'SNMPGeneral' in rest_body:
+                                    snmp_general = {}
+                                    if 'snmp_version' in rest_body['SNMPGeneral']:
+                                        snmp_general['snmp_version'] = rest_body['SNMPGeneral']['snmp_version']
+                                    if 'enable_snmp' in rest_body['SNMPGeneral']:
+                                        snmp_general['snmp_enable'] = rest_body['SNMPGeneral']['enable_snmp']
+                                    if 'snmp_enable' in rest_body['SNMPGeneral']:
+                                        snmp_general['snmp_enable'] = rest_body['SNMPGeneral']['snmp_enable']
+                                    if 'community' in rest_body['SNMPGeneral']:
+                                        snmp_general['community'] = rest_body['SNMPGeneral']['community']
+                                    if 'v3_user' in rest_body['SNMPGeneral']:
+                                        for user in rest_body['SNMPGeneral']['v3_user']:
+                                            if 'username' in user and 'auth_password' in user and 'level' in user:
+                                                v3user = user['username']+' '+user['auth_password']+' '+user['level']
+                                                if 'priv_password' in user:
+                                                    v3user += ' '+user['priv_password']
+                                                snmp_general['v3user'] = v3user
+                                            break
+                                    set_device_snmp(data['id'], json.dumps(snmp_general))
+                            except Exception, e:
+                                logger.error('Set device<%s>(%s) snmp failed. %s' % (data['name'], data['ip'], str(e)))
+                elif data['type'].lower() in VPN_TYPE_LIST:
+                    # Failed to query the snmp general info from FieldGroup `general` in current AG versions.
+                    # It should add more judgment when the bug is fixed in new AG version. But the old version
+                    # will always query the snmp info follow below.
+                    snmp_general = {}
+                    try:
+                        new_url = modify_url('/rest/device_type/admintools/snmp/SNMPGeneral/snmp_version', data['type'])
+                        rest_response_data = send_https_rest_request('GET', new_url, '', data['ip'], data['restapi_port'], data['restapi_username'], data['restapi_password'])
+                    except Exception, e:
+                        logger.error('Get device<%s>(%s) SNMP version failed. %s' % (data['name'], data['ip'], str(e)))
+                    else:
+                        if rest_response_data['status'] == 200:
+                            rest_body = json.loads(rest_response_data['body'])
+                            try:
+                                if 'SNMPGeneral' in rest_body:
+                                    if 'snmp_version' in rest_body['SNMPGeneral']:
+                                        snmp_general['snmp_version'] = rest_body['SNMPGeneral']['snmp_version']
+                            except Exception, e:
+                                logger.error('Set device<%s>(%s) snmp version failed. %s' % (data['name'], data['ip'], str(e)))
 
-                        try:
-                            new_url = modify_url('/rest/device_type/admintools/snmp/SNMPGeneral/snmp_enable', self.data['type'])
-                            rest_response_data = send_https_rest_request('GET', new_url, '', self.data['ip'], self.data['restapi_port'], self.data['restapi_username'], self.data['restapi_password'])
-                        except Exception, e:
-                            logger.error('Get device<%s>(%s) SNMP enable failed. %s' % (self.data['name'], self.data['ip'], str(e)))
-                        else:
-                            if rest_response_data['status'] == 200:
-                                rest_body = json.loads(rest_response_data['body'])
-                                try:
-                                    if 'SNMPGeneral' in rest_body:
-                                        if 'snmp_enable' in rest_body['SNMPGeneral']:
-                                            snmp_general['snmp_enable'] = rest_body['SNMPGeneral']['snmp_enable']
-                                except Exception, e:
-                                    logger.error('Set device<%s>(%s) snmp enable failed. %s' % (self.data['name'], self.data['ip'], str(e)))
+                    try:
+                        new_url = modify_url('/rest/device_type/admintools/snmp/SNMPGeneral/snmp_enable', data['type'])
+                        rest_response_data = send_https_rest_request('GET', new_url, '', data['ip'], data['restapi_port'], data['restapi_username'], data['restapi_password'])
+                    except Exception, e:
+                        logger.error('Get device<%s>(%s) SNMP enable failed. %s' % (data['name'], data['ip'], str(e)))
+                    else:
+                        if rest_response_data['status'] == 200:
+                            rest_body = json.loads(rest_response_data['body'])
+                            try:
+                                if 'SNMPGeneral' in rest_body:
+                                    if 'snmp_enable' in rest_body['SNMPGeneral']:
+                                        snmp_general['snmp_enable'] = rest_body['SNMPGeneral']['snmp_enable']
+                            except Exception, e:
+                                logger.error('Set device<%s>(%s) snmp enable failed. %s' % (data['name'], data['ip'], str(e)))
 
-                        try:
-                            new_url = modify_url('/rest/device_type/admintools/snmp/SNMPGeneral/community', self.data['type'])
-                            rest_response_data = send_https_rest_request('GET', new_url, '', self.data['ip'], self.data['restapi_port'], self.data['restapi_username'], self.data['restapi_password'])
-                        except Exception, e:
-                            logger.error('Get device<%s>(%s) SNMP community failed. %s' % (self.data['name'], self.data['ip'], str(e)))
-                        else:
-                            if rest_response_data['status'] == 200:
-                                rest_body = json.loads(rest_response_data['body'])
-                                try:
-                                    if 'SNMPGeneral' in rest_body:
-                                        if 'community' in rest_body['SNMPGeneral']:
-                                            snmp_general['community'] = rest_body['SNMPGeneral']['community']
-                                except Exception, e:
-                                    logger.error('Set device<%s>(%s) snmp community failed. %s' % (self.data['name'], self.data['ip'], str(e)))
-                        try:
-                            new_url = modify_url('/rest/device_type/admintools/snmp/SNMPGeneral/v3_user', self.data['type'])
-                            rest_response_data = send_https_rest_request('GET', new_url, '', self.data['ip'], self.data['restapi_port'], self.data['restapi_username'], self.data['restapi_password'])
-                        except Exception, e:
-                            logger.error('Get device<%s>(%s) SNMP v3_user failed. %s' % (self.data['name'], self.data['ip'], str(e)))
-                        else:
-                            if rest_response_data['status'] == 200:
-                                rest_body = json.loads(rest_response_data['body'])
-                                try:
-                                    if 'SNMPGeneral' in rest_body:
-                                        if 'v3_user' in rest_body['SNMPGeneral']:
-                                            for user in rest_body['SNMPGeneral']['v3_user']:
-                                                if 'username' in user and 'auth_password' in user and 'level' in user:
-                                                    v3user = user['username']+' '+user['auth_password']+' '+user['level']
-                                                    if 'priv_password' in user:
-                                                        v3user += ' '+user['priv_password']
-                                                    snmp_general['v3user'] = v3user
-                                                break
-                                except Exception, e:
-                                    logger.error('Set device<%s>(%s) snmp v3_user failed. %s' % (self.data['name'], self.data['ip'], str(e)))
-                        try:
-                            set_device_snmp(self.data['id'], json.dumps(snmp_general))
-                        except Exception, e:
-                            logger.error('Set device<%s>(%s) snmp failed. %s' % (self.data['name'], self.data['ip'], str(e)))
+                    try:
+                        new_url = modify_url('/rest/device_type/admintools/snmp/SNMPGeneral/community', data['type'])
+                        rest_response_data = send_https_rest_request('GET', new_url, '', data['ip'], data['restapi_port'], data['restapi_username'], data['restapi_password'])
+                    except Exception, e:
+                        logger.error('Get device<%s>(%s) SNMP community failed. %s' % (data['name'], data['ip'], str(e)))
+                    else:
+                        if rest_response_data['status'] == 200:
+                            rest_body = json.loads(rest_response_data['body'])
+                            try:
+                                if 'SNMPGeneral' in rest_body:
+                                    if 'community' in rest_body['SNMPGeneral']:
+                                        snmp_general['community'] = rest_body['SNMPGeneral']['community']
+                            except Exception, e:
+                                logger.error('Set device<%s>(%s) snmp community failed. %s' % (data['name'], data['ip'], str(e)))
+                    try:
+                        new_url = modify_url('/rest/device_type/admintools/snmp/SNMPGeneral/v3_user', data['type'])
+                        rest_response_data = send_https_rest_request('GET', new_url, '', data['ip'], data['restapi_port'], data['restapi_username'], data['restapi_password'])
+                    except Exception, e:
+                        logger.error('Get device<%s>(%s) SNMP v3_user failed. %s' % (data['name'], data['ip'], str(e)))
+                    else:
+                        if rest_response_data['status'] == 200:
+                            rest_body = json.loads(rest_response_data['body'])
+                            try:
+                                if 'SNMPGeneral' in rest_body:
+                                    if 'v3_user' in rest_body['SNMPGeneral']:
+                                        for user in rest_body['SNMPGeneral']['v3_user']:
+                                            if 'username' in user and 'auth_password' in user and 'level' in user:
+                                                v3user = user['username']+' '+user['auth_password']+' '+user['level']
+                                                if 'priv_password' in user:
+                                                    v3user += ' '+user['priv_password']
+                                                snmp_general['v3user'] = v3user
+                                            break
+                            except Exception, e:
+                                logger.error('Set device<%s>(%s) snmp v3_user failed. %s' % (data['name'], data['ip'], str(e)))
+                    try:
+                        set_device_snmp(data['id'], json.dumps(snmp_general))
+                    except Exception, e:
+                        logger.error('Set device<%s>(%s) snmp failed. %s' % (data['name'], data['ip'], str(e)))
 
 def check_device_status():
+    db = None
     try:
-        db = DB.get_connected_db()        
-        select_sql = "SELECT id, name, ip_address, protocol, restapi_port, restapi_username, restapi_password, type, enable_password, connection FROM device"
-        data = db.fetchall(select_sql)
-        db.close()
+        try:
+            db = DB.get_connected_db()        
+            select_sql = "SELECT id, name, ip_address, protocol, restapi_port, restapi_username, restapi_password, type, enable_password, connection FROM device"
+            data = db.fetchall(select_sql)
+        finally:
+            if db:
+                db.close()
     except Exception, e:
         # select from db failed, mostly because avx extension is not installed.
         return
     key = ['id', 'name', 'ip', 'protocol', 'restapi_port', 'restapi_username', 'restapi_password', 'type', 'enable_password', 'connection']
     result = [dict(zip(key, each)) for each in data]
     for each in result:
-        t = DeviceStatusThread(each)
-        t.start()
+        device_check_pool.apply_async(check_one_device_status, (each,))
 '''
 end check device status
 '''
@@ -2213,61 +2218,57 @@
     db.execute_sql(update_sql)
     db.close()
 
-class HostInfoThread(threading.Thread):
-    def __init__(self, data):
-        super(HostInfoThread, self).__init__()
-        self.data = data
+def check_one_host_status(data):
+    try:
+        rest_response_data = call_restapi('GET', '/rest/avx/system/SystemInfo/version', '', data['ip'], data['restapi_port'], data['restapi_username'], data['restapi_password'], 'https')      
+    except Exception, e:
+        logger.error('Get host<%s> version failed. %s' % (data['ip'], str(e)))
+        set_host_connection(data['id'], 'Unconnected')
+        mark_expire_all(get_model('cm', ['virtualization', 'Host']))
+    else:
+        if rest_response_data['status'] == 200:
+            set_host_connection(data['id'], 'Connected')
+            set_host_version(data['id'], json.loads(rest_response_data['body'])['SystemInfo']['version'])
+            # get cpu/memory/disk data
+            try:
+                rest_response_data = call_restapi('GET', '/rest/avx/system/SystemStatus/status', '', data['ip'], data['restapi_port'], data['restapi_username'], data['restapi_password'], 'https')
+            except Exception, e:
+                logger.error('Get host<%s> cpu/memory/disk usage failed. %s' % (data['ip'], str(e)))
+                mark_expire_all(get_model('cm', ['virtualization', 'Host']))
+            else:
+                if rest_response_data['status'] == 200:
+                    set_host_sys_status(data['id'], json.loads(rest_response_data['body'])['SystemStatus'])
+                mark_expire_all(get_model('cm', ['virtualization', 'Host']))
+            #get vm numbers
+            try:
+                rest_response_data = call_restapi('GET', '/rest/avx/va/instance/VAInstance/_get_list_data', '', data['ip'], data['restapi_port'], data['restapi_username'], data['restapi_password'], 'https')
+            except Exception, e:
+                logger.error('Get host<%s> vm numbers failed. %s' % (data['ip'], str(e)))
+                mark_expire_all(get_model('cm', ['virtualization', 'Host']))
+            else:
+                if rest_response_data['status'] == 200:
+                    set_host_vm_number(data['id'], len(json.loads(rest_response_data['body'])['VAInstance']))
+                mark_expire_all(get_model('cm', ['virtualization', 'Host']))
+        mark_expire_all(get_model('cm', ['virtualization', 'Host']))
 
-    def run(self):
-        try:
-            rest_response_data = call_restapi('GET', '/rest/avx/system/SystemInfo/version', '', self.data['ip'], self.data['restapi_port'], self.data['restapi_username'], self.data['restapi_password'], 'https')      
-        except Exception, e:
-            logger.error('Get host<%s> version failed. %s' % (self.data['ip'], str(e)))
-            set_host_connection(self.data['id'], 'Unconnected')
-            mark_expire_all(get_model('cm', ['virtualization', 'Host']))
-        else:
-            if rest_response_data['status'] == 200:
-                set_host_connection(self.data['id'], 'Connected')
-                set_host_version(self.data['id'], json.loads(rest_response_data['body'])['SystemInfo']['version'])
-                # get cpu/memory/disk data
-                try:
-                    rest_response_data = call_restapi('GET', '/rest/avx/system/SystemStatus/status', '', self.data['ip'], self.data['restapi_port'], self.data['restapi_username'], self.data['restapi_password'], 'https')
-                except Exception, e:
-                    logger.error('Get host<%s> cpu/memory/disk usage failed. %s' % (self.data['ip'], str(e)))
-                    mark_expire_all(get_model('cm', ['virtualization', 'Host']))
-                else:
-                    if rest_response_data['status'] == 200:
-                        set_host_sys_status(self.data['id'], json.loads(rest_response_data['body'])['SystemStatus'])
-                    mark_expire_all(get_model('cm', ['virtualization', 'Host']))
-                #get vm numbers
-                try:
-                    rest_response_data = call_restapi('GET', '/rest/avx/va/instance/VAInstance/_get_list_data', '', self.data['ip'], self.data['restapi_port'], self.data['restapi_username'], self.data['restapi_password'], 'https')
-                except Exception, e:
-                    logger.error('Get host<%s> vm numbers failed. %s' % (self.data['ip'], str(e)))
-                    mark_expire_all(get_model('cm', ['virtualization', 'Host']))
-                else:
-                    if rest_response_data['status'] == 200:
-                        set_host_vm_number(self.data['id'], len(json.loads(rest_response_data['body'])['VAInstance']))
-                    mark_expire_all(get_model('cm', ['virtualization', 'Host']))
-            mark_expire_all(get_model('cm', ['virtualization', 'Host']))
 
-
 def check_host_status():
+    db = None
     try:
-        db = DB.get_connected_db()        
-        select_sql = "SELECT id, name, ip, restapi_port, restapi_username, restapi_password, connection FROM host"
-        data = db.fetchall(select_sql)
-        db.close()
+        try:
+            db = DB.get_connected_db()        
+            select_sql = "SELECT id, name, ip, restapi_port, restapi_username, restapi_password, connection FROM host"
+            data = db.fetchall(select_sql)
+        finally:
+            if db:
+                db.close()
     except:
         # select from db failed, mostly because avx extension is not installed.
-        if db:
-            db.close()
         return
     key = ['id', 'name', 'ip', 'restapi_port', 'restapi_username', 'restapi_password', 'connection']
     result = [dict(zip(key, each)) for each in data]
     for each in result:
-        t = HostInfoThread(each)
-        t.start()
+        device_check_pool.apply_async(check_one_host_status, (each,))
 
 
 def scheduler_error_listener(event):
