Преглед изворни кода

Merge branch 'master' of http://jiecheng2024.tpddns.cn:3000/liyuezong/SmartPDCode

guohui пре 1 година
родитељ
комит
72868507c7
8 измењених фајлова са 316 додато и 100 уклоњено
  1. 60 2
      ip/src/app.c
  2. 58 2
      ip/src/appweb_handle.c
  3. 35 5
      ip/src/common.h
  4. 57 25
      ip/src/json_handle.c
  5. 11 5
      ip/src/json_handle.h
  6. 36 46
      ip/src/pduMIB_trap.c
  7. 8 2
      ip/src/pduMIB_trap.h
  8. 51 13
      ip/src/sqlite_handle.c

+ 60 - 2
ip/src/app.c

@@ -1154,8 +1154,9 @@ void* ntp_thread(void *arg)
 void* snmp_thread(void *arg)
 {
     //int ret = 0 ;
-    thread_handle_t *h=(thread_handle_t*)arg;
-    
+    thread_handle_t *h = (thread_handle_t *)arg;
+    GlobalDeviceManager *dm = (GlobalDeviceManager *)h->arg;
+
     /* we are a subagent */
     char path[PATH_MAX];
     /* we are a subagent */
@@ -1172,6 +1173,12 @@ void* snmp_thread(void *arg)
     
     /* pduMIB will be used to read pduMIB.conf files. */
     init_snmp(path);
+    init_snmp_alarm();
+
+    dev_get_snmp_info(dm->db, &_globalSNMPManager);
+    dev_Alarm_Run_message(dm, language_alarm_Init_Success[0], "SNMP");
+
+
     int reconfig=0;
     while(h->quit==0)
     {
@@ -1470,5 +1477,56 @@ int _global_device_manage_init(GlobalDeviceManager* _globalDeviceManager)
     {
         sleep(20);
     }
+    return 0;
+}
+
+
+int snmp_config(SNMPManager *m)
+{
+    char path[200];
+    char *conf="/snmp/conf/snmpd.conf";
+    char user[100],pass[200],cmd[400],trapinfo[200];
+    sys_get_path(path, conf);
+
+    sprintf(trapinfo, "trap2sink \"%s\" %s \"%s\"", m->nmsIP, "public", "162");
+    //delete
+    sprintf(cmd, "sed -e /^trap2sink.*$/d -i %s", path);
+    system(cmd);
+    sprintf(cmd, "sed -e \"$ a %s\" -i %s", trapinfo, path);
+    system(cmd);
+
+    if (strlen(m->crypt.auth.alg) > 1)
+    {
+        // createUser afafafaf MD5 "md5123456" DES "des123456
+
+        // delete
+        sprintf(cmd, "sed -e /^createUser.*$/d -e /^rouser.*$/d -i %s", path);
+        system(cmd);
+
+        if (m->version == 2)
+        {
+            sprintf(user, "rouser \"%s\"", m->crypt.user);
+            sprintf(pass, "createUser \"%s\" %s \"%s\"", m->crypt.user, m->crypt.auth.alg, m->crypt.auth.passwd);
+            if (strlen(m->crypt.priv.alg) > 1)
+            {
+                sprintf(cmd, " %s \"%s\"", m->crypt.priv.alg, m->crypt.priv.passwd);
+                strcat(pass, cmd);
+            }
+
+            sprintf(cmd, "sed -e \"$ a %s\" -e \"$ a %s\" -i %s", pass, user, path);
+            system(cmd);
+        }
+    }
+
+    system("killall snmpd");
+    sleep(1);
+    char *snmpdorder="/snmp/bin/snmpd";
+    char snmpdpath[200];
+    sys_get_path(snmpdpath, snmpdorder);
+    sprintf(cmd, "%s -Cc %s -Lo", snmpdpath,path);
+    system(cmd);
+    //exit(0);
+    //thread_restart(THREAD_ID_SNMP);
+
     return 0;
 }

+ 58 - 2
ip/src/appweb_handle.c

@@ -3502,6 +3502,7 @@ static void serviceManagement(void *conn)
     }   
     else if(strcmp(_serviceManageRequestInfo.type,"SnmpSave")==0)
     {
+        int len = 0;
         _globalSNMPManager.mode = atoi(_serviceManageRequestInfo._snmp_info.mode);
         if (strcmp(_serviceManageRequestInfo._snmp_info.version,"SNMP V1")==0)
         {
@@ -3520,15 +3521,70 @@ static void serviceManagement(void *conn)
             _globalSNMPManager.version=-1;
         }
     
-        _globalSNMPManager.public= malloc(32);
         strcpy(_globalSNMPManager.public, _serviceManageRequestInfo._snmp_info.public);
         _globalSNMPManager.trapmode = atoi(_serviceManageRequestInfo._snmp_info.trapmode);
+        if (_serviceManageRequestInfo._snmp_info.nms_IP)
+            strcpy(_globalSNMPManager.nmsIP, _serviceManageRequestInfo._snmp_info.nms_IP);
+
+        crypt_t *pc = &_globalSNMPManager.crypt;
+        _SNMP_ServerRequestInfo *info = &_serviceManageRequestInfo._snmp_info;
+
+        if (_globalSNMPManager.version == 2)
+        {
+            if (info->user)
+            {
+                len = strlen(info->user);
+                if (len < 8 || len > 32)
+                {
+                    log_e("snmp user length %d is wrong! correct is [8~32]", len);
+                    return;
+                }
+                strcpy(pc->user, info->user);
+
+                if (info->auth_alg)
+                {
+                    strcpy(pc->auth.alg, info->auth_alg);
+
+                    if (strlen(info->auth_alg) > 1 && info->auth_passwd)
+                    {
+                        len = strlen(info->auth_passwd);
+                        if (len < 8 || len > 32)
+                        {
+                            log_e("snmp auth passwd length %d is wrong! correct is [8~32]", len);
+                            return;
+                        }
+                        strcpy(pc->auth.passwd, info->auth_passwd);
+                    }
+                }
+
+                if (info->priv_alg)
+                {
+                    strcpy(pc->priv.alg, info->priv_alg);
+
+                    if (strlen(info->priv_alg) > 1 && info->priv_passwd)
+                    {
+                        len = strlen(info->priv_passwd);
+                        if (len < 8 || len > 32)
+                        {
+                            log_e("snmp priv passwd length %d is wrong! correct is [8~32]", len);
+                            return;
+                        }
+                        strcpy(pc->priv.passwd, info->priv_passwd);
+                    }
+                }
+            }
+        }
+
         _globalSNMPManager.product_id = __globalDeviceManage._globalDevInfo.product_id;
         if (dev_update_snmp_info(__globalDeviceManage.db,_globalSNMPManager.product_id,&_globalSNMPManager) != 0)
         {
             log_e("update snmp info err.");
         }
-        
+        else
+        {
+            extern int snmp_config(SNMPManager * m);
+            snmp_config(&_globalSNMPManager);
+        }
     }   
     //查询snmp_trap信息
     else if(strcmp(_serviceManageRequestInfo.type,"listStQuery")==0)

