78e91ef2e4fd7a45de50c75ecb9f4ce9d483a5ca
1 /**
2 * collectd - src/zone.c
3 * Copyright (C) 2011 Mathijs Mohlmann
4 *
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.
8 *
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.
13 *
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
17 *
18 * Authors:
19 * Mathijs Mohlmann
20 **/
22 #define _BSD_SOURCE
24 #include "collectd.h"
25 #include "common.h"
26 #include "plugin.h"
28 #include <sys/types.h>
29 #include <sys/vm_usage.h>
30 #include <procfs.h>
31 #include <zone.h>
33 #include "utils_avltree.h"
35 #define MAX_PROCFS_PATH 40
36 #define FRC2PCT(pp)(((float)(pp))/0x8000*100)
38 typedef struct zone_stats {
39 ushort_t pctcpu;
40 ushort_t pctmem;
41 } zone_stats_t;
43 static long pagesize;
45 static int zone_init (void)
46 {
47 pagesize = sysconf(_SC_PAGESIZE);
48 return (0);
49 }
51 static int
52 zone_compare(const zoneid_t *a, const zoneid_t *b)
53 {
54 if (*a == *b)
55 return(0);
56 if (*a < *b)
57 return(-1);
58 return(1);
59 }
61 static int
62 zone_read_procfile(char *pidstr, char *file, void *buf, size_t bufsize)
63 {
64 int fd;
66 char procfile[MAX_PROCFS_PATH];
67 (void)snprintf(procfile, sizeof(procfile), "/proc/%s/%s", pidstr, file);
68 if ((fd = open(procfile, O_RDONLY)) == -1) {
69 return (1);
70 }
72 if (pread(fd, buf, bufsize, 0) != bufsize) {
73 close(fd);
74 return (1);
75 }
76 close(fd);
77 return (0);
78 }
80 static int
81 zone_submit_value(char *zone, gauge_t value)
82 {
83 value_list_t vl = VALUE_LIST_INIT;
84 value_t values[1];
86 values[0].gauge = value;
88 vl.values = values;
89 vl.values_len = 1; /*STATIC_ARRAY_SIZE (values);*/
90 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
91 sstrncpy (vl.plugin, "zone", sizeof (vl.plugin));
92 sstrncpy (vl.type, "percent", sizeof (vl.type));
93 sstrncpy (vl.type_instance, zone, sizeof (vl.type_instance));
95 return(plugin_dispatch_values (&vl));
96 }
98 static zone_stats_t *
99 zone_find_stats(c_avl_tree_t *tree, zoneid_t zoneid)
100 {
101 zone_stats_t *ret = NULL;
102 zoneid_t *key = NULL;
104 if (c_avl_get(tree, (void **)&zoneid, (void **)&ret)) {
105 if (!(ret = malloc(sizeof(zone_stats_t)))) {
106 WARNING("zone plugin: no memory");
107 return(NULL);
108 }
109 if (!(key = malloc(sizeof(zoneid_t)))) {
110 WARNING("zone plugin: no memory");
111 return(NULL);
112 }
113 *key = zoneid;
114 if (c_avl_insert(tree, key, ret)) {
115 WARNING("zone plugin: error inserting into tree");
116 return(NULL);
117 }
118 }
119 return(ret);
120 }
122 static void
123 zone_submit_values(c_avl_tree_t *tree)
124 {
125 char zonename[ZONENAME_MAX];
126 zoneid_t *zoneid = NULL;
127 zone_stats_t *stats = NULL;
129 while (c_avl_pick (tree, (void **)&zoneid, (void **)&stats) == 0)
130 {
131 if (getzonenamebyid(*zoneid, zonename, sizeof( zonename )) == -1) {
132 WARNING("zone plugin: error retreiving zonename");
133 } else {
134 zone_submit_value(zonename, (gauge_t)FRC2PCT(stats->pctcpu));
135 }
136 free(stats);
137 free(zoneid);
138 }
139 c_avl_destroy(tree);
140 }
142 static c_avl_tree_t *
143 zone_scandir(DIR *procdir)
144 {
145 char *pidstr;
146 pid_t pid;
147 dirent_t *direntp;
148 psinfo_t psinfo;
149 c_avl_tree_t *tree;
150 zone_stats_t *stats;
152 if (!(tree=c_avl_create((void *) zone_compare))) {
153 WARNING("zone plugin: Failed to create tree");
154 return(NULL);
155 }
157 for (rewinddir(procdir); (direntp = readdir(procdir)); ) {
158 pidstr = direntp->d_name;
159 if (pidstr[0] == '.') /* skip "." and ".." */
160 continue;
161 pid = atoi(pidstr);
162 if (pid == 0 || pid == 2 || pid == 3)
163 continue; /* skip sched, pageout and fsflush */
164 if (zone_read_procfile(pidstr, "psinfo", &psinfo,
165 sizeof(psinfo_t)) != 0)
166 continue;
167 stats = zone_find_stats(tree, psinfo.pr_zoneid);
168 if( stats ) {
169 stats->pctcpu += psinfo.pr_pctcpu;
170 stats->pctmem += psinfo.pr_pctmem;
171 }
172 }
173 return(tree);
174 }
177 static int zone_read (void)
178 {
179 DIR *procdir;
180 c_avl_tree_t *tree;
182 if ((procdir = opendir("/proc")) == NULL) {
183 ERROR("zone plugin: cannot open /proc directory\n");
184 return (-1);
185 }
187 tree=zone_scandir(procdir);
188 closedir(procdir);
189 if (tree == NULL) {
190 return (-1);
191 }
192 zone_submit_values(tree); /* this also frees tree */
193 return (0);
194 }
196 void module_register (void)
197 {
198 plugin_register_init ("zone", zone_init);
199 plugin_register_read ("zone", zone_read);
200 } /* void module_register */