Various plugins: Change various plugins to use "derive" internally.
[collectd.git] / src / fscache.c
1 /**
2  * collectd - src/fscache.c
3  * Copyright (C) 2009 Edward "Koko" Konetzko
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  *   Edward "Koko" Konetzko <konetzed at quixoticagony.com>
20  **/
21
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
25 #include <stdio.h>  /* a header needed for FILE */
26 #include <string.h> /* a header needed for scanf function */
27 #include <stdlib.h> /* used for atoi */
28
29
30 #if !KERNEL_LINUX
31 # error "This module only supports the Linux implementation of fscache"
32 #endif
33
34 #define BUFSIZE 1024
35
36 /*
37 see /proc/fs/fscache/stats
38 see Documentation/filesystems/caching/fscache.txt in linux kernel >= 2.6.30
39
40 This shows counts of a number of events that can happen in FS-Cache:
41
42 CLASS   EVENT   MEANING
43 ======= ======= =======================================================
44 Cookies idx=N   Number of index cookies allocated
45         dat=N   Number of data storage cookies allocated
46         spc=N   Number of special cookies allocated
47 Objects alc=N   Number of objects allocated
48         nal=N   Number of object allocation failures
49         avl=N   Number of objects that reached the available state
50         ded=N   Number of objects that reached the dead state
51 ChkAux  non=N   Number of objects that didn't have a coherency check
52         ok=N    Number of objects that passed a coherency check
53         upd=N   Number of objects that needed a coherency data update
54         obs=N   Number of objects that were declared obsolete
55 Pages   mrk=N   Number of pages marked as being cached
56         unc=N   Number of uncache page requests seen
57 Acquire n=N Number of acquire cookie requests seen
58         nul=N   Number of acq reqs given a NULL parent
59         noc=N   Number of acq reqs rejected due to no cache available
60         ok=N    Number of acq reqs succeeded
61         nbf=N   Number of acq reqs rejected due to error
62         oom=N   Number of acq reqs failed on ENOMEM
63 Lookups n=N Number of lookup calls made on cache backends
64         neg=N   Number of negative lookups made
65         pos=N   Number of positive lookups made
66         crt=N   Number of objects created by lookup
67 Updates n=N Number of update cookie requests seen
68         nul=N   Number of upd reqs given a NULL parent
69         run=N   Number of upd reqs granted CPU time
70 Relinqs n=N Number of relinquish cookie requests seen
71         nul=N   Number of rlq reqs given a NULL parent
72         wcr=N   Number of rlq reqs waited on completion of creation
73 AttrChg n=N Number of attribute changed requests seen
74         ok=N    Number of attr changed requests queued
75         nbf=N   Number of attr changed rejected -ENOBUFS
76         oom=N   Number of attr changed failed -ENOMEM
77         run=N   Number of attr changed ops given CPU time
78 Allocs  n=N Number of allocation requests seen
79         ok=N    Number of successful alloc reqs
80         wt=N    Number of alloc reqs that waited on lookup completion
81         nbf=N   Number of alloc reqs rejected -ENOBUFS
82         ops=N   Number of alloc reqs submitted
83         owt=N   Number of alloc reqs waited for CPU time
84 Retrvls n=N Number of retrieval (read) requests seen
85         ok=N    Number of successful retr reqs
86         wt=N    Number of retr reqs that waited on lookup completion
87         nod=N   Number of retr reqs returned -ENODATA
88         nbf=N   Number of retr reqs rejected -ENOBUFS
89         int=N   Number of retr reqs aborted -ERESTARTSYS
90         oom=N   Number of retr reqs failed -ENOMEM
91         ops=N   Number of retr reqs submitted
92         owt=N   Number of retr reqs waited for CPU time
93 Stores  n=N Number of storage (write) requests seen
94         ok=N    Number of successful store reqs
95         agn=N   Number of store reqs on a page already pending storage
96         nbf=N   Number of store reqs rejected -ENOBUFS
97         oom=N   Number of store reqs failed -ENOMEM
98         ops=N   Number of store reqs submitted
99         run=N   Number of store reqs granted CPU time
100 Ops pend=N  Number of times async ops added to pending queues
101         run=N   Number of times async ops given CPU time
102         enq=N   Number of times async ops queued for processing
103         dfr=N   Number of async ops queued for deferred release
104         rel=N   Number of async ops released
105         gc=N    Number of deferred-release async ops garbage collected
106
107 63 events to collect in 13 groups
108 */
109 static void fscache_submit (const char *section, const char *name,
110         value_t value)
111 {
112     value_list_t vl = VALUE_LIST_INIT;
113
114     vl.values = &value;
115     vl.values_len = 1;
116
117     sstrncpy(vl.host, hostname_g, sizeof (vl.host));
118     sstrncpy(vl.plugin, "fscache", sizeof (vl.plugin));
119     sstrncpy(vl.plugin_instance, section, sizeof (vl.plugin_instance));
120     sstrncpy(vl.type, "fscache_stat", sizeof(vl.type));
121     sstrncpy(vl.type_instance, name, sizeof(vl.type_instance));
122
123     plugin_dispatch_values (&vl);
124 }
125
126 static void fscache_read_stats_file (FILE *fh)
127 {
128     char section[DATA_MAX_NAME_LEN];
129     size_t section_len;
130
131     char linebuffer[BUFSIZE];
132
133 /*
134  *  cat /proc/fs/fscache/stats
135  *      FS-Cache statistics
136  *      Cookies: idx=2 dat=0 spc=0
137  *      Objects: alc=0 nal=0 avl=0 ded=0
138  *      ChkAux : non=0 ok=0 upd=0 obs=0
139  *      Pages  : mrk=0 unc=0
140  *      Acquire: n=2 nul=0 noc=0 ok=2 nbf=0 oom=0
141  *      Lookups: n=0 neg=0 pos=0 crt=0
142  *      Updates: n=0 nul=0 run=0
143  *      Relinqs: n=0 nul=0 wcr=0
144  *      AttrChg: n=0 ok=0 nbf=0 oom=0 run=0
145  *      Allocs : n=0 ok=0 wt=0 nbf=0
146  *      Allocs : ops=0 owt=0
147  *      Retrvls: n=0 ok=0 wt=0 nod=0 nbf=0 int=0 oom=0
148  *      Retrvls: ops=0 owt=0
149  *      Stores : n=0 ok=0 agn=0 nbf=0 oom=0
150  *      Stores : ops=0 run=0
151  *      Ops    : pend=0 run=0 enq=0
152  *      Ops    : dfr=0 rel=0 gc=0
153  */
154
155     /* Read file line by line */
156     while (fgets (linebuffer, sizeof (linebuffer), fh) != NULL)
157     {
158         char *lineptr;
159         char *fields[32];
160         int fields_num;
161         int i;
162
163         /* Find the colon and replace it with a null byte */
164         lineptr = strchr (linebuffer, ':');
165         if (lineptr == NULL)
166             continue;
167         *lineptr = 0;
168         lineptr++;
169
170         /* Copy and clean up the section name */
171         sstrncpy (section, linebuffer, sizeof (section));
172         section_len = strlen (section);
173         while ((section_len > 0) && isspace ((int) section[section_len - 1]))
174         {
175             section_len--;
176             section[section_len] = 0;
177         }
178         if (section_len <= 0)
179             continue;
180
181         fields_num = strsplit (lineptr, fields, STATIC_ARRAY_SIZE (fields));
182         if (fields_num <= 0)
183             continue;
184
185         for (i = 0; i < fields_num; i++)
186         {
187             char *field_name;
188             char *field_value_str;
189             value_t field_value_cnt;
190             int status;
191
192             field_name = fields[i];
193             assert (field_name != NULL);
194
195             field_value_str = strchr (field_name, '=');
196             if (field_value_str == NULL)
197                 continue;
198             *field_value_str = 0;
199             field_value_str++;
200
201             status = parse_value (field_value_str, &field_value_cnt,
202                     DS_TYPE_DERIVE);
203             if (status != 0)
204                 continue;
205
206             fscache_submit (section, field_name, field_value_cnt);
207         }
208     } /* while (fgets) */
209 } /* void fscache_read_stats_file */
210
211 static int fscache_read (void){
212     FILE *fh;
213     fh = fopen("/proc/fs/fscache/stats", "r");
214     if (fh != NULL){
215         fscache_read_stats_file(fh);
216         fclose(fh);
217
218     }else{
219         printf("cant open file\n");
220         return (-1);
221     }
222     return (0);
223 }
224
225 void module_register (void)
226 {
227     plugin_register_read ("fscache", fscache_read);
228 } /* void module_register */
229
230 /* vim: set sw=4 sts=4 et : */