| 1 |
/** |
| 2 |
* collectd - src/mbmon.c |
| 3 |
* Copyright (C) 2006 Flavio Stanchina |
| 4 |
* Copyright (C) 2006-2007 Florian octo Forster |
| 5 |
* Based on the hddtemp plugin. |
| 6 |
* |
| 7 |
* This program is free software; you can redistribute it and/or modify it |
| 8 |
* under the terms of the GNU General Public License as published by the |
| 9 |
* Free Software Foundation; either version 2 of the License, or (at your |
| 10 |
* option) any later version. |
| 11 |
* |
| 12 |
* This program is distributed in the hope that it will be useful, but |
| 13 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 |
* General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU General Public License along |
| 18 |
* with this program; if not, write to the Free Software Foundation, Inc., |
| 19 |
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 |
* |
| 21 |
* Authors: |
| 22 |
* Flavio Stanchina <flavio at stanchina.net> |
| 23 |
* Florian Forster <octo at collectd.org> |
| 24 |
**/ |
| 25 |
|
| 26 |
#include "collectd.h" |
| 27 |
|
| 28 |
#include "plugin.h" |
| 29 |
#include "utils/common/common.h" |
| 30 |
|
| 31 |
#include <netdb.h> |
| 32 |
#include <netinet/in.h> |
| 33 |
#include <netinet/tcp.h> |
| 34 |
|
| 35 |
#define MBMON_DEF_HOST "127.0.0.1" |
| 36 |
#define MBMON_DEF_PORT "411" /* the default for Debian */ |
| 37 |
|
| 38 |
static const char *config_keys[] = {"Host", "Port", NULL}; |
| 39 |
static int config_keys_num = 2; |
| 40 |
|
| 41 |
static char *mbmon_host; |
| 42 |
static char *mbmon_port; |
| 43 |
|
| 44 |
/* |
| 45 |
* NAME |
| 46 |
* mbmon_query_daemon |
| 47 |
* |
| 48 |
* DESCRIPTION |
| 49 |
* Connect to the mbmon daemon and receive data. |
| 50 |
* |
| 51 |
* ARGUMENTS: |
| 52 |
* `buffer' The buffer where we put the received ascii string. |
| 53 |
* `buffer_size' Size of the buffer |
| 54 |
* |
| 55 |
* RETURN VALUE: |
| 56 |
* >= 0 if ok, < 0 otherwise. |
| 57 |
* |
| 58 |
* NOTES: |
| 59 |
* Example of possible strings, as received from daemon: |
| 60 |
* TEMP0 : 27.0 |
| 61 |
* TEMP1 : 31.0 |
| 62 |
* TEMP2 : 29.5 |
| 63 |
* FAN0 : 4411 |
| 64 |
* FAN1 : 4470 |
| 65 |
* FAN2 : 4963 |
| 66 |
* VC0 : +1.68 |
| 67 |
* VC1 : +1.73 |
| 68 |
* |
| 69 |
* FIXME: |
| 70 |
* we need to create a new socket each time. Is there another way? |
| 71 |
* Hm, maybe we can re-use the `sockaddr' structure? -octo |
| 72 |
*/ |
| 73 |
static int mbmon_query_daemon(char *buffer, int buffer_size) { |
| 74 |
int fd; |
| 75 |
ssize_t status; |
| 76 |
int buffer_fill; |
| 77 |
|
| 78 |
const char *host; |
| 79 |
const char *port; |
| 80 |
|
| 81 |
struct addrinfo *ai_list; |
| 82 |
int ai_return; |
| 83 |
|
| 84 |
host = mbmon_host; |
| 85 |
if (host == NULL) |
| 86 |
host = MBMON_DEF_HOST; |
| 87 |
|
| 88 |
port = mbmon_port; |
| 89 |
if (port == NULL) |
| 90 |
port = MBMON_DEF_PORT; |
| 91 |
|
| 92 |
struct addrinfo ai_hints = {.ai_family = AF_UNSPEC, |
| 93 |
.ai_flags = AI_ADDRCONFIG, |
| 94 |
.ai_protocol = IPPROTO_TCP, |
| 95 |
.ai_socktype = SOCK_STREAM}; |
| 96 |
|
| 97 |
if ((ai_return = getaddrinfo(host, port, &ai_hints, &ai_list)) != 0) { |
| 98 |
ERROR("mbmon: getaddrinfo (%s, %s): %s", host, port, |
| 99 |
(ai_return == EAI_SYSTEM) ? STRERRNO : gai_strerror(ai_return)); |
| 100 |
return -1; |
| 101 |
} |
| 102 |
|
| 103 |
fd = -1; |
| 104 |
for (struct addrinfo *ai_ptr = ai_list; ai_ptr != NULL; |
| 105 |
ai_ptr = ai_ptr->ai_next) { |
| 106 |
/* create our socket descriptor */ |
| 107 |
if ((fd = socket(ai_ptr->ai_family, ai_ptr->ai_socktype, |
| 108 |
ai_ptr->ai_protocol)) < 0) { |
| 109 |
ERROR("mbmon: socket: %s", STRERRNO); |
| 110 |
continue; |
| 111 |
} |
| 112 |
|
| 113 |
/* connect to the mbmon daemon */ |
| 114 |
if (connect(fd, (struct sockaddr *)ai_ptr->ai_addr, ai_ptr->ai_addrlen)) { |
| 115 |
INFO("mbmon: connect (%s, %s): %s", host, port, STRERRNO); |
| 116 |
close(fd); |
| 117 |
fd = -1; |
| 118 |
continue; |
| 119 |
} |
| 120 |
|
| 121 |
/* A socket could be opened and connecting succeeded. We're |
| 122 |
* done. */ |
| 123 |
break; |
| 124 |
} |
| 125 |
|
| 126 |
freeaddrinfo(ai_list); |
| 127 |
|
| 128 |
if (fd < 0) { |
| 129 |
ERROR("mbmon: Could not connect to daemon."); |
| 130 |
return -1; |
| 131 |
} |
| 132 |
|
| 133 |
/* receive data from the mbmon daemon */ |
| 134 |
memset(buffer, '\0', buffer_size); |
| 135 |
|
| 136 |
buffer_fill = 0; |
| 137 |
while ((status = read(fd, buffer + buffer_fill, buffer_size - buffer_fill)) != |
| 138 |
0) { |
| 139 |
if (status == -1) { |
| 140 |
|
| 141 |
if ((errno == EAGAIN) || (errno == EINTR)) |
| 142 |
continue; |
| 143 |
|
| 144 |
ERROR("mbmon: Error reading from socket: %s", STRERRNO); |
| 145 |
close(fd); |
| 146 |
return -1; |
| 147 |
} |
| 148 |
buffer_fill += status; |
| 149 |
|
| 150 |
if (buffer_fill >= buffer_size) |
| 151 |
break; |
| 152 |
} |
| 153 |
|
| 154 |
if (buffer_fill >= buffer_size) { |
| 155 |
buffer[buffer_size - 1] = '\0'; |
| 156 |
WARNING("mbmon: Message from mbmon has been truncated."); |
| 157 |
} else if (buffer_fill == 0) { |
| 158 |
WARNING("mbmon: Peer has unexpectedly shut down the socket. " |
| 159 |
"Buffer: `%s'", |
| 160 |
buffer); |
| 161 |
close(fd); |
| 162 |
return -1; |
| 163 |
} |
| 164 |
|
| 165 |
close(fd); |
| 166 |
return 0; |
| 167 |
} |
| 168 |
|
| 169 |
static int mbmon_config(const char *key, const char *value) { |
| 170 |
if (strcasecmp(key, "host") == 0) { |
| 171 |
if (mbmon_host != NULL) |
| 172 |
free(mbmon_host); |
| 173 |
mbmon_host = strdup(value); |
| 174 |
} else if (strcasecmp(key, "port") == 0) { |
| 175 |
if (mbmon_port != NULL) |
| 176 |
free(mbmon_port); |
| 177 |
mbmon_port = strdup(value); |
| 178 |
} else { |
| 179 |
return -1; |
| 180 |
} |
| 181 |
|
| 182 |
return 0; |
| 183 |
} |
| 184 |
|
| 185 |
static void mbmon_submit(const char *type, const char *type_instance, |
| 186 |
double value) { |
| 187 |
value_list_t vl = VALUE_LIST_INIT; |
| 188 |
|
| 189 |
vl.values = &(value_t){.gauge = value}; |
| 190 |
vl.values_len = 1; |
| 191 |
sstrncpy(vl.plugin, "mbmon", sizeof(vl.plugin)); |
| 192 |
sstrncpy(vl.type, type, sizeof(vl.type)); |
| 193 |
sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance)); |
| 194 |
|
| 195 |
plugin_dispatch_values(&vl); |
| 196 |
} /* void mbmon_submit */ |
| 197 |
|
| 198 |
/* Trim trailing whitespace from a string. */ |
| 199 |
static void trim_spaces(char *s) { |
| 200 |
for (size_t l = strlen(s) - 1; (l > 0) && isspace((int)s[l]); l--) |
| 201 |
s[l] = '\0'; |
| 202 |
} |
| 203 |
|
| 204 |
static int mbmon_read(void) { |
| 205 |
char buf[1024]; |
| 206 |
char *s, *t; |
| 207 |
|
| 208 |
/* get data from daemon */ |
| 209 |
if (mbmon_query_daemon(buf, sizeof(buf)) < 0) |
| 210 |
return -1; |
| 211 |
|
| 212 |
s = buf; |
| 213 |
while ((t = strchr(s, ':')) != NULL) { |
| 214 |
double value; |
| 215 |
char *nextc; |
| 216 |
|
| 217 |
const char *type; |
| 218 |
const char *inst; |
| 219 |
|
| 220 |
*t++ = '\0'; |
| 221 |
trim_spaces(s); |
| 222 |
|
| 223 |
value = strtod(t, &nextc); |
| 224 |
if ((*nextc != '\n') && (*nextc != '\0')) { |
| 225 |
ERROR("mbmon: value for `%s' contains invalid characters: `%s'", s, t); |
| 226 |
break; |
| 227 |
} |
| 228 |
|
| 229 |
if (strncmp(s, "TEMP", 4) == 0) { |
| 230 |
inst = s + 4; |
| 231 |
type = "temperature"; |
| 232 |
} else if (strncmp(s, "FAN", 3) == 0) { |
| 233 |
inst = s + 3; |
| 234 |
type = "fanspeed"; |
| 235 |
} else if (strncmp(s, "V", 1) == 0) { |
| 236 |
inst = s + 1; |
| 237 |
type = "voltage"; |
| 238 |
} else { |
| 239 |
continue; |
| 240 |
} |
| 241 |
|
| 242 |
mbmon_submit(type, inst, value); |
| 243 |
|
| 244 |
if (*nextc == '\0') |
| 245 |
break; |
| 246 |
|
| 247 |
s = nextc + 1; |
| 248 |
} |
| 249 |
|
| 250 |
return 0; |
| 251 |
} /* void mbmon_read */ |
| 252 |
|
| 253 |
/* module_register |
| 254 |
Register collectd plugin. */ |
| 255 |
void module_register(void) { |
| 256 |
plugin_register_config("mbmon", mbmon_config, config_keys, config_keys_num); |
| 257 |
plugin_register_read("mbmon", mbmon_read); |
| 258 |
} /* void module_register */ |