summaryrefslogtreecommitdiffstats
path: root/src/zabbix_agent_win32/procinfo.cpp
blob: ace2dd369650aaed1f007e192fd8c705b86e35d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/* 
** ZabbixW32 - Win32 agent for Zabbix
** Copyright (C) 2002 Victor Kirhenshtein
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
**
** $module: sysinfo.cpp
**
**/

#include "zabbixw32.h"


//
// Convert process time from FILETIME structure (100-nanosecond units) to double (milliseconds)
//

static double ConvertProcessTime(FILETIME *lpft)
{
   __int64 i;

   memcpy(&i,lpft,sizeof(__int64));
   i/=10000;      // Convert 100-nanosecond units to milliseconds
   return (double)i;
}


//
// Check if attribute supported or not
//

static BOOL IsAttributeSupported(int attr)
{
   switch(attr)
   {
      case 5:        // gdiobj
      case 6:        // userobj
         if (imp_GetGuiResources==NULL)
            return FALSE;     // No appropriate function available, probably we are running on NT4
         break;
      case 7:        // io_read_b
      case 8:        // io_read_op
      case 9:        // io_write_b
      case 10:       // io_write_op
      case 11:       // io_other_b
      case 12:       // io_other_op
         if (imp_GetProcessIoCounters==NULL)
            return FALSE;     // No appropriate function available, probably we are running on NT4
         break;
      default:
         break;
   }

   return TRUE;
}


//
// Get specific process attribute
//

static double GetProcessAttribute(HANDLE hProcess,int attr,int type,int count,double lastValue)
{
   double value;  
   PROCESS_MEMORY_COUNTERS mc;
   IO_COUNTERS ioCounters;
   FILETIME ftCreate,ftExit,ftKernel,ftUser;

   // Get value for current process instance
   switch(attr)
   {
      case 0:        // vmsize
         GetProcessMemoryInfo(hProcess,&mc,sizeof(PROCESS_MEMORY_COUNTERS));
         value=(double)mc.PagefileUsage/1024;   // Convert to Kbytes
         break;
      case 1:        // wkset
         GetProcessMemoryInfo(hProcess,&mc,sizeof(PROCESS_MEMORY_COUNTERS));
         value=(double)mc.WorkingSetSize/1024;   // Convert to Kbytes
         break;
      case 2:        // pf
         GetProcessMemoryInfo(hProcess,&mc,sizeof(PROCESS_MEMORY_COUNTERS));
         value=(double)mc.PageFaultCount;
         break;
      case 3:        // ktime
      case 4:        // utime
         GetProcessTimes(hProcess,&ftCreate,&ftExit,&ftKernel,&ftUser);
         value=ConvertProcessTime(attr==3 ? &ftKernel : &ftUser);
         break;
      case 5:        // gdiobj
      case 6:        // userobj
         value=(double)imp_GetGuiResources(hProcess,attr==5 ? 0 : 1);
         break;
      case 7:        // io_read_b
         imp_GetProcessIoCounters(hProcess,&ioCounters);
         value=(double)((__int64)ioCounters.ReadTransferCount);
         break;
      case 8:        // io_read_op
         imp_GetProcessIoCounters(hProcess,&ioCounters);
         value=(double)((__int64)ioCounters.ReadOperationCount);
         break;
      case 9:        // io_write_b
         imp_GetProcessIoCounters(hProcess,&ioCounters);
         value=(double)((__int64)ioCounters.WriteTransferCount);
         break;
      case 10:       // io_write_op
         imp_GetProcessIoCounters(hProcess,&ioCounters);
         value=(double)((__int64)ioCounters.WriteOperationCount);
         break;
      case 11:       // io_other_b
         imp_GetProcessIoCounters(hProcess,&ioCounters);
         value=(double)((__int64)ioCounters.OtherTransferCount);
         break;
      case 12:       // io_other_op
         imp_GetProcessIoCounters(hProcess,&ioCounters);
         value=(double)((__int64)ioCounters.OtherOperationCount);
         break;
      default:       // Unknown attribute
         WriteLog(MSG_UNEXPECTED_ATTRIBUTE,EVENTLOG_ERROR_TYPE,"x",attr);
         value=0;
   }

   // Recalculate final value according to selected type
   if (count==1)     // First instance
   {
      return value;
   }

   switch(type)
   {
      case 0:     // min
         return min(lastValue,value);
      case 1:     // max
         return max(lastValue,value);
      case 2:     // avg
         return (lastValue*(count-1)+value)/count;
      case 3:     // sum
         return lastValue+value;
      default:
         WriteLog(MSG_UNEXPECTED_TYPE,EVENTLOG_ERROR_TYPE,"x",type);
         return 0;
   }
}


