Merged revision 304 (removal of all `extern time_t curtime' in all plugins) to the...
[collectd.git] / src / processes.c
1 /**
2  * collectd - src/processes.c
3  * Copyright (C) 2005  Lyonel Vincent
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; either version 2 of the License, or (at your
8  * option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
18  *
19  * Authors:
20  *   Lyonel Vincent <lyonel at ezix.org>
21  *   Florian octo Forster <octo at verplant.org>
22  **/
23
24 #include "processes.h"
25
26 #if COLLECT_PROCESSES
27 #define MODULE_NAME "processes"
28
29 #include "common.h"
30 #include "plugin.h"
31
32 static char *ps_file = "processes.rrd";
33
34 static char *ds_def[] =
35 {
36         "DS:running:GAUGE:25:0:65535",
37         "DS:sleeping:GAUGE:25:0:65535",
38         "DS:zombies:GAUGE:25:0:65535",
39         "DS:stopped:GAUGE:25:0:65535",
40         "DS:paging:GAUGE:25:0:65535",
41         "DS:blocked:GAUGE:25:0:65535",
42         NULL
43 };
44 static int ds_num = 6;
45
46 void ps_init (void)
47 {
48 }
49
50 void ps_write (char *host, char *inst, char *val)
51 {
52         rrd_update_file (host, ps_file, val, ds_def, ds_num);
53 }
54
55 #define BUFSIZE 256
56 void ps_submit (unsigned int running,
57                 unsigned int sleeping,
58                 unsigned int zombies,
59                 unsigned int stopped,
60                 unsigned int paging,
61                 unsigned int blocked)
62 {
63         char buf[BUFSIZE];
64
65         if (snprintf (buf, BUFSIZE, "%u:%u:%u:%u:%u:%u:%u",
66                                 (unsigned int) curtime,
67                                 running, sleeping, zombies, stopped, paging,
68                                 blocked) >= BUFSIZE)
69                 return;
70
71         plugin_submit (MODULE_NAME, "-", buf);
72 }
73
74 void ps_read (void)
75 {
76 #ifdef KERNEL_LINUX
77         unsigned int running, sleeping, zombies, stopped, paging, blocked;
78
79         char buf[BUFSIZE];
80         char filename[20]; /* need 17 bytes */
81         char *fields[256];
82
83         struct dirent *ent;
84         DIR *proc;
85         FILE *fh;
86
87         running = sleeping = zombies = stopped = paging = blocked = 0;
88
89         if ((proc = opendir ("/proc")) == NULL)
90         {
91                 syslog (LOG_ERR, "Cannot open `/proc': %s", strerror (errno));
92                 return;
93         }
94
95         while ((ent = readdir (proc)) != NULL)
96         {
97                 if (!isdigit (ent->d_name[0]))
98                         continue;
99
100                 if (snprintf (filename, 20, "/proc/%s/stat", ent->d_name) >= 20)
101                         continue;
102
103                 if ((fh = fopen (filename, "r")) == NULL)
104                 {
105                         syslog (LOG_ERR, "Cannot open `%s': %s", filename, strerror (errno));
106                         continue;
107                 }
108
109                 if (fgets (buf, BUFSIZE, fh) == NULL)
110                 {
111                         fclose (fh);
112                         continue;
113                 }
114
115                 fclose (fh);
116
117                 if (strsplit (buf, fields, 256) < 3)
118                         continue;
119
120                 switch (fields[2][0])
121                 {
122                         case 'R': running++;  break;
123                         case 'S': sleeping++; break;
124                         case 'D': blocked++;  break;
125                         case 'Z': zombies++;  break;
126                         case 'T': stopped++;  break;
127                         case 'W': paging++;   break;
128                 }
129         }
130
131         closedir(proc);
132
133         ps_submit (running, sleeping, zombies, stopped, paging, blocked);
134 #endif /* defined(KERNEL_LINUX) */
135 }
136 #undef BUFSIZE
137
138 void module_register (void)
139 {
140         plugin_register (MODULE_NAME, ps_init, ps_read, ps_write);
141 }
142
143 #undef MODULE_NAME
144 #endif /* COLLECT_PROCESSES */