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>
37 #include "cfs-utils.h"
41 #define KVSTORE_CPG_GROUP_NAME "pve_kvstore_v1"
44 KVSTORE_MESSAGE_UPDATE
= 1,
45 KVSTORE_MESSAGE_UPDATE_COMPLETE
= 2,
46 KVSTORE_MESSAGE_LOG
= 3,
49 static uint32_t vminfo_version_counter
;
77 static memdb_change_t memdb_change_array
[] = {
78 { .path
= "corosync.conf" },
79 { .path
= "corosync.conf.new" },
80 { .path
= "storage.cfg" },
81 { .path
= "user.cfg" },
82 { .path
= "domains.cfg" },
83 { .path
= "priv/shadow.cfg" },
84 { .path
= "datacenter.cfg" },
85 { .path
= "vzdump.cron" },
86 { .path
= "ha/crm_commands" },
87 { .path
= "ha/manager_status" },
88 { .path
= "ha/resources.cfg" },
89 { .path
= "ha/groups.cfg" },
90 { .path
= "ha/fence.cfg" },
91 { .path
= "status.cfg" },
92 { .path
= "replication.cfg" },
102 cfs_clinfo_t
*clinfo
;
103 uint32_t clinfo_version
;
106 uint32_t vmlist_version
;
113 GHashTable
*memdb_changes
;
115 clusterlog_t
*clusterlog
;
118 static cfs_status_t cfs_status
;
130 uint32_t cman_version
;
132 GHashTable
*nodes_byid
;
133 GHashTable
*nodes_byname
;
137 g_int32_hash (gconstpointer v
)
139 return *(const uint32_t *) v
;
143 g_int32_equal (gconstpointer v1
,
146 return *((const uint32_t*) v1
) == *((const uint32_t*) v2
);
149 static void vminfo_free(vminfo_t
*vminfo
)
151 g_return_if_fail(vminfo
!= NULL
);
153 if (vminfo
->nodename
)
154 g_free(vminfo
->nodename
);
160 void cfs_clnode_destroy(
161 cfs_clnode_t
*clnode
)
163 g_return_if_fail(clnode
!= NULL
);
166 g_hash_table_destroy(clnode
->kvhash
);
169 g_free(clnode
->name
);
174 cfs_clnode_t
*cfs_clnode_new(
179 g_return_val_if_fail(name
!= NULL
, NULL
);
181 cfs_clnode_t
*clnode
= g_new0(cfs_clnode_t
, 1);
185 clnode
->name
= g_strdup(name
);
186 clnode
->nodeid
= nodeid
;
187 clnode
->votes
= votes
;
192 gboolean
cfs_clinfo_destroy(
193 cfs_clinfo_t
*clinfo
)
195 g_return_val_if_fail(clinfo
!= NULL
, FALSE
);
197 if (clinfo
->cluster_name
)
198 g_free(clinfo
->cluster_name
);
200 if (clinfo
->nodes_byname
)
201 g_hash_table_destroy(clinfo
->nodes_byname
);
203 if (clinfo
->nodes_byid
)
204 g_hash_table_destroy(clinfo
->nodes_byid
);
211 cfs_clinfo_t
*cfs_clinfo_new(
212 const char *cluster_name
,
213 uint32_t cman_version
)
215 g_return_val_if_fail(cluster_name
!= NULL
, NULL
);
217 cfs_clinfo_t
*clinfo
= g_new0(cfs_clinfo_t
, 1);
221 clinfo
->cluster_name
= g_strdup(cluster_name
);
222 clinfo
->cman_version
= cman_version
;
224 if (!(clinfo
->nodes_byid
= g_hash_table_new_full(
225 g_int32_hash
, g_int32_equal
, NULL
,
226 (GDestroyNotify
)cfs_clnode_destroy
)))
229 if (!(clinfo
->nodes_byname
= g_hash_table_new(g_str_hash
, g_str_equal
)))
235 cfs_clinfo_destroy(clinfo
);
240 gboolean
cfs_clinfo_add_node(
241 cfs_clinfo_t
*clinfo
,
242 cfs_clnode_t
*clnode
)
244 g_return_val_if_fail(clinfo
!= NULL
, FALSE
);
245 g_return_val_if_fail(clnode
!= NULL
, FALSE
);
247 g_hash_table_replace(clinfo
->nodes_byid
, &clnode
->nodeid
, clnode
);
248 g_hash_table_replace(clinfo
->nodes_byname
, clnode
->name
, clnode
);
254 cfs_create_memberlist_msg(
257 g_return_val_if_fail(str
!= NULL
, -EINVAL
);
259 g_mutex_lock (&mutex
);
261 g_string_append_printf(str
,"{\n");
265 cfs_clinfo_t
*clinfo
= cfs_status
.clinfo
;
267 if (clinfo
&& clinfo
->nodes_byid
)
268 nodecount
= g_hash_table_size(clinfo
->nodes_byid
);
271 g_string_append_printf(str
, "\"nodename\": \"%s\",\n", cfs
.nodename
);
272 g_string_append_printf(str
, "\"version\": %u,\n", cfs_status
.clinfo_version
);
274 g_string_append_printf(str
, "\"cluster\": { ");
275 g_string_append_printf(str
, "\"name\": \"%s\", \"version\": %d, "
276 "\"nodes\": %d, \"quorate\": %d ",
277 clinfo
->cluster_name
, clinfo
->cman_version
,
278 nodecount
, cfs_status
.quorate
);
280 g_string_append_printf(str
,"},\n");
281 g_string_append_printf(str
,"\"nodelist\": {\n");
283 GHashTable
*ht
= clinfo
->nodes_byid
;
287 g_hash_table_iter_init (&iter
, ht
);
290 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
291 cfs_clnode_t
*node
= (cfs_clnode_t
*)value
;
292 if (i
) g_string_append_printf(str
, ",\n");
295 g_string_append_printf(str
, " \"%s\": { \"id\": %d, \"online\": %d",
296 node
->name
, node
->nodeid
, node
->online
);
299 char *ip
= (char *)g_hash_table_lookup(cfs_status
.iphash
, node
->name
);
301 g_string_append_printf(str
, ", \"ip\": \"%s\"", ip
);
304 g_string_append_printf(str
, "}");
307 g_string_append_printf(str
,"\n }\n");
309 g_string_append_printf(str
, "\"nodename\": \"%s\",\n", cfs
.nodename
);
310 g_string_append_printf(str
, "\"version\": %u\n", cfs_status
.clinfo_version
);
313 g_string_append_printf(str
,"}\n");
315 g_mutex_unlock (&mutex
);
321 kventry_free(kventry_t
*entry
)
323 g_return_if_fail(entry
!= NULL
);
331 kventry_hash_new(void)
333 return g_hash_table_new_full(g_str_hash
, g_str_equal
, NULL
,
334 (GDestroyNotify
)kventry_free
);
338 rrdentry_free(rrdentry_t
*entry
)
340 g_return_if_fail(entry
!= NULL
);
348 rrdentry_hash_new(void)
350 return g_hash_table_new_full(g_str_hash
, g_str_equal
, NULL
,
351 (GDestroyNotify
)rrdentry_free
);
355 cfs_cluster_log_dump(GString
*str
, const char *user
, guint max_entries
)
357 clusterlog_dump(cfs_status
.clusterlog
, str
, user
, max_entries
);
361 cfs_cluster_log(clog_entry_t
*entry
)
363 g_return_if_fail(entry
!= NULL
);
365 clusterlog_insert(cfs_status
.clusterlog
, entry
);
367 if (cfs_status
.kvstore
) {
369 iov
[0].iov_base
= (char *)entry
;
370 iov
[0].iov_len
= clog_entry_size(entry
);
372 if (dfsm_is_initialized(cfs_status
.kvstore
))
373 dfsm_send_message(cfs_status
.kvstore
, KVSTORE_MESSAGE_LOG
, iov
, 1);
377 void cfs_status_init(void)
379 g_mutex_lock (&mutex
);
381 cfs_status
.start_time
= time(NULL
);
383 cfs_status
.vmlist
= vmlist_hash_new();
385 cfs_status
.kvhash
= kventry_hash_new();
387 cfs_status
.rrdhash
= rrdentry_hash_new();
389 cfs_status
.iphash
= g_hash_table_new_full(g_str_hash
, g_str_equal
, g_free
, g_free
);
391 cfs_status
.memdb_changes
= g_hash_table_new(g_str_hash
, g_str_equal
);
393 for (int i
= 0; i
< G_N_ELEMENTS(memdb_change_array
); i
++) {
394 g_hash_table_replace(cfs_status
.memdb_changes
,
395 memdb_change_array
[i
].path
,
396 &memdb_change_array
[i
]);
399 cfs_status
.clusterlog
= clusterlog_new();
402 clusterlog_add(cfs_status
.clusterlog
, "root", "cluster", getpid(),
403 LOG_INFO
, "starting cluster log");
405 g_mutex_unlock (&mutex
);
408 void cfs_status_cleanup(void)
410 g_mutex_lock (&mutex
);
412 cfs_status
.clinfo_version
++;
414 if (cfs_status
.clinfo
) {
415 cfs_clinfo_destroy(cfs_status
.clinfo
);
416 cfs_status
.clinfo
= NULL
;
419 if (cfs_status
.vmlist
) {
420 g_hash_table_destroy(cfs_status
.vmlist
);
421 cfs_status
.vmlist
= NULL
;
424 if (cfs_status
.kvhash
) {
425 g_hash_table_destroy(cfs_status
.kvhash
);
426 cfs_status
.kvhash
= NULL
;
429 if (cfs_status
.rrdhash
) {
430 g_hash_table_destroy(cfs_status
.rrdhash
);
431 cfs_status
.rrdhash
= NULL
;
434 if (cfs_status
.iphash
) {
435 g_hash_table_destroy(cfs_status
.iphash
);
436 cfs_status
.iphash
= NULL
;
439 if (cfs_status
.clusterlog
)
440 clusterlog_destroy(cfs_status
.clusterlog
);
442 g_mutex_unlock (&mutex
);
445 void cfs_status_set_clinfo(
446 cfs_clinfo_t
*clinfo
)
448 g_return_if_fail(clinfo
!= NULL
);
450 g_mutex_lock (&mutex
);
452 cfs_status
.clinfo_version
++;
454 cfs_clinfo_t
*old
= cfs_status
.clinfo
;
456 cfs_status
.clinfo
= clinfo
;
458 cfs_message("update cluster info (cluster name %s, version = %d)",
459 clinfo
->cluster_name
, clinfo
->cman_version
);
462 if (old
&& old
->nodes_byid
&& clinfo
->nodes_byid
) {
464 GHashTable
*ht
= clinfo
->nodes_byid
;
468 g_hash_table_iter_init (&iter
, ht
);
470 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
471 cfs_clnode_t
*node
= (cfs_clnode_t
*)value
;
472 cfs_clnode_t
*oldnode
;
473 if ((oldnode
= g_hash_table_lookup(old
->nodes_byid
, key
))) {
474 node
->online
= oldnode
->online
;
475 node
->kvhash
= oldnode
->kvhash
;
476 oldnode
->kvhash
= NULL
;
483 cfs_clinfo_destroy(old
);
486 g_mutex_unlock (&mutex
);
490 dump_kvstore_versions(
493 const char *nodename
)
495 g_return_if_fail(kvhash
!= NULL
);
496 g_return_if_fail(str
!= NULL
);
497 g_return_if_fail(nodename
!= NULL
);
499 GHashTable
*ht
= kvhash
;
503 g_string_append_printf(str
, "\"%s\": {\n", nodename
);
505 g_hash_table_iter_init (&iter
, ht
);
508 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
509 kventry_t
*entry
= (kventry_t
*)value
;
510 if (i
) g_string_append_printf(str
, ",\n");
512 g_string_append_printf(str
,"\"%s\": %u", entry
->key
, entry
->version
);
515 g_string_append_printf(str
, "}\n");
519 cfs_create_version_msg(GString
*str
)
521 g_return_val_if_fail(str
!= NULL
, -EINVAL
);
523 g_mutex_lock (&mutex
);
525 g_string_append_printf(str
,"{\n");
527 g_string_append_printf(str
, "\"starttime\": %lu,\n", (unsigned long)cfs_status
.start_time
);
529 g_string_append_printf(str
, "\"clinfo\": %u,\n", cfs_status
.clinfo_version
);
531 g_string_append_printf(str
, "\"vmlist\": %u,\n", cfs_status
.vmlist_version
);
533 for (int i
= 0; i
< G_N_ELEMENTS(memdb_change_array
); i
++) {
534 g_string_append_printf(str
, "\"%s\": %u,\n",
535 memdb_change_array
[i
].path
,
536 memdb_change_array
[i
].version
);
539 g_string_append_printf(str
, "\"kvstore\": {\n");
541 dump_kvstore_versions(str
, cfs_status
.kvhash
, cfs
.nodename
);
543 cfs_clinfo_t
*clinfo
= cfs_status
.clinfo
;
545 if (clinfo
&& clinfo
->nodes_byid
) {
546 GHashTable
*ht
= clinfo
->nodes_byid
;
550 g_hash_table_iter_init (&iter
, ht
);
552 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
553 cfs_clnode_t
*node
= (cfs_clnode_t
*)value
;
556 g_string_append_printf(str
, ",\n");
557 dump_kvstore_versions(str
, node
->kvhash
, node
->name
);
561 g_string_append_printf(str
,"}\n");
563 g_string_append_printf(str
,"}\n");
565 g_mutex_unlock (&mutex
);
571 vmlist_hash_new(void)
573 return g_hash_table_new_full(g_int_hash
, g_int_equal
, NULL
,
574 (GDestroyNotify
)vminfo_free
);
578 vmlist_hash_insert_vm(
582 const char *nodename
,
585 g_return_val_if_fail(vmlist
!= NULL
, FALSE
);
586 g_return_val_if_fail(nodename
!= NULL
, FALSE
);
587 g_return_val_if_fail(vmid
!= 0, FALSE
);
588 g_return_val_if_fail(vmtype
== VMTYPE_QEMU
|| vmtype
== VMTYPE_OPENVZ
||
589 vmtype
== VMTYPE_LXC
, FALSE
);
591 if (!replace
&& g_hash_table_lookup(vmlist
, &vmid
)) {
592 cfs_critical("detected duplicate VMID %d", vmid
);
596 vminfo_t
*vminfo
= g_new0(vminfo_t
, 1);
599 vminfo
->vmtype
= vmtype
;
600 vminfo
->nodename
= g_strdup(nodename
);
602 vminfo
->version
= ++vminfo_version_counter
;
604 g_hash_table_replace(vmlist
, &vminfo
->vmid
, vminfo
);
613 const char *nodename
)
615 g_return_if_fail(cfs_status
.vmlist
!= NULL
);
616 g_return_if_fail(nodename
!= NULL
);
617 g_return_if_fail(vmid
!= 0);
618 g_return_if_fail(vmtype
== VMTYPE_QEMU
|| vmtype
== VMTYPE_OPENVZ
||
619 vmtype
== VMTYPE_LXC
);
621 cfs_debug("vmlist_register_vm: %s/%u %d", nodename
, vmid
, vmtype
);
623 g_mutex_lock (&mutex
);
625 cfs_status
.vmlist_version
++;
627 vmlist_hash_insert_vm(cfs_status
.vmlist
, vmtype
, vmid
, nodename
, TRUE
);
629 g_mutex_unlock (&mutex
);
633 vmlist_different_vm_exists(
636 const char *nodename
)
638 g_return_val_if_fail(cfs_status
.vmlist
!= NULL
, FALSE
);
639 g_return_val_if_fail(vmid
!= 0, FALSE
);
641 gboolean res
= FALSE
;
643 g_mutex_lock (&mutex
);
646 if ((vminfo
= (vminfo_t
*)g_hash_table_lookup(cfs_status
.vmlist
, &vmid
))) {
647 if (!(vminfo
->vmtype
== vmtype
&& strcmp(vminfo
->nodename
, nodename
) == 0))
650 g_mutex_unlock (&mutex
);
659 g_return_val_if_fail(cfs_status
.vmlist
!= NULL
, FALSE
);
660 g_return_val_if_fail(vmid
!= 0, FALSE
);
662 g_mutex_lock (&mutex
);
664 gpointer res
= g_hash_table_lookup(cfs_status
.vmlist
, &vmid
);
666 g_mutex_unlock (&mutex
);
675 g_return_if_fail(cfs_status
.vmlist
!= NULL
);
676 g_return_if_fail(vmid
!= 0);
678 g_mutex_lock (&mutex
);
680 cfs_status
.vmlist_version
++;
682 g_hash_table_remove(cfs_status
.vmlist
, &vmid
);
684 g_mutex_unlock (&mutex
);
687 void cfs_status_set_vmlist(
690 g_return_if_fail(vmlist
!= NULL
);
692 g_mutex_lock (&mutex
);
694 cfs_status
.vmlist_version
++;
696 if (cfs_status
.vmlist
)
697 g_hash_table_destroy(cfs_status
.vmlist
);
699 cfs_status
.vmlist
= vmlist
;
701 g_mutex_unlock (&mutex
);
705 cfs_create_vmlist_msg(GString
*str
)
707 g_return_val_if_fail(cfs_status
.vmlist
!= NULL
, -EINVAL
);
708 g_return_val_if_fail(str
!= NULL
, -EINVAL
);
710 g_mutex_lock (&mutex
);
712 g_string_append_printf(str
,"{\n");
714 GHashTable
*ht
= cfs_status
.vmlist
;
716 guint count
= g_hash_table_size(ht
);
719 g_string_append_printf(str
,"\"version\": %u\n", cfs_status
.vmlist_version
);
721 g_string_append_printf(str
,"\"version\": %u,\n", cfs_status
.vmlist_version
);
723 g_string_append_printf(str
,"\"ids\": {\n");
728 g_hash_table_iter_init (&iter
, ht
);
731 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
732 vminfo_t
*vminfo
= (vminfo_t
*)value
;
734 if (vminfo
->vmtype
== VMTYPE_QEMU
) {
736 } else if (vminfo
->vmtype
== VMTYPE_OPENVZ
) {
738 } else if (vminfo
->vmtype
== VMTYPE_LXC
) {
745 g_string_append_printf(str
, ",\n");
748 g_string_append_printf(str
,"\"%u\": { \"node\": \"%s\", \"type\": \"%s\", \"version\": %u }",
749 vminfo
->vmid
, vminfo
->nodename
, type
, vminfo
->version
);
752 g_string_append_printf(str
,"}\n");
754 g_string_append_printf(str
,"\n}\n");
756 g_mutex_unlock (&mutex
);
762 record_memdb_change(const char *path
)
764 g_return_if_fail(cfs_status
.memdb_changes
!= 0);
768 if ((ce
= (memdb_change_t
*)g_hash_table_lookup(cfs_status
.memdb_changes
, path
))) {
774 record_memdb_reload(void)
776 for (int i
= 0; i
< G_N_ELEMENTS(memdb_change_array
); i
++) {
777 memdb_change_array
[i
].version
++;
788 g_return_val_if_fail(kvhash
!= NULL
, FALSE
);
789 g_return_val_if_fail(key
!= NULL
, FALSE
);
790 g_return_val_if_fail(data
!= NULL
, FALSE
);
793 if ((entry
= (kventry_t
*)g_hash_table_lookup(kvhash
, key
))) {
795 entry
->data
= g_memdup(data
, len
);
799 kventry_t
*entry
= g_new0(kventry_t
, 1);
801 entry
->key
= g_strdup(key
);
802 entry
->data
= g_memdup(data
, len
);
805 g_hash_table_replace(kvhash
, entry
->key
, entry
);
811 static const char *rrd_def_node
[] = {
812 "DS:loadavg:GAUGE:120:0:U",
813 "DS:maxcpu:GAUGE:120:0:U",
814 "DS:cpu:GAUGE:120:0:U",
815 "DS:iowait:GAUGE:120:0:U",
816 "DS:memtotal:GAUGE:120:0:U",
817 "DS:memused:GAUGE:120:0:U",
818 "DS:swaptotal:GAUGE:120:0:U",
819 "DS:swapused:GAUGE:120:0:U",
820 "DS:roottotal:GAUGE:120:0:U",
821 "DS:rootused:GAUGE:120:0:U",
822 "DS:netin:DERIVE:120:0:U",
823 "DS:netout:DERIVE:120:0:U",
825 "RRA:AVERAGE:0.5:1:70", // 1 min avg - one hour
826 "RRA:AVERAGE:0.5:30:70", // 30 min avg - one day
827 "RRA:AVERAGE:0.5:180:70", // 3 hour avg - one week
828 "RRA:AVERAGE:0.5:720:70", // 12 hour avg - one month
829 "RRA:AVERAGE:0.5:10080:70", // 7 day avg - ony year
831 "RRA:MAX:0.5:1:70", // 1 min max - one hour
832 "RRA:MAX:0.5:30:70", // 30 min max - one day
833 "RRA:MAX:0.5:180:70", // 3 hour max - one week
834 "RRA:MAX:0.5:720:70", // 12 hour max - one month
835 "RRA:MAX:0.5:10080:70", // 7 day max - ony year
839 static const char *rrd_def_vm
[] = {
840 "DS:maxcpu:GAUGE:120:0:U",
841 "DS:cpu:GAUGE:120:0:U",
842 "DS:maxmem:GAUGE:120:0:U",
843 "DS:mem:GAUGE:120:0:U",
844 "DS:maxdisk:GAUGE:120:0:U",
845 "DS:disk:GAUGE:120:0:U",
846 "DS:netin:DERIVE:120:0:U",
847 "DS:netout:DERIVE:120:0:U",
848 "DS:diskread:DERIVE:120:0:U",
849 "DS:diskwrite:DERIVE:120:0:U",
851 "RRA:AVERAGE:0.5:1:70", // 1 min avg - one hour
852 "RRA:AVERAGE:0.5:30:70", // 30 min avg - one day
853 "RRA:AVERAGE:0.5:180:70", // 3 hour avg - one week
854 "RRA:AVERAGE:0.5:720:70", // 12 hour avg - one month
855 "RRA:AVERAGE:0.5:10080:70", // 7 day avg - ony year
857 "RRA:MAX:0.5:1:70", // 1 min max - one hour
858 "RRA:MAX:0.5:30:70", // 30 min max - one day
859 "RRA:MAX:0.5:180:70", // 3 hour max - one week
860 "RRA:MAX:0.5:720:70", // 12 hour max - one month
861 "RRA:MAX:0.5:10080:70", // 7 day max - ony year
865 static const char *rrd_def_storage
[] = {
866 "DS:total:GAUGE:120:0:U",
867 "DS:used:GAUGE:120:0:U",
869 "RRA:AVERAGE:0.5:1:70", // 1 min avg - one hour
870 "RRA:AVERAGE:0.5:30:70", // 30 min avg - one day
871 "RRA:AVERAGE:0.5:180:70", // 3 hour avg - one week
872 "RRA:AVERAGE:0.5:720:70", // 12 hour avg - one month
873 "RRA:AVERAGE:0.5:10080:70", // 7 day avg - ony year
875 "RRA:MAX:0.5:1:70", // 1 min max - one hour
876 "RRA:MAX:0.5:30:70", // 30 min max - one day
877 "RRA:MAX:0.5:180:70", // 3 hour max - one week
878 "RRA:MAX:0.5:720:70", // 12 hour max - one month
879 "RRA:MAX:0.5:10080:70", // 7 day max - ony year
883 #define RRDDIR "/var/lib/rrdcached/db"
887 const char *filename
,
889 const char *rrddef
[])
891 /* start at day boundary */
894 struct tm
*ltm
= localtime(&ctime
);
900 if (rrd_create_r(filename
, 60, timelocal(ltm
), argcount
, rrddef
)) {
901 cfs_message("RRD create error %s: %s", filename
, rrd_get_error());
905 static inline const char *
911 while (*data
&& found
< count
) {
924 g_return_if_fail(key
!= NULL
);
925 g_return_if_fail(data
!= NULL
);
926 g_return_if_fail(len
> 0);
927 g_return_if_fail(len
< 4096);
929 static const char *rrdcsock
= "unix:/var/run/rrdcached.sock";
932 if (rrdc_connect(rrdcsock
) != 0)
935 char *filename
= NULL
;
939 if (strncmp(key
, "pve2-node/", 10) == 0) {
940 const char *node
= key
+ 10;
944 if (strchr(node
, '/') != NULL
)
947 if (strlen(node
) < 1)
950 filename
= g_strdup_printf(RRDDIR
"/%s", key
);
952 if (!g_file_test(filename
, G_FILE_TEST_EXISTS
)) {
954 mkdir(RRDDIR
"/pve2-node", 0755);
955 int argcount
= sizeof(rrd_def_node
)/sizeof(void*) - 1;
956 create_rrd_file(filename
, argcount
, rrd_def_node
);
959 } else if ((strncmp(key
, "pve2-vm/", 8) == 0) ||
960 (strncmp(key
, "pve2.3-vm/", 10) == 0)) {
963 if (strncmp(key
, "pve2-vm/", 8) == 0) {
971 if (strchr(vmid
, '/') != NULL
)
974 if (strlen(vmid
) < 1)
977 filename
= g_strdup_printf(RRDDIR
"/%s/%s", "pve2-vm", vmid
);
979 if (!g_file_test(filename
, G_FILE_TEST_EXISTS
)) {
981 mkdir(RRDDIR
"/pve2-vm", 0755);
982 int argcount
= sizeof(rrd_def_vm
)/sizeof(void*) - 1;
983 create_rrd_file(filename
, argcount
, rrd_def_vm
);
986 } else if (strncmp(key
, "pve2-storage/", 13) == 0) {
987 const char *node
= key
+ 13;
989 const char *storage
= node
;
990 while (*storage
&& *storage
!= '/')
993 if (*storage
!= '/' || ((storage
- node
) < 1))
998 if (strchr(storage
, '/') != NULL
)
1001 if (strlen(storage
) < 1)
1004 filename
= g_strdup_printf(RRDDIR
"/%s", key
);
1006 if (!g_file_test(filename
, G_FILE_TEST_EXISTS
)) {
1008 mkdir(RRDDIR
"/pve2-storage", 0755);
1010 char *dir
= g_path_get_dirname(filename
);
1014 int argcount
= sizeof(rrd_def_storage
)/sizeof(void*) - 1;
1015 create_rrd_file(filename
, argcount
, rrd_def_storage
);
1022 const char *dp
= skip
? rrd_skip_data(data
, skip
) : data
;
1024 const char *update_args
[] = { dp
, NULL
};
1028 if ((status
= rrdc_update(filename
, 1, update_args
)) != 0) {
1029 cfs_message("RRDC update error %s: %d", filename
, status
);
1032 if (rrd_update_r(filename
, NULL
, 1, update_args
) != 0) {
1033 cfs_message("RRD update error %s: %s", filename
, rrd_get_error());
1039 if (rrd_update_r(filename
, NULL
, 1, update_args
) != 0) {
1040 cfs_message("RRD update error %s: %s", filename
, rrd_get_error());
1051 cfs_critical("RRD update error: unknown/wrong key %s", key
);
1061 rrdentry_t
*entry
= (rrdentry_t
*)value
;
1062 uint32_t ctime
= GPOINTER_TO_UINT(user_data
);
1064 int diff
= ctime
- entry
->time
;
1066 /* remove everything older than 5 minutes */
1069 return (diff
> expire
) ? TRUE
: FALSE
;
1072 static char *rrd_dump_buf
= NULL
;
1073 static time_t rrd_dump_last
= 0;
1076 cfs_rrd_dump(GString
*str
)
1080 g_mutex_lock (&mutex
);
1083 if (rrd_dump_buf
&& (ctime
- rrd_dump_last
) < 2) {
1084 g_string_assign(str
, rrd_dump_buf
);
1085 g_mutex_unlock (&mutex
);
1089 /* remove old data */
1090 g_hash_table_foreach_remove(cfs_status
.rrdhash
, rrd_entry_is_old
,
1091 GUINT_TO_POINTER(ctime
));
1093 g_string_set_size(str
, 0);
1095 GHashTableIter iter
;
1096 gpointer key
, value
;
1098 g_hash_table_iter_init (&iter
, cfs_status
.rrdhash
);
1100 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
1101 rrdentry_t
*entry
= (rrdentry_t
*)value
;
1102 g_string_append(str
, key
);
1103 g_string_append(str
, ":");
1104 g_string_append(str
, entry
->data
);
1105 g_string_append(str
, "\n");
1108 g_string_append_c(str
, 0); // never return undef
1110 rrd_dump_last
= ctime
;
1112 g_free(rrd_dump_buf
);
1113 rrd_dump_buf
= g_strdup(str
->str
);
1115 g_mutex_unlock (&mutex
);
1121 const char *nodename
,
1125 g_return_val_if_fail(iphash
!= NULL
, FALSE
);
1126 g_return_val_if_fail(nodename
!= NULL
, FALSE
);
1127 g_return_val_if_fail(ip
!= NULL
, FALSE
);
1128 g_return_val_if_fail(len
> 0, FALSE
);
1129 g_return_val_if_fail(len
< 256, FALSE
);
1130 g_return_val_if_fail(ip
[len
-1] == 0, FALSE
);
1132 char *oldip
= (char *)g_hash_table_lookup(iphash
, nodename
);
1134 if (!oldip
|| (strcmp(oldip
, ip
) != 0)) {
1135 cfs_status
.clinfo_version
++;
1136 g_hash_table_replace(iphash
, g_strdup(nodename
), g_strdup(ip
));
1144 GHashTable
*rrdhash
,
1149 g_return_val_if_fail(rrdhash
!= NULL
, FALSE
);
1150 g_return_val_if_fail(key
!= NULL
, FALSE
);
1151 g_return_val_if_fail(data
!= NULL
, FALSE
);
1152 g_return_val_if_fail(len
> 0, FALSE
);
1153 g_return_val_if_fail(len
< 4096, FALSE
);
1154 g_return_val_if_fail(data
[len
-1] == 0, FALSE
);
1157 if ((entry
= (rrdentry_t
*)g_hash_table_lookup(rrdhash
, key
))) {
1158 g_free(entry
->data
);
1159 entry
->data
= g_memdup(data
, len
);
1161 entry
->time
= time(NULL
);
1163 rrdentry_t
*entry
= g_new0(rrdentry_t
, 1);
1165 entry
->key
= g_strdup(key
);
1166 entry
->data
= g_memdup(data
, len
);
1168 entry
->time
= time(NULL
);
1170 g_hash_table_replace(rrdhash
, entry
->key
, entry
);
1173 update_rrd_data(key
, data
, len
);
1179 kvstore_send_update_message(
1185 if (!dfsm_is_initialized(dfsm
))
1188 struct iovec iov
[2];
1191 g_strlcpy(name
, key
, sizeof(name
));
1193 iov
[0].iov_base
= &name
;
1194 iov
[0].iov_len
= sizeof(name
);
1196 iov
[1].iov_base
= (char *)data
;
1197 iov
[1].iov_len
= len
;
1199 if (dfsm_send_message(dfsm
, KVSTORE_MESSAGE_UPDATE
, iov
, 2) == CS_OK
)
1205 static clog_entry_t
*
1206 kvstore_parse_log_message(
1210 g_return_val_if_fail(msg
!= NULL
, NULL
);
1212 if (msg_len
< sizeof(clog_entry_t
)) {
1213 cfs_critical("received short log message (%zu < %zu)", msg_len
, sizeof(clog_entry_t
));
1217 clog_entry_t
*entry
= (clog_entry_t
*)msg
;
1219 uint32_t size
= sizeof(clog_entry_t
) + entry
->node_len
+
1220 entry
->ident_len
+ entry
->tag_len
+ entry
->msg_len
;
1222 if (msg_len
!= size
) {
1223 cfs_critical("received log message with wrong size (%zu != %u)", msg_len
, size
);
1229 if (*((char *)msg
+ entry
->node_len
- 1)) {
1230 cfs_critical("unterminated string in log message");
1233 msg
+= entry
->node_len
;
1235 if (*((char *)msg
+ entry
->ident_len
- 1)) {
1236 cfs_critical("unterminated string in log message");
1239 msg
+= entry
->ident_len
;
1241 if (*((char *)msg
+ entry
->tag_len
- 1)) {
1242 cfs_critical("unterminated string in log message");
1245 msg
+= entry
->tag_len
;
1247 if (*((char *)msg
+ entry
->msg_len
- 1)) {
1248 cfs_critical("unterminated string in log message");
1256 kvstore_parse_update_message(
1260 gconstpointer
*data
,
1263 g_return_val_if_fail(msg
!= NULL
, FALSE
);
1264 g_return_val_if_fail(key
!= NULL
, FALSE
);
1265 g_return_val_if_fail(data
!= NULL
, FALSE
);
1266 g_return_val_if_fail(len
!= NULL
, FALSE
);
1268 if (msg_len
< 256) {
1269 cfs_critical("received short kvstore message (%zu < 256)", msg_len
);
1273 /* test if key is null terminated */
1275 for (i
= 0; i
< 256; i
++)
1276 if (((char *)msg
)[i
] == 0)
1283 *len
= msg_len
- 256;
1291 cfs_create_status_msg(
1293 const char *nodename
,
1296 g_return_val_if_fail(str
!= NULL
, -EINVAL
);
1297 g_return_val_if_fail(key
!= NULL
, -EINVAL
);
1301 GHashTable
*kvhash
= NULL
;
1303 g_mutex_lock (&mutex
);
1305 if (!nodename
|| !nodename
[0] || !strcmp(nodename
, cfs
.nodename
)) {
1306 kvhash
= cfs_status
.kvhash
;
1307 } else if (cfs_status
.clinfo
&& cfs_status
.clinfo
->nodes_byname
) {
1308 cfs_clnode_t
*clnode
;
1309 if ((clnode
= g_hash_table_lookup(cfs_status
.clinfo
->nodes_byname
, nodename
)))
1310 kvhash
= clnode
->kvhash
;
1314 if (kvhash
&& (entry
= (kventry_t
*)g_hash_table_lookup(kvhash
, key
))) {
1315 g_string_append_len(str
, entry
->data
, entry
->len
);
1319 g_mutex_unlock (&mutex
);
1330 g_return_val_if_fail(key
!= NULL
, FALSE
);
1331 g_return_val_if_fail(data
!= NULL
, FALSE
);
1332 g_return_val_if_fail(cfs_status
.kvhash
!= NULL
, FALSE
);
1334 if (len
> CFS_MAX_STATUS_SIZE
)
1337 g_mutex_lock (&mutex
);
1341 if (strncmp(key
, "rrd/", 4) == 0) {
1342 res
= rrdentry_hash_set(cfs_status
.rrdhash
, key
+ 4, data
, len
);
1343 } else if (!strcmp(key
, "nodeip")) {
1344 res
= nodeip_hash_set(cfs_status
.iphash
, cfs
.nodename
, data
, len
);
1346 res
= kventry_hash_set(cfs_status
.kvhash
, key
, data
, len
);
1348 g_mutex_unlock (&mutex
);
1350 if (cfs_status
.kvstore
)
1351 kvstore_send_update_message(cfs_status
.kvstore
, key
, data
, len
);
1353 return res
? 0 : -ENOMEM
;
1357 cfs_kvstore_node_set(
1363 g_return_val_if_fail(nodeid
!= 0, FALSE
);
1364 g_return_val_if_fail(key
!= NULL
, FALSE
);
1365 g_return_val_if_fail(data
!= NULL
, FALSE
);
1367 g_mutex_lock (&mutex
);
1369 if (!cfs_status
.clinfo
|| !cfs_status
.clinfo
->nodes_byid
)
1370 goto ret
; /* ignore */
1372 cfs_clnode_t
*clnode
= g_hash_table_lookup(cfs_status
.clinfo
->nodes_byid
, &nodeid
);
1374 goto ret
; /* ignore */
1376 cfs_debug("got node %d status update %s", nodeid
, key
);
1378 if (strncmp(key
, "rrd/", 4) == 0) {
1379 rrdentry_hash_set(cfs_status
.rrdhash
, key
+ 4, data
, len
);
1380 } else if (!strcmp(key
, "nodeip")) {
1381 nodeip_hash_set(cfs_status
.iphash
, clnode
->name
, data
, len
);
1383 if (!clnode
->kvhash
) {
1384 if (!(clnode
->kvhash
= kventry_hash_new())) {
1385 goto ret
; /*ignore */
1389 kventry_hash_set(clnode
->kvhash
, key
, data
, len
);
1393 g_mutex_unlock (&mutex
);
1399 cfs_kvstore_sync(void)
1401 g_return_val_if_fail(cfs_status
.kvhash
!= NULL
, FALSE
);
1402 g_return_val_if_fail(cfs_status
.kvstore
!= NULL
, FALSE
);
1404 gboolean res
= TRUE
;
1406 g_mutex_lock (&mutex
);
1408 GHashTable
*ht
= cfs_status
.kvhash
;
1409 GHashTableIter iter
;
1410 gpointer key
, value
;
1412 g_hash_table_iter_init (&iter
, ht
);
1414 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
1415 kventry_t
*entry
= (kventry_t
*)value
;
1416 kvstore_send_update_message(cfs_status
.kvstore
, entry
->key
, entry
->data
, entry
->len
);
1419 g_mutex_unlock (&mutex
);
1436 g_return_val_if_fail(dfsm
!= NULL
, -1);
1437 g_return_val_if_fail(msg
!= NULL
, -1);
1438 g_return_val_if_fail(res_ptr
!= NULL
, -1);
1440 /* ignore message for ourself */
1441 if (dfsm_nodeid_is_local(dfsm
, nodeid
, pid
))
1444 if (msg_type
== KVSTORE_MESSAGE_UPDATE
) {
1448 if (kvstore_parse_update_message(msg
, msg_len
, &key
, &data
, &len
)) {
1449 cfs_kvstore_node_set(nodeid
, key
, data
, len
);
1451 cfs_critical("cant parse update message");
1453 } else if (msg_type
== KVSTORE_MESSAGE_LOG
) {
1454 cfs_message("received log"); // fixme: remove
1455 const clog_entry_t
*entry
;
1456 if ((entry
= kvstore_parse_log_message(msg
, msg_len
))) {
1457 clusterlog_insert(cfs_status
.clusterlog
, entry
);
1459 cfs_critical("cant parse log message");
1462 cfs_critical("received unknown message type %d\n", msg_type
);
1479 const struct cpg_address
*member_list
,
1480 size_t member_list_entries
)
1482 g_return_if_fail(dfsm
!= NULL
);
1483 g_return_if_fail(member_list
!= NULL
);
1485 cfs_debug("enter %s", __func__
);
1487 g_mutex_lock (&mutex
);
1489 cfs_clinfo_t
*clinfo
= cfs_status
.clinfo
;
1491 if (clinfo
&& clinfo
->nodes_byid
) {
1493 GHashTable
*ht
= clinfo
->nodes_byid
;
1494 GHashTableIter iter
;
1495 gpointer key
, value
;
1497 g_hash_table_iter_init (&iter
, ht
);
1499 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
1500 cfs_clnode_t
*node
= (cfs_clnode_t
*)value
;
1501 node
->online
= FALSE
;
1504 for (int i
= 0; i
< member_list_entries
; i
++) {
1506 if ((node
= g_hash_table_lookup(clinfo
->nodes_byid
, &member_list
[i
].nodeid
))) {
1507 node
->online
= TRUE
;
1511 cfs_status
.clinfo_version
++;
1514 g_mutex_unlock (&mutex
);
1521 unsigned int *res_len
)
1523 g_return_val_if_fail(dfsm
!= NULL
, NULL
);
1525 gpointer msg
= clusterlog_get_state(cfs_status
.clusterlog
, res_len
);
1531 dfsm_process_update(
1534 dfsm_sync_info_t
*syncinfo
,
1540 cfs_critical("%s: received unexpected update message", __func__
);
1546 dfsm_process_state_update(
1549 dfsm_sync_info_t
*syncinfo
)
1551 g_return_val_if_fail(dfsm
!= NULL
, -1);
1552 g_return_val_if_fail(syncinfo
!= NULL
, -1);
1554 clog_base_t
*clog
[syncinfo
->node_count
];
1556 int local_index
= -1;
1557 for (int i
= 0; i
< syncinfo
->node_count
; i
++) {
1558 dfsm_node_info_t
*ni
= &syncinfo
->nodes
[i
];
1561 if (syncinfo
->local
== ni
)
1564 clog_base_t
*base
= (clog_base_t
*)ni
->state
;
1565 if (ni
->state_len
> 8 && ni
->state_len
== clog_size(base
)) {
1566 clog
[i
] = ni
->state
;
1568 cfs_critical("received log with wrong size %u", ni
->state_len
);
1573 if (!clusterlog_merge(cfs_status
.clusterlog
, clog
, syncinfo
->node_count
, local_index
)) {
1574 cfs_critical("unable to merge log files");
1586 dfsm_sync_info_t
*syncinfo
)
1588 g_return_val_if_fail(dfsm
!= NULL
, -1);
1589 g_return_val_if_fail(syncinfo
!= NULL
, -1);
1595 dfsm_synced(dfsm_t
*dfsm
)
1597 g_return_if_fail(dfsm
!= NULL
);
1599 char *ip
= (char *)g_hash_table_lookup(cfs_status
.iphash
, cfs
.nodename
);
1603 cfs_status_set("nodeip", ip
, strlen(ip
) + 1);
1610 dfsm_sync_info_t
*syncinfo
)
1615 static dfsm_callbacks_t kvstore_dfsm_callbacks
= {
1616 .dfsm_deliver_fn
= dfsm_deliver
,
1617 .dfsm_confchg_fn
= dfsm_confchg
,
1619 .dfsm_get_state_fn
= dfsm_get_state
,
1620 .dfsm_process_state_update_fn
= dfsm_process_state_update
,
1621 .dfsm_process_update_fn
= dfsm_process_update
,
1622 .dfsm_commit_fn
= dfsm_commit
,
1623 .dfsm_cleanup_fn
= dfsm_cleanup
,
1624 .dfsm_synced_fn
= dfsm_synced
,
1628 cfs_status_dfsm_new(void)
1630 g_mutex_lock (&mutex
);
1632 cfs_status
.kvstore
= dfsm_new(NULL
, KVSTORE_CPG_GROUP_NAME
, G_LOG_DOMAIN
,
1633 0, &kvstore_dfsm_callbacks
);
1634 g_mutex_unlock (&mutex
);
1636 return cfs_status
.kvstore
;
1640 cfs_is_quorate(void)
1642 g_mutex_lock (&mutex
);
1643 gboolean res
= cfs_status
.quorate
;
1644 g_mutex_unlock (&mutex
);
1654 g_mutex_lock (&mutex
);
1656 uint32_t prev_quorate
= cfs_status
.quorate
;
1657 cfs_status
.quorate
= quorate
;
1659 if (!prev_quorate
&& cfs_status
.quorate
) {
1661 cfs_message("node has quorum");
1664 if (prev_quorate
&& !cfs_status
.quorate
) {
1666 cfs_message("node lost quorum");
1669 g_mutex_unlock (&mutex
);