+ 35 - 5
ip/src/common.h

@@ -364,13 +364,43 @@ typedef struct
 }NTPManager;
 NTPManager _globalNTPManager;
 
+enum
+{
+    SNMP_AUTH_NONE = 0,
+    SNMP_AUTH_MD5,
+    SNMP_AUTH_SHA,
+
+    SNMP_AUTH_MAX
+};
+enum
+{
+    SNMP_ENCYPT_NONE = 0,
+    SNMP_ENCYPT_DES,
+    SNMP_ENCYPT_AES,
+
+    SNMP_ENCYPT_MAX
+};
 typedef struct
 {
-    int                 product_id;
-    int                 mode;
-    int                 version;
-    char*               public;
-    int                 trapmode;
+    char alg[12];
+    char passwd[64];
+} crypt_data_t;
+typedef struct
+{
+    char user[32];
+    crypt_data_t auth;
+    crypt_data_t priv;
+} crypt_t;
+typedef struct
+{
+    int product_id;
+    int mode;
+    int version;
+    char public[64];
+    int trapmode;
+
+    crypt_t crypt;
+    char nmsIP[64];
 }SNMPManager;
 SNMPManager _globalSNMPManager;
 

+ 57 - 25
ip/src/json_handle.c

@@ -4399,21 +4399,47 @@ cJSON* json_to_service_manage(const char* str,_ServiceManageRequestInfo* _servic
     //SNMP上传参数
     else if(strcmp(_serviceManageRequestInfo->type,"SnmpSave")==0)
     {
-        cJSON* cjson_snmp_mode = cJSON_GetObjectItem(cjson,"mode");
-        cJSON* cjson_snmp_Version = cJSON_GetObjectItem(cjson,"snmpVersion");
-        cJSON* cjson_snmp_public = cJSON_GetObjectItem(cjson,"public"); 
-        cJSON* cjson_snmp_Trapmode = cJSON_GetObjectItem(cjson,"trapMode");
+        cJSON *tmp = NULL;
 
+        tmp = cJSON_GetObjectItem(cjson, "mode");
+        if (tmp != NULL)
+            _serviceManageRequestInfo->_snmp_info.mode = tmp->valuestring;
 
-        if(cjson_snmp_mode!=NULL)
-            _serviceManageRequestInfo->_snmp_info.mode = cjson_snmp_mode->valuestring ;
-        if(cjson_snmp_public!=NULL)
-            _serviceManageRequestInfo->_snmp_info.public = cjson_snmp_public->valuestring ;    
-        if(cjson_snmp_Version!=NULL)
-            _serviceManageRequestInfo->_snmp_info.version = cjson_snmp_Version->valuestring ;
-        if(cjson_snmp_Trapmode!=NULL)
-            _serviceManageRequestInfo->_snmp_info.trapmode = cjson_snmp_Trapmode->valuestring ;   
+        tmp = cJSON_GetObjectItem(cjson, "snmpVersion");
+        if (tmp != NULL)
+            _serviceManageRequestInfo->_snmp_info.version = tmp->valuestring;
 
+        tmp = cJSON_GetObjectItem(cjson, "public");
+        if (tmp != NULL)
+            _serviceManageRequestInfo->_snmp_info.public = tmp->valuestring;
+
+        tmp = cJSON_GetObjectItem(cjson, "trapMode");
+        if (tmp != NULL)
+            _serviceManageRequestInfo->_snmp_info.trapmode = tmp->valuestring;
+
+        tmp = cJSON_GetObjectItem(cjson, "yhm");
+        if (tmp != NULL)
+            _serviceManageRequestInfo->_snmp_info.user = tmp->valuestring;
+
+        tmp = cJSON_GetObjectItem(cjson, "rzxy");
+        if (tmp != NULL)
+            _serviceManageRequestInfo->_snmp_info.auth_alg = tmp->valuestring;
+
+        tmp = cJSON_GetObjectItem(cjson, "rzmm");
+        if (tmp != NULL)
+            _serviceManageRequestInfo->_snmp_info.auth_passwd = tmp->valuestring;
+
+        tmp = cJSON_GetObjectItem(cjson, "jmxy");
+        if (tmp != NULL)
+            _serviceManageRequestInfo->_snmp_info.priv_alg = tmp->valuestring;
+
+        tmp = cJSON_GetObjectItem(cjson, "jmmm");
+        if (tmp != NULL)
+            _serviceManageRequestInfo->_snmp_info.priv_passwd = tmp->valuestring;
+
+        tmp = cJSON_GetObjectItem(cjson, "nmsId");
+        if (tmp != NULL)
+            _serviceManageRequestInfo->_snmp_info.nms_IP = tmp->valuestring;
     }
     //SNMP TRAP参数
     else if(strcmp(_serviceManageRequestInfo->type,"StSave")==0)
@@ -4806,9 +4832,10 @@ char* service_ntp_ack_to_json(NTPManager* __NTPManager_t)
     return str;
 }
 
