- added ticket logging for checks
authorAlex Kalimulin <alex@netxms.org>
Tue, 30 Aug 2011 22:16:03 +0000 (22:16 +0000)
committerAlex Kalimulin <alex@netxms.org>
Tue, 30 Aug 2011 22:16:03 +0000 (22:16 +0000)
src/server/core/slmcheck.cpp
src/server/include/nms_objects.h

index 3e9dd5d..30c5cc3 100644 (file)
@@ -24,6 +24,7 @@
 
 #define QUERY_LENGTH           (512)
 
+long SlmCheck::ticketId = -1;
 
 //
 // SLM check default constructor
@@ -297,17 +298,21 @@ void SlmCheck::execute()
 {
        NXSL_ServerEnv *pEnv;
        NXSL_Value *pValue;
+       DWORD oldStatus;
 
        pEnv = new NXSL_ServerEnv;
 
        switch (m_type)
        {
                case check_script:
+                       oldStatus = m_iStatus;
                        if (m_pCompiledScript->run(pEnv, 0, NULL) == 0)
                        {
                                pValue = m_pCompiledScript->getResult();
                                m_iStatus = pValue->getValueAsInt32() == 0 ? STATUS_NORMAL : STATUS_CRITICAL;
                                DbgPrintf(9, _T("SlmCheck::execute: %s/%ld ret value %d"), m_szName, (long)m_dwId, pValue->getValueAsInt32());
+                               if (m_iStatus == STATUS_CRITICAL && m_iStatus != oldStatus)
+                                       insertTicket();
                        }
                        else
                        {
@@ -325,3 +330,49 @@ void SlmCheck::execute()
                        break;
        }
 }
+
+//
+// Insert ticket for this check into slm_tickets
+//
+
+BOOL SlmCheck::insertTicket()
+{
+       DB_RESULT hResult;
+       DB_STATEMENT hStmt;
+
+       if (m_iStatus == STATUS_NORMAL)
+               return FALSE;
+
+       if (SlmCheck::ticketId < 0) // not initialized yet
+       {
+               hResult = DBSelect(g_hCoreDB, _T("SELECT max(ticket_id) FROM slm_tickets"));
+               if (hResult == NULL)
+                       return FALSE;
+               SlmCheck::ticketId = DBGetNumRows(hResult) > 0 ? DBGetFieldLong(hResult, 0, 0) : 0;
+               DBFreeResult(hResult);
+       }
+
+       SlmCheck::ticketId++;
+
+       hStmt = DBPrepare(g_hCoreDB, _T("INSERT INTO slm_tickets (ticket_id,check_id,create_timestamp,close_timestamp,reason) ")
+                                       _T("VALUES (?,?,?,0,'-')"));
+       if (hStmt != NULL)
+       {
+               DBBind(hStmt, 1, DB_SQLTYPE_INTEGER, SlmCheck::ticketId);
+               DBBind(hStmt, 2, DB_SQLTYPE_INTEGER, m_dwId);
+               DBBind(hStmt, 3, DB_SQLTYPE_INTEGER, DWORD(time(NULL)));
+               if (!DBExecute(hStmt))
+               {
+                       DBFreeStatement(hStmt);
+                       return FALSE;
+               }
+               DbgPrintf(9, _T("SlmCheck::insertTicket() ok with id %ld"), SlmCheck::ticketId);
+       }
+       else
+       {
+               return FALSE;
+       }
+
+       DBFreeStatement(hStmt);
+       return TRUE;
+}
\ No newline at end of file
index 2e20cd5..6e26116 100644 (file)
@@ -1591,6 +1591,7 @@ protected:
        TCHAR *m_script;
        NXSL_Program *m_pCompiledScript;
        TCHAR m_reason[256];
+       static long ticketId;
 
        void setScript(const TCHAR *script);
 
@@ -1609,6 +1610,7 @@ public:
        virtual DWORD ModifyFromMessage(CSCPMessage *pRequest, BOOL bAlreadyLocked = FALSE);
 
        void execute();
+       BOOL insertTicket();
        void setReason(const TCHAR *reason) { nx_strncpy(m_reason, CHECK_NULL_EX(reason), 256); }
        const TCHAR *getReason() { return m_reason; }
 };