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