-char* service_snmp_ack_to_json(SNMPManager* __SNMPManager_t)
+char* service_snmp_ack_to_json(SNMPManager* sm)
 {
-    char* str = NULL;
+    char* str=NULL;
+    char buf[30];
     
     // 创建 JSON 对象
     cJSON* root = cJSON_CreateObject();   
@@ -4818,27 +4845,23 @@ char* service_snmp_ack_to_json(SNMPManager* __SNMPManager_t)
 
     cJSON* data_filed = cJSON_CreateObject();
 
- 
-
-    char buf[30];
-
 
     //创建数据
     memset(buf, 0, sizeof(buf));
-    sprintf(buf, "%d", __SNMPManager_t->mode);
+    sprintf(buf, "%d", sm->mode);
     cJSON_AddStringToObject(data_filed, "mode", buf);//data_mode_array);
 
 
     memset(buf, 0, sizeof(buf));
-    if (__SNMPManager_t->version==0)
+    if (sm->version==0)
     {
         sprintf(buf, "SNMP V1");
     }
-    else if (__SNMPManager_t->version==1)
+    else if (sm->version==1)
     {
         sprintf(buf, "SNMP V2c");
     }
-    else if (__SNMPManager_t->version==2)
+    else if (sm->version==2)
     {
         sprintf(buf, "SNMP V3");
     }
@@ -4847,12 +4870,21 @@ char* service_snmp_ack_to_json(SNMPManager* __SNMPManager_t)
         sprintf(buf, "null");
     }
     cJSON_AddStringToObject(data_filed,"snmpVersion", buf);
