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;
46 extern time_t curtime;
52 void ps_write (char *host, char *inst, char *val)
54 rrd_update_file (host, ps_file, val, ds_def, ds_num);
58 void ps_submit (unsigned int running,
59 unsigned int sleeping,
67 if (snprintf (buf, BUFSIZE, "%u:%u:%u:%u:%u:%u:%u",
68 (unsigned int) curtime,
69 running, sleeping, zombies, stopped, paging,
73 plugin_submit (MODULE_NAME, "-", buf);
79 unsigned int running, sleeping, zombies, stopped, paging, blocked;
82 char filename[20]; /* need 17 bytes */
89 running = sleeping = zombies = stopped = paging = blocked = 0;
91 if ((proc = opendir ("/proc")) == NULL)
93 syslog (LOG_ERR, "Cannot open `/proc': %s", strerror (errno));
97 while ((ent = readdir (proc)) != NULL)
99 if (!isdigit (ent->d_name[0]))
102 if (snprintf (filename, 20, "/proc/%s/stat", ent->d_name) >= 20)
105 if ((fh = fopen (filename, "r")) == NULL)
107 syslog (LOG_ERR, "Cannot open `%s': %s", filename, strerror (errno));
111 if (fgets (buf, BUFSIZE, fh) == NULL)
119 if (strsplit (buf, fields, 256) < 3)
122 switch (fields[2][0])
124 case 'R': running++; break;
125 case 'S': sleeping++; break;
126 case 'D': blocked++; break;
127 case 'Z': zombies++; break;
128 case 'T': stopped++; break;
129 case 'W': paging++; break;
135 ps_submit (running, sleeping, zombies, stopped, paging, blocked);
136 #endif /* defined(KERNEL_LINUX) */
140 void module_register (void)
142 plugin_register (MODULE_NAME, ps_init, ps_read, ps_write);
146 #endif /* COLLECT_PROCESSES */