Hi. Plugin "table" support retrieving status only from regular files or pipes, but can't work with unixsocket. There is a patch for add stream unixsocket support into plugin.
Service can reply not immediately to unixsocket and plugin table will blocked until it reply. This problem actual for slow network fs too. So there is another patch which register separate read callback for each table. It is not very well tested, but work for me. It will be also great for specify rows by number or by regex, but it is not very urgent for me, so no patch. -- with best regards, Alexander Golovko email: [email protected] xmpp: [email protected]
--- src/table.c.orig
+++ src/table.c
@@ -29,6 +29,11 @@
#include "configfile.h"
#include "plugin.h"
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+
#define log_err(...) ERROR ("table plugin: " __VA_ARGS__)
#define log_warn(...) WARNING ("table plugin: " __VA_ARGS__)
@@ -469,8 +474,43 @@
{
FILE *fh;
char buf[4096];
+ int sock_fd;
+ struct stat statbuf;
+ struct sockaddr_un sa;
+
+ if ( 0 != stat (tbl->file, &statbuf)) {
+ char errbuf[1024];
+ log_err ("Failed to stat file \"%s\": %s.", tbl->file,
+ sstrerror (errno, errbuf, sizeof (errbuf)));
+ return -1;
+ }
+
+ if( S_ISSOCK( statbuf.st_mode)) {
+ sock_fd = socket (PF_UNIX, SOCK_STREAM, 0);
+ if (sock_fd < 0) {
+ char errbuf[1024];
+ log_err ("Failed to open socket: %s.",
+ sstrerror (errno, errbuf, sizeof (errbuf)));
+ return -1;
+ }
+
+ memset (&sa, '\0', sizeof (sa));
+ sa.sun_family = AF_UNIX;
+ sstrncpy (sa.sun_path, tbl->file, sizeof (sa.sun_path));
+
+ if ( 0 != connect (sock_fd, (struct sockaddr *) &sa, sizeof (sa))) {
+ char errbuf[1024];
+ log_err ("Failed to connect to socket \"%s\": %s.", tbl->file,
+ sstrerror (errno, errbuf, sizeof (errbuf)));
+ close (sock_fd);
+ return -1;
+ }
+
+ fh = fdopen (sock_fd, "r");
+ } else {
+ fh = fopen (tbl->file, "r");
+ }
- fh = fopen (tbl->file, "r");
if (NULL == fh) {
char errbuf[1024];
log_err ("Failed to open file \"%s\": %s.", tbl->file,
--- src/table.c.orig
+++ src/table.c
@@ -547,28 +547,23 @@
* collectd callbacks
*/
-static int tbl_read (void)
+static int tbl_read (user_data_t *ud)
{
- int status = -1;
- size_t i;
-
- if (0 == tables_num)
- return 0;
-
- for (i = 0; i < tables_num; ++i) {
- tbl_t *tbl = tables + i;
+ tbl_t *tbl = (tbl_t *) ud->data;
- if (0 != tbl_prepare (tbl)) {
- log_err ("Failed to prepare and parse table \"%s\".", tbl->file);
- continue;
- }
-
- if (0 == tbl_read_table (tbl))
- status = 0;
+ if (0 != tbl_prepare (tbl)) {
+ log_err ("Failed to prepare and parse table \"%s\".", tbl->file);
+ tbl_finish (tbl);
+ return -1;
+ }
+ if (0 != tbl_read_table (tbl)) {
tbl_finish (tbl);
+ return -1;
}
- return status;
+
+ tbl_finish (tbl);
+ return 0;
} /* tbl_read */
static int tbl_shutdown (void)
@@ -586,7 +586,18 @@
if (0 == tables_num)
return 0;
- plugin_register_read ("table", tbl_read);
+ size_t i;
+ char cb_name[DATA_MAX_NAME_LEN];
+ user_data_t cb_data;
+
+ for (i = 0; i < tables_num; ++i) {
+ ssnprintf( cb_name, sizeof(cb_name), "table/%s", tables[i].instance);
+ cb_data.data = &tables[i];
+ cb_data.free_func = NULL;
+
+ plugin_register_complex_read( NULL, cb_name, tbl_read, NULL, &cb_data);
+ }
+
plugin_register_shutdown ("table", tbl_shutdown);
return 0;
} /* tbl_init */
signature.asc
Description: PGP signature
_______________________________________________ collectd mailing list [email protected] http://mailman.verplant.org/listinfo/collectd