-    cJSON_AddStringToObject(data_filed,"public", __SNMPManager_t->public);
-   
+    cJSON_AddStringToObject(data_filed,"public", sm->public);
 
     memset(buf, 0, sizeof(buf));
-    sprintf(buf, "%d", __SNMPManager_t->trapmode);
+    sprintf(buf, "%d", sm->trapmode);
     cJSON_AddStringToObject(data_filed, "trapMode", buf);//data_trapmode_array);
+
+    crypt_t *pc=&sm->crypt;
+    cJSON_AddStringToObject(data_filed, "yhm",  pc->user);
+
+    cJSON_AddStringToObject(data_filed, "rzxy", pc->auth.alg);
+    cJSON_AddStringToObject(data_filed, "rzmm", pc->auth.passwd);
+    
+    cJSON_AddStringToObject(data_filed, "jmxy", pc->priv.alg);
+    cJSON_AddStringToObject(data_filed, "jmmm", pc->priv.passwd);
+    cJSON_AddStringToObject(data_filed, "nmsId", sm->nmsIP);
     //添加汇总
     cJSON_AddItemToObject(root, "data", data_filed);
 

+ 11 - 5
ip/src/json_handle.h

@@ -360,10 +360,16 @@ typedef struct
 /// @brief SNMP信息
 typedef struct
 {
-    char*   mode;
-    char*   version;
-    char*   public;
-    char*   trapmode;
+    char *mode;
+    char *version;
+    char *public;
+    char *trapmode;
+    char *user;
+    char *auth_alg;
+    char *auth_passwd;
+    char *priv_alg;
+    char *priv_passwd;
+    char *nms_IP;
 }_SNMP_ServerRequestInfo;
 
 /// @brief SNMP TRAP信息
