8b937968e2d834f5cbacfab049e9c35b428ef83e
[collectd.git] / src / processes.c
1 /**
2  * collectd - src/processes.c
3  * Copyright (C) 2005       Lyonel Vincent
4  * Copyright (C) 2006-2008  Florian octo Forster
5  * Copyright (C) 2008       Oleg King
6  * Copyright (C) 2009       Sebastian Harl
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License as published by the
10  * Free Software Foundation; either version 2 of the License, or (at your
11  * option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
21  *
22  * Authors:
23  *   Lyonel Vincent <lyonel at ezix.org>
24  *   Florian octo Forster <octo at verplant.org>
25  *   Oleg King <king2 at kaluga.ru>
26  *   Sebastian Harl <sh at tokkee.org>
27  **/
28
29 #include "collectd.h"
30 #include "common.h"
31 #include "plugin.h"
32 #include "configfile.h"
33
34 /* Include header files for the mach system, if they exist.. */
35 #if HAVE_THREAD_INFO
36 #  if HAVE_MACH_MACH_INIT_H
37 #    include <mach/mach_init.h>
38 #  endif
39 #  if HAVE_MACH_HOST_PRIV_H
40 #    include <mach/host_priv.h>
41 #  endif
42 #  if HAVE_MACH_MACH_ERROR_H
43 #    include <mach/mach_error.h>
44 #  endif
45 #  if HAVE_MACH_MACH_HOST_H
46 #    include <mach/mach_host.h>
47 #  endif
48 #  if HAVE_MACH_MACH_PORT_H
49 #    include <mach/mach_port.h>
50 #  endif
51 #  if HAVE_MACH_MACH_TYPES_H
52 #    include <mach/mach_types.h>
53 #  endif
54 #  if HAVE_MACH_MESSAGE_H
55 #    include <mach/message.h>
56 #  endif
57 #  if HAVE_MACH_PROCESSOR_SET_H
58 #    include <mach/processor_set.h>
59 #  endif
60 #  if HAVE_MACH_TASK_H
61 #    include <mach/task.h>
62 #  endif
63 #  if HAVE_MACH_THREAD_ACT_H
64 #    include <mach/thread_act.h>
65 #  endif
66 #  if HAVE_MACH_VM_REGION_H
67 #    include <mach/vm_region.h>
68 #  endif
69 #  if HAVE_MACH_VM_MAP_H
70 #    include <mach/vm_map.h>
71 #  endif
72 #  if HAVE_MACH_VM_PROT_H
73 #    include <mach/vm_prot.h>
74 #  endif
75 #  if HAVE_SYS_SYSCTL_H
76 #    include <sys/sysctl.h>
77 #  endif
78 /* #endif HAVE_THREAD_INFO */
79
80 #elif KERNEL_LINUX
81 #  if HAVE_LINUX_CONFIG_H
82 #    include <linux/config.h>
83 #  endif
84 #  ifndef CONFIG_HZ
85 #    define CONFIG_HZ 100
86 #  endif
87 /* #endif KERNEL_LINUX */
88
89 #elif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD
90 #  include <kvm.h>
91 #  include <sys/param.h>
92 #  include <sys/sysctl.h>
93 #  include <sys/user.h>
94 #  include <sys/proc.h>
95 /* #endif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD */
96
97 #else
98 # error "No applicable input method."
99 #endif
100
101 #if HAVE_REGEX_H
102 # include <regex.h>
103 #endif
104
105 #ifndef ARG_MAX
106 #  define ARG_MAX 4096
107 #endif
108
109 #define BUFSIZE 256
110
111 static const char *config_keys[] =
112 {
113         "Process",
114         "ProcessMatch"
115 };
116 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
117
118 typedef struct procstat_entry_s
119 {
120         unsigned long id;
121         unsigned long age;
122
123         unsigned long num_proc;
124         unsigned long num_lwp;
125         unsigned long vmem_size;
126         unsigned long vmem_rss;
127         unsigned long stack_size;
128
129         unsigned long vmem_minflt;
130         unsigned long vmem_majflt;
131         unsigned long vmem_minflt_counter;
132         unsigned long vmem_majflt_counter;
133
134         unsigned long cpu_user;
135         unsigned long cpu_system;
136         unsigned long cpu_user_counter;
137         unsigned long cpu_system_counter;
138
139         struct procstat_entry_s *next;
140 } procstat_entry_t;
141
142 #define PROCSTAT_NAME_LEN 256
143 typedef struct procstat
144 {
145         char          name[PROCSTAT_NAME_LEN];
146 #if HAVE_REGEX_H
147         regex_t *re;
148 #endif
149
150         unsigned long num_proc;
151         unsigned long num_lwp;
152         unsigned long vmem_size;
153         unsigned long vmem_rss;
154         unsigned long stack_size;
155
156         unsigned long vmem_minflt_counter;
157         unsigned long vmem_majflt_counter;
158
159         unsigned long cpu_user_counter;
160         unsigned long cpu_system_counter;
161
162         struct procstat   *next;
163         struct procstat_entry_s *instances;
164 } procstat_t;
165
166 static procstat_t *list_head_g = NULL;
167
168 #if HAVE_THREAD_INFO
169 static mach_port_t port_host_self;
170 static mach_port_t port_task_self;
171
172 static processor_set_name_array_t pset_list;
173 static mach_msg_type_number_t     pset_list_len;
174 /* #endif HAVE_THREAD_INFO */
175
176 #elif KERNEL_LINUX
177 static long pagesize_g;
178 /* #endif KERNEL_LINUX */
179
180 #elif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD
181 /* no global variables */
182 #endif /* HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD */
183
184 /* put name of process from config to list_head_g tree
185    list_head_g is a list of 'procstat_t' structs with
186    processes names we want to watch */
187 static void ps_list_register (const char *name, const char *regexp)
188 {
189         procstat_t *new;
190         procstat_t *ptr;
191         int status;
192
193         new = (procstat_t *) malloc (sizeof (procstat_t));
194         if (new == NULL)
195         {
196                 ERROR ("processes plugin: ps_list_register: malloc failed.");
197                 return;
198         }
199         memset (new, 0, sizeof (procstat_t));
200         sstrncpy (new->name, name, sizeof (new->name));
201
202 #if HAVE_REGEX_H
203         if (regexp != NULL)
204         {
205                 DEBUG ("ProcessMatch: adding \"%s\" as criteria to process %s.", regexp, name);
206                 new->re = (regex_t *) malloc (sizeof (regex_t));
207                 if (new->re == NULL)
208                 {
209                         ERROR ("processes plugin: ps_list_register: malloc failed.");
210                         sfree (new);
211                         return;
212                 }
213
214                 status = regcomp (new->re, regexp, REG_EXTENDED | REG_NOSUB);
215                 if (status != 0)
216                 {
217                         DEBUG ("ProcessMatch: compiling the regular expression \"%s\" failed.", regexp);
218                         sfree(new->re);
219                         return;
220                 }
221         }
222 #else
223         if (regexp != NULL)
224         {
225                 ERROR ("processes plugin: ps_list_register: "
226                                 "Regular expression \"%s\" found in config "
227                                 "file, but support for regular expressions "
228                                 "has been dispabled at compile time.",
229                                 regexp);
230                 sfree (new);
231                 return;
232         }
233 #endif
234         
235         for (ptr = list_head_g; ptr != NULL; ptr = ptr->next)
236         {
237                 if (strcmp (ptr->name, name) == 0)
238                 {
239                         WARNING ("processes plugin: You have configured more "
240                                         "than one `Process' or "
241                                         "`ProcessMatch' with the same name. "
242                                         "All but the first setting will be "
243                                         "ignored.");
244                         sfree (new->re);
245                         sfree (new);
246                         return;
247                 }
248
249                 if (ptr->next == NULL)
250                         break;
251         }
252
253         if (ptr == NULL)
254                 list_head_g = new;
255         else
256                 ptr->next = new;
257 } /* void ps_list_register */
258
259 /* try to match name against entry, returns 1 if success */
260 static int ps_list_match (const char *name, const char *cmdline, procstat_t *ps)
261 {
262 #if HAVE_REGEX_H
263         if (ps->re != NULL)
264         {
265                 int status;
266                 const char *str;
267
268                 str = cmdline;
269                 if ((str == NULL) || (str[0] == 0))
270                         str = name;
271
272                 assert (str != NULL);
273
274                 status = regexec (ps->re, str,
275                                 /* nmatch = */ 0,
276                                 /* pmatch = */ NULL,
277                                 /* eflags = */ 0);
278                 if (status == 0)
279                         return (1);
280         }
281         else
282 #endif
283         if (strcmp (ps->name, name) == 0)
284                 return (1);
285
286         return (0);
287 } /* int ps_list_match */
288
289 /* add process entry to 'instances' of process 'name' (or refresh it) */
290 static void ps_list_add (const char *name, const char *cmdline, procstat_entry_t *entry)
291 {
292         procstat_t *ps;
293         procstat_entry_t *pse;
294
295         if (entry->id == 0)
296                 return;
297
298         for (ps = list_head_g; ps != NULL; ps = ps->next)
299         {
300                 if ((ps_list_match (name, cmdline, ps)) == 0)
301                         continue;
302
303                 for (pse = ps->instances; pse != NULL; pse = pse->next)
304                         if ((pse->id == entry->id) || (pse->next == NULL))
305                                 break;
306
307                 if ((pse == NULL) || (pse->id != entry->id))
308                 {
309                         procstat_entry_t *new;
310                         
311                         new = (procstat_entry_t *) malloc (sizeof (procstat_entry_t));
312                         if (new == NULL)
313                                 return;
314                         memset (new, 0, sizeof (procstat_entry_t));
315                         new->id = entry->id;
316                         
317                         if (pse == NULL)
318                                 ps->instances = new;
319                         else
320                                 pse->next = new;
321
322                         pse = new;
323                 }
324
325                 pse->age = 0;
326                 pse->num_proc   = entry->num_proc;
327                 pse->num_lwp    = entry->num_lwp;
328                 pse->vmem_size  = entry->vmem_size;
329                 pse->vmem_rss   = entry->vmem_rss;
330                 pse->stack_size = entry->stack_size;
331
332                 ps->num_proc   += pse->num_proc;
333                 ps->num_lwp    += pse->num_lwp;
334                 ps->vmem_size  += pse->vmem_size;
335                 ps->vmem_rss   += pse->vmem_rss;
336                 ps->stack_size += pse->stack_size;
337
338                 if ((entry->vmem_minflt_counter == 0)
339                                 && (entry->vmem_majflt_counter == 0))
340                 {
341                         pse->vmem_minflt_counter += entry->vmem_minflt;
342                         pse->vmem_minflt = entry->vmem_minflt;
343
344                         pse->vmem_majflt_counter += entry->vmem_majflt;
345                         pse->vmem_majflt = entry->vmem_majflt;
346                 }
347                 else
348                 {
349                         if (entry->vmem_minflt_counter < pse->vmem_minflt_counter)
350                         {
351                                 pse->vmem_minflt = entry->vmem_minflt_counter
352                                         + (ULONG_MAX - pse->vmem_minflt_counter);
353                         }
354                         else
355                         {
356                                 pse->vmem_minflt = entry->vmem_minflt_counter - pse->vmem_minflt_counter;
357                         }
358                         pse->vmem_minflt_counter = entry->vmem_minflt_counter;
359                         
360                         if (entry->vmem_majflt_counter < pse->vmem_majflt_counter)
361                         {
362                                 pse->vmem_majflt = entry->vmem_majflt_counter
363                                         + (ULONG_MAX - pse->vmem_majflt_counter);
364                         }
365                         else
366                         {
367                                 pse->vmem_majflt = entry->vmem_majflt_counter - pse->vmem_majflt_counter;
368                         }
369                         pse->vmem_majflt_counter = entry->vmem_majflt_counter;
370                 }
371
372                 ps->vmem_minflt_counter += pse->vmem_minflt;
373                 ps->vmem_majflt_counter += pse->vmem_majflt;
374
375                 if ((entry->cpu_user_counter == 0)
376                                 && (entry->cpu_system_counter == 0))
377                 {
378                         pse->cpu_user_counter += entry->cpu_user;
379                         pse->cpu_user = entry->cpu_user;
380
381                         pse->cpu_system_counter += entry->cpu_system;
382                         pse->cpu_system = entry->cpu_system;
383                 }
384                 else
385                 {
386                         if (entry->cpu_user_counter < pse->cpu_user_counter)
387                         {
388                                 pse->cpu_user = entry->cpu_user_counter
389                                         + (ULONG_MAX - pse->cpu_user_counter);
390                         }
391                         else
392                         {
393                                 pse->cpu_user = entry->cpu_user_counter - pse->cpu_user_counter;
394                         }
395                         pse->cpu_user_counter = entry->cpu_user_counter;
396                         
397                         if (entry->cpu_system_counter < pse->cpu_system_counter)
398                         {
399                                 pse->cpu_system = entry->cpu_system_counter
400                                         + (ULONG_MAX - pse->cpu_system_counter);
401                         }
402                         else
403                         {
404                                 pse->cpu_system = entry->cpu_system_counter - pse->cpu_system_counter;
405                         }
406                         pse->cpu_system_counter = entry->cpu_system_counter;
407                 }
408
409                 ps->cpu_user_counter   += pse->cpu_user;
410                 ps->cpu_system_counter += pse->cpu_system;
411         }
412 }
413
414 /* remove old entries from instances of processes in list_head_g */
415 static void ps_list_reset (void)
416 {
417         procstat_t *ps;
418         procstat_entry_t *pse;
419         procstat_entry_t *pse_prev;
420
421         for (ps = list_head_g; ps != NULL; ps = ps->next)
422         {
423                 ps->num_proc    = 0;
424                 ps->num_lwp     = 0;
425                 ps->vmem_size   = 0;
426                 ps->vmem_rss    = 0;
427                 ps->stack_size  = 0;
428
429                 pse_prev = NULL;
430                 pse = ps->instances;
431                 while (pse != NULL)
432                 {
433                         if (pse->age > 10)
434                         {
435                                 DEBUG ("Removing this procstat entry cause it's too old: "
436                                                 "id = %lu; name = %s;",
437                                                 pse->id, ps->name);
438
439                                 if (pse_prev == NULL)
440                                 {
441                                         ps->instances = pse->next;
442                                         free (pse);
443                                         pse = ps->instances;
444                                 }
445                                 else
446                                 {
447                                         pse_prev->next = pse->next;
448                                         free (pse);
449                                         pse = pse_prev->next;
450                                 }
451                         }
452                         else
453                         {
454                                 pse->age++;
455                                 pse_prev = pse;
456                                 pse = pse->next;
457                         }
458                 } /* while (pse != NULL) */
459         } /* for (ps = list_head_g; ps != NULL; ps = ps->next) */
460 }
461
462 /* put all pre-defined 'Process' names from config to list_head_g tree */
463 static int ps_config (const char *key, const char *value)
464 {
465         if (strcasecmp (key, "Process") == 0)
466         {
467                 ps_list_register (value, NULL);
468         }
469         else if (strcasecmp (key, "ProcessMatch") == 0)
470         {
471                 char *new_val;
472                 char *fields[3];
473                 int fields_num;
474
475                 new_val = strdup (value);
476                 if (new_val == NULL) {
477                         ERROR ("processes plugin: strdup failed when processing "
478                                         "`ProcessMatch %s'.", value);
479                         return (1);
480                 }
481
482                 fields_num = strsplit (new_val, fields,
483                                 STATIC_ARRAY_SIZE (fields));
484                 if (fields_num != 2)
485                 {
486                         ERROR ("processes plugin: `ProcessMatch' needs exactly "
487                                         "two string arguments.");
488                         sfree (new_val);
489                         return (1);
490                 }
491                 ps_list_register (fields[0], fields[1]);
492                 sfree (new_val);
493         }
494         else
495         {
496                 ERROR ("processes plugin: The `%s' configuration option is not "
497                                 "understood and will be ignored.", key);
498                 return (-1);
499         }
500
501         return (0);
502 }
503
504 static int ps_init (void)
505 {
506 #if HAVE_THREAD_INFO
507         kern_return_t status;
508
509         port_host_self = mach_host_self ();
510         port_task_self = mach_task_self ();
511
512         if (pset_list != NULL)
513         {
514                 vm_deallocate (port_task_self,
515                                 (vm_address_t) pset_list,
516                                 pset_list_len * sizeof (processor_set_t));
517                 pset_list = NULL;
518                 pset_list_len = 0;
519         }
520
521         if ((status = host_processor_sets (port_host_self,
522                                         &pset_list,
523                                         &pset_list_len)) != KERN_SUCCESS)
524         {
525                 ERROR ("host_processor_sets failed: %s\n",
526                                 mach_error_string (status));
527                 pset_list = NULL;
528                 pset_list_len = 0;
529                 return (-1);
530         }
531 /* #endif HAVE_THREAD_INFO */
532
533 #elif KERNEL_LINUX
534         pagesize_g = sysconf(_SC_PAGESIZE);
535         DEBUG ("pagesize_g = %li; CONFIG_HZ = %i;",
536                         pagesize_g, CONFIG_HZ);
537 /* #endif KERNEL_LINUX */
538
539 #elif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD
540 /* no initialization */
541 #endif /* HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD */
542
543         return (0);
544 } /* int ps_init */
545
546 /* submit global state (e.g.: qty of zombies, running, etc..) */
547 static void ps_submit_state (const char *state, double value)
548 {
549         value_t values[1];
550         value_list_t vl = VALUE_LIST_INIT;
551
552         values[0].gauge = value;
553
554         vl.values = values;
555         vl.values_len = 1;
556         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
557         sstrncpy (vl.plugin, "processes", sizeof (vl.plugin));
558         sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
559         sstrncpy (vl.type, "ps_state", sizeof (vl.type));
560         sstrncpy (vl.type_instance, state, sizeof (vl.type_instance));
561
562         plugin_dispatch_values (&vl);
563 }
564
565 /* submit info about specific process (e.g.: memory taken, cpu usage, etc..) */
566 static void ps_submit_proc_list (procstat_t *ps)
567 {
568         value_t values[2];
569         value_list_t vl = VALUE_LIST_INIT;
570
571         vl.values = values;
572         vl.values_len = 2;
573         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
574         sstrncpy (vl.plugin, "processes", sizeof (vl.plugin));
575         sstrncpy (vl.plugin_instance, ps->name, sizeof (vl.plugin_instance));
576
577         sstrncpy (vl.type, "ps_vm", sizeof (vl.type));
578         vl.values[0].gauge = ps->vmem_size;
579         vl.values_len = 1;
580         plugin_dispatch_values (&vl);
581
582         sstrncpy (vl.type, "ps_rss", sizeof (vl.type));
583         vl.values[0].gauge = ps->vmem_rss;
584         vl.values_len = 1;
585         plugin_dispatch_values (&vl);
586
587         sstrncpy (vl.type, "ps_stacksize", sizeof (vl.type));
588         vl.values[0].gauge = ps->stack_size;
589         vl.values_len = 1;
590         plugin_dispatch_values (&vl);
591
592         sstrncpy (vl.type, "ps_cputime", sizeof (vl.type));
593         vl.values[0].counter = ps->cpu_user_counter;
594         vl.values[1].counter = ps->cpu_system_counter;
595         vl.values_len = 2;
596         plugin_dispatch_values (&vl);
597
598         sstrncpy (vl.type, "ps_count", sizeof (vl.type));
599         vl.values[0].gauge = ps->num_proc;
600         vl.values[1].gauge = ps->num_lwp;
601         vl.values_len = 2;
602         plugin_dispatch_values (&vl);
603
604         sstrncpy (vl.type, "ps_pagefaults", sizeof (vl.type));
605         vl.values[0].counter = ps->vmem_minflt_counter;
606         vl.values[1].counter = ps->vmem_majflt_counter;
607         vl.values_len = 2;
608         plugin_dispatch_values (&vl);
609
610         DEBUG ("name = %s; num_proc = %lu; num_lwp = %lu; vmem_rss = %lu; "
611                         "vmem_minflt_counter = %lu; vmem_majflt_counter = %lu; "
612                         "cpu_user_counter = %lu; cpu_system_counter = %lu;",
613                         ps->name, ps->num_proc, ps->num_lwp, ps->vmem_rss,
614                         ps->vmem_minflt_counter, ps->vmem_majflt_counter,
615                         ps->cpu_user_counter, ps->cpu_system_counter);
616 } /* void ps_submit_proc_list */
617
618 /* ------- additional functions for KERNEL_LINUX/HAVE_THREAD_INFO ------- */
619 #if KERNEL_LINUX
620 static int ps_read_tasks (int pid)
621 {
622         char           dirname[64];
623         DIR           *dh;
624         struct dirent *ent;
625         int count = 0;
626
627         ssnprintf (dirname, sizeof (dirname), "/proc/%i/task", pid);
628
629         if ((dh = opendir (dirname)) == NULL)
630         {
631                 DEBUG ("Failed to open directory `%s'", dirname);
632                 return (-1);
633         }
634
635         while ((ent = readdir (dh)) != NULL)
636         {
637                 if (!isdigit ((int) ent->d_name[0]))
638                         continue;
639                 else
640                         count++;
641         }
642         closedir (dh);
643
644         return ((count >= 1) ? count : 1);
645 } /* int *ps_read_tasks */
646
647 int ps_read_process (int pid, procstat_t *ps, char *state)
648 {
649         char  filename[64];
650         char  buffer[1024];
651
652         char *fields[64];
653         char  fields_len;
654
655         int   i;
656
657         int   ppid;
658         int   name_len;
659
660         long long unsigned cpu_user_counter;
661         long long unsigned cpu_system_counter;
662         long long unsigned vmem_size;
663         long long unsigned vmem_rss;
664         long long unsigned stack_size;
665
666         memset (ps, 0, sizeof (procstat_t));
667
668         ssnprintf (filename, sizeof (filename), "/proc/%i/stat", pid);
669
670         i = read_file_contents (filename, buffer, sizeof(buffer) - 1);
671         if (i <= 0)
672                 return (-1);
673         buffer[i] = 0;
674
675         fields_len = strsplit (buffer, fields, 64);
676         if (fields_len < 24)
677         {
678                 DEBUG ("processes plugin: ps_read_process (pid = %i):"
679                                 " `%s' has only %i fields..",
680                                 (int) pid, filename, fields_len);
681                 return (-1);
682         }
683
684         /* copy the name, strip brackets in the process */
685         name_len = strlen (fields[1]) - 2;
686         if ((fields[1][0] != '(') || (fields[1][name_len + 1] != ')'))
687         {
688                 DEBUG ("No brackets found in process name: `%s'", fields[1]);
689                 return (-1);
690         }
691         fields[1] = fields[1] + 1;
692         fields[1][name_len] = '\0';
693         strncpy (ps->name, fields[1], PROCSTAT_NAME_LEN);
694
695         ppid = atoi (fields[3]);
696
697         *state = fields[2][0];
698
699         if (*state == 'Z')
700         {
701                 ps->num_lwp  = 0;
702                 ps->num_proc = 0;
703         }
704         else
705         {
706                 if ( (ps->num_lwp = ps_read_tasks (pid)) == -1 )
707                 {
708                         /* returns -1 => kernel 2.4 */
709                         ps->num_lwp = 1;
710                 }
711                 ps->num_proc = 1;
712         }
713
714         /* Leave the rest at zero if this is only a zombi */
715         if (ps->num_proc == 0)
716         {
717                 DEBUG ("processes plugin: This is only a zombi: pid = %i; "
718                                 "name = %s;", pid, ps->name);
719                 return (0);
720         }
721
722         cpu_user_counter   = atoll (fields[13]);
723         cpu_system_counter = atoll (fields[14]);
724         vmem_size          = atoll (fields[22]);
725         vmem_rss           = atoll (fields[23]);
726         ps->vmem_minflt_counter = atol (fields[9]);
727         ps->vmem_majflt_counter = atol (fields[11]);
728
729         {
730                 unsigned long long stack_start = atoll (fields[27]);
731                 unsigned long long stack_ptr   = atoll (fields[28]);
732
733                 stack_size = (stack_start > stack_ptr)
734                         ? stack_start - stack_ptr
735                         : stack_ptr - stack_start;
736         }
737
738         /* Convert jiffies to useconds */
739         cpu_user_counter   = cpu_user_counter   * 1000000 / CONFIG_HZ;
740         cpu_system_counter = cpu_system_counter * 1000000 / CONFIG_HZ;
741         vmem_rss = vmem_rss * pagesize_g;
742
743         ps->cpu_user_counter = (unsigned long) cpu_user_counter;
744         ps->cpu_system_counter = (unsigned long) cpu_system_counter;
745         ps->vmem_size = (unsigned long) vmem_size;
746         ps->vmem_rss = (unsigned long) vmem_rss;
747         ps->stack_size = (unsigned long) stack_size;
748
749         /* success */
750         return (0);
751 } /* int ps_read_process (...) */
752
753 static char *ps_get_cmdline (pid_t pid, char *name, char *buf, size_t buf_len)
754 {
755         char  *buf_ptr;
756         size_t len;
757
758         char file[PATH_MAX];
759         int  fd;
760
761         size_t n;
762
763         if ((pid < 1) || (NULL == buf) || (buf_len < 2))
764                 return NULL;
765
766         ssnprintf (file, sizeof (file), "/proc/%u/cmdline", pid);
767
768         fd = open (file, O_RDONLY);
769         if (fd < 0) {
770                 char errbuf[4096];
771                 WARNING ("processes plugin: Failed to open `%s': %s.", file,
772                                 sstrerror (errno, errbuf, sizeof (errbuf)));
773                 return NULL;
774         }
775
776         buf_ptr = buf;
777         len     = buf_len;
778
779         n = 0;
780
781         while (42) {
782                 ssize_t status;
783
784                 status = read (fd, (void *)buf_ptr, len);
785
786                 if (status < 0) {
787                         char errbuf[4096];
788
789                         if ((EAGAIN == errno) || (EINTR == errno))
790                                 continue;
791
792                         WARNING ("processes plugin: Failed to read from `%s': %s.", file,
793                                         sstrerror (errno, errbuf, sizeof (errbuf)));
794                         close (fd);
795                         return NULL;
796                 }
797
798                 n += status;
799
800                 if (status == 0)
801                         break;
802
803                 buf_ptr += status;
804                 len     -= status;
805
806                 if (len <= 0)
807                         break;
808         }
809
810         close (fd);
811
812         if (0 == n) {
813                 /* cmdline not available; e.g. kernel thread, zombie */
814                 if (NULL == name)
815                         return NULL;
816
817                 ssnprintf (buf, buf_len, "[%s]", name);
818                 return buf;
819         }
820
821         assert (n <= buf_len);
822
823         if (n == buf_len)
824                 --n;
825         buf[n] = '\0';
826
827         --n;
828         /* remove trailing whitespace */
829         while ((n > 0) && (isspace (buf[n]) || ('\0' == buf[n]))) {
830                 buf[n] = '\0';
831                 --n;
832         }
833
834         /* arguments are separated by '\0' in /proc/<pid>/cmdline */
835         while (n > 0) {
836                 if ('\0' == buf[n])
837                         buf[n] = ' ';
838                 --n;
839         }
840         return buf;
841 } /* char *ps_get_cmdline (...) */
842
843 static unsigned long read_fork_rate ()
844 {
845         FILE *proc_stat;
846         char buf[1024];
847         unsigned long result = 0;
848         int numfields;
849         char *fields[3];
850
851         proc_stat = fopen("/proc/stat", "r");
852         if (proc_stat == NULL) {
853                 char errbuf[1024];
854                 ERROR ("processes plugin: fopen (/proc/stat) failed: %s",
855                                 sstrerror (errno, errbuf, sizeof (errbuf)));
856                 return ULONG_MAX;
857         }
858
859         while (fgets (buf, sizeof(buf), proc_stat) != NULL)
860         {
861                 char *endptr;
862
863                 numfields = strsplit(buf, fields, STATIC_ARRAY_SIZE (fields));
864                 if (numfields != 2) {
865                         ERROR ("processes plugin: processes in /proc/stat "
866                                         "contains more than 2 fields.");
867                         continue;
868                 }
869                 if (strcmp ("processes", fields[0]) != 0)
870                         continue;
871
872                 errno = 0;
873                 endptr = NULL;
874                 result = strtoul(fields[1], &endptr, 10);
875                 if ((endptr == fields[1]) || (errno != 0)) {
876                         ERROR ("processes plugin: Cannot parse fork rate: %s",
877                                         fields[1]);
878                         result = ULONG_MAX;
879                         break;
880                 }
881
882                 break;
883         }
884
885         fclose(proc_stat);
886
887         return result;
888 }
889
890 static void ps_submit_fork_rate (unsigned long value)
891 {
892         value_t values[1];
893         value_list_t vl = VALUE_LIST_INIT;
894
895         values[0].derive = (derive_t) value;
896
897         vl.values = values;
898         vl.values_len = 1;
899         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
900         sstrncpy (vl.plugin, "processes", sizeof (vl.plugin));
901         sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
902         sstrncpy (vl.type, "fork_rate", sizeof (vl.type));
903         sstrncpy (vl.type_instance, "", sizeof (vl.type_instance));
904
905         plugin_dispatch_values (&vl);
906 }
907
908 #endif /* KERNEL_LINUX */
909
910 #if HAVE_THREAD_INFO
911 static int mach_get_task_name (task_t t, int *pid, char *name, size_t name_max_len)
912 {
913         int mib[4];
914
915         struct kinfo_proc kp;
916         size_t            kp_size;
917
918         mib[0] = CTL_KERN;
919         mib[1] = KERN_PROC;
920         mib[2] = KERN_PROC_PID;
921
922         if (pid_for_task (t, pid) != KERN_SUCCESS)
923                 return (-1);
924         mib[3] = *pid;
925
926         kp_size = sizeof (kp);
927         if (sysctl (mib, 4, &kp, &kp_size, NULL, 0) != 0)
928                 return (-1);
929
930         if (name_max_len > (MAXCOMLEN + 1))
931                 name_max_len = MAXCOMLEN + 1;
932
933         strncpy (name, kp.kp_proc.p_comm, name_max_len - 1);
934         name[name_max_len - 1] = '\0';
935
936         DEBUG ("pid = %i; name = %s;", *pid, name);
937
938         /* We don't do the special handling for `p_comm == "LaunchCFMApp"' as
939          * `top' does it, because it is a lot of work and only used when
940          * debugging. -octo */
941
942         return (0);
943 }
944 #endif /* HAVE_THREAD_INFO */
945 /* ------- end of additional functions for KERNEL_LINUX/HAVE_THREAD_INFO ------- */
946
947 /* do actual readings from kernel */
948 static int ps_read (void)
949 {
950 #if HAVE_THREAD_INFO
951         kern_return_t            status;
952
953         int                      pset;
954         processor_set_t          port_pset_priv;
955
956         int                      task;
957         task_array_t             task_list;
958         mach_msg_type_number_t   task_list_len;
959
960         int                      task_pid;
961         char                     task_name[MAXCOMLEN + 1];
962
963         int                      thread;
964         thread_act_array_t       thread_list;
965         mach_msg_type_number_t   thread_list_len;
966         thread_basic_info_data_t thread_data;
967         mach_msg_type_number_t   thread_data_len;
968
969         int running  = 0;
970         int sleeping = 0;
971         int zombies  = 0;
972         int stopped  = 0;
973         int blocked  = 0;
974
975         procstat_t *ps;
976         procstat_entry_t pse;
977
978         ps_list_reset ();
979
980         /*
981          * The Mach-concept is a little different from the traditional UNIX
982          * concept: All the work is done in threads. Threads are contained in
983          * `tasks'. Therefore, `task status' doesn't make much sense, since
984          * it's actually a `thread status'.
985          * Tasks are assigned to sets of processors, so that's where you go to
986          * get a list.
987          */
988         for (pset = 0; pset < pset_list_len; pset++)
989         {
990                 if ((status = host_processor_set_priv (port_host_self,
991                                                 pset_list[pset],
992                                                 &port_pset_priv)) != KERN_SUCCESS)
993                 {
994                         ERROR ("host_processor_set_priv failed: %s\n",
995                                         mach_error_string (status));
996                         continue;
997                 }
998
999                 if ((status = processor_set_tasks (port_pset_priv,
1000                                                 &task_list,
1001                                                 &task_list_len)) != KERN_SUCCESS)
1002                 {
1003                         ERROR ("processor_set_tasks failed: %s\n",
1004                                         mach_error_string (status));
1005                         mach_port_deallocate (port_task_self, port_pset_priv);
1006                         continue;
1007                 }
1008
1009                 for (task = 0; task < task_list_len; task++)
1010                 {
1011                         ps = NULL;
1012                         if (mach_get_task_name (task_list[task],
1013                                                 &task_pid,
1014                                                 task_name, PROCSTAT_NAME_LEN) == 0)
1015                         {
1016                                 /* search for at least one match */
1017                                 for (ps = list_head_g; ps != NULL; ps = ps->next)
1018                                         /* FIXME: cmdline should be here instead of NULL */
1019                                         if (ps_list_match (task_name, NULL, ps) == 1)
1020                                                 break;
1021                         }
1022
1023                         /* Collect more detailed statistics for this process */
1024                         if (ps != NULL)
1025                         {
1026                                 task_basic_info_data_t        task_basic_info;
1027                                 mach_msg_type_number_t        task_basic_info_len;
1028                                 task_events_info_data_t       task_events_info;
1029                                 mach_msg_type_number_t        task_events_info_len;
1030                                 task_absolutetime_info_data_t task_absolutetime_info;
1031                                 mach_msg_type_number_t        task_absolutetime_info_len;
1032
1033                                 memset (&pse, '\0', sizeof (pse));
1034                                 pse.id = task_pid;
1035
1036                                 task_basic_info_len = TASK_BASIC_INFO_COUNT;
1037                                 status = task_info (task_list[task],
1038                                                 TASK_BASIC_INFO,
1039                                                 (task_info_t) &task_basic_info,
1040                                                 &task_basic_info_len);
1041                                 if (status != KERN_SUCCESS)
1042                                 {
1043                                         ERROR ("task_info failed: %s",
1044                                                         mach_error_string (status));
1045                                         continue; /* with next thread_list */
1046                                 }
1047
1048                                 task_events_info_len = TASK_EVENTS_INFO_COUNT;
1049                                 status = task_info (task_list[task],
1050                                                 TASK_EVENTS_INFO,
1051                                                 (task_info_t) &task_events_info,
1052                                                 &task_events_info_len);
1053                                 if (status != KERN_SUCCESS)
1054                                 {
1055                                         ERROR ("task_info failed: %s",
1056                                                         mach_error_string (status));
1057                                         continue; /* with next thread_list */
1058                                 }
1059
1060                                 task_absolutetime_info_len = TASK_ABSOLUTETIME_INFO_COUNT;
1061                                 status = task_info (task_list[task],
1062                                                 TASK_ABSOLUTETIME_INFO,
1063                                                 (task_info_t) &task_absolutetime_info,
1064                                                 &task_absolutetime_info_len);
1065                                 if (status != KERN_SUCCESS)
1066                                 {
1067                                         ERROR ("task_info failed: %s",
1068                                                         mach_error_string (status));
1069                                         continue; /* with next thread_list */
1070                                 }
1071
1072                                 pse.num_proc++;
1073                                 pse.vmem_rss = task_basic_info.resident_size;
1074
1075                                 pse.vmem_minflt_counter = task_events_info.cow_faults;
1076                                 pse.vmem_majflt_counter = task_events_info.faults;
1077
1078                                 pse.cpu_user_counter = task_absolutetime_info.total_user;
1079                                 pse.cpu_system_counter = task_absolutetime_info.total_system;
1080                         }
1081
1082                         status = task_threads (task_list[task], &thread_list,
1083                                         &thread_list_len);
1084                         if (status != KERN_SUCCESS)
1085                         {
1086                                 /* Apple's `top' treats this case a zombie. It
1087                                  * makes sense to some extend: A `zombie'
1088                                  * thread is nonsense, since the task/process
1089                                  * is dead. */
1090                                 zombies++;
1091                                 DEBUG ("task_threads failed: %s",
1092                                                 mach_error_string (status));
1093                                 if (task_list[task] != port_task_self)
1094                                         mach_port_deallocate (port_task_self,
1095                                                         task_list[task]);
1096                                 continue; /* with next task_list */
1097                         }
1098
1099                         for (thread = 0; thread < thread_list_len; thread++)
1100                         {
1101                                 thread_data_len = THREAD_BASIC_INFO_COUNT;
1102                                 status = thread_info (thread_list[thread],
1103                                                 THREAD_BASIC_INFO,
1104                                                 (thread_info_t) &thread_data,
1105                                                 &thread_data_len);
1106                                 if (status != KERN_SUCCESS)
1107                                 {
1108                                         ERROR ("thread_info failed: %s",
1109                                                         mach_error_string (status));
1110                                         if (task_list[task] != port_task_self)
1111                                                 mach_port_deallocate (port_task_self,
1112                                                                 thread_list[thread]);
1113                                         continue; /* with next thread_list */
1114                                 }
1115
1116                                 if (ps != NULL)
1117                                         pse.num_lwp++;
1118
1119                                 switch (thread_data.run_state)
1120                                 {
1121                                         case TH_STATE_RUNNING:
1122                                                 running++;
1123                                                 break;
1124                                         case TH_STATE_STOPPED:
1125                                         /* What exactly is `halted'? */
1126                                         case TH_STATE_HALTED:
1127                                                 stopped++;
1128                                                 break;
1129                                         case TH_STATE_WAITING:
1130                                                 sleeping++;
1131                                                 break;
1132                                         case TH_STATE_UNINTERRUPTIBLE:
1133                                                 blocked++;
1134                                                 break;
1135                                         /* There is no `zombie' case here,
1136                                          * since there are no zombie-threads.
1137                                          * There's only zombie tasks, which are
1138                                          * handled above. */
1139                                         default:
1140                                                 WARNING ("Unknown thread status: %i",
1141                                                                 thread_data.run_state);
1142                                                 break;
1143                                 } /* switch (thread_data.run_state) */
1144
1145                                 if (task_list[task] != port_task_self)
1146                                 {
1147                                         status = mach_port_deallocate (port_task_self,
1148                                                         thread_list[thread]);
1149                                         if (status != KERN_SUCCESS)
1150                                                 ERROR ("mach_port_deallocate failed: %s",
1151                                                                 mach_error_string (status));
1152                                 }
1153                         } /* for (thread_list) */
1154
1155                         if ((status = vm_deallocate (port_task_self,
1156                                                         (vm_address_t) thread_list,
1157                                                         thread_list_len * sizeof (thread_act_t)))
1158                                         != KERN_SUCCESS)
1159                         {
1160                                 ERROR ("vm_deallocate failed: %s",
1161                                                 mach_error_string (status));
1162                         }
1163                         thread_list = NULL;
1164                         thread_list_len = 0;
1165
1166                         /* Only deallocate the task port, if it isn't our own.
1167                          * Don't know what would happen in that case, but this
1168                          * is what Apple's top does.. ;) */
1169                         if (task_list[task] != port_task_self)
1170                         {
1171                                 status = mach_port_deallocate (port_task_self,
1172                                                 task_list[task]);
1173                                 if (status != KERN_SUCCESS)
1174                                         ERROR ("mach_port_deallocate failed: %s",
1175                                                         mach_error_string (status));
1176                         }
1177
1178                         if (ps != NULL)
1179                                 /* FIXME: cmdline should be here instead of NULL */
1180                                 ps_list_add (task_name, NULL, &pse);
1181                 } /* for (task_list) */
1182
1183                 if ((status = vm_deallocate (port_task_self,
1184                                 (vm_address_t) task_list,
1185                                 task_list_len * sizeof (task_t))) != KERN_SUCCESS)
1186                 {
1187                         ERROR ("vm_deallocate failed: %s",
1188                                         mach_error_string (status));
1189                 }
1190                 task_list = NULL;
1191                 task_list_len = 0;
1192
1193                 if ((status = mach_port_deallocate (port_task_self, port_pset_priv))
1194                                 != KERN_SUCCESS)
1195                 {
1196                         ERROR ("mach_port_deallocate failed: %s",
1197                                         mach_error_string (status));
1198                 }
1199         } /* for (pset_list) */
1200
1201         ps_submit_state ("running", running);
1202         ps_submit_state ("sleeping", sleeping);
1203         ps_submit_state ("zombies", zombies);
1204         ps_submit_state ("stopped", stopped);
1205         ps_submit_state ("blocked", blocked);
1206
1207         for (ps = list_head_g; ps != NULL; ps = ps->next)
1208                 ps_submit_proc_list (ps);
1209 /* #endif HAVE_THREAD_INFO */
1210
1211 #elif KERNEL_LINUX
1212         int running  = 0;
1213         int sleeping = 0;
1214         int zombies  = 0;
1215         int stopped  = 0;
1216         int paging   = 0;
1217         int blocked  = 0;
1218
1219         struct dirent *ent;
1220         DIR           *proc;
1221         int            pid;
1222
1223         char cmdline[ARG_MAX];
1224
1225         int        status;
1226         procstat_t ps;
1227         procstat_entry_t pse;
1228         char       state;
1229
1230         unsigned long fork_rate;
1231
1232         procstat_t *ps_ptr;
1233
1234         running = sleeping = zombies = stopped = paging = blocked = 0;
1235         ps_list_reset ();
1236
1237         if ((proc = opendir ("/proc")) == NULL)
1238         {
1239                 char errbuf[1024];
1240                 ERROR ("Cannot open `/proc': %s",
1241                                 sstrerror (errno, errbuf, sizeof (errbuf)));
1242                 return (-1);
1243         }
1244
1245         while ((ent = readdir (proc)) != NULL)
1246         {
1247                 if (!isdigit (ent->d_name[0]))
1248                         continue;
1249
1250                 if ((pid = atoi (ent->d_name)) < 1)
1251                         continue;
1252
1253                 status = ps_read_process (pid, &ps, &state);
1254                 if (status != 0)
1255                 {
1256                         DEBUG ("ps_read_process failed: %i", status);
1257                         continue;
1258                 }
1259
1260                 pse.id       = pid;
1261                 pse.age      = 0;
1262
1263                 pse.num_proc   = ps.num_proc;
1264                 pse.num_lwp    = ps.num_lwp;
1265                 pse.vmem_size  = ps.vmem_size;
1266                 pse.vmem_rss   = ps.vmem_rss;
1267                 pse.stack_size = ps.stack_size;
1268
1269                 pse.vmem_minflt = 0;
1270                 pse.vmem_minflt_counter = ps.vmem_minflt_counter;
1271                 pse.vmem_majflt = 0;
1272                 pse.vmem_majflt_counter = ps.vmem_majflt_counter;
1273
1274                 pse.cpu_user = 0;
1275                 pse.cpu_user_counter = ps.cpu_user_counter;
1276                 pse.cpu_system = 0;
1277                 pse.cpu_system_counter = ps.cpu_system_counter;
1278
1279                 switch (state)
1280                 {
1281                         case 'R': running++;  break;
1282                         case 'S': sleeping++; break;
1283                         case 'D': blocked++;  break;
1284                         case 'Z': zombies++;  break;
1285                         case 'T': stopped++;  break;
1286                         case 'W': paging++;   break;
1287                 }
1288
1289                 ps_list_add (ps.name,
1290                                 ps_get_cmdline (pid, ps.name, cmdline, sizeof (cmdline)),
1291                                 &pse);
1292         }
1293
1294         closedir (proc);
1295
1296         ps_submit_state ("running",  running);
1297         ps_submit_state ("sleeping", sleeping);
1298         ps_submit_state ("zombies",  zombies);
1299         ps_submit_state ("stopped",  stopped);
1300         ps_submit_state ("paging",   paging);
1301         ps_submit_state ("blocked",  blocked);
1302
1303         for (ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
1304                 ps_submit_proc_list (ps_ptr);
1305
1306         fork_rate = read_fork_rate();
1307         if (fork_rate != ULONG_MAX)
1308                 ps_submit_fork_rate(fork_rate);
1309 /* #endif KERNEL_LINUX */
1310
1311 #elif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD
1312         int running  = 0;
1313         int sleeping = 0;
1314         int zombies  = 0;
1315         int stopped  = 0;
1316         int blocked  = 0;
1317         int idle     = 0;
1318         int wait     = 0;
1319
1320         kvm_t *kd;
1321         char errbuf[1024];
1322         char cmdline[ARG_MAX];
1323         char *cmdline_ptr;
1324         struct kinfo_proc *procs;          /* array of processes */
1325         char **argv;
1326         int count;                         /* returns number of processes */
1327         int i;
1328
1329         procstat_t *ps_ptr;
1330         procstat_entry_t pse;
1331
1332         ps_list_reset ();
1333
1334         /* Open the kvm interface, get a descriptor */
1335         kd = kvm_open (NULL, NULL, NULL, 0, errbuf);
1336         if (kd == NULL)
1337         {
1338                 ERROR ("processes plugin: Cannot open kvm interface: %s",
1339                                 errbuf);
1340                 return (0);
1341         }
1342
1343         /* Get the list of processes. */
1344         procs = kvm_getprocs(kd, KERN_PROC_ALL, 0, &count);
1345         if (procs == NULL)
1346         {
1347                 kvm_close (kd);
1348                 ERROR ("processes plugin: Cannot get kvm processes list: %s",
1349                                 kvm_geterr(kd));
1350                 return (0);
1351         }
1352
1353         /* Iterate through the processes in kinfo_proc */
1354         for (i = 0; i < count; i++)
1355         {
1356                 /* retrieve the arguments */
1357                 cmdline[0] = 0;
1358                 cmdline_ptr = NULL;
1359
1360                 argv = kvm_getargv (kd, (const struct kinfo_proc *) &(procs[i]), 0);
1361                 if (argv != NULL)
1362                 {
1363                         int status;
1364                         int argc;
1365
1366                         argc = 0;
1367                         while (argv[argc] != NULL)
1368                                 argc++;
1369
1370                         status = strjoin (cmdline, sizeof (cmdline),
1371                                         argv, argc, " ");
1372
1373                         if (status < 0)
1374                         {
1375                                 WARNING ("processes plugin: Command line did "
1376                                                 "not fit into buffer.");
1377                         }
1378                         else
1379                         {
1380                                 cmdline_ptr = &cmdline[0];
1381                         }
1382                 }
1383
1384                 pse.id       = procs[i].ki_pid;
1385                 pse.age      = 0;
1386
1387                 pse.num_proc = 1;
1388                 pse.num_lwp  = procs[i].ki_numthreads;
1389
1390                 pse.vmem_size = procs[i].ki_size;
1391                 pse.vmem_rss = procs[i].ki_rssize * getpagesize();
1392                 pse.stack_size = procs[i].ki_ssize * getpagesize();
1393                 pse.vmem_minflt = 0;
1394                 pse.vmem_minflt_counter = procs[i].ki_rusage.ru_minflt;
1395                 pse.vmem_majflt = 0;
1396                 pse.vmem_majflt_counter = procs[i].ki_rusage.ru_majflt;
1397
1398                 pse.cpu_user = 0;
1399                 pse.cpu_user_counter = procs[i].ki_rusage.ru_utime.tv_sec
1400                         * 1000
1401                         + procs[i].ki_rusage.ru_utime.tv_usec;
1402                 pse.cpu_system = 0;
1403                 pse.cpu_system_counter = procs[i].ki_rusage.ru_stime.tv_sec
1404                         * 1000
1405                         + procs[i].ki_rusage.ru_stime.tv_usec;
1406
1407                 switch (procs[i].ki_stat)
1408                 {
1409                         case SSTOP:     stopped++;      break;
1410                         case SSLEEP:    sleeping++;     break;
1411                         case SRUN:      running++;      break;
1412                         case SIDL:      idle++;         break;
1413                         case SWAIT:     wait++;         break;
1414                         case SLOCK:     blocked++;      break;
1415                         case SZOMB:     zombies++;      break;
1416                 }
1417
1418                 ps_list_add (procs[i].ki_comm, cmdline_ptr, &pse);
1419         }
1420
1421         kvm_close(kd);
1422
1423         ps_submit_state ("running",  running);
1424         ps_submit_state ("sleeping", sleeping);
1425         ps_submit_state ("zombies",  zombies);
1426         ps_submit_state ("stopped",  stopped);
1427         ps_submit_state ("blocked",  blocked);
1428         ps_submit_state ("idle",     idle);
1429         ps_submit_state ("wait",     wait);
1430
1431         for (ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
1432                 ps_submit_proc_list (ps_ptr);
1433 #endif /* HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD */
1434
1435         return (0);
1436 } /* int ps_read */
1437
1438 void module_register (void)
1439 {
1440         plugin_register_config ("processes", ps_config,
1441                         config_keys, config_keys_num);
1442         plugin_register_init ("processes", ps_init);
1443         plugin_register_read ("processes", ps_read);
1444 } /* void module_register */