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