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