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" },
93 { .path
= "ceph.conf" },
103 cfs_clinfo_t
*clinfo
;
104 uint32_t clinfo_version
;
107 uint32_t vmlist_version
;
114 GHashTable
*memdb_changes
;
116 clusterlog_t
*clusterlog
;
119 static cfs_status_t cfs_status
;
131 uint32_t cman_version
;
133 GHashTable
*nodes_byid
;
134 GHashTable
*nodes_byname
;
138 g_int32_hash (gconstpointer v
)
140 return *(const uint32_t *) v
;
144 g_int32_equal (gconstpointer v1
,
147 return *((const uint32_t*) v1
) == *((const uint32_t*) v2
);
150 static void vminfo_free(vminfo_t
*vminfo
)
152 g_return_if_fail(vminfo
!= NULL
);
154 if (vminfo
->nodename
)
155 g_free(vminfo
->nodename
);
161 void cfs_clnode_destroy(
162 cfs_clnode_t
*clnode
)
164 g_return_if_fail(clnode
!= NULL
);
167 g_hash_table_destroy(clnode
->kvhash
);
170 g_free(clnode
->name
);
175 cfs_clnode_t
*cfs_clnode_new(
180 g_return_val_if_fail(name
!= NULL
, NULL
);
182 cfs_clnode_t
*clnode
= g_new0(cfs_clnode_t
, 1);
186 clnode
->name
= g_strdup(name
);
187 clnode
->nodeid
= nodeid
;
188 clnode
->votes
= votes
;
193 gboolean
cfs_clinfo_destroy(
194 cfs_clinfo_t
*clinfo
)
196 g_return_val_if_fail(clinfo
!= NULL
, FALSE
);
198 if (clinfo
->cluster_name
)
199 g_free(clinfo
->cluster_name
);
201 if (clinfo
->nodes_byname
)
202 g_hash_table_destroy(clinfo
->nodes_byname
);
204 if (clinfo
->nodes_byid
)
205 g_hash_table_destroy(clinfo
->nodes_byid
);
212 cfs_clinfo_t
*cfs_clinfo_new(
213 const char *cluster_name
,
214 uint32_t cman_version
)
216 g_return_val_if_fail(cluster_name
!= NULL
, NULL
);
218 cfs_clinfo_t
*clinfo
= g_new0(cfs_clinfo_t
, 1);
222 clinfo
->cluster_name
= g_strdup(cluster_name
);
223 clinfo
->cman_version
= cman_version
;
225 if (!(clinfo
->nodes_byid
= g_hash_table_new_full(
226 g_int32_hash
, g_int32_equal
, NULL
,
227 (GDestroyNotify
)cfs_clnode_destroy
)))
230 if (!(clinfo
->nodes_byname
= g_hash_table_new(g_str_hash
, g_str_equal
)))
236 cfs_clinfo_destroy(clinfo
);
241 gboolean
cfs_clinfo_add_node(
242 cfs_clinfo_t
*clinfo
,
243 cfs_clnode_t
*clnode
)
245 g_return_val_if_fail(clinfo
!= NULL
, FALSE
);
246 g_return_val_if_fail(clnode
!= NULL
, FALSE
);
248 g_hash_table_replace(clinfo
->nodes_byid
, &clnode
->nodeid
, clnode
);
249 g_hash_table_replace(clinfo
->nodes_byname
, clnode
->name
, clnode
);
255 cfs_create_memberlist_msg(
258 g_return_val_if_fail(str
!= NULL
, -EINVAL
);
260 g_mutex_lock (&mutex
);
262 g_string_append_printf(str
,"{\n");
266 cfs_clinfo_t
*clinfo
= cfs_status
.clinfo
;
268 if (clinfo
&& clinfo
->nodes_byid
)
269 nodecount
= g_hash_table_size(clinfo
->nodes_byid
);
272 g_string_append_printf(str
, "\"nodename\": \"%s\",\n", cfs
.nodename
);
273 g_string_append_printf(str
, "\"version\": %u,\n", cfs_status
.clinfo_version
);
275 g_string_append_printf(str
, "\"cluster\": { ");
276 g_string_append_printf(str
, "\"name\": \"%s\", \"version\": %d, "
277 "\"nodes\": %d, \"quorate\": %d ",
278 clinfo
->cluster_name
, clinfo
->cman_version
,
279 nodecount
, cfs_status
.quorate
);
281 g_string_append_printf(str
,"},\n");
282 g_string_append_printf(str
,"\"nodelist\": {\n");
284 GHashTable
*ht
= clinfo
->nodes_byid
;
288 g_hash_table_iter_init (&iter
, ht
);
291 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
292 cfs_clnode_t
*node
= (cfs_clnode_t
*)value
;
293 if (i
) g_string_append_printf(str
, ",\n");
296 g_string_append_printf(str
, " \"%s\": { \"id\": %d, \"online\": %d",
297 node
->name
, node
->nodeid
, node
->online
);
300 char *ip
= (char *)g_hash_table_lookup(cfs_status
.iphash
, node
->name
);
302 g_string_append_printf(str
, ", \"ip\": \"%s\"", ip
);
305 g_string_append_printf(str
, "}");
308 g_string_append_printf(str
,"\n }\n");
310 g_string_append_printf(str
, "\"nodename\": \"%s\",\n", cfs
.nodename
);
311 g_string_append_printf(str
, "\"version\": %u\n", cfs_status
.clinfo_version
);
314 g_string_append_printf(str
,"}\n");
316 g_mutex_unlock (&mutex
);
322 kventry_free(kventry_t
*entry
)
324 g_return_if_fail(entry
!= NULL
);
332 kventry_hash_new(void)
334 return g_hash_table_new_full(g_str_hash
, g_str_equal
, NULL
,
335 (GDestroyNotify
)kventry_free
);
339 rrdentry_free(rrdentry_t
*entry
)
341 g_return_if_fail(entry
!= NULL
);
349 rrdentry_hash_new(void)
351 return g_hash_table_new_full(g_str_hash
, g_str_equal
, NULL
,
352 (GDestroyNotify
)rrdentry_free
);
356 cfs_cluster_log_dump(GString
*str
, const char *user
, guint max_entries
)
358 clusterlog_dump(cfs_status
.clusterlog
, str
, user
, max_entries
);
362 cfs_cluster_log(clog_entry_t
*entry
)
364 g_return_if_fail(entry
!= NULL
);
366 clusterlog_insert(cfs_status
.clusterlog
, entry
);
368 if (cfs_status
.kvstore
) {
370 iov
[0].iov_base
= (char *)entry
;
371 iov
[0].iov_len
= clog_entry_size(entry
);
373 if (dfsm_is_initialized(cfs_status
.kvstore
))
374 dfsm_send_message(cfs_status
.kvstore
, KVSTORE_MESSAGE_LOG
, iov
, 1);
378 void cfs_status_init(void)
380 g_mutex_lock (&mutex
);
382 cfs_status
.start_time
= time(NULL
);
384 cfs_status
.vmlist
= vmlist_hash_new();
386 cfs_status
.kvhash
= kventry_hash_new();
388 cfs_status
.rrdhash
= rrdentry_hash_new();
390 cfs_status
.iphash
= g_hash_table_new_full(g_str_hash
, g_str_equal
, g_free
, g_free
);
392 cfs_status
.memdb_changes
= g_hash_table_new(g_str_hash
, g_str_equal
);
394 for (int i
= 0; i
< G_N_ELEMENTS(memdb_change_array
); i
++) {
395 g_hash_table_replace(cfs_status
.memdb_changes
,
396 memdb_change_array
[i
].path
,
397 &memdb_change_array
[i
]);
400 cfs_status
.clusterlog
= clusterlog_new();
403 clusterlog_add(cfs_status
.clusterlog
, "root", "cluster", getpid(),
404 LOG_INFO
, "starting cluster log");
406 g_mutex_unlock (&mutex
);
409 void cfs_status_cleanup(void)
411 g_mutex_lock (&mutex
);
413 cfs_status
.clinfo_version
++;
415 if (cfs_status
.clinfo
) {
416 cfs_clinfo_destroy(cfs_status
.clinfo
);
417 cfs_status
.clinfo
= NULL
;
420 if (cfs_status
.vmlist
) {
421 g_hash_table_destroy(cfs_status
.vmlist
);
422 cfs_status
.vmlist
= NULL
;
425 if (cfs_status
.kvhash
) {
426 g_hash_table_destroy(cfs_status
.kvhash
);
427 cfs_status
.kvhash
= NULL
;
430 if (cfs_status
.rrdhash
) {
431 g_hash_table_destroy(cfs_status
.rrdhash
);
432 cfs_status
.rrdhash
= NULL
;
435 if (cfs_status
.iphash
) {
436 g_hash_table_destroy(cfs_status
.iphash
);
437 cfs_status
.iphash
= NULL
;
440 if (cfs_status
.clusterlog
)
441 clusterlog_destroy(cfs_status
.clusterlog
);
443 g_mutex_unlock (&mutex
);
446 void cfs_status_set_clinfo(
447 cfs_clinfo_t
*clinfo
)
449 g_return_if_fail(clinfo
!= NULL
);
451 g_mutex_lock (&mutex
);
453 cfs_status
.clinfo_version
++;
455 cfs_clinfo_t
*old
= cfs_status
.clinfo
;
457 cfs_status
.clinfo
= clinfo
;
459 cfs_message("update cluster info (cluster name %s, version = %d)",
460 clinfo
->cluster_name
, clinfo
->cman_version
);
463 if (old
&& old
->nodes_byid
&& clinfo
->nodes_byid
) {
465 GHashTable
*ht
= clinfo
->nodes_byid
;
469 g_hash_table_iter_init (&iter
, ht
);
471 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
472 cfs_clnode_t
*node
= (cfs_clnode_t
*)value
;
473 cfs_clnode_t
*oldnode
;
474 if ((oldnode
= g_hash_table_lookup(old
->nodes_byid
, key
))) {
475 node
->online
= oldnode
->online
;
476 node
->kvhash
= oldnode
->kvhash
;
477 oldnode
->kvhash
= NULL
;
484 cfs_clinfo_destroy(old
);
487 g_mutex_unlock (&mutex
);
491 dump_kvstore_versions(
494 const char *nodename
)
496 g_return_if_fail(kvhash
!= NULL
);
497 g_return_if_fail(str
!= NULL
);
498 g_return_if_fail(nodename
!= NULL
);
500 GHashTable
*ht
= kvhash
;
504 g_string_append_printf(str
, "\"%s\": {\n", nodename
);
506 g_hash_table_iter_init (&iter
, ht
);
509 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
510 kventry_t
*entry
= (kventry_t
*)value
;
511 if (i
) g_string_append_printf(str
, ",\n");
513 g_string_append_printf(str
,"\"%s\": %u", entry
->key
, entry
->version
);
516 g_string_append_printf(str
, "}\n");
520 cfs_create_version_msg(GString
*str
)
522 g_return_val_if_fail(str
!= NULL
, -EINVAL
);
524 g_mutex_lock (&mutex
);
526 g_string_append_printf(str
,"{\n");
528 g_string_append_printf(str
, "\"starttime\": %lu,\n", (unsigned long)cfs_status
.start_time
);
530 g_string_append_printf(str
, "\"clinfo\": %u,\n", cfs_status
.clinfo_version
);
532 g_string_append_printf(str
, "\"vmlist\": %u,\n", cfs_status
.vmlist_version
);
534 for (int i
= 0; i
< G_N_ELEMENTS(memdb_change_array
); i
++) {
535 g_string_append_printf(str
, "\"%s\": %u,\n",
536 memdb_change_array
[i
].path
,
537 memdb_change_array
[i
].version
);
540 g_string_append_printf(str
, "\"kvstore\": {\n");
542 dump_kvstore_versions(str
, cfs_status
.kvhash
, cfs
.nodename
);
544 cfs_clinfo_t
*clinfo
= cfs_status
.clinfo
;
546 if (clinfo
&& clinfo
->nodes_byid
) {
547 GHashTable
*ht
= clinfo
->nodes_byid
;
551 g_hash_table_iter_init (&iter
, ht
);
553 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
554 cfs_clnode_t
*node
= (cfs_clnode_t
*)value
;
557 g_string_append_printf(str
, ",\n");
558 dump_kvstore_versions(str
, node
->kvhash
, node
->name
);
562 g_string_append_printf(str
,"}\n");
564 g_string_append_printf(str
,"}\n");
566 g_mutex_unlock (&mutex
);
572 vmlist_hash_new(void)
574 return g_hash_table_new_full(g_int_hash
, g_int_equal
, NULL
,
575 (GDestroyNotify
)vminfo_free
);
579 vmlist_hash_insert_vm(
583 const char *nodename
,
586 g_return_val_if_fail(vmlist
!= NULL
, FALSE
);
587 g_return_val_if_fail(nodename
!= NULL
, FALSE
);
588 g_return_val_if_fail(vmid
!= 0, FALSE
);
589 g_return_val_if_fail(vmtype
== VMTYPE_QEMU
|| vmtype
== VMTYPE_OPENVZ
||
590 vmtype
== VMTYPE_LXC
, FALSE
);
592 if (!replace
&& g_hash_table_lookup(vmlist
, &vmid
)) {
593 cfs_critical("detected duplicate VMID %d", vmid
);
597 vminfo_t
*vminfo
= g_new0(vminfo_t
, 1);
600 vminfo
->vmtype
= vmtype
;
601 vminfo
->nodename
= g_strdup(nodename
);
603 vminfo
->version
= ++vminfo_version_counter
;
605 g_hash_table_replace(vmlist
, &vminfo
->vmid
, vminfo
);
614 const char *nodename
)
616 g_return_if_fail(cfs_status
.vmlist
!= NULL
);
617 g_return_if_fail(nodename
!= NULL
);
618 g_return_if_fail(vmid
!= 0);
619 g_return_if_fail(vmtype
== VMTYPE_QEMU
|| vmtype
== VMTYPE_OPENVZ
||
620 vmtype
== VMTYPE_LXC
);
622 cfs_debug("vmlist_register_vm: %s/%u %d", nodename
, vmid
, vmtype
);
624 g_mutex_lock (&mutex
);
626 cfs_status
.vmlist_version
++;
628 vmlist_hash_insert_vm(cfs_status
.vmlist
, vmtype
, vmid
, nodename
, TRUE
);
630 g_mutex_unlock (&mutex
);
634 vmlist_different_vm_exists(
637 const char *nodename
)
639 g_return_val_if_fail(cfs_status
.vmlist
!= NULL
, FALSE
);
640 g_return_val_if_fail(vmid
!= 0, FALSE
);
642 gboolean res
= FALSE
;
644 g_mutex_lock (&mutex
);
647 if ((vminfo
= (vminfo_t
*)g_hash_table_lookup(cfs_status
.vmlist
, &vmid
))) {
648 if (!(vminfo
->vmtype
== vmtype
&& strcmp(vminfo
->nodename
, nodename
) == 0))
651 g_mutex_unlock (&mutex
);
660 g_return_val_if_fail(cfs_status
.vmlist
!= NULL
, FALSE
);
661 g_return_val_if_fail(vmid
!= 0, FALSE
);
663 g_mutex_lock (&mutex
);
665 gpointer res
= g_hash_table_lookup(cfs_status
.vmlist
, &vmid
);
667 g_mutex_unlock (&mutex
);
676 g_return_if_fail(cfs_status
.vmlist
!= NULL
);
677 g_return_if_fail(vmid
!= 0);
679 g_mutex_lock (&mutex
);
681 cfs_status
.vmlist_version
++;
683 g_hash_table_remove(cfs_status
.vmlist
, &vmid
);
685 g_mutex_unlock (&mutex
);
688 void cfs_status_set_vmlist(
691 g_return_if_fail(vmlist
!= NULL
);
693 g_mutex_lock (&mutex
);
695 cfs_status
.vmlist_version
++;
697 if (cfs_status
.vmlist
)
698 g_hash_table_destroy(cfs_status
.vmlist
);
700 cfs_status
.vmlist
= vmlist
;
702 g_mutex_unlock (&mutex
);
706 cfs_create_vmlist_msg(GString
*str
)
708 g_return_val_if_fail(cfs_status
.vmlist
!= NULL
, -EINVAL
);
709 g_return_val_if_fail(str
!= NULL
, -EINVAL
);
711 g_mutex_lock (&mutex
);
713 g_string_append_printf(str
,"{\n");
715 GHashTable
*ht
= cfs_status
.vmlist
;
717 guint count
= g_hash_table_size(ht
);
720 g_string_append_printf(str
,"\"version\": %u\n", cfs_status
.vmlist_version
);
722 g_string_append_printf(str
,"\"version\": %u,\n", cfs_status
.vmlist_version
);
724 g_string_append_printf(str
,"\"ids\": {\n");
729 g_hash_table_iter_init (&iter
, ht
);
732 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
733 vminfo_t
*vminfo
= (vminfo_t
*)value
;
735 if (vminfo
->vmtype
== VMTYPE_QEMU
) {
737 } else if (vminfo
->vmtype
== VMTYPE_OPENVZ
) {
739 } else if (vminfo
->vmtype
== VMTYPE_LXC
) {
746 g_string_append_printf(str
, ",\n");
749 g_string_append_printf(str
,"\"%u\": { \"node\": \"%s\", \"type\": \"%s\", \"version\": %u }",
750 vminfo
->vmid
, vminfo
->nodename
, type
, vminfo
->version
);
753 g_string_append_printf(str
,"}\n");
755 g_string_append_printf(str
,"\n}\n");
757 g_mutex_unlock (&mutex
);
763 record_memdb_change(const char *path
)
765 g_return_if_fail(cfs_status
.memdb_changes
!= 0);
769 if ((ce
= (memdb_change_t
*)g_hash_table_lookup(cfs_status
.memdb_changes
, path
))) {
775 record_memdb_reload(void)
777 for (int i
= 0; i
< G_N_ELEMENTS(memdb_change_array
); i
++) {
778 memdb_change_array
[i
].version
++;
789 g_return_val_if_fail(kvhash
!= NULL
, FALSE
);
790 g_return_val_if_fail(key
!= NULL
, FALSE
);
791 g_return_val_if_fail(data
!= NULL
, FALSE
);
794 if ((entry
= (kventry_t
*)g_hash_table_lookup(kvhash
, key
))) {
796 entry
->data
= g_memdup(data
, len
);
800 kventry_t
*entry
= g_new0(kventry_t
, 1);
802 entry
->key
= g_strdup(key
);
803 entry
->data
= g_memdup(data
, len
);
806 g_hash_table_replace(kvhash
, entry
->key
, entry
);
812 static const char *rrd_def_node
[] = {
813 "DS:loadavg:GAUGE:120:0:U",
814 "DS:maxcpu:GAUGE:120:0:U",
815 "DS:cpu:GAUGE:120:0:U",
816 "DS:iowait:GAUGE:120:0:U",
817 "DS:memtotal:GAUGE:120:0:U",
818 "DS:memused:GAUGE:120:0:U",
819 "DS:swaptotal:GAUGE:120:0:U",
820 "DS:swapused:GAUGE:120:0:U",
821 "DS:roottotal:GAUGE:120:0:U",
822 "DS:rootused:GAUGE:120:0:U",
823 "DS:netin:DERIVE:120:0:U",
824 "DS:netout:DERIVE:120:0:U",
826 "RRA:AVERAGE:0.5:1:70", // 1 min avg - one hour
827 "RRA:AVERAGE:0.5:30:70", // 30 min avg - one day
828 "RRA:AVERAGE:0.5:180:70", // 3 hour avg - one week
829 "RRA:AVERAGE:0.5:720:70", // 12 hour avg - one month
830 "RRA:AVERAGE:0.5:10080:70", // 7 day avg - ony year
832 "RRA:MAX:0.5:1:70", // 1 min max - one hour
833 "RRA:MAX:0.5:30:70", // 30 min max - one day
834 "RRA:MAX:0.5:180:70", // 3 hour max - one week
835 "RRA:MAX:0.5:720:70", // 12 hour max - one month
836 "RRA:MAX:0.5:10080:70", // 7 day max - ony year
840 static const char *rrd_def_vm
[] = {
841 "DS:maxcpu:GAUGE:120:0:U",
842 "DS:cpu:GAUGE:120:0:U",
843 "DS:maxmem:GAUGE:120:0:U",
844 "DS:mem:GAUGE:120:0:U",
845 "DS:maxdisk:GAUGE:120:0:U",
846 "DS:disk:GAUGE:120:0:U",
847 "DS:netin:DERIVE:120:0:U",
848 "DS:netout:DERIVE:120:0:U",
849 "DS:diskread:DERIVE:120:0:U",
850 "DS:diskwrite:DERIVE:120:0:U",
852 "RRA:AVERAGE:0.5:1:70", // 1 min avg - one hour
853 "RRA:AVERAGE:0.5:30:70", // 30 min avg - one day
854 "RRA:AVERAGE:0.5:180:70", // 3 hour avg - one week
855 "RRA:AVERAGE:0.5:720:70", // 12 hour avg - one month
856 "RRA:AVERAGE:0.5:10080:70", // 7 day avg - ony year
858 "RRA:MAX:0.5:1:70", // 1 min max - one hour
859 "RRA:MAX:0.5:30:70", // 30 min max - one day
860 "RRA:MAX:0.5:180:70", // 3 hour max - one week
861 "RRA:MAX:0.5:720:70", // 12 hour max - one month
862 "RRA:MAX:0.5:10080:70", // 7 day max - ony year
866 static const char *rrd_def_storage
[] = {
867 "DS:total:GAUGE:120:0:U",
868 "DS:used:GAUGE:120:0:U",
870 "RRA:AVERAGE:0.5:1:70", // 1 min avg - one hour
871 "RRA:AVERAGE:0.5:30:70", // 30 min avg - one day
872 "RRA:AVERAGE:0.5:180:70", // 3 hour avg - one week
873 "RRA:AVERAGE:0.5:720:70", // 12 hour avg - one month
874 "RRA:AVERAGE:0.5:10080:70", // 7 day avg - ony year
876 "RRA:MAX:0.5:1:70", // 1 min max - one hour
877 "RRA:MAX:0.5:30:70", // 30 min max - one day
878 "RRA:MAX:0.5:180:70", // 3 hour max - one week
879 "RRA:MAX:0.5:720:70", // 12 hour max - one month
880 "RRA:MAX:0.5:10080:70", // 7 day max - ony year
884 #define RRDDIR "/var/lib/rrdcached/db"
888 const char *filename
,
890 const char *rrddef
[])
892 /* start at day boundary */
895 struct tm
*ltm
= localtime(&ctime
);
901 if (rrd_create_r(filename
, 60, timelocal(ltm
), argcount
, rrddef
)) {
902 cfs_message("RRD create error %s: %s", filename
, rrd_get_error());
906 static inline const char *
912 while (*data
&& found
< count
) {
925 g_return_if_fail(key
!= NULL
);
926 g_return_if_fail(data
!= NULL
);
927 g_return_if_fail(len
> 0);
928 g_return_if_fail(len
< 4096);
930 static const char *rrdcsock
= "unix:/var/run/rrdcached.sock";
933 if (rrdc_connect(rrdcsock
) != 0)
936 char *filename
= NULL
;
940 if (strncmp(key
, "pve2-node/", 10) == 0) {
941 const char *node
= key
+ 10;
945 if (strchr(node
, '/') != NULL
)
948 if (strlen(node
) < 1)
951 filename
= g_strdup_printf(RRDDIR
"/%s", key
);
953 if (!g_file_test(filename
, G_FILE_TEST_EXISTS
)) {
955 mkdir(RRDDIR
"/pve2-node", 0755);
956 int argcount
= sizeof(rrd_def_node
)/sizeof(void*) - 1;
957 create_rrd_file(filename
, argcount
, rrd_def_node
);
960 } else if ((strncmp(key
, "pve2-vm/", 8) == 0) ||
961 (strncmp(key
, "pve2.3-vm/", 10) == 0)) {
964 if (strncmp(key
, "pve2-vm/", 8) == 0) {
972 if (strchr(vmid
, '/') != NULL
)
975 if (strlen(vmid
) < 1)
978 filename
= g_strdup_printf(RRDDIR
"/%s/%s", "pve2-vm", vmid
);
980 if (!g_file_test(filename
, G_FILE_TEST_EXISTS
)) {
982 mkdir(RRDDIR
"/pve2-vm", 0755);
983 int argcount
= sizeof(rrd_def_vm
)/sizeof(void*) - 1;
984 create_rrd_file(filename
, argcount
, rrd_def_vm
);
987 } else if (strncmp(key
, "pve2-storage/", 13) == 0) {
988 const char *node
= key
+ 13;
990 const char *storage
= node
;
991 while (*storage
&& *storage
!= '/')
994 if (*storage
!= '/' || ((storage
- node
) < 1))
999 if (strchr(storage
, '/') != NULL
)
1002 if (strlen(storage
) < 1)
1005 filename
= g_strdup_printf(RRDDIR
"/%s", key
);
1007 if (!g_file_test(filename
, G_FILE_TEST_EXISTS
)) {
1009 mkdir(RRDDIR
"/pve2-storage", 0755);
1011 char *dir
= g_path_get_dirname(filename
);
1015 int argcount
= sizeof(rrd_def_storage
)/sizeof(void*) - 1;
1016 create_rrd_file(filename
, argcount
, rrd_def_storage
);
1023 const char *dp
= skip
? rrd_skip_data(data
, skip
) : data
;
1025 const char *update_args
[] = { dp
, NULL
};
1029 if ((status
= rrdc_update(filename
, 1, update_args
)) != 0) {
1030 cfs_message("RRDC update error %s: %d", filename
, status
);
1033 if (rrd_update_r(filename
, NULL
, 1, update_args
) != 0) {
1034 cfs_message("RRD update error %s: %s", filename
, rrd_get_error());
1040 if (rrd_update_r(filename
, NULL
, 1, update_args
) != 0) {
1041 cfs_message("RRD update error %s: %s", filename
, rrd_get_error());
1052 cfs_critical("RRD update error: unknown/wrong key %s", key
);
1062 rrdentry_t
*entry
= (rrdentry_t
*)value
;
1063 uint32_t ctime
= GPOINTER_TO_UINT(user_data
);
1065 int diff
= ctime
- entry
->time
;
1067 /* remove everything older than 5 minutes */
1070 return (diff
> expire
) ? TRUE
: FALSE
;
1073 static char *rrd_dump_buf
= NULL
;
1074 static time_t rrd_dump_last
= 0;
1077 cfs_rrd_dump(GString
*str
)
1081 g_mutex_lock (&mutex
);
1084 if (rrd_dump_buf
&& (ctime
- rrd_dump_last
) < 2) {
1085 g_string_assign(str
, rrd_dump_buf
);
1086 g_mutex_unlock (&mutex
);
1090 /* remove old data */
1091 g_hash_table_foreach_remove(cfs_status
.rrdhash
, rrd_entry_is_old
,
1092 GUINT_TO_POINTER(ctime
));
1094 g_string_set_size(str
, 0);
1096 GHashTableIter iter
;
1097 gpointer key
, value
;
1099 g_hash_table_iter_init (&iter
, cfs_status
.rrdhash
);
1101 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
1102 rrdentry_t
*entry
= (rrdentry_t
*)value
;
1103 g_string_append(str
, key
);
1104 g_string_append(str
, ":");
1105 g_string_append(str
, entry
->data
);
1106 g_string_append(str
, "\n");
1109 g_string_append_c(str
, 0); // never return undef
1111 rrd_dump_last
= ctime
;
1113 g_free(rrd_dump_buf
);
1114 rrd_dump_buf
= g_strdup(str
->str
);
1116 g_mutex_unlock (&mutex
);
1122 const char *nodename
,
1126 g_return_val_if_fail(iphash
!= NULL
, FALSE
);
1127 g_return_val_if_fail(nodename
!= NULL
, FALSE
);
1128 g_return_val_if_fail(ip
!= NULL
, FALSE
);
1129 g_return_val_if_fail(len
> 0, FALSE
);
1130 g_return_val_if_fail(len
< 256, FALSE
);
1131 g_return_val_if_fail(ip
[len
-1] == 0, FALSE
);
1133 char *oldip
= (char *)g_hash_table_lookup(iphash
, nodename
);
1135 if (!oldip
|| (strcmp(oldip
, ip
) != 0)) {
1136 cfs_status
.clinfo_version
++;
1137 g_hash_table_replace(iphash
, g_strdup(nodename
), g_strdup(ip
));
1145 GHashTable
*rrdhash
,
1150 g_return_val_if_fail(rrdhash
!= NULL
, FALSE
);
1151 g_return_val_if_fail(key
!= NULL
, FALSE
);
1152 g_return_val_if_fail(data
!= NULL
, FALSE
);
1153 g_return_val_if_fail(len
> 0, FALSE
);
1154 g_return_val_if_fail(len
< 4096, FALSE
);
1155 g_return_val_if_fail(data
[len
-1] == 0, FALSE
);
1158 if ((entry
= (rrdentry_t
*)g_hash_table_lookup(rrdhash
, key
))) {
1159 g_free(entry
->data
);
1160 entry
->data
= g_memdup(data
, len
);
1162 entry
->time
= time(NULL
);
1164 rrdentry_t
*entry
= g_new0(rrdentry_t
, 1);
1166 entry
->key
= g_strdup(key
);
1167 entry
->data
= g_memdup(data
, len
);
1169 entry
->time
= time(NULL
);
1171 g_hash_table_replace(rrdhash
, entry
->key
, entry
);
1174 update_rrd_data(key
, data
, len
);
1180 kvstore_send_update_message(
1186 if (!dfsm_is_initialized(dfsm
))
1189 struct iovec iov
[2];
1192 g_strlcpy(name
, key
, sizeof(name
));
1194 iov
[0].iov_base
= &name
;
1195 iov
[0].iov_len
= sizeof(name
);
1197 iov
[1].iov_base
= (char *)data
;
1198 iov
[1].iov_len
= len
;
1200 if (dfsm_send_message(dfsm
, KVSTORE_MESSAGE_UPDATE
, iov
, 2) == CS_OK
)
1206 static clog_entry_t
*
1207 kvstore_parse_log_message(
1211 g_return_val_if_fail(msg
!= NULL
, NULL
);
1213 if (msg_len
< sizeof(clog_entry_t
)) {
1214 cfs_critical("received short log message (%zu < %zu)", msg_len
, sizeof(clog_entry_t
));
1218 clog_entry_t
*entry
= (clog_entry_t
*)msg
;
1220 uint32_t size
= sizeof(clog_entry_t
) + entry
->node_len
+
1221 entry
->ident_len
+ entry
->tag_len
+ entry
->msg_len
;
1223 if (msg_len
!= size
) {
1224 cfs_critical("received log message with wrong size (%zu != %u)", msg_len
, size
);
1230 if (*((char *)msg
+ entry
->node_len
- 1)) {
1231 cfs_critical("unterminated string in log message");
1234 msg
+= entry
->node_len
;
1236 if (*((char *)msg
+ entry
->ident_len
- 1)) {
1237 cfs_critical("unterminated string in log message");
1240 msg
+= entry
->ident_len
;
1242 if (*((char *)msg
+ entry
->tag_len
- 1)) {
1243 cfs_critical("unterminated string in log message");
1246 msg
+= entry
->tag_len
;
1248 if (*((char *)msg
+ entry
->msg_len
- 1)) {
1249 cfs_critical("unterminated string in log message");
1257 kvstore_parse_update_message(
1261 gconstpointer
*data
,
1264 g_return_val_if_fail(msg
!= NULL
, FALSE
);
1265 g_return_val_if_fail(key
!= NULL
, FALSE
);
1266 g_return_val_if_fail(data
!= NULL
, FALSE
);
1267 g_return_val_if_fail(len
!= NULL
, FALSE
);
1269 if (msg_len
< 256) {
1270 cfs_critical("received short kvstore message (%zu < 256)", msg_len
);
1274 /* test if key is null terminated */
1276 for (i
= 0; i
< 256; i
++)
1277 if (((char *)msg
)[i
] == 0)
1284 *len
= msg_len
- 256;
1292 cfs_create_status_msg(
1294 const char *nodename
,
1297 g_return_val_if_fail(str
!= NULL
, -EINVAL
);
1298 g_return_val_if_fail(key
!= NULL
, -EINVAL
);
1302 GHashTable
*kvhash
= NULL
;
1304 g_mutex_lock (&mutex
);
1306 if (!nodename
|| !nodename
[0] || !strcmp(nodename
, cfs
.nodename
)) {
1307 kvhash
= cfs_status
.kvhash
;
1308 } else if (cfs_status
.clinfo
&& cfs_status
.clinfo
->nodes_byname
) {
1309 cfs_clnode_t
*clnode
;
1310 if ((clnode
= g_hash_table_lookup(cfs_status
.clinfo
->nodes_byname
, nodename
)))
1311 kvhash
= clnode
->kvhash
;
1315 if (kvhash
&& (entry
= (kventry_t
*)g_hash_table_lookup(kvhash
, key
))) {
1316 g_string_append_len(str
, entry
->data
, entry
->len
);
1320 g_mutex_unlock (&mutex
);
1331 g_return_val_if_fail(key
!= NULL
, FALSE
);
1332 g_return_val_if_fail(data
!= NULL
, FALSE
);
1333 g_return_val_if_fail(cfs_status
.kvhash
!= NULL
, FALSE
);
1335 if (len
> CFS_MAX_STATUS_SIZE
)
1338 g_mutex_lock (&mutex
);
1342 if (strncmp(key
, "rrd/", 4) == 0) {
1343 res
= rrdentry_hash_set(cfs_status
.rrdhash
, key
+ 4, data
, len
);
1344 } else if (!strcmp(key
, "nodeip")) {
1345 res
= nodeip_hash_set(cfs_status
.iphash
, cfs
.nodename
, data
, len
);
1347 res
= kventry_hash_set(cfs_status
.kvhash
, key
, data
, len
);
1349 g_mutex_unlock (&mutex
);
1351 if (cfs_status
.kvstore
)
1352 kvstore_send_update_message(cfs_status
.kvstore
, key
, data
, len
);
1354 return res
? 0 : -ENOMEM
;
1358 cfs_kvstore_node_set(
1364 g_return_val_if_fail(nodeid
!= 0, FALSE
);
1365 g_return_val_if_fail(key
!= NULL
, FALSE
);
1366 g_return_val_if_fail(data
!= NULL
, FALSE
);
1368 g_mutex_lock (&mutex
);
1370 if (!cfs_status
.clinfo
|| !cfs_status
.clinfo
->nodes_byid
)
1371 goto ret
; /* ignore */
1373 cfs_clnode_t
*clnode
= g_hash_table_lookup(cfs_status
.clinfo
->nodes_byid
, &nodeid
);
1375 goto ret
; /* ignore */
1377 cfs_debug("got node %d status update %s", nodeid
, key
);
1379 if (strncmp(key
, "rrd/", 4) == 0) {
1380 rrdentry_hash_set(cfs_status
.rrdhash
, key
+ 4, data
, len
);
1381 } else if (!strcmp(key
, "nodeip")) {
1382 nodeip_hash_set(cfs_status
.iphash
, clnode
->name
, data
, len
);
1384 if (!clnode
->kvhash
) {
1385 if (!(clnode
->kvhash
= kventry_hash_new())) {
1386 goto ret
; /*ignore */
1390 kventry_hash_set(clnode
->kvhash
, key
, data
, len
);
1394 g_mutex_unlock (&mutex
);
1400 cfs_kvstore_sync(void)
1402 g_return_val_if_fail(cfs_status
.kvhash
!= NULL
, FALSE
);
1403 g_return_val_if_fail(cfs_status
.kvstore
!= NULL
, FALSE
);
1405 gboolean res
= TRUE
;
1407 g_mutex_lock (&mutex
);
1409 GHashTable
*ht
= cfs_status
.kvhash
;
1410 GHashTableIter iter
;
1411 gpointer key
, value
;
1413 g_hash_table_iter_init (&iter
, ht
);
1415 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
1416 kventry_t
*entry
= (kventry_t
*)value
;
1417 kvstore_send_update_message(cfs_status
.kvstore
, entry
->key
, entry
->data
, entry
->len
);
1420 g_mutex_unlock (&mutex
);
1437 g_return_val_if_fail(dfsm
!= NULL
, -1);
1438 g_return_val_if_fail(msg
!= NULL
, -1);
1439 g_return_val_if_fail(res_ptr
!= NULL
, -1);
1441 /* ignore message for ourself */
1442 if (dfsm_nodeid_is_local(dfsm
, nodeid
, pid
))
1445 if (msg_type
== KVSTORE_MESSAGE_UPDATE
) {
1449 if (kvstore_parse_update_message(msg
, msg_len
, &key
, &data
, &len
)) {
1450 cfs_kvstore_node_set(nodeid
, key
, data
, len
);
1452 cfs_critical("cant parse update message");
1454 } else if (msg_type
== KVSTORE_MESSAGE_LOG
) {
1455 cfs_message("received log"); // fixme: remove
1456 const clog_entry_t
*entry
;
1457 if ((entry
= kvstore_parse_log_message(msg
, msg_len
))) {
1458 clusterlog_insert(cfs_status
.clusterlog
, entry
);
1460 cfs_critical("cant parse log message");
1463 cfs_critical("received unknown message type %d\n", msg_type
);
1480 const struct cpg_address
*member_list
,
1481 size_t member_list_entries
)
1483 g_return_if_fail(dfsm
!= NULL
);
1484 g_return_if_fail(member_list
!= NULL
);
1486 cfs_debug("enter %s", __func__
);
1488 g_mutex_lock (&mutex
);
1490 cfs_clinfo_t
*clinfo
= cfs_status
.clinfo
;
1492 if (clinfo
&& clinfo
->nodes_byid
) {
1494 GHashTable
*ht
= clinfo
->nodes_byid
;
1495 GHashTableIter iter
;
1496 gpointer key
, value
;
1498 g_hash_table_iter_init (&iter
, ht
);
1500 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
1501 cfs_clnode_t
*node
= (cfs_clnode_t
*)value
;
1502 node
->online
= FALSE
;
1505 for (int i
= 0; i
< member_list_entries
; i
++) {
1507 if ((node
= g_hash_table_lookup(clinfo
->nodes_byid
, &member_list
[i
].nodeid
))) {
1508 node
->online
= TRUE
;
1512 cfs_status
.clinfo_version
++;
1515 g_mutex_unlock (&mutex
);
1522 unsigned int *res_len
)
1524 g_return_val_if_fail(dfsm
!= NULL
, NULL
);
1526 gpointer msg
= clusterlog_get_state(cfs_status
.clusterlog
, res_len
);
1532 dfsm_process_update(
1535 dfsm_sync_info_t
*syncinfo
,
1541 cfs_critical("%s: received unexpected update message", __func__
);
1547 dfsm_process_state_update(
1550 dfsm_sync_info_t
*syncinfo
)
1552 g_return_val_if_fail(dfsm
!= NULL
, -1);
1553 g_return_val_if_fail(syncinfo
!= NULL
, -1);
1555 clog_base_t
*clog
[syncinfo
->node_count
];
1557 int local_index
= -1;
1558 for (int i
= 0; i
< syncinfo
->node_count
; i
++) {
1559 dfsm_node_info_t
*ni
= &syncinfo
->nodes
[i
];
1562 if (syncinfo
->local
== ni
)
1565 clog_base_t
*base
= (clog_base_t
*)ni
->state
;
1566 if (ni
->state_len
> 8 && ni
->state_len
== clog_size(base
)) {
1567 clog
[i
] = ni
->state
;
1569 cfs_critical("received log with wrong size %u", ni
->state_len
);
1574 if (!clusterlog_merge(cfs_status
.clusterlog
, clog
, syncinfo
->node_count
, local_index
)) {
1575 cfs_critical("unable to merge log files");
1587 dfsm_sync_info_t
*syncinfo
)
1589 g_return_val_if_fail(dfsm
!= NULL
, -1);
1590 g_return_val_if_fail(syncinfo
!= NULL
, -1);
1596 dfsm_synced(dfsm_t
*dfsm
)
1598 g_return_if_fail(dfsm
!= NULL
);
1600 char *ip
= (char *)g_hash_table_lookup(cfs_status
.iphash
, cfs
.nodename
);
1604 cfs_status_set("nodeip", ip
, strlen(ip
) + 1);
1611 dfsm_sync_info_t
*syncinfo
)
1616 static dfsm_callbacks_t kvstore_dfsm_callbacks
= {
1617 .dfsm_deliver_fn
= dfsm_deliver
,
1618 .dfsm_confchg_fn
= dfsm_confchg
,
1620 .dfsm_get_state_fn
= dfsm_get_state
,
1621 .dfsm_process_state_update_fn
= dfsm_process_state_update
,
1622 .dfsm_process_update_fn
= dfsm_process_update
,
1623 .dfsm_commit_fn
= dfsm_commit
,
1624 .dfsm_cleanup_fn
= dfsm_cleanup
,
1625 .dfsm_synced_fn
= dfsm_synced
,
1629 cfs_status_dfsm_new(void)
1631 g_mutex_lock (&mutex
);
1633 cfs_status
.kvstore
= dfsm_new(NULL
, KVSTORE_CPG_GROUP_NAME
, G_LOG_DOMAIN
,
1634 0, &kvstore_dfsm_callbacks
);
1635 g_mutex_unlock (&mutex
);
1637 return cfs_status
.kvstore
;
1641 cfs_is_quorate(void)
1643 g_mutex_lock (&mutex
);
1644 gboolean res
= cfs_status
.quorate
;
1645 g_mutex_unlock (&mutex
);
1655 g_mutex_lock (&mutex
);
1657 uint32_t prev_quorate
= cfs_status
.quorate
;
1658 cfs_status
.quorate
= quorate
;
1660 if (!prev_quorate
&& cfs_status
.quorate
) {
1662 cfs_message("node has quorum");
1665 if (prev_quorate
&& !cfs_status
.quorate
) {
1667 cfs_message("node lost quorum");
1670 g_mutex_unlock (&mutex
);