@@ -597,7 +603,7 @@ char* service_ds_ack_to_json(char* chn_name,char* chn_id,_PowerDSManage_t* __pow
 char* service_ds_all_ack_to_json(char* pDate,char* Name,char* nchID,GlobalPowerManger* __powerDSManage_t);
 char* service_sx_ack_to_json(int product_id, GlobalPowerManger * __PowerManage_t);
 char* service_ntp_ack_to_json(NTPManager* __NTPManager_t);
-char* service_snmp_ack_to_json(SNMPManager* __SNMPManager_t);
+char* service_snmp_ack_to_json(SNMPManager* sm);
 char* service_smtp_ack_to_json(smtp_info_t* info);
 char* service_mqtt_ack_to_json(mqtt_info_t* info);
 char* service_group_ack_to_json(GroupInfo* _globalgroupInfo);

+ 36 - 46
ip/src/pduMIB_trap.c

@@ -8,62 +8,42 @@
 #include <net-snmp/agent/net-snmp-agent-includes.h>
 #include "pduMIB_trap.h"
 
-extern const oid snmptrap_oid[];
-extern const size_t snmptrap_oid_len;
+const oid snmptrap_oid[] = {1,3,6,1,6,3,1,1,4,1,0};
+const size_t snmptrap_oid_len = OID_LENGTH(snmptrap_oid);
 
-int
-send_pduAlarmAdded_trap( void )
+void
+send_pduPowerAlarm_trap(unsigned int TrapNum,void *Data)
 {
+    AlarmTrapinfo GlobalAlarmTrapinfo = *(AlarmTrapinfo*)Data;
     netsnmp_variable_list  *var_list = NULL;
-    const oid pduAlarmAdded_oid[] = { 1,3,6,1,4,1,2024,2,1 };
-    const oid pduAlarmType_oid[] = { 1,3,6,1,4,1,2024,1,6,1,1,2, /* insert index here */ };
-    const oid pduAlarmContext_oid[] = { 1,3,6,1,4,1,2024,1,6,1,1,3, /* insert index here */ };
-    const oid pduAlarmAction_oid[] = { 1,3,6,1,4,1,2024,1,6,1,1,4, /* insert index here */ };
-    const oid pduAlarmActionPara_oid[] = { 1,3,6,1,4,1,2024,1,6,1,1,5, /* insert index here */ };
-    const oid pduAlarmDate_oid[] = { 1,3,6,1,4,1,2024,1,6,1,1,6, /* insert index here */ };
-    const oid pduAlarmTime_oid[] = { 1,3,6,1,4,1,2024,1,6,1,1,7, /* insert index here */ };
+    const oid pduPowerAlarmTraps_oid[] = { 1,3,6,1,4,1,2024,2,1 };
+    const oid pduAlarmID_oid[] = { 1,3,6,1,4,1,2024,1,6,1,1,2,GlobalAlarmTrapinfo.ID };
+    const oid pduAlarmContext_oid[] = { 1,3,6,1,4,1,2024,1,6,1,1,3, GlobalAlarmTrapinfo.ID};
+    const oid pduAlarmDate_oid[] = { 1,3,6,1,4,1,2024,1,6,1,1,6, GlobalAlarmTrapinfo.ID };
 
     /*
      * Set the snmpTrapOid.0 value
      */
     snmp_varlist_add_variable(&var_list,
-        snmptrap_oid, snmptrap_oid_len,
+        snmptrap_oid,OID_LENGTH(snmptrap_oid) ,
         ASN_OBJECT_ID,
-        pduAlarmAdded_oid, sizeof(pduAlarmAdded_oid));
+        pduPowerAlarmTraps_oid, sizeof(pduPowerAlarmTraps_oid));
     
     /*
      * Add any objects from the trap definition
      */
     snmp_varlist_add_variable(&var_list,
-        pduAlarmType_oid, OID_LENGTH(pduAlarmType_oid),
+        pduAlarmID_oid, OID_LENGTH(pduAlarmID_oid),
         ASN_INTEGER,
-        /* Set an appropriate value for pduAlarmType */
-        NULL, 0);
+        &GlobalAlarmTrapinfo.Alarmid, sizeof(GlobalAlarmTrapinfo.Alarmid));
     snmp_varlist_add_variable(&var_list,
         pduAlarmContext_oid, OID_LENGTH(pduAlarmContext_oid),
         ASN_OCTET_STR,
-        /* Set an appropriate value for pduAlarmContext */
-        NULL, 0);
-    snmp_varlist_add_variable(&var_list,
-        pduAlarmAction_oid, OID_LENGTH(pduAlarmAction_oid),
-        ASN_INTEGER,
-        /* Set an appropriate value for pduAlarmAction */
-        NULL, 0);
-    snmp_varlist_add_variable(&var_list,
-        pduAlarmActionPara_oid, OID_LENGTH(pduAlarmActionPara_oid),
-        ASN_INTEGER,
-        /* Set an appropriate value for pduAlarmActionPara */
-        NULL, 0);
+        GlobalAlarmTrapinfo.AlarmContext, strlen(GlobalAlarmTrapinfo.AlarmContext));
     snmp_varlist_add_variable(&var_list,
         pduAlarmDate_oid, OID_LENGTH(pduAlarmDate_oid),
         ASN_OCTET_STR,
-        /* Set an appropriate value for pduAlarmDate */
-        NULL, 0);
-    snmp_varlist_add_variable(&var_list,
-        pduAlarmTime_oid, OID_LENGTH(pduAlarmTime_oid),
-        ASN_OCTET_STR,
-        /* Set an appropriate value for pduAlarmTime */
-        NULL, 0);
+        GlobalAlarmTrapinfo.AlarmDate, strlen(GlobalAlarmTrapinfo.AlarmDate));
 
     /*
      * Add any extra (optional) objects here
@@ -76,31 +56,41 @@ send_pduAlarmAdded_trap( void )
     send_v2trap( var_list );
     snmp_free_varbind( var_list );
 
-    return SNMP_ERR_NOERROR;
+   // return SNMP_ERR_NOERROR;
 }
-int
-send_pduAlarmRemoved_trap( void )
+void
+send_pduSensorAlarm_trap( unsigned int TrapNum,void *Data)
 {
+    AlarmTrapinfo GlobalAlarmTrapinfo = *(AlarmTrapinfo*)Data;
     netsnmp_variable_list  *var_list = NULL;
-    const oid pduAlarmRemoved_oid[] = { 1,3,6,1,4,1,2024,2,2 };
-    const oid pduAlarmContext_oid[] = { 1,3,6,1,4,1,2024,1,6,1,1,3, /* insert index here */ };
+    const oid pduSensorAlarmTraps_oid[] = { 1,3,6,1,4,1,2024,2,2 };
+    const oid pduAlarmID_oid[] = { 1,3,6,1,4,1,2024,1,6,1,1,2, GlobalAlarmTrapinfo.ID };
+    const oid pduAlarmContext_oid[] = { 1,3,6,1,4,1,2024,1,6,1,1,3, GlobalAlarmTrapinfo.ID};
+    const oid pduAlarmDate_oid[] = { 1,3,6,1,4,1,2024,1,6,1,1,6,GlobalAlarmTrapinfo.ID };
 
     /*
      * Set the snmpTrapOid.0 value
      */
     snmp_varlist_add_variable(&var_list,
-        snmptrap_oid, snmptrap_oid_len,
+        snmptrap_oid, OID_LENGTH(snmptrap_oid),
         ASN_OBJECT_ID,
-        pduAlarmRemoved_oid, sizeof(pduAlarmRemoved_oid));
+        pduSensorAlarmTraps_oid, sizeof(pduSensorAlarmTraps_oid));
     
     /*
      * Add any objects from the trap definition
      */