//
// Get process-specific information
// Parameter has the following syntax:
//    proc_info[<process>:<attribute>:<type>]
// where
//    <process>   - process name (same as in proc_cnt[] parameter)
//    <attribute> - requested process attribute (see documentation for list of valid attributes)
//    <type>      - representation type (meaningful when more than one process with the same
//                  name exists). Valid values are:
//         min - minimal value among all processes named <process>
//         max - maximal value among all processes named <process>
//         avg - average value for all processes named <process>
//         sum - sum of values for all processes named <process>
//

LONG H_ProcInfo(char *cmd,char *arg,double *value)
{
   char buffer[256];
   char *ptr1,*ptr2;
   int attr,type,i,procCount,counter;
   DWORD *procList,dwSize;
   HMODULE *modList;
   static char *attrList[]=
   {
      "vmsize",
      "wkset",
      "pf",
      "ktime",
      "utime",
      "gdiobj",
      "userobj",
      "io_read_b",
      "io_read_op",
      "io_write_b",
      "io_write_op",
      "io_other_b",
      "io_other_op",
      NULL
   };
   static char *typeList[]={ "min","max","avg","sum" };

   // Get parameter arguments
   GetParameterInstance(cmd,buffer,255);
   if (!MatchString("*:*:*",buffer))
      return SYSINFO_RC_NOTSUPPORTED;     // Invalid parameter syntax

   // Parse arguments
   ptr1=strchr(buffer,':');
   *ptr1=0;
   ptr1++;
   ptr2=strchr(ptr1,':');
   *ptr2=0;
   ptr2++;  // Now ptr1 points to attribute and ptr2 points to type

   // Get attribute code from string
   for(attr=0;attrList[attr]!=NULL;attr++)
      if (!strcmp(attrList[attr],ptr1))
         break;
   if (attrList[attr]==NULL)
      return SYSINFO_RC_NOTSUPPORTED;     // Unsupported attribute

   if (!IsAttributeSupported(attr))
      return SYSINFO_RC_NOTSUPPORTED;     // Unsupported attribute

   // Get type code from string
   for(type=0;typeList[type]!=NULL;type++)
      if (!strcmp(typeList[type],ptr2))
         break;
   if (typeList[type]==NULL)
      return SYSINFO_RC_NOTSUPPORTED;     // Unsupported type

   // Gather information
   *value=0;   // Initialize to zero
   procList=(DWORD *)malloc(MAX_PROCESSES*sizeof(DWORD));
   modList=(HMODULE *)malloc(MAX_MODULES*sizeof(HMODULE));
   EnumProcesses(procList,sizeof(DWORD)*MAX_PROCESSES,&dwSize);
   procCount=dwSize/sizeof(DWORD);
   for(i=0,counter=0;i<procCount;i++)
   {
      HANDLE hProcess;

      hProcess=OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,FALSE,procList[i]);
      if (hProcess!=NULL)
      {
         if (EnumProcessModules(hProcess,modList,sizeof(HMODULE)*MAX_MODULES,&dwSize))
         {
            if (dwSize>=sizeof(HMODULE))     // At least one module exist
            {
               char baseName[MAX_PATH];

               GetModuleBaseName(hProcess,modList[0],baseName,sizeof(baseName));
               if (!stricmp(baseName,buffer))
               {
                  counter++;  // Number of processes with specific name
                  *value=GetProcessAttribute(hProcess,attr,type,counter,*value);
               }
            }
         }
         CloseHandle(hProcess);
      }
   }

   // Cleanup
   free(procList);
   free(modList);

   return SYSINFO_RC_SUCCESS;
}