~ubuntu-dev/ubuntu/lucid/zabbix/lucid-201002110857

« back to all changes in this revision

Viewing changes to src/zabbix_agent_win32/eventlog.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michael Ablassmeier
  • Date: 2007-07-02 09:06:51 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20070702090651-8l6fl3fjw9rh6l2u
Tags: 1:1.4.1-2
Add patch from SVN in order to fix Incorrect processing of character '%'
in user parameters and remote commands.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#include "zabbixw32.h"
2
 
 
3
 
#define DllExport   __declspec( dllexport )
4
 
#define MAX_INSERT_STRS 64
5
 
#define MAX_MSG_LENGTH 1024
6
 
 
7
 
DllExport   long    MyOpenEventLog(char *pAppName,HANDLE
8
 
*pEventHandle,long *pNumRecords,long *pLatestRecord);
9
 
DllExport   long    MyCloseEventLog(HANDLE hAppLog);
10
 
DllExport   long    MyClearEventLog(HANDLE hAppLog);
11
 
DllExport   long    MyGetAEventLog(char *pAppName,HANDLE hAppLog,long
12
 
which,double *pTime,char *pSource,char *pMessage,DWORD *pType,WORD
13
 
*pCategory, DWORD *timestamp);
14
 
 
15
 
int process_eventlog(
16
 
        char *source,
17
 
        int *lastlogsize, 
18
 
        char *timestamp, 
19
 
        char *src, 
20
 
        char *severity,
21
 
        char *message)
22
 
{
23
 
 
24
 
    HANDLE  hAppLog;
25
 
    long    FirstID;
26
 
    long    LastID;
27
 
    long    i;
28
 
    double  time;
29
 
        DWORD    t,type;
30
 
        WORD    category;
31
 
        int             ret = 1;
32
 
 
33
 
LOG_FUNC_CALL("In process_eventlog()");
34
 
INIT_CHECK_MEMORY(main);
35
 
 
36
 
        if (!MyOpenEventLog(source,&hAppLog,&LastID /* number */, &FirstID /* oldest */))
37
 
        {
38
 
                LastID += FirstID; 
39
 
 
40
 
                if(*lastlogsize > LastID)
41
 
                        *lastlogsize = FirstID;
42
 
                else if((*lastlogsize) >= FirstID)
43
 
                        FirstID = (*lastlogsize)+1;
44
 
                
45
 
                for (i = FirstID; i < LastID; i++)
46
 
                {
47
 
                        if(MyGetAEventLog(source,hAppLog,i,&time,src,message,&type,&category,&t) == 0)
48
 
                        {
49
 
                                sprintf(timestamp,"%ld",t);
50
 
 
51
 
                                if(type==EVENTLOG_ERROR_TYPE)                           type=4;
52
 
                                else if(type==EVENTLOG_AUDIT_FAILURE)           type=7;
53
 
                                else if(type==EVENTLOG_AUDIT_SUCCESS)           type=8;
54
 
                                else if(type==EVENTLOG_INFORMATION_TYPE)        type=1;
55
 
                                else if(type==EVENTLOG_WARNING_TYPE)            type=2;
56
 
                                sprintf(severity,"%d",type);
57
 
                                *lastlogsize = i;
58
 
                                ret = 0;
59
 
                                break;
60
 
                        }
61
 
                }
62
 
                MyCloseEventLog(hAppLog);
63
 
        }
64
 
 
65
 
CHECK_MEMORY(main, "process_eventlog","end");
66
 
LOG_FUNC_CALL("End of process_eventlog()");
67
 
 
68
 
        return ret;
69
 
}
70
 
 
71
 
// open event logger and return number of records
72
 
DllExport   long    MyOpenEventLog(
73
 
        char    *pAppName,
74
 
        HANDLE  *pEventHandle,
75
 
        long    *pNumRecords,
76
 
        long    *pLatestRecord)
77
 
{
78
 
    HANDLE  hAppLog;            /* handle to the application log */
79
 
 
80
 
LOG_FUNC_CALL("In MyOpenEventLog()");
81
 
INIT_CHECK_MEMORY(main);
82
 
 
83
 
    *pEventHandle = 0;
84
 
    *pNumRecords = 0;
85
 
    hAppLog = OpenEventLog(NULL,pAppName);              // open log file
86
 
    if (!hAppLog)
87
 
        {
88
 
                LOG_DEBUG_INFO("s","MyOpenEventLog: 1");
89
 
        return(GetLastError());
90
 
        }
91
 
    GetNumberOfEventLogRecords(hAppLog,(unsigned long*)pNumRecords);// get number of records
92
 
    GetOldestEventLogRecord(hAppLog,(unsigned long*)pLatestRecord);
93
 
    *pEventHandle = hAppLog;
94
 
 
95
 
CHECK_MEMORY(main, "MyOpenEventLog", "end");
96
 
LOG_FUNC_CALL("End of MyOpenEventLog()");
97
 
    return(0);
98
 
 
99
 
}
100
 
 
101
 
