Index: /branches/rel_apv_10_7/usr/click/bin/global_monitord/monitor.c
===================================================================
--- /branches/rel_apv_10_7/usr/click/bin/global_monitord/monitor.c	(revision 39742)
+++ /branches/rel_apv_10_7/usr/click/bin/global_monitord/monitor.c	(working copy)
@@ -98,6 +98,11 @@
 int quick_timeout;
 int kq;
 
+#define SSL_CARD_DOWNTIMES 5
+int qat_pci_count = 0;
+int cavium_pci_count = 0;
+int ssl_pci_down_cnt = 0;
+
 extern int monitor_get_ntp_status();
 extern int monitor_ntp_status_mon(void);
 extern int	monitor_cpu_utilization_mon(void);
@@ -2569,10 +2574,17 @@
 			}
 		}
 
+		/*TWSD-1078, count DOWN if the number of running cards is lower than detected cards on the pci bus*/
+		if (qat_pci_count > qat_card_count){
+			ssl_pci_down_cnt = (ssl_pci_down_cnt >= SSL_CARD_DOWNTIMES)? SSL_CARD_DOWNTIMES: ssl_pci_down_cnt + 1;
+		} else {
+			ssl_pci_down_cnt = 0;
+		}
+
 		/* if one qat node down consider the qat card as down */
