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" },
101 cfs_clinfo_t
*clinfo
;
102 uint32_t clinfo_version
;
105 uint32_t vmlist_version
;
112 GHashTable
*memdb_changes
;
114 clusterlog_t
*clusterlog
;
117 static cfs_status_t cfs_status
;
129 uint32_t cman_version
;
131 GHashTable
*nodes_byid
;
132 GHashTable
*nodes_byname
;
136 g_int32_hash (gconstpointer v
)
138 return *(const uint32_t *) v
;
142 g_int32_equal (gconstpointer v1
,
145 return *((const uint32_t*) v1
) == *((const uint32_t*) v2
);
148 static void vminfo_free(vminfo_t
*vminfo
)
150 g_return_if_fail(vminfo
!= NULL
);
152 if (vminfo
->nodename
)
153 g_free(vminfo
->nodename
);
159 void cfs_clnode_destroy(
160 cfs_clnode_t
*clnode
)
162 g_return_if_fail(clnode
!= NULL
);
165 g_hash_table_destroy(clnode
->kvhash
);
168 g_free(clnode
->name
);
173 cfs_clnode_t
*cfs_clnode_new(
178 g_return_val_if_fail(name
!= NULL
, NULL
);
180 cfs_clnode_t
*clnode
= g_new0(cfs_clnode_t
, 1);
184 clnode
->name
= g_strdup(name
);
185 clnode
->nodeid
= nodeid
;
186 clnode
->votes
= votes
;
191 gboolean
cfs_clinfo_destroy(
192 cfs_clinfo_t
*clinfo
)
194 g_return_val_if_fail(clinfo
!= NULL
, FALSE
);
196 if (clinfo
->cluster_name
)
197 g_free(clinfo
->cluster_name
);
199 if (clinfo
->nodes_byname
)
200 g_hash_table_destroy(clinfo
->nodes_byname
);
202 if (clinfo
->nodes_byid
)
203 g_hash_table_destroy(clinfo
->nodes_byid
);
210 cfs_clinfo_t
*cfs_clinfo_new(
211 const char *cluster_name
,
212 uint32_t cman_version
)
214 g_return_val_if_fail(cluster_name
!= NULL
, NULL
);
216 cfs_clinfo_t
*clinfo
= g_new0(cfs_clinfo_t
, 1);
220 clinfo
->cluster_name
= g_strdup(cluster_name
);
221 clinfo
->cman_version
= cman_version
;
223 if (!(clinfo
->nodes_byid
= g_hash_table_new_full(
224 g_int32_hash
, g_int32_equal
, NULL
,
225 (GDestroyNotify
)cfs_clnode_destroy
)))
228 if (!(clinfo
->nodes_byname
= g_hash_table_new(g_str_hash
, g_str_equal
)))
234 cfs_clinfo_destroy(clinfo
);
239 gboolean
cfs_clinfo_add_node(
240 cfs_clinfo_t
*clinfo
,
241 cfs_clnode_t
*clnode
)
243 g_return_val_if_fail(clinfo
!= NULL
, FALSE
);
244 g_return_val_if_fail(clnode
!= NULL
, FALSE
);
246 g_hash_table_replace(clinfo
->nodes_byid
, &clnode
->nodeid
, clnode
);
247 g_hash_table_replace(clinfo
->nodes_byname
, clnode
->name
, clnode
);
253 cfs_create_memberlist_msg(
256 g_return_val_if_fail(str
!= NULL
, -EINVAL
);
258 g_mutex_lock (&mutex
);
260 g_string_append_printf(str
,"{\n");
264 cfs_clinfo_t
*clinfo
= cfs_status
.clinfo
;
266 if (clinfo
&& clinfo
->nodes_byid
)
267 nodecount
= g_hash_table_size(clinfo
->nodes_byid
);
270 g_string_append_printf(str
, "\"nodename\": \"%s\",\n", cfs
.nodename
);
271 g_string_append_printf(str
, "\"version\": %u,\n", cfs_status
.clinfo_version
);
273 g_string_append_printf(str
, "\"cluster\": { ");
274 g_string_append_printf(str
, "\"name\": \"%s\", \"version\": %d, "
275 "\"nodes\": %d, \"quorate\": %d ",
276 clinfo
->cluster_name
, clinfo
->cman_version
,
277 nodecount
, cfs_status
.quorate
);
279 g_string_append_printf(str
,"},\n");
280 g_string_append_printf(str
,"\"nodelist\": {\n");
282 GHashTable
*ht
= clinfo
->nodes_byid
;
286 g_hash_table_iter_init (&iter
, ht
);
289 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
290 cfs_clnode_t
*node
= (cfs_clnode_t
*)value
;
291 if (i
) g_string_append_printf(str
, ",\n");
294 g_string_append_printf(str
, " \"%s\": { \"id\": %d, \"online\": %d",
295 node
->name
, node
->nodeid
, node
->online
);
298 char *ip
= (char *)g_hash_table_lookup(cfs_status
.iphash
, node
->name
);
300 g_string_append_printf(str
, ", \"ip\": \"%s\"", ip
);
303 g_string_append_printf(str
, "}");
306 g_string_append_printf(str
,"\n }\n");
308 g_string_append_printf(str
, "\"nodename\": \"%s\",\n", cfs
.nodename
);
309 g_string_append_printf(str
, "\"version\": %u\n", cfs_status
.clinfo_version
);
312 g_string_append_printf(str
,"}\n");
314 g_mutex_unlock (&mutex
);
320 kventry_free(kventry_t
*entry
)
322 g_return_if_fail(entry
!= NULL
);
330 kventry_hash_new(void)
332 return g_hash_table_new_full(g_str_hash
, g_str_equal
, NULL
,
333 (GDestroyNotify
)kventry_free
);
337 rrdentry_free(rrdentry_t
*entry
)
339 g_return_if_fail(entry
!= NULL
);
347 rrdentry_hash_new(void)
349 return g_hash_table_new_full(g_str_hash
, g_str_equal
, NULL
,
350 (GDestroyNotify
)rrdentry_free
);
354 cfs_cluster_log_dump(GString
*str
, const char *user
, guint max_entries
)
356 clusterlog_dump(cfs_status
.clusterlog
, str
, user
, max_entries
);
360 cfs_cluster_log(clog_entry_t
*entry
)
362 g_return_if_fail(entry
!= NULL
);
364 clusterlog_insert(cfs_status
.clusterlog
, entry
);
366 if (cfs_status
.kvstore
) {
368 iov
[0].iov_base
= (char *)entry
;
369 iov
[0].iov_len
= clog_entry_size(entry
);
371 if (dfsm_is_initialized(cfs_status
.kvstore
))
372 dfsm_send_message(cfs_status
.kvstore
, KVSTORE_MESSAGE_LOG
, iov
, 1);
376 void cfs_status_init(void)
378 g_mutex_lock (&mutex
);
380 cfs_status
.start_time
= time(NULL
);
382 cfs_status
.vmlist
= vmlist_hash_new();
384 cfs_status
.kvhash
= kventry_hash_new();
386 cfs_status
.rrdhash
= rrdentry_hash_new();
388 cfs_status
.iphash
= g_hash_table_new_full(g_str_hash
, g_str_equal
, g_free
, g_free
);
390 cfs_status
.memdb_changes
= g_hash_table_new(g_str_hash
, g_str_equal
);
392 for (int i
= 0; i
< G_N_ELEMENTS(memdb_change_array
); i
++) {
393 g_hash_table_replace(cfs_status
.memdb_changes
,
394 memdb_change_array
[i
].path
,
395 &memdb_change_array
[i
]);
398 cfs_status
.clusterlog
= clusterlog_new();
401 clusterlog_add(cfs_status
.clusterlog
, "root", "cluster", getpid(),
402 LOG_INFO
, "starting cluster log");
404 g_mutex_unlock (&mutex
);
407 void cfs_status_cleanup(void)
409 g_mutex_lock (&mutex
);
411 cfs_status
.clinfo_version
++;
413 if (cfs_status
.clinfo
) {
414 cfs_clinfo_destroy(cfs_status
.clinfo
);
415 cfs_status
.clinfo
= NULL
;
418 if (cfs_status
.vmlist
) {
419 g_hash_table_destroy(cfs_status
.vmlist
);
420 cfs_status
.vmlist
= NULL
;
423 if (cfs_status
.kvhash
) {
424 g_hash_table_destroy(cfs_status
.kvhash
);
425 cfs_status
.kvhash
= NULL
;
428 if (cfs_status
.rrdhash
) {
429 g_hash_table_destroy(cfs_status
.rrdhash
);
430 cfs_status
.rrdhash
= NULL
;
433 if (cfs_status
.iphash
) {
434 g_hash_table_destroy(cfs_status
.iphash
);
435 cfs_status
.iphash
= NULL
;
438 if (cfs_status
.clusterlog
)
439 clusterlog_destroy(cfs_status
.clusterlog
);
441 g_mutex_unlock (&mutex
);
444 void cfs_status_set_clinfo(
445 cfs_clinfo_t
*clinfo
)
447 g_return_if_fail(clinfo
!= NULL
);
449 g_mutex_lock (&mutex
);
451 cfs_status
.clinfo_version
++;
453 cfs_clinfo_t
*old
= cfs_status
.clinfo
;
455 cfs_status
.clinfo
= clinfo
;
457 cfs_message("update cluster info (cluster name %s, version = %d)",
458 clinfo
->cluster_name
, clinfo
->cman_version
);
461 if (old
&& old
->nodes_byid
&& clinfo
->nodes_byid
) {
463 GHashTable
*ht
= clinfo
->nodes_byid
;
467 g_hash_table_iter_init (&iter
, ht
);
469 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
470 cfs_clnode_t
*node
= (cfs_clnode_t
*)value
;
471 cfs_clnode_t
*oldnode
;
472 if ((oldnode
= g_hash_table_lookup(old
->nodes_byid
, key
))) {
473 node
->online
= oldnode
->online
;
474 node
->kvhash
= oldnode
->kvhash
;
475 oldnode
->kvhash
= NULL
;
482 cfs_clinfo_destroy(old
);
485 g_mutex_unlock (&mutex
);
489 dump_kvstore_versions(
492 const char *nodename
)
494 g_return_if_fail(kvhash
!= NULL
);
495 g_return_if_fail(str
!= NULL
);
496 g_return_if_fail(nodename
!= NULL
);
498 GHashTable
*ht
= kvhash
;
502 g_string_append_printf(str
, "\"%s\": {\n", nodename
);
504 g_hash_table_iter_init (&iter
, ht
);
507 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
508 kventry_t
*entry
= (kventry_t
*)value
;
509 if (i
) g_string_append_printf(str
, ",\n");
511 g_string_append_printf(str
,"\"%s\": %u", entry
->key
, entry
->version
);
514 g_string_append_printf(str
, "}\n");
518 cfs_create_version_msg(GString
*str
)
520 g_return_val_if_fail(str
!= NULL
, -EINVAL
);
522 g_mutex_lock (&mutex
);
524 g_string_append_printf(str
,"{\n");
526 g_string_append_printf(str
, "\"starttime\": %lu,\n", (unsigned long)cfs_status
.start_time
);
528 g_string_append_printf(str
, "\"clinfo\": %u,\n", cfs_status
.clinfo_version
);
530 g_string_append_printf(str
, "\"vmlist\": %u,\n", cfs_status
.vmlist_version
);
532 for (int i
= 0; i
< G_N_ELEMENTS(memdb_change_array
); i
++) {
533 g_string_append_printf(str
, "\"%s\": %u,\n",
534 memdb_change_array
[i
].path
,
535 memdb_change_array
[i
].version
);
538 g_string_append_printf(str
, "\"kvstore\": {\n");
540 dump_kvstore_versions(str
, cfs_status
.kvhash
, cfs
.nodename
);
542 cfs_clinfo_t
*clinfo
= cfs_status
.clinfo
;
544 if (clinfo
&& clinfo
->nodes_byid
) {
545 GHashTable
*ht
= clinfo
->nodes_byid
;
549 g_hash_table_iter_init (&iter
, ht
);
551 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
552 cfs_clnode_t
*node
= (cfs_clnode_t
*)value
;
555 g_string_append_printf(str
, ",\n");
556 dump_kvstore_versions(str
, node
->kvhash
, node
->name
);
560 g_string_append_printf(str
,"}\n");
562 g_string_append_printf(str
,"}\n");
564 g_mutex_unlock (&mutex
);
570 vmlist_hash_new(void)
572 return g_hash_table_new_full(g_int_hash
, g_int_equal
, NULL
,
573 (GDestroyNotify
)vminfo_free
);
577 vmlist_hash_insert_vm(
581 const char *nodename
,
584 g_return_val_if_fail(vmlist
!= NULL
, FALSE
);
585 g_return_val_if_fail(nodename
!= NULL
, FALSE
);
586 g_return_val_if_fail(vmid
!= 0, FALSE
);
587 g_return_val_if_fail(vmtype
== VMTYPE_QEMU
|| vmtype
== VMTYPE_OPENVZ
||
588 vmtype
== VMTYPE_LXC
, FALSE
);
590 if (!replace
&& g_hash_table_lookup(vmlist
, &vmid
)) {
591 cfs_critical("detected duplicate VMID %d", vmid
);
595 vminfo_t
*vminfo
= g_new0(vminfo_t
, 1);
598 vminfo
->vmtype
= vmtype
;
599 vminfo
->nodename
= g_strdup(nodename
);
601 vminfo
->version
= ++vminfo_version_counter
;
603 g_hash_table_replace(vmlist
, &vminfo
->vmid
, vminfo
);
612 const char *nodename
)
614 g_return_if_fail(cfs_status
.vmlist
!= NULL
);
615 g_return_if_fail(nodename
!= NULL
);
616 g_return_if_fail(vmid
!= 0);
617 g_return_if_fail(vmtype
== VMTYPE_QEMU
|| vmtype
== VMTYPE_OPENVZ
||
618 vmtype
== VMTYPE_LXC
);
620 cfs_debug("vmlist_register_vm: %s/%u %d", nodename
, vmid
, vmtype
);
622 g_mutex_lock (&mutex
);
624 cfs_status
.vmlist_version
++;
626 vmlist_hash_insert_vm(cfs_status
.vmlist
, vmtype
, vmid
, nodename
, TRUE
);
628 g_mutex_unlock (&mutex
);
632 vmlist_different_vm_exists(
635 const char *nodename
)
637 g_return_val_if_fail(cfs_status
.vmlist
!= NULL
, FALSE
);
638 g_return_val_if_fail(vmid
!= 0, FALSE
);
640 gboolean res
= FALSE
;
642 g_mutex_lock (&mutex
);
645 if ((vminfo
= (vminfo_t
*)g_hash_table_lookup(cfs_status
.vmlist
, &vmid
))) {
646 if (!(vminfo
->vmtype
== vmtype
&& strcmp(vminfo
->nodename
, nodename
) == 0))
649 g_mutex_unlock (&mutex
);
658 g_return_val_if_fail(cfs_status
.vmlist
!= NULL
, FALSE
);
659 g_return_val_if_fail(vmid
!= 0, FALSE
);
661 g_mutex_lock (&mutex
);
663 gpointer res
= g_hash_table_lookup(cfs_status
.vmlist
, &vmid
);
665 g_mutex_unlock (&mutex
);
674 g_return_if_fail(cfs_status
.vmlist
!= NULL
);
675 g_return_if_fail(vmid
!= 0);
677 g_mutex_lock (&mutex
);
679 cfs_status
.vmlist_version
++;
681 g_hash_table_remove(cfs_status
.vmlist
, &vmid
);
683 g_mutex_unlock (&mutex
);
686 void cfs_status_set_vmlist(
689 g_return_if_fail(vmlist
!= NULL
);
691 g_mutex_lock (&mutex
);
693 cfs_status
.vmlist_version
++;
695 if (cfs_status
.vmlist
)
696 g_hash_table_destroy(cfs_status
.vmlist
);
698 cfs_status
.vmlist
= vmlist
;
700 g_mutex_unlock (&mutex
);
704 cfs_create_vmlist_msg(GString
*str
)
706 g_return_val_if_fail(cfs_status
.vmlist
!= NULL
, -EINVAL
);
707 g_return_val_if_fail(str
!= NULL
, -EINVAL
);
709 g_mutex_lock (&mutex
);
711 g_string_append_printf(str
,"{\n");
713 GHashTable
*ht
= cfs_status
.vmlist
;
715 guint count
= g_hash_table_size(ht
);
718 g_string_append_printf(str
,"\"version\": %u\n", cfs_status
.vmlist_version
);
720 g_string_append_printf(str
,"\"version\": %u,\n", cfs_status
.vmlist_version
);
722 g_string_append_printf(str
,"\"ids\": {\n");
727 g_hash_table_iter_init (&iter
, ht
);
730 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
731 vminfo_t
*vminfo
= (vminfo_t
*)value
;
733 if (vminfo
->vmtype
== VMTYPE_QEMU
) {
735 } else if (vminfo
->vmtype
== VMTYPE_OPENVZ
) {
737 } else if (vminfo
->vmtype
== VMTYPE_LXC
) {
744 g_string_append_printf(str
, ",\n");
747 g_string_append_printf(str
,"\"%u\": { \"node\": \"%s\", \"type\": \"%s\", \"version\": %u }",
748 vminfo
->vmid
, vminfo
->nodename
, type
, vminfo
->version
);
751 g_string_append_printf(str
,"}\n");
753 g_string_append_printf(str
,"\n}\n");
755 g_mutex_unlock (&mutex
);
761 record_memdb_change(const char *path
)
763 g_return_if_fail(cfs_status
.memdb_changes
!= 0);
767 if ((ce
= (memdb_change_t
*)g_hash_table_lookup(cfs_status
.memdb_changes
, path
))) {
773 record_memdb_reload(void)
775 for (int i
= 0; i
< G_N_ELEMENTS(memdb_change_array
); i
++) {
776 memdb_change_array
[i
].version
++;
787 g_return_val_if_fail(kvhash
!= NULL
, FALSE
);
788 g_return_val_if_fail(key
!= NULL
, FALSE
);
789 g_return_val_if_fail(data
!= NULL
, FALSE
);
792 if ((entry
= (kventry_t
*)g_hash_table_lookup(kvhash
, key
))) {
794 entry
->data
= g_memdup(data
, len
);
798 kventry_t
*entry
= g_new0(kventry_t
, 1);
800 entry
->key
= g_strdup(key
);
801 entry
->data
= g_memdup(data
, len
);
804 g_hash_table_replace(kvhash
, entry
->key
, entry
);
810 static const char *rrd_def_node
[] = {
811 "DS:loadavg:GAUGE:120:0:U",
812 "DS:maxcpu:GAUGE:120:0:U",
813 "DS:cpu:GAUGE:120:0:U",
814 "DS:iowait:GAUGE:120:0:U",
815 "DS:memtotal:GAUGE:120:0:U",
816 "DS:memused:GAUGE:120:0:U",
817 "DS:swaptotal:GAUGE:120:0:U",
818 "DS:swapused:GAUGE:120:0:U",
819 "DS:roottotal:GAUGE:120:0:U",
820 "DS:rootused:GAUGE:120:0:U",
821 "DS:netin:DERIVE:120:0:U",
822 "DS:netout:DERIVE:120:0:U",
824 "RRA:AVERAGE:0.5:1:70", // 1 min avg - one hour
825 "RRA:AVERAGE:0.5:30:70", // 30 min avg - one day
826 "RRA:AVERAGE:0.5:180:70", // 3 hour avg - one week
827 "RRA:AVERAGE:0.5:720:70", // 12 hour avg - one month
828 "RRA:AVERAGE:0.5:10080:70", // 7 day avg - ony year
830 "RRA:MAX:0.5:1:70", // 1 min max - one hour
831 "RRA:MAX:0.5:30:70", // 30 min max - one day
832 "RRA:MAX:0.5:180:70", // 3 hour max - one week
833 "RRA:MAX:0.5:720:70", // 12 hour max - one month
834 "RRA:MAX:0.5:10080:70", // 7 day max - ony year
838 static const char *rrd_def_vm
[] = {
839 "DS:maxcpu:GAUGE:120:0:U",
840 "DS:cpu:GAUGE:120:0:U",
841 "DS:maxmem:GAUGE:120:0:U",
842 "DS:mem:GAUGE:120:0:U",
843 "DS:maxdisk:GAUGE:120:0:U",
844 "DS:disk:GAUGE:120:0:U",
845 "DS:netin:DERIVE:120:0:U",
846 "DS:netout:DERIVE:120:0:U",
847 "DS:diskread:DERIVE:120:0:U",
848 "DS:diskwrite:DERIVE:120:0:U",
850 "RRA:AVERAGE:0.5:1:70", // 1 min avg - one hour
851 "RRA:AVERAGE:0.5:30:70", // 30 min avg - one day
852 "RRA:AVERAGE:0.5:180:70", // 3 hour avg - one week
853 "RRA:AVERAGE:0.5:720:70", // 12 hour avg - one month
854 "RRA:AVERAGE:0.5:10080:70", // 7 day avg - ony year
856 "RRA:MAX:0.5:1:70", // 1 min max - one hour
857 "RRA:MAX:0.5:30:70", // 30 min max - one day
858 "RRA:MAX:0.5:180:70", // 3 hour max - one week
859 "RRA:MAX:0.5:720:70", // 12 hour max - one month
860 "RRA:MAX:0.5:10080:70", // 7 day max - ony year
864 static const char *rrd_def_storage
[] = {
865 "DS:total:GAUGE:120:0:U",
866 "DS:used:GAUGE:120:0:U",
868 "RRA:AVERAGE:0.5:1:70", // 1 min avg - one hour
869 "RRA:AVERAGE:0.5:30:70", // 30 min avg - one day
870 "RRA:AVERAGE:0.5:180:70", // 3 hour avg - one week
871 "RRA:AVERAGE:0.5:720:70", // 12 hour avg - one month
872 "RRA:AVERAGE:0.5:10080:70", // 7 day avg - ony year
874 "RRA:MAX:0.5:1:70", // 1 min max - one hour
875 "RRA:MAX:0.5:30:70", // 30 min max - one day
876 "RRA:MAX:0.5:180:70", // 3 hour max - one week
877 "RRA:MAX:0.5:720:70", // 12 hour max - one month
878 "RRA:MAX:0.5:10080:70", // 7 day max - ony year
882 #define RRDDIR "/var/lib/rrdcached/db"
886 const char *filename
,
888 const char *rrddef
[])
890 /* start at day boundary */
893 struct tm
*ltm
= localtime(&ctime
);
899 if (rrd_create_r(filename
, 60, timelocal(ltm
), argcount
, rrddef
)) {
900 cfs_message("RRD create error %s: %s", filename
, rrd_get_error());
904 static inline const char *
910 while (*data
&& found
< count
) {
923 g_return_if_fail(key
!= NULL
);
924 g_return_if_fail(data
!= NULL
);
925 g_return_if_fail(len
> 0);
926 g_return_if_fail(len
< 4096);
928 static const char *rrdcsock
= "unix:/var/run/rrdcached.sock";
931 if (rrdc_connect(rrdcsock
) != 0)
934 char *filename
= NULL
;
938 if (strncmp(key
, "pve2-node/", 10) == 0) {
939 const char *node
= key
+ 10;
943 if (strchr(node
, '/') != NULL
)
946 if (strlen(node
) < 1)
949 filename
= g_strdup_printf(RRDDIR
"/%s", key
);
951 if (!g_file_test(filename
, G_FILE_TEST_EXISTS
)) {
953 mkdir(RRDDIR
"/pve2-node", 0755);
954 int argcount
= sizeof(rrd_def_node
)/sizeof(void*) - 1;
955 create_rrd_file(filename
, argcount
, rrd_def_node
);
958 } else if ((strncmp(key
, "pve2-vm/", 8) == 0) ||
959 (strncmp(key
, "pve2.3-vm/", 10) == 0)) {
962 if (strncmp(key
, "pve2-vm/", 8) == 0) {
970 if (strchr(vmid
, '/') != NULL
)
973 if (strlen(vmid
) < 1)
976 filename
= g_strdup_printf(RRDDIR
"/%s/%s", "pve2-vm", vmid
);
978 if (!g_file_test(filename
, G_FILE_TEST_EXISTS
)) {
980 mkdir(RRDDIR
"/pve2-vm", 0755);
981 int argcount
= sizeof(rrd_def_vm
)/sizeof(void*) - 1;
982 create_rrd_file(filename
, argcount
, rrd_def_vm
);
985 } else if (strncmp(key
, "pve2-storage/", 13) == 0) {
986 const char *node
= key
+ 13;
988 const char *storage
= node
;
989 while (*storage
&& *storage
!= '/')
992 if (*storage
!= '/' || ((storage
- node
) < 1))
997 if (strchr(storage
, '/') != NULL
)
1000 if (strlen(storage
) < 1)
1003 filename
= g_strdup_printf(RRDDIR
"/%s", key
);
1005 if (!g_file_test(filename
, G_FILE_TEST_EXISTS
)) {
1007 mkdir(RRDDIR
"/pve2-storage", 0755);
1009 char *dir
= g_path_get_dirname(filename
);
1013 int argcount
= sizeof(rrd_def_storage
)/sizeof(void*) - 1;
1014 create_rrd_file(filename
, argcount
, rrd_def_storage
);
1021 const char *dp
= skip
? rrd_skip_data(data
, skip
) : data
;
1023 const char *update_args
[] = { dp
, NULL
};
1027 if ((status
= rrdc_update(filename
, 1, update_args
)) != 0) {
1028 cfs_message("RRDC update error %s: %d", filename
, status
);
1031 if (rrd_update_r(filename
, NULL
, 1, update_args
) != 0) {
1032 cfs_message("RRD update error %s: %s", filename
, rrd_get_error());
1038 if (rrd_update_r(filename
, NULL
, 1, update_args
) != 0) {
1039 cfs_message("RRD update error %s: %s", filename
, rrd_get_error());
1050 cfs_critical("RRD update error: unknown/wrong key %s", key
);
1060 rrdentry_t
*entry
= (rrdentry_t
*)value
;
1061 uint32_t ctime
= GPOINTER_TO_UINT(user_data
);
1063 int diff
= ctime
- entry
->time
;
1065 /* remove everything older than 5 minutes */
1068 return (diff
> expire
) ? TRUE
: FALSE
;
1071 static char *rrd_dump_buf
= NULL
;
1072 static time_t rrd_dump_last
= 0;
1075 cfs_rrd_dump(GString
*str
)
1080 if (rrd_dump_buf
&& (ctime
- rrd_dump_last
) < 2) {
1081 g_string_assign(str
, rrd_dump_buf
);
1085 /* remove old data */
1086 g_hash_table_foreach_remove(cfs_status
.rrdhash
, rrd_entry_is_old
,
1087 GUINT_TO_POINTER(ctime
));
1089 g_string_set_size(str
, 0);
1091 GHashTableIter iter
;
1092 gpointer key
, value
;
1094 g_hash_table_iter_init (&iter
, cfs_status
.rrdhash
);
1096 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
1097 rrdentry_t
*entry
= (rrdentry_t
*)value
;
1098 g_string_append(str
, key
);
1099 g_string_append(str
, ":");
1100 g_string_append(str
, entry
->data
);
1101 g_string_append(str
, "\n");
1104 g_string_append_c(str
, 0); // never return undef
1106 rrd_dump_last
= ctime
;
1108 g_free(rrd_dump_buf
);
1109 rrd_dump_buf
= g_strdup(str
->str
);
1115 const char *nodename
,
1119 g_return_val_if_fail(iphash
!= NULL
, FALSE
);
1120 g_return_val_if_fail(nodename
!= NULL
, FALSE
);
1121 g_return_val_if_fail(ip
!= NULL
, FALSE
);
1122 g_return_val_if_fail(len
> 0, FALSE
);
1123 g_return_val_if_fail(len
< 256, FALSE
);
1124 g_return_val_if_fail(ip
[len
-1] == 0, FALSE
);
1126 char *oldip
= (char *)g_hash_table_lookup(iphash
, nodename
);
1128 if (!oldip
|| (strcmp(oldip
, ip
) != 0)) {
1129 cfs_status
.clinfo_version
++;
1130 g_hash_table_replace(iphash
, g_strdup(nodename
), g_strdup(ip
));
1138 GHashTable
*rrdhash
,
1143 g_return_val_if_fail(rrdhash
!= NULL
, FALSE
);
1144 g_return_val_if_fail(key
!= NULL
, FALSE
);
1145 g_return_val_if_fail(data
!= NULL
, FALSE
);
1146 g_return_val_if_fail(len
> 0, FALSE
);
1147 g_return_val_if_fail(len
< 4096, FALSE
);
1148 g_return_val_if_fail(data
[len
-1] == 0, FALSE
);
1151 if ((entry
= (rrdentry_t
*)g_hash_table_lookup(rrdhash
, key
))) {
1152 g_free(entry
->data
);
1153 entry
->data
= g_memdup(data
, len
);
1155 entry
->time
= time(NULL
);
1157 rrdentry_t
*entry
= g_new0(rrdentry_t
, 1);
1159 entry
->key
= g_strdup(key
);
1160 entry
->data
= g_memdup(data
, len
);
1162 entry
->time
= time(NULL
);
1164 g_hash_table_replace(rrdhash
, entry
->key
, entry
);
1167 update_rrd_data(key
, data
, len
);
1173 kvstore_send_update_message(
1179 if (!dfsm_is_initialized(dfsm
))
1182 struct iovec iov
[2];
1185 g_strlcpy(name
, key
, sizeof(name
));
1187 iov
[0].iov_base
= &name
;
1188 iov
[0].iov_len
= sizeof(name
);
1190 iov
[1].iov_base
= (char *)data
;
1191 iov
[1].iov_len
= len
;
1193 if (dfsm_send_message(dfsm
, KVSTORE_MESSAGE_UPDATE
, iov
, 2) == CS_OK
)
1199 static clog_entry_t
*
1200 kvstore_parse_log_message(
1204 g_return_val_if_fail(msg
!= NULL
, NULL
);
1206 if (msg_len
< sizeof(clog_entry_t
)) {
1207 cfs_critical("received short log message (%lu < %lu)", msg_len
, sizeof(clog_entry_t
));
1211 clog_entry_t
*entry
= (clog_entry_t
*)msg
;
1213 uint32_t size
= sizeof(clog_entry_t
) + entry
->node_len
+
1214 entry
->ident_len
+ entry
->tag_len
+ entry
->msg_len
;
1216 if (msg_len
!= size
) {
1217 cfs_critical("received log message with wrong size (%lu != %u)", msg_len
, size
);
1223 if (*((char *)msg
+ entry
->node_len
- 1)) {
1224 cfs_critical("unterminated string in log message");
1227 msg
+= entry
->node_len
;
1229 if (*((char *)msg
+ entry
->ident_len
- 1)) {
1230 cfs_critical("unterminated string in log message");
1233 msg
+= entry
->ident_len
;
1235 if (*((char *)msg
+ entry
->tag_len
- 1)) {
1236 cfs_critical("unterminated string in log message");
1239 msg
+= entry
->tag_len
;
1241 if (*((char *)msg
+ entry
->msg_len
- 1)) {
1242 cfs_critical("unterminated string in log message");
1250 kvstore_parse_update_message(
1254 gconstpointer
*data
,
1257 g_return_val_if_fail(msg
!= NULL
, FALSE
);
1258 g_return_val_if_fail(key
!= NULL
, FALSE
);
1259 g_return_val_if_fail(data
!= NULL
, FALSE
);
1260 g_return_val_if_fail(len
!= NULL
, FALSE
);
1262 if (msg_len
< 256) {
1263 cfs_critical("received short kvstore message (%lu < 256)", msg_len
);
1267 /* test if key is null terminated */
1269 for (i
= 0; i
< 256; i
++)
1270 if (((char *)msg
)[i
] == 0)
1277 *len
= msg_len
- 256;
1285 cfs_create_status_msg(
1287 const char *nodename
,
1290 g_return_val_if_fail(str
!= NULL
, -EINVAL
);
1291 g_return_val_if_fail(key
!= NULL
, -EINVAL
);
1295 GHashTable
*kvhash
= NULL
;
1297 g_mutex_lock (&mutex
);
1299 if (!nodename
|| !nodename
[0] || !strcmp(nodename
, cfs
.nodename
)) {
1300 kvhash
= cfs_status
.kvhash
;
1302 cfs_clnode_t
*clnode
;
1303 if ((clnode
= g_hash_table_lookup(cfs_status
.clinfo
->nodes_byname
, nodename
)))
1304 kvhash
= clnode
->kvhash
;
1308 if (kvhash
&& (entry
= (kventry_t
*)g_hash_table_lookup(kvhash
, key
))) {
1309 g_string_append_len(str
, entry
->data
, entry
->len
);
1313 g_mutex_unlock (&mutex
);
1324 g_return_val_if_fail(key
!= NULL
, FALSE
);
1325 g_return_val_if_fail(data
!= NULL
, FALSE
);
1326 g_return_val_if_fail(cfs_status
.kvhash
!= NULL
, FALSE
);
1328 if (len
> CFS_MAX_STATUS_SIZE
)
1331 g_mutex_lock (&mutex
);
1335 if (strncmp(key
, "rrd/", 4) == 0) {
1336 res
= rrdentry_hash_set(cfs_status
.rrdhash
, key
+ 4, data
, len
);
1337 } else if (!strcmp(key
, "nodeip")) {
1338 res
= nodeip_hash_set(cfs_status
.iphash
, cfs
.nodename
, data
, len
);
1340 res
= kventry_hash_set(cfs_status
.kvhash
, key
, data
, len
);
1342 g_mutex_unlock (&mutex
);
1344 if (cfs_status
.kvstore
)
1345 kvstore_send_update_message(cfs_status
.kvstore
, key
, data
, len
);
1347 return res
? 0 : -ENOMEM
;
1351 cfs_kvstore_node_set(
1357 g_return_val_if_fail(nodeid
!= 0, FALSE
);
1358 g_return_val_if_fail(key
!= NULL
, FALSE
);
1359 g_return_val_if_fail(data
!= NULL
, FALSE
);
1361 g_mutex_lock (&mutex
);
1363 if (!cfs_status
.clinfo
|| !cfs_status
.clinfo
->nodes_byid
)
1364 goto ret
; /* ignore */
1366 cfs_clnode_t
*clnode
= g_hash_table_lookup(cfs_status
.clinfo
->nodes_byid
, &nodeid
);
1368 goto ret
; /* ignore */
1370 cfs_debug("got node %d status update %s", nodeid
, key
);
1372 if (strncmp(key
, "rrd/", 4) == 0) {
1373 rrdentry_hash_set(cfs_status
.rrdhash
, key
+ 4, data
, len
);
1374 } else if (!strcmp(key
, "nodeip")) {
1375 nodeip_hash_set(cfs_status
.iphash
, clnode
->name
, data
, len
);
1377 if (!clnode
->kvhash
) {
1378 if (!(clnode
->kvhash
= kventry_hash_new())) {
1379 goto ret
; /*ignore */
1383 kventry_hash_set(clnode
->kvhash
, key
, data
, len
);
1387 g_mutex_unlock (&mutex
);
1393 cfs_kvstore_sync(void)
1395 g_return_val_if_fail(cfs_status
.kvhash
!= NULL
, FALSE
);
1396 g_return_val_if_fail(cfs_status
.kvstore
!= NULL
, FALSE
);
1398 gboolean res
= TRUE
;
1400 g_mutex_lock (&mutex
);
1402 GHashTable
*ht
= cfs_status
.kvhash
;
1403 GHashTableIter iter
;
1404 gpointer key
, value
;
1406 g_hash_table_iter_init (&iter
, ht
);
1408 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
1409 kventry_t
*entry
= (kventry_t
*)value
;
1410 kvstore_send_update_message(cfs_status
.kvstore
, entry
->key
, entry
->data
, entry
->len
);
1413 g_mutex_unlock (&mutex
);
1430 g_return_val_if_fail(dfsm
!= NULL
, -1);
1431 g_return_val_if_fail(msg
!= NULL
, -1);
1432 g_return_val_if_fail(res_ptr
!= NULL
, -1);
1434 /* ignore message for ourself */
1435 if (dfsm_nodeid_is_local(dfsm
, nodeid
, pid
))
1438 if (msg_type
== KVSTORE_MESSAGE_UPDATE
) {
1442 if (kvstore_parse_update_message(msg
, msg_len
, &key
, &data
, &len
)) {
1443 cfs_kvstore_node_set(nodeid
, key
, data
, len
);
1445 cfs_critical("cant parse update message");
1447 } else if (msg_type
== KVSTORE_MESSAGE_LOG
) {
1448 cfs_message("received log"); // fixme: remove
1449 const clog_entry_t
*entry
;
1450 if ((entry
= kvstore_parse_log_message(msg
, msg_len
))) {
1451 clusterlog_insert(cfs_status
.clusterlog
, entry
);
1453 cfs_critical("cant parse log message");
1456 cfs_critical("received unknown message type %d\n", msg_type
);
1473 const struct cpg_address
*member_list
,
1474 size_t member_list_entries
)
1476 g_return_if_fail(dfsm
!= NULL
);
1477 g_return_if_fail(member_list
!= NULL
);
1479 cfs_debug("enter %s", __func__
);
1481 g_mutex_lock (&mutex
);
1483 cfs_clinfo_t
*clinfo
= cfs_status
.clinfo
;
1485 if (clinfo
&& clinfo
->nodes_byid
) {
1487 GHashTable
*ht
= clinfo
->nodes_byid
;
1488 GHashTableIter iter
;
1489 gpointer key
, value
;
1491 g_hash_table_iter_init (&iter
, ht
);
1493 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
1494 cfs_clnode_t
*node
= (cfs_clnode_t
*)value
;
1495 node
->online
= FALSE
;
1498 for (int i
= 0; i
< member_list_entries
; i
++) {
1500 if ((node
= g_hash_table_lookup(clinfo
->nodes_byid
, &member_list
[i
].nodeid
))) {
1501 node
->online
= TRUE
;
1505 cfs_status
.clinfo_version
++;
1508 g_mutex_unlock (&mutex
);
1515 unsigned int *res_len
)
1517 g_return_val_if_fail(dfsm
!= NULL
, NULL
);
1519 gpointer msg
= clusterlog_get_state(cfs_status
.clusterlog
, res_len
);
1525 dfsm_process_update(
1528 dfsm_sync_info_t
*syncinfo
,
1534 cfs_critical("%s: received unexpected update message", __func__
);
1540 dfsm_process_state_update(
1543 dfsm_sync_info_t
*syncinfo
)
1545 g_return_val_if_fail(dfsm
!= NULL
, -1);
1546 g_return_val_if_fail(syncinfo
!= NULL
, -1);
1548 clog_base_t
*clog
[syncinfo
->node_count
];
1550 int local_index
= -1;
1551 for (int i
= 0; i
< syncinfo
->node_count
; i
++) {
1552 dfsm_node_info_t
*ni
= &syncinfo
->nodes
[i
];
1555 if (syncinfo
->local
== ni
)
1558 clog_base_t
*base
= (clog_base_t
*)ni
->state
;
1559 if (ni
->state_len
> 8 && ni
->state_len
== clog_size(base
)) {
1560 clog
[i
] = ni
->state
;
1562 cfs_critical("received log with wrong size %u", ni
->state_len
);
1567 if (!clusterlog_merge(cfs_status
.clusterlog
, clog
, syncinfo
->node_count
, local_index
)) {
1568 cfs_critical("unable to merge log files");
1580 dfsm_sync_info_t
*syncinfo
)
1582 g_return_val_if_fail(dfsm
!= NULL
, -1);
1583 g_return_val_if_fail(syncinfo
!= NULL
, -1);
1589 dfsm_synced(dfsm_t
*dfsm
)
1591 g_return_if_fail(dfsm
!= NULL
);
1593 char *ip
= (char *)g_hash_table_lookup(cfs_status
.iphash
, cfs
.nodename
);
1597 cfs_status_set("nodeip", ip
, strlen(ip
) + 1);
1604 dfsm_sync_info_t
*syncinfo
)
1609 static dfsm_callbacks_t kvstore_dfsm_callbacks
= {
1610 .dfsm_deliver_fn
= dfsm_deliver
,
1611 .dfsm_confchg_fn
= dfsm_confchg
,
1613 .dfsm_get_state_fn
= dfsm_get_state
,
1614 .dfsm_process_state_update_fn
= dfsm_process_state_update
,
1615 .dfsm_process_update_fn
= dfsm_process_update
,
1616 .dfsm_commit_fn
= dfsm_commit
,
1617 .dfsm_cleanup_fn
= dfsm_cleanup
,
1618 .dfsm_synced_fn
= dfsm_synced
,
1622 cfs_status_dfsm_new(void)
1624 g_mutex_lock (&mutex
);
1626 cfs_status
.kvstore
= dfsm_new(NULL
, KVSTORE_CPG_GROUP_NAME
, G_LOG_DOMAIN
,
1627 0, &kvstore_dfsm_callbacks
);
1628 g_mutex_unlock (&mutex
);
1630 return cfs_status
.kvstore
;
1634 cfs_is_quorate(void)
1636 g_mutex_lock (&mutex
);
1637 gboolean res
= cfs_status
.quorate
;
1638 g_mutex_unlock (&mutex
);
1648 g_mutex_lock (&mutex
);
1650 uint32_t prev_quorate
= cfs_status
.quorate
;
1651 cfs_status
.quorate
= quorate
;
1653 if (!prev_quorate
&& cfs_status
.quorate
) {
1655 cfs_message("node has quorum");
1658 if (prev_quorate
&& !cfs_status
.quorate
) {
1660 cfs_message("node lost quorum");
1663 g_mutex_unlock (&mutex
);