nice program to store context from the cloud
[handlervirt.git] / rrdsaver.c
blob82b1cd4d9271f91aa61c6b4a577de7913769bf89
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <errno.h>
6 #include <rrd.h>
7 #include <sys/stat.h>
9 #include <avahi-client/client.h>
10 #include <avahi-client/publish.h>
11 #include <avahi-client/lookup.h>
13 #include <avahi-common/alternative.h>
14 #include <avahi-common/simple-watch.h>
15 #include <avahi-common/malloc.h>
16 #include <avahi-common/error.h>
17 #include <avahi-common/timeval.h>
19 #include <libvirt/libvirt.h>
21 static AvahiSimplePoll *simple_poll = NULL;
23 static void resolve_callback(
24 AvahiServiceResolver *r,
25 AVAHI_GCC_UNUSED AvahiIfIndex interface,
26 AVAHI_GCC_UNUSED AvahiProtocol protocol,
27 AvahiResolverEvent event,
28 const char *name,
29 const char *type,
30 const char *domain,
31 const char *host_name,
32 const AvahiAddress *address,
33 uint16_t port,
34 AvahiStringList *txt,
35 AvahiLookupResultFlags flags,
36 AVAHI_GCC_UNUSED void* userdata) {
38 assert(r);
40 /* Called whenever a service has been resolved successfully or timed out */
42 switch (event) {
43 case AVAHI_RESOLVER_FAILURE:
44 fprintf(stderr, "(Resolver) Failed to resolve service '%s' of type '%s' in domain '%s': %s\n", name, type, domain, avahi_strerror(avahi_client_errno(avahi_service_resolver_get_client(r))));
45 avahi_service_resolver_free(r);
46 break;
48 case AVAHI_RESOLVER_FOUND: {
49 char a[AVAHI_ADDRESS_STR_MAX], *t;
50 AvahiStringList *needle;
52 fprintf(stderr, "Service '%s' of type '%s' in domain '%s':\n", name, type, domain);
54 avahi_address_snprint(a, sizeof(a), address);
55 t = avahi_string_list_to_string(txt);
56 fprintf(stderr,
57 "\t%s:%u (%s)\n"
58 "\tTXT=%s\n"
59 "\tcookie is %u\n"
60 "\tis_local: %i\n"
61 "\tour_own: %i\n"
62 "\twide_area: %i\n"
63 "\tmulticast: %i\n"
64 "\tcached: %i\n",
65 host_name, port, a,
67 avahi_string_list_get_service_cookie(txt),
68 !!(flags & AVAHI_LOOKUP_RESULT_LOCAL),
69 !!(flags & AVAHI_LOOKUP_RESULT_OUR_OWN),
70 !!(flags & AVAHI_LOOKUP_RESULT_WIDE_AREA),
71 !!(flags & AVAHI_LOOKUP_RESULT_MULTICAST),
72 !!(flags & AVAHI_LOOKUP_RESULT_CACHED));
74 char *customer = strdup(name);
75 char *vm = strchr(customer, '_');
76 if (vm != NULL) {
77 char rrdfile[1024];
78 *vm = '\0';
79 vm++;
80 snprintf(rrdfile, 1023, "/mnt/netapp/users/%s/%s/%s.rrd", customer, vm, "cpuTime");
81 rrdfile[1023] = '\0';
83 if ((needle = avahi_string_list_find (txt, "cpuTime")) != NULL) {
84 char *winner;
85 avahi_string_list_get_pair (needle, NULL, &winner, NULL);
86 if (winner != NULL) {
87 struct stat statbuf;
88 char buf[128];
89 char *r_update[3] = {"update", rrdfile, buf};
91 if (stat(rrdfile, &statbuf) != 0) {
92 char *r_create[7] = {"create", rrdfile, "-s 30", "DS:cpuTime:DERIVE:1800:0:100000000000", "RRA:AVERAGE:0.5:1:1200", "RRA:AVERAGE:0.5:10:1200", "RRA:AVERAGE:0.5:120:2400" };
93 rrd_create(7, r_create);
94 if (rrd_test_error()) {
95 fprintf(stderr, "create: %s\n", rrd_get_error());
96 rrd_clear_error();
100 snprintf(buf, 127, "N:%s", winner);
101 buf[127] = '\0';
102 avahi_free(winner);
103 rrd_update(3, r_update);
105 if (rrd_test_error()) {
106 fprintf(stderr, "%s\n", rrd_get_error());
107 rrd_clear_error();
112 free(customer);
114 avahi_free(t);
120 static void browse_callback(
121 AvahiServiceBrowser *b,
122 AvahiIfIndex interface,
123 AvahiProtocol protocol,
124 AvahiBrowserEvent event,
125 const char *name,
126 const char *type,
127 const char *domain,
128 AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
129 void* userdata) {
131 AvahiClient *c = userdata;
132 assert(b);
134 /* Called whenever a new services becomes available on the LAN or is removed from the LAN */
136 switch (event) {
137 case AVAHI_BROWSER_FAILURE:
139 fprintf(stderr, "(Browser) %s\n", avahi_strerror(avahi_client_errno(avahi_service_browser_get_client(b))));
140 avahi_simple_poll_quit(simple_poll);
141 return;
143 case AVAHI_BROWSER_NEW:
144 fprintf(stderr, "(Browser) NEW: service '%s' of type '%s' in domain '%s'\n", name, type, domain);
146 /* We ignore the returned resolver object. In the callback
147 function we free it. If the server is terminated before
148 the callback function is called the server will free
149 the resolver for us. */
151 if (!(avahi_service_resolver_new(c, interface, protocol, name, type, domain, AVAHI_PROTO_UNSPEC, 0, resolve_callback, c)))
152 fprintf(stderr, "Failed to resolve service '%s': %s\n", name, avahi_strerror(avahi_client_errno(c)));
154 break;
156 case AVAHI_BROWSER_REMOVE:
157 fprintf(stderr, "(Browser) REMOVE: service '%s' of type '%s' in domain '%s'\n", name, type, domain);
158 break;
160 case AVAHI_BROWSER_ALL_FOR_NOW:
161 case AVAHI_BROWSER_CACHE_EXHAUSTED:
162 fprintf(stderr, "(Browser) %s\n", event == AVAHI_BROWSER_CACHE_EXHAUSTED ? "CACHE_EXHAUSTED" : "ALL_FOR_NOW");
163 break;
167 static void client_callback(AvahiClient *c, AvahiClientState state, AVAHI_GCC_UNUSED void * userdata) {
168 assert(c);
170 /* Called whenever the client or server state changes */
172 switch (state) {
173 case AVAHI_CLIENT_S_RUNNING:
174 /* The server has startup successfully and registered its host
175 * name on the network, so it's time to create our services */
176 break;
178 case AVAHI_CLIENT_FAILURE:
179 fprintf(stderr, "Client failure: %s\n", avahi_strerror(avahi_client_errno(c)));
180 avahi_simple_poll_quit(simple_poll);
181 break;
183 case AVAHI_CLIENT_S_COLLISION:
184 /* Let's drop our registered services. When the server is back
185 * in AVAHI_SERVER_RUNNING state we will register them
186 * again with the new host name. */
188 case AVAHI_CLIENT_S_REGISTERING:
189 /* The server records are now being established. This
190 * might be caused by a host name change. We need to wait
191 * for our own records to register until the host name is
192 * properly esatblished. */
193 break;
195 case AVAHI_CLIENT_CONNECTING:
201 int main(AVAHI_GCC_UNUSED int argc, AVAHI_GCC_UNUSED char*argv[]) {
202 AvahiClient *client = NULL;
203 AvahiServiceBrowser *sb = NULL;
204 int error;
205 int ret = 1;
207 /* Allocate main loop object */
208 if (!(simple_poll = avahi_simple_poll_new())) {
209 fprintf(stderr, "Failed to create simple poll object.\n");
210 goto fail;
213 /* Allocate a new client */
214 client = avahi_client_new(avahi_simple_poll_get(simple_poll), 0, client_callback, NULL, &error);
216 /* Check wether creating the client object succeeded */
217 if (!client) {
218 fprintf(stderr, "Failed to create client: %s\n", avahi_strerror(error));
219 goto fail;
222 /* Create the service browser */
223 if (!(sb = avahi_service_browser_new(client, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, "_domu._tcp", NULL, 0, browse_callback, client))) {
224 fprintf(stderr, "Failed to create service browser: %s\n", avahi_strerror(avahi_client_errno(client)));
225 goto fail;
229 /* Run the main loop */
230 avahi_simple_poll_loop(simple_poll);
232 ret = 0;
234 fail:
236 /* Cleanup things */
237 if (sb)
238 avahi_service_browser_free(sb);
240 if (client)
241 avahi_client_free(client);
243 if (simple_poll)
244 avahi_simple_poll_free(simple_poll);
246 return ret;