CSnmpClass.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #pragma once
  2. #include "TSysThread.h"
  3. #include "TSysThreadPool.h"
  4. #define HAVE_SSIZE_T
  5. extern "C" {
  6. #include "net-snmp/net-snmp-config.h"
  7. #include "net-snmp/net-snmp-includes.h"
  8. #include "net-snmp/agent/net-snmp-agent-includes.h"
  9. #include "net-snmp/library/fd_event_manager.h"
  10. #include "net-snmp/agent/netsnmp_close_fds.h"
  11. #include "net-snmp/snmplib/snmp_syslog.h"
  12. #include "net-snmp/agent/agent_global_vars.h"
  13. #include "net-snmp/agent/mibgroup/snmpv3/snmpEngine.h"
  14. #include "net-snmp/agent/mibgroup/snmpv3/usmUser.h"
  15. #include "net-snmp/agent/mibgroup/agent/nsVacmAccessTable.h"
  16. #include "net-snmp/agent/mibgroup/agentx/subagent.h"
  17. #include "net-snmp/agent/mibgroup/notification-log-mib/notification_log.h"
  18. #include "net-snmp/agent/mibgroup/tlstm-mib/snmpTlstmCertToTSNTable/snmpTlstmCertToTSNTable.h"
  19. #include "net-snmp/agent/mibgroup/mibII/vacm_conf.h"
  20. }
  21. #include "snmptrapd_handlers.h"
  22. #include "snmptrapd_log.h"
  23. #include "snmptrapd_auth.h"
  24. #include "snmptrapd_sql.h"
  25. /*
  26. * Include winservice.h to support Windows Service
  27. */
  28. #ifdef WIN32
  29. #include <windows.h>
  30. #include <tchar.h>
  31. extern "C" {
  32. #include "net-snmp/library/winservice.h"
  33. }
  34. #define WIN32SERVICE
  35. #endif
  36. extern "C" {
  37. #include "net-snmp/net-snmp-features.h"
  38. }
  39. #ifndef BSD4_3
  40. #define BSD4_2
  41. #endif
  42. #define SNMPTRAPD_RUNNING 1
  43. #define SNMPTRAPD_STOPPED 0
  44. #define PRINT_V23_NOTIFICATION_FORMAT "%.4y-%.2m-%.2l %.2h:%.2j:%.2k %B [%b]:\n%v\n"
  45. typedef struct tagDEVICEINFO_{
  46. std::string srcip;
  47. std::string port_index;
  48. }DEVICEINFO;
  49. class CSnmpClass : public TSysThreadImpl {
  50. public:
  51. CSnmpClass();
  52. ~CSnmpClass();
  53. int Init(std::string addr);
  54. static void* m_pInstance;
  55. TSysThreadPool *m_pOolInvoker;
  56. void Action();
  57. std::string m_sDeviceIp;
  58. int m_nPowerFlag; // 0/正常 1/报警
  59. int m_nErpsFlag;// 0/正常 1/报警
  60. int m_nPhyFlag;// 0/正常 1/报警
  61. int m_nBoardFlag;// 0/正常 1/报警
  62. std::vector <DEVICEINFO*> m_pWhite_list;
  63. int m_nAlarmStatus;
  64. int m_nMtime;
  65. int m_needTime;
  66. std::mutex m_Alarm;
  67. std::string m_sCommunity;
  68. std::string m_receive_bind_ip;
  69. std::string m_send_bind_ip;
  70. char m_bindip[100];
  71. std::vector<std::string> m_identity;
  72. std::vector<std::string> m_dst_ip;
  73. std::string m_systemType;
  74. private:
  75. static char *m_pDdefault_port; /* Default default port */
  76. static char *m_pDefault_port;
  77. char *m_pTrap1_fmt_str_remember;
  78. static int m_nDofork;
  79. int m_nTrapd_status;
  80. /* m_pApp_name_long used for SCM, registry etc */
  81. LPCSTR m_pApp_name_long; /* Application Name */
  82. const char *m_pApp_name;
  83. void GetIdentity(std::string data);
  84. bool LoadDeviceInfo();
  85. bool CheckPower(std::string buf, std::string comunnity);
  86. bool CheckResetPower(std::string buf, std::string comunnity);
  87. bool CheckNetwork(std::string buf, std::string comunnity);
  88. bool CheckPort_index(std::string src,std::string port_index);
  89. bool FindNetAlarm(std::string buf, std::string comunnity, std::string moduleIdentity);
  90. void StopSnmpTrapd(void);
  91. static RETSIGTYPE TermHandler(int sig);
  92. static int PreParse(netsnmp_session * session, netsnmp_transport *transport, void *transport_data, int transport_data_length);
  93. netsnmp_session * SnmptrapdAddSession(netsnmp_transport *t);
  94. void SnmptrapdCloseSessions(netsnmp_session * sess_list);
  95. static void ParseTrapdAddress(const char *token, char *cptr);
  96. static void FreeTrapdAddress(void);
  97. static void ParseConfigDoNotLogTraps(const char *token, char *cptr);
  98. static void ParseConfigDoNotFork(const char *token, char *cptr);
  99. static void ParseConfigIgnoreAuthFailure(const char *token, char *cptr);
  100. static void ParseConfigOutputOption(const char *token, char *cptr);
  101. static void ParseConfigAddForwarderInfo(const char *token, char *cptr);
  102. void SnmptrapdMainLoop(void);
  103. int SnmpTrapdMain(int argc, char * argv[]);
  104. void ReadProcess(std::string buf, std::string comunnity);
  105. void InitNetsnmpTrapdAuth(void);
  106. static int PrintHandler(netsnmp_pdu* pdu, netsnmp_transport* transport, netsnmp_trapd_handler * handler);
  107. static int RecvHandler(netsnmp_pdu* pdu, netsnmp_transport *transport, netsnmp_trapd_handler * handler);
  108. void ParseBuffer(std::string buf, std::string comunnity);
  109. void MoxaParseBuffer(std::string buf, std::string comunnity);
  110. void H3cParseBuffer(std::string buf, std::string comunnity);
  111. int ParsePtype(std::string buf, std::string upAnddown);
  112. int ParseEtype(std::string buf, std::string upAnddown);
  113. int ParseBtype(std::string buf);
  114. std::string FindString(std::string a, std::string b);
  115. bool FindIntegerOrSend(std::string src, std::string comunnity, std::string moduleIdentity, std::string power);
  116. void SendString(int status, std::string comunnity, std::string buf, std::string moduleIdentity, std::string power);
  117. void SendStringH3c(int status, std::string comunnity, std::string buf);
  118. };