2 * collectd - src/processes.c
3 * Copyright (C) 2005 Lyonel Vincent
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.
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.
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
20 * Lyonel Vincent <lyonel at ezix.org>
21 * Florian octo Forster <octo at verplant.org>
28 #define MODULE_NAME "processes"
31 # define PROCESSES_HAVE_READ 1
33 # define PROCESSES_HAVE_READ 0
36 static char *ps_file = "processes.rrd";
38 static char *ds_def[] =
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",
48 static int ds_num = 6;
54 void ps_write (char *host, char *inst, char *val)
56 rrd_update_file (host, ps_file, val, ds_def, ds_num);
60 void ps_submit (unsigned int running,
61 unsigned int sleeping,
69 if (snprintf (buf, BUFSIZE, "%u:%u:%u:%u:%u:%u:%u",
70 (unsigned int) curtime,
71 running, sleeping, zombies, stopped, paging,
75 plugin_submit (MODULE_NAME, "-", buf);
78 #if PROCESSES_HAVE_READ
82 unsigned int running, sleeping, zombies, stopped, paging, blocked;
85 char filename[20]; /* need 17 bytes */
92 running = sleeping = zombies = stopped = paging = blocked = 0;
94 if ((proc = opendir ("/proc")) == NULL)
96 syslog (LOG_ERR, "Cannot open `/proc': %s", strerror (errno));
100 while ((ent = readdir (proc)) != NULL)
102 if (!isdigit (ent->d_name[0]))
105 if (snprintf (filename, 20, "/proc/%s/stat", ent->d_name) >= 20)
108 if ((fh = fopen (filename, "r")) == NULL)
110 syslog (LOG_ERR, "Cannot open `%s': %s", filename, strerror (errno));
114 if (fgets (buf, BUFSIZE, fh) == NULL)
122 if (strsplit (buf, fields, 256) < 3)
125 switch (fields[2][0])
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;
138 ps_submit (running, sleeping, zombies, stopped, paging, blocked);
139 #endif /* defined(KERNEL_LINUX) */
141 #endif /* PROCESSES_HAVE_READ */
144 void module_register (void)
146 plugin_register (MODULE_NAME, ps_init,
147 #if PROCESSES_HAVE_READ