]> git.proxmox.com Git - pve-cluster.git/blame - data/src/status.c
add sdn vnets/zones/controllers.cfg
[pve-cluster.git] / data / src / status.c
CommitLineData
fe000966
DM
1/*
2 Copyright (C) 2010 Proxmox Server Solutions GmbH
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU Affero General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Affero General Public License for more details.
13
14 You should have received a copy of the GNU Affero General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17 Author: Dietmar Maurer <dietmar@proxmox.com>
18
19*/
20
21#define G_LOG_DOMAIN "status"
22
23#ifdef HAVE_CONFIG_H
24#include <config.h>
25#endif /* HAVE_CONFIG_H */
26
27#include <stdio.h>
28#include <stdint.h>
29#include <string.h>
30#include <errno.h>
31#include <glib.h>
32#include <sys/syslog.h>
33#include <rrd.h>
34#include <rrd_client.h>
35#include <time.h>
cf1b19d9 36#include <ctype.h>
fe000966
DM
37
38#include "cfs-utils.h"
39#include "status.h"
cf1b19d9 40#include "memdb.h"
fe000966
DM
41#include "logger.h"
42
43#define KVSTORE_CPG_GROUP_NAME "pve_kvstore_v1"
44
45typedef enum {
46 KVSTORE_MESSAGE_UPDATE = 1,
47 KVSTORE_MESSAGE_UPDATE_COMPLETE = 2,
48 KVSTORE_MESSAGE_LOG = 3,
49} kvstore_message_t;
50
51static uint32_t vminfo_version_counter;
52
53typedef struct {
54 uint32_t vmid;
55 char *nodename;
56 int vmtype;
57 uint32_t version;
58} vminfo_t;
59
60typedef struct {
61 char *key;
62 gpointer data;
63 size_t len;
64 uint32_t version;
65} kventry_t;
66
67typedef struct {
68 char *key;
69 gpointer data;
70 size_t len;
71 uint32_t time;
72} rrdentry_t;
73
74typedef struct {
75 char *path;
76 uint32_t version;
77} memdb_change_t;
78
79static memdb_change_t memdb_change_array[] = {
2113d031
DM
80 { .path = "corosync.conf" },
81 { .path = "corosync.conf.new" },
fe000966
DM
82 { .path = "storage.cfg" },
83 { .path = "user.cfg" },
84 { .path = "domains.cfg" },
85 { .path = "priv/shadow.cfg" },
8a9456dc 86 { .path = "priv/tfa.cfg" },
fe000966 87 { .path = "datacenter.cfg" },
e1735a61 88 { .path = "vzdump.cron" },
5a5417e6
DM
89 { .path = "ha/crm_commands" },
90 { .path = "ha/manager_status" },
91 { .path = "ha/resources.cfg" },
92 { .path = "ha/groups.cfg" },
e9af3eb7 93 { .path = "ha/fence.cfg" },
9d4f69ff 94 { .path = "status.cfg" },
f6de131a 95 { .path = "replication.cfg" },
22e2ed76 96 { .path = "ceph.conf" },
1bb7107a
AD
97 { .path = "sdn/vnets.cfg" },
98 { .path = "sdn/vnets.cfg.new" },
99 { .path = "sdn/zones.cfg" },
100 { .path = "sdn/zones.cfg.new" },
101 { .path = "sdn/controllers.cfg" },
102 { .path = "sdn/controllers.cfg.new" },
fe000966
DM
103};
104
89fde9ac 105static GMutex mutex;
fe000966
DM
106
107typedef struct {
108 time_t start_time;
109
110 uint32_t quorate;
111
112 cfs_clinfo_t *clinfo;
113 uint32_t clinfo_version;
114
115 GHashTable *vmlist;
116 uint32_t vmlist_version;
117
118 dfsm_t *kvstore;
119 GHashTable *kvhash;
120 GHashTable *rrdhash;
121 GHashTable *iphash;
122
123 GHashTable *memdb_changes;
124
125 clusterlog_t *clusterlog;
126} cfs_status_t;
127
128static cfs_status_t cfs_status;
129
130struct cfs_clnode {
131 char *name;
132 uint32_t nodeid;
133 uint32_t votes;
134 gboolean online;
135 GHashTable *kvhash;
136};
137
138struct cfs_clinfo {
139 char *cluster_name;
140 uint32_t cman_version;
141
142 GHashTable *nodes_byid;
143 GHashTable *nodes_byname;
144};
145
146static guint
147g_int32_hash (gconstpointer v)
148{
149 return *(const uint32_t *) v;
150}
151
152static gboolean
153g_int32_equal (gconstpointer v1,
154 gconstpointer v2)
155{
156 return *((const uint32_t*) v1) == *((const uint32_t*) v2);
157}
158
159static void vminfo_free(vminfo_t *vminfo)
160{
161 g_return_if_fail(vminfo != NULL);
162
163 if (vminfo->nodename)
164 g_free(vminfo->nodename);
165
166
167 g_free(vminfo);
168}
169
99abbd31
TL
170static const char *vminfo_type_to_string(vminfo_t *vminfo)
171{
172 if (vminfo->vmtype == VMTYPE_QEMU) {
173 return "qemu";
174 } else if (vminfo->vmtype == VMTYPE_OPENVZ) {
175 return "openvz";
176 } else if (vminfo->vmtype == VMTYPE_LXC) {
177 return "lxc";
178 } else {
179 return "unknown";
180 }
c26ca647
TL
181}
182
cf1b19d9
TL
183static const char *vminfo_type_to_path_type(vminfo_t *vminfo)
184{
185 if (vminfo->vmtype == VMTYPE_QEMU) {
186 return "qemu-server"; // special case..
187 } else {
188 return vminfo_type_to_string(vminfo);
189 }
190}
191
192int vminfo_to_path(vminfo_t *vminfo, GString *path)
193{
194 g_return_val_if_fail(vminfo != NULL, -1);
195 g_return_val_if_fail(path != NULL, -1);
196
197 if (!vminfo->nodename)
198 return 0;
199
200 const char *type = vminfo_type_to_path_type(vminfo);
201 g_string_printf(path, "/nodes/%s/%s/%u.conf", vminfo->nodename, type, vminfo->vmid);
202
203 return 1;
204}
205
fe000966
DM
206void cfs_clnode_destroy(
207 cfs_clnode_t *clnode)
208{
209 g_return_if_fail(clnode != NULL);
210
211 if (clnode->kvhash)
212 g_hash_table_destroy(clnode->kvhash);
213
214 if (clnode->name)
215 g_free(clnode->name);
216
217 g_free(clnode);
218}
219
220cfs_clnode_t *cfs_clnode_new(
221 const char *name,
222 uint32_t nodeid,
223 uint32_t votes)
224{
225 g_return_val_if_fail(name != NULL, NULL);
226
227 cfs_clnode_t *clnode = g_new0(cfs_clnode_t, 1);
228 if (!clnode)
229 return NULL;
230
231 clnode->name = g_strdup(name);
232 clnode->nodeid = nodeid;
233 clnode->votes = votes;
234
235 return clnode;
236}
237
238gboolean cfs_clinfo_destroy(
239 cfs_clinfo_t *clinfo)
240{
241 g_return_val_if_fail(clinfo != NULL, FALSE);
242
243 if (clinfo->cluster_name)
244 g_free(clinfo->cluster_name);
245
246 if (clinfo->nodes_byname)
247 g_hash_table_destroy(clinfo->nodes_byname);
248
249 if (clinfo->nodes_byid)
250 g_hash_table_destroy(clinfo->nodes_byid);
251
252 g_free(clinfo);
253
254 return TRUE;
255}
256
257cfs_clinfo_t *cfs_clinfo_new(
258 const char *cluster_name,
259 uint32_t cman_version)
260{
261 g_return_val_if_fail(cluster_name != NULL, NULL);
262
263 cfs_clinfo_t *clinfo = g_new0(cfs_clinfo_t, 1);
264 if (!clinfo)
265 return NULL;
266
267 clinfo->cluster_name = g_strdup(cluster_name);
268 clinfo->cman_version = cman_version;
269
270 if (!(clinfo->nodes_byid = g_hash_table_new_full(
271 g_int32_hash, g_int32_equal, NULL,
272 (GDestroyNotify)cfs_clnode_destroy)))
273 goto fail;
274
275 if (!(clinfo->nodes_byname = g_hash_table_new(g_str_hash, g_str_equal)))
276 goto fail;
277
278 return clinfo;
279
280fail:
281 cfs_clinfo_destroy(clinfo);
282
283 return NULL;
284}
285
286gboolean cfs_clinfo_add_node(
287 cfs_clinfo_t *clinfo,
288 cfs_clnode_t *clnode)
289{
290 g_return_val_if_fail(clinfo != NULL, FALSE);
291 g_return_val_if_fail(clnode != NULL, FALSE);
292
293 g_hash_table_replace(clinfo->nodes_byid, &clnode->nodeid, clnode);
294 g_hash_table_replace(clinfo->nodes_byname, clnode->name, clnode);
295
296 return TRUE;
297}
298
299int
300cfs_create_memberlist_msg(
301 GString *str)
302{
303 g_return_val_if_fail(str != NULL, -EINVAL);
304
89fde9ac 305 g_mutex_lock (&mutex);
fe000966
DM
306
307 g_string_append_printf(str,"{\n");
308
309 guint nodecount = 0;
310
311 cfs_clinfo_t *clinfo = cfs_status.clinfo;
312
313 if (clinfo && clinfo->nodes_byid)
314 nodecount = g_hash_table_size(clinfo->nodes_byid);
315
316 if (nodecount) {
317 g_string_append_printf(str, "\"nodename\": \"%s\",\n", cfs.nodename);
318 g_string_append_printf(str, "\"version\": %u,\n", cfs_status.clinfo_version);
319
320 g_string_append_printf(str, "\"cluster\": { ");
321 g_string_append_printf(str, "\"name\": \"%s\", \"version\": %d, "
322 "\"nodes\": %d, \"quorate\": %d ",
323 clinfo->cluster_name, clinfo->cman_version,
324 nodecount, cfs_status.quorate);
325
326 g_string_append_printf(str,"},\n");
327 g_string_append_printf(str,"\"nodelist\": {\n");
328
329 GHashTable *ht = clinfo->nodes_byid;
330 GHashTableIter iter;
331 gpointer key, value;
332
333 g_hash_table_iter_init (&iter, ht);
334
335 int i = 0;
336 while (g_hash_table_iter_next (&iter, &key, &value)) {
337 cfs_clnode_t *node = (cfs_clnode_t *)value;
338 if (i) g_string_append_printf(str, ",\n");
339 i++;
340
341 g_string_append_printf(str, " \"%s\": { \"id\": %d, \"online\": %d",
342 node->name, node->nodeid, node->online);
343
344
345 char *ip = (char *)g_hash_table_lookup(cfs_status.iphash, node->name);
346 if (ip) {
347 g_string_append_printf(str, ", \"ip\": \"%s\"", ip);
348 }
349
350 g_string_append_printf(str, "}");
351
352 }
353 g_string_append_printf(str,"\n }\n");
354 } else {
355 g_string_append_printf(str, "\"nodename\": \"%s\",\n", cfs.nodename);
356 g_string_append_printf(str, "\"version\": %u\n", cfs_status.clinfo_version);
357 }
358
359 g_string_append_printf(str,"}\n");
360
89fde9ac 361 g_mutex_unlock (&mutex);
fe000966
DM
362
363 return 0;
364}
365
366static void
367kventry_free(kventry_t *entry)
368{
369 g_return_if_fail(entry != NULL);
370
371 g_free(entry->key);
372 g_free(entry->data);
373 g_free(entry);
374}
375
376static GHashTable *
377kventry_hash_new(void)
378{
379 return g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
380 (GDestroyNotify)kventry_free);
381}
382
383static void
384rrdentry_free(rrdentry_t *entry)
385{
386 g_return_if_fail(entry != NULL);
387
388 g_free(entry->key);
389 g_free(entry->data);
390 g_free(entry);
391}
392
393static GHashTable *
394rrdentry_hash_new(void)
395{
396 return g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
397 (GDestroyNotify)rrdentry_free);
398}
399
400void
401cfs_cluster_log_dump(GString *str, const char *user, guint max_entries)
402{
403 clusterlog_dump(cfs_status.clusterlog, str, user, max_entries);
404}
405
406void
407cfs_cluster_log(clog_entry_t *entry)
408{
409 g_return_if_fail(entry != NULL);
410
411 clusterlog_insert(cfs_status.clusterlog, entry);
412
413 if (cfs_status.kvstore) {
414 struct iovec iov[1];
415 iov[0].iov_base = (char *)entry;
416 iov[0].iov_len = clog_entry_size(entry);
417
af2e9dd4
DM
418 if (dfsm_is_initialized(cfs_status.kvstore))
419 dfsm_send_message(cfs_status.kvstore, KVSTORE_MESSAGE_LOG, iov, 1);
fe000966
DM
420 }
421}
422
423void cfs_status_init(void)
424{
89fde9ac 425 g_mutex_lock (&mutex);
fe000966
DM
426
427 cfs_status.start_time = time(NULL);
428
429 cfs_status.vmlist = vmlist_hash_new();
430
431 cfs_status.kvhash = kventry_hash_new();
432
433 cfs_status.rrdhash = rrdentry_hash_new();
434
435 cfs_status.iphash = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
436
437 cfs_status.memdb_changes = g_hash_table_new(g_str_hash, g_str_equal);
438
439 for (int i = 0; i < G_N_ELEMENTS(memdb_change_array); i++) {
440 g_hash_table_replace(cfs_status.memdb_changes,
441 memdb_change_array[i].path,
442 &memdb_change_array[i]);
443 }
444
445 cfs_status.clusterlog = clusterlog_new();
446
447 // fixme:
448 clusterlog_add(cfs_status.clusterlog, "root", "cluster", getpid(),
449 LOG_INFO, "starting cluster log");
450
89fde9ac 451 g_mutex_unlock (&mutex);
fe000966
DM
452}
453
454void cfs_status_cleanup(void)
455{
89fde9ac 456 g_mutex_lock (&mutex);
fe000966
DM
457
458 cfs_status.clinfo_version++;
459
460 if (cfs_status.clinfo) {
461 cfs_clinfo_destroy(cfs_status.clinfo);
462 cfs_status.clinfo = NULL;
463 }
464
465 if (cfs_status.vmlist) {
466 g_hash_table_destroy(cfs_status.vmlist);
467 cfs_status.vmlist = NULL;
468 }
469
470 if (cfs_status.kvhash) {
471 g_hash_table_destroy(cfs_status.kvhash);
472 cfs_status.kvhash = NULL;
473 }
474
475 if (cfs_status.rrdhash) {
476 g_hash_table_destroy(cfs_status.rrdhash);
477 cfs_status.rrdhash = NULL;
478 }
479
480 if (cfs_status.iphash) {
481 g_hash_table_destroy(cfs_status.iphash);
482 cfs_status.iphash = NULL;
483 }
484
485 if (cfs_status.clusterlog)
486 clusterlog_destroy(cfs_status.clusterlog);
487
89fde9ac 488 g_mutex_unlock (&mutex);
fe000966
DM
489}
490
491void cfs_status_set_clinfo(
492 cfs_clinfo_t *clinfo)
493{
494 g_return_if_fail(clinfo != NULL);
495
89fde9ac 496 g_mutex_lock (&mutex);
fe000966
DM
497
498 cfs_status.clinfo_version++;
499
500 cfs_clinfo_t *old = cfs_status.clinfo;
501
502 cfs_status.clinfo = clinfo;
503
504 cfs_message("update cluster info (cluster name %s, version = %d)",
505 clinfo->cluster_name, clinfo->cman_version);
506
507
508 if (old && old->nodes_byid && clinfo->nodes_byid) {
509 /* copy kvstore */
510 GHashTable *ht = clinfo->nodes_byid;
511 GHashTableIter iter;
512 gpointer key, value;
513
514 g_hash_table_iter_init (&iter, ht);
515
516 while (g_hash_table_iter_next (&iter, &key, &value)) {
517 cfs_clnode_t *node = (cfs_clnode_t *)value;
518 cfs_clnode_t *oldnode;
469d3acb 519 if ((oldnode = g_hash_table_lookup(old->nodes_byid, key))) {
fe000966
DM
520 node->online = oldnode->online;
521 node->kvhash = oldnode->kvhash;
522 oldnode->kvhash = NULL;
523 }
524 }
525
526 }
527
528 if (old)
529 cfs_clinfo_destroy(old);
530
531
89fde9ac 532 g_mutex_unlock (&mutex);
fe000966
DM
533}
534
535static void
536dump_kvstore_versions(
537 GString *str,
538 GHashTable *kvhash,
539 const char *nodename)
540{
541 g_return_if_fail(kvhash != NULL);
542 g_return_if_fail(str != NULL);
543 g_return_if_fail(nodename != NULL);
544
545 GHashTable *ht = kvhash;
546 GHashTableIter iter;
547 gpointer key, value;
548
549 g_string_append_printf(str, "\"%s\": {\n", nodename);
550
551 g_hash_table_iter_init (&iter, ht);
552
553 int i = 0;
554 while (g_hash_table_iter_next (&iter, &key, &value)) {
555 kventry_t *entry = (kventry_t *)value;
556 if (i) g_string_append_printf(str, ",\n");
557 i++;
558 g_string_append_printf(str,"\"%s\": %u", entry->key, entry->version);
559 }
560
561 g_string_append_printf(str, "}\n");
562}
563
564int
565cfs_create_version_msg(GString *str)
566{
567 g_return_val_if_fail(str != NULL, -EINVAL);
568
89fde9ac 569 g_mutex_lock (&mutex);
fe000966
DM
570
571 g_string_append_printf(str,"{\n");
572
573 g_string_append_printf(str, "\"starttime\": %lu,\n", (unsigned long)cfs_status.start_time);
574
575 g_string_append_printf(str, "\"clinfo\": %u,\n", cfs_status.clinfo_version);
576
577 g_string_append_printf(str, "\"vmlist\": %u,\n", cfs_status.vmlist_version);
578
579 for (int i = 0; i < G_N_ELEMENTS(memdb_change_array); i++) {
580 g_string_append_printf(str, "\"%s\": %u,\n",
581 memdb_change_array[i].path,
582 memdb_change_array[i].version);
583 }
584
585 g_string_append_printf(str, "\"kvstore\": {\n");
586
587 dump_kvstore_versions(str, cfs_status.kvhash, cfs.nodename);
588
589 cfs_clinfo_t *clinfo = cfs_status.clinfo;
590
591 if (clinfo && clinfo->nodes_byid) {
592 GHashTable *ht = clinfo->nodes_byid;
593 GHashTableIter iter;
594 gpointer key, value;
595
596 g_hash_table_iter_init (&iter, ht);
597
598 while (g_hash_table_iter_next (&iter, &key, &value)) {
599 cfs_clnode_t *node = (cfs_clnode_t *)value;
600 if (!node->kvhash)
601 continue;
602 g_string_append_printf(str, ",\n");
603 dump_kvstore_versions(str, node->kvhash, node->name);
604 }
605 }
606
607 g_string_append_printf(str,"}\n");
608
609 g_string_append_printf(str,"}\n");
610
89fde9ac 611 g_mutex_unlock (&mutex);
fe000966
DM
612
613 return 0;
614}
615
616GHashTable *
617vmlist_hash_new(void)
618{
619 return g_hash_table_new_full(g_int_hash, g_int_equal, NULL,
620 (GDestroyNotify)vminfo_free);
621}
622
623gboolean
624vmlist_hash_insert_vm(
625 GHashTable *vmlist,
626 int vmtype,
627 guint32 vmid,
628 const char *nodename,
629 gboolean replace)
630{
631 g_return_val_if_fail(vmlist != NULL, FALSE);
632 g_return_val_if_fail(nodename != NULL, FALSE);
633 g_return_val_if_fail(vmid != 0, FALSE);
7f66b436
DM
634 g_return_val_if_fail(vmtype == VMTYPE_QEMU || vmtype == VMTYPE_OPENVZ ||
635 vmtype == VMTYPE_LXC, FALSE);
fe000966
DM
636
637 if (!replace && g_hash_table_lookup(vmlist, &vmid)) {
638 cfs_critical("detected duplicate VMID %d", vmid);
639 return FALSE;
640 }
641
642 vminfo_t *vminfo = g_new0(vminfo_t, 1);
643
644 vminfo->vmid = vmid;
645 vminfo->vmtype = vmtype;
646 vminfo->nodename = g_strdup(nodename);
647
648 vminfo->version = ++vminfo_version_counter;
649
650 g_hash_table_replace(vmlist, &vminfo->vmid, vminfo);
651
652 return TRUE;
653}
654
655void
656vmlist_register_vm(
657 int vmtype,
658 guint32 vmid,
659 const char *nodename)
660{
661 g_return_if_fail(cfs_status.vmlist != NULL);
662 g_return_if_fail(nodename != NULL);
663 g_return_if_fail(vmid != 0);
7f66b436
DM
664 g_return_if_fail(vmtype == VMTYPE_QEMU || vmtype == VMTYPE_OPENVZ ||
665 vmtype == VMTYPE_LXC);
fe000966
DM
666
667 cfs_debug("vmlist_register_vm: %s/%u %d", nodename, vmid, vmtype);
668
89fde9ac 669 g_mutex_lock (&mutex);
fe000966
DM
670
671 cfs_status.vmlist_version++;
672
673 vmlist_hash_insert_vm(cfs_status.vmlist, vmtype, vmid, nodename, TRUE);
674
89fde9ac 675 g_mutex_unlock (&mutex);
fe000966
DM
676}
677
678gboolean
679vmlist_different_vm_exists(
680 int vmtype,
681 guint32 vmid,
682 const char *nodename)
683{
684 g_return_val_if_fail(cfs_status.vmlist != NULL, FALSE);
685 g_return_val_if_fail(vmid != 0, FALSE);
686
687 gboolean res = FALSE;
688
89fde9ac 689 g_mutex_lock (&mutex);
fe000966
DM
690
691 vminfo_t *vminfo;
692 if ((vminfo = (vminfo_t *)g_hash_table_lookup(cfs_status.vmlist, &vmid))) {
693 if (!(vminfo->vmtype == vmtype && strcmp(vminfo->nodename, nodename) == 0))
694 res = TRUE;
695 }
89fde9ac 696 g_mutex_unlock (&mutex);
fe000966
DM
697
698 return res;
699}
700
701gboolean
702vmlist_vm_exists(
703 guint32 vmid)
704{
705 g_return_val_if_fail(cfs_status.vmlist != NULL, FALSE);
706 g_return_val_if_fail(vmid != 0, FALSE);
707
89fde9ac 708 g_mutex_lock (&mutex);
fe000966
DM
709
710 gpointer res = g_hash_table_lookup(cfs_status.vmlist, &vmid);
711
89fde9ac 712 g_mutex_unlock (&mutex);
fe000966
DM
713
714 return res != NULL;
715}
716
717void
718vmlist_delete_vm(
719 guint32 vmid)
720{
721 g_return_if_fail(cfs_status.vmlist != NULL);
722 g_return_if_fail(vmid != 0);
723
89fde9ac 724 g_mutex_lock (&mutex);
fe000966
DM
725
726 cfs_status.vmlist_version++;
727
728 g_hash_table_remove(cfs_status.vmlist, &vmid);
729
89fde9ac 730 g_mutex_unlock (&mutex);
fe000966
DM
731}
732
733void cfs_status_set_vmlist(
734 GHashTable *vmlist)
735{
736 g_return_if_fail(vmlist != NULL);
737
89fde9ac 738 g_mutex_lock (&mutex);
fe000966
DM
739
740 cfs_status.vmlist_version++;
741
742 if (cfs_status.vmlist)
743 g_hash_table_destroy(cfs_status.vmlist);
744
745 cfs_status.vmlist = vmlist;
746
89fde9ac 747 g_mutex_unlock (&mutex);
fe000966
DM
748}
749
750int
751cfs_create_vmlist_msg(GString *str)
752{
753 g_return_val_if_fail(cfs_status.vmlist != NULL, -EINVAL);
754 g_return_val_if_fail(str != NULL, -EINVAL);
755
89fde9ac 756 g_mutex_lock (&mutex);
fe000966
DM
757
758 g_string_append_printf(str,"{\n");
759
760 GHashTable *ht = cfs_status.vmlist;
761
762 guint count = g_hash_table_size(ht);
763
764 if (!count) {
765 g_string_append_printf(str,"\"version\": %u\n", cfs_status.vmlist_version);
766 } else {
767 g_string_append_printf(str,"\"version\": %u,\n", cfs_status.vmlist_version);
768
769 g_string_append_printf(str,"\"ids\": {\n");
770
771 GHashTableIter iter;
772 gpointer key, value;
773
774 g_hash_table_iter_init (&iter, ht);
775
776 int first = 1;
777 while (g_hash_table_iter_next (&iter, &key, &value)) {
778 vminfo_t *vminfo = (vminfo_t *)value;
c26ca647 779 const char *type = vminfo_type_to_string(vminfo);
fe000966
DM
780
781 if (!first)
782 g_string_append_printf(str, ",\n");
783 first = 0;
784
785 g_string_append_printf(str,"\"%u\": { \"node\": \"%s\", \"type\": \"%s\", \"version\": %u }",
786 vminfo->vmid, vminfo->nodename, type, vminfo->version);
787 }
788
789 g_string_append_printf(str,"}\n");
790 }
791 g_string_append_printf(str,"\n}\n");
792
89fde9ac 793 g_mutex_unlock (&mutex);
fe000966
DM
794
795 return 0;
796}
797
cf1b19d9
TL
798// checks the conf for a line starting with '$prop:' and returns the value
799// afterwards, whitout initial whitespace(s), we only deal with the format
800// restricion imposed by our perl VM config parser, main reference is
801// PVE::QemuServer::parse_vm_config this allows to be very fast and still
802// relatively simple
803// main restrictions used for our advantage is the properties match reges:
804// ($line =~ m/^([a-z][a-z_]*\d*):\s*(.+?)\s*$/) from parse_vm_config
805// currently we only look at the current configuration in place, i.e., *no*
806// snapshort and *no* pending changes
807static char *
5c86492f 808_get_property_value(char *conf, int conf_size, const char *prop, int prop_len)
cf1b19d9 809{
217a9463 810 const char *const conf_end = conf + conf_size;
5c86492f 811 char *line = conf;
217a9463 812 size_t remaining_size;
5c86492f
TL
813
814 char *next_newline = memchr(conf, '\n', conf_size);
815 if (next_newline == NULL) {
816 return NULL; // valid property lines end with \n, but none in the config
817 }
818 *next_newline = '\0';
cf1b19d9 819
cf1b19d9
TL
820 while (line != NULL) {
821 if (!line[0]) goto next;
822
5c86492f 823 // snapshot or pending section start, but nothing found yet -> not found
cf1b19d9
TL
824 if (line[0] == '[') return NULL;
825 // properties start with /^[a-z]/, so continue early if not
826 if (line[0] < 'a' || line[0] > 'z') goto next;
827
828 int line_len = strlen(line);
829 if (line_len <= prop_len + 1) goto next;
830
831 if (line[prop_len] == ':' && memcmp(line, prop, prop_len) == 0) { // found
832 char *v_start = &line[prop_len + 1];
833
834 // drop initial value whitespaces here already
835 while (*v_start && isspace(*v_start)) v_start++;
836
837 if (!*v_start) return NULL;
838
839 char *v_end = &line[line_len - 1];
840 while (v_end > v_start && isspace(*v_end)) v_end--;
841 v_end[1] = '\0';
842
843 return v_start;
844 }
845next:
217a9463
WB
846 line = next_newline + 1;
847 remaining_size = conf_end - line;
848 if (remaining_size <= prop_len) {
5c86492f
TL
849 return NULL;
850 }
5c86492f
TL
851 next_newline = memchr(line, '\n', remaining_size);
852 if (next_newline == NULL) {
853 return NULL; // valid property lines end with \n, but none in the config
854 }
855 *next_newline = '\0';
cf1b19d9
TL
856 }
857
858 return NULL; // not found
859}
860
cb7cea80
TL
861static void
862_g_str_append_kv_jsonescaped(GString *str, const char *k, const char *v)
863{
864 g_string_append_printf(str, "\"%s\": \"", k);
865
866 for (; *v; v++) {
867 if (*v == '\\' || *v == '"') {
868 g_string_append_c(str, '\\');
869 }
870 g_string_append_c(str, *v);
871 }
872
873 g_string_append_c(str, '"');
874}
875
cf1b19d9
TL
876int
877cfs_create_guest_conf_property_msg(GString *str, memdb_t *memdb, const char *prop, uint32_t vmid)
878{
879 g_return_val_if_fail(cfs_status.vmlist != NULL, -EINVAL);
880 g_return_val_if_fail(str != NULL, -EINVAL);
881
882 int prop_len = strlen(prop);
883 int res = 0;
884 GString *path = NULL;
885
886 g_mutex_lock (&mutex);
887
888 g_string_printf(str,"{\n");
889
890 GHashTable *ht = cfs_status.vmlist;
891 gpointer tmp = NULL;
892 if (!g_hash_table_size(ht)) {
893 goto ret;
894 }
895
5b17ee16 896 path = g_string_sized_new(256);
cf1b19d9
TL
897 if (vmid >= 100) {
898 vminfo_t *vminfo = (vminfo_t *) g_hash_table_lookup(cfs_status.vmlist, &vmid);
899 if (vminfo == NULL) goto enoent;
900
901 if (!vminfo_to_path(vminfo, path)) goto err;
902
903 int size = memdb_read(memdb, path->str, &tmp);
904 if (tmp == NULL) goto err;
905 if (size <= prop_len) goto ret;
906
5c86492f 907 char *val = _get_property_value(tmp, size, prop, prop_len);
cf1b19d9
TL
908 if (val == NULL) goto ret;
909
cb7cea80
TL
910 g_string_append_printf(str, "\"%u\":{", vmid);
911 _g_str_append_kv_jsonescaped(str, prop, val);
912 g_string_append_c(str, '}');
cf1b19d9
TL
913
914 } else {
915 GHashTableIter iter;
916 g_hash_table_iter_init (&iter, ht);
917
918 gpointer key, value;
919 int first = 1;
920 while (g_hash_table_iter_next (&iter, &key, &value)) {
921 vminfo_t *vminfo = (vminfo_t *)value;
922
923 if (!vminfo_to_path(vminfo, path)) goto err;
924
925 g_free(tmp); // no-op if already null
926 tmp = NULL;
927 int size = memdb_read(memdb, path->str, &tmp);
928 if (tmp == NULL || size <= prop_len) continue;
929
5c86492f 930 char *val = _get_property_value(tmp, size, prop, prop_len);
cf1b19d9
TL
931 if (val == NULL) continue;
932
933 if (!first) g_string_append_printf(str, ",\n");
934 else first = 0;
935
cb7cea80
TL
936 g_string_append_printf(str, "\"%u\":{", vminfo->vmid);
937 _g_str_append_kv_jsonescaped(str, prop, val);
938 g_string_append_c(str, '}');
cf1b19d9
TL
939 }
940 }
941ret:
942 g_free(tmp);
3f1c7eb1
TL
943 if (path != NULL) {
944 g_string_free(path, TRUE);
945 }
cf1b19d9
TL
946 g_string_append_printf(str,"\n}\n");
947 g_mutex_unlock (&mutex);
948
949 return res;
950err:
951 res = -EIO;
952 goto ret;
953enoent:
954 res = -ENOENT;
955 goto ret;
956}
957
fe000966
DM
958void
959record_memdb_change(const char *path)
960{
961 g_return_if_fail(cfs_status.memdb_changes != 0);
962
963 memdb_change_t *ce;
964
965 if ((ce = (memdb_change_t *)g_hash_table_lookup(cfs_status.memdb_changes, path))) {
966 ce->version++;
967 }
968}
969
970void
971record_memdb_reload(void)
972{
973 for (int i = 0; i < G_N_ELEMENTS(memdb_change_array); i++) {
974 memdb_change_array[i].version++;
975 }
976}
977
978static gboolean
979kventry_hash_set(
980 GHashTable *kvhash,
981 const char *key,
982 gconstpointer data,
983 size_t len)
984{
985 g_return_val_if_fail(kvhash != NULL, FALSE);
986 g_return_val_if_fail(key != NULL, FALSE);
987 g_return_val_if_fail(data != NULL, FALSE);
988
989 kventry_t *entry;
71cc17bc
DC
990 if (!len) {
991 g_hash_table_remove(kvhash, key);
992 } else if ((entry = (kventry_t *)g_hash_table_lookup(kvhash, key))) {
fe000966
DM
993 g_free(entry->data);
994 entry->data = g_memdup(data, len);
995 entry->len = len;
996 entry->version++;
997 } else {
998 kventry_t *entry = g_new0(kventry_t, 1);
999
1000 entry->key = g_strdup(key);
1001 entry->data = g_memdup(data, len);
1002 entry->len = len;
1003
1004 g_hash_table_replace(kvhash, entry->key, entry);
1005 }
1006
1007 return TRUE;
1008}
1009
1010static const char *rrd_def_node[] = {
1011 "DS:loadavg:GAUGE:120:0:U",
1012 "DS:maxcpu:GAUGE:120:0:U",
1013 "DS:cpu:GAUGE:120:0:U",
1014 "DS:iowait:GAUGE:120:0:U",
1015 "DS:memtotal:GAUGE:120:0:U",
1016 "DS:memused:GAUGE:120:0:U",
1017 "DS:swaptotal:GAUGE:120:0:U",
1018 "DS:swapused:GAUGE:120:0:U",
1019 "DS:roottotal:GAUGE:120:0:U",
1020 "DS:rootused:GAUGE:120:0:U",
764296f1
DM
1021 "DS:netin:DERIVE:120:0:U",
1022 "DS:netout:DERIVE:120:0:U",
fe000966
DM
1023
1024 "RRA:AVERAGE:0.5:1:70", // 1 min avg - one hour
1025 "RRA:AVERAGE:0.5:30:70", // 30 min avg - one day
1026 "RRA:AVERAGE:0.5:180:70", // 3 hour avg - one week
1027 "RRA:AVERAGE:0.5:720:70", // 12 hour avg - one month
1028 "RRA:AVERAGE:0.5:10080:70", // 7 day avg - ony year
1029
1030 "RRA:MAX:0.5:1:70", // 1 min max - one hour
1031 "RRA:MAX:0.5:30:70", // 30 min max - one day
1032 "RRA:MAX:0.5:180:70", // 3 hour max - one week
1033 "RRA:MAX:0.5:720:70", // 12 hour max - one month
1034 "RRA:MAX:0.5:10080:70", // 7 day max - ony year
1035 NULL,
1036};
1037
1038static const char *rrd_def_vm[] = {
1039 "DS:maxcpu:GAUGE:120:0:U",
1040 "DS:cpu:GAUGE:120:0:U",
1041 "DS:maxmem:GAUGE:120:0:U",
1042 "DS:mem:GAUGE:120:0:U",
1043 "DS:maxdisk:GAUGE:120:0:U",
1044 "DS:disk:GAUGE:120:0:U",
764296f1
DM
1045 "DS:netin:DERIVE:120:0:U",
1046 "DS:netout:DERIVE:120:0:U",
1047 "DS:diskread:DERIVE:120:0:U",
1048 "DS:diskwrite:DERIVE:120:0:U",
fe000966
DM
1049
1050 "RRA:AVERAGE:0.5:1:70", // 1 min avg - one hour
1051 "RRA:AVERAGE:0.5:30:70", // 30 min avg - one day
1052 "RRA:AVERAGE:0.5:180:70", // 3 hour avg - one week
1053 "RRA:AVERAGE:0.5:720:70", // 12 hour avg - one month
1054 "RRA:AVERAGE:0.5:10080:70", // 7 day avg - ony year
1055
1056 "RRA:MAX:0.5:1:70", // 1 min max - one hour
1057 "RRA:MAX:0.5:30:70", // 30 min max - one day
1058 "RRA:MAX:0.5:180:70", // 3 hour max - one week
1059 "RRA:MAX:0.5:720:70", // 12 hour max - one month
1060 "RRA:MAX:0.5:10080:70", // 7 day max - ony year
1061 NULL,
1062};
1063
1064static const char *rrd_def_storage[] = {
1065 "DS:total:GAUGE:120:0:U",
1066 "DS:used:GAUGE:120:0:U",
1067
1068 "RRA:AVERAGE:0.5:1:70", // 1 min avg - one hour
1069 "RRA:AVERAGE:0.5:30:70", // 30 min avg - one day
1070 "RRA:AVERAGE:0.5:180:70", // 3 hour avg - one week
1071 "RRA:AVERAGE:0.5:720:70", // 12 hour avg - one month
1072 "RRA:AVERAGE:0.5:10080:70", // 7 day avg - ony year
1073
1074 "RRA:MAX:0.5:1:70", // 1 min max - one hour
1075 "RRA:MAX:0.5:30:70", // 30 min max - one day
1076 "RRA:MAX:0.5:180:70", // 3 hour max - one week
1077 "RRA:MAX:0.5:720:70", // 12 hour max - one month
1078 "RRA:MAX:0.5:10080:70", // 7 day max - ony year
1079 NULL,
1080};
1081
1082#define RRDDIR "/var/lib/rrdcached/db"
1083
1084static void
1085create_rrd_file(
1086 const char *filename,
1087 int argcount,
1088 const char *rrddef[])
1089{
1090 /* start at day boundary */
1091 time_t ctime;
1092 time(&ctime);
1093 struct tm *ltm = localtime(&ctime);
1094 ltm->tm_sec = 0;
1095 ltm->tm_min = 0;
1096 ltm->tm_hour = 0;
1097
1098 rrd_clear_error();
1099 if (rrd_create_r(filename, 60, timelocal(ltm), argcount, rrddef)) {
1100 cfs_message("RRD create error %s: %s", filename, rrd_get_error());
1101 }
1102}
1103
1104static inline const char *
1105rrd_skip_data(
1106 const char *data,
1107 int count)
1108{
1109 int found = 0;
1110 while (*data && found < count) {
1111 if (*data++ == ':')
1112 found++;
1113 }
1114 return data;
1115}
1116
1117static void
1118update_rrd_data(
1119 const char *key,
1120 gconstpointer data,
1121 size_t len)
1122{
1123 g_return_if_fail(key != NULL);
1124 g_return_if_fail(data != NULL);
1125 g_return_if_fail(len > 0);
1126 g_return_if_fail(len < 4096);
1127
1128 static const char *rrdcsock = "unix:/var/run/rrdcached.sock";
1129
1130 int use_daemon = 1;
1131 if (rrdc_connect(rrdcsock) != 0)
1132 use_daemon = 0;
1133
ba9dcfc1 1134 char *filename = NULL;
fe000966
DM
1135
1136 int skip = 0;
1137
1138 if (strncmp(key, "pve2-node/", 10) == 0) {
1139 const char *node = key + 10;
1140
f9c865a8 1141 skip = 2;
fe000966
DM
1142
1143 if (strchr(node, '/') != NULL)
1144 goto keyerror;
1145
1146 if (strlen(node) < 1)
1147 goto keyerror;
1148
ba9dcfc1
DM
1149 filename = g_strdup_printf(RRDDIR "/%s", key);
1150
fe000966
DM
1151 if (!g_file_test(filename, G_FILE_TEST_EXISTS)) {
1152
1153 mkdir(RRDDIR "/pve2-node", 0755);
1154 int argcount = sizeof(rrd_def_node)/sizeof(void*) - 1;
1155 create_rrd_file(filename, argcount, rrd_def_node);
1156 }
1157
ba9dcfc1
DM
1158 } else if ((strncmp(key, "pve2-vm/", 8) == 0) ||
1159 (strncmp(key, "pve2.3-vm/", 10) == 0)) {
1160 const char *vmid;
fe000966 1161
ba9dcfc1
DM
1162 if (strncmp(key, "pve2-vm/", 8) == 0) {
1163 vmid = key + 8;
1164 skip = 2;
1165 } else {
1166 vmid = key + 10;
94e4cba3 1167 skip = 4;
ba9dcfc1 1168 }
fe000966
DM
1169
1170 if (strchr(vmid, '/') != NULL)
1171 goto keyerror;
1172
1173 if (strlen(vmid) < 1)
1174 goto keyerror;
1175
ba9dcfc1
DM
1176 filename = g_strdup_printf(RRDDIR "/%s/%s", "pve2-vm", vmid);
1177
fe000966
DM
1178 if (!g_file_test(filename, G_FILE_TEST_EXISTS)) {
1179
1180 mkdir(RRDDIR "/pve2-vm", 0755);
1181 int argcount = sizeof(rrd_def_vm)/sizeof(void*) - 1;
1182 create_rrd_file(filename, argcount, rrd_def_vm);
1183 }
1184
1185 } else if (strncmp(key, "pve2-storage/", 13) == 0) {
1186 const char *node = key + 13;
1187
1188 const char *storage = node;
1189 while (*storage && *storage != '/')
1190 storage++;
1191
1192 if (*storage != '/' || ((storage - node) < 1))
1193 goto keyerror;
1194
1195 storage++;
1196
1197 if (strchr(storage, '/') != NULL)
1198 goto keyerror;
1199
1200 if (strlen(storage) < 1)
1201 goto keyerror;
1202
ba9dcfc1
DM
1203 filename = g_strdup_printf(RRDDIR "/%s", key);
1204
fe000966
DM
1205 if (!g_file_test(filename, G_FILE_TEST_EXISTS)) {
1206
1207 mkdir(RRDDIR "/pve2-storage", 0755);
1208
1209 char *dir = g_path_get_dirname(filename);
1210 mkdir(dir, 0755);
1211 g_free(dir);
1212
1213 int argcount = sizeof(rrd_def_storage)/sizeof(void*) - 1;
1214 create_rrd_file(filename, argcount, rrd_def_storage);
1215 }
1216
1217 } else {
1218 goto keyerror;
1219 }
1220
1221 const char *dp = skip ? rrd_skip_data(data, skip) : data;
1222
1223 const char *update_args[] = { dp, NULL };
1224
1225 if (use_daemon) {
1226 int status;
1227 if ((status = rrdc_update(filename, 1, update_args)) != 0) {
1228 cfs_message("RRDC update error %s: %d", filename, status);
1229 rrdc_disconnect();
1230 rrd_clear_error();
1231 if (rrd_update_r(filename, NULL, 1, update_args) != 0) {
1232 cfs_message("RRD update error %s: %s", filename, rrd_get_error());
1233 }
1234 }
1235
1236 } else {
1237 rrd_clear_error();
1238 if (rrd_update_r(filename, NULL, 1, update_args) != 0) {
1239 cfs_message("RRD update error %s: %s", filename, rrd_get_error());
1240 }
1241 }
1242
1243ret:
ba9dcfc1
DM
1244 if (filename)
1245 g_free(filename);
1246
fe000966
DM
1247 return;
1248
1249keyerror:
1250 cfs_critical("RRD update error: unknown/wrong key %s", key);
1251 goto ret;
1252}
1253
1254static gboolean
1255rrd_entry_is_old(
1256 gpointer key,
1257 gpointer value,
1258 gpointer user_data)
1259{
1260 rrdentry_t *entry = (rrdentry_t *)value;
1261 uint32_t ctime = GPOINTER_TO_UINT(user_data);
1262
1263 int diff = ctime - entry->time;
1264
1265 /* remove everything older than 5 minutes */
1266 int expire = 60*5;
1267
1268 return (diff > expire) ? TRUE : FALSE;
1269}
1270
1271static char *rrd_dump_buf = NULL;
1272static time_t rrd_dump_last = 0;
1273
1274void
1275cfs_rrd_dump(GString *str)
1276{
1277 time_t ctime;
fe000966 1278
663896cf
TL
1279 g_mutex_lock (&mutex);
1280
1281 time(&ctime);
fe000966
DM
1282 if (rrd_dump_buf && (ctime - rrd_dump_last) < 2) {
1283 g_string_assign(str, rrd_dump_buf);
663896cf 1284 g_mutex_unlock (&mutex);
fe000966
DM
1285 return;
1286 }
1287
1288 /* remove old data */
1289 g_hash_table_foreach_remove(cfs_status.rrdhash, rrd_entry_is_old,
1290 GUINT_TO_POINTER(ctime));
1291
1292 g_string_set_size(str, 0);
1293
1294 GHashTableIter iter;
1295 gpointer key, value;
1296
1297 g_hash_table_iter_init (&iter, cfs_status.rrdhash);
1298
1299 while (g_hash_table_iter_next (&iter, &key, &value)) {
1300 rrdentry_t *entry = (rrdentry_t *)value;
1301 g_string_append(str, key);
1302 g_string_append(str, ":");
1303 g_string_append(str, entry->data);
1304 g_string_append(str, "\n");
1305 }
1306
1307 g_string_append_c(str, 0); // never return undef
1308
1309 rrd_dump_last = ctime;
1310 if (rrd_dump_buf)
1311 g_free(rrd_dump_buf);
1312 rrd_dump_buf = g_strdup(str->str);
663896cf
TL
1313
1314 g_mutex_unlock (&mutex);
fe000966
DM
1315}
1316
1317static gboolean
1318nodeip_hash_set(
1319 GHashTable *iphash,
1320 const char *nodename,
1321 const char *ip,
1322 size_t len)
1323{
1324 g_return_val_if_fail(iphash != NULL, FALSE);
1325 g_return_val_if_fail(nodename != NULL, FALSE);
1326 g_return_val_if_fail(ip != NULL, FALSE);
1327 g_return_val_if_fail(len > 0, FALSE);
1328 g_return_val_if_fail(len < 256, FALSE);
1329 g_return_val_if_fail(ip[len-1] == 0, FALSE);
1330
1331 char *oldip = (char *)g_hash_table_lookup(iphash, nodename);
1332
1333 if (!oldip || (strcmp(oldip, ip) != 0)) {
1334 cfs_status.clinfo_version++;
1335 g_hash_table_replace(iphash, g_strdup(nodename), g_strdup(ip));
1336 }
1337
1338 return TRUE;
1339}
1340
1341static gboolean
1342rrdentry_hash_set(
1343 GHashTable *rrdhash,
1344 const char *key,
1345 const char *data,
1346 size_t len)
1347{
1348 g_return_val_if_fail(rrdhash != NULL, FALSE);
1349 g_return_val_if_fail(key != NULL, FALSE);
1350 g_return_val_if_fail(data != NULL, FALSE);
1351 g_return_val_if_fail(len > 0, FALSE);
1352 g_return_val_if_fail(len < 4096, FALSE);
1353 g_return_val_if_fail(data[len-1] == 0, FALSE);
1354
1355 rrdentry_t *entry;
1356 if ((entry = (rrdentry_t *)g_hash_table_lookup(rrdhash, key))) {
1357 g_free(entry->data);
1358 entry->data = g_memdup(data, len);
1359 entry->len = len;
1360 entry->time = time(NULL);
1361 } else {
1362 rrdentry_t *entry = g_new0(rrdentry_t, 1);
1363
1364 entry->key = g_strdup(key);
1365 entry->data = g_memdup(data, len);
1366 entry->len = len;
1367 entry->time = time(NULL);
1368
1369 g_hash_table_replace(rrdhash, entry->key, entry);
1370 }
1371
1372 update_rrd_data(key, data, len);
1373
1374 return TRUE;
1375}
1376
1377static int
1378kvstore_send_update_message(
1379 dfsm_t *dfsm,
1380 const char *key,
1381 gpointer data,
1382 guint32 len)
1383{
af2e9dd4
DM
1384 if (!dfsm_is_initialized(dfsm))
1385 return -EACCES;
fe000966
DM
1386
1387 struct iovec iov[2];
1388
1389 char name[256];
1390 g_strlcpy(name, key, sizeof(name));
1391
1392 iov[0].iov_base = &name;
1393 iov[0].iov_len = sizeof(name);
1394
1395 iov[1].iov_base = (char *)data;
1396 iov[1].iov_len = len;
1397
1398 if (dfsm_send_message(dfsm, KVSTORE_MESSAGE_UPDATE, iov, 2) == CS_OK)
1399 return 0;
1400
1401 return -EACCES;
1402}
1403
1404static clog_entry_t *
1405kvstore_parse_log_message(
1406 const void *msg,
1407 size_t msg_len)
1408{
1409 g_return_val_if_fail(msg != NULL, NULL);
1410
1411 if (msg_len < sizeof(clog_entry_t)) {
e5a5a3ea 1412 cfs_critical("received short log message (%zu < %zu)", msg_len, sizeof(clog_entry_t));
fe000966
DM
1413 return NULL;
1414 }
1415
1416 clog_entry_t *entry = (clog_entry_t *)msg;
1417
1418 uint32_t size = sizeof(clog_entry_t) + entry->node_len +
1419 entry->ident_len + entry->tag_len + entry->msg_len;
1420
1421 if (msg_len != size) {
e5a5a3ea 1422 cfs_critical("received log message with wrong size (%zu != %u)", msg_len, size);
fe000966
DM
1423 return NULL;
1424 }
1425
ce669d14 1426 char *msgptr = entry->data;
fe000966 1427
ce669d14 1428 if (*((char *)msgptr + entry->node_len - 1)) {
fe000966
DM
1429 cfs_critical("unterminated string in log message");
1430 return NULL;
1431 }
ce669d14 1432 msgptr += entry->node_len;
fe000966 1433
ce669d14 1434 if (*((char *)msgptr + entry->ident_len - 1)) {
fe000966
DM
1435 cfs_critical("unterminated string in log message");
1436 return NULL;
1437 }
ce669d14 1438 msgptr += entry->ident_len;
fe000966 1439
ce669d14 1440 if (*((char *)msgptr + entry->tag_len - 1)) {
fe000966
DM
1441 cfs_critical("unterminated string in log message");
1442 return NULL;
1443 }
ce669d14 1444 msgptr += entry->tag_len;
fe000966 1445
ce669d14 1446 if (*((char *)msgptr + entry->msg_len - 1)) {
fe000966
DM
1447 cfs_critical("unterminated string in log message");
1448 return NULL;
1449 }
1450
1451 return entry;
1452}
1453
1454static gboolean
1455kvstore_parse_update_message(
1456 const void *msg,
1457 size_t msg_len,
1458 const char **key,
1459 gconstpointer *data,
1460 guint32 *len)
1461{
1462 g_return_val_if_fail(msg != NULL, FALSE);
1463 g_return_val_if_fail(key != NULL, FALSE);
1464 g_return_val_if_fail(data != NULL, FALSE);
1465 g_return_val_if_fail(len != NULL, FALSE);
1466
1467 if (msg_len < 256) {
e5a5a3ea 1468 cfs_critical("received short kvstore message (%zu < 256)", msg_len);
fe000966
DM
1469 return FALSE;
1470 }
1471
1472 /* test if key is null terminated */
1473 int i = 0;
1474 for (i = 0; i < 256; i++)
1475 if (((char *)msg)[i] == 0)
1476 break;
1477
1478 if (i == 256)
1479 return FALSE;
1480
1481
1482 *len = msg_len - 256;
1483 *key = msg;
ce669d14 1484 *data = (char *) msg + 256;
fe000966
DM
1485
1486 return TRUE;
1487}
1488
1489int
1490cfs_create_status_msg(
1491 GString *str,
1492 const char *nodename,
1493 const char *key)
1494{
1495 g_return_val_if_fail(str != NULL, -EINVAL);
1496 g_return_val_if_fail(key != NULL, -EINVAL);
1497
1498 int res = -ENOENT;
1499
1500 GHashTable *kvhash = NULL;
1501
89fde9ac 1502 g_mutex_lock (&mutex);
fe000966
DM
1503
1504 if (!nodename || !nodename[0] || !strcmp(nodename, cfs.nodename)) {
1505 kvhash = cfs_status.kvhash;
043bbd8f 1506 } else if (cfs_status.clinfo && cfs_status.clinfo->nodes_byname) {
fe000966
DM
1507 cfs_clnode_t *clnode;
1508 if ((clnode = g_hash_table_lookup(cfs_status.clinfo->nodes_byname, nodename)))
1509 kvhash = clnode->kvhash;
1510 }
1511
1512 kventry_t *entry;
1513 if (kvhash && (entry = (kventry_t *)g_hash_table_lookup(kvhash, key))) {
1514 g_string_append_len(str, entry->data, entry->len);
1515 res = 0;
1516 }
1517
89fde9ac 1518 g_mutex_unlock (&mutex);
fe000966
DM
1519
1520 return res;
1521}
1522
1523int
1524cfs_status_set(
1525 const char *key,
1526 gpointer data,
1527 size_t len)
1528{
1529 g_return_val_if_fail(key != NULL, FALSE);
1530 g_return_val_if_fail(data != NULL, FALSE);
1531 g_return_val_if_fail(cfs_status.kvhash != NULL, FALSE);
1532
1533 if (len > CFS_MAX_STATUS_SIZE)
1534 return -EFBIG;
1535
89fde9ac 1536 g_mutex_lock (&mutex);
fe000966
DM
1537
1538 gboolean res;
1539
1540 if (strncmp(key, "rrd/", 4) == 0) {
1541 res = rrdentry_hash_set(cfs_status.rrdhash, key + 4, data, len);
1542 } else if (!strcmp(key, "nodeip")) {
1543 res = nodeip_hash_set(cfs_status.iphash, cfs.nodename, data, len);
1544 } else {
1545 res = kventry_hash_set(cfs_status.kvhash, key, data, len);
1546 }
89fde9ac 1547 g_mutex_unlock (&mutex);
fe000966
DM
1548
1549 if (cfs_status.kvstore)
1550 kvstore_send_update_message(cfs_status.kvstore, key, data, len);
1551
1552 return res ? 0 : -ENOMEM;
1553}
1554
1555gboolean
1556cfs_kvstore_node_set(
1557 uint32_t nodeid,
1558 const char *key,
1559 gconstpointer data,
1560 size_t len)
1561{
1562 g_return_val_if_fail(nodeid != 0, FALSE);
1563 g_return_val_if_fail(key != NULL, FALSE);
1564 g_return_val_if_fail(data != NULL, FALSE);
1565
89fde9ac 1566 g_mutex_lock (&mutex);
fe000966
DM
1567
1568 if (!cfs_status.clinfo || !cfs_status.clinfo->nodes_byid)
1569 goto ret; /* ignore */
1570
1571 cfs_clnode_t *clnode = g_hash_table_lookup(cfs_status.clinfo->nodes_byid, &nodeid);
1572 if (!clnode)
1573 goto ret; /* ignore */
1574
1575 cfs_debug("got node %d status update %s", nodeid, key);
1576
1577 if (strncmp(key, "rrd/", 4) == 0) {
1578 rrdentry_hash_set(cfs_status.rrdhash, key + 4, data, len);
1579 } else if (!strcmp(key, "nodeip")) {
1580 nodeip_hash_set(cfs_status.iphash, clnode->name, data, len);
1581 } else {
1582 if (!clnode->kvhash) {
1583 if (!(clnode->kvhash = kventry_hash_new())) {
1584 goto ret; /*ignore */
1585 }
1586 }
1587
1588 kventry_hash_set(clnode->kvhash, key, data, len);
1589
1590 }
1591ret:
89fde9ac 1592 g_mutex_unlock (&mutex);
fe000966
DM
1593
1594 return TRUE;
1595}
1596
1597static gboolean
1598cfs_kvstore_sync(void)
1599{
1600 g_return_val_if_fail(cfs_status.kvhash != NULL, FALSE);
1601 g_return_val_if_fail(cfs_status.kvstore != NULL, FALSE);
1602
1603 gboolean res = TRUE;
1604
89fde9ac 1605 g_mutex_lock (&mutex);
fe000966
DM
1606
1607 GHashTable *ht = cfs_status.kvhash;
1608 GHashTableIter iter;
1609 gpointer key, value;
1610
1611 g_hash_table_iter_init (&iter, ht);
1612
1613 while (g_hash_table_iter_next (&iter, &key, &value)) {
1614 kventry_t *entry = (kventry_t *)value;
1615 kvstore_send_update_message(cfs_status.kvstore, entry->key, entry->data, entry->len);
1616 }
1617
89fde9ac 1618 g_mutex_unlock (&mutex);
fe000966
DM
1619
1620 return res;
1621}
1622
1623static int
1624dfsm_deliver(
1625 dfsm_t *dfsm,
1626 gpointer data,
1627 int *res_ptr,
1628 uint32_t nodeid,
1629 uint32_t pid,
1630 uint16_t msg_type,
1631 uint32_t msg_time,
1632 const void *msg,
1633 size_t msg_len)
1634{
1635 g_return_val_if_fail(dfsm != NULL, -1);
1636 g_return_val_if_fail(msg != NULL, -1);
1637 g_return_val_if_fail(res_ptr != NULL, -1);
1638
1639 /* ignore message for ourself */
1640 if (dfsm_nodeid_is_local(dfsm, nodeid, pid))
1641 goto ret;
1642
1643 if (msg_type == KVSTORE_MESSAGE_UPDATE) {
1644 const char *key;
1645 gconstpointer data;
1646 guint32 len;
1647 if (kvstore_parse_update_message(msg, msg_len, &key, &data, &len)) {
1648 cfs_kvstore_node_set(nodeid, key, data, len);
1649 } else {
1650 cfs_critical("cant parse update message");
1651 }
1652 } else if (msg_type == KVSTORE_MESSAGE_LOG) {
1653 cfs_message("received log"); // fixme: remove
1654 const clog_entry_t *entry;
1655 if ((entry = kvstore_parse_log_message(msg, msg_len))) {
1656 clusterlog_insert(cfs_status.clusterlog, entry);
1657 } else {
1658 cfs_critical("cant parse log message");
1659 }
1660 } else {
1661 cfs_critical("received unknown message type %d\n", msg_type);
1662 goto fail;
1663 }
1664
1665ret:
1666 *res_ptr = 0;
1667 return 1;
1668
1669fail:
1670 *res_ptr = -EACCES;
1671 return 1;
1672}
1673
1674static void
1675dfsm_confchg(
1676 dfsm_t *dfsm,
1677 gpointer data,
1678 const struct cpg_address *member_list,
1679 size_t member_list_entries)
1680{
1681 g_return_if_fail(dfsm != NULL);
1682 g_return_if_fail(member_list != NULL);
1683
1684 cfs_debug("enter %s", __func__);
1685
89fde9ac 1686 g_mutex_lock (&mutex);
fe000966
DM
1687
1688 cfs_clinfo_t *clinfo = cfs_status.clinfo;
1689
1690 if (clinfo && clinfo->nodes_byid) {
1691
1692 GHashTable *ht = clinfo->nodes_byid;
1693 GHashTableIter iter;
1694 gpointer key, value;
1695
1696 g_hash_table_iter_init (&iter, ht);
1697
1698 while (g_hash_table_iter_next (&iter, &key, &value)) {
1699 cfs_clnode_t *node = (cfs_clnode_t *)value;
1700 node->online = FALSE;
1701 }
1702
1703 for (int i = 0; i < member_list_entries; i++) {
1704 cfs_clnode_t *node;
1705 if ((node = g_hash_table_lookup(clinfo->nodes_byid, &member_list[i].nodeid))) {
1706 node->online = TRUE;
1707 }
1708 }
1709
1710 cfs_status.clinfo_version++;
1711 }
1712
89fde9ac 1713 g_mutex_unlock (&mutex);
fe000966
DM
1714}
1715
1716static gpointer
1717dfsm_get_state(
1718 dfsm_t *dfsm,
1719 gpointer data,
1720 unsigned int *res_len)
1721{
1722 g_return_val_if_fail(dfsm != NULL, NULL);
1723
1724 gpointer msg = clusterlog_get_state(cfs_status.clusterlog, res_len);
1725
1726 return msg;
1727}
1728
1729static int
1730dfsm_process_update(
1731 dfsm_t *dfsm,
1732 gpointer data,
1733 dfsm_sync_info_t *syncinfo,
1734 uint32_t nodeid,
1735 uint32_t pid,
1736 const void *msg,
1737 size_t msg_len)
1738{
1739 cfs_critical("%s: received unexpected update message", __func__);
1740
1741 return -1;
1742}
1743
1744static int
1745dfsm_process_state_update(
1746 dfsm_t *dfsm,
1747 gpointer data,
1748 dfsm_sync_info_t *syncinfo)
1749{
1750 g_return_val_if_fail(dfsm != NULL, -1);
1751 g_return_val_if_fail(syncinfo != NULL, -1);
1752
1753 clog_base_t *clog[syncinfo->node_count];
1754
1755 int local_index = -1;
1756 for (int i = 0; i < syncinfo->node_count; i++) {
1757 dfsm_node_info_t *ni = &syncinfo->nodes[i];
1758 ni->synced = 1;
1759
1760 if (syncinfo->local == ni)
1761 local_index = i;
1762
1763 clog_base_t *base = (clog_base_t *)ni->state;
1764 if (ni->state_len > 8 && ni->state_len == clog_size(base)) {
1765 clog[i] = ni->state;
1766 } else {
1767 cfs_critical("received log with wrong size %u", ni->state_len);
1768 clog[i] = NULL;
1769 }
1770 }
1771
1772 if (!clusterlog_merge(cfs_status.clusterlog, clog, syncinfo->node_count, local_index)) {
1773 cfs_critical("unable to merge log files");
1774 }
1775
1776 cfs_kvstore_sync();
1777
1778 return 1;
1779}
1780
1781static int
1782dfsm_commit(
1783 dfsm_t *dfsm,
1784 gpointer data,
1785 dfsm_sync_info_t *syncinfo)
1786{
1787 g_return_val_if_fail(dfsm != NULL, -1);
1788 g_return_val_if_fail(syncinfo != NULL, -1);
1789
1790 return 1;
1791}
1792
1793static void
1794dfsm_synced(dfsm_t *dfsm)
1795{
1796 g_return_if_fail(dfsm != NULL);
1797
1798 char *ip = (char *)g_hash_table_lookup(cfs_status.iphash, cfs.nodename);
1799 if (!ip)
1800 ip = cfs.ip;
1801
1802 cfs_status_set("nodeip", ip, strlen(ip) + 1);
1803}
1804
1805static int
1806dfsm_cleanup(
1807 dfsm_t *dfsm,
1808 gpointer data,
1809 dfsm_sync_info_t *syncinfo)
1810{
1811 return 1;
1812}
1813
1814static dfsm_callbacks_t kvstore_dfsm_callbacks = {
1815 .dfsm_deliver_fn = dfsm_deliver,
1816 .dfsm_confchg_fn = dfsm_confchg,
1817
1818 .dfsm_get_state_fn = dfsm_get_state,
1819 .dfsm_process_state_update_fn = dfsm_process_state_update,
1820 .dfsm_process_update_fn = dfsm_process_update,
1821 .dfsm_commit_fn = dfsm_commit,
1822 .dfsm_cleanup_fn = dfsm_cleanup,
1823 .dfsm_synced_fn = dfsm_synced,
1824};
1825
1826dfsm_t *
1827cfs_status_dfsm_new(void)
1828{
89fde9ac 1829 g_mutex_lock (&mutex);
fe000966
DM
1830
1831 cfs_status.kvstore = dfsm_new(NULL, KVSTORE_CPG_GROUP_NAME, G_LOG_DOMAIN,
1832 0, &kvstore_dfsm_callbacks);
89fde9ac 1833 g_mutex_unlock (&mutex);
fe000966
DM
1834
1835 return cfs_status.kvstore;
1836}
1837
1838gboolean
1839cfs_is_quorate(void)
1840{
89fde9ac 1841 g_mutex_lock (&mutex);
fe000966 1842 gboolean res = cfs_status.quorate;
89fde9ac 1843 g_mutex_unlock (&mutex);
fe000966
DM
1844
1845 return res;
1846}
1847
1848void
1849cfs_set_quorate(
1850 uint32_t quorate,
1851 gboolean quiet)
1852{
89fde9ac 1853 g_mutex_lock (&mutex);
fe000966
DM
1854
1855 uint32_t prev_quorate = cfs_status.quorate;
1856 cfs_status.quorate = quorate;
1857
1858 if (!prev_quorate && cfs_status.quorate) {
1859 if (!quiet)
1860 cfs_message("node has quorum");
1861 }
1862
1863 if (prev_quorate && !cfs_status.quorate) {
1864 if (!quiet)
1865 cfs_message("node lost quorum");
1866 }
1867
89fde9ac 1868 g_mutex_unlock (&mutex);
fe000966 1869}