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 **/
20 #include "collectd.h"
22 #include "common.h"
23 #include "plugin.h"
24 #include "configfile.h"
27 static const char *config_keys[] = {
28 "ValuesAbsolute",
29 "ValuesPercentage"
30 };
31 static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
33 static _Bool values_absolute = 1;
34 static _Bool values_percentage = 0;
37 static int fhcount_config(const char *key, const char *value) {
38 int ret = -1;
40 if (strcasecmp(key, "ValuesAbsolute") == 0) {
41 if (IS_TRUE(value)) {
42 values_absolute = 1;
43 } else {
44 values_absolute = 0;
45 }
47 ret = 0;
48 } else if (strcasecmp(key, "ValuesPercentage") == 0) {
49 if (IS_TRUE(value)) {
50 values_percentage = 1;
51 } else {
52 values_percentage = 0;
53 }
55 ret = 0;
56 }
58 return(ret);
59 }
62 static void fhcount_submit(
63 const char *type, const char *type_instance, gauge_t value) {
65 value_t values[1];
66 value_list_t vl = VALUE_LIST_INIT;
68 values[0].gauge = value;
70 vl.values = values;
71 vl.values_len = 1;
73 // Compose the metric
74 sstrncpy(vl.host, hostname_g, sizeof(vl.host));
75 sstrncpy(vl.plugin, "fhcount", sizeof(vl.plugin));
76 sstrncpy(vl.type, type, sizeof(vl.type));
77 sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
79 // Dispatch the metric
80 plugin_dispatch_values(&vl);
81 }
84 static int fhcount_read(void) {
85 int numfields = 0;
86 int buffer_len = 60;
87 gauge_t used, unused, max;
88 int prc_used, prc_unused;
89 char *fields[3];
90 char buffer[buffer_len];
91 char errbuf[1024];
92 FILE *fp;
94 // Open file
95 fp = fopen("/proc/sys/fs/file-nr" , "r");
96 if (fp == NULL) {
97 ERROR("fhcount: fopen: %s", sstrerror(errno, errbuf, sizeof(errbuf)));
98 return(EXIT_FAILURE);
99 }
100 if (fgets(buffer, buffer_len, fp) == NULL) {
101 ERROR("fhcount: fgets: %s", sstrerror(errno, errbuf, sizeof(errbuf)));
102 fclose(fp);
103 return(EXIT_FAILURE);
104 }
105 fclose(fp);
107 // Tokenize string
108 numfields = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
110 if (numfields != 3) {
111 ERROR("fhcount: Line doesn't contain 3 fields");
112 return(EXIT_FAILURE);
113 }
115 // Define the values
116 strtogauge(fields[0], &used);
117 strtogauge(fields[1], &unused);
118 strtogauge(fields[2], &max);
119 prc_used = (gauge_t) used/max*100;
120 prc_unused = (gauge_t) unused/max*100;
122 // Submit values
123 if (values_absolute) {
124 fhcount_submit("file_handles", "used", (gauge_t) used);
125 fhcount_submit("file_handles", "unused", (gauge_t) unused);
126 fhcount_submit("file_handles", "max", (gauge_t) max);
127 }
128 if (values_percentage) {
129 fhcount_submit("percent", "used", (gauge_t) prc_used);
130 fhcount_submit("percent", "unused", (gauge_t) prc_unused);
131 }
133 return(0);
134 }
137 void module_register(void) {
138 plugin_register_config(
139 "fhcount", fhcount_config, config_keys, config_keys_num);
140 plugin_register_read("fhcount", fhcount_read);
141 }