2 * collectd - src/zone.c
3 * Copyright (C) 2011 Mathijs Mohlmann
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
28 #include <sys/types.h>
29 #include <sys/vm_usage.h>
33 #include "utils_avltree.h"
35 #define MAX_PROCFS_PATH 40
36 #define MAX_ZONE_NAME 20
37 #define FRC2PCT(pp)(((float)(pp))/0x8000*100)
39 typedef struct zone_stats {
47 zone_compare(const zoneid_t *a, const zoneid_t *b)
57 zone_read_procfile(char *pidstr, char *file, void *buf, size_t bufsize)
61 char procfile[MAX_PROCFS_PATH];
62 (void)snprintf(procfile, MAX_PROCFS_PATH, "/proc/%s/%s", pidstr, file);
63 while ((fd = open(procfile, O_RDONLY)) == -1) {
64 if ((errno != EMFILE) || (errno != ENFILE)) {
69 if (pread(fd, buf, bufsize, 0) != bufsize) {
78 zone_submit_value(char *zone, gauge_t value)
80 value_list_t vl = VALUE_LIST_INIT;
83 values[0].gauge = value;
86 vl.values_len = 1; /*STATIC_ARRAY_SIZE (values);*/
87 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
88 sstrncpy (vl.plugin, "zone", sizeof (vl.plugin));
89 sstrncpy (vl.type, "percent", sizeof (vl.type));
90 sstrncpy (vl.type_instance, zone, sizeof (vl.type_instance));
92 return(plugin_dispatch_values (&vl));
96 zone_find_stats(c_avl_tree_t *tree, zoneid_t zoneid)
98 zone_stats_t *ret = NULL;
101 if (c_avl_get(tree, (void **)&zoneid, (void **)&ret)) {
102 if (!(ret = malloc(sizeof(zone_stats_t)))) {
103 WARNING("no memory");
106 if (!(key = malloc(sizeof(zoneid_t)))) {
107 WARNING("no memory");
111 if (c_avl_insert(tree, key, ret)) {
112 WARNING("error inserting into tree");
120 zone_submit_values(c_avl_tree_t *tree)
122 char zonename[MAX_ZONE_NAME];
123 zoneid_t *zoneid = NULL;
124 zone_stats_t *stats = NULL;
126 while (c_avl_pick (tree, (void **)&zoneid, (void **)&stats) == 0)
128 getzonenamebyid(*zoneid, zonename, MAX_ZONE_NAME-1);
129 zone_submit_value(zonename, (gauge_t)FRC2PCT(stats->pctcpu));
136 static c_avl_tree_t *
137 zone_scandir(DIR *procdir)
145 /* size_t physmem = sysconf(_SC_PHYS_PAGES) * pagesize;*/
147 if (!(tree=c_avl_create((int (*)
148 (const void *, const void *))zone_compare))) {
149 WARNING("Failed to create tree");
153 for (rewinddir(procdir); (direntp = readdir(procdir)); ) {
154 pidstr = direntp->d_name;
155 if (pidstr[0] == '.') /* skip "." and ".." */
158 if (pid == 0 || pid == 2 || pid == 3)
159 continue; /* skip sched, pageout and fsflush */
160 if (zone_read_procfile(pidstr, "psinfo", &psinfo,
161 sizeof(psinfo_t)) != 0)
163 stats = zone_find_stats(tree, psinfo.pr_zoneid);
164 stats->pctcpu += psinfo.pr_pctcpu;
165 stats->pctmem += psinfo.pr_pctmem;
171 static int zone_read (void)
176 pagesize = sysconf(_SC_PAGESIZE);
177 if ((procdir = opendir("/proc")) == NULL) {
178 ERROR("cannot open /proc directory\n");
182 tree=zone_scandir(procdir);
184 zone_submit_values(tree); /* this also frees tree */
188 void module_register (void)
190 plugin_register_read ("zone", zone_read);
191 } /* void module_register */