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