+    snmp_varlist_add_variable(&var_list,
+        pduAlarmID_oid, OID_LENGTH(pduAlarmID_oid),
+        ASN_INTEGER,
+        &GlobalAlarmTrapinfo.Alarmid, sizeof(GlobalAlarmTrapinfo.Alarmid));
     snmp_varlist_add_variable(&var_list,
         pduAlarmContext_oid, OID_LENGTH(pduAlarmContext_oid),
         ASN_OCTET_STR,
-        /* Set an appropriate value for pduAlarmContext */
-        NULL, 0);
+         GlobalAlarmTrapinfo.AlarmContext, strlen(GlobalAlarmTrapinfo.AlarmContext));
+    snmp_varlist_add_variable(&var_list,
+        pduAlarmDate_oid, OID_LENGTH(pduAlarmDate_oid),
+        ASN_OCTET_STR,
+         GlobalAlarmTrapinfo.AlarmDate, strlen( GlobalAlarmTrapinfo.AlarmDate));
 
     /*
      * Add any extra (optional) objects here
@@ -113,5 +103,5 @@ send_pduAlarmRemoved_trap( void )
     send_v2trap( var_list );
     snmp_free_varbind( var_list );
 
-    return SNMP_ERR_NOERROR;
-}
+   // return SNMP_ERR_NOERROR;
+}

+ 8 - 2
ip/src/pduMIB_trap.h

@@ -4,9 +4,15 @@
  */
 #ifndef PDUMIB_H
 #define PDUMIB_H
+typedef struct AlarmTrapinfo{
+    uint32_t    Alarmid;
+    uint32_t    ID;
+    char            AlarmContext[256];
+    char            AlarmDate[80];
+}AlarmTrapinfo;
 
 /* function declarations */
-int send_pduAlarmAdded_trap(void);
-int send_pduAlarmRemoved_trap(void);
+void send_pduPowerAlarm_trap( unsigned int TrapNum,void *Data);
+void send_pduSensorAlarm_trap( unsigned int TrapNum,void *Data);
 
 #endif /* PDUMIB_H */

+ 51 - 13
ip/src/sqlite_handle.c

@@ -7,6 +7,7 @@
 #include "cfg.h"
 #include "smtp.h"
 #include "switch_ctrl.h"
+#include "pduMIB_trap.h"
 
 #define	TOTAL_COUNT	60//统计总数
 
