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