0b52b9895209f09ab61b27429f4169fa3ee3236a
[collectd.git] / src / processes.c
1 /**
2  * collectd - src/processes.c
3  * Copyright (C) 2005  Lyonel Vincent
4  * Copyright (C) 2006  Florian Forster (Mach code)
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the
8  * Free Software Foundation; either version 2 of the License, or (at your
9  * option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
19  *
20  * Authors:
21  *   Lyonel Vincent <lyonel at ezix.org>
22  *   Florian octo Forster <octo at verplant.org>
23  **/
24
25 #include "collectd.h"
26 #include "common.h"
27 #include "plugin.h"
28 #include "utils_debug.h"
29 #include "configfile.h"
30
31 /* Include header files for the mach system, if they exist.. */
32 #if HAVE_THREAD_INFO
33 #  if HAVE_MACH_MACH_INIT_H
34 #    include <mach/mach_init.h>
35 #  endif
36 #  if HAVE_MACH_HOST_PRIV_H
37 #    include <mach/host_priv.h>
38 #  endif
39 #  if HAVE_MACH_MACH_ERROR_H
40 #    include <mach/mach_error.h>
41 #  endif
42 #  if HAVE_MACH_MACH_HOST_H
43 #    include <mach/mach_host.h>
44 #  endif
45 #  if HAVE_MACH_MACH_PORT_H
46 #    include <mach/mach_port.h>
47 #  endif
48 #  if HAVE_MACH_MACH_TYPES_H
49 #    include <mach/mach_types.h>
50 #  endif
51 #  if HAVE_MACH_MESSAGE_H
52 #    include <mach/message.h>
53 #  endif
54 #  if HAVE_MACH_PROCESSOR_SET_H
55 #    include <mach/processor_set.h>
56 #  endif
57 #  if HAVE_MACH_TASK_H
58 #    include <mach/task.h>
59 #  endif
60 #  if HAVE_MACH_THREAD_ACT_H
61 #    include <mach/thread_act.h>
62 #  endif
63 #  if HAVE_MACH_VM_REGION_H
64 #    include <mach/vm_region.h>
65 #  endif
66 #  if HAVE_MACH_VM_MAP_H
67 #    include <mach/vm_map.h>
68 #  endif
69 #  if HAVE_MACH_VM_PROT_H
70 #    include <mach/vm_prot.h>
71 #  endif
72 /* #endif HAVE_THREAD_INFO */
73
74 #elif KERNEL_LINUX
75 #  if HAVE_LINUX_CONFIG_H
76 #    include <linux/config.h>
77 #  endif
78 #  ifndef CONFIG_HZ
79 #    define CONFIG_HZ 100
80 #  endif
81 #endif /* KERNEL_LINUX */
82
83 #define MODULE_NAME "processes"
84
85 #if HAVE_THREAD_INFO || KERNEL_LINUX
86 # define PROCESSES_HAVE_READ 1
87 #else
88 # define PROCESSES_HAVE_READ 0
89 #endif
90
91 #define BUFSIZE 256
92
93 static char *processes_file = "processes.rrd";
94 static char *processes_ds_def[] =
95 {
96         "DS:running:GAUGE:"COLLECTD_HEARTBEAT":0:65535",
97         "DS:sleeping:GAUGE:"COLLECTD_HEARTBEAT":0:65535",
98         "DS:zombies:GAUGE:"COLLECTD_HEARTBEAT":0:65535",
99         "DS:stopped:GAUGE:"COLLECTD_HEARTBEAT":0:65535",
100         "DS:paging:GAUGE:"COLLECTD_HEARTBEAT":0:65535",
101         "DS:blocked:GAUGE:"COLLECTD_HEARTBEAT":0:65535",
102         NULL
103 };
104 static int processes_ds_num = 6;
105
106 static char *ps_rss_file = "processes/ps_rss-%s.rrd";
107 static char *ps_rss_ds_def[] =
108 {
109         /* max = 2^63 - 1 */
110         "DS:byte:GAUGE:"COLLECTD_HEARTBEAT":0:9223372036854775807",
111         NULL
112 };
113 static int ps_rss_ds_num = 1;
114
115 static char *ps_cputime_file = "processes/ps_cputime-%s.rrd";
116 static char *ps_cputime_ds_def[] =
117 {
118         /* 1 second in user-mode per second ought to be enough.. */
119         "DS:user:COUNTER:"COLLECTD_HEARTBEAT":0:1000000",
120         "DS:syst:COUNTER:"COLLECTD_HEARTBEAT":0:1000000",
121         NULL
122 };
123 static int ps_cputime_ds_num = 2;
124
125 static char *ps_count_file = "processes/ps_count-%s.rrd";
126 static char *ps_count_ds_def[] =
127 {
128         "DS:processes:GAUGE:"COLLECTD_HEARTBEAT":0:65535",
129         "DS:threads:GAUGE:"COLLECTD_HEARTBEAT":0:65535",
130         NULL
131 };
132 static int ps_count_ds_num = 2;
133
134 static char *config_keys[] =
135 {
136         "CollectName",
137         NULL
138 };
139 static int config_keys_num = 1;
140
141 typedef struct procstat
142 {
143 #define PROCSTAT_NAME_LEN 256
144         char               name[PROCSTAT_NAME_LEN];
145         unsigned int       num_proc;
146         unsigned int       num_lwp;
147         unsigned long      vmem_rss;
148         unsigned long      vmem_minflt;
149         unsigned long      vmem_majflt;
150         unsigned long long cpu_user;
151         unsigned long long cpu_system;
152         struct procstat   *next;
153 } procstat_t;
154
155 static procstat_t *list_head_g = NULL;
156
157 #if HAVE_THREAD_INFO
158 static mach_port_t port_host_self;
159 static mach_port_t port_task_self;
160
161 static processor_set_name_array_t pset_list;
162 static mach_msg_type_number_t     pset_list_len;
163 /* #endif HAVE_THREAD_INFO */
164
165 #elif KERNEL_LINUX
166 static long pagesize_g;
167 #endif /* KERNEL_LINUX */
168
169 static procstat_t *ps_list_append (procstat_t *list, const char *name)
170 {
171         procstat_t *new;
172         procstat_t *ptr;
173
174         if ((new = (procstat_t *) malloc (sizeof (procstat_t))) == NULL)
175                 return (NULL);
176         memset (new, 0, sizeof (procstat_t));
177         strncpy (new->name, name, PROCSTAT_NAME_LEN);
178
179         for (ptr = list; ptr != NULL; ptr = ptr->next)
180                 if (ptr->next == NULL)
181                         break;
182
183         if (ptr != NULL)
184                 ptr->next = new;
185
186         return (new);
187 }
188
189 static void ps_list_add (procstat_t *list, procstat_t *entry)
190 {
191         procstat_t *ptr;
192
193         ptr = list;
194         while ((ptr != NULL) && (strcmp (ptr->name, entry->name) != 0))
195                 ptr = ptr->next;
196
197         if (ptr == NULL)
198                 return;
199
200         ptr->num_proc    += entry->num_proc;
201         ptr->num_lwp     += entry->num_lwp;
202         ptr->vmem_rss    += entry->vmem_rss;
203         ptr->vmem_minflt += entry->vmem_minflt;
204         ptr->vmem_majflt += entry->vmem_majflt;
205         ptr->cpu_user    += entry->cpu_user;
206         ptr->cpu_system  += entry->cpu_system;
207 }
208
209 static void ps_list_reset (procstat_t *ps)
210 {
211         while (ps != NULL)
212         {
213                 ps->num_proc    = 0;
214                 ps->num_lwp     = 0;
215                 ps->vmem_rss    = 0;
216                 ps->vmem_minflt = 0;
217                 ps->vmem_majflt = 0;
218                 ps->cpu_user    = 0;
219                 ps->cpu_system  = 0;
220                 ps = ps->next;
221         }
222 }
223
224 static int ps_config (char *key, char *value)
225 {
226         if (strcasecmp (key, "CollectName") == 0)
227         {
228                 procstat_t *entry;
229
230                 entry = ps_list_append (list_head_g, value);
231                 if (entry == NULL)
232                 {
233                         syslog (LOG_ERR, "processes plugin: ps_list_append failed.");
234                         return (1);
235                 }
236                 if (list_head_g == NULL)
237                         list_head_g = entry;
238         }
239         else
240         {
241                 return (-1);
242         }
243
244         return (0);
245 }
246
247 static void ps_init (void)
248 {
249 #if HAVE_THREAD_INFO
250         kern_return_t status;
251
252         port_host_self = mach_host_self ();
253         port_task_self = mach_task_self ();
254
255         if (pset_list != NULL)
256         {
257                 vm_deallocate (port_task_self,
258                                 (vm_address_t) pset_list,
259                                 pset_list_len * sizeof (processor_set_t));
260                 pset_list = NULL;
261                 pset_list_len = 0;
262         }
263
264         if ((status = host_processor_sets (port_host_self,
265                                         &pset_list,
266                                         &pset_list_len)) != KERN_SUCCESS)
267         {
268                 syslog (LOG_ERR, "host_processor_sets failed: %s\n",
269                                 mach_error_string (status));
270                 pset_list = NULL;
271                 pset_list_len = 0;
272                 return;
273         }
274 /* #endif HAVE_THREAD_INFO */
275
276 #elif KERNEL_LINUX
277         pagesize_g = sysconf(_SC_PAGESIZE);
278         DBG ("pagesize_g = %li; CONFIG_HZ = %i;",
279                         pagesize_g, CONFIG_HZ);
280 #endif /* KERNEL_LINUX */
281
282         return;
283 }
284
285 static void ps_write (char *host, char *inst, char *val)
286 {
287         rrd_update_file (host, processes_file, val,
288                         processes_ds_def, processes_ds_num);
289 }
290
291 static void ps_rss_write (char *host, char *inst, char *val)
292 {
293         char filename[256];
294         int status;
295
296         status = snprintf (filename, 256, ps_rss_file, inst);
297         if ((status < 1) || (status >= 256))
298                 return;
299
300         rrd_update_file (host, filename, val, ps_rss_ds_def, ps_rss_ds_num);
301 }
302
303 static void ps_cputime_write (char *host, char *inst, char *val)
304 {
305         char filename[256];
306         int status;
307
308         status = snprintf (filename, 256, ps_cputime_file, inst);
309         if ((status < 1) || (status >= 256))
310                 return;
311
312         DBG ("host = %s; filename = %s; val = %s;",
313                         host, filename, val);
314         rrd_update_file (host, filename, val,
315                         ps_cputime_ds_def, ps_cputime_ds_num);
316 }
317
318 static void ps_count_write (char *host, char *inst, char *val)
319 {
320         char filename[256];
321         int status;
322
323         status = snprintf (filename, 256, ps_count_file, inst);
324         if ((status < 1) || (status >= 256))
325                 return;
326
327         DBG ("host = %s; filename = %s; val = %s;",
328                         host, filename, val);
329         rrd_update_file (host, filename, val,
330                         ps_count_ds_def, ps_count_ds_num);
331 }
332
333 #if PROCESSES_HAVE_READ
334 static void ps_submit (int running,
335                 int sleeping,
336                 int zombies,
337                 int stopped,
338                 int paging,
339                 int blocked)
340 {
341         char buf[BUFSIZE];
342
343         if (snprintf (buf, BUFSIZE, "%u:%i:%i:%i:%i:%i:%i",
344                                 (unsigned int) curtime,
345                                 running, sleeping, zombies, stopped, paging,
346                                 blocked) >= BUFSIZE)
347                 return;
348
349         DBG ("running = %i; sleeping = %i; zombies = %i; stopped = %i; paging = %i; blocked = %i;",
350                         running, sleeping, zombies, stopped, paging, blocked);
351
352         plugin_submit (MODULE_NAME, "-", buf);
353 }
354
355 static void ps_submit_proc (procstat_t *ps)
356 {
357         char buffer[64];
358
359         if (ps == NULL)
360                 return;
361
362         snprintf (buffer, 64, "%u:%lu",
363                         (unsigned int) curtime,
364                         ps->vmem_rss);
365         buffer[63] = '\0';
366         plugin_submit ("ps_rss", ps->name, buffer);
367
368         snprintf (buffer, 64, "%u:%u:%u",
369                         (unsigned int) curtime,
370                         /* Make the counter overflow */
371                         (unsigned int) (ps->cpu_user   & 0xFFFFFFFF),
372                         (unsigned int) (ps->cpu_system & 0xFFFFFFFF));
373         buffer[63] = '\0';
374         plugin_submit ("ps_cputime", ps->name, buffer);
375
376         snprintf (buffer, 64, "%u:%u:%u",
377                         (unsigned int) curtime,
378                         ps->num_proc, ps->num_lwp);
379         buffer[63] = '\0';
380         plugin_submit ("ps_count", ps->name, buffer);
381
382         DBG ("name = %s; num_proc = %i; num_lwp = %i; vmem_rss = %i; "
383                         "vmem_minflt = %i; vmem_majflt = %i; "
384                         "cpu_user = %i; cpu_system = %i;",
385                         ps->name, ps->num_proc, ps->num_lwp, ps->vmem_rss,
386                         ps->vmem_minflt, ps->vmem_majflt, ps->cpu_user,
387                         ps->cpu_system);
388
389 }
390
391 #if KERNEL_LINUX
392 static int *ps_read_tasks (int pid)
393 {
394         int *list = NULL;
395         int  list_size = 1; /* size of allocated space, in elements */
396         int  list_len = 0;  /* number of currently used elements */
397
398         char           dirname[64];
399         DIR           *dh;
400         struct dirent *ent;
401
402         snprintf (dirname, 64, "/proc/%i/task", pid);
403         dirname[63] = '\0';
404
405         if ((dh = opendir (dirname)) == NULL)
406         {
407                 syslog (LOG_NOTICE, "processes plugin: Failed to open directory `%s'",
408                                 dirname);
409                 return (NULL);
410         }
411
412         while ((ent = readdir (dh)) != NULL)
413         {
414                 if (!isdigit (ent->d_name[0]))
415                         continue;
416
417                 if ((list_len + 1) >= list_size)
418                 {
419                         int *new_ptr;
420                         int  new_size = 2 * list_size;
421                         /* Comes in sizes: 2, 4, 8, 16, ... */
422
423                         new_ptr = (int *) realloc (list, (size_t) (sizeof (int) * new_size));
424                         if (new_ptr == NULL)
425                         {
426                                 if (list != NULL)
427                                         free (list);
428                                 syslog (LOG_ERR, "processes plugin: "
429                                                 "Failed to allocate more memory.");
430                                 return (NULL);
431                         }
432
433                         list = new_ptr;
434                         list_size = new_size;
435
436                         memset (list + list_len, 0, sizeof (int) * (list_size - list_len));
437                 }
438
439                 list[list_len] = atoi (ent->d_name);
440                 if (list[list_len] != 0)
441                         list_len++;
442         }
443
444         closedir (dh);
445
446         assert (list_len < list_size);
447         assert (list[list_len] == 0);
448
449         return (list);
450 }
451
452 int ps_read_process (int pid, procstat_t *ps, char *state)
453 {
454         char  filename[64];
455         char  buffer[1024];
456         FILE *fh;
457
458         char *fields[64];
459         char  fields_len;
460
461         int  *tasks;
462         int   i;
463
464         int   ppid;
465         int   name_len;
466
467         memset (ps, 0, sizeof (procstat_t));
468
469         snprintf (filename, 64, "/proc/%i/stat", pid);
470         filename[63] = '\0';
471
472         if ((fh = fopen (filename, "r")) == NULL)
473                 return (-1);
474
475         if (fgets (buffer, 1024, fh) == NULL)
476         {
477                 fclose (fh);
478                 return (-1);
479         }
480
481         fclose (fh);
482
483         fields_len = strsplit (buffer, fields, 64);
484         if (fields_len < 24)
485         {
486                 DBG ("`%s' has only %i fields..",
487                                 filename, fields_len);
488                 return (-1);
489         }
490         else if (fields_len != 41)
491         {
492                 DBG ("WARNING: (fields_len = %i) != 41", fields_len);
493         }
494
495         /* copy the name, strip brackets in the process */
496         name_len = strlen (fields[1]) - 2;
497         if ((fields[1][0] != '(') || (fields[1][name_len + 1] != ')'))
498         {
499                 DBG ("No brackets found in process name: `%s'", fields[1]);
500                 return (-1);
501         }
502         fields[1] = fields[1] + 1;
503         fields[1][name_len] = '\0';
504         strncpy (ps->name, fields[1], PROCSTAT_NAME_LEN);
505
506         ppid = atoi (fields[3]);
507
508         if ((tasks = ps_read_tasks (pid)) == NULL)
509         {
510                 DBG ("ps_read_tasks (%i) failed.", pid);
511                 return (-1);
512         }
513
514         *state = '\0';
515         ps->num_lwp  = 0;
516         ps->num_proc = 1;
517         for (i = 0; tasks[i] != 0; i++)
518                 ps->num_lwp++;
519
520         free (tasks);
521         tasks = NULL;
522
523         /* Leave the rest at zero if this is only an LWP */
524         if (ps->num_proc == 0)
525         {
526                 DBG ("This is only an LWP: pid = %i; name = %s;",
527                                 pid, ps->name);
528                 return (0);
529         }
530
531         ps->vmem_minflt = atol  (fields[9]);
532         ps->vmem_majflt = atol  (fields[11]);
533         ps->cpu_user    = atoll (fields[13]);
534         ps->cpu_system  = atoll (fields[14]);
535         ps->vmem_rss    = atol  (fields[23]);
536         
537         /* Convert jiffies to useconds */
538         ps->cpu_user   = ps->cpu_user   * 1000000 / CONFIG_HZ;
539         ps->cpu_system = ps->cpu_system * 1000000 / CONFIG_HZ;
540         ps->vmem_rss   = ps->vmem_rss * pagesize_g;
541
542         *state = fields[2][0];
543
544         /* success */
545         return (0);
546 } /* int ps_read_process (...) */
547 #endif /* KERNEL_LINUX */
548
549 static void ps_read (void)
550 {
551 #if HAVE_THREAD_INFO
552         kern_return_t            status;
553
554         int                      pset;
555         processor_set_t          port_pset_priv;
556
557         int                      task;
558         task_array_t             task_list;
559         mach_msg_type_number_t   task_list_len;
560
561         int                      thread;
562         thread_act_array_t       thread_list;
563         mach_msg_type_number_t   thread_list_len;
564         thread_basic_info_data_t thread_data;
565         mach_msg_type_number_t   thread_data_len;
566
567         int running  = 0;
568         int sleeping = 0;
569         int zombies  = 0;
570         int stopped  = 0;
571         int blocked  = 0;
572
573         /*
574          * The Mach-concept is a little different from the traditional UNIX
575          * concept: All the work is done in threads. Threads are contained in
576          * `tasks'. Therefore, `task status' doesn't make much sense, since
577          * it's actually a `thread status'.
578          * Tasks are assigned to sets of processors, so that's where you go to
579          * get a list.
580          */
581         for (pset = 0; pset < pset_list_len; pset++)
582         {
583                 if ((status = host_processor_set_priv (port_host_self,
584                                                 pset_list[pset],
585                                                 &port_pset_priv)) != KERN_SUCCESS)
586                 {
587                         syslog (LOG_ERR, "host_processor_set_priv failed: %s\n",
588                                         mach_error_string (status));
589                         continue;
590                 }
591
592                 if ((status = processor_set_tasks (port_pset_priv,
593                                                 &task_list,
594                                                 &task_list_len)) != KERN_SUCCESS)
595                 {
596                         syslog (LOG_ERR, "processor_set_tasks failed: %s\n",
597                                         mach_error_string (status));
598                         mach_port_deallocate (port_task_self, port_pset_priv);
599                         continue;
600                 }
601
602                 for (task = 0; task < task_list_len; task++)
603                 {
604                         status = task_threads (task_list[task], &thread_list,
605                                         &thread_list_len);
606                         if (status != KERN_SUCCESS)
607                         {
608                                 /* Apple's `top' treats this case a zombie. It
609                                  * makes sense to some extend: A `zombie'
610                                  * thread is nonsense, since the task/process
611                                  * is dead. */
612                                 zombies++;
613                                 DBG ("task_threads failed: %s",
614                                                 mach_error_string (status));
615                                 if (task_list[task] != port_task_self)
616                                         mach_port_deallocate (port_task_self,
617                                                         task_list[task]);
618                                 continue; /* with next task_list */
619                         }
620
621                         for (thread = 0; thread < thread_list_len; thread++)
622                         {
623                                 thread_data_len = THREAD_BASIC_INFO_COUNT;
624                                 status = thread_info (thread_list[thread],
625                                                 THREAD_BASIC_INFO,
626                                                 (thread_info_t) &thread_data,
627                                                 &thread_data_len);
628                                 if (status != KERN_SUCCESS)
629                                 {
630                                         syslog (LOG_ERR, "thread_info failed: %s\n",
631                                                         mach_error_string (status));
632                                         if (task_list[task] != port_task_self)
633                                                 mach_port_deallocate (port_task_self,
634                                                                 thread_list[thread]);
635                                         continue; /* with next thread_list */
636                                 }
637
638                                 switch (thread_data.run_state)
639                                 {
640                                         case TH_STATE_RUNNING:
641                                                 running++;
642                                                 break;
643                                         case TH_STATE_STOPPED:
644                                         /* What exactly is `halted'? */
645                                         case TH_STATE_HALTED:
646                                                 stopped++;
647                                                 break;
648                                         case TH_STATE_WAITING:
649                                                 sleeping++;
650                                                 break;
651                                         case TH_STATE_UNINTERRUPTIBLE:
652                                                 blocked++;
653                                                 break;
654                                         /* There is no `zombie' case here,
655                                          * since there are no zombie-threads.
656                                          * There's only zombie tasks, which are
657                                          * handled above. */
658                                         default:
659                                                 syslog (LOG_WARNING,
660                                                                 "Unknown thread status: %s",
661                                                                 thread_data.run_state);
662                                                 break;
663                                 } /* switch (thread_data.run_state) */
664
665                                 if (task_list[task] != port_task_self)
666                                 {
667                                         status = mach_port_deallocate (port_task_self,
668                                                         thread_list[thread]);
669                                         if (status != KERN_SUCCESS)
670                                                 syslog (LOG_ERR, "mach_port_deallocate failed: %s",
671                                                                 mach_error_string (status));
672                                 }
673                         } /* for (thread_list) */
674
675                         if ((status = vm_deallocate (port_task_self,
676                                                         (vm_address_t) thread_list,
677                                                         thread_list_len * sizeof (thread_act_t)))
678                                         != KERN_SUCCESS)
679                         {
680                                 syslog (LOG_ERR, "vm_deallocate failed: %s",
681                                                 mach_error_string (status));
682                         }
683                         thread_list = NULL;
684                         thread_list_len = 0;
685
686                         /* Only deallocate the task port, if it isn't our own.
687                          * Don't know what would happen in that case, but this
688                          * is what Apple's top does.. ;) */
689                         if (task_list[task] != port_task_self)
690                         {
691                                 status = mach_port_deallocate (port_task_self,
692                                                 task_list[task]);
693                                 if (status != KERN_SUCCESS)
694                                         syslog (LOG_ERR, "mach_port_deallocate failed: %s",
695                                                         mach_error_string (status));
696                         }
697                 } /* for (task_list) */
698
699                 if ((status = vm_deallocate (port_task_self,
700                                 (vm_address_t) task_list,
701                                 task_list_len * sizeof (task_t))) != KERN_SUCCESS)
702                 {
703                         syslog (LOG_ERR, "vm_deallocate failed: %s",
704                                         mach_error_string (status));
705                 }
706                 task_list = NULL;
707                 task_list_len = 0;
708
709                 if ((status = mach_port_deallocate (port_task_self, port_pset_priv))
710                                 != KERN_SUCCESS)
711                 {
712                         syslog (LOG_ERR, "mach_port_deallocate failed: %s",
713                                         mach_error_string (status));
714                 }
715         } /* for (pset_list) */
716
717         ps_submit (running, sleeping, zombies, stopped, -1, blocked);
718 /* #endif HAVE_THREAD_INFO */
719
720 #elif KERNEL_LINUX
721         int running  = 0;
722         int sleeping = 0;
723         int zombies  = 0;
724         int stopped  = 0;
725         int paging   = 0;
726         int blocked  = 0;
727
728         struct dirent *ent;
729         DIR           *proc;
730         int            pid;
731
732         int        status;
733         procstat_t ps;
734         char       state;
735
736         procstat_t *ps_ptr;
737
738         running = sleeping = zombies = stopped = paging = blocked = 0;
739         ps_list_reset (list_head_g);
740
741         if ((proc = opendir ("/proc")) == NULL)
742         {
743                 syslog (LOG_ERR, "Cannot open `/proc': %s", strerror (errno));
744                 return;
745         }
746
747         while ((ent = readdir (proc)) != NULL)
748         {
749                 if (!isdigit (ent->d_name[0]))
750                         continue;
751
752                 if ((pid = atoi (ent->d_name)) < 1)
753                         continue;
754
755                 status = ps_read_process (pid, &ps, &state);
756                 if (status != 0)
757                 {
758                         DBG ("ps_read_process failed: %i", status);
759                         continue;
760                 }
761
762                 switch (state)
763                 {
764                         case 'R': running++;  break;
765                         case 'S': sleeping++; break;
766                         case 'D': blocked++;  break;
767                         case 'Z': zombies++;  break;
768                         case 'T': stopped++;  break;
769                         case 'W': paging++;   break;
770                 }
771
772                 if (list_head_g != NULL)
773                         ps_list_add (list_head_g, &ps);
774         }
775
776         closedir (proc);
777
778         ps_submit (running, sleeping, zombies, stopped, paging, blocked);
779
780         for (ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
781                 ps_submit_proc (ps_ptr);
782 #endif /* KERNEL_LINUX */
783 }
784 #else
785 # define ps_read NULL
786 #endif /* PROCESSES_HAVE_READ */
787
788 void module_register (void)
789 {
790         plugin_register (MODULE_NAME, ps_init, ps_read, ps_write);
791         plugin_register ("ps_rss", NULL, NULL, ps_rss_write);
792         plugin_register ("ps_cputime", NULL, NULL, ps_cputime_write);
793         plugin_register ("ps_count", NULL, NULL, ps_count_write);
794         cf_register (MODULE_NAME, ps_config, config_keys, config_keys_num);
795 }
796
797 #undef BUFSIZE
798 #undef MODULE_NAME