@@ -2712,8 +2713,9 @@ int dev_insert_alarm_info(sqlite3* db,int product_id,int alarm_type,char* date_t
     char* err_msg=NULL;
     char select_sql[1024] = {0};
     char *table="Table_AlarmManage";
+    int alarmNumber=-1;
     if(overwrite_flag) {
-        int alarmNumber=get_key(db, table, "alarmNumber", DB_MAX);
+        alarmNumber=get_key(db, table, "alarmNumber", DB_MAX);
         if(alarmNumber<0) {
             return -1;
         }
@@ -2746,7 +2748,7 @@ int dev_insert_alarm_info(sqlite3* db,int product_id,int alarm_type,char* date_t
 
     //smtp_send();
 
-    return 0;
+    return alarmNumber;
 }
 
 /// @brief 查询电源 传感器以及网络告警信息
@@ -3077,7 +3079,7 @@ int dev_get_snmp_info(sqlite3 *db, SNMPManager* _globalSNMPInfo)
     char* err_msg=NULL;
     char select_sql[1024] = {0};
 
-    sprintf(select_sql, "SELECT* from Table_SNMPManage WHERE product_id = %d;", __globalDeviceManage._globalDevInfo.product_id);
+    sprintf(select_sql, "SELECT* from Table_SNMPManage;");
     res = sqlite3_get_table(db, select_sql, &pResult, &nrow, &ncolumn, &err_msg);
     if(res != SQLITE_OK) {
         log_e("sqlite handle error: %s\n%s\n", err_msg,select_sql); sqlite3_free(err_msg);
@@ -3092,10 +3094,16 @@ int dev_get_snmp_info(sqlite3 *db, SNMPManager* _globalSNMPInfo)
     _globalSNMPInfo->product_id = atoi(pResult[rowIndex*ncolumn+0]); 
     _globalSNMPInfo->version= atoi(pResult[rowIndex*ncolumn+1]);
     _globalSNMPInfo->mode = atoi(pResult[rowIndex*ncolumn+2]); 
-    _globalSNMPInfo->public= malloc(64);
     strcpy(_globalSNMPInfo->public,pResult[rowIndex*ncolumn+3]);
-    _globalSNMPInfo->trapmode = atoi(pResult[rowIndex*ncolumn+4]); 
-
+    _globalSNMPInfo->trapmode = atoi(pResult[rowIndex*ncolumn+4]);
+    if(_globalSNMPInfo->version==2) {
+        if(pResult[rowIndex*ncolumn+5]) strcpy(_globalSNMPInfo->crypt.user,pResult[rowIndex*ncolumn+5]);
+        if(pResult[rowIndex*ncolumn+6]) strcpy(_globalSNMPInfo->crypt.auth.alg,pResult[rowIndex*ncolumn+6]);
+        if(pResult[rowIndex*ncolumn+7]) strcpy(_globalSNMPInfo->crypt.auth.passwd,pResult[rowIndex*ncolumn+7]);
+        if(pResult[rowIndex*ncolumn+8]) strcpy(_globalSNMPInfo->crypt.priv.alg,pResult[rowIndex*ncolumn+8]);
+        if(pResult[rowIndex*ncolumn+9]) strcpy(_globalSNMPInfo->crypt.priv.passwd,pResult[rowIndex*ncolumn+9]);
+    }
+    strcpy(_globalSNMPInfo->nmsIP,pResult[rowIndex*ncolumn+10]);
     sqlite3_free_table(pResult);
     return 0;
 }
@@ -3111,12 +3119,20 @@ int dev_insert_snmp_info(sqlite3 *db, SNMPManager* _globalSNMPInfo)
     char* err_msg=NULL;
     char select_sql[1024] = {0};
 
-    sprintf(select_sql,"INSERT INTO Table_SNMPManage (product_id, proto_type, enable, public, TrapEnable) VALUES (%d,%d,%d,'%s',%d);",
+    sprintf(select_sql,"INSERT INTO Table_SNMPManage (product_id, proto_type, enable, public, TrapEnable, "
+                       "user, auth_alg, auth_passwd, priv_alg, priv_passwd,nmsIP) VALUES (%d,%d,%d,'%s',%d,'%s','%s','%s','%s','%s','%s');",
         _globalSNMPInfo->product_id,
         _globalSNMPInfo->version,
         _globalSNMPInfo->mode,
         _globalSNMPInfo->public,
-        _globalSNMPInfo->trapmode);
+        _globalSNMPInfo->trapmode,
+        _globalSNMPInfo->crypt.user,
+        _globalSNMPInfo->crypt.auth.alg,
+        _globalSNMPInfo->crypt.auth.passwd,
+        _globalSNMPInfo->crypt.priv.alg,
+        _globalSNMPInfo->crypt.priv.passwd,
+        _globalSNMPInfo->nmsIP
+        );
     res = sqlite_SQL_RUN(db, select_sql, &pResult, &nrow, &ncolumn, &err_msg);
     if(res != SQLITE_OK) {
         log_e("sqlite handle error: %s\n%s\n", err_msg,select_sql); sqlite3_free(err_msg);
@@ -3136,12 +3152,18 @@ int dev_update_snmp_info(sqlite3 *db, int product_id, SNMPManager* _globalSNMPIn
     char** pResult = NULL;  //用来指向sql执行结果的指针
     char* err_msg=NULL;
     char select_sql[1024] = {0};
-    sprintf(select_sql,"UPDATE Table_SNMPManage SET proto_type='%d',enable=%d,public='%s',TrapEnable=%d WHERE product_id=%d;",
+    sprintf(select_sql,"UPDATE Table_SNMPManage SET proto_type='%d',enable=%d,public='%s',TrapEnable=%d, "
+                       "user='%s',auth_alg='%s',auth_passwd='%s',priv_alg='%s',priv_passwd='%s',nmsIP='%s';",
         _globalSNMPInfo->version,
         _globalSNMPInfo->mode,
         _globalSNMPInfo->public,
         _globalSNMPInfo->trapmode,
-        product_id);
+        _globalSNMPInfo->crypt.user,
+        _globalSNMPInfo->crypt.auth.alg,
+        _globalSNMPInfo->crypt.auth.passwd,
+        _globalSNMPInfo->crypt.priv.alg,
+        _globalSNMPInfo->crypt.priv.passwd,
+        _globalSNMPInfo->nmsIP);
     res = sqlite_SQL_RUN(db, select_sql, &pResult, &nrow, &ncolumn, &err_msg);
     if(res != SQLITE_OK) {
         log_e("sqlite handle error: %s\n%s\n", err_msg,select_sql); sqlite3_free(err_msg);
@@ -3757,12 +3779,20 @@ int dev_Alarm_send_message(GlobalDeviceManager* globalDeviceManger,GlobalPowerMa
 
     char buffer[80];
     strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", t);    
-    dev_insert_alarm_info(globalDeviceManger->db,__globalPowerTemp->product_id,0,buffer,AlarmText);
+    int nAlarmID=dev_insert_alarm_info(globalDeviceManger->db,__globalPowerTemp->product_id,0,buffer,AlarmText);
 
 
     // 超限动作
     smtp_post_alarm(ALARM_TYPE_POWER, 0, _AlarmType, AlarmText);
-    
+
+    AlarmTrapinfo pAlarmTrapinfo; ////Trap information
+    pAlarmTrapinfo.ID = __globalPowerTemp->product_ch_id;
+    pAlarmTrapinfo.Alarmid = nAlarmID;
+    strcpy(pAlarmTrapinfo.AlarmContext, AlarmText);
+    strcpy(pAlarmTrapinfo.AlarmDate, buffer);
+    if (_globalSNMPManager.trapmode == 1)
+        send_pduPowerAlarm_trap(1, (void *)&pAlarmTrapinfo);
+
     int ret = -1 ;
     int nchID = __globalPowerTemp->product_ch_id;
     if (__globalPowerTemp->global_over_manager)
@@ -3992,11 +4022,19 @@ int dev_sensor_Alarm_send_message(GlobalDeviceManager* globalDeviceManger,Global
 
     char buffer[80];
     strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", t);    
-    dev_insert_alarm_info(globalDeviceManger->db,__globalPowerTemp->product_id,1,buffer,AlarmText);
+    int nAlarmID=dev_insert_alarm_info(globalDeviceManger->db,__globalPowerTemp->product_id,1,buffer,AlarmText);
 
     int subtype=__globalPowerTemp->sensor_type;
     smtp_post_alarm(ALARM_TYPE_SENSOR, subtype, _AlarmType, AlarmText);
 
+    AlarmTrapinfo pAlarmTrapinfo; ////Trap information
+    pAlarmTrapinfo.ID = __globalPowerTemp->sensor_id;
+    pAlarmTrapinfo.Alarmid = nAlarmID;
+    strcpy(pAlarmTrapinfo.AlarmContext, AlarmText);
+    strcpy(pAlarmTrapinfo.AlarmDate, buffer);
+    if (_globalSNMPManager.trapmode == 1)
+        send_pduSensorAlarm_trap(1, (void *)&pAlarmTrapinfo);
+
     return 0;
 }
 int dev_Alarm_Run_message(GlobalDeviceManager* globalDeviceManger, const char* strRunMSG, const char *strOther)