process name matching chaged from patern to exact match
[public/netxms.git] / src / agent / subagents / linux / proc.cpp
1 /* $Id: proc.cpp,v 1.2 2005-01-18 17:09:33 alk Exp $ */
2
3 /*
4 ** NetXMS subagent for GNU/Linux
5 ** Copyright (C) 2004 Alex Kirhenshtein
6 **
7 ** This program is free software; you can redistribute it and/or modify
8 ** it under the terms of the GNU General Public License as published by
9 ** the Free Software Foundation; either version 2 of the License, or
10 ** (at your option) any later version.
11 **
12 ** This program is distributed in the hope that it will be useful,
13 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ** GNU General Public License for more details.
16 **
17 ** You should have received a copy of the GNU General Public License
18 ** along with this program; if not, write to the Free Software
19 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 **
21 **/
22
23 #include <stdio.h>
24 #include <string.h>
25 #include <stdlib.h>
26 #include <sys/types.h>
27 #include <dirent.h>
28
29 #include "proc.h"
30
31 static int ProcFilter(const struct dirent *pEnt)
32 {
33 char *pTmp;
34
35 if (pEnt == NULL)
36 {
37 return 0; // ignore file
38 }
39
40 pTmp = (char *)pEnt->d_name;
41 while (*pTmp != 0)
42 {
43 if (*pTmp < '0' || *pTmp > '9')
44 {
45 return 0; // skip
46 }
47 pTmp++;
48 }
49
50 return 1;
51 }
52
53 int ProcRead(PROC_ENT **pEnt, char *szPatern)
54 {
55 struct dirent **pNameList;
56 int nCount, nFound;
57
58 nFound = -1;
59
60 nCount = scandir("/proc", &pNameList, &ProcFilter, alphasort);
61 if (nCount < 0)
62 {
63 //perror("scandir");
64 }
65 else
66 {
67 nFound = 0;
68
69 if (nCount > 0 && pEnt != NULL)
70 {
71 *pEnt = (PROC_ENT *)malloc(sizeof(PROC_ENT) * (nCount + 1));
72
73 if (*pEnt == NULL)
74 {
75 nFound = -1;
76 // cleanup
77 while(nCount--)
78 {
79 free(pNameList[nCount]);
80 }
81 }
82 else
83 {
84 memset(*pEnt, 0, sizeof(PROC_ENT) * (nCount + 1));
85 }
86 }
87
88 while(nCount--)
89 {
90 char szFileName[256];
91 FILE *hFile;
92
93 snprintf(szFileName, sizeof(szFileName),
94 "/proc/%s/stat", pNameList[nCount]->d_name);
95 hFile = fopen(szFileName, "r");
96 if (hFile != NULL)
97 {
98 char szBuff[1024];
99 if (fgets(szBuff, sizeof(szBuff), hFile) != NULL)
100 {
101 char *pProcName;
102 unsigned int nPid;
103
104 if (sscanf(szBuff, "%lu ", &nPid) == 1)
105 {
106 pProcName = strchr(szBuff, ')');
107 if (pProcName != NULL)
108 {
109 *pProcName = 0;
110
111 pProcName = strchr(szBuff, '(');
112 if (pProcName != NULL)
113 {
114 pProcName++;
115
116 if (szPatern == NULL ||
117 strcasecmp(pProcName, szPatern) == 0)
118 {
119 nFound++;
120 if (pEnt != NULL)
121 {
122 (*pEnt)[nFound].nPid = nPid;
123 strncpy((*pEnt)[nFound].szProcName, pProcName,
124 sizeof((*pEnt)[nFound].szProcName));
125 }
126 }
127 }
128 }
129 }
130 }
131
132 fclose(hFile);
133 }
134 free(pNameList[nCount]);
135 }
136 free(pNameList);
137 }
138
139 return nFound;
140 }
141
142 ///////////////////////////////////////////////////////////////////////////////
143 /*
144
145 $Log: not supported by cvs2svn $
146 Revision 1.1 2004/10/22 22:08:34 alk
147 source restructured;
148 implemented:
149 Net.IP.Forwarding
150 Net.IP6.Forwarding
151 Process.Count(*)
152 Net.ArpCache
153 Net.InterfaceList (if-type not implemented yet)
154 System.ProcessList
155
156
157 */