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