2 Copyright (C) 2010 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
= "priv/shadow.cfg" },
86 { .path
= "priv/acme/plugins.cfg" },
87 { .path
= "priv/tfa.cfg" },
88 { .path
= "priv/token.cfg" },
89 { .path
= "datacenter.cfg" },
90 { .path
= "vzdump.cron" },
91 { .path
= "ha/crm_commands" },
92 { .path
= "ha/manager_status" },
93 { .path
= "ha/resources.cfg" },
94 { .path
= "ha/groups.cfg" },
95 { .path
= "ha/fence.cfg" },
96 { .path
= "status.cfg" },
97 { .path
= "replication.cfg" },
98 { .path
= "ceph.conf" },
99 { .path
= "sdn/vnets.cfg" },
100 { .path
= "sdn/vnets.cfg.new" },
101 { .path
= "sdn/zones.cfg" },
102 { .path
= "sdn/zones.cfg.new" },
103 { .path
= "sdn/controllers.cfg" },
104 { .path
= "sdn/controllers.cfg.new" },
105 { .path
= "virtual-guest/cpu-models.conf" },
115 cfs_clinfo_t
*clinfo
;
116 uint32_t clinfo_version
;
119 uint32_t vmlist_version
;
126 GHashTable
*memdb_changes
;
128 clusterlog_t
*clusterlog
;
131 static cfs_status_t cfs_status
;
143 uint32_t cman_version
;
145 GHashTable
*nodes_byid
;
146 GHashTable
*nodes_byname
;
150 g_int32_hash (gconstpointer v
)
152 return *(const uint32_t *) v
;
156 g_int32_equal (gconstpointer v1
,
159 return *((const uint32_t*) v1
) == *((const uint32_t*) v2
);
162 static void vminfo_free(vminfo_t
*vminfo
)
164 g_return_if_fail(vminfo
!= NULL
);
166 if (vminfo
->nodename
)
167 g_free(vminfo
->nodename
);
173 static const char *vminfo_type_to_string(vminfo_t
*vminfo
)
175 if (vminfo
->vmtype
== VMTYPE_QEMU
) {
177 } else if (vminfo
->vmtype
== VMTYPE_OPENVZ
) {
179 } else if (vminfo
->vmtype
== VMTYPE_LXC
) {
186 static const char *vminfo_type_to_path_type(vminfo_t
*vminfo
)
188 if (vminfo
->vmtype
== VMTYPE_QEMU
) {
189 return "qemu-server"; // special case..
191 return vminfo_type_to_string(vminfo
);
195 int vminfo_to_path(vminfo_t
*vminfo
, GString
*path
)
197 g_return_val_if_fail(vminfo
!= NULL
, -1);
198 g_return_val_if_fail(path
!= NULL
, -1);
200 if (!vminfo
->nodename
)
203 const char *type
= vminfo_type_to_path_type(vminfo
);
204 g_string_printf(path
, "/nodes/%s/%s/%u.conf", vminfo
->nodename
, type
, vminfo
->vmid
);
209 void cfs_clnode_destroy(
210 cfs_clnode_t
*clnode
)
212 g_return_if_fail(clnode
!= NULL
);
215 g_hash_table_destroy(clnode
->kvhash
);
218 g_free(clnode
->name
);
223 cfs_clnode_t
*cfs_clnode_new(
228 g_return_val_if_fail(name
!= NULL
, NULL
);
230 cfs_clnode_t
*clnode
= g_new0(cfs_clnode_t
, 1);
234 clnode
->name
= g_strdup(name
);
235 clnode
->nodeid
= nodeid
;
236 clnode
->votes
= votes
;
241 gboolean
cfs_clinfo_destroy(
242 cfs_clinfo_t
*clinfo
)
244 g_return_val_if_fail(clinfo
!= NULL
, FALSE
);
246 if (clinfo
->cluster_name
)
247 g_free(clinfo
->cluster_name
);
249 if (clinfo
->nodes_byname
)
250 g_hash_table_destroy(clinfo
->nodes_byname
);
252 if (clinfo
->nodes_byid
)
253 g_hash_table_destroy(clinfo
->nodes_byid
);
260 cfs_clinfo_t
*cfs_clinfo_new(
261 const char *cluster_name
,
262 uint32_t cman_version
)
264 g_return_val_if_fail(cluster_name
!= NULL
, NULL
);
266 cfs_clinfo_t
*clinfo
= g_new0(cfs_clinfo_t
, 1);
270 clinfo
->cluster_name
= g_strdup(cluster_name
);
271 clinfo
->cman_version
= cman_version
;
273 if (!(clinfo
->nodes_byid
= g_hash_table_new_full(
274 g_int32_hash
, g_int32_equal
, NULL
,
275 (GDestroyNotify
)cfs_clnode_destroy
)))
278 if (!(clinfo
->nodes_byname
= g_hash_table_new(g_str_hash
, g_str_equal
)))
284 cfs_clinfo_destroy(clinfo
);
289 gboolean
cfs_clinfo_add_node(
290 cfs_clinfo_t
*clinfo
,
291 cfs_clnode_t
*clnode
)
293 g_return_val_if_fail(clinfo
!= NULL
, FALSE
);
294 g_return_val_if_fail(clnode
!= NULL
, FALSE
);
296 g_hash_table_replace(clinfo
->nodes_byid
, &clnode
->nodeid
, clnode
);
297 g_hash_table_replace(clinfo
->nodes_byname
, clnode
->name
, clnode
);
303 cfs_create_memberlist_msg(
306 g_return_val_if_fail(str
!= NULL
, -EINVAL
);
308 g_mutex_lock (&mutex
);
310 g_string_append_printf(str
,"{\n");
314 cfs_clinfo_t
*clinfo
= cfs_status
.clinfo
;
316 if (clinfo
&& clinfo
->nodes_byid
)
317 nodecount
= g_hash_table_size(clinfo
->nodes_byid
);
320 g_string_append_printf(str
, "\"nodename\": \"%s\",\n", cfs
.nodename
);
321 g_string_append_printf(str
, "\"version\": %u,\n", cfs_status
.clinfo_version
);
323 g_string_append_printf(str
, "\"cluster\": { ");
324 g_string_append_printf(str
, "\"name\": \"%s\", \"version\": %d, "
325 "\"nodes\": %d, \"quorate\": %d ",
326 clinfo
->cluster_name
, clinfo
->cman_version
,
327 nodecount
, cfs_status
.quorate
);
329 g_string_append_printf(str
,"},\n");
330 g_string_append_printf(str
,"\"nodelist\": {\n");
332 GHashTable
*ht
= clinfo
->nodes_byid
;
336 g_hash_table_iter_init (&iter
, ht
);
339 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
340 cfs_clnode_t
*node
= (cfs_clnode_t
*)value
;
341 if (i
) g_string_append_printf(str
, ",\n");
344 g_string_append_printf(str
, " \"%s\": { \"id\": %d, \"online\": %d",
345 node
->name
, node
->nodeid
, node
->online
);
348 char *ip
= (char *)g_hash_table_lookup(cfs_status
.iphash
, node
->name
);
350 g_string_append_printf(str
, ", \"ip\": \"%s\"", ip
);
353 g_string_append_printf(str
, "}");
356 g_string_append_printf(str
,"\n }\n");
358 g_string_append_printf(str
, "\"nodename\": \"%s\",\n", cfs
.nodename
);
359 g_string_append_printf(str
, "\"version\": %u\n", cfs_status
.clinfo_version
);
362 g_string_append_printf(str
,"}\n");
364 g_mutex_unlock (&mutex
);
370 kventry_free(kventry_t
*entry
)
372 g_return_if_fail(entry
!= NULL
);
380 kventry_hash_new(void)
382 return g_hash_table_new_full(g_str_hash
, g_str_equal
, NULL
,
383 (GDestroyNotify
)kventry_free
);
387 rrdentry_free(rrdentry_t
*entry
)
389 g_return_if_fail(entry
!= NULL
);
397 rrdentry_hash_new(void)
399 return g_hash_table_new_full(g_str_hash
, g_str_equal
, NULL
,
400 (GDestroyNotify
)rrdentry_free
);
404 cfs_cluster_log_dump(GString
*str
, const char *user
, guint max_entries
)
406 clusterlog_dump(cfs_status
.clusterlog
, str
, user
, max_entries
);
410 cfs_cluster_log(clog_entry_t
*entry
)
412 g_return_if_fail(entry
!= NULL
);
414 clusterlog_insert(cfs_status
.clusterlog
, entry
);
416 if (cfs_status
.kvstore
) {
418 iov
[0].iov_base
= (char *)entry
;
419 iov
[0].iov_len
= clog_entry_size(entry
);
421 if (dfsm_is_initialized(cfs_status
.kvstore
))
422 dfsm_send_message(cfs_status
.kvstore
, KVSTORE_MESSAGE_LOG
, iov
, 1);
426 void cfs_status_init(void)
428 g_mutex_lock (&mutex
);
430 cfs_status
.start_time
= time(NULL
);
432 cfs_status
.vmlist
= vmlist_hash_new();
434 cfs_status
.kvhash
= kventry_hash_new();
436 cfs_status
.rrdhash
= rrdentry_hash_new();
438 cfs_status
.iphash
= g_hash_table_new_full(g_str_hash
, g_str_equal
, g_free
, g_free
);
440 cfs_status
.memdb_changes
= g_hash_table_new(g_str_hash
, g_str_equal
);
442 for (int i
= 0; i
< G_N_ELEMENTS(memdb_change_array
); i
++) {
443 g_hash_table_replace(cfs_status
.memdb_changes
,
444 memdb_change_array
[i
].path
,
445 &memdb_change_array
[i
]);
448 cfs_status
.clusterlog
= clusterlog_new();
451 clusterlog_add(cfs_status
.clusterlog
, "root", "cluster", getpid(),
452 LOG_INFO
, "starting cluster log");
454 g_mutex_unlock (&mutex
);
457 void cfs_status_cleanup(void)
459 g_mutex_lock (&mutex
);
461 cfs_status
.clinfo_version
++;
463 if (cfs_status
.clinfo
) {
464 cfs_clinfo_destroy(cfs_status
.clinfo
);
465 cfs_status
.clinfo
= NULL
;
468 if (cfs_status
.vmlist
) {
469 g_hash_table_destroy(cfs_status
.vmlist
);
470 cfs_status
.vmlist
= NULL
;
473 if (cfs_status
.kvhash
) {
474 g_hash_table_destroy(cfs_status
.kvhash
);
475 cfs_status
.kvhash
= NULL
;
478 if (cfs_status
.rrdhash
) {
479 g_hash_table_destroy(cfs_status
.rrdhash
);
480 cfs_status
.rrdhash
= NULL
;
483 if (cfs_status
.iphash
) {
484 g_hash_table_destroy(cfs_status
.iphash
);
485 cfs_status
.iphash
= NULL
;
488 if (cfs_status
.clusterlog
)
489 clusterlog_destroy(cfs_status
.clusterlog
);
491 g_mutex_unlock (&mutex
);
494 void cfs_status_set_clinfo(
495 cfs_clinfo_t
*clinfo
)
497 g_return_if_fail(clinfo
!= NULL
);
499 g_mutex_lock (&mutex
);
501 cfs_status
.clinfo_version
++;
503 cfs_clinfo_t
*old
= cfs_status
.clinfo
;
505 cfs_status
.clinfo
= clinfo
;
507 cfs_message("update cluster info (cluster name %s, version = %d)",
508 clinfo
->cluster_name
, clinfo
->cman_version
);
511 if (old
&& old
->nodes_byid
&& clinfo
->nodes_byid
) {
513 GHashTable
*ht
= clinfo
->nodes_byid
;
517 g_hash_table_iter_init (&iter
, ht
);
519 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
520 cfs_clnode_t
*node
= (cfs_clnode_t
*)value
;
521 cfs_clnode_t
*oldnode
;
522 if ((oldnode
= g_hash_table_lookup(old
->nodes_byid
, key
))) {
523 node
->online
= oldnode
->online
;
524 node
->kvhash
= oldnode
->kvhash
;
525 oldnode
->kvhash
= NULL
;
532 cfs_clinfo_destroy(old
);
535 g_mutex_unlock (&mutex
);
539 dump_kvstore_versions(
542 const char *nodename
)
544 g_return_if_fail(kvhash
!= NULL
);
545 g_return_if_fail(str
!= NULL
);
546 g_return_if_fail(nodename
!= NULL
);
548 GHashTable
*ht
= kvhash
;
552 g_string_append_printf(str
, "\"%s\": {\n", nodename
);
554 g_hash_table_iter_init (&iter
, ht
);
557 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
558 kventry_t
*entry
= (kventry_t
*)value
;
559 if (i
) g_string_append_printf(str
, ",\n");
561 g_string_append_printf(str
,"\"%s\": %u", entry
->key
, entry
->version
);
564 g_string_append_printf(str
, "}\n");
568 cfs_create_version_msg(GString
*str
)
570 g_return_val_if_fail(str
!= NULL
, -EINVAL
);
572 g_mutex_lock (&mutex
);
574 g_string_append_printf(str
,"{\n");
576 g_string_append_printf(str
, "\"starttime\": %lu,\n", (unsigned long)cfs_status
.start_time
);
578 g_string_append_printf(str
, "\"clinfo\": %u,\n", cfs_status
.clinfo_version
);
580 g_string_append_printf(str
, "\"vmlist\": %u,\n", cfs_status
.vmlist_version
);
582 for (int i
= 0; i
< G_N_ELEMENTS(memdb_change_array
); i
++) {
583 g_string_append_printf(str
, "\"%s\": %u,\n",
584 memdb_change_array
[i
].path
,
585 memdb_change_array
[i
].version
);
588 g_string_append_printf(str
, "\"kvstore\": {\n");
590 dump_kvstore_versions(str
, cfs_status
.kvhash
, cfs
.nodename
);
592 cfs_clinfo_t
*clinfo
= cfs_status
.clinfo
;
594 if (clinfo
&& clinfo
->nodes_byid
) {
595 GHashTable
*ht
= clinfo
->nodes_byid
;
599 g_hash_table_iter_init (&iter
, ht
);
601 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
602 cfs_clnode_t
*node
= (cfs_clnode_t
*)value
;
605 g_string_append_printf(str
, ",\n");
606 dump_kvstore_versions(str
, node
->kvhash
, node
->name
);
610 g_string_append_printf(str
,"}\n");
612 g_string_append_printf(str
,"}\n");
614 g_mutex_unlock (&mutex
);
620 vmlist_hash_new(void)
622 return g_hash_table_new_full(g_int_hash
, g_int_equal
, NULL
,
623 (GDestroyNotify
)vminfo_free
);
627 vmlist_hash_insert_vm(
631 const char *nodename
,
634 g_return_val_if_fail(vmlist
!= NULL
, FALSE
);
635 g_return_val_if_fail(nodename
!= NULL
, FALSE
);
636 g_return_val_if_fail(vmid
!= 0, FALSE
);
637 g_return_val_if_fail(vmtype
== VMTYPE_QEMU
|| vmtype
== VMTYPE_OPENVZ
||
638 vmtype
== VMTYPE_LXC
, FALSE
);
640 if (!replace
&& g_hash_table_lookup(vmlist
, &vmid
)) {
641 cfs_critical("detected duplicate VMID %d", vmid
);
645 vminfo_t
*vminfo
= g_new0(vminfo_t
, 1);
648 vminfo
->vmtype
= vmtype
;
649 vminfo
->nodename
= g_strdup(nodename
);
651 vminfo
->version
= ++vminfo_version_counter
;
653 g_hash_table_replace(vmlist
, &vminfo
->vmid
, vminfo
);
662 const char *nodename
)
664 g_return_if_fail(cfs_status
.vmlist
!= NULL
);
665 g_return_if_fail(nodename
!= NULL
);
666 g_return_if_fail(vmid
!= 0);
667 g_return_if_fail(vmtype
== VMTYPE_QEMU
|| vmtype
== VMTYPE_OPENVZ
||
668 vmtype
== VMTYPE_LXC
);
670 cfs_debug("vmlist_register_vm: %s/%u %d", nodename
, vmid
, vmtype
);
672 g_mutex_lock (&mutex
);
674 cfs_status
.vmlist_version
++;
676 vmlist_hash_insert_vm(cfs_status
.vmlist
, vmtype
, vmid
, nodename
, TRUE
);
678 g_mutex_unlock (&mutex
);
682 vmlist_different_vm_exists(
685 const char *nodename
)
687 g_return_val_if_fail(cfs_status
.vmlist
!= NULL
, FALSE
);
688 g_return_val_if_fail(vmid
!= 0, FALSE
);
690 gboolean res
= FALSE
;
692 g_mutex_lock (&mutex
);
695 if ((vminfo
= (vminfo_t
*)g_hash_table_lookup(cfs_status
.vmlist
, &vmid
))) {
696 if (!(vminfo
->vmtype
== vmtype
&& strcmp(vminfo
->nodename
, nodename
) == 0))
699 g_mutex_unlock (&mutex
);
708 g_return_val_if_fail(cfs_status
.vmlist
!= NULL
, FALSE
);
709 g_return_val_if_fail(vmid
!= 0, FALSE
);
711 g_mutex_lock (&mutex
);
713 gpointer res
= g_hash_table_lookup(cfs_status
.vmlist
, &vmid
);
715 g_mutex_unlock (&mutex
);
724 g_return_if_fail(cfs_status
.vmlist
!= NULL
);
725 g_return_if_fail(vmid
!= 0);
727 g_mutex_lock (&mutex
);
729 cfs_status
.vmlist_version
++;
731 g_hash_table_remove(cfs_status
.vmlist
, &vmid
);
733 g_mutex_unlock (&mutex
);
736 void cfs_status_set_vmlist(
739 g_return_if_fail(vmlist
!= NULL
);
741 g_mutex_lock (&mutex
);
743 cfs_status
.vmlist_version
++;
745 if (cfs_status
.vmlist
)
746 g_hash_table_destroy(cfs_status
.vmlist
);
748 cfs_status
.vmlist
= vmlist
;
750 g_mutex_unlock (&mutex
);
754 cfs_create_vmlist_msg(GString
*str
)
756 g_return_val_if_fail(cfs_status
.vmlist
!= NULL
, -EINVAL
);
757 g_return_val_if_fail(str
!= NULL
, -EINVAL
);
759 g_mutex_lock (&mutex
);
761 g_string_append_printf(str
,"{\n");
763 GHashTable
*ht
= cfs_status
.vmlist
;
765 guint count
= g_hash_table_size(ht
);
768 g_string_append_printf(str
,"\"version\": %u\n", cfs_status
.vmlist_version
);
770 g_string_append_printf(str
,"\"version\": %u,\n", cfs_status
.vmlist_version
);
772 g_string_append_printf(str
,"\"ids\": {\n");
777 g_hash_table_iter_init (&iter
, ht
);
780 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
781 vminfo_t
*vminfo
= (vminfo_t
*)value
;
782 const char *type
= vminfo_type_to_string(vminfo
);
785 g_string_append_printf(str
, ",\n");
788 g_string_append_printf(str
,"\"%u\": { \"node\": \"%s\", \"type\": \"%s\", \"version\": %u }",
789 vminfo
->vmid
, vminfo
->nodename
, type
, vminfo
->version
);
792 g_string_append_printf(str
,"}\n");
794 g_string_append_printf(str
,"\n}\n");
796 g_mutex_unlock (&mutex
);
801 // checks the conf for a line starting with '$prop:' and returns the value
802 // afterwards, whitout initial whitespace(s), we only deal with the format
803 // restricion imposed by our perl VM config parser, main reference is
804 // PVE::QemuServer::parse_vm_config this allows to be very fast and still
806 // main restrictions used for our advantage is the properties match reges:
807 // ($line =~ m/^([a-z][a-z_]*\d*):\s*(.+?)\s*$/) from parse_vm_config
808 // currently we only look at the current configuration in place, i.e., *no*
809 // snapshort and *no* pending changes
811 _get_property_value(char *conf
, int conf_size
, const char *prop
, int prop_len
)
813 const char *const conf_end
= conf
+ conf_size
;
815 size_t remaining_size
;
817 char *next_newline
= memchr(conf
, '\n', conf_size
);
818 if (next_newline
== NULL
) {
819 return NULL
; // valid property lines end with \n, but none in the config
821 *next_newline
= '\0';
823 while (line
!= NULL
) {
824 if (!line
[0]) goto next
;
826 // snapshot or pending section start, but nothing found yet -> not found
827 if (line
[0] == '[') return NULL
;
828 // properties start with /^[a-z]/, so continue early if not
829 if (line
[0] < 'a' || line
[0] > 'z') goto next
;
831 int line_len
= strlen(line
);
832 if (line_len
<= prop_len
+ 1) goto next
;
834 if (line
[prop_len
] == ':' && memcmp(line
, prop
, prop_len
) == 0) { // found
835 char *v_start
= &line
[prop_len
+ 1];
837 // drop initial value whitespaces here already
838 while (*v_start
&& isspace(*v_start
)) v_start
++;
840 if (!*v_start
) return NULL
;
842 char *v_end
= &line
[line_len
- 1];
843 while (v_end
> v_start
&& isspace(*v_end
)) v_end
--;
849 line
= next_newline
+ 1;
850 remaining_size
= conf_end
- line
;
851 if (remaining_size
<= prop_len
) {
854 next_newline
= memchr(line
, '\n', remaining_size
);
855 if (next_newline
== NULL
) {
856 return NULL
; // valid property lines end with \n, but none in the config
858 *next_newline
= '\0';
861 return NULL
; // not found
865 _g_str_append_kv_jsonescaped(GString
*str
, const char *k
, const char *v
)
867 g_string_append_printf(str
, "\"%s\": \"", k
);
870 if (*v
== '\\' || *v
== '"') {
871 g_string_append_c(str
, '\\');
873 g_string_append_c(str
, *v
);
876 g_string_append_c(str
, '"');
880 cfs_create_guest_conf_property_msg(GString
*str
, memdb_t
*memdb
, const char *prop
, uint32_t vmid
)
882 g_return_val_if_fail(cfs_status
.vmlist
!= NULL
, -EINVAL
);
883 g_return_val_if_fail(str
!= NULL
, -EINVAL
);
885 int prop_len
= strlen(prop
);
887 GString
*path
= NULL
;
889 // Prelock &memdb->mutex in order to enable the usage of memdb_read_nolock
890 // to prevent Deadlocks as in #2553
891 g_mutex_lock (&memdb
->mutex
);
892 g_mutex_lock (&mutex
);
894 g_string_printf(str
,"{\n");
896 GHashTable
*ht
= cfs_status
.vmlist
;
898 if (!g_hash_table_size(ht
)) {
902 path
= g_string_sized_new(256);
904 vminfo_t
*vminfo
= (vminfo_t
*) g_hash_table_lookup(cfs_status
.vmlist
, &vmid
);
905 if (vminfo
== NULL
) goto enoent
;
907 if (!vminfo_to_path(vminfo
, path
)) goto err
;
909 // use memdb_read_nolock because lock is handled here
910 int size
= memdb_read_nolock(memdb
, path
->str
, &tmp
);
911 if (tmp
== NULL
) goto err
;
912 if (size
<= prop_len
) goto ret
;
914 char *val
= _get_property_value(tmp
, size
, prop
, prop_len
);
915 if (val
== NULL
) goto ret
;
917 g_string_append_printf(str
, "\"%u\":{", vmid
);
918 _g_str_append_kv_jsonescaped(str
, prop
, val
);
919 g_string_append_c(str
, '}');
923 g_hash_table_iter_init (&iter
, ht
);
927 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
928 vminfo_t
*vminfo
= (vminfo_t
*)value
;
930 if (!vminfo_to_path(vminfo
, path
)) goto err
;
932 g_free(tmp
); // no-op if already null
934 // use memdb_read_nolock because lock is handled here
935 int size
= memdb_read_nolock(memdb
, path
->str
, &tmp
);
936 if (tmp
== NULL
|| size
<= prop_len
) continue;
938 char *val
= _get_property_value(tmp
, size
, prop
, prop_len
);
939 if (val
== NULL
) continue;
941 if (!first
) g_string_append_printf(str
, ",\n");
944 g_string_append_printf(str
, "\"%u\":{", vminfo
->vmid
);
945 _g_str_append_kv_jsonescaped(str
, prop
, val
);
946 g_string_append_c(str
, '}');
952 g_string_free(path
, TRUE
);
954 g_string_append_printf(str
,"\n}\n");
955 g_mutex_unlock (&mutex
);
956 g_mutex_unlock (&memdb
->mutex
);
967 record_memdb_change(const char *path
)
969 g_return_if_fail(cfs_status
.memdb_changes
!= 0);
973 if ((ce
= (memdb_change_t
*)g_hash_table_lookup(cfs_status
.memdb_changes
, path
))) {
979 record_memdb_reload(void)
981 for (int i
= 0; i
< G_N_ELEMENTS(memdb_change_array
); i
++) {
982 memdb_change_array
[i
].version
++;
993 g_return_val_if_fail(kvhash
!= NULL
, FALSE
);
994 g_return_val_if_fail(key
!= NULL
, FALSE
);
995 g_return_val_if_fail(data
!= NULL
, FALSE
);
999 g_hash_table_remove(kvhash
, key
);
1000 } else if ((entry
= (kventry_t
*)g_hash_table_lookup(kvhash
, key
))) {
1001 g_free(entry
->data
);
1002 entry
->data
= g_memdup(data
, len
);
1006 kventry_t
*entry
= g_new0(kventry_t
, 1);
1008 entry
->key
= g_strdup(key
);
1009 entry
->data
= g_memdup(data
, len
);
1012 g_hash_table_replace(kvhash
, entry
->key
, entry
);
1018 static const char *rrd_def_node
[] = {
1019 "DS:loadavg:GAUGE:120:0:U",
1020 "DS:maxcpu:GAUGE:120:0:U",
1021 "DS:cpu:GAUGE:120:0:U",
1022 "DS:iowait:GAUGE:120:0:U",
1023 "DS:memtotal:GAUGE:120:0:U",
1024 "DS:memused:GAUGE:120:0:U",
1025 "DS:swaptotal:GAUGE:120:0:U",
1026 "DS:swapused:GAUGE:120:0:U",
1027 "DS:roottotal:GAUGE:120:0:U",
1028 "DS:rootused:GAUGE:120:0:U",
1029 "DS:netin:DERIVE:120:0:U",
1030 "DS:netout:DERIVE:120:0:U",
1032 "RRA:AVERAGE:0.5:1:70", // 1 min avg - one hour
1033 "RRA:AVERAGE:0.5:30:70", // 30 min avg - one day
1034 "RRA:AVERAGE:0.5:180:70", // 3 hour avg - one week
1035 "RRA:AVERAGE:0.5:720:70", // 12 hour avg - one month
1036 "RRA:AVERAGE:0.5:10080:70", // 7 day avg - ony year
1038 "RRA:MAX:0.5:1:70", // 1 min max - one hour
1039 "RRA:MAX:0.5:30:70", // 30 min max - one day
1040 "RRA:MAX:0.5:180:70", // 3 hour max - one week
1041 "RRA:MAX:0.5:720:70", // 12 hour max - one month
1042 "RRA:MAX:0.5:10080:70", // 7 day max - ony year
1046 static const char *rrd_def_vm
[] = {
1047 "DS:maxcpu:GAUGE:120:0:U",
1048 "DS:cpu:GAUGE:120:0:U",
1049 "DS:maxmem:GAUGE:120:0:U",
1050 "DS:mem:GAUGE:120:0:U",
1051 "DS:maxdisk:GAUGE:120:0:U",
1052 "DS:disk:GAUGE:120:0:U",
1053 "DS:netin:DERIVE:120:0:U",
1054 "DS:netout:DERIVE:120:0:U",
1055 "DS:diskread:DERIVE:120:0:U",
1056 "DS:diskwrite:DERIVE:120:0:U",
1058 "RRA:AVERAGE:0.5:1:70", // 1 min avg - one hour
1059 "RRA:AVERAGE:0.5:30:70", // 30 min avg - one day
1060 "RRA:AVERAGE:0.5:180:70", // 3 hour avg - one week
1061 "RRA:AVERAGE:0.5:720:70", // 12 hour avg - one month
1062 "RRA:AVERAGE:0.5:10080:70", // 7 day avg - ony year
1064 "RRA:MAX:0.5:1:70", // 1 min max - one hour
1065 "RRA:MAX:0.5:30:70", // 30 min max - one day
1066 "RRA:MAX:0.5:180:70", // 3 hour max - one week
1067 "RRA:MAX:0.5:720:70", // 12 hour max - one month
1068 "RRA:MAX:0.5:10080:70", // 7 day max - ony year
1072 static const char *rrd_def_storage
[] = {
1073 "DS:total:GAUGE:120:0:U",
1074 "DS:used:GAUGE:120:0:U",
1076 "RRA:AVERAGE:0.5:1:70", // 1 min avg - one hour
1077 "RRA:AVERAGE:0.5:30:70", // 30 min avg - one day
1078 "RRA:AVERAGE:0.5:180:70", // 3 hour avg - one week
1079 "RRA:AVERAGE:0.5:720:70", // 12 hour avg - one month
1080 "RRA:AVERAGE:0.5:10080:70", // 7 day avg - ony year
1082 "RRA:MAX:0.5:1:70", // 1 min max - one hour
1083 "RRA:MAX:0.5:30:70", // 30 min max - one day
1084 "RRA:MAX:0.5:180:70", // 3 hour max - one week
1085 "RRA:MAX:0.5:720:70", // 12 hour max - one month
1086 "RRA:MAX:0.5:10080:70", // 7 day max - ony year
1090 #define RRDDIR "/var/lib/rrdcached/db"
1094 const char *filename
,
1096 const char *rrddef
[])
1098 /* start at day boundary */
1101 struct tm
*ltm
= localtime(&ctime
);
1107 if (rrd_create_r(filename
, 60, timelocal(ltm
), argcount
, rrddef
)) {
1108 cfs_message("RRD create error %s: %s", filename
, rrd_get_error());
1112 static inline const char *
1118 while (*data
&& found
< count
) {
1131 g_return_if_fail(key
!= NULL
);
1132 g_return_if_fail(data
!= NULL
);
1133 g_return_if_fail(len
> 0);
1134 g_return_if_fail(len
< 4096);
1136 static const char *rrdcsock
= "unix:/var/run/rrdcached.sock";
1139 if (rrdc_connect(rrdcsock
) != 0)
1142 char *filename
= NULL
;
1146 if (strncmp(key
, "pve2-node/", 10) == 0) {
1147 const char *node
= key
+ 10;
1151 if (strchr(node
, '/') != NULL
)
1154 if (strlen(node
) < 1)
1157 filename
= g_strdup_printf(RRDDIR
"/%s", key
);
1159 if (!g_file_test(filename
, G_FILE_TEST_EXISTS
)) {
1161 mkdir(RRDDIR
"/pve2-node", 0755);
1162 int argcount
= sizeof(rrd_def_node
)/sizeof(void*) - 1;
1163 create_rrd_file(filename
, argcount
, rrd_def_node
);
1166 } else if ((strncmp(key
, "pve2-vm/", 8) == 0) ||
1167 (strncmp(key
, "pve2.3-vm/", 10) == 0)) {
1170 if (strncmp(key
, "pve2-vm/", 8) == 0) {
1178 if (strchr(vmid
, '/') != NULL
)
1181 if (strlen(vmid
) < 1)
1184 filename
= g_strdup_printf(RRDDIR
"/%s/%s", "pve2-vm", vmid
);
1186 if (!g_file_test(filename
, G_FILE_TEST_EXISTS
)) {
1188 mkdir(RRDDIR
"/pve2-vm", 0755);
1189 int argcount
= sizeof(rrd_def_vm
)/sizeof(void*) - 1;
1190 create_rrd_file(filename
, argcount
, rrd_def_vm
);
1193 } else if (strncmp(key
, "pve2-storage/", 13) == 0) {
1194 const char *node
= key
+ 13;
1196 const char *storage
= node
;
1197 while (*storage
&& *storage
!= '/')
1200 if (*storage
!= '/' || ((storage
- node
) < 1))
1205 if (strchr(storage
, '/') != NULL
)
1208 if (strlen(storage
) < 1)
1211 filename
= g_strdup_printf(RRDDIR
"/%s", key
);
1213 if (!g_file_test(filename
, G_FILE_TEST_EXISTS
)) {
1215 mkdir(RRDDIR
"/pve2-storage", 0755);
1217 char *dir
= g_path_get_dirname(filename
);
1221 int argcount
= sizeof(rrd_def_storage
)/sizeof(void*) - 1;
1222 create_rrd_file(filename
, argcount
, rrd_def_storage
);
1229 const char *dp
= skip
? rrd_skip_data(data
, skip
) : data
;
1231 const char *update_args
[] = { dp
, NULL
};
1235 if ((status
= rrdc_update(filename
, 1, update_args
)) != 0) {
1236 cfs_message("RRDC update error %s: %d", filename
, status
);
1239 if (rrd_update_r(filename
, NULL
, 1, update_args
) != 0) {
1240 cfs_message("RRD update error %s: %s", filename
, rrd_get_error());
1246 if (rrd_update_r(filename
, NULL
, 1, update_args
) != 0) {
1247 cfs_message("RRD update error %s: %s", filename
, rrd_get_error());
1258 cfs_critical("RRD update error: unknown/wrong key %s", key
);
1268 rrdentry_t
*entry
= (rrdentry_t
*)value
;
1269 uint32_t ctime
= GPOINTER_TO_UINT(user_data
);
1271 int diff
= ctime
- entry
->time
;
1273 /* remove everything older than 5 minutes */
1276 return (diff
> expire
) ? TRUE
: FALSE
;
1279 static char *rrd_dump_buf
= NULL
;
1280 static time_t rrd_dump_last
= 0;
1283 cfs_rrd_dump(GString
*str
)
1287 g_mutex_lock (&mutex
);
1290 if (rrd_dump_buf
&& (ctime
- rrd_dump_last
) < 2) {
1291 g_string_assign(str
, rrd_dump_buf
);
1292 g_mutex_unlock (&mutex
);
1296 /* remove old data */
1297 g_hash_table_foreach_remove(cfs_status
.rrdhash
, rrd_entry_is_old
,
1298 GUINT_TO_POINTER(ctime
));
1300 g_string_set_size(str
, 0);
1302 GHashTableIter iter
;
1303 gpointer key
, value
;
1305 g_hash_table_iter_init (&iter
, cfs_status
.rrdhash
);
1307 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
1308 rrdentry_t
*entry
= (rrdentry_t
*)value
;
1309 g_string_append(str
, key
);
1310 g_string_append(str
, ":");
1311 g_string_append(str
, entry
->data
);
1312 g_string_append(str
, "\n");
1315 g_string_append_c(str
, 0); // never return undef
1317 rrd_dump_last
= ctime
;
1319 g_free(rrd_dump_buf
);
1320 rrd_dump_buf
= g_strdup(str
->str
);
1322 g_mutex_unlock (&mutex
);
1328 const char *nodename
,
1332 g_return_val_if_fail(iphash
!= NULL
, FALSE
);
1333 g_return_val_if_fail(nodename
!= NULL
, FALSE
);
1334 g_return_val_if_fail(ip
!= NULL
, FALSE
);
1335 g_return_val_if_fail(len
> 0, FALSE
);
1336 g_return_val_if_fail(len
< 256, FALSE
);
1337 g_return_val_if_fail(ip
[len
-1] == 0, FALSE
);
1339 char *oldip
= (char *)g_hash_table_lookup(iphash
, nodename
);
1341 if (!oldip
|| (strcmp(oldip
, ip
) != 0)) {
1342 cfs_status
.clinfo_version
++;
1343 g_hash_table_replace(iphash
, g_strdup(nodename
), g_strdup(ip
));
1351 GHashTable
*rrdhash
,
1356 g_return_val_if_fail(rrdhash
!= NULL
, FALSE
);
1357 g_return_val_if_fail(key
!= NULL
, FALSE
);
1358 g_return_val_if_fail(data
!= NULL
, FALSE
);
1359 g_return_val_if_fail(len
> 0, FALSE
);
1360 g_return_val_if_fail(len
< 4096, FALSE
);
1361 g_return_val_if_fail(data
[len
-1] == 0, FALSE
);
1364 if ((entry
= (rrdentry_t
*)g_hash_table_lookup(rrdhash
, key
))) {
1365 g_free(entry
->data
);
1366 entry
->data
= g_memdup(data
, len
);
1368 entry
->time
= time(NULL
);
1370 rrdentry_t
*entry
= g_new0(rrdentry_t
, 1);
1372 entry
->key
= g_strdup(key
);
1373 entry
->data
= g_memdup(data
, len
);
1375 entry
->time
= time(NULL
);
1377 g_hash_table_replace(rrdhash
, entry
->key
, entry
);
1380 update_rrd_data(key
, data
, len
);
1386 kvstore_send_update_message(
1392 if (!dfsm_is_initialized(dfsm
))
1395 struct iovec iov
[2];
1398 g_strlcpy(name
, key
, sizeof(name
));
1400 iov
[0].iov_base
= &name
;
1401 iov
[0].iov_len
= sizeof(name
);
1403 iov
[1].iov_base
= (char *)data
;
1404 iov
[1].iov_len
= len
;
1406 if (dfsm_send_message(dfsm
, KVSTORE_MESSAGE_UPDATE
, iov
, 2) == CS_OK
)
1412 static clog_entry_t
*
1413 kvstore_parse_log_message(
1417 g_return_val_if_fail(msg
!= NULL
, NULL
);
1419 if (msg_len
< sizeof(clog_entry_t
)) {
1420 cfs_critical("received short log message (%zu < %zu)", msg_len
, sizeof(clog_entry_t
));
1424 clog_entry_t
*entry
= (clog_entry_t
*)msg
;
1426 uint32_t size
= sizeof(clog_entry_t
) + entry
->node_len
+
1427 entry
->ident_len
+ entry
->tag_len
+ entry
->msg_len
;
1429 if (msg_len
!= size
) {
1430 cfs_critical("received log message with wrong size (%zu != %u)", msg_len
, size
);
1434 char *msgptr
= entry
->data
;
1436 if (*((char *)msgptr
+ entry
->node_len
- 1)) {
1437 cfs_critical("unterminated string in log message");
1440 msgptr
+= entry
->node_len
;
1442 if (*((char *)msgptr
+ entry
->ident_len
- 1)) {
1443 cfs_critical("unterminated string in log message");
1446 msgptr
+= entry
->ident_len
;
1448 if (*((char *)msgptr
+ entry
->tag_len
- 1)) {
1449 cfs_critical("unterminated string in log message");
1452 msgptr
+= entry
->tag_len
;
1454 if (*((char *)msgptr
+ entry
->msg_len
- 1)) {
1455 cfs_critical("unterminated string in log message");
1463 kvstore_parse_update_message(
1467 gconstpointer
*data
,
1470 g_return_val_if_fail(msg
!= NULL
, FALSE
);
1471 g_return_val_if_fail(key
!= NULL
, FALSE
);
1472 g_return_val_if_fail(data
!= NULL
, FALSE
);
1473 g_return_val_if_fail(len
!= NULL
, FALSE
);
1475 if (msg_len
< 256) {
1476 cfs_critical("received short kvstore message (%zu < 256)", msg_len
);
1480 /* test if key is null terminated */
1482 for (i
= 0; i
< 256; i
++)
1483 if (((char *)msg
)[i
] == 0)
1490 *len
= msg_len
- 256;
1492 *data
= (char *) msg
+ 256;
1498 cfs_create_status_msg(
1500 const char *nodename
,
1503 g_return_val_if_fail(str
!= NULL
, -EINVAL
);
1504 g_return_val_if_fail(key
!= NULL
, -EINVAL
);
1508 GHashTable
*kvhash
= NULL
;
1510 g_mutex_lock (&mutex
);
1512 if (!nodename
|| !nodename
[0] || !strcmp(nodename
, cfs
.nodename
)) {
1513 kvhash
= cfs_status
.kvhash
;
1514 } else if (cfs_status
.clinfo
&& cfs_status
.clinfo
->nodes_byname
) {
1515 cfs_clnode_t
*clnode
;
1516 if ((clnode
= g_hash_table_lookup(cfs_status
.clinfo
->nodes_byname
, nodename
)))
1517 kvhash
= clnode
->kvhash
;
1521 if (kvhash
&& (entry
= (kventry_t
*)g_hash_table_lookup(kvhash
, key
))) {
1522 g_string_append_len(str
, entry
->data
, entry
->len
);
1526 g_mutex_unlock (&mutex
);
1537 g_return_val_if_fail(key
!= NULL
, FALSE
);
1538 g_return_val_if_fail(data
!= NULL
, FALSE
);
1539 g_return_val_if_fail(cfs_status
.kvhash
!= NULL
, FALSE
);
1541 if (len
> CFS_MAX_STATUS_SIZE
)
1544 g_mutex_lock (&mutex
);
1548 if (strncmp(key
, "rrd/", 4) == 0) {
1549 res
= rrdentry_hash_set(cfs_status
.rrdhash
, key
+ 4, data
, len
);
1550 } else if (!strcmp(key
, "nodeip")) {
1551 res
= nodeip_hash_set(cfs_status
.iphash
, cfs
.nodename
, data
, len
);
1553 res
= kventry_hash_set(cfs_status
.kvhash
, key
, data
, len
);
1555 g_mutex_unlock (&mutex
);
1557 if (cfs_status
.kvstore
)
1558 kvstore_send_update_message(cfs_status
.kvstore
, key
, data
, len
);
1560 return res
? 0 : -ENOMEM
;
1564 cfs_kvstore_node_set(
1570 g_return_val_if_fail(nodeid
!= 0, FALSE
);
1571 g_return_val_if_fail(key
!= NULL
, FALSE
);
1572 g_return_val_if_fail(data
!= NULL
, FALSE
);
1574 g_mutex_lock (&mutex
);
1576 if (!cfs_status
.clinfo
|| !cfs_status
.clinfo
->nodes_byid
)
1577 goto ret
; /* ignore */
1579 cfs_clnode_t
*clnode
= g_hash_table_lookup(cfs_status
.clinfo
->nodes_byid
, &nodeid
);
1581 goto ret
; /* ignore */
1583 cfs_debug("got node %d status update %s", nodeid
, key
);
1585 if (strncmp(key
, "rrd/", 4) == 0) {
1586 rrdentry_hash_set(cfs_status
.rrdhash
, key
+ 4, data
, len
);
1587 } else if (!strcmp(key
, "nodeip")) {
1588 nodeip_hash_set(cfs_status
.iphash
, clnode
->name
, data
, len
);
1590 if (!clnode
->kvhash
) {
1591 if (!(clnode
->kvhash
= kventry_hash_new())) {
1592 goto ret
; /*ignore */
1596 kventry_hash_set(clnode
->kvhash
, key
, data
, len
);
1600 g_mutex_unlock (&mutex
);
1606 cfs_kvstore_sync(void)
1608 g_return_val_if_fail(cfs_status
.kvhash
!= NULL
, FALSE
);
1609 g_return_val_if_fail(cfs_status
.kvstore
!= NULL
, FALSE
);
1611 gboolean res
= TRUE
;
1613 g_mutex_lock (&mutex
);
1615 GHashTable
*ht
= cfs_status
.kvhash
;
1616 GHashTableIter iter
;
1617 gpointer key
, value
;
1619 g_hash_table_iter_init (&iter
, ht
);
1621 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
1622 kventry_t
*entry
= (kventry_t
*)value
;
1623 kvstore_send_update_message(cfs_status
.kvstore
, entry
->key
, entry
->data
, entry
->len
);
1626 g_mutex_unlock (&mutex
);
1643 g_return_val_if_fail(dfsm
!= NULL
, -1);
1644 g_return_val_if_fail(msg
!= NULL
, -1);
1645 g_return_val_if_fail(res_ptr
!= NULL
, -1);
1647 /* ignore message for ourself */
1648 if (dfsm_nodeid_is_local(dfsm
, nodeid
, pid
))
1651 if (msg_type
== KVSTORE_MESSAGE_UPDATE
) {
1655 if (kvstore_parse_update_message(msg
, msg_len
, &key
, &data
, &len
)) {
1656 cfs_kvstore_node_set(nodeid
, key
, data
, len
);
1658 cfs_critical("cant parse update message");
1660 } else if (msg_type
== KVSTORE_MESSAGE_LOG
) {
1661 cfs_message("received log"); // fixme: remove
1662 const clog_entry_t
*entry
;
1663 if ((entry
= kvstore_parse_log_message(msg
, msg_len
))) {
1664 clusterlog_insert(cfs_status
.clusterlog
, entry
);
1666 cfs_critical("cant parse log message");
1669 cfs_critical("received unknown message type %d\n", msg_type
);
1686 const struct cpg_address
*member_list
,
1687 size_t member_list_entries
)
1689 g_return_if_fail(dfsm
!= NULL
);
1690 g_return_if_fail(member_list
!= NULL
);
1692 cfs_debug("enter %s", __func__
);
1694 g_mutex_lock (&mutex
);
1696 cfs_clinfo_t
*clinfo
= cfs_status
.clinfo
;
1698 if (clinfo
&& clinfo
->nodes_byid
) {
1700 GHashTable
*ht
= clinfo
->nodes_byid
;
1701 GHashTableIter iter
;
1702 gpointer key
, value
;
1704 g_hash_table_iter_init (&iter
, ht
);
1706 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
1707 cfs_clnode_t
*node
= (cfs_clnode_t
*)value
;
1708 node
->online
= FALSE
;
1711 for (int i
= 0; i
< member_list_entries
; i
++) {
1713 if ((node
= g_hash_table_lookup(clinfo
->nodes_byid
, &member_list
[i
].nodeid
))) {
1714 node
->online
= TRUE
;
1718 cfs_status
.clinfo_version
++;
1721 g_mutex_unlock (&mutex
);
1728 unsigned int *res_len
)
1730 g_return_val_if_fail(dfsm
!= NULL
, NULL
);
1732 gpointer msg
= clusterlog_get_state(cfs_status
.clusterlog
, res_len
);
1738 dfsm_process_update(
1741 dfsm_sync_info_t
*syncinfo
,
1747 cfs_critical("%s: received unexpected update message", __func__
);
1753 dfsm_process_state_update(
1756 dfsm_sync_info_t
*syncinfo
)
1758 g_return_val_if_fail(dfsm
!= NULL
, -1);
1759 g_return_val_if_fail(syncinfo
!= NULL
, -1);
1761 clog_base_t
*clog
[syncinfo
->node_count
];
1763 int local_index
= -1;
1764 for (int i
= 0; i
< syncinfo
->node_count
; i
++) {
1765 dfsm_node_info_t
*ni
= &syncinfo
->nodes
[i
];
1768 if (syncinfo
->local
== ni
)
1771 clog_base_t
*base
= (clog_base_t
*)ni
->state
;
1772 if (ni
->state_len
> 8 && ni
->state_len
== clog_size(base
)) {
1773 clog
[i
] = ni
->state
;
1775 cfs_critical("received log with wrong size %u", ni
->state_len
);
1780 if (!clusterlog_merge(cfs_status
.clusterlog
, clog
, syncinfo
->node_count
, local_index
)) {
1781 cfs_critical("unable to merge log files");
1793 dfsm_sync_info_t
*syncinfo
)
1795 g_return_val_if_fail(dfsm
!= NULL
, -1);
1796 g_return_val_if_fail(syncinfo
!= NULL
, -1);
1802 dfsm_synced(dfsm_t
*dfsm
)
1804 g_return_if_fail(dfsm
!= NULL
);
1806 char *ip
= (char *)g_hash_table_lookup(cfs_status
.iphash
, cfs
.nodename
);
1810 cfs_status_set("nodeip", ip
, strlen(ip
) + 1);
1817 dfsm_sync_info_t
*syncinfo
)
1822 static dfsm_callbacks_t kvstore_dfsm_callbacks
= {
1823 .dfsm_deliver_fn
= dfsm_deliver
,
1824 .dfsm_confchg_fn
= dfsm_confchg
,
1826 .dfsm_get_state_fn
= dfsm_get_state
,
1827 .dfsm_process_state_update_fn
= dfsm_process_state_update
,
1828 .dfsm_process_update_fn
= dfsm_process_update
,
1829 .dfsm_commit_fn
= dfsm_commit
,
1830 .dfsm_cleanup_fn
= dfsm_cleanup
,
1831 .dfsm_synced_fn
= dfsm_synced
,
1835 cfs_status_dfsm_new(void)
1837 g_mutex_lock (&mutex
);
1839 cfs_status
.kvstore
= dfsm_new(NULL
, KVSTORE_CPG_GROUP_NAME
, G_LOG_DOMAIN
,
1840 0, &kvstore_dfsm_callbacks
);
1841 g_mutex_unlock (&mutex
);
1843 return cfs_status
.kvstore
;
1847 cfs_is_quorate(void)
1849 g_mutex_lock (&mutex
);
1850 gboolean res
= cfs_status
.quorate
;
1851 g_mutex_unlock (&mutex
);
1861 g_mutex_lock (&mutex
);
1863 uint32_t prev_quorate
= cfs_status
.quorate
;
1864 cfs_status
.quorate
= quorate
;
1866 if (!prev_quorate
&& cfs_status
.quorate
) {
1868 cfs_message("node has quorum");
1871 if (prev_quorate
&& !cfs_status
.quorate
) {
1873 cfs_message("node lost quorum");
1876 g_mutex_unlock (&mutex
);