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
= "priv/shadow.cfg" },
86 { .path
= "priv/acme/plugins.cfg" },
87 { .path
= "priv/tfa.cfg" },
88 { .path
= "priv/token.cfg" },
89 { .path
= "priv/ipam.db" },
90 { .path
= "datacenter.cfg" },
91 { .path
= "vzdump.cron" },
92 { .path
= "vzdump.conf" },
93 { .path
= "jobs.cfg" },
94 { .path
= "ha/crm_commands" },
95 { .path
= "ha/manager_status" },
96 { .path
= "ha/resources.cfg" },
97 { .path
= "ha/groups.cfg" },
98 { .path
= "ha/fence.cfg" },
99 { .path
= "status.cfg" },
100 { .path
= "replication.cfg" },
101 { .path
= "ceph.conf" },
102 { .path
= "sdn/vnets.cfg" },
103 { .path
= "sdn/zones.cfg" },
104 { .path
= "sdn/controllers.cfg" },
105 { .path
= "sdn/subnets.cfg" },
106 { .path
= "sdn/ipams.cfg" },
107 { .path
= "sdn/dns.cfg" },
108 { .path
= "sdn/.running-config" },
109 { .path
= "virtual-guest/cpu-models.conf" },
110 { .path
= "firewall/cluster.fw" },
120 cfs_clinfo_t
*clinfo
;
121 uint32_t clinfo_version
;
124 uint32_t vmlist_version
;
131 GHashTable
*memdb_changes
;
133 clusterlog_t
*clusterlog
;
136 static cfs_status_t cfs_status
;
148 uint32_t cman_version
;
150 GHashTable
*nodes_byid
;
151 GHashTable
*nodes_byname
;
155 g_int32_hash (gconstpointer v
)
157 return *(const uint32_t *) v
;
161 g_int32_equal (gconstpointer v1
,
164 return *((const uint32_t*) v1
) == *((const uint32_t*) v2
);
167 static void vminfo_free(vminfo_t
*vminfo
)
169 g_return_if_fail(vminfo
!= NULL
);
171 if (vminfo
->nodename
)
172 g_free(vminfo
->nodename
);
178 static const char *vminfo_type_to_string(vminfo_t
*vminfo
)
180 if (vminfo
->vmtype
== VMTYPE_QEMU
) {
182 } else if (vminfo
->vmtype
== VMTYPE_OPENVZ
) {
183 // FIXME: remove openvz stuff for 7.x
185 } else if (vminfo
->vmtype
== VMTYPE_LXC
) {
192 static const char *vminfo_type_to_path_type(vminfo_t
*vminfo
)
194 if (vminfo
->vmtype
== VMTYPE_QEMU
) {
195 return "qemu-server"; // special case..
197 return vminfo_type_to_string(vminfo
);
201 int vminfo_to_path(vminfo_t
*vminfo
, GString
*path
)
203 g_return_val_if_fail(vminfo
!= NULL
, -1);
204 g_return_val_if_fail(path
!= NULL
, -1);
206 if (!vminfo
->nodename
)
209 const char *type
= vminfo_type_to_path_type(vminfo
);
210 g_string_printf(path
, "/nodes/%s/%s/%u.conf", vminfo
->nodename
, type
, vminfo
->vmid
);
215 void cfs_clnode_destroy(
216 cfs_clnode_t
*clnode
)
218 g_return_if_fail(clnode
!= NULL
);
221 g_hash_table_destroy(clnode
->kvhash
);
224 g_free(clnode
->name
);
229 cfs_clnode_t
*cfs_clnode_new(
234 g_return_val_if_fail(name
!= NULL
, NULL
);
236 cfs_clnode_t
*clnode
= g_new0(cfs_clnode_t
, 1);
240 clnode
->name
= g_strdup(name
);
241 clnode
->nodeid
= nodeid
;
242 clnode
->votes
= votes
;
247 gboolean
cfs_clinfo_destroy(
248 cfs_clinfo_t
*clinfo
)
250 g_return_val_if_fail(clinfo
!= NULL
, FALSE
);
252 if (clinfo
->cluster_name
)
253 g_free(clinfo
->cluster_name
);
255 if (clinfo
->nodes_byname
)
256 g_hash_table_destroy(clinfo
->nodes_byname
);
258 if (clinfo
->nodes_byid
)
259 g_hash_table_destroy(clinfo
->nodes_byid
);
266 cfs_clinfo_t
*cfs_clinfo_new(
267 const char *cluster_name
,
268 uint32_t cman_version
)
270 g_return_val_if_fail(cluster_name
!= NULL
, NULL
);
272 cfs_clinfo_t
*clinfo
= g_new0(cfs_clinfo_t
, 1);
276 clinfo
->cluster_name
= g_strdup(cluster_name
);
277 clinfo
->cman_version
= cman_version
;
279 if (!(clinfo
->nodes_byid
= g_hash_table_new_full(
280 g_int32_hash
, g_int32_equal
, NULL
,
281 (GDestroyNotify
)cfs_clnode_destroy
)))
284 if (!(clinfo
->nodes_byname
= g_hash_table_new(g_str_hash
, g_str_equal
)))
290 cfs_clinfo_destroy(clinfo
);
295 gboolean
cfs_clinfo_add_node(
296 cfs_clinfo_t
*clinfo
,
297 cfs_clnode_t
*clnode
)
299 g_return_val_if_fail(clinfo
!= NULL
, FALSE
);
300 g_return_val_if_fail(clnode
!= NULL
, FALSE
);
302 g_hash_table_replace(clinfo
->nodes_byid
, &clnode
->nodeid
, clnode
);
303 g_hash_table_replace(clinfo
->nodes_byname
, clnode
->name
, clnode
);
309 cfs_create_memberlist_msg(
312 g_return_val_if_fail(str
!= NULL
, -EINVAL
);
314 g_mutex_lock (&mutex
);
316 g_string_append_printf(str
,"{\n");
320 cfs_clinfo_t
*clinfo
= cfs_status
.clinfo
;
322 if (clinfo
&& clinfo
->nodes_byid
)
323 nodecount
= g_hash_table_size(clinfo
->nodes_byid
);
326 g_string_append_printf(str
, "\"nodename\": \"%s\",\n", cfs
.nodename
);
327 g_string_append_printf(str
, "\"version\": %u,\n", cfs_status
.clinfo_version
);
329 g_string_append_printf(str
, "\"cluster\": { ");
330 g_string_append_printf(str
, "\"name\": \"%s\", \"version\": %d, "
331 "\"nodes\": %d, \"quorate\": %d ",
332 clinfo
->cluster_name
, clinfo
->cman_version
,
333 nodecount
, cfs_status
.quorate
);
335 g_string_append_printf(str
,"},\n");
336 g_string_append_printf(str
,"\"nodelist\": {\n");
338 GHashTable
*ht
= clinfo
->nodes_byid
;
342 g_hash_table_iter_init (&iter
, ht
);
345 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
346 cfs_clnode_t
*node
= (cfs_clnode_t
*)value
;
347 if (i
) g_string_append_printf(str
, ",\n");
350 g_string_append_printf(str
, " \"%s\": { \"id\": %d, \"online\": %d",
351 node
->name
, node
->nodeid
, node
->online
);
354 char *ip
= (char *)g_hash_table_lookup(cfs_status
.iphash
, node
->name
);
356 g_string_append_printf(str
, ", \"ip\": \"%s\"", ip
);
359 g_string_append_printf(str
, "}");
362 g_string_append_printf(str
,"\n }\n");
364 g_string_append_printf(str
, "\"nodename\": \"%s\",\n", cfs
.nodename
);
365 g_string_append_printf(str
, "\"version\": %u\n", cfs_status
.clinfo_version
);
368 g_string_append_printf(str
,"}\n");
370 g_mutex_unlock (&mutex
);
376 kventry_free(kventry_t
*entry
)
378 g_return_if_fail(entry
!= NULL
);
386 kventry_hash_new(void)
388 return g_hash_table_new_full(g_str_hash
, g_str_equal
, NULL
,
389 (GDestroyNotify
)kventry_free
);
393 rrdentry_free(rrdentry_t
*entry
)
395 g_return_if_fail(entry
!= NULL
);
403 rrdentry_hash_new(void)
405 return g_hash_table_new_full(g_str_hash
, g_str_equal
, NULL
,
406 (GDestroyNotify
)rrdentry_free
);
410 cfs_cluster_log_dump(GString
*str
, const char *user
, guint max_entries
)
412 clusterlog_dump(cfs_status
.clusterlog
, str
, user
, max_entries
);
416 cfs_cluster_log(clog_entry_t
*entry
)
418 g_return_if_fail(entry
!= NULL
);
420 clusterlog_insert(cfs_status
.clusterlog
, entry
);
422 if (cfs_status
.kvstore
) {
424 iov
[0].iov_base
= (char *)entry
;
425 iov
[0].iov_len
= clog_entry_size(entry
);
427 if (dfsm_is_initialized(cfs_status
.kvstore
))
428 dfsm_send_message(cfs_status
.kvstore
, KVSTORE_MESSAGE_LOG
, iov
, 1);
432 void cfs_status_init(void)
434 g_mutex_lock (&mutex
);
436 cfs_status
.start_time
= time(NULL
);
438 cfs_status
.vmlist
= vmlist_hash_new();
440 cfs_status
.kvhash
= kventry_hash_new();
442 cfs_status
.rrdhash
= rrdentry_hash_new();
444 cfs_status
.iphash
= g_hash_table_new_full(g_str_hash
, g_str_equal
, g_free
, g_free
);
446 cfs_status
.memdb_changes
= g_hash_table_new(g_str_hash
, g_str_equal
);
448 for (int i
= 0; i
< G_N_ELEMENTS(memdb_change_array
); i
++) {
449 g_hash_table_replace(cfs_status
.memdb_changes
,
450 memdb_change_array
[i
].path
,
451 &memdb_change_array
[i
]);
454 cfs_status
.clusterlog
= clusterlog_new();
457 clusterlog_add(cfs_status
.clusterlog
, "root", "cluster", getpid(),
458 LOG_INFO
, "starting cluster log");
460 g_mutex_unlock (&mutex
);
463 void cfs_status_cleanup(void)
465 g_mutex_lock (&mutex
);
467 cfs_status
.clinfo_version
++;
469 if (cfs_status
.clinfo
) {
470 cfs_clinfo_destroy(cfs_status
.clinfo
);
471 cfs_status
.clinfo
= NULL
;
474 if (cfs_status
.vmlist
) {
475 g_hash_table_destroy(cfs_status
.vmlist
);
476 cfs_status
.vmlist
= NULL
;
479 if (cfs_status
.kvhash
) {
480 g_hash_table_destroy(cfs_status
.kvhash
);
481 cfs_status
.kvhash
= NULL
;
484 if (cfs_status
.rrdhash
) {
485 g_hash_table_destroy(cfs_status
.rrdhash
);
486 cfs_status
.rrdhash
= NULL
;
489 if (cfs_status
.iphash
) {
490 g_hash_table_destroy(cfs_status
.iphash
);
491 cfs_status
.iphash
= NULL
;
494 if (cfs_status
.clusterlog
)
495 clusterlog_destroy(cfs_status
.clusterlog
);
497 g_mutex_unlock (&mutex
);
500 void cfs_status_set_clinfo(
501 cfs_clinfo_t
*clinfo
)
503 g_return_if_fail(clinfo
!= NULL
);
505 g_mutex_lock (&mutex
);
507 cfs_status
.clinfo_version
++;
509 cfs_clinfo_t
*old
= cfs_status
.clinfo
;
511 cfs_status
.clinfo
= clinfo
;
513 cfs_message("update cluster info (cluster name %s, version = %d)",
514 clinfo
->cluster_name
, clinfo
->cman_version
);
517 if (old
&& old
->nodes_byid
&& clinfo
->nodes_byid
) {
519 GHashTable
*ht
= clinfo
->nodes_byid
;
523 g_hash_table_iter_init (&iter
, ht
);
525 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
526 cfs_clnode_t
*node
= (cfs_clnode_t
*)value
;
527 cfs_clnode_t
*oldnode
;
528 if ((oldnode
= g_hash_table_lookup(old
->nodes_byid
, key
))) {
529 node
->online
= oldnode
->online
;
530 node
->kvhash
= oldnode
->kvhash
;
531 oldnode
->kvhash
= NULL
;
538 cfs_clinfo_destroy(old
);
541 g_mutex_unlock (&mutex
);
545 dump_kvstore_versions(
548 const char *nodename
)
550 g_return_if_fail(kvhash
!= NULL
);
551 g_return_if_fail(str
!= NULL
);
552 g_return_if_fail(nodename
!= NULL
);
554 GHashTable
*ht
= kvhash
;
558 g_string_append_printf(str
, "\"%s\": {\n", nodename
);
560 g_hash_table_iter_init (&iter
, ht
);
563 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
564 kventry_t
*entry
= (kventry_t
*)value
;
565 if (i
) g_string_append_printf(str
, ",\n");
567 g_string_append_printf(str
,"\"%s\": %u", entry
->key
, entry
->version
);
570 g_string_append_printf(str
, "}\n");
574 cfs_create_version_msg(GString
*str
)
576 g_return_val_if_fail(str
!= NULL
, -EINVAL
);
578 g_mutex_lock (&mutex
);
580 g_string_append_printf(str
,"{\n");
582 g_string_append_printf(str
, "\"starttime\": %lu,\n", (unsigned long)cfs_status
.start_time
);
584 g_string_append_printf(str
, "\"clinfo\": %u,\n", cfs_status
.clinfo_version
);
586 g_string_append_printf(str
, "\"vmlist\": %u,\n", cfs_status
.vmlist_version
);
588 for (int i
= 0; i
< G_N_ELEMENTS(memdb_change_array
); i
++) {
589 g_string_append_printf(str
, "\"%s\": %u,\n",
590 memdb_change_array
[i
].path
,
591 memdb_change_array
[i
].version
);
594 g_string_append_printf(str
, "\"kvstore\": {\n");
596 dump_kvstore_versions(str
, cfs_status
.kvhash
, cfs
.nodename
);
598 cfs_clinfo_t
*clinfo
= cfs_status
.clinfo
;
600 if (clinfo
&& clinfo
->nodes_byid
) {
601 GHashTable
*ht
= clinfo
->nodes_byid
;
605 g_hash_table_iter_init (&iter
, ht
);
607 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
608 cfs_clnode_t
*node
= (cfs_clnode_t
*)value
;
611 g_string_append_printf(str
, ",\n");
612 dump_kvstore_versions(str
, node
->kvhash
, node
->name
);
616 g_string_append_printf(str
,"}\n");
618 g_string_append_printf(str
,"}\n");
620 g_mutex_unlock (&mutex
);
626 vmlist_hash_new(void)
628 return g_hash_table_new_full(g_int_hash
, g_int_equal
, NULL
,
629 (GDestroyNotify
)vminfo_free
);
633 vmlist_hash_insert_vm(
637 const char *nodename
,
640 g_return_val_if_fail(vmlist
!= NULL
, FALSE
);
641 g_return_val_if_fail(nodename
!= NULL
, FALSE
);
642 g_return_val_if_fail(vmid
!= 0, FALSE
);
643 // FIXME: remove openvz stuff for 7.x
644 g_return_val_if_fail(vmtype
== VMTYPE_QEMU
|| vmtype
== VMTYPE_OPENVZ
||
645 vmtype
== VMTYPE_LXC
, FALSE
);
647 if (!replace
&& g_hash_table_lookup(vmlist
, &vmid
)) {
648 cfs_critical("detected duplicate VMID %d", vmid
);
652 vminfo_t
*vminfo
= g_new0(vminfo_t
, 1);
655 vminfo
->vmtype
= vmtype
;
656 vminfo
->nodename
= g_strdup(nodename
);
658 vminfo
->version
= ++vminfo_version_counter
;
660 g_hash_table_replace(vmlist
, &vminfo
->vmid
, vminfo
);
669 const char *nodename
)
671 g_return_if_fail(cfs_status
.vmlist
!= NULL
);
672 g_return_if_fail(nodename
!= NULL
);
673 g_return_if_fail(vmid
!= 0);
674 // FIXME: remove openvz stuff for 7.x
675 g_return_if_fail(vmtype
== VMTYPE_QEMU
|| vmtype
== VMTYPE_OPENVZ
||
676 vmtype
== VMTYPE_LXC
);
678 cfs_debug("vmlist_register_vm: %s/%u %d", nodename
, vmid
, vmtype
);
680 g_mutex_lock (&mutex
);
682 cfs_status
.vmlist_version
++;
684 vmlist_hash_insert_vm(cfs_status
.vmlist
, vmtype
, vmid
, nodename
, TRUE
);
686 g_mutex_unlock (&mutex
);
690 vmlist_different_vm_exists(
693 const char *nodename
)
695 g_return_val_if_fail(cfs_status
.vmlist
!= NULL
, FALSE
);
696 g_return_val_if_fail(vmid
!= 0, FALSE
);
698 gboolean res
= FALSE
;
700 g_mutex_lock (&mutex
);
703 if ((vminfo
= (vminfo_t
*)g_hash_table_lookup(cfs_status
.vmlist
, &vmid
))) {
704 if (!(vminfo
->vmtype
== vmtype
&& strcmp(vminfo
->nodename
, nodename
) == 0))
707 g_mutex_unlock (&mutex
);
716 g_return_val_if_fail(cfs_status
.vmlist
!= NULL
, FALSE
);
717 g_return_val_if_fail(vmid
!= 0, FALSE
);
719 g_mutex_lock (&mutex
);
721 gpointer res
= g_hash_table_lookup(cfs_status
.vmlist
, &vmid
);
723 g_mutex_unlock (&mutex
);
732 g_return_if_fail(cfs_status
.vmlist
!= NULL
);
733 g_return_if_fail(vmid
!= 0);
735 g_mutex_lock (&mutex
);
737 cfs_status
.vmlist_version
++;
739 g_hash_table_remove(cfs_status
.vmlist
, &vmid
);
741 g_mutex_unlock (&mutex
);
744 void cfs_status_set_vmlist(
747 g_return_if_fail(vmlist
!= NULL
);
749 g_mutex_lock (&mutex
);
751 cfs_status
.vmlist_version
++;
753 if (cfs_status
.vmlist
)
754 g_hash_table_destroy(cfs_status
.vmlist
);
756 cfs_status
.vmlist
= vmlist
;
758 g_mutex_unlock (&mutex
);
762 cfs_create_vmlist_msg(GString
*str
)
764 g_return_val_if_fail(cfs_status
.vmlist
!= NULL
, -EINVAL
);
765 g_return_val_if_fail(str
!= NULL
, -EINVAL
);
767 g_mutex_lock (&mutex
);
769 g_string_append_printf(str
,"{\n");
771 GHashTable
*ht
= cfs_status
.vmlist
;
773 guint count
= g_hash_table_size(ht
);
776 g_string_append_printf(str
,"\"version\": %u\n", cfs_status
.vmlist_version
);
778 g_string_append_printf(str
,"\"version\": %u,\n", cfs_status
.vmlist_version
);
780 g_string_append_printf(str
,"\"ids\": {\n");
785 g_hash_table_iter_init (&iter
, ht
);
788 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
789 vminfo_t
*vminfo
= (vminfo_t
*)value
;
790 const char *type
= vminfo_type_to_string(vminfo
);
793 g_string_append_printf(str
, ",\n");
796 g_string_append_printf(str
,"\"%u\": { \"node\": \"%s\", \"type\": \"%s\", \"version\": %u }",
797 vminfo
->vmid
, vminfo
->nodename
, type
, vminfo
->version
);
800 g_string_append_printf(str
,"}\n");
802 g_string_append_printf(str
,"\n}\n");
804 g_mutex_unlock (&mutex
);
809 // checks if a config line starts with the given prop. if yes, writes a '\0'
810 // at the end of the value, and returns the pointer where the value starts
811 // note: line[line_end] needs to be guaranteed a null byte
813 _get_property_value_from_line(char *line
, size_t line_len
, const char *prop
, size_t prop_len
)
815 if (line_len
<= prop_len
+ 1) return NULL
;
817 if (line
[prop_len
] == ':' && memcmp(line
, prop
, prop_len
) == 0) { // found
818 char *v_start
= &line
[prop_len
+ 1];
819 char *v_end
= &line
[line_len
- 1];
821 // drop initial value whitespaces here already
822 while (v_start
< v_end
&& *v_start
&& isspace(*v_start
)) v_start
++;
824 if (!*v_start
) return NULL
;
826 while (v_end
> v_start
&& isspace(*v_end
)) v_end
--;
827 if (v_end
< &line
[line_len
- 1]) {
837 // checks the conf for lines starting with the given props and
838 // writes the pointers into the correct positions into the 'found' array
839 // afterwards, without initial whitespace(s), we only deal with the format
840 // restriction imposed by our perl VM config parser, main reference is
841 // PVE::QemuServer::parse_vm_config this allows to be very fast and still
843 // main restrictions used for our advantage is the properties match regex:
844 // ($line =~ m/^([a-z][a-z_]*\d*):\s*(.+?)\s*$/) from parse_vm_config
845 // currently we only look at the current configuration in place, i.e., *no*
846 // snapshot and *no* pending changes
848 // min..max is the char range of the first character of the given props,
849 // so that we can return early when checking the line
850 // note: conf must end with a newline
852 _get_property_values(char **found
, char *conf
, int conf_size
, const char **props
, uint8_t num_props
, char min
, char max
)
854 const char *const conf_end
= conf
+ conf_size
;
856 size_t remaining_size
= conf_size
;
859 if (conf_size
== 0) {
863 char *next_newline
= memchr(conf
, '\n', conf_size
);
864 if (next_newline
== NULL
) {
865 return; // valid property lines end with \n, but none in the config
867 *next_newline
= '\0';
869 while (line
!= NULL
) {
870 if (!line
[0]) goto next
;
872 // snapshot or pending section start, but nothing found yet -> not found
873 if (line
[0] == '[') return;
874 // continue early if line does not begin with the min/max char of the properties
875 if (line
[0] < min
|| line
[0] > max
) goto next
;
877 size_t line_len
= next_newline
- line
;
878 for (uint8_t i
= 0; i
< num_props
; i
++) {
879 char * value
= _get_property_value_from_line(line
, line_len
, props
[i
], strlen(props
[i
]));
881 count
+= (found
[i
] != NULL
) & 0x1; // count newly found lines
885 if (count
== num_props
) {
889 line
= next_newline
+ 1;
890 remaining_size
= conf_end
- line
;
891 next_newline
= memchr(line
, '\n', remaining_size
);
892 if (next_newline
== NULL
) {
893 return; // valid property lines end with \n, but none in the config
895 *next_newline
= '\0';
902 _g_str_append_kv_jsonescaped(GString
*str
, const char *k
, const char *v
)
904 g_string_append_printf(str
, "\"%s\": \"", k
);
907 if (*v
== '\\' || *v
== '"') {
908 g_string_append_c(str
, '\\');
910 g_string_append_c(str
, *v
);
913 g_string_append_c(str
, '"');
917 _print_found_properties(
929 _get_property_values(values
, conf
, size
, props
, num_props
, min
, max
);
932 for (uint8_t i
= 0; i
< num_props
; i
++) {
933 if (values
[i
] == NULL
) {
937 g_string_append_c(str
, ',');
940 g_string_append_printf(str
, ",\n");
944 g_string_append_printf(str
, "\"%u\":{", vmid
);
947 _g_str_append_kv_jsonescaped(str
, props
[i
], values
[i
]);
951 g_string_append_c(str
, '}');
958 cfs_create_guest_conf_properties_msg(GString
*str
, memdb_t
*memdb
, const char **props
, uint8_t num_props
, uint32_t vmid
)
960 g_return_val_if_fail(cfs_status
.vmlist
!= NULL
, -EINVAL
);
961 g_return_val_if_fail(str
!= NULL
, -EINVAL
);
963 // Prelock &memdb->mutex in order to enable the usage of memdb_read_nolock
964 // to prevent Deadlocks as in #2553
965 g_mutex_lock (&memdb
->mutex
);
966 g_mutex_lock (&mutex
);
968 g_string_printf(str
, "{\n");
970 GHashTable
*ht
= cfs_status
.vmlist
;
973 GString
*path
= NULL
;
975 char **values
= calloc(num_props
, sizeof(char*));
976 char min
= 'z', max
= 'a';
978 for (uint8_t i
= 0; i
< num_props
; i
++) {
979 if (props
[i
][0] > max
) {
983 if (props
[i
][0] < min
) {
988 if (!g_hash_table_size(ht
)) {
992 if ((path
= g_string_sized_new(256)) == NULL
) {
998 vminfo_t
*vminfo
= (vminfo_t
*) g_hash_table_lookup(cfs_status
.vmlist
, &vmid
);
999 if (vminfo
== NULL
) goto enoent
;
1001 if (!vminfo_to_path(vminfo
, path
)) goto err
;
1003 // use memdb_read_nolock because lock is handled here
1004 int size
= memdb_read_nolock(memdb
, path
->str
, &tmp
);
1005 if (tmp
== NULL
) goto err
;
1007 // conf needs to be newline terminated
1008 if (((char *)tmp
)[size
- 1] != '\n') {
1009 gpointer
new = realloc(tmp
, size
+ 1);
1010 if (new == NULL
) goto err
;
1012 ((char *)tmp
)[size
++] = '\n';
1014 _print_found_properties(str
, tmp
, size
, props
, num_props
, vmid
, values
, min
, max
, 1);
1016 GHashTableIter iter
;
1017 g_hash_table_iter_init (&iter
, ht
);
1019 gpointer key
, value
;
1021 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
1022 vminfo_t
*vminfo
= (vminfo_t
*)value
;
1024 if (!vminfo_to_path(vminfo
, path
)) goto err
;
1026 g_free(tmp
); // no-op if already null
1028 // use memdb_read_nolock because lock is handled here
1029 int size
= memdb_read_nolock(memdb
, path
->str
, &tmp
);
1030 if (tmp
== NULL
) continue;
1032 // conf needs to be newline terminated
1033 if (((char *)tmp
)[size
- 1] != '\n') {
1034 gpointer
new = realloc(tmp
, size
+ 1);
1035 if (new == NULL
) continue;
1037 ((char *)tmp
)[size
++] = '\n';
1040 memset(values
, 0, sizeof(char*)*num_props
); // reset array
1041 first
= _print_found_properties(str
, tmp
, size
, props
, num_props
,
1042 vminfo
->vmid
, values
, min
, max
, first
);
1049 g_string_free(path
, TRUE
);
1051 g_string_append_printf(str
,"\n}\n");
1052 g_mutex_unlock (&mutex
);
1053 g_mutex_unlock (&memdb
->mutex
);
1064 cfs_create_guest_conf_property_msg(GString
*str
, memdb_t
*memdb
, const char *prop
, uint32_t vmid
)
1066 return cfs_create_guest_conf_properties_msg(str
, memdb
, &prop
, 1, vmid
);
1070 record_memdb_change(const char *path
)
1072 g_return_if_fail(cfs_status
.memdb_changes
!= 0);
1076 if ((ce
= (memdb_change_t
*)g_hash_table_lookup(cfs_status
.memdb_changes
, path
))) {
1082 record_memdb_reload(void)
1084 for (int i
= 0; i
< G_N_ELEMENTS(memdb_change_array
); i
++) {
1085 memdb_change_array
[i
].version
++;
1096 g_return_val_if_fail(kvhash
!= NULL
, FALSE
);
1097 g_return_val_if_fail(key
!= NULL
, FALSE
);
1098 g_return_val_if_fail(data
!= NULL
, FALSE
);
1102 g_hash_table_remove(kvhash
, key
);
1103 } else if ((entry
= (kventry_t
*)g_hash_table_lookup(kvhash
, key
))) {
1104 g_free(entry
->data
);
1105 entry
->data
= g_memdup(data
, len
);
1109 kventry_t
*entry
= g_new0(kventry_t
, 1);
1111 entry
->key
= g_strdup(key
);
1112 entry
->data
= g_memdup(data
, len
);
1115 g_hash_table_replace(kvhash
, entry
->key
, entry
);
1121 static const char *rrd_def_node
[] = {
1122 "DS:loadavg:GAUGE:120:0:U",
1123 "DS:maxcpu:GAUGE:120:0:U",
1124 "DS:cpu:GAUGE:120:0:U",
1125 "DS:iowait:GAUGE:120:0:U",
1126 "DS:memtotal:GAUGE:120:0:U",
1127 "DS:memused:GAUGE:120:0:U",
1128 "DS:swaptotal:GAUGE:120:0:U",
1129 "DS:swapused:GAUGE:120:0:U",
1130 "DS:roottotal:GAUGE:120:0:U",
1131 "DS:rootused:GAUGE:120:0:U",
1132 "DS:netin:DERIVE:120:0:U",
1133 "DS:netout:DERIVE:120:0:U",
1135 "RRA:AVERAGE:0.5:1:70", // 1 min avg - one hour
1136 "RRA:AVERAGE:0.5:30:70", // 30 min avg - one day
1137 "RRA:AVERAGE:0.5:180:70", // 3 hour avg - one week
1138 "RRA:AVERAGE:0.5:720:70", // 12 hour avg - one month
1139 "RRA:AVERAGE:0.5:10080:70", // 7 day avg - ony year
1141 "RRA:MAX:0.5:1:70", // 1 min max - one hour
1142 "RRA:MAX:0.5:30:70", // 30 min max - one day
1143 "RRA:MAX:0.5:180:70", // 3 hour max - one week
1144 "RRA:MAX:0.5:720:70", // 12 hour max - one month
1145 "RRA:MAX:0.5:10080:70", // 7 day max - ony year
1149 static const char *rrd_def_vm
[] = {
1150 "DS:maxcpu:GAUGE:120:0:U",
1151 "DS:cpu:GAUGE:120:0:U",
1152 "DS:maxmem:GAUGE:120:0:U",
1153 "DS:mem:GAUGE:120:0:U",
1154 "DS:maxdisk:GAUGE:120:0:U",
1155 "DS:disk:GAUGE:120:0:U",
1156 "DS:netin:DERIVE:120:0:U",
1157 "DS:netout:DERIVE:120:0:U",
1158 "DS:diskread:DERIVE:120:0:U",
1159 "DS:diskwrite:DERIVE:120:0:U",
1161 "RRA:AVERAGE:0.5:1:70", // 1 min avg - one hour
1162 "RRA:AVERAGE:0.5:30:70", // 30 min avg - one day
1163 "RRA:AVERAGE:0.5:180:70", // 3 hour avg - one week
1164 "RRA:AVERAGE:0.5:720:70", // 12 hour avg - one month
1165 "RRA:AVERAGE:0.5:10080:70", // 7 day avg - ony year
1167 "RRA:MAX:0.5:1:70", // 1 min max - one hour
1168 "RRA:MAX:0.5:30:70", // 30 min max - one day
1169 "RRA:MAX:0.5:180:70", // 3 hour max - one week
1170 "RRA:MAX:0.5:720:70", // 12 hour max - one month
1171 "RRA:MAX:0.5:10080:70", // 7 day max - ony year
1175 static const char *rrd_def_storage
[] = {
1176 "DS:total:GAUGE:120:0:U",
1177 "DS:used:GAUGE:120:0:U",
1179 "RRA:AVERAGE:0.5:1:70", // 1 min avg - one hour
1180 "RRA:AVERAGE:0.5:30:70", // 30 min avg - one day
1181 "RRA:AVERAGE:0.5:180:70", // 3 hour avg - one week
1182 "RRA:AVERAGE:0.5:720:70", // 12 hour avg - one month
1183 "RRA:AVERAGE:0.5:10080:70", // 7 day avg - ony year
1185 "RRA:MAX:0.5:1:70", // 1 min max - one hour
1186 "RRA:MAX:0.5:30:70", // 30 min max - one day
1187 "RRA:MAX:0.5:180:70", // 3 hour max - one week
1188 "RRA:MAX:0.5:720:70", // 12 hour max - one month
1189 "RRA:MAX:0.5:10080:70", // 7 day max - ony year
1193 #define RRDDIR "/var/lib/rrdcached/db"
1197 const char *filename
,
1199 const char *rrddef
[])
1201 /* start at day boundary */
1204 struct tm
*ltm
= localtime(&ctime
);
1210 if (rrd_create_r(filename
, 60, timelocal(ltm
), argcount
, rrddef
)) {
1211 cfs_message("RRD create error %s: %s", filename
, rrd_get_error());
1215 static inline const char *
1221 while (*data
&& found
< count
) {
1234 g_return_if_fail(key
!= NULL
);
1235 g_return_if_fail(data
!= NULL
);
1236 g_return_if_fail(len
> 0);
1237 g_return_if_fail(len
< 4096);
1239 static const char *rrdcsock
= "unix:/var/run/rrdcached.sock";
1242 if (rrdc_connect(rrdcsock
) != 0)
1245 char *filename
= NULL
;
1249 if (strncmp(key
, "pve2-node/", 10) == 0) {
1250 const char *node
= key
+ 10;
1254 if (strchr(node
, '/') != NULL
)
1257 if (strlen(node
) < 1)
1260 filename
= g_strdup_printf(RRDDIR
"/%s", key
);
1262 if (!g_file_test(filename
, G_FILE_TEST_EXISTS
)) {
1264 mkdir(RRDDIR
"/pve2-node", 0755);
1265 int argcount
= sizeof(rrd_def_node
)/sizeof(void*) - 1;
1266 create_rrd_file(filename
, argcount
, rrd_def_node
);
1269 } else if ((strncmp(key
, "pve2-vm/", 8) == 0) ||
1270 (strncmp(key
, "pve2.3-vm/", 10) == 0)) {
1273 if (strncmp(key
, "pve2-vm/", 8) == 0) {
1281 if (strchr(vmid
, '/') != NULL
)
1284 if (strlen(vmid
) < 1)
1287 filename
= g_strdup_printf(RRDDIR
"/%s/%s", "pve2-vm", vmid
);
1289 if (!g_file_test(filename
, G_FILE_TEST_EXISTS
)) {
1291 mkdir(RRDDIR
"/pve2-vm", 0755);
1292 int argcount
= sizeof(rrd_def_vm
)/sizeof(void*) - 1;
1293 create_rrd_file(filename
, argcount
, rrd_def_vm
);
1296 } else if (strncmp(key
, "pve2-storage/", 13) == 0) {
1297 const char *node
= key
+ 13;
1299 const char *storage
= node
;
1300 while (*storage
&& *storage
!= '/')
1303 if (*storage
!= '/' || ((storage
- node
) < 1))
1308 if (strchr(storage
, '/') != NULL
)
1311 if (strlen(storage
) < 1)
1314 filename
= g_strdup_printf(RRDDIR
"/%s", key
);
1316 if (!g_file_test(filename
, G_FILE_TEST_EXISTS
)) {
1318 mkdir(RRDDIR
"/pve2-storage", 0755);
1320 char *dir
= g_path_get_dirname(filename
);
1324 int argcount
= sizeof(rrd_def_storage
)/sizeof(void*) - 1;
1325 create_rrd_file(filename
, argcount
, rrd_def_storage
);
1332 const char *dp
= skip
? rrd_skip_data(data
, skip
) : data
;
1334 const char *update_args
[] = { dp
, NULL
};
1338 if ((status
= rrdc_update(filename
, 1, update_args
)) != 0) {
1339 cfs_message("RRDC update error %s: %d", filename
, status
);
1342 if (rrd_update_r(filename
, NULL
, 1, update_args
) != 0) {
1343 cfs_message("RRD update error %s: %s", filename
, rrd_get_error());
1349 if (rrd_update_r(filename
, NULL
, 1, update_args
) != 0) {
1350 cfs_message("RRD update error %s: %s", filename
, rrd_get_error());
1361 cfs_critical("RRD update error: unknown/wrong key %s", key
);
1371 rrdentry_t
*entry
= (rrdentry_t
*)value
;
1372 uint32_t ctime
= GPOINTER_TO_UINT(user_data
);
1374 int diff
= ctime
- entry
->time
;
1376 /* remove everything older than 5 minutes */
1379 return (diff
> expire
) ? TRUE
: FALSE
;
1382 static char *rrd_dump_buf
= NULL
;
1383 static time_t rrd_dump_last
= 0;
1386 cfs_rrd_dump(GString
*str
)
1390 g_mutex_lock (&mutex
);
1393 if (rrd_dump_buf
&& (ctime
- rrd_dump_last
) < 2) {
1394 g_string_assign(str
, rrd_dump_buf
);
1395 g_mutex_unlock (&mutex
);
1399 /* remove old data */
1400 g_hash_table_foreach_remove(cfs_status
.rrdhash
, rrd_entry_is_old
,
1401 GUINT_TO_POINTER(ctime
));
1403 g_string_set_size(str
, 0);
1405 GHashTableIter iter
;
1406 gpointer key
, value
;
1408 g_hash_table_iter_init (&iter
, cfs_status
.rrdhash
);
1410 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
1411 rrdentry_t
*entry
= (rrdentry_t
*)value
;
1412 g_string_append(str
, key
);
1413 g_string_append(str
, ":");
1414 g_string_append(str
, entry
->data
);
1415 g_string_append(str
, "\n");
1418 g_string_append_c(str
, 0); // never return undef
1420 rrd_dump_last
= ctime
;
1422 g_free(rrd_dump_buf
);
1423 rrd_dump_buf
= g_strdup(str
->str
);
1425 g_mutex_unlock (&mutex
);
1431 const char *nodename
,
1435 g_return_val_if_fail(iphash
!= NULL
, FALSE
);
1436 g_return_val_if_fail(nodename
!= NULL
, FALSE
);
1437 g_return_val_if_fail(ip
!= NULL
, FALSE
);
1438 g_return_val_if_fail(len
> 0, FALSE
);
1439 g_return_val_if_fail(len
< 256, FALSE
);
1440 g_return_val_if_fail(ip
[len
-1] == 0, FALSE
);
1442 char *oldip
= (char *)g_hash_table_lookup(iphash
, nodename
);
1444 if (!oldip
|| (strcmp(oldip
, ip
) != 0)) {
1445 cfs_status
.clinfo_version
++;
1446 g_hash_table_replace(iphash
, g_strdup(nodename
), g_strdup(ip
));
1454 GHashTable
*rrdhash
,
1459 g_return_val_if_fail(rrdhash
!= NULL
, FALSE
);
1460 g_return_val_if_fail(key
!= NULL
, FALSE
);
1461 g_return_val_if_fail(data
!= NULL
, FALSE
);
1462 g_return_val_if_fail(len
> 0, FALSE
);
1463 g_return_val_if_fail(len
< 4096, FALSE
);
1464 g_return_val_if_fail(data
[len
-1] == 0, FALSE
);
1467 if ((entry
= (rrdentry_t
*)g_hash_table_lookup(rrdhash
, key
))) {
1468 g_free(entry
->data
);
1469 entry
->data
= g_memdup(data
, len
);
1471 entry
->time
= time(NULL
);
1473 rrdentry_t
*entry
= g_new0(rrdentry_t
, 1);
1475 entry
->key
= g_strdup(key
);
1476 entry
->data
= g_memdup(data
, len
);
1478 entry
->time
= time(NULL
);
1480 g_hash_table_replace(rrdhash
, entry
->key
, entry
);
1483 update_rrd_data(key
, data
, len
);
1489 kvstore_send_update_message(
1495 if (!dfsm_is_initialized(dfsm
))
1498 struct iovec iov
[2];
1501 g_strlcpy(name
, key
, sizeof(name
));
1503 iov
[0].iov_base
= &name
;
1504 iov
[0].iov_len
= sizeof(name
);
1506 iov
[1].iov_base
= (char *)data
;
1507 iov
[1].iov_len
= len
;
1509 if (dfsm_send_message(dfsm
, KVSTORE_MESSAGE_UPDATE
, iov
, 2) == CS_OK
)
1515 static clog_entry_t
*
1516 kvstore_parse_log_message(
1520 g_return_val_if_fail(msg
!= NULL
, NULL
);
1522 if (msg_len
< sizeof(clog_entry_t
)) {
1523 cfs_critical("received short log message (%zu < %zu)", msg_len
, sizeof(clog_entry_t
));
1527 clog_entry_t
*entry
= (clog_entry_t
*)msg
;
1529 uint32_t size
= sizeof(clog_entry_t
) + entry
->node_len
+
1530 entry
->ident_len
+ entry
->tag_len
+ entry
->msg_len
;
1532 if (msg_len
!= size
) {
1533 cfs_critical("received log message with wrong size (%zu != %u)", msg_len
, size
);
1537 char *msgptr
= entry
->data
;
1539 if (*((char *)msgptr
+ entry
->node_len
- 1)) {
1540 cfs_critical("unterminated string in log message");
1543 msgptr
+= entry
->node_len
;
1545 if (*((char *)msgptr
+ entry
->ident_len
- 1)) {
1546 cfs_critical("unterminated string in log message");
1549 msgptr
+= entry
->ident_len
;
1551 if (*((char *)msgptr
+ entry
->tag_len
- 1)) {
1552 cfs_critical("unterminated string in log message");
1555 msgptr
+= entry
->tag_len
;
1557 if (*((char *)msgptr
+ entry
->msg_len
- 1)) {
1558 cfs_critical("unterminated string in log message");
1566 kvstore_parse_update_message(
1570 gconstpointer
*data
,
1573 g_return_val_if_fail(msg
!= NULL
, FALSE
);
1574 g_return_val_if_fail(key
!= NULL
, FALSE
);
1575 g_return_val_if_fail(data
!= NULL
, FALSE
);
1576 g_return_val_if_fail(len
!= NULL
, FALSE
);
1578 if (msg_len
< 256) {
1579 cfs_critical("received short kvstore message (%zu < 256)", msg_len
);
1583 /* test if key is null terminated */
1585 for (i
= 0; i
< 256; i
++)
1586 if (((char *)msg
)[i
] == 0)
1593 *len
= msg_len
- 256;
1595 *data
= (char *) msg
+ 256;
1601 cfs_create_status_msg(
1603 const char *nodename
,
1606 g_return_val_if_fail(str
!= NULL
, -EINVAL
);
1607 g_return_val_if_fail(key
!= NULL
, -EINVAL
);
1611 GHashTable
*kvhash
= NULL
;
1613 g_mutex_lock (&mutex
);
1615 if (!nodename
|| !nodename
[0] || !strcmp(nodename
, cfs
.nodename
)) {
1616 kvhash
= cfs_status
.kvhash
;
1617 } else if (cfs_status
.clinfo
&& cfs_status
.clinfo
->nodes_byname
) {
1618 cfs_clnode_t
*clnode
;
1619 if ((clnode
= g_hash_table_lookup(cfs_status
.clinfo
->nodes_byname
, nodename
)))
1620 kvhash
= clnode
->kvhash
;
1624 if (kvhash
&& (entry
= (kventry_t
*)g_hash_table_lookup(kvhash
, key
))) {
1625 g_string_append_len(str
, entry
->data
, entry
->len
);
1629 g_mutex_unlock (&mutex
);
1640 g_return_val_if_fail(key
!= NULL
, FALSE
);
1641 g_return_val_if_fail(data
!= NULL
, FALSE
);
1642 g_return_val_if_fail(cfs_status
.kvhash
!= NULL
, FALSE
);
1644 if (len
> CFS_MAX_STATUS_SIZE
)
1647 g_mutex_lock (&mutex
);
1651 if (strncmp(key
, "rrd/", 4) == 0) {
1652 res
= rrdentry_hash_set(cfs_status
.rrdhash
, key
+ 4, data
, len
);
1653 } else if (!strcmp(key
, "nodeip")) {
1654 res
= nodeip_hash_set(cfs_status
.iphash
, cfs
.nodename
, data
, len
);
1656 res
= kventry_hash_set(cfs_status
.kvhash
, key
, data
, len
);
1658 g_mutex_unlock (&mutex
);
1660 if (cfs_status
.kvstore
)
1661 kvstore_send_update_message(cfs_status
.kvstore
, key
, data
, len
);
1663 return res
? 0 : -ENOMEM
;
1667 cfs_kvstore_node_set(
1673 g_return_val_if_fail(nodeid
!= 0, FALSE
);
1674 g_return_val_if_fail(key
!= NULL
, FALSE
);
1675 g_return_val_if_fail(data
!= NULL
, FALSE
);
1677 g_mutex_lock (&mutex
);
1679 if (!cfs_status
.clinfo
|| !cfs_status
.clinfo
->nodes_byid
)
1680 goto ret
; /* ignore */
1682 cfs_clnode_t
*clnode
= g_hash_table_lookup(cfs_status
.clinfo
->nodes_byid
, &nodeid
);
1684 goto ret
; /* ignore */
1686 cfs_debug("got node %d status update %s", nodeid
, key
);
1688 if (strncmp(key
, "rrd/", 4) == 0) {
1689 rrdentry_hash_set(cfs_status
.rrdhash
, key
+ 4, data
, len
);
1690 } else if (!strcmp(key
, "nodeip")) {
1691 nodeip_hash_set(cfs_status
.iphash
, clnode
->name
, data
, len
);
1693 if (!clnode
->kvhash
) {
1694 if (!(clnode
->kvhash
= kventry_hash_new())) {
1695 goto ret
; /*ignore */
1699 kventry_hash_set(clnode
->kvhash
, key
, data
, len
);
1703 g_mutex_unlock (&mutex
);
1709 cfs_kvstore_sync(void)
1711 g_return_val_if_fail(cfs_status
.kvhash
!= NULL
, FALSE
);
1712 g_return_val_if_fail(cfs_status
.kvstore
!= NULL
, FALSE
);
1714 gboolean res
= TRUE
;
1716 g_mutex_lock (&mutex
);
1718 GHashTable
*ht
= cfs_status
.kvhash
;
1719 GHashTableIter iter
;
1720 gpointer key
, value
;
1722 g_hash_table_iter_init (&iter
, ht
);
1724 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
1725 kventry_t
*entry
= (kventry_t
*)value
;
1726 kvstore_send_update_message(cfs_status
.kvstore
, entry
->key
, entry
->data
, entry
->len
);
1729 g_mutex_unlock (&mutex
);
1746 g_return_val_if_fail(dfsm
!= NULL
, -1);
1747 g_return_val_if_fail(msg
!= NULL
, -1);
1748 g_return_val_if_fail(res_ptr
!= NULL
, -1);
1750 /* ignore message for ourself */
1751 if (dfsm_nodeid_is_local(dfsm
, nodeid
, pid
))
1754 if (msg_type
== KVSTORE_MESSAGE_UPDATE
) {
1758 if (kvstore_parse_update_message(msg
, msg_len
, &key
, &data
, &len
)) {
1759 cfs_kvstore_node_set(nodeid
, key
, data
, len
);
1761 cfs_critical("cant parse update message");
1763 } else if (msg_type
== KVSTORE_MESSAGE_LOG
) {
1764 cfs_message("received log"); // fixme: remove
1765 const clog_entry_t
*entry
;
1766 if ((entry
= kvstore_parse_log_message(msg
, msg_len
))) {
1767 clusterlog_insert(cfs_status
.clusterlog
, entry
);
1769 cfs_critical("cant parse log message");
1772 cfs_critical("received unknown message type %d\n", msg_type
);
1789 const struct cpg_address
*member_list
,
1790 size_t member_list_entries
)
1792 g_return_if_fail(dfsm
!= NULL
);
1793 g_return_if_fail(member_list
!= NULL
);
1795 cfs_debug("enter %s", __func__
);
1797 g_mutex_lock (&mutex
);
1799 cfs_clinfo_t
*clinfo
= cfs_status
.clinfo
;
1801 if (clinfo
&& clinfo
->nodes_byid
) {
1803 GHashTable
*ht
= clinfo
->nodes_byid
;
1804 GHashTableIter iter
;
1805 gpointer key
, value
;
1807 g_hash_table_iter_init (&iter
, ht
);
1809 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
1810 cfs_clnode_t
*node
= (cfs_clnode_t
*)value
;
1811 node
->online
= FALSE
;
1814 for (int i
= 0; i
< member_list_entries
; i
++) {
1816 if ((node
= g_hash_table_lookup(clinfo
->nodes_byid
, &member_list
[i
].nodeid
))) {
1817 node
->online
= TRUE
;
1821 cfs_status
.clinfo_version
++;
1824 g_mutex_unlock (&mutex
);
1831 unsigned int *res_len
)
1833 g_return_val_if_fail(dfsm
!= NULL
, NULL
);
1835 gpointer msg
= clusterlog_get_state(cfs_status
.clusterlog
, res_len
);
1841 dfsm_process_update(
1844 dfsm_sync_info_t
*syncinfo
,
1850 cfs_critical("%s: received unexpected update message", __func__
);
1856 dfsm_process_state_update(
1859 dfsm_sync_info_t
*syncinfo
)
1861 g_return_val_if_fail(dfsm
!= NULL
, -1);
1862 g_return_val_if_fail(syncinfo
!= NULL
, -1);
1864 clog_base_t
*clog
[syncinfo
->node_count
];
1866 int local_index
= -1;
1867 for (int i
= 0; i
< syncinfo
->node_count
; i
++) {
1868 dfsm_node_info_t
*ni
= &syncinfo
->nodes
[i
];
1871 if (syncinfo
->local
== ni
)
1874 clog_base_t
*base
= (clog_base_t
*)ni
->state
;
1875 if (ni
->state_len
> 8 && ni
->state_len
== clog_size(base
)) {
1876 clog
[i
] = ni
->state
;
1878 cfs_critical("received log with wrong size %u", ni
->state_len
);
1883 if (!clusterlog_merge(cfs_status
.clusterlog
, clog
, syncinfo
->node_count
, local_index
)) {
1884 cfs_critical("unable to merge log files");
1896 dfsm_sync_info_t
*syncinfo
)
1898 g_return_val_if_fail(dfsm
!= NULL
, -1);
1899 g_return_val_if_fail(syncinfo
!= NULL
, -1);
1905 dfsm_synced(dfsm_t
*dfsm
)
1907 g_return_if_fail(dfsm
!= NULL
);
1909 char *ip
= (char *)g_hash_table_lookup(cfs_status
.iphash
, cfs
.nodename
);
1913 cfs_status_set("nodeip", ip
, strlen(ip
) + 1);
1920 dfsm_sync_info_t
*syncinfo
)
1925 static dfsm_callbacks_t kvstore_dfsm_callbacks
= {
1926 .dfsm_deliver_fn
= dfsm_deliver
,
1927 .dfsm_confchg_fn
= dfsm_confchg
,
1929 .dfsm_get_state_fn
= dfsm_get_state
,
1930 .dfsm_process_state_update_fn
= dfsm_process_state_update
,
1931 .dfsm_process_update_fn
= dfsm_process_update
,
1932 .dfsm_commit_fn
= dfsm_commit
,
1933 .dfsm_cleanup_fn
= dfsm_cleanup
,
1934 .dfsm_synced_fn
= dfsm_synced
,
1938 cfs_status_dfsm_new(void)
1940 g_mutex_lock (&mutex
);
1942 cfs_status
.kvstore
= dfsm_new(NULL
, KVSTORE_CPG_GROUP_NAME
, G_LOG_DOMAIN
,
1943 0, &kvstore_dfsm_callbacks
);
1944 g_mutex_unlock (&mutex
);
1946 return cfs_status
.kvstore
;
1950 cfs_is_quorate(void)
1952 g_mutex_lock (&mutex
);
1953 gboolean res
= cfs_status
.quorate
;
1954 g_mutex_unlock (&mutex
);
1964 g_mutex_lock (&mutex
);
1966 uint32_t prev_quorate
= cfs_status
.quorate
;
1967 cfs_status
.quorate
= quorate
;
1969 if (!prev_quorate
&& cfs_status
.quorate
) {
1971 cfs_message("node has quorum");
1974 if (prev_quorate
&& !cfs_status
.quorate
) {
1976 cfs_message("node lost quorum");
1979 g_mutex_unlock (&mutex
);