// close event logger
102
 
DllExport   long    MyCloseEventLog(
103
 
        HANDLE hAppLog
104
 
        )
105
 
{
106
 
LOG_FUNC_CALL("In MyCloseEventLog()");
107
 
INIT_CHECK_MEMORY(main);
108
 
 
109
 
    if (hAppLog)  CloseEventLog(hAppLog);
110
 
 
111
 
CHECK_MEMORY(main, "MyCloseEventLog", "end");
112
 
LOG_FUNC_CALL("End of MyCloseEventLog()");
113
 
        return(0);
114
 
}
115
 
 
116
 
// clear event log
117
 
DllExport   long    MyClearEventLog(
118
 
        HANDLE hAppLog
119
 
        )
120
 
{
121
 
LOG_FUNC_CALL("In MyClearEventLog()");
122
 
INIT_CHECK_MEMORY(main);
123
 
 
124
 
    if (!(ClearEventLog(hAppLog,0)))
125
 
        {
126
 
LOG_DEBUG_INFO("s","MyClearEventLog: error exit");
127
 
        return(GetLastError());
128
 
        }
129
 
 
130
 
CHECK_MEMORY(main, "MyClearEventLog", "end");
131
 
LOG_FUNC_CALL("End of MyClearEventLog()");
132
 
    return(0);
133
 
 
134
 
}
135
 
 
136
 
// get Nth error from event log. 1 is the first.
137
 
DllExport   long    MyGetAEventLog(
138
 
                char *pAppName,
139
 
                HANDLE hAppLog,
140
 
                long which,
141
 
                double *pTime,
142
 
                char *pSource,
143
 
                char *pMessage,
144
 
                DWORD *pType,
145
 
                WORD *pCategory, 
146
 
                DWORD *timestamp
147
 
                )
148
 
{
149
 
    EVENTLOGRECORD  *pELR = NULL;
150
 
    BYTE            bBuffer[1024];                      /* hold the event log record raw data */
151
 
    DWORD           dwRead, dwNeeded;
152
 
    char            temp[MAX_PATH];
153
 
    char            MsgDll[MAX_PATH];                   /* the name of the message DLL */
154
 
    HKEY            hk = NULL;
155
 
    DWORD           Data;
156
 
    DWORD           Type;
157
 
    HINSTANCE       hLib = NULL;                        /* handle to the messagetable DLL */
158
 
    char            *pCh = NULL, *pFile = NULL, *pNextFile = NULL;
159
 
    char            *aInsertStrs[MAX_INSERT_STRS];      // array of pointers to insert
160
 
    long            i;
161
 
    LPTSTR          msgBuf = NULL;                       // hold text of the error message that we
162
 
    long            err = 0;
163
 
 
164
 
LOG_FUNC_CALL("In MyGetAEventLog()");
165
 
INIT_CHECK_MEMORY(main);
166
 
 
167
 
    if (!hAppLog)        return(0);
168
 
 
169
 
        pMessage[0] = '\0';
170
 
 
171
 
    if(!ReadEventLog(hAppLog,                    /* event-log handle */
172
 
                EVENTLOG_SEEK_READ |                    /* read forward */
173
 
                EVENTLOG_FORWARDS_READ,                 /* sequential read */
174
 
                which,                                  /* which record to read 1 is first */
175
 
                bBuffer,                                /* address of buffer */
176
 
                sizeof(bBuffer),                        /* size of buffer */
177
 
                &dwRead,                                /* count of bytes read */
178
 
                &dwNeeded))                             /* bytes in next record */
179
 
        {
180
 
                return GetLastError();
181
 
        }
182
 
        pELR = (EVENTLOGRECORD*)bBuffer;                    // point to data
183
 
 
184
 
        *pTime          = (double)pELR->TimeGenerated;          // return double timestamp
185
 
        *pType          = pELR->EventType;                  // return event type
186
 
        *pCategory      = pELR->EventCategory;                          // return category
187
 
        *timestamp      = pELR->TimeGenerated;                          // return timestamp
188
 
 
189
 
        strcpy(pSource,((char*)pELR + sizeof(EVENTLOGRECORD)));// copy source name
190
 
 
191
 
// Get path to message dll
192
 
        strcpy(temp,"SYSTEM\\CurrentControlSet\\Services\\EventLog\\");
193
 
        strcat(temp,pAppName);
194
 
        strcat(temp,"\\");
195
 
        strcat(temp,((char*)pELR + sizeof(EVENTLOGRECORD)));
196
 
 
197
 
        pFile = NULL;
198
 
        if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, temp, 0, KEY_READ, &hk) == ERROR_SUCCESS)
