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>
24 #include "processes.h"
27 #define MODULE_NAME "processes"
32 static char *ps_file = "processes.rrd";
34 static char *ds_def[] =
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",
44 static int ds_num = 6;
50 void ps_write (char *host, char *inst, char *val)
52 rrd_update_file (host, ps_file, val, ds_def, ds_num);
56 void ps_submit (unsigned int running,
57 unsigned int sleeping,
65 if (snprintf (buf, BUFSIZE, "%u:%u:%u:%u:%u:%u:%u",
66 (unsigned int) curtime,
67 running, sleeping, zombies, stopped, paging,
71 plugin_submit (MODULE_NAME, "-", buf);
77 unsigned int running, sleeping, zombies, stopped, paging, blocked;
80 char filename[20]; /* need 17 bytes */
87 running = sleeping = zombies = stopped = paging = blocked = 0;
89 if ((proc = opendir ("/proc")) == NULL)
91 syslog (LOG_ERR, "Cannot open `/proc': %s", strerror (errno));
95 while ((ent = readdir (proc)) != NULL)
97 if (!isdigit (ent->d_name[0]))
100 if (snprintf (filename, 20, "/proc/%s/stat", ent->d_name) >= 20)
103 if ((fh = fopen (filename, "r")) == NULL)
105 syslog (LOG_ERR, "Cannot open `%s': %s", filename, strerror (errno));
109 if (fgets (buf, BUFSIZE, fh) == NULL)
117 if (strsplit (buf, fields, 256) < 3)
120 switch (fields[2][0])
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;
133 ps_submit (running, sleeping, zombies, stopped, paging, blocked);
134 #endif /* defined(KERNEL_LINUX) */
138 void module_register (void)
140 plugin_register (MODULE_NAME, ps_init, ps_read, ps_write);
144 #endif /* COLLECT_PROCESSES */