Cleaned up all the modules, unified define order and the like..
[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 "collectd.h"
25 #include "common.h"
26 #include "plugin.h"
27
28 #define MODULE_NAME "processes"
29
30 #ifdef KERNEL_LINUX
31 # define PROCESSES_HAVE_READ 1
32 #else
33 # define PROCESSES_HAVE_READ 0
34 #endif
35
36 static char *ps_file = "processes.rrd";
37
38 static char *ds_def[] =
39 {
40         "DS:running:GAUGE:25:0:65535",
41         "DS:sleeping:GAUGE:25:0:65535",
42         "DS:zombies:GAUGE:25:0:65535",
43         "DS:stopped:GAUGE:25:0:65535",
44         "DS:paging:GAUGE:25:0:65535",
45         "DS:blocked:GAUGE:25:0:65535",
46         NULL
47 };
48 static int ds_num = 6;
49
50 static void ps_init (void)
51 {
52 }
53
54 static void ps_write (char *host, char *inst, char *val)
55 {
56         rrd_update_file (host, ps_file, val, ds_def, ds_num);
57 }
58
59 #define BUFSIZE 256
60 static void ps_submit (unsigned int running,
61                 unsigned int sleeping,
62                 unsigned int zombies,
63                 unsigned int stopped,
64                 unsigned int paging,
65                 unsigned int blocked)
66 {
67         char buf[BUFSIZE];
68
69         if (snprintf (buf, BUFSIZE, "%u:%u:%u:%u:%u:%u:%u",
70                                 (unsigned int) curtime,
71                                 running, sleeping, zombies, stopped, paging,
72                                 blocked) >= BUFSIZE)
73                 return;
74
75         plugin_submit (MODULE_NAME, "-", buf);
76 }
77
78 #if PROCESSES_HAVE_READ
79 static void ps_read (void)
80 {
81 #ifdef KERNEL_LINUX
82         unsigned int running, sleeping, zombies, stopped, paging, blocked;
83
84         char buf[BUFSIZE];
85         char filename[20]; /* need 17 bytes */
86         char *fields[256];
87
88         struct dirent *ent;
89         DIR *proc;
90         FILE *fh;
91
92         running = sleeping = zombies = stopped = paging = blocked = 0;
93
94         if ((proc = opendir ("/proc")) == NULL)
95         {
96                 syslog (LOG_ERR, "Cannot open `/proc': %s", strerror (errno));
97                 return;
98         }
99
100         while ((ent = readdir (proc)) != NULL)
101         {
102                 if (!isdigit (ent->d_name[0]))
103                         continue;
104
105                 if (snprintf (filename, 20, "/proc/%s/stat", ent->d_name) >= 20)
106                         continue;
107
108                 if ((fh = fopen (filename, "r")) == NULL)
109                 {
110                         syslog (LOG_ERR, "Cannot open `%s': %s", filename, strerror (errno));
111                         continue;
112                 }
113
114                 if (fgets (buf, BUFSIZE, fh) == NULL)
115                 {
116                         fclose (fh);
117                         continue;
118                 }
119
120                 fclose (fh);
121
122                 if (strsplit (buf, fields, 256) < 3)
123                         continue;
124
125                 switch (fields[2][0])
126                 {
127                         case 'R': running++;  break;
128                         case 'S': sleeping++; break;
129                         case 'D': blocked++;  break;
130                         case 'Z': zombies++;  break;
131                         case 'T': stopped++;  break;
132                         case 'W': paging++;   break;
133                 }
134         }
135
136         closedir(proc);
137
138         ps_submit (running, sleeping, zombies, stopped, paging, blocked);
139 #endif /* defined(KERNEL_LINUX) */
140 }
141 #else
142 # define ps_read NULL
143 #endif /* PROCESSES_HAVE_READ */
144 #undef BUFSIZE
145
146 void module_register (void)
147 {
148         plugin_register (MODULE_NAME, ps_init, ps_read, ps_write);
149 }
150
151 #undef MODULE_NAME