| 1 |
/** |
| 2 |
* collectd - src/protocols.c |
| 3 |
* Copyright (C) 2009,2010 Florian octo Forster |
| 4 |
* |
| 5 |
* Permission is hereby granted, free of charge, to any person obtaining a |
| 6 |
* copy of this software and associated documentation files (the "Software"), |
| 7 |
* to deal in the Software without restriction, including without limitation |
| 8 |
* the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 9 |
* and/or sell copies of the Software, and to permit persons to whom the |
| 10 |
* Software is furnished to do so, subject to the following conditions: |
| 11 |
* |
| 12 |
* The above copyright notice and this permission notice shall be included in |
| 13 |
* all copies or substantial portions of the Software. |
| 14 |
* |
| 15 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 20 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 21 |
* DEALINGS IN THE SOFTWARE. |
| 22 |
* |
| 23 |
* Authors: |
| 24 |
* Florian octo Forster <octo at collectd.org> |
| 25 |
**/ |
| 26 |
|
| 27 |
#include "collectd.h" |
| 28 |
|
| 29 |
#include "plugin.h" |
| 30 |
#include "utils/common/common.h" |
| 31 |
#include "utils/ignorelist/ignorelist.h" |
| 32 |
|
| 33 |
#if !KERNEL_LINUX |
| 34 |
#error "No applicable input method." |
| 35 |
#endif |
| 36 |
|
| 37 |
#define SNMP_FILE "/proc/net/snmp" |
| 38 |
#define NETSTAT_FILE "/proc/net/netstat" |
| 39 |
|
| 40 |
/* |
| 41 |
* Global variables |
| 42 |
*/ |
| 43 |
static const char *config_keys[] = { |
| 44 |
"Value", |
| 45 |
"IgnoreSelected", |
| 46 |
}; |
| 47 |
static int config_keys_num = STATIC_ARRAY_SIZE(config_keys); |
| 48 |
|
| 49 |
static ignorelist_t *values_list; |
| 50 |
|
| 51 |
/* |
| 52 |
* Functions |
| 53 |
*/ |
| 54 |
static void submit(const char *protocol_name, const char *str_key, |
| 55 |
const char *str_value) { |
| 56 |
value_t value; |
| 57 |
value_list_t vl = VALUE_LIST_INIT; |
| 58 |
int status; |
| 59 |
|
| 60 |
status = parse_value(str_value, &value, DS_TYPE_DERIVE); |
| 61 |
if (status != 0) { |
| 62 |
return; |
| 63 |
} |
| 64 |
|
| 65 |
vl.values = &value; |
| 66 |
vl.values_len = 1; |
| 67 |
sstrncpy(vl.plugin, "protocols", sizeof(vl.plugin)); |
| 68 |
sstrncpy(vl.plugin_instance, protocol_name, sizeof(vl.plugin_instance)); |
| 69 |
sstrncpy(vl.type, "protocol_counter", sizeof(vl.type)); |
| 70 |
sstrncpy(vl.type_instance, str_key, sizeof(vl.type_instance)); |
| 71 |
|
| 72 |
plugin_dispatch_values(&vl); |
| 73 |
} /* void submit */ |
| 74 |
|
| 75 |
static int read_file(const char *path) { |
| 76 |
FILE *fh; |
| 77 |
char key_buffer[4096]; |
| 78 |
char value_buffer[4096]; |
| 79 |
char *key_ptr; |
| 80 |
char *value_ptr; |
| 81 |
char *key_fields[256]; |
| 82 |
char *value_fields[256]; |
| 83 |
int key_fields_num; |
| 84 |
int value_fields_num; |
| 85 |
int status; |
| 86 |
int i; |
| 87 |
|
| 88 |
fh = fopen(path, "r"); |
| 89 |
if (fh == NULL) { |
| 90 |
ERROR("protocols plugin: fopen (%s) failed: %s.", path, STRERRNO); |
| 91 |
return -1; |
| 92 |
} |
| 93 |
|
| 94 |
status = -1; |
| 95 |
while (42) { |
| 96 |
clearerr(fh); |
| 97 |
key_ptr = fgets(key_buffer, sizeof(key_buffer), fh); |
| 98 |
if (key_ptr == NULL) { |
| 99 |
if (feof(fh) != 0) { |
| 100 |
status = 0; |
| 101 |
break; |
| 102 |
} else if (ferror(fh) != 0) { |
| 103 |
ERROR("protocols plugin: Reading from %s failed.", path); |
| 104 |
break; |
| 105 |
} else { |
| 106 |
ERROR("protocols plugin: fgets failed for an unknown reason."); |
| 107 |
break; |
| 108 |
} |
| 109 |
} /* if (key_ptr == NULL) */ |
| 110 |
|
| 111 |
value_ptr = fgets(value_buffer, sizeof(value_buffer), fh); |
| 112 |
if (value_ptr == NULL) { |
| 113 |
ERROR("protocols plugin: read_file (%s): Could not read values line.", |
| 114 |
path); |
| 115 |
break; |
| 116 |
} |
| 117 |
|
| 118 |
key_ptr = strchr(key_buffer, ':'); |
| 119 |
if (key_ptr == NULL) { |
| 120 |
ERROR("protocols plugin: Could not find protocol name in keys line."); |
| 121 |
break; |
| 122 |
} |
| 123 |
*key_ptr = 0; |
| 124 |
key_ptr++; |
| 125 |
|
| 126 |
value_ptr = strchr(value_buffer, ':'); |
| 127 |
if (value_ptr == NULL) { |
| 128 |
ERROR("protocols plugin: Could not find protocol name " |
| 129 |
"in values line."); |
| 130 |
break; |
| 131 |
} |
| 132 |
*value_ptr = 0; |
| 133 |
value_ptr++; |
| 134 |
|
| 135 |
if (strcmp(key_buffer, value_buffer) != 0) { |
| 136 |
ERROR("protocols plugin: Protocol names in keys and values lines " |
| 137 |
"don't match: `%s' vs. `%s'.", |
| 138 |
key_buffer, value_buffer); |
| 139 |
break; |
| 140 |
} |
| 141 |
|
| 142 |
key_fields_num = |
| 143 |
strsplit(key_ptr, key_fields, STATIC_ARRAY_SIZE(key_fields)); |
| 144 |
value_fields_num = |
| 145 |
strsplit(value_ptr, value_fields, STATIC_ARRAY_SIZE(value_fields)); |
| 146 |
|
| 147 |
if (key_fields_num != value_fields_num) { |
| 148 |
ERROR("protocols plugin: Number of fields in keys and values lines " |
| 149 |
"don't match: %i vs %i.", |
| 150 |
key_fields_num, value_fields_num); |
| 151 |
break; |
| 152 |
} |
| 153 |
|
| 154 |
for (i = 0; i < key_fields_num; i++) { |
| 155 |
if (values_list != NULL) { |
| 156 |
char match_name[2 * DATA_MAX_NAME_LEN]; |
| 157 |
|
| 158 |
ssnprintf(match_name, sizeof(match_name), "%s:%s", key_buffer, |
| 159 |
key_fields[i]); |
| 160 |
|
| 161 |
if (ignorelist_match(values_list, match_name)) |
| 162 |
continue; |
| 163 |
} /* if (values_list != NULL) */ |
| 164 |
|
| 165 |
submit(key_buffer, key_fields[i], value_fields[i]); |
| 166 |
} /* for (i = 0; i < key_fields_num; i++) */ |
| 167 |
} /* while (42) */ |
| 168 |
|
| 169 |
fclose(fh); |
| 170 |
|
| 171 |
return status; |
| 172 |
} /* int read_file */ |
| 173 |
|
| 174 |
static int protocols_read(void) { |
| 175 |
int status; |
| 176 |
int success = 0; |
| 177 |
|
| 178 |
status = read_file(SNMP_FILE); |
| 179 |
if (status == 0) |
| 180 |
success++; |
| 181 |
|
| 182 |
status = read_file(NETSTAT_FILE); |
| 183 |
if (status == 0) |
| 184 |
success++; |
| 185 |
|
| 186 |
if (success == 0) |
| 187 |
return -1; |
| 188 |
|
| 189 |
return 0; |
| 190 |
} /* int protocols_read */ |
| 191 |
|
| 192 |
static int protocols_config(const char *key, const char *value) { |
| 193 |
if (values_list == NULL) |
| 194 |
values_list = ignorelist_create(/* invert = */ 1); |
| 195 |
|
| 196 |
if (strcasecmp(key, "Value") == 0) { |
| 197 |
ignorelist_add(values_list, value); |
| 198 |
} else if (strcasecmp(key, "IgnoreSelected") == 0) { |
| 199 |
int invert = 1; |
| 200 |
if (IS_TRUE(value)) |
| 201 |
invert = 0; |
| 202 |
ignorelist_set_invert(values_list, invert); |
| 203 |
} else { |
| 204 |
return -1; |
| 205 |
} |
| 206 |
|
| 207 |
return 0; |
| 208 |
} /* int protocols_config */ |
| 209 |
|
| 210 |
void module_register(void) { |
| 211 |
plugin_register_config("protocols", protocols_config, config_keys, |
| 212 |
config_keys_num); |
| 213 |
plugin_register_read("protocols", protocols_read); |
| 214 |
} /* void module_register */ |