| 1 |
/** |
| 2 |
* collectd - src/buddyinfo.c |
| 3 |
* Copyright (C) 2019 Asaf Kahlon |
| 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 |
* Asaf Kahlon <asafka7 at gmail.com> |
| 20 |
**/ |
| 21 |
|
| 22 |
#include "collectd.h" |
| 23 |
|
| 24 |
#include "plugin.h" |
| 25 |
#include "utils/common/common.h" |
| 26 |
#include "utils/ignorelist/ignorelist.h" |
| 27 |
|
| 28 |
#if !KERNEL_LINUX |
| 29 |
#error "No applicable input method." |
| 30 |
#endif |
| 31 |
|
| 32 |
#include <unistd.h> |
| 33 |
|
| 34 |
#define MAX_ORDER 11 |
| 35 |
#define BUDDYINFO_FIELDS \ |
| 36 |
MAX_ORDER + 4 // "Node" + node_num + "zone" + Name + (MAX_ORDER entries) |
| 37 |
#define NUM_OF_KB(pagesize, order) ((pagesize) / 1024) * (1 << (order)) |
| 38 |
|
| 39 |
static const char *config_keys[] = {"Zone"}; |
| 40 |
static int config_keys_num = STATIC_ARRAY_SIZE(config_keys); |
| 41 |
|
| 42 |
static ignorelist_t *ignorelist; |
| 43 |
|
| 44 |
static int buddyinfo_config(const char *key, const char *value) { |
| 45 |
if (ignorelist == NULL) { |
| 46 |
ignorelist = ignorelist_create(1); |
| 47 |
if (ignorelist == NULL) { |
| 48 |
ERROR("buddyinfo plugin: ignorelist_create failed"); |
| 49 |
return -ENOMEM; |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
if (strcasecmp(key, "Zone") == 0) { |
| 54 |
if (ignorelist_add(ignorelist, value)) { |
| 55 |
ERROR("buddyinfo plugin: cannot add value to ignorelist"); |
| 56 |
return -1; |
| 57 |
} |
| 58 |
} else { |
| 59 |
ERROR("buddyinfo plugin: invalid option: %s", key); |
| 60 |
return -1; |
| 61 |
} |
| 62 |
|
| 63 |
return 0; |
| 64 |
} |
| 65 |
|
| 66 |
static void buddyinfo_submit(const char *zone_fullname, const char *zone, |
| 67 |
const char *size, const int freepages) { |
| 68 |
value_list_t vl = VALUE_LIST_INIT; |
| 69 |
value_t value = {.gauge = freepages}; |
| 70 |
|
| 71 |
if (ignorelist_match(ignorelist, zone) != 0) |
| 72 |
return; |
| 73 |
|
| 74 |
vl.values = &value; |
| 75 |
vl.values_len = 1; |
| 76 |
sstrncpy(vl.plugin, "buddyinfo", sizeof(vl.plugin)); |
| 77 |
sstrncpy(vl.plugin_instance, zone_fullname, sizeof(vl.plugin_instance)); |
| 78 |
sstrncpy(vl.type, "freepages", sizeof(vl.type)); |
| 79 |
sstrncpy(vl.type_instance, size, sizeof(vl.type_instance)); |
| 80 |
|
| 81 |
plugin_dispatch_values(&vl); |
| 82 |
} |
| 83 |
|
| 84 |
static int buddyinfo_read(void) { |
| 85 |
FILE *fh; |
| 86 |
char buffer[1024], pagesize_kb[8], zone_fullname[16]; |
| 87 |
char *dummy, *zone; |
| 88 |
char *fields[BUDDYINFO_FIELDS]; |
| 89 |
int node_num, numfields, pagesize = getpagesize(); |
| 90 |
|
| 91 |
if ((fh = fopen("/proc/buddyinfo", "r")) == NULL) { |
| 92 |
WARNING("buddyinfo plugin: fopen: %s", STRERRNO); |
| 93 |
return -1; |
| 94 |
} |
| 95 |
|
| 96 |
while (fgets(buffer, sizeof(buffer), fh) != NULL) { |
| 97 |
if (!(dummy = strstr(buffer, "Node"))) |
| 98 |
continue; |
| 99 |
|
| 100 |
numfields = strsplit(dummy, fields, BUDDYINFO_FIELDS); |
| 101 |
if (numfields != BUDDYINFO_FIELDS) { |
| 102 |
WARNING("line %s doesn't contain %d orders, skipping...", buffer, |
| 103 |
MAX_ORDER); |
| 104 |
continue; |
| 105 |
} |
| 106 |
|
| 107 |
node_num = atoi(fields[1]); |
| 108 |
zone = fields[3]; |
| 109 |
ssnprintf(zone_fullname, sizeof(zone_fullname), "Node%d/%s", node_num, |
| 110 |
zone); |
| 111 |
for (int i = 1; i <= MAX_ORDER; i++) { |
| 112 |
ssnprintf(pagesize_kb, sizeof(pagesize_kb), "%dKB", |
| 113 |
NUM_OF_KB(pagesize, i - 1)); |
| 114 |
buddyinfo_submit(zone_fullname, zone, pagesize_kb, atoi(fields[i + 3])); |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
fclose(fh); |
| 119 |
return 0; |
| 120 |
} |
| 121 |
|
| 122 |
static int buddyinfo_shutdown(void) { |
| 123 |
ignorelist_free(ignorelist); |
| 124 |
|
| 125 |
return 0; |
| 126 |
} |
| 127 |
|
| 128 |
void module_register(void) { |
| 129 |
|
| 130 |
plugin_register_config("buddyinfo", buddyinfo_config, config_keys, |
| 131 |
config_keys_num); |
| 132 |
plugin_register_read("buddyinfo", buddyinfo_read); |
| 133 |
plugin_register_shutdown("buddy", buddyinfo_shutdown); |
| 134 |
} |