4 * Originally written by Lyonel Vincent
8 #define MODULE_NAME "processes"
13 static char *ps_file = "processes.rrd";
15 static char *ds_def[] =
17 "DS:running:GAUGE:25:0:65535",
18 "DS:sleeping:GAUGE:25:0:65535",
19 "DS:zombies:GAUGE:25:0:65535",
20 "DS:stopped:GAUGE:25:0:65535",
21 "DS:paging:GAUGE:25:0:65535",
22 "DS:blocked:GAUGE:25:0:65535",
25 static int ds_num = 6;
27 extern time_t curtime;
33 void ps_write (char *host, char *inst, char *val)
35 rrd_update_file (host, ps_file, val, ds_def, ds_num);
39 void ps_submit (unsigned int running,
40 unsigned int sleeping,
48 if (snprintf (buf, BUFSIZE, "%u:%u:%u:%u:%u:%u:%u",
49 (unsigned int) curtime,
50 running, sleeping, zombies, stopped, paging,
54 plugin_submit (MODULE_NAME, "-", buf);
60 unsigned int running, sleeping, zombies, stopped, paging, blocked;
63 char filename[20]; /* need 17 bytes */
70 running = sleeping = zombies = stopped = paging = blocked = 0;
72 if ((proc = opendir ("/proc")) == NULL)
74 syslog (LOG_ERR, "Cannot open `/proc': %s", strerror (errno));
78 int strsplit (char *string, char **fields, size_t size);
80 while ((ent = readdir (proc)) != NULL)
82 if (!isdigit (ent->d_name[0]))
85 if (snprintf (filename, 20, "/proc/%s/stat", ent->d_name) >= 20)
88 if ((fh = fopen (filename, "r")) == NULL)
90 syslog (LOG_ERR, "Cannot open `%s': %s", filename, strerror (errno));
94 if (fgets (buf, BUFSIZE, fh) == NULL)
102 if (strsplit (buf, fields, 256) < 3)
105 switch (fields[2][0])
107 case 'R': running++; break;
108 case 'S': sleeping++; break;
109 case 'D': blocked++; break;
110 case 'Z': zombies++; break;
111 case 'T': stopped++; break;
112 case 'W': paging++; break;
118 ps_submit (running, sleeping, zombies, stopped, paging, blocked);
119 #endif /* defined(KERNEL_LINUX) */
123 void module_register (void)
125 plugin_register (MODULE_NAME, ps_init, ps_read, ps_write);
129 #endif /* COLLECT_PROCESSES */