199
 
        {
200
 
                pFile = temp; 
201
 
                Data = MAX_PATH;
202
 
                err = RegQueryValueEx(
203
 
                                hk,                                             /* handle of key to query */
204
 
                                "EventMessageFile",     /* value name             */
205
 
                                NULL,                   /* must be NULL           */
206
 
                                &Type,                  /* address of type value  */
207
 
                                (UCHAR*)pFile,          /* address of value data  */
208
 
                                &Data);                 /* length of value data   */
209
 
                RegCloseKey(hk);
210
 
 
211
 
                if(err != ERROR_SUCCESS)
212
 
                        pFile = NULL;
213
 
        }
214
 
 
215
 
        err = 1;
216
 
        while(pFile)
217
 
        {
218
 
                pNextFile = strchr(pFile,';');
219
 
                if(pNextFile)
220
 
                {
221
 
                        *pNextFile = '\0';
222
 
                        pNextFile++;
223
 
                }
224
 
 
225
 
                if (ExpandEnvironmentStrings(pFile, MsgDll, MAX_PATH))
226
 
                {
227
 
                        hLib = LoadLibraryEx(MsgDll, NULL, LOAD_LIBRARY_AS_DATAFILE);
228
 
                        if(hLib)
229
 
                        {
230
 
                                /* prepare the array of insert strings for FormatMessage - the
231
 
                                insert strings are in the log entry. */
232
 
                                for (
233
 
                                        i = 0,  pCh = (char *)((LPBYTE)pELR + pELR->StringOffset);
234
 
                                        i < pELR->NumStrings && i < MAX_INSERT_STRS; 
235
 
                                        i++,    pCh += strlen(pCh) + 1) /* point to next string */
236
 
                                {
237
 
                                        aInsertStrs[i] = pCh;
238
 
                                }
239
 
 
240
 
                                /* Format the message from the message DLL with the insert strings */
241
 
                                FormatMessage(
242
 
                                        FORMAT_MESSAGE_FROM_HMODULE |
243
 
                                        FORMAT_MESSAGE_ALLOCATE_BUFFER |
244
 
                                        FORMAT_MESSAGE_ARGUMENT_ARRAY |
245
 
                                        FORMAT_MESSAGE_FROM_SYSTEM,
246
 
                                        hLib,                                                           /* the messagetable DLL handle */
247
 
                                        pELR->EventID,                      /* message ID */
248
 
                                        MAKELANGID(LANG_NEUTRAL, SUBLANG_ENGLISH_US),   /* language ID */
249
 
                                        (LPTSTR) &msgBuf,                   /* address of pointer to buffer for message */
250
 
                                        MAX_MSG_LENGTH,                     /* maximum size of the message buffer */
251
 
                                        aInsertStrs);                       /* array of insert strings for the message */
252
 
 
253
 
                                if(msgBuf)
254
 
                                {
255
 
                                        strcpy(pMessage,msgBuf);                    // copy message
256
 
                                        err = 0;
257
 
 
258
 
                                        /* Free the buffer that FormatMessage allocated for us. */
259
 
                                        LocalFree((HLOCAL) msgBuf);
260
 
                                }
261
 
                                FreeLibrary(hLib);
262
 
                        }
263
 
                }
264
 
 
265
 
                if(err == 0) break;
266
 
 
267
 
                pFile = pNextFile;
268
 
        }
269
 
 
270
 
        if(err)
271
 
        {
272
 
                for (
273
 
                        i = 0,  pCh = (char *)((LPBYTE)pELR + pELR->StringOffset);
274
 
                        i < pELR->NumStrings && i < MAX_INSERT_STRS; 
275
 
                        i++,    pCh += strlen(pCh) + 1) /* point to next string */
276
 
                {
277
 
                        if(i > 0)                       strcat(pMessage,",");
278
 
                        strcat(pMessage,pCh);
279
 
                }
280
 
        }
281
 
 
282
 
CHECK_MEMORY(main, "MyGetAEventLog", "end");
283
 
LOG_FUNC_CALL("End of MyGetAEventLog()");
284
 
    return 0;
285
 
 
286