From: octo Date: Mon, 12 Dec 2005 07:58:44 +0000 (+0000) Subject: Merged changes on `processes.c', `collectd.c' and `collectd.h' from quota-branch... X-Git-Tag: collectd-3.5.0~18 X-Git-Url: https://git.octo.it/?a=commitdiff_plain;h=8f7201dae99ba1ec487a0e0b66c59c394335eace;p=collectd.git Merged changes on `processes.c', `collectd.c' and `collectd.h' from quota-branch to trunk --- diff --git a/src/collectd.c b/src/collectd.c index dabc346e..ddfb9f20 100644 --- a/src/collectd.c +++ b/src/collectd.c @@ -211,7 +211,9 @@ int pidfile_remove (void) int main (int argc, char **argv) { struct sigaction sigIntAction, sigChldAction; +#if COLLECT_DAEMON pid_t pid; +#endif char *plugindir = NULL; char *basedir = DATADIR; @@ -314,7 +316,7 @@ int main (int argc, char **argv) /* * fork off child */ -#if DEBUG == 0 +#if COLLECT_DAEMON if (daemonize) { if ((pid = fork ()) == -1) @@ -358,7 +360,7 @@ int main (int argc, char **argv) return (1); } } /* if (daemonize) */ -#endif +#endif /* COLLECT_DAEMON */ /* * run the actual loops diff --git a/src/collectd.h b/src/collectd.h index 57b709c5..1614483a 100644 --- a/src/collectd.h +++ b/src/collectd.h @@ -2,7 +2,7 @@ #define COLLECTD_H #if HAVE_CONFIG_H -# include "config.h" +# include #endif #include @@ -38,13 +38,64 @@ #if HAVE_UNISTD_H # include #endif -#include -#include -#include -#include -#include -#include -#include +#if HAVE_SYS_WAIT_H +# include +#endif +#ifndef WEXITSTATUS +# define WEXITSTATUS(stat_val) ((unsigned int) (stat_val) >> 8) +#endif +#ifndef WIFEXITED +# define WIFEXITED(stat_val) (((stat_val) & 255) == 0) +#endif +#if HAVE_SIGNAL_H +# include +#endif +#if HAVE_FCNTL_H +# include +#endif +#if HAVE_ERRNO_H +# include +#endif +#if HAVE_SYSLOG_H +# include +#endif +#if HAVE_LIMITS_H +# include +#endif +#if TIME_WITH_SYS_TIME +# include +# include +#else +# if HAVE_SYS_TIME_H +# include +# else +# include +# endif +#endif + +#if HAVE_DIRENT_H +# include +# define NAMLEN(dirent) strlen((dirent)->d_name) +#else +# define dirent direct +# define NAMLEN(dirent) (dirent)->d_namlen +# if HAVE_SYS_NDIR_H +# include +# endif +# if HAVE_SYS_DIR_H +# include +# endif +# if HAVE_NDIR_H +# include +# endif +#endif + +#if HAVE_STDARG_H +# include +#endif +#if HAVE_CTYPE_H +# include +#endif #ifndef HAVE_RRD_H #undef HAVE_LIBRRD @@ -73,10 +124,6 @@ #include #endif -#ifndef DEBUG -#define DEBUG 0 -#endif - #ifndef LOCALSTATEDIR #define LOCALSTATEDIR "/opt/collectd/var" #endif @@ -97,4 +144,7 @@ #define MODE_CLIENT 0x02 #define MODE_LOCAL 0x03 +extern time_t curtime; +extern int operating_mode; + #endif /* COLLECTD_H */ diff --git a/src/processes.c b/src/processes.c new file mode 100644 index 00000000..94e86dd5 --- /dev/null +++ b/src/processes.c @@ -0,0 +1,129 @@ +#include "processes.h" + +/* + * Originally written by Lyonel Vincent + */ + +#if COLLECT_PROCESSES +#define MODULE_NAME "processes" + +#include "common.h" +#include "plugin.h" + +static char *ps_file = "processes.rrd"; + +static char *ds_def[] = +{ + "DS:running:GAUGE:25:0:65535", + "DS:sleeping:GAUGE:25:0:65535", + "DS:zombies:GAUGE:25:0:65535", + "DS:stopped:GAUGE:25:0:65535", + "DS:paging:GAUGE:25:0:65535", + "DS:blocked:GAUGE:25:0:65535", + NULL +}; +static int ds_num = 6; + +extern time_t curtime; + +void ps_init (void) +{ +} + +void ps_write (char *host, char *inst, char *val) +{ + rrd_update_file (host, ps_file, val, ds_def, ds_num); +} + +#define BUFSIZE 256 +void ps_submit (unsigned int running, + unsigned int sleeping, + unsigned int zombies, + unsigned int stopped, + unsigned int paging, + unsigned int blocked) +{ + char buf[BUFSIZE]; + + if (snprintf (buf, BUFSIZE, "%u:%u:%u:%u:%u:%u:%u", + (unsigned int) curtime, + running, sleeping, zombies, stopped, paging, + blocked) >= BUFSIZE) + return; + + plugin_submit (MODULE_NAME, "-", buf); +} + +void ps_read (void) +{ +#ifdef KERNEL_LINUX + unsigned int running, sleeping, zombies, stopped, paging, blocked; + + char buf[BUFSIZE]; + char filename[20]; /* need 17 bytes */ + char *fields[256]; + + struct dirent *ent; + DIR *proc; + FILE *fh; + + running = sleeping = zombies = stopped = paging = blocked = 0; + + if ((proc = opendir ("/proc")) == NULL) + { + syslog (LOG_ERR, "Cannot open `/proc': %s", strerror (errno)); + return; + } + + int strsplit (char *string, char **fields, size_t size); + + while ((ent = readdir (proc)) != NULL) + { + if (!isdigit (ent->d_name[0])) + continue; + + if (snprintf (filename, 20, "/proc/%s/stat", ent->d_name) >= 20) + continue; + + if ((fh = fopen (filename, "r")) == NULL) + { + syslog (LOG_ERR, "Cannot open `%s': %s", filename, strerror (errno)); + continue; + } + + if (fgets (buf, BUFSIZE, fh) == NULL) + { + fclose (fh); + continue; + } + + fclose (fh); + + if (strsplit (buf, fields, 256) < 3) + continue; + + switch (fields[2][0]) + { + case 'R': running++; break; + case 'S': sleeping++; break; + case 'D': blocked++; break; + case 'Z': zombies++; break; + case 'T': stopped++; break; + case 'W': paging++; break; + } + } + + closedir(proc); + + ps_submit (running, sleeping, zombies, stopped, paging, blocked); +#endif /* defined(KERNEL_LINUX) */ +} +#undef BUFSIZE + +void module_register (void) +{ + plugin_register (MODULE_NAME, ps_init, ps_read, ps_write); +} + +#undef MODULE_NAME +#endif /* COLLECT_PROCESSES */