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