Added copyright notices to src/multicast.[ch] and src/collectd.[ch] in trunk
[collectd.git] / src / processes.c
1 #include "processes.h"
2
3 /*
4  * Originally written by Lyonel Vincent
5  */
6
7 #if COLLECT_PROCESSES
8 #define MODULE_NAME "processes"
9
10 #include "common.h"
11 #include "plugin.h"
12
13 static char *ps_file = "processes.rrd";
14
15 static char *ds_def[] =
16 {
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",
23         NULL
24 };
25 static int ds_num = 6;
26
27 extern time_t curtime;
28
29 void ps_init (void)
30 {
31 }
32
33 void ps_write (char *host, char *inst, char *val)
34 {
35         rrd_update_file (host, ps_file, val, ds_def, ds_num);
36 }
37
38 #define BUFSIZE 256
39 void ps_submit (unsigned int running,
40                 unsigned int sleeping,
41                 unsigned int zombies,
42                 unsigned int stopped,
43                 unsigned int paging,
44                 unsigned int blocked)
45 {
46         char buf[BUFSIZE];
47
48         if (snprintf (buf, BUFSIZE, "%u:%u:%u:%u:%u:%u:%u",
49                                 (unsigned int) curtime,
50                                 running, sleeping, zombies, stopped, paging,
51                                 blocked) >= BUFSIZE)
52                 return;
53
54         plugin_submit (MODULE_NAME, "-", buf);
55 }
56
57 void ps_read (void)
58 {
59 #ifdef KERNEL_LINUX
60         unsigned int running, sleeping, zombies, stopped, paging, blocked;
61
62         char buf[BUFSIZE];
63         char filename[20]; /* need 17 bytes */
64         char *fields[256];
65
66         struct dirent *ent;
67         DIR *proc;
68         FILE *fh;
69
70         running = sleeping = zombies = stopped = paging = blocked = 0;
71
72         if ((proc = opendir ("/proc")) == NULL)
73         {
74                 syslog (LOG_ERR, "Cannot open `/proc': %s", strerror (errno));
75                 return;
76         }
77
78         int strsplit (char *string, char **fields, size_t size);
79
80         while ((ent = readdir (proc)) != NULL)
81         {
82                 if (!isdigit (ent->d_name[0]))
83                         continue;
84
85                 if (snprintf (filename, 20, "/proc/%s/stat", ent->d_name) >= 20)
86                         continue;
87
88                 if ((fh = fopen (filename, "r")) == NULL)
89                 {
90                         syslog (LOG_ERR, "Cannot open `%s': %s", filename, strerror (errno));
91                         continue;
92                 }
93
94                 if (fgets (buf, BUFSIZE, fh) == NULL)
95                 {
96                         fclose (fh);
97                         continue;
98                 }
99
100                 fclose (fh);
101
102                 if (strsplit (buf, fields, 256) < 3)
103                         continue;
104
105                 switch (fields[2][0])
106                 {
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;
113                 }
114         }
115
116         closedir(proc);
117
118         ps_submit (running, sleeping, zombies, stopped, paging, blocked);
119 #endif /* defined(KERNEL_LINUX) */
120 }
121 #undef BUFSIZE
122
123 void module_register (void)
124 {
125         plugin_register (MODULE_NAME, ps_init, ps_read, ps_write);
126 }
127
128 #undef MODULE_NAME
129 #endif /* COLLECT_PROCESSES */