| 1 |
/** |
| 2 |
* collectd - src/ubi.c |
| 3 |
* |
| 4 |
* This program is free software; you can redistribute it and/or modify it |
| 5 |
* under the terms of the GNU General Public License as published by the |
| 6 |
* Free Software Foundation; only version 2 of the License is applicable. |
| 7 |
* |
| 8 |
* This program is distributed in the hope that it will be useful, but |
| 9 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 |
* General Public License for more details. |
| 12 |
* |
| 13 |
* You should have received a copy of the GNU General Public License along |
| 14 |
* with this program; if not, write to the Free Software Foundation, Inc., |
| 15 |
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 16 |
*/ |
| 17 |
|
| 18 |
#include "collectd.h" |
| 19 |
|
| 20 |
#include "plugin.h" |
| 21 |
#include "utils/common/common.h" |
| 22 |
#include "utils/ignorelist/ignorelist.h" |
| 23 |
|
| 24 |
#if !KERNEL_LINUX |
| 25 |
#error "No applicable input method." |
| 26 |
#endif |
| 27 |
|
| 28 |
#define PLUGIN_NAME "ubi" |
| 29 |
|
| 30 |
#define SYS_PATH "/sys/class/ubi/" |
| 31 |
|
| 32 |
/* |
| 33 |
* Device attributes |
| 34 |
*/ |
| 35 |
#define DEV_BAD_COUNT \ |
| 36 |
"bad_peb_count" // Count of bad physical eraseblocks on the underlying MTD |
| 37 |
// device. |
| 38 |
#define MAXIMUM_ERASE "max_ec" // Current maximum erase counter value |
| 39 |
|
| 40 |
/* |
| 41 |
* The config key strings |
| 42 |
*/ |
| 43 |
#define DEVICE_KEY "Device" |
| 44 |
#define IGNORE_KEY "IgnoreSelected" |
| 45 |
|
| 46 |
static const char *config_keys[] = { |
| 47 |
DEVICE_KEY, |
| 48 |
IGNORE_KEY, |
| 49 |
}; |
| 50 |
static int config_keys_num = STATIC_ARRAY_SIZE(config_keys); |
| 51 |
|
| 52 |
static ignorelist_t *ignorelist = NULL; |
| 53 |
|
| 54 |
/* |
| 55 |
* Private functions |
| 56 |
*/ |
| 57 |
static int ubi_config(const char *key, const char *value) { |
| 58 |
if (ignorelist == NULL && |
| 59 |
(ignorelist = ignorelist_create(/* invert = */ 1)) == NULL) |
| 60 |
return -1; |
| 61 |
|
| 62 |
if (strcasecmp(key, DEVICE_KEY) == 0) |
| 63 |
ignorelist_add(ignorelist, value); |
| 64 |
else if (strcasecmp(key, IGNORE_KEY) == 0) |
| 65 |
ignorelist_set_invert(ignorelist, IS_TRUE(value) ? 0 : 1); |
| 66 |
else |
| 67 |
return -1; |
| 68 |
|
| 69 |
return 0; |
| 70 |
} /* int ubi_config */ |
| 71 |
|
| 72 |
static void ubi_submit(const char *dev_name, const char *type, gauge_t value) { |
| 73 |
value_list_t vl = VALUE_LIST_INIT; |
| 74 |
|
| 75 |
if (ignorelist_match(ignorelist, dev_name) != 0) |
| 76 |
return; |
| 77 |
|
| 78 |
vl.values = &(value_t){.gauge = value}; |
| 79 |
vl.values_len = 1; |
| 80 |
sstrncpy(vl.plugin, PLUGIN_NAME, sizeof(vl.plugin)); |
| 81 |
sstrncpy(vl.type_instance, dev_name, sizeof(vl.type_instance)); |
| 82 |
sstrncpy(vl.type, type, sizeof(vl.type)); |
| 83 |
|
| 84 |
plugin_dispatch_values(&vl); |
| 85 |
} /* void ubi_submit */ |
| 86 |
|
| 87 |
static int ubi_read_dev_attr(const char *dev_name, const char *attr) { |
| 88 |
FILE *f; |
| 89 |
int val; |
| 90 |
char |
| 91 |
str[sizeof(SYS_PATH) + strlen(dev_name) + sizeof("/") + strlen(attr) + 1]; |
| 92 |
int n; |
| 93 |
|
| 94 |
snprintf(str, sizeof(str), SYS_PATH "%s/%s", dev_name, attr); |
| 95 |
|
| 96 |
if ((f = fopen(str, "r")) == NULL) { |
| 97 |
ERROR(PLUGIN_NAME ": cannot open [%s]", str); |
| 98 |
return -1; |
| 99 |
} |
| 100 |
|
| 101 |
n = fscanf(f, "%d", &val); |
| 102 |
fclose(f); |
| 103 |
|
| 104 |
if (n != 1) { |
| 105 |
ERROR(PLUGIN_NAME " : did not find an integer in %s", str); |
| 106 |
return -1; |
| 107 |
} |
| 108 |
|
| 109 |
ubi_submit(dev_name, attr, (gauge_t)val); |
| 110 |
|
| 111 |
return 0; |
| 112 |
} /* int ubi_read_dev_attr */ |
| 113 |
|
| 114 |
static inline int ubi_read_dev_bad_count(const char *dev_name) { |
| 115 |
return ubi_read_dev_attr(dev_name, DEV_BAD_COUNT); |
| 116 |
} /* int ubi_read_dev_bad_count */ |
| 117 |
|
| 118 |
static inline int ubi_read_max_ec(const char *dev_name) { |
| 119 |
return ubi_read_dev_attr(dev_name, MAXIMUM_ERASE); |
| 120 |
} /* int ubi_read_max_ec */ |
| 121 |
|
| 122 |
static int ubi_read(void) { |
| 123 |
DIR *dir; |
| 124 |
struct dirent *dirent; |
| 125 |
|
| 126 |
if ((dir = opendir(SYS_PATH)) == NULL) { |
| 127 |
ERROR(PLUGIN_NAME " : cannot open dir " SYS_PATH); |
| 128 |
return -1; |
| 129 |
} |
| 130 |
|
| 131 |
while ((dirent = readdir(dir)) != NULL) { |
| 132 |
if (ignorelist_match(ignorelist, dirent->d_name)) |
| 133 |
continue; |
| 134 |
|
| 135 |
ubi_read_dev_bad_count(dirent->d_name); |
| 136 |
ubi_read_max_ec(dirent->d_name); |
| 137 |
} |
| 138 |
|
| 139 |
closedir(dir); |
| 140 |
|
| 141 |
return 0; |
| 142 |
} /* int ubi_read */ |
| 143 |
|
| 144 |
void module_register(void) { |
| 145 |
plugin_register_config(PLUGIN_NAME, ubi_config, config_keys, config_keys_num); |
| 146 |
plugin_register_read(PLUGIN_NAME, ubi_read); |
| 147 |
} /* void module_register */ |