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
,
31 const char *host_name
,
32 const AvahiAddress
*address
,
35 AvahiLookupResultFlags flags
,
36 AVAHI_GCC_UNUSED
void* userdata
) {
40 /* Called whenever a service has been resolved successfully or timed out */
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
);
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
);
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
, '_');
80 snprintf(rrdfile
, 1023, "/mnt/netapp/users/%s/%s/%s.rrd", customer
, vm
, "cpuTime");
83 if ((needle
= avahi_string_list_find (txt
, "cpuTime")) != NULL
) {
85 avahi_string_list_get_pair (needle
, NULL
, &winner
, NULL
);
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());
100 snprintf(buf
, 127, "N:%s", winner
);
103 rrd_update(3, r_update
);
105 if (rrd_test_error()) {
106 fprintf(stderr
, "%s\n", rrd_get_error());
120 static void browse_callback(
121 AvahiServiceBrowser
*b
,
122 AvahiIfIndex interface
,
123 AvahiProtocol protocol
,
124 AvahiBrowserEvent event
,
128 AVAHI_GCC_UNUSED AvahiLookupResultFlags flags
,
131 AvahiClient
*c
= userdata
;
134 /* Called whenever a new services becomes available on the LAN or is removed from the LAN */
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
);
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
)));
156 case AVAHI_BROWSER_REMOVE
:
157 fprintf(stderr
, "(Browser) REMOVE: service '%s' of type '%s' in domain '%s'\n", name
, type
, domain
);
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");
167 static void client_callback(AvahiClient
*c
, AvahiClientState state
, AVAHI_GCC_UNUSED
void * userdata
) {
170 /* Called whenever the client or server state changes */
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 */
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
);
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. */
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
;
207 /* Allocate main loop object */
208 if (!(simple_poll
= avahi_simple_poll_new())) {
209 fprintf(stderr
, "Failed to create simple poll object.\n");
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 */
218 fprintf(stderr
, "Failed to create client: %s\n", avahi_strerror(error
));
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
)));
229 /* Run the main loop */
230 avahi_simple_poll_loop(simple_poll
);
238 avahi_service_browser_free(sb
);
241 avahi_client_free(client
);
244 avahi_simple_poll_free(simple_poll
);