| 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 "plugin.h" |
| 23 |
#include "utils/common/common.h" |
| 24 |
|
| 25 |
static const char *config_keys[] = {"ValuesAbsolute", "ValuesPercentage"}; |
| 26 |
static int config_keys_num = STATIC_ARRAY_SIZE(config_keys); |
| 27 |
|
| 28 |
static bool values_absolute = true; |
| 29 |
static bool values_percentage; |
| 30 |
|
| 31 |
static int fhcount_config(const char *key, const char *value) { |
| 32 |
int ret = -1; |
| 33 |
|
| 34 |
if (strcasecmp(key, "ValuesAbsolute") == 0) { |
| 35 |
if (IS_TRUE(value)) { |
| 36 |
values_absolute = true; |
| 37 |
} else { |
| 38 |
values_absolute = false; |
| 39 |
} |
| 40 |
|
| 41 |
ret = 0; |
| 42 |
} else if (strcasecmp(key, "ValuesPercentage") == 0) { |
| 43 |
if (IS_TRUE(value)) { |
| 44 |
values_percentage = true; |
| 45 |
} else { |
| 46 |
values_percentage = false; |
| 47 |
} |
| 48 |
|
| 49 |
ret = 0; |
| 50 |
} |
| 51 |
|
| 52 |
return ret; |
| 53 |
} |
| 54 |
|
| 55 |
static void fhcount_submit(const char *type, const char *type_instance, |
| 56 |
gauge_t value) { |
| 57 |
value_list_t vl = VALUE_LIST_INIT; |
| 58 |
|
| 59 |
vl.values = &(value_t){.gauge = value}; |
| 60 |
vl.values_len = 1; |
| 61 |
|
| 62 |
// Compose the metric |
| 63 |
sstrncpy(vl.plugin, "fhcount", sizeof(vl.plugin)); |
| 64 |
sstrncpy(vl.type, type, sizeof(vl.type)); |
| 65 |
sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance)); |
| 66 |
|
| 67 |
// Dispatch the metric |
| 68 |
plugin_dispatch_values(&vl); |
| 69 |
} |
| 70 |
|
| 71 |
static int fhcount_read(void) { |
| 72 |
int numfields = 0; |
| 73 |
int buffer_len = 60; |
| 74 |
gauge_t used, unused, max; |
| 75 |
int prc_used, prc_unused; |
| 76 |
char *fields[3]; |
| 77 |
char buffer[buffer_len]; |
| 78 |
FILE *fp; |
| 79 |
|
| 80 |
// Open file |
| 81 |
fp = fopen("/proc/sys/fs/file-nr", "r"); |
| 82 |
if (fp == NULL) { |
| 83 |
ERROR("fhcount: fopen: %s", STRERRNO); |
| 84 |
return EXIT_FAILURE; |
| 85 |
} |
| 86 |
if (fgets(buffer, buffer_len, fp) == NULL) { |
| 87 |
ERROR("fhcount: fgets: %s", STRERRNO); |
| 88 |
fclose(fp); |
| 89 |
return EXIT_FAILURE; |
| 90 |
} |
| 91 |
fclose(fp); |
| 92 |
|
| 93 |
// Tokenize string |
| 94 |
numfields = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields)); |
| 95 |
|
| 96 |
if (numfields != 3) { |
| 97 |
ERROR("fhcount: Line doesn't contain 3 fields"); |
| 98 |
return EXIT_FAILURE; |
| 99 |
} |
| 100 |
|
| 101 |
// Define the values |
| 102 |
strtogauge(fields[0], &used); |
| 103 |
strtogauge(fields[1], &unused); |
| 104 |
strtogauge(fields[2], &max); |
| 105 |
prc_used = (gauge_t)used / max * 100; |
| 106 |
prc_unused = (gauge_t)unused / max * 100; |
| 107 |
|
| 108 |
// Submit values |
| 109 |
if (values_absolute) { |
| 110 |
fhcount_submit("file_handles", "used", (gauge_t)used); |
| 111 |
fhcount_submit("file_handles", "unused", (gauge_t)unused); |
| 112 |
fhcount_submit("file_handles", "max", (gauge_t)max); |
| 113 |
} |
| 114 |
if (values_percentage) { |
| 115 |
fhcount_submit("percent", "used", (gauge_t)prc_used); |
| 116 |
fhcount_submit("percent", "unused", (gauge_t)prc_unused); |
| 117 |
} |
| 118 |
|
| 119 |
return 0; |
| 120 |
} |
| 121 |
|
| 122 |
void module_register(void) { |
| 123 |
plugin_register_config("fhcount", fhcount_config, config_keys, |
| 124 |
config_keys_num); |
| 125 |
plugin_register_read("fhcount", fhcount_read); |
| 126 |
} |