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
20 * Dagobert Michelsen (forward-porting)
27 /* avoid procfs.h error "Cannot use procfs in the large file compilation environment" */
28 #if !defined(_LP64) && _FILE_OFFSET_BITS == 64
29 # undef _FILE_OFFSET_BITS
30 # undef _LARGEFILE64_SOURCE
37 #include <sys/types.h>
38 #include <sys/vm_usage.h>
42 #include "utils_avltree.h"
44 #define MAX_PROCFS_PATH 40
45 #define FRC2PCT(pp)(((float)(pp))/0x8000*100)
47 typedef struct zone_stats {
54 static int zone_init (void)
56 pagesize = sysconf(_SC_PAGESIZE);
61 zone_compare(const zoneid_t *a, const zoneid_t *b)
71 zone_read_procfile(char const *pidstr, char const *name, void *buf, size_t bufsize)
75 char procfile[MAX_PROCFS_PATH];
76 (void)snprintf(procfile, sizeof(procfile), "/proc/%s/%s", pidstr, name);
77 if ((fd = open(procfile, O_RDONLY)) == -1) {
81 if (sread(fd, buf, bufsize) != 0) {
83 ERROR ("zone plugin: Reading \"%s\" failed: %s", procfile,
84 sstrerror (errno, errbuf, sizeof (errbuf)));
94 zone_submit_value(char *zone, gauge_t value)
96 value_list_t vl = VALUE_LIST_INIT;
99 values[0].gauge = value;
102 vl.values_len = 1; /*STATIC_ARRAY_SIZE (values);*/
103 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
104 sstrncpy (vl.plugin, "zone", sizeof (vl.plugin));
105 sstrncpy (vl.type, "percent", sizeof (vl.type));
106 sstrncpy (vl.type_instance, zone, sizeof (vl.type_instance));
108 return(plugin_dispatch_values (&vl));
111 static zone_stats_t *
112 zone_find_stats(c_avl_tree_t *tree, zoneid_t zoneid)
114 zone_stats_t *ret = NULL;
115 zoneid_t *key = NULL;
117 if (c_avl_get(tree, (void **)&zoneid, (void **)&ret)) {
118 if (!(ret = malloc(sizeof(*ret)))) {
119 WARNING("zone plugin: no memory");
122 if (!(key = malloc(sizeof(*key)))) {
123 WARNING("zone plugin: no memory");
127 if (c_avl_insert(tree, key, ret)) {
128 WARNING("zone plugin: error inserting into tree");
136 zone_submit_values(c_avl_tree_t *tree)
138 char zonename[ZONENAME_MAX];
139 zoneid_t *zoneid = NULL;
140 zone_stats_t *stats = NULL;
142 while (c_avl_pick (tree, (void **)&zoneid, (void **)&stats) == 0)
144 if (getzonenamebyid(*zoneid, zonename, sizeof( zonename )) == -1) {
145 WARNING("zone plugin: error retreiving zonename");
147 zone_submit_value(zonename, (gauge_t)FRC2PCT(stats->pctcpu));
155 static c_avl_tree_t *
156 zone_scandir(DIR *procdir)
164 if (!(tree=c_avl_create((void *) zone_compare))) {
165 WARNING("zone plugin: Failed to create tree");
170 while ((direntp = readdir(procdir))) {
171 char const *pidstr = direntp->d_name;
172 if (pidstr[0] == '.') /* skip "." and ".." */
176 if (pid == 0 || pid == 2 || pid == 3)
177 continue; /* skip sched, pageout and fsflush */
179 if (zone_read_procfile(pidstr, "psinfo", &psinfo, sizeof(psinfo_t)) != 0)
182 stats = zone_find_stats(tree, psinfo.pr_zoneid);
184 stats->pctcpu += psinfo.pr_pctcpu;
185 stats->pctmem += psinfo.pr_pctmem;
191 static int zone_read (void)
196 if ((procdir = opendir("/proc")) == NULL) {
197 ERROR("zone plugin: cannot open /proc directory\n");
201 tree=zone_scandir(procdir);
206 zone_submit_values(tree); /* this also frees tree */
210 void module_register (void)
212 plugin_register_init ("zone", zone_init);
213 plugin_register_read ("zone", zone_read);
214 } /* void module_register */