e4aad81513bb2b9858139d3990e31de924f6b73f
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, MAX_PROCFS_PATH, "/proc/%s/%s", pidstr, file);
68 while ((fd = open(procfile, O_RDONLY)) == -1) {
69 if ((errno != EMFILE) || (errno != ENFILE)) {
70 return(1);
71 }
72 }
74 if (pread(fd, buf, bufsize, 0) != bufsize) {
75 close(fd);
76 return (1);
77 }
78 close(fd);
79 return (0);
80 }
82 static int
83 zone_submit_value(char *zone, gauge_t value)
84 {
85 value_list_t vl = VALUE_LIST_INIT;
86 value_t values[1];
88 values[0].gauge = value;
90 vl.values = values;
91 vl.values_len = 1; /*STATIC_ARRAY_SIZE (values);*/
92 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
93 sstrncpy (vl.plugin, "zone", sizeof (vl.plugin));
94 sstrncpy (vl.type, "percent", sizeof (vl.type));
95 sstrncpy (vl.type_instance, zone, sizeof (vl.type_instance));
97 return(plugin_dispatch_values (&vl));
98 }
100 static zone_stats_t *
101 zone_find_stats(c_avl_tree_t *tree, zoneid_t zoneid)
102 {
103 zone_stats_t *ret = NULL;
104 zoneid_t *key = NULL;
106 if (c_avl_get(tree, (void **)&zoneid, (void **)&ret)) {
107 if (!(ret = malloc(sizeof(zone_stats_t)))) {
108 WARNING("zone plugin: no memory");
109 return(NULL);
110 }
111 if (!(key = malloc(sizeof(zoneid_t)))) {
112 WARNING("zone plugin: no memory");
113 return(NULL);
114 }
115 *key = zoneid;
116 if (c_avl_insert(tree, key, ret)) {
117 WARNING("zone plugin: error inserting into tree");
118 return(NULL);
119 }
120 }
121 return(ret);
122 }
124 static void
125 zone_submit_values(c_avl_tree_t *tree)
126 {
127 char zonename[ZONENAME_MAX];
128 zoneid_t *zoneid = NULL;
129 zone_stats_t *stats = NULL;
131 while (c_avl_pick (tree, (void **)&zoneid, (void **)&stats) == 0)
132 {
133 if (getzonenamebyid(*zoneid, zonename, sizeof( zonename )) == -1) {
134 WARNING("zone plugin: error retreiving zonename");
135 } else {
136 zone_submit_value(zonename, (gauge_t)FRC2PCT(stats->pctcpu));
137 }
138 free(stats);
139 free(zoneid);
140 }
141 c_avl_destroy(tree);
142 }
144 static c_avl_tree_t *
145 zone_scandir(DIR *procdir)
146 {
147 char *pidstr;
148 pid_t pid;
149 dirent_t *direntp;
150 psinfo_t psinfo;
151 c_avl_tree_t *tree;
152 zone_stats_t *stats;
154 if (!(tree=c_avl_create((void *) zone_compare))) {
155 WARNING("zone plugin: Failed to create tree");
156 return(NULL);
157 }
159 for (rewinddir(procdir); (direntp = readdir(procdir)); ) {
160 pidstr = direntp->d_name;
161 if (pidstr[0] == '.') /* skip "." and ".." */
162 continue;
163 pid = atoi(pidstr);
164 if (pid == 0 || pid == 2 || pid == 3)
165 continue; /* skip sched, pageout and fsflush */
166 if (zone_read_procfile(pidstr, "psinfo", &psinfo,
167 sizeof(psinfo_t)) != 0)
168 continue;
169 stats = zone_find_stats(tree, psinfo.pr_zoneid);
170 if( stats ) {
171 stats->pctcpu += psinfo.pr_pctcpu;
172 stats->pctmem += psinfo.pr_pctmem;
173 }
174 }
175 return(tree);
176 }
179 static int zone_read (void)
180 {
181 DIR *procdir;
182 c_avl_tree_t *tree;
184 if ((procdir = opendir("/proc")) == NULL) {
185 ERROR("zone plugin: cannot open /proc directory\n");
186 return (-1);
187 }
189 tree=zone_scandir(procdir);
190 closedir(procdir);
191 if (tree == NULL) {
192 return (-1);
193 }
194 zone_submit_values(tree); /* this also frees tree */
195 return (0);
196 }
198 void module_register (void)
199 {
200 plugin_register_init ("zone", zone_init);
201 plugin_register_read ("zone", zone_read);
202 } /* void module_register */