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