Tree wide: Don't set vl.host to hostname_g in plugin code.
[collectd.git] / src / fhcount.c
1 /**
2  *
3  * collectd - src/fhcount.c
4  * Copyright (c) 2015, Jiri Tyr <jiri.tyr at gmail.com>
5  *
6  * Permission to use, copy, modify, and/or distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
13  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
15  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
16  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  *
18  **/
19
20 #include "collectd.h"
21
22 #include "common.h"
23 #include "plugin.h"
24
25
26 static const char *config_keys[] = {
27   "ValuesAbsolute",
28   "ValuesPercentage"
29 };
30 static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
31
32 static _Bool values_absolute = 1;
33 static _Bool values_percentage = 0;
34
35
36 static int fhcount_config(const char *key, const char *value) {
37   int ret = -1;
38
39   if (strcasecmp(key, "ValuesAbsolute") == 0) {
40     if (IS_TRUE(value)) {
41       values_absolute = 1;
42     } else {
43       values_absolute = 0;
44     }
45
46     ret = 0;
47   } else if (strcasecmp(key, "ValuesPercentage") == 0) {
48     if (IS_TRUE(value)) {
49       values_percentage = 1;
50     } else {
51       values_percentage = 0;
52     }
53
54     ret = 0;
55   }
56
57   return(ret);
58 }
59
60
61 static void fhcount_submit(
62     const char *type, const char *type_instance, gauge_t value) {
63   value_list_t vl = VALUE_LIST_INIT;
64
65   vl.values = &(value_t) { .gauge = value };
66   vl.values_len = 1;
67
68   // Compose the metric
69   sstrncpy(vl.plugin, "fhcount", sizeof(vl.plugin));
70   sstrncpy(vl.type, type, sizeof(vl.type));
71   sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
72
73   // Dispatch the metric
74   plugin_dispatch_values(&vl);
75 }
76
77 static int fhcount_read(void) {
78   int numfields = 0;
79   int buffer_len = 60;
80   gauge_t used, unused, max;
81   int prc_used, prc_unused;
82   char *fields[3];
83   char buffer[buffer_len];
84   char errbuf[1024];
85   FILE *fp;
86
87   // Open file
88   fp = fopen("/proc/sys/fs/file-nr" , "r");
89   if (fp == NULL) {
90     ERROR("fhcount: fopen: %s", sstrerror(errno, errbuf, sizeof(errbuf)));
91     return(EXIT_FAILURE);
92   }
93   if (fgets(buffer, buffer_len, fp) == NULL) {
94     ERROR("fhcount: fgets: %s", sstrerror(errno, errbuf, sizeof(errbuf)));
95     fclose(fp);
96     return(EXIT_FAILURE);
97   }
98   fclose(fp);
99
100   // Tokenize string
101   numfields = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
102
103   if (numfields != 3) {
104     ERROR("fhcount: Line doesn't contain 3 fields");
105     return(EXIT_FAILURE);
106   }
107
108   // Define the values
109   strtogauge(fields[0], &used);
110   strtogauge(fields[1], &unused);
111   strtogauge(fields[2], &max);
112   prc_used = (gauge_t) used/max*100;
113   prc_unused = (gauge_t) unused/max*100;
114
115   // Submit values
116   if (values_absolute) {
117     fhcount_submit("file_handles", "used", (gauge_t) used);
118     fhcount_submit("file_handles", "unused", (gauge_t) unused);
119     fhcount_submit("file_handles", "max", (gauge_t) max);
120   }
121   if (values_percentage) {
122     fhcount_submit("percent", "used", (gauge_t) prc_used);
123     fhcount_submit("percent", "unused", (gauge_t) prc_unused);
124   }
125
126   return(0);
127 }
128
129
130 void module_register(void) {
131   plugin_register_config(
132     "fhcount", fhcount_config, config_keys, config_keys_num);
133   plugin_register_read("fhcount", fhcount_read);
134 }