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