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
38 static char *ps_file = "processes.rrd";
40 static char *ds_def[] =
42 "DS:running:GAUGE:"COLLECTD_HEARTBEAT":0:65535",
43 "DS:sleeping:GAUGE:"COLLECTD_HEARTBEAT":0:65535",
44 "DS:zombies:GAUGE:"COLLECTD_HEARTBEAT":0:65535",
45 "DS:stopped:GAUGE:"COLLECTD_HEARTBEAT":0:65535",
46 "DS:paging:GAUGE:"COLLECTD_HEARTBEAT":0:65535",
47 "DS:blocked:GAUGE:"COLLECTD_HEARTBEAT":0:65535",
50 static int ds_num = 6;
52 static void ps_init (void)
56 static void ps_write (char *host, char *inst, char *val)
58 rrd_update_file (host, ps_file, val, ds_def, ds_num);
61 #if PROCESSES_HAVE_READ
62 static void ps_submit (unsigned int running,
63 unsigned int sleeping,
71 if (snprintf (buf, BUFSIZE, "%u:%u:%u:%u:%u:%u:%u",
72 (unsigned int) curtime,
73 running, sleeping, zombies, stopped, paging,
77 plugin_submit (MODULE_NAME, "-", buf);
80 static void ps_read (void)
83 unsigned int running, sleeping, zombies, stopped, paging, blocked;
86 char filename[20]; /* need 17 bytes */
87 char *fields[BUFSIZE];
93 running = sleeping = zombies = stopped = paging = blocked = 0;
95 if ((proc = opendir ("/proc")) == NULL)
97 syslog (LOG_ERR, "Cannot open `/proc': %s", strerror (errno));
101 while ((ent = readdir (proc)) != NULL)
103 if (!isdigit (ent->d_name[0]))
106 if (snprintf (filename, 20, "/proc/%s/stat", ent->d_name) >= 20)
109 if ((fh = fopen (filename, "r")) == NULL)
111 syslog (LOG_ERR, "Cannot open `%s': %s", filename, strerror (errno));
115 if (fgets (buf, BUFSIZE, fh) == NULL)
123 if (strsplit (buf, fields, BUFSIZE) < 3)
126 switch (fields[2][0])
128 case 'R': running++; break;
129 case 'S': sleeping++; break;
130 case 'D': blocked++; break;
131 case 'Z': zombies++; break;
132 case 'T': stopped++; break;
133 case 'W': paging++; break;
139 ps_submit (running, sleeping, zombies, stopped, paging, blocked);
140 #endif /* defined(KERNEL_LINUX) */
143 # define ps_read NULL
144 #endif /* PROCESSES_HAVE_READ */
146 void module_register (void)
148 plugin_register (MODULE_NAME, ps_init, ps_read, ps_write);