Kernelcmdline, end for rrd
[handlervirt.git] / domumdns.c
blob58a0e11a8361c917bbdf816f20fc637fae2203e8
1 #include <time.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <assert.h>
6 #include <avahi-client/client.h>
7 #include <avahi-client/publish.h>
9 #include <avahi-common/alternative.h>
10 #include <avahi-common/simple-watch.h>
11 #include <avahi-common/malloc.h>
12 #include <avahi-common/error.h>
13 #include <avahi-common/timeval.h>
15 #include <libvirt/libvirt.h>
17 #define POLL 5
19 struct list_el {
20 unsigned int domainid;
21 AvahiEntryGroup *group;
22 unsigned short keep;
25 typedef struct list_el domu;
27 domu * domus = NULL; /* list of domains */
28 unsigned int domu_count = 0;
29 static AvahiSimplePoll *simple_poll = NULL;
31 static AvahiStringList * domainProperties(virDomainPtr thisDomain) {
32 AvahiStringList *new = avahi_string_list_new(NULL);
33 virDomainInfo info;
34 virDomainInterfaceStatsStruct stats;
35 virDomainGetInfo(thisDomain, &info);
36 new = avahi_string_list_add_printf(new, "maxMem=%lu", info.maxMem);
37 new = avahi_string_list_add_printf(new, "memory=%lu", info.memory);
38 new = avahi_string_list_add_printf(new, "cpuTime=%llu", info.cpuTime);
40 /* TODO: multiple network interfaces :( */
41 char vifname[16];
42 snprintf(vifname, 14, "vif%d.0", virDomainGetID(thisDomain));
43 vifname[15] = '\0';
44 unsigned int no = 0;
46 virDomainInterfaceStats(thisDomain, vifname, &stats, sizeof(stats));
48 new = avahi_string_list_add_printf(new, "interfaces=eth%d", no);
49 new = avahi_string_list_add_printf(new, "interface_eth%d_rxbytes=%lld", no, stats.rx_bytes);
50 new = avahi_string_list_add_printf(new, "interface_eth%d_rxpackets=%lld", no, stats.rx_packets);
51 // new = avahi_string_list_add_printf(new, "interface_eth%d_rxerrs=%lld", no, stats.rx_errs);
52 // new = avahi_string_list_add_printf(new, "interface_eth%d_rxdrop=%lld", no, stats.rx_drop);
53 new = avahi_string_list_add_printf(new, "interface_eth%d_txbytes=%lld", no, stats.tx_bytes);
54 new = avahi_string_list_add_printf(new, "interface_eth%d_txpackets=%lld", no, stats.tx_packets);
55 // new = avahi_string_list_add_printf(new, "interface_eth%d_txerrs=%lld", no, stats.tx_errs);
56 // new = avahi_string_list_add_printf(new, "interface_eth%d_txdrop=%lld", no, stats.tx_drop);
58 return new;
61 static void entry_group_callback(AvahiEntryGroup *g, AvahiEntryGroupState state, AVAHI_GCC_UNUSED void *userdata) {
62 /* Called whenever the entry group state changes */
64 switch (state) {
65 case AVAHI_ENTRY_GROUP_ESTABLISHED :
66 /* The entry group has been established successfully */
67 //fprintf(stderr, "Service '%s' successfully established.\n", name);
68 break;
70 case AVAHI_ENTRY_GROUP_COLLISION : {
71 // char *n;
73 /* A service name collision with a remote service
74 * happened. Let's pick a new name */
75 //n = avahi_alternative_service_name(name);
76 //avahi_free(name);
77 //name = n;
79 // fprintf(stderr, "Service name collision, renaming service to '%s'\n", name);
81 /* And recreate the services */
82 //create_services(avahi_entry_group_get_client(g));
83 break;
86 case AVAHI_ENTRY_GROUP_FAILURE :
88 fprintf(stderr, "Entry group failure: %s\n", avahi_strerror(avahi_client_errno(avahi_entry_group_get_client(g))));
90 /* Some kind of failure happened while we were registering our services */
91 avahi_simple_poll_quit(simple_poll);
92 break;
94 case AVAHI_ENTRY_GROUP_UNCOMMITED:
95 case AVAHI_ENTRY_GROUP_REGISTERING:
100 void create_services(AvahiClient *c) {
101 virConnectPtr conn = virConnectOpenReadOnly(NULL);
102 if (conn == NULL)
103 return;
104 int maxid = virConnectNumOfDomains(conn);
105 if (maxid > 1) { /* ignore dom0 */
106 int *ids = (int *) malloc(sizeof(int) * maxid);
107 if ((maxid = virConnectListDomains(conn, &ids[0], maxid)) < 0) {
108 // error
109 } else {
110 int i;
111 unsigned int domu_count_new = (maxid - 1);
112 domu * domus_old = domus;
113 domus = (domu *) malloc(sizeof(domu) * domu_count_new);
114 const char *type = "_domu._tcp";
115 for (i = 0; i < domu_count_new; i++) {
116 int j;
117 domus[i].group = NULL;
118 domus[i].domainid = ids[i+1];
119 domus[i].keep = 0;
121 for (j = 0; j < domu_count; j++) {
122 if (ids[i+1] == domus_old[j].domainid) {
123 virDomainPtr thisDomain = virDomainLookupByID(conn, domus[i].domainid);
124 const char *name;
125 AvahiStringList *domainProps;
126 domus[i].group = domus_old[j].group;
127 domus_old[j].keep = 1;
128 domainProps = domainProperties(thisDomain);
129 name = virDomainGetName(thisDomain);
130 if (name) {
131 avahi_entry_group_update_service_txt_strlst(domus[i].group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, name, type, NULL, domainProps);
132 } else {
133 domus[i].keep = 0;
135 avahi_string_list_free(domainProps);
136 virDomainFree(thisDomain);
139 if (i > domu_count || domus[i].group == NULL) {
140 const char *name;
141 AvahiStringList *domainProps;
142 virDomainPtr thisDomain = virDomainLookupByID(conn, ids[i+1]);
143 domus[i].group = avahi_entry_group_new(c, entry_group_callback, NULL);
144 domainProps = domainProperties(thisDomain);
145 name = virDomainGetName(thisDomain);
147 if (name) {
148 avahi_entry_group_add_service_strlst(domus[i].group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, name, type, NULL, NULL, 651, domainProps);
149 avahi_entry_group_commit(domus[i].group);
150 } else {
151 domus[i].keep = 0;
154 avahi_string_list_free(domainProps);
155 virDomainFree(thisDomain);
160 for (i = 0; i < domu_count; i++) {
161 if (domus_old[i].keep == 0 && domus_old[i].group != NULL) {
162 avahi_entry_group_free(domus_old[i].group);
166 free(domus_old);
167 domu_count = domu_count_new;
169 free(ids);
171 virConnectClose(conn);
175 static void client_callback(AvahiClient *c, AvahiClientState state, AVAHI_GCC_UNUSED void * userdata) {
176 assert(c);
178 /* Called whenever the client or server state changes */
180 switch (state) {
181 case AVAHI_CLIENT_S_RUNNING:
183 /* The server has startup successfully and registered its host
184 * name on the network, so it's time to create our services */
185 create_services(c);
186 break;
188 case AVAHI_CLIENT_FAILURE:
190 fprintf(stderr, "Client failure: %s\n", avahi_strerror(avahi_client_errno(c)));
191 avahi_simple_poll_quit(simple_poll);
193 break;
195 case AVAHI_CLIENT_S_COLLISION:
197 /* Let's drop our registered services. When the server is back
198 * in AVAHI_SERVER_RUNNING state we will register them
199 * again with the new host name. */
201 case AVAHI_CLIENT_S_REGISTERING:
203 /* The server records are now being established. This
204 * might be caused by a host name change. We need to wait
205 * for our own records to register until the host name is
206 * properly esatblished. */
208 /* if (group)
209 avahi_entry_group_reset(group);*/
211 break;
213 case AVAHI_CLIENT_CONNECTING:
220 static void modify_callback(AvahiTimeout *e, void *userdata) {
221 struct timeval tv;
222 AvahiClient *client = userdata;
224 // fprintf(stderr, "Doing some weird modification\n");
226 /* If the server is currently running, we need to remove our
227 * service and create it anew */
228 if (avahi_client_get_state(client) == AVAHI_CLIENT_S_RUNNING) {
229 /* And create them again with the new name */
230 create_services(client);
232 avahi_simple_poll_get(simple_poll)->timeout_update(e, avahi_elapse_time(&tv, 1000*POLL, 0));
235 int main(AVAHI_GCC_UNUSED int argc, AVAHI_GCC_UNUSED char*argv[]) {
236 AvahiClient *client = NULL;
237 int error;
238 int ret = 1;
239 struct timeval tv;
240 /* Allocate main loop object */
241 if (!(simple_poll = avahi_simple_poll_new())) {
242 fprintf(stderr, "Failed to create simple poll object.\n");
243 goto fail;
246 /* Allocate a new client */
247 client = avahi_client_new(avahi_simple_poll_get(simple_poll), 0, client_callback, NULL, &error);
249 /* Check wether creating the client object succeeded */
250 if (!client) {
251 fprintf(stderr, "Failed to create client: %s\n", avahi_strerror(error));
252 goto fail;
255 /* After 10s do some weird modification to the service */
256 avahi_simple_poll_get(simple_poll)->timeout_new(
257 avahi_simple_poll_get(simple_poll),
258 avahi_elapse_time(&tv, 1000*POLL, 0),
259 modify_callback,
260 client);
262 /* Run the main loop */
263 avahi_simple_poll_loop(simple_poll);
265 ret = 0;
267 fail:
269 /* Cleanup things */
271 if (client)
272 avahi_client_free(client);
274 if (simple_poll)
275 avahi_simple_poll_free(simple_poll);
277 return ret;