2 * collectd - src/rrdc.c
3 * Copyright (C) 2008 Florian octo Forster
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.
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.
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
19 * Florian octo Forster <octo at verplant.org>
25 static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
29 static int buffer_add_string (const char *str,
30 char **buffer_ret, size_t *buffer_size_ret)
34 str_size = strlen (str) + 1;
36 if (*buffer_size_ret < str_size)
39 memcpy (*buffer_ret, str, str_size);
40 *buffer_ret += str_size;
41 *buffer_size_ret -= str_size;
44 } /* int buffer_add_string */
46 static int buffer_add_value (const char *value,
47 char **buffer_ret, size_t *buffer_size_ret)
51 if (strncmp (value, "N:", 2) == 0)
52 snprintf (temp, sizeof (temp), "%lu:%s",
53 (unsigned long) time (NULL), value + 2);
55 strncpy (temp, value, sizeof (temp));
56 temp[sizeof (temp) - 1] = 0;
58 return (buffer_add_string (temp, buffer_ret, buffer_size_ret));
59 } /* int buffer_add_value */
61 int rrdd_connect (const char *path)
63 struct sockaddr_un sa;
67 path = RRDD_SOCK_PATH;
69 pthread_mutex_lock (&lock);
73 pthread_mutex_unlock (&lock);
77 sd = socket (PF_UNIX, SOCK_STREAM, /* protocol = */ 0);
81 pthread_mutex_unlock (&lock);
85 memset (&sa, 0, sizeof (sa));
86 sa.sun_family = AF_UNIX;
87 strncpy (sa.sun_path, path, sizeof (sa.sun_path) - 1);
89 status = connect (sd, (struct sockaddr *) &sa, sizeof (sa));
93 pthread_mutex_unlock (&lock);
97 sh = fdopen (sd, "w+");
103 pthread_mutex_unlock (&lock);
107 pthread_mutex_unlock (&lock);
110 } /* int rrdd_connect */
112 int rrdd_disconnect (void)
116 pthread_mutex_lock (&lock);
120 pthread_mutex_unlock (&lock);
124 status = fclose (sh);
131 pthread_mutex_unlock (&lock);
134 } /* int rrdd_disconnect */
136 int rrdd_update (const char *filename, int values_num,
137 const char * const *values)
145 memset (buffer, 0, sizeof (buffer));
146 buffer_ptr = &buffer[0];
147 buffer_size = sizeof (buffer) - 1;
149 buffer_add_string ("update", &buffer_ptr, &buffer_size);
150 buffer_add_string (filename, &buffer_ptr, &buffer_size);
151 for (i = 0; i < values_num; i++)
152 buffer_add_value (values[i], &buffer_ptr, &buffer_size);
154 pthread_mutex_lock (&lock);
158 pthread_mutex_unlock (&lock);
162 status = write (sd, buffer, sizeof (buffer) - buffer_size);
164 status = read (sd, buffer, sizeof (buffer));
168 pthread_mutex_unlock (&lock);
171 else if (status == 0)
173 pthread_mutex_unlock (&lock);
177 status = atoi (buffer);
179 pthread_mutex_unlock (&lock);
182 } /* int rrd_update_daemon */
185 * vim: set sw=2 sts=2 ts=8 et fdm=marker :