2 Copyright (C) 2010 - 2020 Proxmox Server Solutions GmbH
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.
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.
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/>.
17 Author: Dietmar Maurer <dietmar@proxmox.com>
21 #define G_LOG_DOMAIN "status"
25 #endif /* HAVE_CONFIG_H */
32 #include <sys/syslog.h>
34 #include <rrd_client.h>
38 #include "cfs-utils.h"
43 #define KVSTORE_CPG_GROUP_NAME "pve_kvstore_v1"
46 KVSTORE_MESSAGE_UPDATE
= 1,
47 KVSTORE_MESSAGE_UPDATE_COMPLETE
= 2,
48 KVSTORE_MESSAGE_LOG
= 3,
51 static uint32_t vminfo_version_counter
;
79 static memdb_change_t memdb_change_array
[] = {
80 { .path
= "corosync.conf" },
81 { .path
= "corosync.conf.new" },
82 { .path
= "storage.cfg" },
83 { .path
= "user.cfg" },
84 { .path
= "domains.cfg" },
85 { .path
= "notifications.cfg"},
86 { .path
= "priv/notifications.cfg"},
87 { .path
= "priv/shadow.cfg" },
88 { .path
= "priv/acme/plugins.cfg" },
89 { .path
= "priv/tfa.cfg" },
90 { .path
= "priv/token.cfg" },
91 { .path
= "priv/ipam.db" },
92 { .path
= "datacenter.cfg" },
93 { .path
= "vzdump.cron" },
94 { .path
= "vzdump.conf" },
95 { .path
= "jobs.cfg" },
96 { .path
= "ha/crm_commands" },
97 { .path
= "ha/manager_status" },
98 { .path
= "ha/resources.cfg" },
99 { .path
= "ha/groups.cfg" },
100 { .path
= "ha/fence.cfg" },
101 { .path
= "status.cfg" },
102 { .path
= "replication.cfg" },
103 { .path
= "ceph.conf" },
104 { .path
= "sdn/vnets.cfg" },
105 { .path
= "sdn/zones.cfg" },
106 { .path
= "sdn/controllers.cfg" },
107 { .path
= "sdn/subnets.cfg" },
108 { .path
= "sdn/ipams.cfg" },
109 { .path
= "sdn/dns.cfg" },
110 { .path
= "sdn/.running-config" },
111 { .path
= "virtual-guest/cpu-models.conf" },
112 { .path
= "firewall/cluster.fw" },
113 { .path
= "mapping/pci.cfg" },
114 { .path
= "mapping/usb.cfg" },
124 cfs_clinfo_t
*clinfo
;
125 uint32_t clinfo_version
;
128 uint32_t vmlist_version
;
135 GHashTable
*memdb_changes
;
137 clusterlog_t
*clusterlog
;
140 static cfs_status_t cfs_status
;
152 uint32_t cman_version
;
154 GHashTable
*nodes_byid
;
155 GHashTable
*nodes_byname
;
159 g_int32_hash (gconstpointer v
)
161 return *(const uint32_t *) v
;
165 g_int32_equal (gconstpointer v1
,
168 return *((const uint32_t*) v1
) == *((const uint32_t*) v2
);
171 static void vminfo_free(vminfo_t
*vminfo
)
173 g_return_if_fail(vminfo
!= NULL
);
175 if (vminfo
->nodename
)
176 g_free(vminfo
->nodename
);
182 static const char *vminfo_type_to_string(vminfo_t
*vminfo
)
184 if (vminfo
->vmtype
== VMTYPE_QEMU
) {
186 } else if (vminfo
->vmtype
== VMTYPE_OPENVZ
) {
187 // FIXME: remove openvz stuff for 7.x
189 } else if (vminfo
->vmtype
== VMTYPE_LXC
) {
196 static const char *vminfo_type_to_path_type(vminfo_t
*vminfo
)
198 if (vminfo
->vmtype
== VMTYPE_QEMU
) {
199 return "qemu-server"; // special case..
201 return vminfo_type_to_string(vminfo
);
205 int vminfo_to_path(vminfo_t
*vminfo
, GString
*path
)
207 g_return_val_if_fail(vminfo
!= NULL
, -1);
208 g_return_val_if_fail(path
!= NULL
, -1);
210 if (!vminfo
->nodename
)
213 const char *type
= vminfo_type_to_path_type(vminfo
);
214 g_string_printf(path
, "/nodes/%s/%s/%u.conf", vminfo
->nodename
, type
, vminfo
->vmid
);
219 void cfs_clnode_destroy(
220 cfs_clnode_t
*clnode
)
222 g_return_if_fail(clnode
!= NULL
);
225 g_hash_table_destroy(clnode
->kvhash
);
228 g_free(clnode
->name
);
233 cfs_clnode_t
*cfs_clnode_new(
238 g_return_val_if_fail(name
!= NULL
, NULL
);
240 cfs_clnode_t
*clnode
= g_new0(cfs_clnode_t
, 1);
244 clnode
->name
= g_strdup(name
);
245 clnode
->nodeid
= nodeid
;
246 clnode
->votes
= votes
;
251 gboolean
cfs_clinfo_destroy(
252 cfs_clinfo_t
*clinfo
)
254 g_return_val_if_fail(clinfo
!= NULL
, FALSE
);
256 if (clinfo
->cluster_name
)
257 g_free(clinfo
->cluster_name
);
259 if (clinfo
->nodes_byname
)
260 g_hash_table_destroy(clinfo
->nodes_byname
);
262 if (clinfo
->nodes_byid
)
263 g_hash_table_destroy(clinfo
->nodes_byid
);
270 cfs_clinfo_t
*cfs_clinfo_new(
271 const char *cluster_name
,
272 uint32_t cman_version
)
274 g_return_val_if_fail(cluster_name
!= NULL
, NULL
);
276 cfs_clinfo_t
*clinfo
= g_new0(cfs_clinfo_t
, 1);
280 clinfo
->cluster_name
= g_strdup(cluster_name
);
281 clinfo
->cman_version
= cman_version
;
283 if (!(clinfo
->nodes_byid
= g_hash_table_new_full(
284 g_int32_hash
, g_int32_equal
, NULL
,
285 (GDestroyNotify
)cfs_clnode_destroy
)))
288 if (!(clinfo
->nodes_byname
= g_hash_table_new(g_str_hash
, g_str_equal
)))
294 cfs_clinfo_destroy(clinfo
);
299 gboolean
cfs_clinfo_add_node(
300 cfs_clinfo_t
*clinfo
,
301 cfs_clnode_t
*clnode
)
303 g_return_val_if_fail(clinfo
!= NULL
, FALSE
);
304 g_return_val_if_fail(clnode
!= NULL
, FALSE
);
306 g_hash_table_replace(clinfo
->nodes_byid
, &clnode
->nodeid
, clnode
);
307 g_hash_table_replace(clinfo
->nodes_byname
, clnode
->name
, clnode
);
313 cfs_create_memberlist_msg(
316 g_return_val_if_fail(str
!= NULL
, -EINVAL
);
318 g_mutex_lock (&mutex
);
320 g_string_append_printf(str
,"{\n");
324 cfs_clinfo_t
*clinfo
= cfs_status
.clinfo
;
326 if (clinfo
&& clinfo
->nodes_byid
)
327 nodecount
= g_hash_table_size(clinfo
->nodes_byid
);
330 g_string_append_printf(str
, "\"nodename\": \"%s\",\n", cfs
.nodename
);
331 g_string_append_printf(str
, "\"version\": %u,\n", cfs_status
.clinfo_version
);
333 g_string_append_printf(str
, "\"cluster\": { ");
334 g_string_append_printf(str
, "\"name\": \"%s\", \"version\": %d, "
335 "\"nodes\": %d, \"quorate\": %d ",
336 clinfo
->cluster_name
, clinfo
->cman_version
,
337 nodecount
, cfs_status
.quorate
);
339 g_string_append_printf(str
,"},\n");
340 g_string_append_printf(str
,"\"nodelist\": {\n");
342 GHashTable
*ht
= clinfo
->nodes_byid
;
346 g_hash_table_iter_init (&iter
, ht
);
349 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
350 cfs_clnode_t
*node
= (cfs_clnode_t
*)value
;
351 if (i
) g_string_append_printf(str
, ",\n");
354 g_string_append_printf(str
, " \"%s\": { \"id\": %d, \"online\": %d",
355 node
->name
, node
->nodeid
, node
->online
);
358 char *ip
= (char *)g_hash_table_lookup(cfs_status
.iphash
, node
->name
);
360 g_string_append_printf(str
, ", \"ip\": \"%s\"", ip
);
363 g_string_append_printf(str
, "}");
366 g_string_append_printf(str
,"\n }\n");
368 g_string_append_printf(str
, "\"nodename\": \"%s\",\n", cfs
.nodename
);
369 g_string_append_printf(str
, "\"version\": %u\n", cfs_status
.clinfo_version
);
372 g_string_append_printf(str
,"}\n");
374 g_mutex_unlock (&mutex
);
380 kventry_free(kventry_t
*entry
)
382 g_return_if_fail(entry
!= NULL
);
390 kventry_hash_new(void)
392 return g_hash_table_new_full(g_str_hash
, g_str_equal
, NULL
,
393 (GDestroyNotify
)kventry_free
);
397 rrdentry_free(rrdentry_t
*entry
)
399 g_return_if_fail(entry
!= NULL
);
407 rrdentry_hash_new(void)
409 return g_hash_table_new_full(g_str_hash
, g_str_equal
, NULL
,
410 (GDestroyNotify
)rrdentry_free
);
414 cfs_cluster_log_dump(GString
*str
, const char *user
, guint max_entries
)
416 clusterlog_dump(cfs_status
.clusterlog
, str
, user
, max_entries
);
420 cfs_cluster_log(clog_entry_t
*entry
)
422 g_return_if_fail(entry
!= NULL
);
424 clusterlog_insert(cfs_status
.clusterlog
, entry
);
426 if (cfs_status
.kvstore
) {
428 iov
[0].iov_base
= (char *)entry
;
429 iov
[0].iov_len
= clog_entry_size(entry
);
431 if (dfsm_is_initialized(cfs_status
.kvstore
))
432 dfsm_send_message(cfs_status
.kvstore
, KVSTORE_MESSAGE_LOG
, iov
, 1);
436 void cfs_status_init(void)
438 g_mutex_lock (&mutex
);
440 cfs_status
.start_time
= time(NULL
);
442 cfs_status
.vmlist
= vmlist_hash_new();
444 cfs_status
.kvhash
= kventry_hash_new();
446 cfs_status
.rrdhash
= rrdentry_hash_new();
448 cfs_status
.iphash
= g_hash_table_new_full(g_str_hash
, g_str_equal
, g_free
, g_free
);
450 cfs_status
.memdb_changes
= g_hash_table_new(g_str_hash
, g_str_equal
);
452 for (int i
= 0; i
< G_N_ELEMENTS(memdb_change_array
); i
++) {
453 g_hash_table_replace(cfs_status
.memdb_changes
,
454 memdb_change_array
[i
].path
,
455 &memdb_change_array
[i
]);
458 cfs_status
.clusterlog
= clusterlog_new();
461 clusterlog_add(cfs_status
.clusterlog
, "root", "cluster", getpid(),
462 LOG_INFO
, "starting cluster log");
464 g_mutex_unlock (&mutex
);
467 void cfs_status_cleanup(void)
469 g_mutex_lock (&mutex
);
471 cfs_status
.clinfo_version
++;
473 if (cfs_status
.clinfo
) {
474 cfs_clinfo_destroy(cfs_status
.clinfo
);
475 cfs_status
.clinfo
= NULL
;
478 if (cfs_status
.vmlist
) {
479 g_hash_table_destroy(cfs_status
.vmlist
);
480 cfs_status
.vmlist
= NULL
;
483 if (cfs_status
.kvhash
) {
484 g_hash_table_destroy(cfs_status
.kvhash
);
485 cfs_status
.kvhash
= NULL
;
488 if (cfs_status
.rrdhash
) {
489 g_hash_table_destroy(cfs_status
.rrdhash
);
490 cfs_status
.rrdhash
= NULL
;
493 if (cfs_status
.iphash
) {
494 g_hash_table_destroy(cfs_status
.iphash
);
495 cfs_status
.iphash
= NULL
;
498 if (cfs_status
.clusterlog
)
499 clusterlog_destroy(cfs_status
.clusterlog
);
501 g_mutex_unlock (&mutex
);
504 void cfs_status_set_clinfo(
505 cfs_clinfo_t
*clinfo
)
507 g_return_if_fail(clinfo
!= NULL
);
509 g_mutex_lock (&mutex
);
511 cfs_status
.clinfo_version
++;
513 cfs_clinfo_t
*old
= cfs_status
.clinfo
;
515 cfs_status
.clinfo
= clinfo
;
517 cfs_message("update cluster info (cluster name %s, version = %d)",
518 clinfo
->cluster_name
, clinfo
->cman_version
);
521 if (old
&& old
->nodes_byid
&& clinfo
->nodes_byid
) {
523 GHashTable
*ht
= clinfo
->nodes_byid
;
527 g_hash_table_iter_init (&iter
, ht
);
529 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
530 cfs_clnode_t
*node
= (cfs_clnode_t
*)value
;
531 cfs_clnode_t
*oldnode
;
532 if ((oldnode
= g_hash_table_lookup(old
->nodes_byid
, key
))) {
533 node
->online
= oldnode
->online
;
534 node
->kvhash
= oldnode
->kvhash
;
535 oldnode
->kvhash
= NULL
;
542 cfs_clinfo_destroy(old
);
545 g_mutex_unlock (&mutex
);
549 dump_kvstore_versions(
552 const char *nodename
)
554 g_return_if_fail(kvhash
!= NULL
);
555 g_return_if_fail(str
!= NULL
);
556 g_return_if_fail(nodename
!= NULL
);
558 GHashTable
*ht
= kvhash
;
562 g_string_append_printf(str
, "\"%s\": {\n", nodename
);
564 g_hash_table_iter_init (&iter
, ht
);
567 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
568 kventry_t
*entry
= (kventry_t
*)value
;
569 if (i
) g_string_append_printf(str
, ",\n");
571 g_string_append_printf(str
,"\"%s\": %u", entry
->key
, entry
->version
);
574 g_string_append_printf(str
, "}\n");
578 cfs_create_version_msg(GString
*str
)
580 g_return_val_if_fail(str
!= NULL
, -EINVAL
);
582 g_mutex_lock (&mutex
);
584 g_string_append_printf(str
,"{\n");
586 g_string_append_printf(str
, "\"starttime\": %lu,\n", (unsigned long)cfs_status
.start_time
);
588 g_string_append_printf(str
, "\"clinfo\": %u,\n", cfs_status
.clinfo_version
);
590 g_string_append_printf(str
, "\"vmlist\": %u,\n", cfs_status
.vmlist_version
);
592 for (int i
= 0; i
< G_N_ELEMENTS(memdb_change_array
); i
++) {
593 g_string_append_printf(str
, "\"%s\": %u,\n",
594 memdb_change_array
[i
].path
,
595 memdb_change_array
[i
].version
);
598 g_string_append_printf(str
, "\"kvstore\": {\n");
600 dump_kvstore_versions(str
, cfs_status
.kvhash
, cfs
.nodename
);
602 cfs_clinfo_t
*clinfo
= cfs_status
.clinfo
;
604 if (clinfo
&& clinfo
->nodes_byid
) {
605 GHashTable
*ht
= clinfo
->nodes_byid
;
609 g_hash_table_iter_init (&iter
, ht
);
611 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
612 cfs_clnode_t
*node
= (cfs_clnode_t
*)value
;
615 g_string_append_printf(str
, ",\n");
616 dump_kvstore_versions(str
, node
->kvhash
, node
->name
);
620 g_string_append_printf(str
,"}\n");
622 g_string_append_printf(str
,"}\n");
624 g_mutex_unlock (&mutex
);
630 vmlist_hash_new(void)
632 return g_hash_table_new_full(g_int_hash
, g_int_equal
, NULL
,
633 (GDestroyNotify
)vminfo_free
);
637 vmlist_hash_insert_vm(
641 const char *nodename
,
644 g_return_val_if_fail(vmlist
!= NULL
, FALSE
);
645 g_return_val_if_fail(nodename
!= NULL
, FALSE
);
646 g_return_val_if_fail(vmid
!= 0, FALSE
);
647 // FIXME: remove openvz stuff for 7.x
648 g_return_val_if_fail(vmtype
== VMTYPE_QEMU
|| vmtype
== VMTYPE_OPENVZ
||
649 vmtype
== VMTYPE_LXC
, FALSE
);
651 if (!replace
&& g_hash_table_lookup(vmlist
, &vmid
)) {
652 cfs_critical("detected duplicate VMID %d", vmid
);
656 vminfo_t
*vminfo
= g_new0(vminfo_t
, 1);
659 vminfo
->vmtype
= vmtype
;
660 vminfo
->nodename
= g_strdup(nodename
);
662 vminfo
->version
= ++vminfo_version_counter
;
664 g_hash_table_replace(vmlist
, &vminfo
->vmid
, vminfo
);
673 const char *nodename
)
675 g_return_if_fail(cfs_status
.vmlist
!= NULL
);
676 g_return_if_fail(nodename
!= NULL
);
677 g_return_if_fail(vmid
!= 0);
678 // FIXME: remove openvz stuff for 7.x
679 g_return_if_fail(vmtype
== VMTYPE_QEMU
|| vmtype
== VMTYPE_OPENVZ
||
680 vmtype
== VMTYPE_LXC
);
682 cfs_debug("vmlist_register_vm: %s/%u %d", nodename
, vmid
, vmtype
);
684 g_mutex_lock (&mutex
);
686 cfs_status
.vmlist_version
++;
688 vmlist_hash_insert_vm(cfs_status
.vmlist
, vmtype
, vmid
, nodename
, TRUE
);
690 g_mutex_unlock (&mutex
);
694 vmlist_different_vm_exists(
697 const char *nodename
)
699 g_return_val_if_fail(cfs_status
.vmlist
!= NULL
, FALSE
);
700 g_return_val_if_fail(vmid
!= 0, FALSE
);
702 gboolean res
= FALSE
;
704 g_mutex_lock (&mutex
);
707 if ((vminfo
= (vminfo_t
*)g_hash_table_lookup(cfs_status
.vmlist
, &vmid
))) {
708 if (!(vminfo
->vmtype
== vmtype
&& strcmp(vminfo
->nodename
, nodename
) == 0))
711 g_mutex_unlock (&mutex
);
720 g_return_val_if_fail(cfs_status
.vmlist
!= NULL
, FALSE
);
721 g_return_val_if_fail(vmid
!= 0, FALSE
);
723 g_mutex_lock (&mutex
);
725 gpointer res
= g_hash_table_lookup(cfs_status
.vmlist
, &vmid
);
727 g_mutex_unlock (&mutex
);
736 g_return_if_fail(cfs_status
.vmlist
!= NULL
);
737 g_return_if_fail(vmid
!= 0);
739 g_mutex_lock (&mutex
);
741 cfs_status
.vmlist_version
++;
743 g_hash_table_remove(cfs_status
.vmlist
, &vmid
);
745 g_mutex_unlock (&mutex
);
748 void cfs_status_set_vmlist(
751 g_return_if_fail(vmlist
!= NULL
);
753 g_mutex_lock (&mutex
);
755 cfs_status
.vmlist_version
++;
757 if (cfs_status
.vmlist
)
758 g_hash_table_destroy(cfs_status
.vmlist
);
760 cfs_status
.vmlist
= vmlist
;
762 g_mutex_unlock (&mutex
);
766 cfs_create_vmlist_msg(GString
*str
)
768 g_return_val_if_fail(cfs_status
.vmlist
!= NULL
, -EINVAL
);
769 g_return_val_if_fail(str
!= NULL
, -EINVAL
);
771 g_mutex_lock (&mutex
);
773 g_string_append_printf(str
,"{\n");
775 GHashTable
*ht
= cfs_status
.vmlist
;
777 guint count
= g_hash_table_size(ht
);
780 g_string_append_printf(str
,"\"version\": %u\n", cfs_status
.vmlist_version
);
782 g_string_append_printf(str
,"\"version\": %u,\n", cfs_status
.vmlist_version
);
784 g_string_append_printf(str
,"\"ids\": {\n");
789 g_hash_table_iter_init (&iter
, ht
);
792 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
793 vminfo_t
*vminfo
= (vminfo_t
*)value
;
794 const char *type
= vminfo_type_to_string(vminfo
);
797 g_string_append_printf(str
, ",\n");
800 g_string_append_printf(str
,"\"%u\": { \"node\": \"%s\", \"type\": \"%s\", \"version\": %u }",
801 vminfo
->vmid
, vminfo
->nodename
, type
, vminfo
->version
);
804 g_string_append_printf(str
,"}\n");
806 g_string_append_printf(str
,"\n}\n");
808 g_mutex_unlock (&mutex
);
813 // checks if a config line starts with the given prop. if yes, writes a '\0'
814 // at the end of the value, and returns the pointer where the value starts
815 // note: line[line_end] needs to be guaranteed a null byte
817 _get_property_value_from_line(char *line
, size_t line_len
, const char *prop
, size_t prop_len
)
819 if (line_len
<= prop_len
+ 1) return NULL
;
821 if (line
[prop_len
] == ':' && memcmp(line
, prop
, prop_len
) == 0) { // found
822 char *v_start
= &line
[prop_len
+ 1];
823 char *v_end
= &line
[line_len
- 1];
825 // drop initial value whitespaces here already
826 while (v_start
< v_end
&& *v_start
&& isspace(*v_start
)) v_start
++;
828 if (!*v_start
) return NULL
;
830 while (v_end
> v_start
&& isspace(*v_end
)) v_end
--;
831 if (v_end
< &line
[line_len
- 1]) {
841 // checks the conf for lines starting with the given props and
842 // writes the pointers into the correct positions into the 'found' array
843 // afterwards, without initial whitespace(s), we only deal with the format
844 // restriction imposed by our perl VM config parser, main reference is
845 // PVE::QemuServer::parse_vm_config this allows to be very fast and still
847 // main restrictions used for our advantage is the properties match regex:
848 // ($line =~ m/^([a-z][a-z_]*\d*):\s*(.+?)\s*$/) from parse_vm_config
849 // currently we only look at the current configuration in place, i.e., *no*
850 // snapshot and *no* pending changes
852 // min..max is the char range of the first character of the given props,
853 // so that we can return early when checking the line
854 // note: conf must end with a newline
856 _get_property_values(char **found
, char *conf
, int conf_size
, const char **props
, uint8_t num_props
, char min
, char max
)
858 const char *const conf_end
= conf
+ conf_size
;
860 size_t remaining_size
= conf_size
;
863 if (conf_size
== 0) {
867 char *next_newline
= memchr(conf
, '\n', conf_size
);
868 if (next_newline
== NULL
) {
869 return; // valid property lines end with \n, but none in the config
871 *next_newline
= '\0';
873 while (line
!= NULL
) {
874 if (!line
[0]) goto next
;
876 // snapshot or pending section start, but nothing found yet -> not found
877 if (line
[0] == '[') return;
878 // continue early if line does not begin with the min/max char of the properties
879 if (line
[0] < min
|| line
[0] > max
) goto next
;
881 size_t line_len
= next_newline
- line
;
882 for (uint8_t i
= 0; i
< num_props
; i
++) {
883 char * value
= _get_property_value_from_line(line
, line_len
, props
[i
], strlen(props
[i
]));
885 count
+= (found
[i
] != NULL
) & 0x1; // count newly found lines
889 if (count
== num_props
) {
893 line
= next_newline
+ 1;
894 remaining_size
= conf_end
- line
;
895 next_newline
= memchr(line
, '\n', remaining_size
);
896 if (next_newline
== NULL
) {
897 return; // valid property lines end with \n, but none in the config
899 *next_newline
= '\0';
906 _g_str_append_kv_jsonescaped(GString
*str
, const char *k
, const char *v
)
908 g_string_append_printf(str
, "\"%s\": \"", k
);
911 if (*v
== '\\' || *v
== '"') {
912 g_string_append_c(str
, '\\');
914 g_string_append_c(str
, *v
);
917 g_string_append_c(str
, '"');
921 _print_found_properties(
933 _get_property_values(values
, conf
, size
, props
, num_props
, min
, max
);
936 for (uint8_t i
= 0; i
< num_props
; i
++) {
937 if (values
[i
] == NULL
) {
941 g_string_append_c(str
, ',');
944 g_string_append_printf(str
, ",\n");
948 g_string_append_printf(str
, "\"%u\":{", vmid
);
951 _g_str_append_kv_jsonescaped(str
, props
[i
], values
[i
]);
955 g_string_append_c(str
, '}');
962 cfs_create_guest_conf_properties_msg(GString
*str
, memdb_t
*memdb
, const char **props
, uint8_t num_props
, uint32_t vmid
)
964 g_return_val_if_fail(cfs_status
.vmlist
!= NULL
, -EINVAL
);
965 g_return_val_if_fail(str
!= NULL
, -EINVAL
);
967 // Prelock &memdb->mutex in order to enable the usage of memdb_read_nolock
968 // to prevent Deadlocks as in #2553
969 g_mutex_lock (&memdb
->mutex
);
970 g_mutex_lock (&mutex
);
972 g_string_printf(str
, "{\n");
974 GHashTable
*ht
= cfs_status
.vmlist
;
977 GString
*path
= NULL
;
979 char **values
= calloc(num_props
, sizeof(char*));
980 char min
= 'z', max
= 'a';
982 for (uint8_t i
= 0; i
< num_props
; i
++) {
983 if (props
[i
][0] > max
) {
987 if (props
[i
][0] < min
) {
992 if (!g_hash_table_size(ht
)) {
996 if ((path
= g_string_sized_new(256)) == NULL
) {
1002 vminfo_t
*vminfo
= (vminfo_t
*) g_hash_table_lookup(cfs_status
.vmlist
, &vmid
);
1003 if (vminfo
== NULL
) goto enoent
;
1005 if (!vminfo_to_path(vminfo
, path
)) goto err
;
1007 // use memdb_read_nolock because lock is handled here
1008 int size
= memdb_read_nolock(memdb
, path
->str
, &tmp
);
1009 if (tmp
== NULL
) goto err
;
1011 // conf needs to be newline terminated
1012 if (((char *)tmp
)[size
- 1] != '\n') {
1013 gpointer
new = realloc(tmp
, size
+ 1);
1014 if (new == NULL
) goto err
;
1016 ((char *)tmp
)[size
++] = '\n';
1018 _print_found_properties(str
, tmp
, size
, props
, num_props
, vmid
, values
, min
, max
, 1);
1020 GHashTableIter iter
;
1021 g_hash_table_iter_init (&iter
, ht
);
1023 gpointer key
, value
;
1025 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
1026 vminfo_t
*vminfo
= (vminfo_t
*)value
;
1028 if (!vminfo_to_path(vminfo
, path
)) goto err
;
1030 g_free(tmp
); // no-op if already null
1032 // use memdb_read_nolock because lock is handled here
1033 int size
= memdb_read_nolock(memdb
, path
->str
, &tmp
);
1034 if (tmp
== NULL
) continue;
1036 // conf needs to be newline terminated
1037 if (((char *)tmp
)[size
- 1] != '\n') {
1038 gpointer
new = realloc(tmp
, size
+ 1);
1039 if (new == NULL
) continue;
1041 ((char *)tmp
)[size
++] = '\n';
1044 memset(values
, 0, sizeof(char*)*num_props
); // reset array
1045 first
= _print_found_properties(str
, tmp
, size
, props
, num_props
,
1046 vminfo
->vmid
, values
, min
, max
, first
);
1053 g_string_free(path
, TRUE
);
1055 g_string_append_printf(str
,"\n}\n");
1056 g_mutex_unlock (&mutex
);
1057 g_mutex_unlock (&memdb
->mutex
);
1068 cfs_create_guest_conf_property_msg(GString
*str
, memdb_t
*memdb
, const char *prop
, uint32_t vmid
)
1070 return cfs_create_guest_conf_properties_msg(str
, memdb
, &prop
, 1, vmid
);
1074 record_memdb_change(const char *path
)
1076 g_return_if_fail(cfs_status
.memdb_changes
!= 0);
1080 if ((ce
= (memdb_change_t
*)g_hash_table_lookup(cfs_status
.memdb_changes
, path
))) {
1086 record_memdb_reload(void)
1088 for (int i
= 0; i
< G_N_ELEMENTS(memdb_change_array
); i
++) {
1089 memdb_change_array
[i
].version
++;
1100 g_return_val_if_fail(kvhash
!= NULL
, FALSE
);
1101 g_return_val_if_fail(key
!= NULL
, FALSE
);
1102 g_return_val_if_fail(data
!= NULL
, FALSE
);
1106 g_hash_table_remove(kvhash
, key
);
1107 } else if ((entry
= (kventry_t
*)g_hash_table_lookup(kvhash
, key
))) {
1108 g_free(entry
->data
);
1109 entry
->data
= g_memdup2(data
, len
);
1113 kventry_t
*entry
= g_new0(kventry_t
, 1);
1115 entry
->key
= g_strdup(key
);
1116 entry
->data
= g_memdup2(data
, len
);
1119 g_hash_table_replace(kvhash
, entry
->key
, entry
);
1125 static const char *rrd_def_node
[] = {
1126 "DS:loadavg:GAUGE:120:0:U",
1127 "DS:maxcpu:GAUGE:120:0:U",
1128 "DS:cpu:GAUGE:120:0:U",
1129 "DS:iowait:GAUGE:120:0:U",
1130 "DS:memtotal:GAUGE:120:0:U",
1131 "DS:memused:GAUGE:120:0:U",
1132 "DS:swaptotal:GAUGE:120:0:U",
1133 "DS:swapused:GAUGE:120:0:U",
1134 "DS:roottotal:GAUGE:120:0:U",
1135 "DS:rootused:GAUGE:120:0:U",
1136 "DS:netin:DERIVE:120:0:U",
1137 "DS:netout:DERIVE:120:0:U",
1139 "RRA:AVERAGE:0.5:1:70", // 1 min avg - one hour
1140 "RRA:AVERAGE:0.5:30:70", // 30 min avg - one day
1141 "RRA:AVERAGE:0.5:180:70", // 3 hour avg - one week
1142 "RRA:AVERAGE:0.5:720:70", // 12 hour avg - one month
1143 "RRA:AVERAGE:0.5:10080:70", // 7 day avg - ony year
1145 "RRA:MAX:0.5:1:70", // 1 min max - one hour
1146 "RRA:MAX:0.5:30:70", // 30 min max - one day
1147 "RRA:MAX:0.5:180:70", // 3 hour max - one week
1148 "RRA:MAX:0.5:720:70", // 12 hour max - one month
1149 "RRA:MAX:0.5:10080:70", // 7 day max - ony year
1153 static const char *rrd_def_vm
[] = {
1154 "DS:maxcpu:GAUGE:120:0:U",
1155 "DS:cpu:GAUGE:120:0:U",
1156 "DS:maxmem:GAUGE:120:0:U",
1157 "DS:mem:GAUGE:120:0:U",
1158 "DS:maxdisk:GAUGE:120:0:U",
1159 "DS:disk:GAUGE:120:0:U",
1160 "DS:netin:DERIVE:120:0:U",
1161 "DS:netout:DERIVE:120:0:U",
1162 "DS:diskread:DERIVE:120:0:U",
1163 "DS:diskwrite:DERIVE:120:0:U",
1165 "RRA:AVERAGE:0.5:1:70", // 1 min avg - one hour
1166 "RRA:AVERAGE:0.5:30:70", // 30 min avg - one day
1167 "RRA:AVERAGE:0.5:180:70", // 3 hour avg - one week
1168 "RRA:AVERAGE:0.5:720:70", // 12 hour avg - one month
1169 "RRA:AVERAGE:0.5:10080:70", // 7 day avg - ony year
1171 "RRA:MAX:0.5:1:70", // 1 min max - one hour
1172 "RRA:MAX:0.5:30:70", // 30 min max - one day
1173 "RRA:MAX:0.5:180:70", // 3 hour max - one week
1174 "RRA:MAX:0.5:720:70", // 12 hour max - one month
1175 "RRA:MAX:0.5:10080:70", // 7 day max - ony year
1179 static const char *rrd_def_storage
[] = {
1180 "DS:total:GAUGE:120:0:U",
1181 "DS:used:GAUGE:120:0:U",
1183 "RRA:AVERAGE:0.5:1:70", // 1 min avg - one hour
1184 "RRA:AVERAGE:0.5:30:70", // 30 min avg - one day
1185 "RRA:AVERAGE:0.5:180:70", // 3 hour avg - one week
1186 "RRA:AVERAGE:0.5:720:70", // 12 hour avg - one month
1187 "RRA:AVERAGE:0.5:10080:70", // 7 day avg - ony year
1189 "RRA:MAX:0.5:1:70", // 1 min max - one hour
1190 "RRA:MAX:0.5:30:70", // 30 min max - one day
1191 "RRA:MAX:0.5:180:70", // 3 hour max - one week
1192 "RRA:MAX:0.5:720:70", // 12 hour max - one month
1193 "RRA:MAX:0.5:10080:70", // 7 day max - ony year
1197 #define RRDDIR "/var/lib/rrdcached/db"
1201 const char *filename
,
1203 const char *rrddef
[])
1205 /* start at day boundary */
1208 struct tm
*ltm
= localtime(&ctime
);
1214 if (rrd_create_r(filename
, 60, timelocal(ltm
), argcount
, rrddef
)) {
1215 cfs_message("RRD create error %s: %s", filename
, rrd_get_error());
1219 static inline const char *
1225 while (*data
&& found
< count
) {
1238 g_return_if_fail(key
!= NULL
);
1239 g_return_if_fail(data
!= NULL
);
1240 g_return_if_fail(len
> 0);
1241 g_return_if_fail(len
< 4096);
1243 static const char *rrdcsock
= "unix:/var/run/rrdcached.sock";
1246 if (rrdc_connect(rrdcsock
) != 0)
1249 char *filename
= NULL
;
1253 if (strncmp(key
, "pve2-node/", 10) == 0) {
1254 const char *node
= key
+ 10;
1258 if (strchr(node
, '/') != NULL
)
1261 if (strlen(node
) < 1)
1264 filename
= g_strdup_printf(RRDDIR
"/%s", key
);
1266 if (!g_file_test(filename
, G_FILE_TEST_EXISTS
)) {
1268 mkdir(RRDDIR
"/pve2-node", 0755);
1269 int argcount
= sizeof(rrd_def_node
)/sizeof(void*) - 1;
1270 create_rrd_file(filename
, argcount
, rrd_def_node
);
1273 } else if ((strncmp(key
, "pve2-vm/", 8) == 0) ||
1274 (strncmp(key
, "pve2.3-vm/", 10) == 0)) {
1277 if (strncmp(key
, "pve2-vm/", 8) == 0) {
1285 if (strchr(vmid
, '/') != NULL
)
1288 if (strlen(vmid
) < 1)
1291 filename
= g_strdup_printf(RRDDIR
"/%s/%s", "pve2-vm", vmid
);
1293 if (!g_file_test(filename
, G_FILE_TEST_EXISTS
)) {
1295 mkdir(RRDDIR
"/pve2-vm", 0755);
1296 int argcount
= sizeof(rrd_def_vm
)/sizeof(void*) - 1;
1297 create_rrd_file(filename
, argcount
, rrd_def_vm
);
1300 } else if (strncmp(key
, "pve2-storage/", 13) == 0) {
1301 const char *node
= key
+ 13;
1303 const char *storage
= node
;
1304 while (*storage
&& *storage
!= '/')
1307 if (*storage
!= '/' || ((storage
- node
) < 1))
1312 if (strchr(storage
, '/') != NULL
)
1315 if (strlen(storage
) < 1)
1318 filename
= g_strdup_printf(RRDDIR
"/%s", key
);
1320 if (!g_file_test(filename
, G_FILE_TEST_EXISTS
)) {
1322 mkdir(RRDDIR
"/pve2-storage", 0755);
1324 char *dir
= g_path_get_dirname(filename
);
1328 int argcount
= sizeof(rrd_def_storage
)/sizeof(void*) - 1;
1329 create_rrd_file(filename
, argcount
, rrd_def_storage
);
1336 const char *dp
= skip
? rrd_skip_data(data
, skip
) : data
;
1338 const char *update_args
[] = { dp
, NULL
};
1342 if ((status
= rrdc_update(filename
, 1, update_args
)) != 0) {
1343 cfs_message("RRDC update error %s: %d", filename
, status
);
1346 if (rrd_update_r(filename
, NULL
, 1, update_args
) != 0) {
1347 cfs_message("RRD update error %s: %s", filename
, rrd_get_error());
1353 if (rrd_update_r(filename
, NULL
, 1, update_args
) != 0) {
1354 cfs_message("RRD update error %s: %s", filename
, rrd_get_error());
1365 cfs_critical("RRD update error: unknown/wrong key %s", key
);
1375 rrdentry_t
*entry
= (rrdentry_t
*)value
;
1376 uint32_t ctime
= GPOINTER_TO_UINT(user_data
);
1378 int diff
= ctime
- entry
->time
;
1380 /* remove everything older than 5 minutes */
1383 return (diff
> expire
) ? TRUE
: FALSE
;
1386 static char *rrd_dump_buf
= NULL
;
1387 static time_t rrd_dump_last
= 0;
1390 cfs_rrd_dump(GString
*str
)
1394 g_mutex_lock (&mutex
);
1397 if (rrd_dump_buf
&& (ctime
- rrd_dump_last
) < 2) {
1398 g_string_assign(str
, rrd_dump_buf
);
1399 g_mutex_unlock (&mutex
);
1403 /* remove old data */
1404 g_hash_table_foreach_remove(cfs_status
.rrdhash
, rrd_entry_is_old
,
1405 GUINT_TO_POINTER(ctime
));
1407 g_string_set_size(str
, 0);
1409 GHashTableIter iter
;
1410 gpointer key
, value
;
1412 g_hash_table_iter_init (&iter
, cfs_status
.rrdhash
);
1414 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
1415 rrdentry_t
*entry
= (rrdentry_t
*)value
;
1416 g_string_append(str
, key
);
1417 g_string_append(str
, ":");
1418 g_string_append(str
, entry
->data
);
1419 g_string_append(str
, "\n");
1422 g_string_append_c(str
, 0); // never return undef
1424 rrd_dump_last
= ctime
;
1426 g_free(rrd_dump_buf
);
1427 rrd_dump_buf
= g_strdup(str
->str
);
1429 g_mutex_unlock (&mutex
);
1435 const char *nodename
,
1439 g_return_val_if_fail(iphash
!= NULL
, FALSE
);
1440 g_return_val_if_fail(nodename
!= NULL
, FALSE
);
1441 g_return_val_if_fail(ip
!= NULL
, FALSE
);
1442 g_return_val_if_fail(len
> 0, FALSE
);
1443 g_return_val_if_fail(len
< 256, FALSE
);
1444 g_return_val_if_fail(ip
[len
-1] == 0, FALSE
);
1446 char *oldip
= (char *)g_hash_table_lookup(iphash
, nodename
);
1448 if (!oldip
|| (strcmp(oldip
, ip
) != 0)) {
1449 cfs_status
.clinfo_version
++;
1450 g_hash_table_replace(iphash
, g_strdup(nodename
), g_strdup(ip
));
1458 GHashTable
*rrdhash
,
1463 g_return_val_if_fail(rrdhash
!= NULL
, FALSE
);
1464 g_return_val_if_fail(key
!= NULL
, FALSE
);
1465 g_return_val_if_fail(data
!= NULL
, FALSE
);
1466 g_return_val_if_fail(len
> 0, FALSE
);
1467 g_return_val_if_fail(len
< 4096, FALSE
);
1468 g_return_val_if_fail(data
[len
-1] == 0, FALSE
);
1471 if ((entry
= (rrdentry_t
*)g_hash_table_lookup(rrdhash
, key
))) {
1472 g_free(entry
->data
);
1473 entry
->data
= g_memdup2(data
, len
);
1475 entry
->time
= time(NULL
);
1477 rrdentry_t
*entry
= g_new0(rrdentry_t
, 1);
1479 entry
->key
= g_strdup(key
);
1480 entry
->data
= g_memdup2(data
, len
);
1482 entry
->time
= time(NULL
);
1484 g_hash_table_replace(rrdhash
, entry
->key
, entry
);
1487 update_rrd_data(key
, data
, len
);
1493 kvstore_send_update_message(
1499 if (!dfsm_is_initialized(dfsm
))
1502 struct iovec iov
[2];
1505 g_strlcpy(name
, key
, sizeof(name
));
1507 iov
[0].iov_base
= &name
;
1508 iov
[0].iov_len
= sizeof(name
);
1510 iov
[1].iov_base
= (char *)data
;
1511 iov
[1].iov_len
= len
;
1513 if (dfsm_send_message(dfsm
, KVSTORE_MESSAGE_UPDATE
, iov
, 2) == CS_OK
)
1519 static clog_entry_t
*
1520 kvstore_parse_log_message(
1524 g_return_val_if_fail(msg
!= NULL
, NULL
);
1526 if (msg_len
< sizeof(clog_entry_t
)) {
1527 cfs_critical("received short log message (%zu < %zu)", msg_len
, sizeof(clog_entry_t
));
1531 clog_entry_t
*entry
= (clog_entry_t
*)msg
;
1533 uint32_t size
= sizeof(clog_entry_t
) + entry
->node_len
+
1534 entry
->ident_len
+ entry
->tag_len
+ entry
->msg_len
;
1536 if (msg_len
!= size
) {
1537 cfs_critical("received log message with wrong size (%zu != %u)", msg_len
, size
);
1541 char *msgptr
= entry
->data
;
1543 if (*((char *)msgptr
+ entry
->node_len
- 1)) {
1544 cfs_critical("unterminated string in log message");
1547 msgptr
+= entry
->node_len
;
1549 if (*((char *)msgptr
+ entry
->ident_len
- 1)) {
1550 cfs_critical("unterminated string in log message");
1553 msgptr
+= entry
->ident_len
;
1555 if (*((char *)msgptr
+ entry
->tag_len
- 1)) {
1556 cfs_critical("unterminated string in log message");
1559 msgptr
+= entry
->tag_len
;
1561 if (*((char *)msgptr
+ entry
->msg_len
- 1)) {
1562 cfs_critical("unterminated string in log message");
1570 kvstore_parse_update_message(
1574 gconstpointer
*data
,
1577 g_return_val_if_fail(msg
!= NULL
, FALSE
);
1578 g_return_val_if_fail(key
!= NULL
, FALSE
);
1579 g_return_val_if_fail(data
!= NULL
, FALSE
);
1580 g_return_val_if_fail(len
!= NULL
, FALSE
);
1582 if (msg_len
< 256) {
1583 cfs_critical("received short kvstore message (%zu < 256)", msg_len
);
1587 /* test if key is null terminated */
1589 for (i
= 0; i
< 256; i
++)
1590 if (((char *)msg
)[i
] == 0)
1597 *len
= msg_len
- 256;
1599 *data
= (char *) msg
+ 256;
1605 cfs_create_status_msg(
1607 const char *nodename
,
1610 g_return_val_if_fail(str
!= NULL
, -EINVAL
);
1611 g_return_val_if_fail(key
!= NULL
, -EINVAL
);
1615 GHashTable
*kvhash
= NULL
;
1617 g_mutex_lock (&mutex
);
1619 if (!nodename
|| !nodename
[0] || !strcmp(nodename
, cfs
.nodename
)) {
1620 kvhash
= cfs_status
.kvhash
;
1621 } else if (cfs_status
.clinfo
&& cfs_status
.clinfo
->nodes_byname
) {
1622 cfs_clnode_t
*clnode
;
1623 if ((clnode
= g_hash_table_lookup(cfs_status
.clinfo
->nodes_byname
, nodename
)))
1624 kvhash
= clnode
->kvhash
;
1628 if (kvhash
&& (entry
= (kventry_t
*)g_hash_table_lookup(kvhash
, key
))) {
1629 g_string_append_len(str
, entry
->data
, entry
->len
);
1633 g_mutex_unlock (&mutex
);
1644 g_return_val_if_fail(key
!= NULL
, FALSE
);
1645 g_return_val_if_fail(data
!= NULL
, FALSE
);
1646 g_return_val_if_fail(cfs_status
.kvhash
!= NULL
, FALSE
);
1648 if (len
> CFS_MAX_STATUS_SIZE
)
1651 g_mutex_lock (&mutex
);
1655 if (strncmp(key
, "rrd/", 4) == 0) {
1656 res
= rrdentry_hash_set(cfs_status
.rrdhash
, key
+ 4, data
, len
);
1657 } else if (!strcmp(key
, "nodeip")) {
1658 res
= nodeip_hash_set(cfs_status
.iphash
, cfs
.nodename
, data
, len
);
1660 res
= kventry_hash_set(cfs_status
.kvhash
, key
, data
, len
);
1662 g_mutex_unlock (&mutex
);
1664 if (cfs_status
.kvstore
)
1665 kvstore_send_update_message(cfs_status
.kvstore
, key
, data
, len
);
1667 return res
? 0 : -ENOMEM
;
1671 cfs_kvstore_node_set(
1677 g_return_val_if_fail(nodeid
!= 0, FALSE
);
1678 g_return_val_if_fail(key
!= NULL
, FALSE
);
1679 g_return_val_if_fail(data
!= NULL
, FALSE
);
1681 g_mutex_lock (&mutex
);
1683 if (!cfs_status
.clinfo
|| !cfs_status
.clinfo
->nodes_byid
)
1684 goto ret
; /* ignore */
1686 cfs_clnode_t
*clnode
= g_hash_table_lookup(cfs_status
.clinfo
->nodes_byid
, &nodeid
);
1688 goto ret
; /* ignore */
1690 cfs_debug("got node %d status update %s", nodeid
, key
);
1692 if (strncmp(key
, "rrd/", 4) == 0) {
1693 rrdentry_hash_set(cfs_status
.rrdhash
, key
+ 4, data
, len
);
1694 } else if (!strcmp(key
, "nodeip")) {
1695 nodeip_hash_set(cfs_status
.iphash
, clnode
->name
, data
, len
);
1697 if (!clnode
->kvhash
) {
1698 if (!(clnode
->kvhash
= kventry_hash_new())) {
1699 goto ret
; /*ignore */
1703 kventry_hash_set(clnode
->kvhash
, key
, data
, len
);
1707 g_mutex_unlock (&mutex
);
1713 cfs_kvstore_sync(void)
1715 g_return_val_if_fail(cfs_status
.kvhash
!= NULL
, FALSE
);
1716 g_return_val_if_fail(cfs_status
.kvstore
!= NULL
, FALSE
);
1718 gboolean res
= TRUE
;
1720 g_mutex_lock (&mutex
);
1722 GHashTable
*ht
= cfs_status
.kvhash
;
1723 GHashTableIter iter
;
1724 gpointer key
, value
;
1726 g_hash_table_iter_init (&iter
, ht
);
1728 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
1729 kventry_t
*entry
= (kventry_t
*)value
;
1730 kvstore_send_update_message(cfs_status
.kvstore
, entry
->key
, entry
->data
, entry
->len
);
1733 g_mutex_unlock (&mutex
);
1750 g_return_val_if_fail(dfsm
!= NULL
, -1);
1751 g_return_val_if_fail(msg
!= NULL
, -1);
1752 g_return_val_if_fail(res_ptr
!= NULL
, -1);
1754 /* ignore message for ourself */
1755 if (dfsm_nodeid_is_local(dfsm
, nodeid
, pid
))
1758 if (msg_type
== KVSTORE_MESSAGE_UPDATE
) {
1762 if (kvstore_parse_update_message(msg
, msg_len
, &key
, &data
, &len
)) {
1763 cfs_kvstore_node_set(nodeid
, key
, data
, len
);
1765 cfs_critical("cant parse update message");
1767 } else if (msg_type
== KVSTORE_MESSAGE_LOG
) {
1768 cfs_message("received log"); // fixme: remove
1769 const clog_entry_t
*entry
;
1770 if ((entry
= kvstore_parse_log_message(msg
, msg_len
))) {
1771 clusterlog_insert(cfs_status
.clusterlog
, entry
);
1773 cfs_critical("cant parse log message");
1776 cfs_critical("received unknown message type %d\n", msg_type
);
1793 const struct cpg_address
*member_list
,
1794 size_t member_list_entries
)
1796 g_return_if_fail(dfsm
!= NULL
);
1797 g_return_if_fail(member_list
!= NULL
);
1799 cfs_debug("enter %s", __func__
);
1801 g_mutex_lock (&mutex
);
1803 cfs_clinfo_t
*clinfo
= cfs_status
.clinfo
;
1805 if (clinfo
&& clinfo
->nodes_byid
) {
1807 GHashTable
*ht
= clinfo
->nodes_byid
;
1808 GHashTableIter iter
;
1809 gpointer key
, value
;
1811 g_hash_table_iter_init (&iter
, ht
);
1813 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
1814 cfs_clnode_t
*node
= (cfs_clnode_t
*)value
;
1815 node
->online
= FALSE
;
1818 for (int i
= 0; i
< member_list_entries
; i
++) {
1820 if ((node
= g_hash_table_lookup(clinfo
->nodes_byid
, &member_list
[i
].nodeid
))) {
1821 node
->online
= TRUE
;
1825 cfs_status
.clinfo_version
++;
1828 g_mutex_unlock (&mutex
);
1835 unsigned int *res_len
)
1837 g_return_val_if_fail(dfsm
!= NULL
, NULL
);
1839 gpointer msg
= clusterlog_get_state(cfs_status
.clusterlog
, res_len
);
1845 dfsm_process_update(
1848 dfsm_sync_info_t
*syncinfo
,
1854 cfs_critical("%s: received unexpected update message", __func__
);
1860 dfsm_process_state_update(
1863 dfsm_sync_info_t
*syncinfo
)
1865 g_return_val_if_fail(dfsm
!= NULL
, -1);
1866 g_return_val_if_fail(syncinfo
!= NULL
, -1);
1868 clog_base_t
*clog
[syncinfo
->node_count
];
1870 int local_index
= -1;
1871 for (int i
= 0; i
< syncinfo
->node_count
; i
++) {
1872 dfsm_node_info_t
*ni
= &syncinfo
->nodes
[i
];
1875 if (syncinfo
->local
== ni
)
1878 clog_base_t
*base
= (clog_base_t
*)ni
->state
;
1879 if (ni
->state_len
> 8 && ni
->state_len
== clog_size(base
)) {
1880 clog
[i
] = ni
->state
;
1882 cfs_critical("received log with wrong size %u", ni
->state_len
);
1887 if (!clusterlog_merge(cfs_status
.clusterlog
, clog
, syncinfo
->node_count
, local_index
)) {
1888 cfs_critical("unable to merge log files");
1900 dfsm_sync_info_t
*syncinfo
)
1902 g_return_val_if_fail(dfsm
!= NULL
, -1);
1903 g_return_val_if_fail(syncinfo
!= NULL
, -1);
1909 dfsm_synced(dfsm_t
*dfsm
)
1911 g_return_if_fail(dfsm
!= NULL
);
1913 char *ip
= (char *)g_hash_table_lookup(cfs_status
.iphash
, cfs
.nodename
);
1917 cfs_status_set("nodeip", ip
, strlen(ip
) + 1);
1924 dfsm_sync_info_t
*syncinfo
)
1929 static dfsm_callbacks_t kvstore_dfsm_callbacks
= {
1930 .dfsm_deliver_fn
= dfsm_deliver
,
1931 .dfsm_confchg_fn
= dfsm_confchg
,
1933 .dfsm_get_state_fn
= dfsm_get_state
,
1934 .dfsm_process_state_update_fn
= dfsm_process_state_update
,
1935 .dfsm_process_update_fn
= dfsm_process_update
,
1936 .dfsm_commit_fn
= dfsm_commit
,
1937 .dfsm_cleanup_fn
= dfsm_cleanup
,
1938 .dfsm_synced_fn
= dfsm_synced
,
1942 cfs_status_dfsm_new(void)
1944 g_mutex_lock (&mutex
);
1946 cfs_status
.kvstore
= dfsm_new(NULL
, KVSTORE_CPG_GROUP_NAME
, G_LOG_DOMAIN
,
1947 0, &kvstore_dfsm_callbacks
);
1948 g_mutex_unlock (&mutex
);
1950 return cfs_status
.kvstore
;
1954 cfs_is_quorate(void)
1956 g_mutex_lock (&mutex
);
1957 gboolean res
= cfs_status
.quorate
;
1958 g_mutex_unlock (&mutex
);
1968 g_mutex_lock (&mutex
);
1970 uint32_t prev_quorate
= cfs_status
.quorate
;
1971 cfs_status
.quorate
= quorate
;
1973 if (!prev_quorate
&& cfs_status
.quorate
) {
1975 cfs_message("node has quorum");
1978 if (prev_quorate
&& !cfs_status
.quorate
) {
1980 cfs_message("node lost quorum");
1983 g_mutex_unlock (&mutex
);