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
= "priv/tfa.cfg" },
85 { .path
= "datacenter.cfg" },
86 { .path
= "vzdump.cron" },
87 { .path
= "ha/crm_commands" },
88 { .path
= "ha/manager_status" },
89 { .path
= "ha/resources.cfg" },
90 { .path
= "ha/groups.cfg" },
91 { .path
= "ha/fence.cfg" },
92 { .path
= "status.cfg" },
93 { .path
= "replication.cfg" },
94 { .path
= "ceph.conf" },
104 cfs_clinfo_t
*clinfo
;
105 uint32_t clinfo_version
;
108 uint32_t vmlist_version
;
115 GHashTable
*memdb_changes
;
117 clusterlog_t
*clusterlog
;
120 static cfs_status_t cfs_status
;
132 uint32_t cman_version
;
134 GHashTable
*nodes_byid
;
135 GHashTable
*nodes_byname
;
139 g_int32_hash (gconstpointer v
)
141 return *(const uint32_t *) v
;
145 g_int32_equal (gconstpointer v1
,
148 return *((const uint32_t*) v1
) == *((const uint32_t*) v2
);
151 static void vminfo_free(vminfo_t
*vminfo
)
153 g_return_if_fail(vminfo
!= NULL
);
155 if (vminfo
->nodename
)
156 g_free(vminfo
->nodename
);
162 static const char *vminfo_type_to_string(vminfo_t
*vminfo
)
164 if (vminfo
->vmtype
== VMTYPE_QEMU
) {
166 } else if (vminfo
->vmtype
== VMTYPE_OPENVZ
) {
168 } else if (vminfo
->vmtype
== VMTYPE_LXC
) {
175 void cfs_clnode_destroy(
176 cfs_clnode_t
*clnode
)
178 g_return_if_fail(clnode
!= NULL
);
181 g_hash_table_destroy(clnode
->kvhash
);
184 g_free(clnode
->name
);
189 cfs_clnode_t
*cfs_clnode_new(
194 g_return_val_if_fail(name
!= NULL
, NULL
);
196 cfs_clnode_t
*clnode
= g_new0(cfs_clnode_t
, 1);
200 clnode
->name
= g_strdup(name
);
201 clnode
->nodeid
= nodeid
;
202 clnode
->votes
= votes
;
207 gboolean
cfs_clinfo_destroy(
208 cfs_clinfo_t
*clinfo
)
210 g_return_val_if_fail(clinfo
!= NULL
, FALSE
);
212 if (clinfo
->cluster_name
)
213 g_free(clinfo
->cluster_name
);
215 if (clinfo
->nodes_byname
)
216 g_hash_table_destroy(clinfo
->nodes_byname
);
218 if (clinfo
->nodes_byid
)
219 g_hash_table_destroy(clinfo
->nodes_byid
);
226 cfs_clinfo_t
*cfs_clinfo_new(
227 const char *cluster_name
,
228 uint32_t cman_version
)
230 g_return_val_if_fail(cluster_name
!= NULL
, NULL
);
232 cfs_clinfo_t
*clinfo
= g_new0(cfs_clinfo_t
, 1);
236 clinfo
->cluster_name
= g_strdup(cluster_name
);
237 clinfo
->cman_version
= cman_version
;
239 if (!(clinfo
->nodes_byid
= g_hash_table_new_full(
240 g_int32_hash
, g_int32_equal
, NULL
,
241 (GDestroyNotify
)cfs_clnode_destroy
)))
244 if (!(clinfo
->nodes_byname
= g_hash_table_new(g_str_hash
, g_str_equal
)))
250 cfs_clinfo_destroy(clinfo
);
255 gboolean
cfs_clinfo_add_node(
256 cfs_clinfo_t
*clinfo
,
257 cfs_clnode_t
*clnode
)
259 g_return_val_if_fail(clinfo
!= NULL
, FALSE
);
260 g_return_val_if_fail(clnode
!= NULL
, FALSE
);
262 g_hash_table_replace(clinfo
->nodes_byid
, &clnode
->nodeid
, clnode
);
263 g_hash_table_replace(clinfo
->nodes_byname
, clnode
->name
, clnode
);
269 cfs_create_memberlist_msg(
272 g_return_val_if_fail(str
!= NULL
, -EINVAL
);
274 g_mutex_lock (&mutex
);
276 g_string_append_printf(str
,"{\n");
280 cfs_clinfo_t
*clinfo
= cfs_status
.clinfo
;
282 if (clinfo
&& clinfo
->nodes_byid
)
283 nodecount
= g_hash_table_size(clinfo
->nodes_byid
);
286 g_string_append_printf(str
, "\"nodename\": \"%s\",\n", cfs
.nodename
);
287 g_string_append_printf(str
, "\"version\": %u,\n", cfs_status
.clinfo_version
);
289 g_string_append_printf(str
, "\"cluster\": { ");
290 g_string_append_printf(str
, "\"name\": \"%s\", \"version\": %d, "
291 "\"nodes\": %d, \"quorate\": %d ",
292 clinfo
->cluster_name
, clinfo
->cman_version
,
293 nodecount
, cfs_status
.quorate
);
295 g_string_append_printf(str
,"},\n");
296 g_string_append_printf(str
,"\"nodelist\": {\n");
298 GHashTable
*ht
= clinfo
->nodes_byid
;
302 g_hash_table_iter_init (&iter
, ht
);
305 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
306 cfs_clnode_t
*node
= (cfs_clnode_t
*)value
;
307 if (i
) g_string_append_printf(str
, ",\n");
310 g_string_append_printf(str
, " \"%s\": { \"id\": %d, \"online\": %d",
311 node
->name
, node
->nodeid
, node
->online
);
314 char *ip
= (char *)g_hash_table_lookup(cfs_status
.iphash
, node
->name
);
316 g_string_append_printf(str
, ", \"ip\": \"%s\"", ip
);
319 g_string_append_printf(str
, "}");
322 g_string_append_printf(str
,"\n }\n");
324 g_string_append_printf(str
, "\"nodename\": \"%s\",\n", cfs
.nodename
);
325 g_string_append_printf(str
, "\"version\": %u\n", cfs_status
.clinfo_version
);
328 g_string_append_printf(str
,"}\n");
330 g_mutex_unlock (&mutex
);
336 kventry_free(kventry_t
*entry
)
338 g_return_if_fail(entry
!= NULL
);
346 kventry_hash_new(void)
348 return g_hash_table_new_full(g_str_hash
, g_str_equal
, NULL
,
349 (GDestroyNotify
)kventry_free
);
353 rrdentry_free(rrdentry_t
*entry
)
355 g_return_if_fail(entry
!= NULL
);
363 rrdentry_hash_new(void)
365 return g_hash_table_new_full(g_str_hash
, g_str_equal
, NULL
,
366 (GDestroyNotify
)rrdentry_free
);
370 cfs_cluster_log_dump(GString
*str
, const char *user
, guint max_entries
)
372 clusterlog_dump(cfs_status
.clusterlog
, str
, user
, max_entries
);
376 cfs_cluster_log(clog_entry_t
*entry
)
378 g_return_if_fail(entry
!= NULL
);
380 clusterlog_insert(cfs_status
.clusterlog
, entry
);
382 if (cfs_status
.kvstore
) {
384 iov
[0].iov_base
= (char *)entry
;
385 iov
[0].iov_len
= clog_entry_size(entry
);
387 if (dfsm_is_initialized(cfs_status
.kvstore
))
388 dfsm_send_message(cfs_status
.kvstore
, KVSTORE_MESSAGE_LOG
, iov
, 1);
392 void cfs_status_init(void)
394 g_mutex_lock (&mutex
);
396 cfs_status
.start_time
= time(NULL
);
398 cfs_status
.vmlist
= vmlist_hash_new();
400 cfs_status
.kvhash
= kventry_hash_new();
402 cfs_status
.rrdhash
= rrdentry_hash_new();
404 cfs_status
.iphash
= g_hash_table_new_full(g_str_hash
, g_str_equal
, g_free
, g_free
);
406 cfs_status
.memdb_changes
= g_hash_table_new(g_str_hash
, g_str_equal
);
408 for (int i
= 0; i
< G_N_ELEMENTS(memdb_change_array
); i
++) {
409 g_hash_table_replace(cfs_status
.memdb_changes
,
410 memdb_change_array
[i
].path
,
411 &memdb_change_array
[i
]);
414 cfs_status
.clusterlog
= clusterlog_new();
417 clusterlog_add(cfs_status
.clusterlog
, "root", "cluster", getpid(),
418 LOG_INFO
, "starting cluster log");
420 g_mutex_unlock (&mutex
);
423 void cfs_status_cleanup(void)
425 g_mutex_lock (&mutex
);
427 cfs_status
.clinfo_version
++;
429 if (cfs_status
.clinfo
) {
430 cfs_clinfo_destroy(cfs_status
.clinfo
);
431 cfs_status
.clinfo
= NULL
;
434 if (cfs_status
.vmlist
) {
435 g_hash_table_destroy(cfs_status
.vmlist
);
436 cfs_status
.vmlist
= NULL
;
439 if (cfs_status
.kvhash
) {
440 g_hash_table_destroy(cfs_status
.kvhash
);
441 cfs_status
.kvhash
= NULL
;
444 if (cfs_status
.rrdhash
) {
445 g_hash_table_destroy(cfs_status
.rrdhash
);
446 cfs_status
.rrdhash
= NULL
;
449 if (cfs_status
.iphash
) {
450 g_hash_table_destroy(cfs_status
.iphash
);
451 cfs_status
.iphash
= NULL
;
454 if (cfs_status
.clusterlog
)
455 clusterlog_destroy(cfs_status
.clusterlog
);
457 g_mutex_unlock (&mutex
);
460 void cfs_status_set_clinfo(
461 cfs_clinfo_t
*clinfo
)
463 g_return_if_fail(clinfo
!= NULL
);
465 g_mutex_lock (&mutex
);
467 cfs_status
.clinfo_version
++;
469 cfs_clinfo_t
*old
= cfs_status
.clinfo
;
471 cfs_status
.clinfo
= clinfo
;
473 cfs_message("update cluster info (cluster name %s, version = %d)",
474 clinfo
->cluster_name
, clinfo
->cman_version
);
477 if (old
&& old
->nodes_byid
&& clinfo
->nodes_byid
) {
479 GHashTable
*ht
= clinfo
->nodes_byid
;
483 g_hash_table_iter_init (&iter
, ht
);
485 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
486 cfs_clnode_t
*node
= (cfs_clnode_t
*)value
;
487 cfs_clnode_t
*oldnode
;
488 if ((oldnode
= g_hash_table_lookup(old
->nodes_byid
, key
))) {
489 node
->online
= oldnode
->online
;
490 node
->kvhash
= oldnode
->kvhash
;
491 oldnode
->kvhash
= NULL
;
498 cfs_clinfo_destroy(old
);
501 g_mutex_unlock (&mutex
);
505 dump_kvstore_versions(
508 const char *nodename
)
510 g_return_if_fail(kvhash
!= NULL
);
511 g_return_if_fail(str
!= NULL
);
512 g_return_if_fail(nodename
!= NULL
);
514 GHashTable
*ht
= kvhash
;
518 g_string_append_printf(str
, "\"%s\": {\n", nodename
);
520 g_hash_table_iter_init (&iter
, ht
);
523 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
524 kventry_t
*entry
= (kventry_t
*)value
;
525 if (i
) g_string_append_printf(str
, ",\n");
527 g_string_append_printf(str
,"\"%s\": %u", entry
->key
, entry
->version
);
530 g_string_append_printf(str
, "}\n");
534 cfs_create_version_msg(GString
*str
)
536 g_return_val_if_fail(str
!= NULL
, -EINVAL
);
538 g_mutex_lock (&mutex
);
540 g_string_append_printf(str
,"{\n");
542 g_string_append_printf(str
, "\"starttime\": %lu,\n", (unsigned long)cfs_status
.start_time
);
544 g_string_append_printf(str
, "\"clinfo\": %u,\n", cfs_status
.clinfo_version
);
546 g_string_append_printf(str
, "\"vmlist\": %u,\n", cfs_status
.vmlist_version
);
548 for (int i
= 0; i
< G_N_ELEMENTS(memdb_change_array
); i
++) {
549 g_string_append_printf(str
, "\"%s\": %u,\n",
550 memdb_change_array
[i
].path
,
551 memdb_change_array
[i
].version
);
554 g_string_append_printf(str
, "\"kvstore\": {\n");
556 dump_kvstore_versions(str
, cfs_status
.kvhash
, cfs
.nodename
);
558 cfs_clinfo_t
*clinfo
= cfs_status
.clinfo
;
560 if (clinfo
&& clinfo
->nodes_byid
) {
561 GHashTable
*ht
= clinfo
->nodes_byid
;
565 g_hash_table_iter_init (&iter
, ht
);
567 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
568 cfs_clnode_t
*node
= (cfs_clnode_t
*)value
;
571 g_string_append_printf(str
, ",\n");
572 dump_kvstore_versions(str
, node
->kvhash
, node
->name
);
576 g_string_append_printf(str
,"}\n");
578 g_string_append_printf(str
,"}\n");
580 g_mutex_unlock (&mutex
);
586 vmlist_hash_new(void)
588 return g_hash_table_new_full(g_int_hash
, g_int_equal
, NULL
,
589 (GDestroyNotify
)vminfo_free
);
593 vmlist_hash_insert_vm(
597 const char *nodename
,
600 g_return_val_if_fail(vmlist
!= NULL
, FALSE
);
601 g_return_val_if_fail(nodename
!= NULL
, FALSE
);
602 g_return_val_if_fail(vmid
!= 0, FALSE
);
603 g_return_val_if_fail(vmtype
== VMTYPE_QEMU
|| vmtype
== VMTYPE_OPENVZ
||
604 vmtype
== VMTYPE_LXC
, FALSE
);
606 if (!replace
&& g_hash_table_lookup(vmlist
, &vmid
)) {
607 cfs_critical("detected duplicate VMID %d", vmid
);
611 vminfo_t
*vminfo
= g_new0(vminfo_t
, 1);
614 vminfo
->vmtype
= vmtype
;
615 vminfo
->nodename
= g_strdup(nodename
);
617 vminfo
->version
= ++vminfo_version_counter
;
619 g_hash_table_replace(vmlist
, &vminfo
->vmid
, vminfo
);
628 const char *nodename
)
630 g_return_if_fail(cfs_status
.vmlist
!= NULL
);
631 g_return_if_fail(nodename
!= NULL
);
632 g_return_if_fail(vmid
!= 0);
633 g_return_if_fail(vmtype
== VMTYPE_QEMU
|| vmtype
== VMTYPE_OPENVZ
||
634 vmtype
== VMTYPE_LXC
);
636 cfs_debug("vmlist_register_vm: %s/%u %d", nodename
, vmid
, vmtype
);
638 g_mutex_lock (&mutex
);
640 cfs_status
.vmlist_version
++;
642 vmlist_hash_insert_vm(cfs_status
.vmlist
, vmtype
, vmid
, nodename
, TRUE
);
644 g_mutex_unlock (&mutex
);
648 vmlist_different_vm_exists(
651 const char *nodename
)
653 g_return_val_if_fail(cfs_status
.vmlist
!= NULL
, FALSE
);
654 g_return_val_if_fail(vmid
!= 0, FALSE
);
656 gboolean res
= FALSE
;
658 g_mutex_lock (&mutex
);
661 if ((vminfo
= (vminfo_t
*)g_hash_table_lookup(cfs_status
.vmlist
, &vmid
))) {
662 if (!(vminfo
->vmtype
== vmtype
&& strcmp(vminfo
->nodename
, nodename
) == 0))
665 g_mutex_unlock (&mutex
);
674 g_return_val_if_fail(cfs_status
.vmlist
!= NULL
, FALSE
);
675 g_return_val_if_fail(vmid
!= 0, FALSE
);
677 g_mutex_lock (&mutex
);
679 gpointer res
= g_hash_table_lookup(cfs_status
.vmlist
, &vmid
);
681 g_mutex_unlock (&mutex
);
690 g_return_if_fail(cfs_status
.vmlist
!= NULL
);
691 g_return_if_fail(vmid
!= 0);
693 g_mutex_lock (&mutex
);
695 cfs_status
.vmlist_version
++;
697 g_hash_table_remove(cfs_status
.vmlist
, &vmid
);
699 g_mutex_unlock (&mutex
);
702 void cfs_status_set_vmlist(
705 g_return_if_fail(vmlist
!= NULL
);
707 g_mutex_lock (&mutex
);
709 cfs_status
.vmlist_version
++;
711 if (cfs_status
.vmlist
)
712 g_hash_table_destroy(cfs_status
.vmlist
);
714 cfs_status
.vmlist
= vmlist
;
716 g_mutex_unlock (&mutex
);
720 cfs_create_vmlist_msg(GString
*str
)
722 g_return_val_if_fail(cfs_status
.vmlist
!= NULL
, -EINVAL
);
723 g_return_val_if_fail(str
!= NULL
, -EINVAL
);
725 g_mutex_lock (&mutex
);
727 g_string_append_printf(str
,"{\n");
729 GHashTable
*ht
= cfs_status
.vmlist
;
731 guint count
= g_hash_table_size(ht
);
734 g_string_append_printf(str
,"\"version\": %u\n", cfs_status
.vmlist_version
);
736 g_string_append_printf(str
,"\"version\": %u,\n", cfs_status
.vmlist_version
);
738 g_string_append_printf(str
,"\"ids\": {\n");
743 g_hash_table_iter_init (&iter
, ht
);
746 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
747 vminfo_t
*vminfo
= (vminfo_t
*)value
;
748 const char *type
= vminfo_type_to_string(vminfo
);
751 g_string_append_printf(str
, ",\n");
754 g_string_append_printf(str
,"\"%u\": { \"node\": \"%s\", \"type\": \"%s\", \"version\": %u }",
755 vminfo
->vmid
, vminfo
->nodename
, type
, vminfo
->version
);
758 g_string_append_printf(str
,"}\n");
760 g_string_append_printf(str
,"\n}\n");
762 g_mutex_unlock (&mutex
);
768 record_memdb_change(const char *path
)
770 g_return_if_fail(cfs_status
.memdb_changes
!= 0);
774 if ((ce
= (memdb_change_t
*)g_hash_table_lookup(cfs_status
.memdb_changes
, path
))) {
780 record_memdb_reload(void)
782 for (int i
= 0; i
< G_N_ELEMENTS(memdb_change_array
); i
++) {
783 memdb_change_array
[i
].version
++;
794 g_return_val_if_fail(kvhash
!= NULL
, FALSE
);
795 g_return_val_if_fail(key
!= NULL
, FALSE
);
796 g_return_val_if_fail(data
!= NULL
, FALSE
);
800 g_hash_table_remove(kvhash
, key
);
801 } else if ((entry
= (kventry_t
*)g_hash_table_lookup(kvhash
, key
))) {
803 entry
->data
= g_memdup(data
, len
);
807 kventry_t
*entry
= g_new0(kventry_t
, 1);
809 entry
->key
= g_strdup(key
);
810 entry
->data
= g_memdup(data
, len
);
813 g_hash_table_replace(kvhash
, entry
->key
, entry
);
819 static const char *rrd_def_node
[] = {
820 "DS:loadavg:GAUGE:120:0:U",
821 "DS:maxcpu:GAUGE:120:0:U",
822 "DS:cpu:GAUGE:120:0:U",
823 "DS:iowait:GAUGE:120:0:U",
824 "DS:memtotal:GAUGE:120:0:U",
825 "DS:memused:GAUGE:120:0:U",
826 "DS:swaptotal:GAUGE:120:0:U",
827 "DS:swapused:GAUGE:120:0:U",
828 "DS:roottotal:GAUGE:120:0:U",
829 "DS:rootused:GAUGE:120:0:U",
830 "DS:netin:DERIVE:120:0:U",
831 "DS:netout:DERIVE:120:0:U",
833 "RRA:AVERAGE:0.5:1:70", // 1 min avg - one hour
834 "RRA:AVERAGE:0.5:30:70", // 30 min avg - one day
835 "RRA:AVERAGE:0.5:180:70", // 3 hour avg - one week
836 "RRA:AVERAGE:0.5:720:70", // 12 hour avg - one month
837 "RRA:AVERAGE:0.5:10080:70", // 7 day avg - ony year
839 "RRA:MAX:0.5:1:70", // 1 min max - one hour
840 "RRA:MAX:0.5:30:70", // 30 min max - one day
841 "RRA:MAX:0.5:180:70", // 3 hour max - one week
842 "RRA:MAX:0.5:720:70", // 12 hour max - one month
843 "RRA:MAX:0.5:10080:70", // 7 day max - ony year
847 static const char *rrd_def_vm
[] = {
848 "DS:maxcpu:GAUGE:120:0:U",
849 "DS:cpu:GAUGE:120:0:U",
850 "DS:maxmem:GAUGE:120:0:U",
851 "DS:mem:GAUGE:120:0:U",
852 "DS:maxdisk:GAUGE:120:0:U",
853 "DS:disk:GAUGE:120:0:U",
854 "DS:netin:DERIVE:120:0:U",
855 "DS:netout:DERIVE:120:0:U",
856 "DS:diskread:DERIVE:120:0:U",
857 "DS:diskwrite:DERIVE:120:0:U",
859 "RRA:AVERAGE:0.5:1:70", // 1 min avg - one hour
860 "RRA:AVERAGE:0.5:30:70", // 30 min avg - one day
861 "RRA:AVERAGE:0.5:180:70", // 3 hour avg - one week
862 "RRA:AVERAGE:0.5:720:70", // 12 hour avg - one month
863 "RRA:AVERAGE:0.5:10080:70", // 7 day avg - ony year
865 "RRA:MAX:0.5:1:70", // 1 min max - one hour
866 "RRA:MAX:0.5:30:70", // 30 min max - one day
867 "RRA:MAX:0.5:180:70", // 3 hour max - one week
868 "RRA:MAX:0.5:720:70", // 12 hour max - one month
869 "RRA:MAX:0.5:10080:70", // 7 day max - ony year
873 static const char *rrd_def_storage
[] = {
874 "DS:total:GAUGE:120:0:U",
875 "DS:used:GAUGE:120:0:U",
877 "RRA:AVERAGE:0.5:1:70", // 1 min avg - one hour
878 "RRA:AVERAGE:0.5:30:70", // 30 min avg - one day
879 "RRA:AVERAGE:0.5:180:70", // 3 hour avg - one week
880 "RRA:AVERAGE:0.5:720:70", // 12 hour avg - one month
881 "RRA:AVERAGE:0.5:10080:70", // 7 day avg - ony year
883 "RRA:MAX:0.5:1:70", // 1 min max - one hour
884 "RRA:MAX:0.5:30:70", // 30 min max - one day
885 "RRA:MAX:0.5:180:70", // 3 hour max - one week
886 "RRA:MAX:0.5:720:70", // 12 hour max - one month
887 "RRA:MAX:0.5:10080:70", // 7 day max - ony year
891 #define RRDDIR "/var/lib/rrdcached/db"
895 const char *filename
,
897 const char *rrddef
[])
899 /* start at day boundary */
902 struct tm
*ltm
= localtime(&ctime
);
908 if (rrd_create_r(filename
, 60, timelocal(ltm
), argcount
, rrddef
)) {
909 cfs_message("RRD create error %s: %s", filename
, rrd_get_error());
913 static inline const char *
919 while (*data
&& found
< count
) {
932 g_return_if_fail(key
!= NULL
);
933 g_return_if_fail(data
!= NULL
);
934 g_return_if_fail(len
> 0);
935 g_return_if_fail(len
< 4096);
937 static const char *rrdcsock
= "unix:/var/run/rrdcached.sock";
940 if (rrdc_connect(rrdcsock
) != 0)
943 char *filename
= NULL
;
947 if (strncmp(key
, "pve2-node/", 10) == 0) {
948 const char *node
= key
+ 10;
952 if (strchr(node
, '/') != NULL
)
955 if (strlen(node
) < 1)
958 filename
= g_strdup_printf(RRDDIR
"/%s", key
);
960 if (!g_file_test(filename
, G_FILE_TEST_EXISTS
)) {
962 mkdir(RRDDIR
"/pve2-node", 0755);
963 int argcount
= sizeof(rrd_def_node
)/sizeof(void*) - 1;
964 create_rrd_file(filename
, argcount
, rrd_def_node
);
967 } else if ((strncmp(key
, "pve2-vm/", 8) == 0) ||
968 (strncmp(key
, "pve2.3-vm/", 10) == 0)) {
971 if (strncmp(key
, "pve2-vm/", 8) == 0) {
979 if (strchr(vmid
, '/') != NULL
)
982 if (strlen(vmid
) < 1)
985 filename
= g_strdup_printf(RRDDIR
"/%s/%s", "pve2-vm", vmid
);
987 if (!g_file_test(filename
, G_FILE_TEST_EXISTS
)) {
989 mkdir(RRDDIR
"/pve2-vm", 0755);
990 int argcount
= sizeof(rrd_def_vm
)/sizeof(void*) - 1;
991 create_rrd_file(filename
, argcount
, rrd_def_vm
);
994 } else if (strncmp(key
, "pve2-storage/", 13) == 0) {
995 const char *node
= key
+ 13;
997 const char *storage
= node
;
998 while (*storage
&& *storage
!= '/')
1001 if (*storage
!= '/' || ((storage
- node
) < 1))
1006 if (strchr(storage
, '/') != NULL
)
1009 if (strlen(storage
) < 1)
1012 filename
= g_strdup_printf(RRDDIR
"/%s", key
);
1014 if (!g_file_test(filename
, G_FILE_TEST_EXISTS
)) {
1016 mkdir(RRDDIR
"/pve2-storage", 0755);
1018 char *dir
= g_path_get_dirname(filename
);
1022 int argcount
= sizeof(rrd_def_storage
)/sizeof(void*) - 1;
1023 create_rrd_file(filename
, argcount
, rrd_def_storage
);
1030 const char *dp
= skip
? rrd_skip_data(data
, skip
) : data
;
1032 const char *update_args
[] = { dp
, NULL
};
1036 if ((status
= rrdc_update(filename
, 1, update_args
)) != 0) {
1037 cfs_message("RRDC update error %s: %d", filename
, status
);
1040 if (rrd_update_r(filename
, NULL
, 1, update_args
) != 0) {
1041 cfs_message("RRD update error %s: %s", filename
, rrd_get_error());
1047 if (rrd_update_r(filename
, NULL
, 1, update_args
) != 0) {
1048 cfs_message("RRD update error %s: %s", filename
, rrd_get_error());
1059 cfs_critical("RRD update error: unknown/wrong key %s", key
);
1069 rrdentry_t
*entry
= (rrdentry_t
*)value
;
1070 uint32_t ctime
= GPOINTER_TO_UINT(user_data
);
1072 int diff
= ctime
- entry
->time
;
1074 /* remove everything older than 5 minutes */
1077 return (diff
> expire
) ? TRUE
: FALSE
;
1080 static char *rrd_dump_buf
= NULL
;
1081 static time_t rrd_dump_last
= 0;
1084 cfs_rrd_dump(GString
*str
)
1088 g_mutex_lock (&mutex
);
1091 if (rrd_dump_buf
&& (ctime
- rrd_dump_last
) < 2) {
1092 g_string_assign(str
, rrd_dump_buf
);
1093 g_mutex_unlock (&mutex
);
1097 /* remove old data */
1098 g_hash_table_foreach_remove(cfs_status
.rrdhash
, rrd_entry_is_old
,
1099 GUINT_TO_POINTER(ctime
));
1101 g_string_set_size(str
, 0);
1103 GHashTableIter iter
;
1104 gpointer key
, value
;
1106 g_hash_table_iter_init (&iter
, cfs_status
.rrdhash
);
1108 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
1109 rrdentry_t
*entry
= (rrdentry_t
*)value
;
1110 g_string_append(str
, key
);
1111 g_string_append(str
, ":");
1112 g_string_append(str
, entry
->data
);
1113 g_string_append(str
, "\n");
1116 g_string_append_c(str
, 0); // never return undef
1118 rrd_dump_last
= ctime
;
1120 g_free(rrd_dump_buf
);
1121 rrd_dump_buf
= g_strdup(str
->str
);
1123 g_mutex_unlock (&mutex
);
1129 const char *nodename
,
1133 g_return_val_if_fail(iphash
!= NULL
, FALSE
);
1134 g_return_val_if_fail(nodename
!= NULL
, FALSE
);
1135 g_return_val_if_fail(ip
!= NULL
, FALSE
);
1136 g_return_val_if_fail(len
> 0, FALSE
);
1137 g_return_val_if_fail(len
< 256, FALSE
);
1138 g_return_val_if_fail(ip
[len
-1] == 0, FALSE
);
1140 char *oldip
= (char *)g_hash_table_lookup(iphash
, nodename
);
1142 if (!oldip
|| (strcmp(oldip
, ip
) != 0)) {
1143 cfs_status
.clinfo_version
++;
1144 g_hash_table_replace(iphash
, g_strdup(nodename
), g_strdup(ip
));
1152 GHashTable
*rrdhash
,
1157 g_return_val_if_fail(rrdhash
!= NULL
, FALSE
);
1158 g_return_val_if_fail(key
!= NULL
, FALSE
);
1159 g_return_val_if_fail(data
!= NULL
, FALSE
);
1160 g_return_val_if_fail(len
> 0, FALSE
);
1161 g_return_val_if_fail(len
< 4096, FALSE
);
1162 g_return_val_if_fail(data
[len
-1] == 0, FALSE
);
1165 if ((entry
= (rrdentry_t
*)g_hash_table_lookup(rrdhash
, key
))) {
1166 g_free(entry
->data
);
1167 entry
->data
= g_memdup(data
, len
);
1169 entry
->time
= time(NULL
);
1171 rrdentry_t
*entry
= g_new0(rrdentry_t
, 1);
1173 entry
->key
= g_strdup(key
);
1174 entry
->data
= g_memdup(data
, len
);
1176 entry
->time
= time(NULL
);
1178 g_hash_table_replace(rrdhash
, entry
->key
, entry
);
1181 update_rrd_data(key
, data
, len
);
1187 kvstore_send_update_message(
1193 if (!dfsm_is_initialized(dfsm
))
1196 struct iovec iov
[2];
1199 g_strlcpy(name
, key
, sizeof(name
));
1201 iov
[0].iov_base
= &name
;
1202 iov
[0].iov_len
= sizeof(name
);
1204 iov
[1].iov_base
= (char *)data
;
1205 iov
[1].iov_len
= len
;
1207 if (dfsm_send_message(dfsm
, KVSTORE_MESSAGE_UPDATE
, iov
, 2) == CS_OK
)
1213 static clog_entry_t
*
1214 kvstore_parse_log_message(
1218 g_return_val_if_fail(msg
!= NULL
, NULL
);
1220 if (msg_len
< sizeof(clog_entry_t
)) {
1221 cfs_critical("received short log message (%zu < %zu)", msg_len
, sizeof(clog_entry_t
));
1225 clog_entry_t
*entry
= (clog_entry_t
*)msg
;
1227 uint32_t size
= sizeof(clog_entry_t
) + entry
->node_len
+
1228 entry
->ident_len
+ entry
->tag_len
+ entry
->msg_len
;
1230 if (msg_len
!= size
) {
1231 cfs_critical("received log message with wrong size (%zu != %u)", msg_len
, size
);
1237 if (*((char *)msg
+ entry
->node_len
- 1)) {
1238 cfs_critical("unterminated string in log message");
1241 msg
+= entry
->node_len
;
1243 if (*((char *)msg
+ entry
->ident_len
- 1)) {
1244 cfs_critical("unterminated string in log message");
1247 msg
+= entry
->ident_len
;
1249 if (*((char *)msg
+ entry
->tag_len
- 1)) {
1250 cfs_critical("unterminated string in log message");
1253 msg
+= entry
->tag_len
;
1255 if (*((char *)msg
+ entry
->msg_len
- 1)) {
1256 cfs_critical("unterminated string in log message");
1264 kvstore_parse_update_message(
1268 gconstpointer
*data
,
1271 g_return_val_if_fail(msg
!= NULL
, FALSE
);
1272 g_return_val_if_fail(key
!= NULL
, FALSE
);
1273 g_return_val_if_fail(data
!= NULL
, FALSE
);
1274 g_return_val_if_fail(len
!= NULL
, FALSE
);
1276 if (msg_len
< 256) {
1277 cfs_critical("received short kvstore message (%zu < 256)", msg_len
);
1281 /* test if key is null terminated */
1283 for (i
= 0; i
< 256; i
++)
1284 if (((char *)msg
)[i
] == 0)
1291 *len
= msg_len
- 256;
1299 cfs_create_status_msg(
1301 const char *nodename
,
1304 g_return_val_if_fail(str
!= NULL
, -EINVAL
);
1305 g_return_val_if_fail(key
!= NULL
, -EINVAL
);
1309 GHashTable
*kvhash
= NULL
;
1311 g_mutex_lock (&mutex
);
1313 if (!nodename
|| !nodename
[0] || !strcmp(nodename
, cfs
.nodename
)) {
1314 kvhash
= cfs_status
.kvhash
;
1315 } else if (cfs_status
.clinfo
&& cfs_status
.clinfo
->nodes_byname
) {
1316 cfs_clnode_t
*clnode
;
1317 if ((clnode
= g_hash_table_lookup(cfs_status
.clinfo
->nodes_byname
, nodename
)))
1318 kvhash
= clnode
->kvhash
;
1322 if (kvhash
&& (entry
= (kventry_t
*)g_hash_table_lookup(kvhash
, key
))) {
1323 g_string_append_len(str
, entry
->data
, entry
->len
);
1327 g_mutex_unlock (&mutex
);
1338 g_return_val_if_fail(key
!= NULL
, FALSE
);
1339 g_return_val_if_fail(data
!= NULL
, FALSE
);
1340 g_return_val_if_fail(cfs_status
.kvhash
!= NULL
, FALSE
);
1342 if (len
> CFS_MAX_STATUS_SIZE
)
1345 g_mutex_lock (&mutex
);
1349 if (strncmp(key
, "rrd/", 4) == 0) {
1350 res
= rrdentry_hash_set(cfs_status
.rrdhash
, key
+ 4, data
, len
);
1351 } else if (!strcmp(key
, "nodeip")) {
1352 res
= nodeip_hash_set(cfs_status
.iphash
, cfs
.nodename
, data
, len
);
1354 res
= kventry_hash_set(cfs_status
.kvhash
, key
, data
, len
);
1356 g_mutex_unlock (&mutex
);
1358 if (cfs_status
.kvstore
)
1359 kvstore_send_update_message(cfs_status
.kvstore
, key
, data
, len
);
1361 return res
? 0 : -ENOMEM
;
1365 cfs_kvstore_node_set(
1371 g_return_val_if_fail(nodeid
!= 0, FALSE
);
1372 g_return_val_if_fail(key
!= NULL
, FALSE
);
1373 g_return_val_if_fail(data
!= NULL
, FALSE
);
1375 g_mutex_lock (&mutex
);
1377 if (!cfs_status
.clinfo
|| !cfs_status
.clinfo
->nodes_byid
)
1378 goto ret
; /* ignore */
1380 cfs_clnode_t
*clnode
= g_hash_table_lookup(cfs_status
.clinfo
->nodes_byid
, &nodeid
);
1382 goto ret
; /* ignore */
1384 cfs_debug("got node %d status update %s", nodeid
, key
);
1386 if (strncmp(key
, "rrd/", 4) == 0) {
1387 rrdentry_hash_set(cfs_status
.rrdhash
, key
+ 4, data
, len
);
1388 } else if (!strcmp(key
, "nodeip")) {
1389 nodeip_hash_set(cfs_status
.iphash
, clnode
->name
, data
, len
);
1391 if (!clnode
->kvhash
) {
1392 if (!(clnode
->kvhash
= kventry_hash_new())) {
1393 goto ret
; /*ignore */
1397 kventry_hash_set(clnode
->kvhash
, key
, data
, len
);
1401 g_mutex_unlock (&mutex
);
1407 cfs_kvstore_sync(void)
1409 g_return_val_if_fail(cfs_status
.kvhash
!= NULL
, FALSE
);
1410 g_return_val_if_fail(cfs_status
.kvstore
!= NULL
, FALSE
);
1412 gboolean res
= TRUE
;
1414 g_mutex_lock (&mutex
);
1416 GHashTable
*ht
= cfs_status
.kvhash
;
1417 GHashTableIter iter
;
1418 gpointer key
, value
;
1420 g_hash_table_iter_init (&iter
, ht
);
1422 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
1423 kventry_t
*entry
= (kventry_t
*)value
;
1424 kvstore_send_update_message(cfs_status
.kvstore
, entry
->key
, entry
->data
, entry
->len
);
1427 g_mutex_unlock (&mutex
);
1444 g_return_val_if_fail(dfsm
!= NULL
, -1);
1445 g_return_val_if_fail(msg
!= NULL
, -1);
1446 g_return_val_if_fail(res_ptr
!= NULL
, -1);
1448 /* ignore message for ourself */
1449 if (dfsm_nodeid_is_local(dfsm
, nodeid
, pid
))
1452 if (msg_type
== KVSTORE_MESSAGE_UPDATE
) {
1456 if (kvstore_parse_update_message(msg
, msg_len
, &key
, &data
, &len
)) {
1457 cfs_kvstore_node_set(nodeid
, key
, data
, len
);
1459 cfs_critical("cant parse update message");
1461 } else if (msg_type
== KVSTORE_MESSAGE_LOG
) {
1462 cfs_message("received log"); // fixme: remove
1463 const clog_entry_t
*entry
;
1464 if ((entry
= kvstore_parse_log_message(msg
, msg_len
))) {
1465 clusterlog_insert(cfs_status
.clusterlog
, entry
);
1467 cfs_critical("cant parse log message");
1470 cfs_critical("received unknown message type %d\n", msg_type
);
1487 const struct cpg_address
*member_list
,
1488 size_t member_list_entries
)
1490 g_return_if_fail(dfsm
!= NULL
);
1491 g_return_if_fail(member_list
!= NULL
);
1493 cfs_debug("enter %s", __func__
);
1495 g_mutex_lock (&mutex
);
1497 cfs_clinfo_t
*clinfo
= cfs_status
.clinfo
;
1499 if (clinfo
&& clinfo
->nodes_byid
) {
1501 GHashTable
*ht
= clinfo
->nodes_byid
;
1502 GHashTableIter iter
;
1503 gpointer key
, value
;
1505 g_hash_table_iter_init (&iter
, ht
);
1507 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
1508 cfs_clnode_t
*node
= (cfs_clnode_t
*)value
;
1509 node
->online
= FALSE
;
1512 for (int i
= 0; i
< member_list_entries
; i
++) {
1514 if ((node
= g_hash_table_lookup(clinfo
->nodes_byid
, &member_list
[i
].nodeid
))) {
1515 node
->online
= TRUE
;
1519 cfs_status
.clinfo_version
++;
1522 g_mutex_unlock (&mutex
);
1529 unsigned int *res_len
)
1531 g_return_val_if_fail(dfsm
!= NULL
, NULL
);
1533 gpointer msg
= clusterlog_get_state(cfs_status
.clusterlog
, res_len
);
1539 dfsm_process_update(
1542 dfsm_sync_info_t
*syncinfo
,
1548 cfs_critical("%s: received unexpected update message", __func__
);
1554 dfsm_process_state_update(
1557 dfsm_sync_info_t
*syncinfo
)
1559 g_return_val_if_fail(dfsm
!= NULL
, -1);
1560 g_return_val_if_fail(syncinfo
!= NULL
, -1);
1562 clog_base_t
*clog
[syncinfo
->node_count
];
1564 int local_index
= -1;
1565 for (int i
= 0; i
< syncinfo
->node_count
; i
++) {
1566 dfsm_node_info_t
*ni
= &syncinfo
->nodes
[i
];
1569 if (syncinfo
->local
== ni
)
1572 clog_base_t
*base
= (clog_base_t
*)ni
->state
;
1573 if (ni
->state_len
> 8 && ni
->state_len
== clog_size(base
)) {
1574 clog
[i
] = ni
->state
;
1576 cfs_critical("received log with wrong size %u", ni
->state_len
);
1581 if (!clusterlog_merge(cfs_status
.clusterlog
, clog
, syncinfo
->node_count
, local_index
)) {
1582 cfs_critical("unable to merge log files");
1594 dfsm_sync_info_t
*syncinfo
)
1596 g_return_val_if_fail(dfsm
!= NULL
, -1);
1597 g_return_val_if_fail(syncinfo
!= NULL
, -1);
1603 dfsm_synced(dfsm_t
*dfsm
)
1605 g_return_if_fail(dfsm
!= NULL
);
1607 char *ip
= (char *)g_hash_table_lookup(cfs_status
.iphash
, cfs
.nodename
);
1611 cfs_status_set("nodeip", ip
, strlen(ip
) + 1);
1618 dfsm_sync_info_t
*syncinfo
)
1623 static dfsm_callbacks_t kvstore_dfsm_callbacks
= {
1624 .dfsm_deliver_fn
= dfsm_deliver
,
1625 .dfsm_confchg_fn
= dfsm_confchg
,
1627 .dfsm_get_state_fn
= dfsm_get_state
,
1628 .dfsm_process_state_update_fn
= dfsm_process_state_update
,
1629 .dfsm_process_update_fn
= dfsm_process_update
,
1630 .dfsm_commit_fn
= dfsm_commit
,
1631 .dfsm_cleanup_fn
= dfsm_cleanup
,
1632 .dfsm_synced_fn
= dfsm_synced
,
1636 cfs_status_dfsm_new(void)
1638 g_mutex_lock (&mutex
);
1640 cfs_status
.kvstore
= dfsm_new(NULL
, KVSTORE_CPG_GROUP_NAME
, G_LOG_DOMAIN
,
1641 0, &kvstore_dfsm_callbacks
);
1642 g_mutex_unlock (&mutex
);
1644 return cfs_status
.kvstore
;
1648 cfs_is_quorate(void)
1650 g_mutex_lock (&mutex
);
1651 gboolean res
= cfs_status
.quorate
;
1652 g_mutex_unlock (&mutex
);
1662 g_mutex_lock (&mutex
);
1664 uint32_t prev_quorate
= cfs_status
.quorate
;
1665 cfs_status
.quorate
= quorate
;
1667 if (!prev_quorate
&& cfs_status
.quorate
) {
1669 cfs_message("node has quorum");
1672 if (prev_quorate
&& !cfs_status
.quorate
) {
1674 cfs_message("node lost quorum");
1677 g_mutex_unlock (&mutex
);