diff options
author | sauros <sauros@97f52cf1-0a1b-0410-bd0e-c28be96e8082> | 2003-01-22 08:17:27 +0000 |
---|---|---|
committer | sauros <sauros@97f52cf1-0a1b-0410-bd0e-c28be96e8082> | 2003-01-22 08:17:27 +0000 |
commit | fbc2ccd1d6cee9629680ab7e9dce25ec56eb03fb (patch) | |
tree | b23827039dff2bc6163576f28802339890cd05b8 /src/zabbix_agent_win32 | |
parent | 7dbaed99f5089ac04e3a78703fb3b06fd15b8e99 (diff) | |
download | zabbix-fbc2ccd1d6cee9629680ab7e9dce25ec56eb03fb.tar.gz zabbix-fbc2ccd1d6cee9629680ab7e9dce25ec56eb03fb.tar.xz zabbix-fbc2ccd1d6cee9629680ab7e9dce25ec56eb03fb.zip |
Added logging to Event Log
git-svn-id: svn://svn.zabbix.com/trunk@656 97f52cf1-0a1b-0410-bd0e-c28be96e8082
Diffstat (limited to 'src/zabbix_agent_win32')
-rwxr-xr-x | src/zabbix_agent_win32/Debug/ZabbixW32.exe | bin | 282700 -> 315468 bytes | |||
-rw-r--r-- | src/zabbix_agent_win32/README | 1 | ||||
-rwxr-xr-x | src/zabbix_agent_win32/Release/ZabbixW32.exe | bin | 86016 -> 102400 bytes | |||
-rw-r--r-- | src/zabbix_agent_win32/ZabbixW32.dsp | 53 | ||||
-rw-r--r-- | src/zabbix_agent_win32/collect.cpp | 20 | ||||
-rw-r--r-- | src/zabbix_agent_win32/comm.cpp | 13 | ||||
-rw-r--r-- | src/zabbix_agent_win32/config.cpp | 12 | ||||
-rw-r--r-- | src/zabbix_agent_win32/log.cpp | 145 | ||||
-rw-r--r-- | src/zabbix_agent_win32/main.cpp | 24 | ||||
-rw-r--r-- | src/zabbix_agent_win32/messages.mc | 143 | ||||
-rw-r--r-- | src/zabbix_agent_win32/procinfo.cpp | 5 | ||||
-rw-r--r-- | src/zabbix_agent_win32/resource.h | 15 | ||||
-rw-r--r-- | src/zabbix_agent_win32/service.cpp | 61 | ||||
-rw-r--r-- | src/zabbix_agent_win32/sysinfo.cpp | 62 | ||||
-rw-r--r-- | src/zabbix_agent_win32/util.cpp | 17 | ||||
-rw-r--r-- | src/zabbix_agent_win32/zabbixw32.h | 13 | ||||
-rw-r--r-- | src/zabbix_agent_win32/zabbixw32.rc | 70 |
17 files changed, 553 insertions, 101 deletions
diff --git a/src/zabbix_agent_win32/Debug/ZabbixW32.exe b/src/zabbix_agent_win32/Debug/ZabbixW32.exe Binary files differindex 53962ead..a7f6b745 100755 --- a/src/zabbix_agent_win32/Debug/ZabbixW32.exe +++ b/src/zabbix_agent_win32/Debug/ZabbixW32.exe diff --git a/src/zabbix_agent_win32/README b/src/zabbix_agent_win32/README index b04d2441..19249d9e 100644 --- a/src/zabbix_agent_win32/README +++ b/src/zabbix_agent_win32/README @@ -125,6 +125,7 @@ disktotal[<path>] <path> can be normal Windows path, like C:\, or UNC filesize[<path>] <path> can be normal Windows path, like C:\, or UNC memory[free] memory[total] +memory[cached] Only on Windows XP ping proc_cnt[<process_name>] swap[free] diff --git a/src/zabbix_agent_win32/Release/ZabbixW32.exe b/src/zabbix_agent_win32/Release/ZabbixW32.exe Binary files differindex f5753582..cd61d25c 100755 --- a/src/zabbix_agent_win32/Release/ZabbixW32.exe +++ b/src/zabbix_agent_win32/Release/ZabbixW32.exe diff --git a/src/zabbix_agent_win32/ZabbixW32.dsp b/src/zabbix_agent_win32/ZabbixW32.dsp index 1a4798c0..003d3ba8 100644 --- a/src/zabbix_agent_win32/ZabbixW32.dsp +++ b/src/zabbix_agent_win32/ZabbixW32.dsp @@ -149,6 +149,59 @@ SOURCE=.\zabbixw32.h # Begin Group "Resource Files" # PROP Default_Filter "ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe" +# Begin Source File + +SOURCE=.\zabbixw32.rc +# End Source File +# End Group +# Begin Group "Message Files" + +# PROP Default_Filter ".mc" +# Begin Source File + +SOURCE=.\messages.mc + +!IF "$(CFG)" == "ZabbixW32 - Win32 Release" + +# Begin Custom Build - Message Compiler +ProjDir=. +InputPath=.\messages.mc +InputName=messages + +BuildCmds= \ + mc -s -U -h $(ProjDir) -r $(ProjDir) $(InputName) \ + del $(ProjDir)\$(InputName).rc \ + + +"$(InputName).h" : $(SOURCE) "$(INTDIR)" "$(OUTDIR)" + $(BuildCmds) + +"Msg00001.bin" : $(SOURCE) "$(INTDIR)" "$(OUTDIR)" + $(BuildCmds) +# End Custom Build + +!ELSEIF "$(CFG)" == "ZabbixW32 - Win32 Debug" + +# Begin Custom Build - Message Compiler +ProjDir=. +InputPath=.\messages.mc +InputName=messages + +BuildCmds= \ + mc -s -U -h $(ProjDir) -r $(ProjDir) $(InputName) \ + del $(ProjDir)\$(InputName).rc \ + + +"$(InputName).h" : $(SOURCE) "$(INTDIR)" "$(OUTDIR)" + $(BuildCmds) + +"Msg00001.bin" : $(SOURCE) "$(INTDIR)" "$(OUTDIR)" + $(BuildCmds) +# End Custom Build + +!ENDIF + +# End Source File # End Group # End Target # End Project diff --git a/src/zabbix_agent_win32/collect.cpp b/src/zabbix_agent_win32/collect.cpp index 85a421b5..e155205d 100644 --- a/src/zabbix_agent_win32/collect.cpp +++ b/src/zabbix_agent_win32/collect.cpp @@ -78,13 +78,13 @@ void CollectorThread(void *) if (PdhOpenQuery(NULL,0,&query)!=ERROR_SUCCESS) { - WriteLog("PdhOpenQuery failed\r\n"); + WriteLog(MSG_PDH_OPEN_QUERY_FAILED,EVENTLOG_ERROR_TYPE,"s",GetSystemErrorText(GetLastError())); return; } if (PdhAddCounter(query,"\\Processor(_Total)\\% Processor Time",0,&cntCpuUsage[0])!=ERROR_SUCCESS) { - WriteLog("PdhAddCounter(\\Processor(_Total)\\%% Processor Time) failed\r\n"); + WriteLog(MSG_PDH_ADD_COUNTER_FAILED,EVENTLOG_ERROR_TYPE,"s","\\Processor(_Total)\\%% Processor Time"); PdhCloseQuery(query); return; } @@ -95,7 +95,7 @@ void CollectorThread(void *) sprintf(counterPath,"\\Processor(%d)\\%% Processor Time",i); if (PdhAddCounter(query,counterPath,0,&cntCpuUsage[i+1])!=ERROR_SUCCESS) { - WriteLog("PdhAddCounter(%s) failed\r\n",counterPath); + WriteLog(MSG_PDH_ADD_COUNTER_FAILED,EVENTLOG_ERROR_TYPE,"s",counterPath); PdhCloseQuery(query); return; } @@ -103,7 +103,8 @@ void CollectorThread(void *) if (PdhCollectQueryData(query)!=ERROR_SUCCESS) { - WriteLog("PdhCollectQueryData failed\r\n"); + WriteLog(MSG_PDH_COLLECT_QUERY_DATA_FAILED,EVENTLOG_ERROR_TYPE,"s", + GetSystemErrorText(GetLastError())); PdhCloseQuery(query); return; } @@ -115,7 +116,7 @@ void CollectorThread(void *) // Prepare for CPU execution queue usage collection if (PdhAddCounter(query,"\\System\\Processor Queue Length",0,&cntCpuQueue)!=ERROR_SUCCESS) { - WriteLog("PdhAddCounter(\\System\\Processor Queue Length) failed\r\n"); + WriteLog(MSG_PDH_ADD_COUNTER_FAILED,EVENTLOG_ERROR_TYPE,"s","\\System\\Processor Queue Length"); PdhCloseQuery(query); return; } @@ -130,13 +131,13 @@ void CollectorThread(void *) { cptr->interval=-1; // Flag for unsupported counters cptr->lastValue=NOTSUPPORTED; - WriteLog("Unable to add user-defined counter %s=\"%s\" to query\r\n", + WriteLog(MSG_USERDEF_COUNTER_FAILED,EVENTLOG_ERROR_TYPE,"ss", cptr->name,cptr->counterPath); } } // Data collection loop - WriteLog("Initialization complete\r\n"); + WriteLog(MSG_COLLECTOR_INIT_OK,EVENTLOG_INFORMATION_TYPE,NULL); do { LONG sum; @@ -145,7 +146,8 @@ void CollectorThread(void *) dwTicksStart=GetTickCount(); if ((status=PdhCollectQueryData(query))!=ERROR_SUCCESS) - WriteLog("PdhCollectQueryData failed (status=%08X)\r\n",status); + WriteLog(MSG_PDH_COLLECT_QUERY_DATA_FAILED,EVENTLOG_ERROR_TYPE,"s", + GetSystemErrorText(GetLastError())); // Process CPU utilization data for(i=0;i<=sysInfo.dwNumberOfProcessors;i++) @@ -231,7 +233,7 @@ void CollectorThread(void *) // Calculate time spent on sample processing and issue warning if it exceeds threshold dwTicksElapsed=GetTickCount()-dwTicksStart; if (dwTicksElapsed>confMaxProcTime) - WriteLog("Processing took more then %d milliseconds (%d milliseconds)\r\n", + WriteLog(MSG_BIG_PROCESSING_TIME,EVENTLOG_WARNING_TYPE,"dd", confMaxProcTime,dwTicksElapsed); // Save processing time to history buffer diff --git a/src/zabbix_agent_win32/comm.cpp b/src/zabbix_agent_win32/comm.cpp index 046fc5df..9832d928 100644 --- a/src/zabbix_agent_win32/comm.cpp +++ b/src/zabbix_agent_win32/comm.cpp @@ -85,14 +85,14 @@ static void CommThread(void *param) timeout.tv_usec=0; if (select(sock+1,&rdfs,NULL,NULL,&timeout)==0) { - WriteLog("Timed out waiting for server command\r\n"); + WriteLog(MSG_COMMAND_TIMEOUT,EVENTLOG_WARNING_TYPE,NULL); goto end_session; } rc=recv(sock,rq.cmd,MAX_ZABBIX_CMD_LEN,0); if (rc<=0) { - WriteLog("recv() failed [%s]\r\n",strerror(errno)); + WriteLog(MSG_RECV_ERROR,EVENTLOG_ERROR_TYPE,"s",strerror(errno)); goto end_session; } @@ -102,7 +102,7 @@ static void CommThread(void *param) if (WaitForSingleObject(hThread,confTimeout)==WAIT_TIMEOUT) { strcpy(rq.result,"ZBX_ERROR\n"); - WriteLog("Timed out while processing request (%s)\r\n",rq.cmd); + WriteLog(MSG_REQUEST_TIMEOUT,EVENTLOG_WARNING_TYPE,"s",rq.cmd); } send(sock,rq.result,strlen(rq.result),0); @@ -135,7 +135,7 @@ void ListenerThread(void *) // Create socket if ((sock=socket(AF_INET,SOCK_STREAM,0))==-1) { - WriteLog("Cannot open socket: %s\r\n",GetSystemErrorText(WSAGetLastError())); + WriteLog(MSG_SOCKET_ERROR,EVENTLOG_ERROR_TYPE,"s",GetSystemErrorText(WSAGetLastError())); exit(1); } @@ -148,13 +148,12 @@ void ListenerThread(void *) // Bind socket if (bind(sock,(struct sockaddr *)&servAddr,sizeof(struct sockaddr_in))!=0) { - WriteLog("Cannot bind socket: %s\r\n",GetSystemErrorText(WSAGetLastError())); + WriteLog(MSG_BIND_ERROR,EVENTLOG_ERROR_TYPE,"s",GetSystemErrorText(WSAGetLastError())); exit(1); } // Set up queue listen(sock,SOMAXCONN); - WriteLog("Accepting connections on port %d\r\n",confListenPort); // Wait for connection requests while(1) @@ -162,7 +161,7 @@ void ListenerThread(void *) iSize=sizeof(struct sockaddr_in); if ((sockClient=accept(sock,(struct sockaddr *)&servAddr,&iSize))==-1) { - WriteLog("accept() error: %s\n",GetSystemErrorText(WSAGetLastError())); + WriteLog(MSG_ACCEPT_ERROR,EVENTLOG_ERROR_TYPE,"s",GetSystemErrorText(WSAGetLastError())); closesocket(sock); exit(1); } diff --git a/src/zabbix_agent_win32/config.cpp b/src/zabbix_agent_win32/config.cpp index 017908fc..d0f875d0 100644 --- a/src/zabbix_agent_win32/config.cpp +++ b/src/zabbix_agent_win32/config.cpp @@ -167,8 +167,16 @@ BOOL ReadConfig(void) if (!stricmp(buffer,"LogFile")) { - memset(logFile,0,MAX_PATH); - strncpy(logFile,ptr,MAX_PATH-1); + if (!stricmp(ptr,"{EventLog}")) + { + optUseEventLog=TRUE; + } + else + { + optUseEventLog=FALSE; + memset(logFile,0,MAX_PATH); + strncpy(logFile,ptr,MAX_PATH-1); + } } else if (!stricmp(buffer,"Server")) { diff --git a/src/zabbix_agent_win32/log.cpp b/src/zabbix_agent_win32/log.cpp index cba743d6..8609befd 100644 --- a/src/zabbix_agent_win32/log.cpp +++ b/src/zabbix_agent_win32/log.cpp @@ -39,23 +39,30 @@ static HANDLE mutexLogAccess; void InitLog(void) { - char tbuf[32],buffer[256]; - struct tm *loc; - time_t t; - DWORD size; + if (optUseEventLog) + { + hLog=RegisterEventSource(NULL,ZABBIX_EVENT_SOURCE); + } + else + { + char tbuf[32],buffer[256]; + struct tm *loc; + time_t t; + DWORD size; - hLog=CreateFile(logFile,GENERIC_WRITE,FILE_SHARE_READ,NULL,OPEN_ALWAYS, - FILE_ATTRIBUTE_NORMAL,NULL); - if (hLog==INVALID_HANDLE_VALUE) - return; - SetFilePointer(hLog,0,NULL,FILE_END); - t=time(NULL); - loc=localtime(&t); - strftime(tbuf,32,"%d-%b-%Y %H:%M:%S",loc); - sprintf(buffer,"**************************************************************\r\n[%s] Log file opened\r\n",tbuf); - WriteFile(hLog,buffer,strlen(buffer),&size,NULL); - - mutexLogAccess=CreateMutex(NULL,FALSE,NULL); + hLog=CreateFile(logFile,GENERIC_WRITE,FILE_SHARE_READ,NULL,OPEN_ALWAYS, + FILE_ATTRIBUTE_NORMAL,NULL); + if (hLog==INVALID_HANDLE_VALUE) + return; + SetFilePointer(hLog,0,NULL,FILE_END); + t=time(NULL); + loc=localtime(&t); + strftime(tbuf,32,"%d-%b-%Y %H:%M:%S",loc); + sprintf(buffer,"**************************************************************\r\n[%s] Log file opened\r\n",tbuf); + WriteFile(hLog,buffer,strlen(buffer),&size,NULL); + + mutexLogAccess=CreateMutex(NULL,FALSE,NULL); + } } @@ -65,21 +72,27 @@ void InitLog(void) void CloseLog(void) { - if (hLog!=INVALID_HANDLE_VALUE) - CloseHandle(hLog); - if (mutexLogAccess!=INVALID_HANDLE_VALUE) - CloseHandle(mutexLogAccess); + if (optUseEventLog) + { + DeregisterEventSource(hLog); + } + else + { + if (hLog!=INVALID_HANDLE_VALUE) + CloseHandle(hLog); + if (mutexLogAccess!=INVALID_HANDLE_VALUE) + CloseHandle(mutexLogAccess); + } } // -// Write log +// Write record to log file // -void WriteLog(char *format,...) +static void WriteLogToFile(char *message) { - va_list args; - char *prefix,buffer[4096]; + char *prefix,buffer[64]; DWORD size; time_t t; struct tm *loc; @@ -102,13 +115,85 @@ void WriteLog(char *format,...) printf("%s",prefix); } - va_start(args,format); - vsprintf(buffer,format,args); - va_end(args); - - WriteFile(hLog,buffer,strlen(buffer),&size,NULL); + WriteFile(hLog,message,strlen(message),&size,NULL); if (optStandalone) - printf("%s",buffer); + printf("%s",message); ReleaseMutex(mutexLogAccess); } + + +// +// Write log record +// Parameters: +// msg - Message ID +// wType - Message type (see ReportEvent() for details) +// format - Parameter format string, each parameter represented by one character. +// The following format characters can be used: +// s - String +// d - Decimal integer +// x - Hex integer +// + +void WriteLog(DWORD msg,WORD wType,char *format...) +{ + va_list args; + char *strings[16]; + int numStrings=0; + + memset(strings,0,sizeof(char *)*16); + + if (format!=NULL) + { + va_start(args,format); + + for(;(format[numStrings]!=0)&&(numStrings<16);numStrings++) + { + switch(format[numStrings]) + { + case 's': + strings[numStrings]=strdup(va_arg(args,char *)); + break; + case 'd': + strings[numStrings]=(char *)malloc(16); + sprintf(strings[numStrings],"%d",va_arg(args,LONG)); + break; + case 'x': + strings[numStrings]=(char *)malloc(16); + sprintf(strings[numStrings],"0x%08X",va_arg(args,DWORD)); + break; + default: + strings[numStrings]=(char *)malloc(32); + sprintf(strings[numStrings],"BAD FORMAT (0x%08X)",va_arg(args,DWORD)); + break; + } + } + va_end(args); + } + + if (optUseEventLog) + { + ReportEvent(hLog,wType,0,msg,NULL,numStrings,0,(const char **)strings,NULL); + } + else + { + LPVOID lpMsgBuf; + + if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_HMODULE | FORMAT_MESSAGE_ARGUMENT_ARRAY, + NULL,msg,0,(LPTSTR)&lpMsgBuf,0,strings)>0) + { + WriteLogToFile((char *)lpMsgBuf); + LocalFree(lpMsgBuf); + } + else + { + char message[64]; + + sprintf(message,"MSG 0x%08X - Unable to find message text\r\n",msg); + WriteLogToFile(message); + } + } + + while(--numStrings>=0) + free(strings[numStrings]); +} diff --git a/src/zabbix_agent_win32/main.cpp b/src/zabbix_agent_win32/main.cpp index 18254528..b6da16c8 100644 --- a/src/zabbix_agent_win32/main.cpp +++ b/src/zabbix_agent_win32/main.cpp @@ -29,9 +29,10 @@ // DWORD dwTlsLogPrefix; -BOOL optStandalone=FALSE; HANDLE eventShutdown; +BOOL optStandalone=FALSE; +BOOL optUseEventLog=TRUE; char confFile[MAX_PATH]="C:\\zabbix_agentd.conf"; char logFile[MAX_PATH]="C:\\zabbix_agentd.log"; WORD confListenPort=10000; @@ -42,6 +43,7 @@ DWORD confMaxProcTime=100; // 100 milliseconds is default acceptable collector s DWORD (__stdcall *imp_GetGuiResources)(HANDLE,DWORD); BOOL (__stdcall *imp_GetProcessIoCounters)(HANDLE,PIO_COUNTERS); +BOOL (__stdcall *imp_GetPerformanceInfo)(PPERFORMANCE_INFORMATION,DWORD); // @@ -54,7 +56,7 @@ static FARPROC GetProcAddressAndLog(HMODULE hModule,LPCSTR procName) ptr=GetProcAddress(hModule,procName); if (ptr==NULL) - WriteLog("Unable to get address for function \"%s\"\r\n",procName); + WriteLog(MSG_NO_FUNCTION,EVENTLOG_WARNING_TYPE,"s",procName); return ptr; } @@ -74,7 +76,7 @@ static void ImportSymbols(void) } else { - WriteLog("Unable to get handle to USER32.DLL\r\n"); + WriteLog(MSG_NO_DLL,EVENTLOG_WARNING_TYPE,"s","USER32.DLL"); } hModule=GetModuleHandle("KERNEL32.DLL"); @@ -84,7 +86,17 @@ static void ImportSymbols(void) } else { - WriteLog("Unable to get handle to KERNEL32.DLL\r\n"); + WriteLog(MSG_NO_DLL,EVENTLOG_WARNING_TYPE,"s","KERNEL32.DLL"); + } + + hModule=GetModuleHandle("PSAPI.DLL"); + if (hModule!=NULL) + { + imp_GetPerformanceInfo=(BOOL (__stdcall *)(PPERFORMANCE_INFORMATION,DWORD))GetProcAddressAndLog(hModule,"GetPerformanceInfo"); + } + else + { + WriteLog(MSG_NO_DLL,EVENTLOG_WARNING_TYPE,"s","PSAPI.DLL"); } } @@ -131,7 +143,7 @@ void Shutdown(void) { SetEvent(eventShutdown); Sleep(1000); // Allow other threads to terminate - WriteLog("Zabbix Win32 agent shutdown\r\n"); + WriteLog(MSG_AGENT_SHUTDOWN,EVENTLOG_INFORMATION_TYPE,NULL); CloseLog(); TlsFree(dwTlsLogPrefix); } @@ -143,7 +155,7 @@ void Shutdown(void) void Main(void) { - WriteLog("Zabbix Win32 agent started\r\n"); + WriteLog(MSG_AGENT_STARTED,EVENTLOG_INFORMATION_TYPE,NULL); if (optStandalone) { diff --git a/src/zabbix_agent_win32/messages.mc b/src/zabbix_agent_win32/messages.mc new file mode 100644 index 00000000..ecde95da --- /dev/null +++ b/src/zabbix_agent_win32/messages.mc @@ -0,0 +1,143 @@ +;/**************************************************************************** +; Messages for Zabbix Win32 Agent +;****************************************************************************/ +; +;#ifndef _messages_h_ +;#define _messages_h_ +; + +MessageIdTypedef=DWORD + +MessageId=1 +SymbolicName=MSG_AGENT_STARTED +Language=English +Zabbix Win32 Agent started +. + +MessageId= +SymbolicName=MSG_AGENT_SHUTDOWN +Language=English +Zabbix Win32 Agent stopped +. + +MessageId= +SymbolicName=MSG_PDH_OPEN_QUERY_FAILED +Language=English +Call to PdhOpenQuery() failed: %1 +. + +MessageId= +SymbolicName=MSG_PDH_ADD_COUNTER_FAILED +Language=English +Unable to add performance counter "%1" to query +. + +MessageId= +SymbolicName=MSG_PDH_COLLECT_QUERY_DATA_FAILED +Language=English +Call to PdhCollectQueryData() failed: %1 +. + +MessageId= +SymbolicName=MSG_USERDEF_COUNTER_FAILED +Language=English +Unable to add user-defined counter "%1" (expanded to "%2") to query +. + +MessageId= +SymbolicName=MSG_COLLECTOR_INIT_OK +Language=English +Collector thread initialized successfully +. + +MessageId= +SymbolicName=MSG_BIG_PROCESSING_TIME +Language=English +Processing took more then %1 milliseconds (%2 milliseconds) +. + +MessageId= +SymbolicName=MSG_COMMAND_TIMEOUT +Language=English +Timeout occured waiting for server command +. + +MessageId= +SymbolicName=MSG_RECV_ERROR +Language=English +Error receiving data from socket: %1 +. + +MessageId= +SymbolicName=MSG_REQUEST_TIMEOUT +Language=English +Timed out while processing request. Requested parameter is "%1" +. + +MessageId= +SymbolicName=MSG_SOCKET_ERROR +Language=English +Unable to open socket: %1 +. + +MessageId= +SymbolicName=MSG_BIND_ERROR +Language=English +Unable to bind socket: %1 +. + +MessageId= +SymbolicName=MSG_ACCEPT_ERROR +Language=English +Unable to accept incoming connection: %1 +. + +MessageId= +SymbolicName=MSG_NO_FUNCTION +Language=English +Unable to resolve symbol "%1" +. + +MessageId= +SymbolicName=MSG_NO_DLL +Language=English +Unable to get handle to "%1" +. + +MessageId= +SymbolicName=MSG_UNEXPECTED_ATTRIBUTE +Language=English +Internal error: unexpected process attribute code %1 in GetProcessAttribute() +. + +MessageId= +SymbolicName=MSG_UNEXPECTED_TYPE +Language=English +Internal error: unexpected type code %1 in GetProcessAttribute() +. + +MessageId= +SymbolicName=MSG_SERVICE_STOPPED +Language=English +Service stoppped +. + +MessageId= +SymbolicName=MSG_FILE_MAP_FAILED +Language=English +Unable to create mapping for file "%1": %2 +. + +MessageId= +SymbolicName=MSG_MAP_VIEW_FAILED +Language=English +MapViewOfFile(%1) failed: %2 +. + +MessageId= +SymbolicName=MSG_UNEXPECTED_IRC +Language=English +Internal error: unexpected iRC=%1 in ProcessCommand("%2") +. + +;#endif diff --git a/src/zabbix_agent_win32/procinfo.cpp b/src/zabbix_agent_win32/procinfo.cpp index 7d25fdfd..ace2dd36 100644 --- a/src/zabbix_agent_win32/procinfo.cpp +++ b/src/zabbix_agent_win32/procinfo.cpp @@ -21,7 +21,6 @@ **/ #include "zabbixw32.h" -#include <psapi.h> // @@ -128,7 +127,7 @@ static double GetProcessAttribute(HANDLE hProcess,int attr,int type,int count,do value=(double)((__int64)ioCounters.OtherOperationCount); break; default: // Unknown attribute - WriteLog("INTERNAL ERROR: Unexpected attribute code %d in GetProcessAttribute()\r\n",attr); + WriteLog(MSG_UNEXPECTED_ATTRIBUTE,EVENTLOG_ERROR_TYPE,"x",attr); value=0; } @@ -149,7 +148,7 @@ static double GetProcessAttribute(HANDLE hProcess,int attr,int type,int count,do case 3: // sum return lastValue+value; default: - WriteLog("INTERNAL ERROR: Unexpected type %d in GetProcessAttribute()\r\n",type); + WriteLog(MSG_UNEXPECTED_TYPE,EVENTLOG_ERROR_TYPE,"x",type); return 0; } } diff --git a/src/zabbix_agent_win32/resource.h b/src/zabbix_agent_win32/resource.h new file mode 100644 index 00000000..2fe35e9b --- /dev/null +++ b/src/zabbix_agent_win32/resource.h @@ -0,0 +1,15 @@ +//{{NO_DEPENDENCIES}} +// Microsoft Developer Studio generated include file. +// Used by zabbixw32.rc +// + +// Next default values for new objects +// +#ifdef APSTUDIO_INVOKED +#ifndef APSTUDIO_READONLY_SYMBOLS +#define _APS_NEXT_RESOURCE_VALUE 102 +#define _APS_NEXT_COMMAND_VALUE 40001 +#define _APS_NEXT_CONTROL_VALUE 1000 +#define _APS_NEXT_SYMED_VALUE 101 +#endif +#endif diff --git a/src/zabbix_agent_win32/service.cpp b/src/zabbix_agent_win32/service.cpp index 0af6de1c..b9af2028 100644 --- a/src/zabbix_agent_win32/service.cpp +++ b/src/zabbix_agent_win32/service.cpp @@ -53,7 +53,7 @@ static VOID WINAPI ServiceCtrlHandler(DWORD ctrlCode) status.dwWaitHint=4000; SetServiceStatus(serviceHandle,&status); - WriteLog("Service stopped [Control code %d]\r\n",ctrlCode); +// WriteLog(MSG_SERVICE_STOPPPED,EVENTLOG_INFORMATION_TYPE,NULL); Shutdown(); status.dwCurrentState=SERVICE_STOPPED; @@ -125,8 +125,6 @@ void InitService(void) void ZabbixCreateService(char *execName) { SC_HANDLE mgr,service; - HKEY key; - DWORD disp; char cmdLine[MAX_PATH*2]; mgr=OpenSCManager(NULL,NULL,GENERIC_WRITE); @@ -156,16 +154,7 @@ void ZabbixCreateService(char *execName) CloseServiceHandle(mgr); - // Create event source in registry - if (RegCreateKeyEx(HKEY_LOCAL_MACHINE,"SYSTEM\\CurrentControlSet\\Services\\EventLog\\System\\Zabbix Win32 Agent", - 0,NULL,REG_OPTION_NON_VOLATILE,KEY_ALL_ACCESS, - NULL,&key,&disp)==ERROR_SUCCESS) - { - disp=EVENTLOG_ERROR_TYPE | EVENTLOG_WARNING_TYPE | EVENTLOG_INFORMATION_TYPE; - RegSetValueEx(key,"TypesSupported",0,REG_DWORD,(BYTE *)&disp,sizeof(DWORD)); - RegSetValueEx(key,"EventMessageFile",0,REG_EXPAND_SZ,(BYTE *)execName,strlen(execName)+1); - RegCloseKey(key); - } + ZabbixInstallEventSource(execName); } @@ -202,6 +191,8 @@ void ZabbixRemoveService(void) } CloseServiceHandle(mgr); + + ZabbixRemoveEventSource(); } @@ -277,3 +268,47 @@ void ZabbixStopService(void) CloseServiceHandle(mgr); } + + +// +// Install event source +// + +void ZabbixInstallEventSource(char *path) +{ + HKEY hKey; + DWORD dwTypes=EVENTLOG_ERROR_TYPE | EVENTLOG_WARNING_TYPE | EVENTLOG_INFORMATION_TYPE; + + if (ERROR_SUCCESS!=RegCreateKeyEx(HKEY_LOCAL_MACHINE, + "System\\CurrentControlSet\\Services\\EventLog\\System\\" ZABBIX_EVENT_SOURCE, + 0,NULL,REG_OPTION_NON_VOLATILE,KEY_SET_VALUE,NULL,&hKey,NULL)) + { + printf("Unable to create registry key: %s\n",GetSystemErrorText(GetLastError())); + return; + } + + RegSetValueEx(hKey,"TypesSupported",0,REG_DWORD,(BYTE *)&dwTypes,sizeof(DWORD)); + RegSetValueEx(hKey,"EventMessageFile",0,REG_EXPAND_SZ,(BYTE *)path,strlen(path)+1); + + RegCloseKey(hKey); + printf("Event source \"" ZABBIX_EVENT_SOURCE "\" installed successfully\n"); +} + + +// +// Remove event source +// + +void ZabbixRemoveEventSource(void) +{ + if (ERROR_SUCCESS==RegDeleteKey(HKEY_LOCAL_MACHINE, + "System\\CurrentControlSet\\Services\\EventLog\\System\\" ZABBIX_EVENT_SOURCE)) + { + printf("Event source \"" ZABBIX_EVENT_SOURCE "\" uninstalled successfully\n"); + } + else + { + printf("Unable to uninstall event source \"" ZABBIX_EVENT_SOURCE "\": %s\n", + GetSystemErrorText(GetLastError())); + } +} diff --git a/src/zabbix_agent_win32/sysinfo.cpp b/src/zabbix_agent_win32/sysinfo.cpp index 6ad571b2..78b0d912 100644 --- a/src/zabbix_agent_win32/sysinfo.cpp +++ b/src/zabbix_agent_win32/sysinfo.cpp @@ -21,7 +21,6 @@ **/ #include "zabbixw32.h" -#include <psapi.h> // @@ -195,27 +194,34 @@ static LONG H_ProcCountSpecific(char *cmd,char *arg,double *value) static LONG H_MemoryInfo(char *cmd,char *arg,double *value) { - MEMORYSTATUS ms; -// PERFORMANCE_INFORMATION pfi; + if (!strcmp(cmd,"memory[cached]")) + { + PERFORMANCE_INFORMATION pfi; -/* if (!strcmp(cmd,"memory[cached]")) - GetPerformanceInfo(&pfi,sizeof(PERFORMANCE_INFORMATION)); - else*/ - GlobalMemoryStatus(&ms); + if (imp_GetPerformanceInfo==NULL) + return SYSINFO_RC_NOTSUPPORTED; - if (!strcmp(cmd,"memory[total]")) - *value=(double)ms.dwTotalPhys; - else if (!strcmp(cmd,"memory[free]")) - *value=(double)ms.dwAvailPhys; -/* else if (!strcmp(cmd,"memory[cached]")) - *value=(double)pfi.SystemCache*(double)pfi.PageSize;*/ - else if (!strcmp(cmd,"swap[total]")) - *value=(double)ms.dwTotalPageFile; - else if (!strcmp(cmd,"swap[free]")) - *value=(double)ms.dwAvailPageFile; + imp_GetPerformanceInfo(&pfi,sizeof(PERFORMANCE_INFORMATION)); + *value=(double)pfi.SystemCache*(double)pfi.PageSize; + } else - return SYSINFO_RC_NOTSUPPORTED; + { + MEMORYSTATUS ms; + GlobalMemoryStatus(&ms); + + if (!strcmp(cmd,"memory[total]")) + *value=(double)ms.dwTotalPhys; + else if (!strcmp(cmd,"memory[free]")) + *value=(double)ms.dwAvailPhys; + else if (!strcmp(cmd,"swap[total]")) + *value=(double)ms.dwTotalPageFile; + else if (!strcmp(cmd,"swap[free]")) + *value=(double)ms.dwAvailPageFile; + else + return SYSINFO_RC_NOTSUPPORTED; + } + return SYSINFO_RC_SUCCESS; } @@ -325,20 +331,22 @@ static LONG H_PerfCounter(char *cmd,char *arg,double *value) if (PdhOpenQuery(NULL,0,&query)!=ERROR_SUCCESS) { - WriteLog("PdhOpenQuery failed\r\n"); + WriteLog(MSG_PDH_OPEN_QUERY_FAILED,EVENTLOG_ERROR_TYPE,"s", + GetSystemErrorText(GetLastError())); return SYSINFO_RC_ERROR; } if (PdhAddCounter(query,counterName,0,&counter)!=ERROR_SUCCESS) { - WriteLog("PdhAddCounter(%s) failed\r\n",counterName); + WriteLog(MSG_PDH_ADD_COUNTER_FAILED,EVENTLOG_ERROR_TYPE,"s",counterName); PdhCloseQuery(query); return SYSINFO_RC_NOTSUPPORTED; } if (PdhCollectQueryData(query)!=ERROR_SUCCESS) { - WriteLog("PdhCollectQueryData failed\r\n"); + WriteLog(MSG_PDH_COLLECT_QUERY_DATA_FAILED,EVENTLOG_ERROR_TYPE,"s", + GetSystemErrorText(GetLastError())); PdhCloseQuery(query); return SYSINFO_RC_ERROR; } @@ -398,7 +406,7 @@ static LONG H_MD5Hash(char *cmd,char *arg,char **value) hFileMapping=CreateFileMapping(hFile,NULL,PAGE_READONLY,0,0,NULL); if (hFileMapping==NULL) { - WriteLog("CreateFileMapping(%s) failed [%s]\r\n", + WriteLog(MSG_FILE_MAP_FAILED,EVENTLOG_ERROR_TYPE,"ss", fileName,GetSystemErrorText(GetLastError())); CloseHandle(hFile); return SYSINFO_RC_ERROR; @@ -408,7 +416,7 @@ static LONG H_MD5Hash(char *cmd,char *arg,char **value) data=(unsigned char *)MapViewOfFile(hFileMapping,FILE_MAP_READ,0,0,0); if (data==NULL) { - WriteLog("MapViewOfFile(%s) failed [%s]\r\n", + WriteLog(MSG_MAP_VIEW_FAILED,EVENTLOG_ERROR_TYPE,"ss", fileName,GetSystemErrorText(GetLastError())); CloseHandle(hFileMapping); CloseHandle(hFile); @@ -452,7 +460,7 @@ static LONG H_CRC32(char *cmd,char *arg,double *value) hFileMapping=CreateFileMapping(hFile,NULL,PAGE_READONLY,0,0,NULL); if (hFileMapping==NULL) { - WriteLog("CreateFileMapping(%s) failed [%s]\r\n", + WriteLog(MSG_FILE_MAP_FAILED,EVENTLOG_ERROR_TYPE,"ss", fileName,GetSystemErrorText(GetLastError())); CloseHandle(hFile); return SYSINFO_RC_ERROR; @@ -462,7 +470,7 @@ static LONG H_CRC32(char *cmd,char *arg,double *value) data=(unsigned char *)MapViewOfFile(hFileMapping,FILE_MAP_READ,0,0,0); if (data==NULL) { - WriteLog("MapViewOfFile(%s) failed [%s]\r\n", + WriteLog(MSG_MAP_VIEW_FAILED,EVENTLOG_ERROR_TYPE,"ss", fileName,GetSystemErrorText(GetLastError())); CloseHandle(hFileMapping); CloseHandle(hFile); @@ -673,8 +681,8 @@ void ProcessCommand(char *received_cmd,char *result) strcpy(result,"ZBX_ERROR\n"); break; default: - strcpy(result,"ZBX_ERROR\n"); - WriteLog("Internal error: unexpected iRC=%d in ProcessCommand(%s)\r\n",iRC,received_cmd); + strcpy(result,"ZBX_ERROR\n"); // MSG_UNEXPECETD_IRC + WriteLog(MSG_UNEXPECTED_IRC,EVENTLOG_ERROR_TYPE,"ds",iRC,received_cmd); break; } } diff --git a/src/zabbix_agent_win32/util.cpp b/src/zabbix_agent_win32/util.cpp index a278e44c..325eab30 100644 --- a/src/zabbix_agent_win32/util.cpp +++ b/src/zabbix_agent_win32/util.cpp @@ -37,6 +37,10 @@ static void Help(void) " stop : Stop Zabbix Win32 Agent service\n" " install : Install Zabbix Win32 Agent as service\n" " remove : Remove previously installed Zabbix Win32 Agent service\n" + " install-events : Install Zabbix Win32 Agent as event source for Event Log\n" + " This is done automatically when service is being installed\n" + " remove-events : Remove Zabbix Win32 Agent event source\n" + " This is done automatically when service is being removed\n" " help : Display help information\n" " version : Display version information\n\n" "And possible options are:\n" @@ -78,7 +82,8 @@ BOOL ParseCommandLine(int argc,char *argv[]) optStandalone=TRUE; return TRUE; } - else if (!strcmp(argv[i],"install")) + else if ((!strcmp(argv[i],"install"))|| + (!strcmp(argv[i],"install-events"))) { char path[MAX_PATH],*ptr; @@ -93,7 +98,10 @@ BOOL ParseCommandLine(int argc,char *argv[]) if (stricmp(&path[strlen(path)-4],".exe")) strcat(path,".exe"); - ZabbixCreateService(path); + if (!strcmp(argv[i],"install")) + ZabbixCreateService(path); + else + ZabbixInstallEventSource(path); return FALSE; } else if (!strcmp(argv[i],"remove")) @@ -101,6 +109,11 @@ BOOL ParseCommandLine(int argc,char *argv[]) ZabbixRemoveService(); return FALSE; } + else if (!strcmp(argv[i],"remove-events")) + { + ZabbixRemoveEventSource(); + return FALSE; + } else if (!strcmp(argv[i],"start")) { ZabbixStartService(); diff --git a/src/zabbix_agent_win32/zabbixw32.h b/src/zabbix_agent_win32/zabbixw32.h index c2711324..228859f2 100644 --- a/src/zabbix_agent_win32/zabbixw32.h +++ b/src/zabbix_agent_win32/zabbixw32.h @@ -27,8 +27,10 @@ #include <process.h> #include <stdio.h> #include <pdh.h> +#include <psapi.h> #include "../../include/common.h" #include "md5.h" +#include "messages.h" // @@ -40,9 +42,10 @@ #else #define DEBUG_SUFFIX #endif -#define AGENT_VERSION "1.0.0-beta8(rc3)" DEBUG_SUFFIX +#define AGENT_VERSION "1.0.0-beta8(rc4)" DEBUG_SUFFIX #define ZABBIX_SERVICE_NAME "ZabbixAgentdW32" +#define ZABBIX_EVENT_SOURCE "Zabbix Win32 Agent" #define COMMAND_TIMEOUT 5 @@ -122,9 +125,12 @@ void ZabbixRemoveService(void); void ZabbixStartService(void); void ZabbixStopService(void); +void ZabbixInstallEventSource(char *path); +void ZabbixRemoveEventSource(void); + void InitLog(void); void CloseLog(void); -void WriteLog(char *format,...); +void WriteLog(DWORD msg,WORD wType,char *format...); BOOL Initialize(void); void Shutdown(void); @@ -147,7 +153,9 @@ void ExpandAlias(char *orig,char *expanded); extern DWORD dwTlsLogPrefix; extern HANDLE eventShutdown; + extern BOOL optStandalone; +extern BOOL optUseEventLog; extern char confFile[]; extern char logFile[]; @@ -170,5 +178,6 @@ extern double statMaxCollectorTime; extern DWORD (__stdcall *imp_GetGuiResources)(HANDLE,DWORD); extern BOOL (__stdcall *imp_GetProcessIoCounters)(HANDLE,PIO_COUNTERS); +extern BOOL (__stdcall *imp_GetPerformanceInfo)(PPERFORMANCE_INFORMATION,DWORD); #endif diff --git a/src/zabbix_agent_win32/zabbixw32.rc b/src/zabbix_agent_win32/zabbixw32.rc new file mode 100644 index 00000000..6c2bc674 --- /dev/null +++ b/src/zabbix_agent_win32/zabbixw32.rc @@ -0,0 +1,70 @@ +//Microsoft Developer Studio generated resource script. +// +#include "resource.h" + +#define APSTUDIO_READONLY_SYMBOLS +///////////////////////////////////////////////////////////////////////////// +// +// Generated from the TEXTINCLUDE 2 resource. +// +#include "afxres.h" + +///////////////////////////////////////////////////////////////////////////// +#undef APSTUDIO_READONLY_SYMBOLS + +///////////////////////////////////////////////////////////////////////////// +// English (U.S.) resources + +#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU) +#ifdef _WIN32 +LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US +#pragma code_page(1252) +#endif //_WIN32 + +#ifdef APSTUDIO_INVOKED +///////////////////////////////////////////////////////////////////////////// +// +// TEXTINCLUDE +// + +1 TEXTINCLUDE DISCARDABLE +BEGIN + "resource.h\0" +END + +2 TEXTINCLUDE DISCARDABLE +BEGIN + "#include ""afxres.h""\r\n" + "\0" +END + +3 TEXTINCLUDE DISCARDABLE +BEGIN + "\r\n" + "\0" +END + +#endif // APSTUDIO_INVOKED + + +///////////////////////////////////////////////////////////////////////////// +// +// 11 +// + +1 11 DISCARDABLE "Msg00001.bin" +#endif // English (U.S.) resources +///////////////////////////////////////////////////////////////////////////// + + + +#ifndef APSTUDIO_INVOKED +///////////////////////////////////////////////////////////////////////////// +// +// Generated from the TEXTINCLUDE 3 resource. +// + + +///////////////////////////////////////////////////////////////////////////// +#endif // not APSTUDIO_INVOKED + |