-		if (qat_node_down_num) {
+		if (qat_node_down_num || ssl_pci_down_cnt >= SSL_CARD_DOWNTIMES) {
 			handle_local_condition_state_change(LOCAL_UNIT, SSLCARD_START_ID, CONDITION_DOWN);
-		} else {
+		} else if (ssl_pci_down_cnt == 0) {
 			handle_local_condition_state_change(LOCAL_UNIT, SSLCARD_START_ID, CONDITION_UP);
 		}
 	#endif
@@ -2613,13 +2625,20 @@
 	len = sizeof(cavium_n5_hang);
 	u_sysctlbyname("kern.ssl.cavium_n5_hang", &cavium_n5_hang, &len, NULL, 0);
 
-	if (cavium_n5_sslcard->down_cnt >= cavium_n5_sslcard->down_times || cavium_n5_hang == 1) {
+	/*TWSD-1078, count DOWN if the number of running cards is lower than detected cards on the pci bus*/
+	if (cavium_pci_count > nitrox_v_count){
+		ssl_pci_down_cnt = (ssl_pci_down_cnt >= SSL_CARD_DOWNTIMES)? SSL_CARD_DOWNTIMES: ssl_pci_down_cnt + 1;
+	} else {
+		ssl_pci_down_cnt = 0;
+	}
+
+	if (cavium_n5_sslcard->down_cnt >= cavium_n5_sslcard->down_times || cavium_n5_hang == 1 || ssl_pci_down_cnt >= SSL_CARD_DOWNTIMES) {
 		handle_local_condition_state_change(LOCAL_UNIT, SSLCARD_START_ID, CONDITION_DOWN);
 		force_ssl = 1;
 		len = sizeof(force_ssl);
 		u_sysctlbyname("kern.ssl.force_sw_ssl", NULL, 0, (void *)&force_ssl, len);
 		
-	} else {
+	} else if (ssl_pci_down_cnt == 0) {
 		handle_local_condition_state_change(LOCAL_UNIT, SSLCARD_START_ID, CONDITION_UP);
 		force_ssl = 0;
 		len = sizeof(force_ssl);
@@ -2629,6 +2648,65 @@
 	return;
 }
 
+#define INTEL_VENDORID "8086"
+/*
+DH895_DEVICE_PCI_ID="0435"
+DH895_DEVICE_PCI_ID_VM="0443"
+C62X_DEVICE_PCI_ID="37c8"
+C62X_DEVICE_PCI_ID_VM="37c9"
+C3XX_DEVICE_PCI_ID="19e2"
+C3XX_DEVICE_PCI_ID_VM="19e3"
+QAT_200XX_DEVICE_PCI_ID="18ee"
+QAT_200XX_DEVICE_PCI_ID_VM="18ef"
+D15XX_DEVICE_PCI_ID="6f54"
+D15XX_DEVICE_PCI_ID_VM="6f55"
+C4XX_DEVICE_PCI_ID="18a0"
+C4XX_DEVICE_PCI_ID_VM="18a1"
+*/
+/*TWSD-1078, get ssl card's number on pci bus*/
+static int count_ssl_pci(void)
+{
+	char buf[32];
+	const char* count_qat_pci_cmd = "lspci -n | egrep -c '"
+		INTEL_VENDORID":0435|"
+		INTEL_VENDORID":0443|"
+		INTEL_VENDORID":37c8|"
+		INTEL_VENDORID":37c9|"
+		INTEL_VENDORID":19e2|"
+		INTEL_VENDORID":19e3|"
+		INTEL_VENDORID":18ee|"
+		INTEL_VENDORID":18ef|"
+		INTEL_VENDORID":6f54|"
+		INTEL_VENDORID":6f55|"
+		INTEL_VENDORID":18a0|"
+		INTEL_VENDORID":18a1'";
+
+	FILE *fp = NULL;
+	fp = popen(count_qat_pci_cmd, "r");
+	if (fp == NULL) {
+		printf("run script error\n");
+		return 0;
+	}
+
+	if (fgets(buf, sizeof(buf), fp) != NULL) {
+		qat_pci_count = atoi(buf);
+	}
+	pclose(fp);
+
+	const char* count_cavium_pci_cmd = "lspci -n | egrep -c '177d'";
+	fp = popen(count_cavium_pci_cmd, "r");
+	if (fp == NULL) {
+		printf("run script error\n");
+		return 0;
+	}
+
+	if (fgets(buf, sizeof(buf), fp) != NULL) {
+		cavium_pci_count = atoi(buf);
+	}
+	pclose(fp);
+	return 1;
+}
+
 static void	monitor_sslcard_mon_immediate(void)
 {
 	ssl_card_t *sc;
@@ -2645,14 +2723,16 @@
 		return;
 	}
 
-	if (qat_card_count > 0) {
+	if (!count_ssl_pci()) return;
+
+	if (qat_pci_count > 0) {
 		monitor_sslcard_qat_mon();
 		return;
 	}
 
-	if (nitrox_v_count > 0) {
+	if (cavium_pci_count > 0) {
 		monitor_sslcard_n5_mon();
-		return 0;
+		return;
 	}
 	switch(*uland_ha_ssltest_status) {
 		case HA_SSL_TEST_CARD_TESTING:
@@ -2730,13 +2810,15 @@
 	if (sc->interval == 0) {
 		return 0;
 	}
-	
-	if (qat_card_count > 0) {
+
+	if (!count_ssl_pci()) return 0;
+
+	if (qat_pci_count > 0) {
 		monitor_sslcard_qat_mon();
 		return 0;
 	}
 
-	if (nitrox_v_count > 0) {
+	if (cavium_pci_count > 0) {
 		monitor_sslcard_n5_mon();
 		return 0;
 	}
@@ -2918,7 +3000,7 @@
 	mon_gateway_t *gateway;
 
 	monitor_init_kern();
-	monitor_config_p->monitor_enable = MONITOR_SYSTEM_ON;
+	monitor_config_p->monitor_enable = ~MONITOR_SYSTEM_ON;
 	monitor_config_p->monitor_immediate = ~MONITOR_IMMEDIATE_ON;
 	
 //	monitor_default_script("monitor_interval_set");
@@ -3083,6 +3165,8 @@
 				if (user_configure_load_finished == 1) {
 					mon_log(LOG_NOTICE, "The user configure load finised.\n");
 					monitor_config_p->monitor_immediate = ~MONITOR_IMMEDIATE_ON;
+					/*TWSD-1078, initialize all status again when configuration is set successfully*/
+					system_monitor_init_state();
 					monitor_update_condition_immediate();
 				} else {
 					mon_log(LOG_NOTICE, "The user configure not load finised.\n");
@@ -3103,7 +3187,7 @@
 		kev_retry = KEV_RETRY_TIMES;
 
 		if(e.filter == EVFILT_TIMER) {
-			if (e.ident & TIMER_SLOWTIMEOUT) {
+			if (e.ident & TIMER_SLOWTIMEOUT && user_configure_load_finished) {
 				/*
 				 * SLOW TIMEOUT time up in every 500ms by default
 				 */
Index: /branches/rel_apv_10_7/usr/click/lib/libca_snmp_mib/CA-SNMP-MIB.txt
===================================================================
--- /branches/rel_apv_10_7/usr/click/lib/libca_snmp_mib/CA-SNMP-MIB.txt	(revision 39742)
+++ /branches/rel_apv_10_7/usr/click/lib/libca_snmp_mib/CA-SNMP-MIB.txt	(working copy)
@@ -2605,6 +2605,14 @@
             "current system temperature"
     ::= { monitor 15 }
 
+sslHWStatus            OBJECT-TYPE
+    SYNTAX      INTEGER(0..255)
+    MAX-ACCESS      read-only
+    STATUS      current
+    DESCRIPTION
+            "current status of ssl hardware (0 means INIT, 1 means DOWN, 2 means UP)"
+    ::= { monitor 16 }
+
 --- End of beijingArrayNetworks monitor Statistics MIB
 
 --- beijingArrayNetworks Sdns Statistics MIB
Index: /branches/rel_apv_10_7/usr/click/lib/libca_snmp_mib/monitor.c
===================================================================
--- /branches/rel_apv_10_7/usr/click/lib/libca_snmp_mib/monitor.c	(revision 39742)
+++ /branches/rel_apv_10_7/usr/click/lib/libca_snmp_mib/monitor.c	(working copy)
@@ -45,6 +45,19 @@
 #define CHIPSET_ID_CAR5060		0x00005060 /*CASELL CAR-5060 MB for APV8900*/
 #define CHIPSET_ID_CAR5070		0x00005070 /*CASWELL CAR-5070 MB*/
 
+#define COND_NAME_LEN		128
+#define COND_MAX_IN_TABLE	256
+typedef struct _condition_t {
+    char        name[COND_NAME_LEN + 1]; /*strlen("Local")+NAME+1*/
+    char        name_no_id[COND_NAME_LEN + 1];
+    uint32_t    id;
+	uint32_t    total;
+    uint8_t	curr_status;
+    uint8_t	last_status;
+
+    void *ext;                 /* external condition information */
+} condition_t;
+
 oid monitor_variables_oid[] = { 1,3,6,1,4,1,ARRAY_COMPINFO_MIBOID,32 };
 
 /* 
@@ -100,6 +113,9 @@
 #define SYSTEMP             15
 {SYSTEMP,       ASN_INTEGER,  NETSNMP_OLDAPI_RONLY,
  var_monitor, 1,  { 15 }},
+#define SSLHWSTATUS           16
+{SSLHWSTATUS,       ASN_INTEGER,  NETSNMP_OLDAPI_RONLY,
+ var_monitor, 1,  { 16 }},
 };
 /*    (L = length of the oidsuffix) */
 
@@ -153,6 +169,13 @@
     int len, ret;
     uint32_t idx;
 
+	int i;
+	condition_t	*condition_table;
+	kern_monitor_len = sizeof(condition_table);
+	if (sysctlbyname("net.inet.clicktcp.condition_table_kern", &condition_table, &kern_monitor_len, NULL, 0) < 0) {
+		return NULL;
+	}
+
     kern_monitor_len = sizeof(kernmonitor);
 #if defined(__linux__)
     if(sysctlbyname("kern.clickosmonitor",&kernmonitor, &kern_monitor_len,NULL,0) == -1){
@@ -481,7 +504,18 @@
 			long_ret = 0;
 		}
 	return ( u_char *)&long_ret;
-    default:
+
+	/*TWSD-1078, ssl card's status: 0 means INIT, 1 means DOWN, 2 means UP*/
+	case SSLHWSTATUS:
+		long_ret = 0;
+		for (i = 0; i < COND_MAX_IN_TABLE; i++) {
+			if (strcmp(condition_table[i].name, "SSL_CARD") == 0) {
+				long_ret = condition_table[i].curr_status;
+				break;
+			}
+		}
+		return (u_char *)&long_ret;
+	default:
       ERROR_MSG("");
     }
     return NULL;
Index: /branches/rel_apv_10_7/usr/click/lib/libglobal_monitor/monitor_cli.c
===================================================================
--- /branches/rel_apv_10_7/usr/click/lib/libglobal_monitor/monitor_cli.c	(revision 39742)
+++ /branches/rel_apv_10_7/usr/click/lib/libglobal_monitor/monitor_cli.c	(working copy)
@@ -160,10 +160,10 @@
 	ssl_card_t *sc;
 	int i;
 
-	if ((cavium_count <= 0 && nitrox_v_count <= 0 && qat_card_count <=0) || interval == 0) {
+	// TWSD-1078, the check of ssl cards count is moved global_monitord/monitor.c
+	if (interval == 0) {
 		/* 0 means don't monitor SSL card*/
 		no_monitor_system_sslcard();
-		printf("No SSL card available\n");
 		return 0;
 	}
 
@@ -975,9 +975,18 @@
 
 int monitor_system_on(void)
 {
-	monitor_config_p->monitor_enable = MONITOR_SYSTEM_ON;
-	monitor_config_p->monitor_immediate = MONITOR_IMMEDIATE_ON;
-
+	/*TWSD-1078, because monitor_immediate turns to ON will reset all status,
+	only when monitor_enable is OFF or in booting configuration can turn ON*/
+	uint32_t user_configure_load_finished = 0;
+	size_t len = sizeof(user_configure_load_finished);
+	if (sysctlbyname("kern.user_configure_load_finished", &user_configure_load_finished, &len, NULL, 0) < 0) {
+		printf("Error: get kernel configration failed\n");
+		return 0;
+	}
+	if (monitor_config_p->monitor_enable != MONITOR_SYSTEM_ON || user_configure_load_finished == 0) {
+		monitor_config_p->monitor_enable = MONITOR_SYSTEM_ON;
+		monitor_config_p->monitor_immediate = MONITOR_IMMEDIATE_ON;
+	}
 	return 0;
 }
 
Index: /branches/rel_apv_10_7/usr/click/lib/libglobal_monitor/monitor_lib.h
===================================================================
--- /branches/rel_apv_10_7/usr/click/lib/libglobal_monitor/monitor_lib.h	(revision 39742)
+++ /branches/rel_apv_10_7/usr/click/lib/libglobal_monitor/monitor_lib.h	(working copy)
@@ -74,6 +74,7 @@
 
 extern int mon_log(int level, const char *fmt, ...);
 extern int monitor_init_kern(void);
+extern void system_monitor_init_state(void);
 extern int register_timer(int kq, uint16_t type, int msec, void *udata);
 extern int register_timer_oneshot(int kq, uint16_t type, int msec, void *udata);
 extern int deregister_timer(int kq, int timer);
Index: /branches/rel_apv_10_7/usr/click/lib/libglobal_monitor/monitor_lib.c
===================================================================
--- /branches/rel_apv_10_7/usr/click/lib/libglobal_monitor/monitor_lib.c	(revision 39742)
+++ /branches/rel_apv_10_7/usr/click/lib/libglobal_monitor/monitor_lib.c	(working copy)
@@ -453,6 +453,24 @@
 	return 0;
 }
 
+void system_monitor_init_state(void)
+{
+	struct monitor_entry *monitor = NULL;
+	for (int entry = 0; entry < MONITOR_MAX; entry++) {
+		monitor = &mem_cnt_table[entry];
+		if (0 == strlen(monitor->name))
+			break;
+
+		monitor->cur_statu = CONDITION_INIT;
+		monitor->down_cnt = 0;
+		monitor->up_cnt = 0;
+		monitor->fatal_cnt = 0;
+	}
+	for (int index = 0; index < CONDITION_MAX_ID; index++) {
+		condition_table[index].curr_status = CONDITION_INIT;
+	}
+}
+
 int register_sock_to_listener(int kq, int fd, void *udata)
 {
 	struct kevent event;
Index: /branches/rel_apv_10_7/usr/src/sys/click/app/monitor/monitor_kern.c
===================================================================
--- /branches/rel_apv_10_7/usr/src/sys/click/app/monitor/monitor_kern.c	(revision 39742)
+++ /branches/rel_apv_10_7/usr/src/sys/click/app/monitor/monitor_kern.c	(working copy)
@@ -300,7 +300,7 @@
 	condition_table_init(condition_table_kern, condition_type_table_kern);
 #endif
 
-	monitor_config_p->monitor_enable = MONITOR_SYSTEM_ON;
+	monitor_config_p->monitor_enable = ~MONITOR_SYSTEM_ON;
 
 	monitor_config_p->cpu_util.util = 60;
 	monitor_config_p->cpu_util.interval = 5000;
