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 void cfs_clnode_destroy(
163 cfs_clnode_t
*clnode
)
165 g_return_if_fail(clnode
!= NULL
);
168 g_hash_table_destroy(clnode
->kvhash
);
171 g_free(clnode
->name
);
176 cfs_clnode_t
*cfs_clnode_new(
181 g_return_val_if_fail(name
!= NULL
, NULL
);
183 cfs_clnode_t
*clnode
= g_new0(cfs_clnode_t
, 1);
187 clnode
->name
= g_strdup(name
);
188 clnode
->nodeid
= nodeid
;
189 clnode
->votes
= votes
;
194 gboolean
cfs_clinfo_destroy(
195 cfs_clinfo_t
*clinfo
)
197 g_return_val_if_fail(clinfo
!= NULL
, FALSE
);
199 if (clinfo
->cluster_name
)
200 g_free(clinfo
->cluster_name
);
202 if (clinfo
->nodes_byname
)
203 g_hash_table_destroy(clinfo
->nodes_byname
);
205 if (clinfo
->nodes_byid
)
206 g_hash_table_destroy(clinfo
->nodes_byid
);
213 cfs_clinfo_t
*cfs_clinfo_new(
214 const char *cluster_name
,
215 uint32_t cman_version
)
217 g_return_val_if_fail(cluster_name
!= NULL
, NULL
);
219 cfs_clinfo_t
*clinfo
= g_new0(cfs_clinfo_t
, 1);
223 clinfo
->cluster_name
= g_strdup(cluster_name
);
224 clinfo
->cman_version
= cman_version
;
226 if (!(clinfo
->nodes_byid
= g_hash_table_new_full(
227 g_int32_hash
, g_int32_equal
, NULL
,
228 (GDestroyNotify
)cfs_clnode_destroy
)))
231 if (!(clinfo
->nodes_byname
= g_hash_table_new(g_str_hash
, g_str_equal
)))
237 cfs_clinfo_destroy(clinfo
);
242 gboolean
cfs_clinfo_add_node(
243 cfs_clinfo_t
*clinfo
,
244 cfs_clnode_t
*clnode
)
246 g_return_val_if_fail(clinfo
!= NULL
, FALSE
);
247 g_return_val_if_fail(clnode
!= NULL
, FALSE
);
249 g_hash_table_replace(clinfo
->nodes_byid
, &clnode
->nodeid
, clnode
);
250 g_hash_table_replace(clinfo
->nodes_byname
, clnode
->name
, clnode
);
256 cfs_create_memberlist_msg(
259 g_return_val_if_fail(str
!= NULL
, -EINVAL
);
261 g_mutex_lock (&mutex
);
263 g_string_append_printf(str
,"{\n");
267 cfs_clinfo_t
*clinfo
= cfs_status
.clinfo
;
269 if (clinfo
&& clinfo
->nodes_byid
)
270 nodecount
= g_hash_table_size(clinfo
->nodes_byid
);
273 g_string_append_printf(str
, "\"nodename\": \"%s\",\n", cfs
.nodename
);
274 g_string_append_printf(str
, "\"version\": %u,\n", cfs_status
.clinfo_version
);
276 g_string_append_printf(str
, "\"cluster\": { ");
277 g_string_append_printf(str
, "\"name\": \"%s\", \"version\": %d, "
278 "\"nodes\": %d, \"quorate\": %d ",
279 clinfo
->cluster_name
, clinfo
->cman_version
,
280 nodecount
, cfs_status
.quorate
);
282 g_string_append_printf(str
,"},\n");
283 g_string_append_printf(str
,"\"nodelist\": {\n");
285 GHashTable
*ht
= clinfo
->nodes_byid
;
289 g_hash_table_iter_init (&iter
, ht
);
292 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
293 cfs_clnode_t
*node
= (cfs_clnode_t
*)value
;
294 if (i
) g_string_append_printf(str
, ",\n");
297 g_string_append_printf(str
, " \"%s\": { \"id\": %d, \"online\": %d",
298 node
->name
, node
->nodeid
, node
->online
);
301 char *ip
= (char *)g_hash_table_lookup(cfs_status
.iphash
, node
->name
);
303 g_string_append_printf(str
, ", \"ip\": \"%s\"", ip
);
306 g_string_append_printf(str
, "}");
309 g_string_append_printf(str
,"\n }\n");
311 g_string_append_printf(str
, "\"nodename\": \"%s\",\n", cfs
.nodename
);
312 g_string_append_printf(str
, "\"version\": %u\n", cfs_status
.clinfo_version
);
315 g_string_append_printf(str
,"}\n");
317 g_mutex_unlock (&mutex
);
323 kventry_free(kventry_t
*entry
)
325 g_return_if_fail(entry
!= NULL
);
333 kventry_hash_new(void)
335 return g_hash_table_new_full(g_str_hash
, g_str_equal
, NULL
,
336 (GDestroyNotify
)kventry_free
);
340 rrdentry_free(rrdentry_t
*entry
)
342 g_return_if_fail(entry
!= NULL
);
350 rrdentry_hash_new(void)
352 return g_hash_table_new_full(g_str_hash
, g_str_equal
, NULL
,
353 (GDestroyNotify
)rrdentry_free
);
357 cfs_cluster_log_dump(GString
*str
, const char *user
, guint max_entries
)
359 clusterlog_dump(cfs_status
.clusterlog
, str
, user
, max_entries
);
363 cfs_cluster_log(clog_entry_t
*entry
)
365 g_return_if_fail(entry
!= NULL
);
367 clusterlog_insert(cfs_status
.clusterlog
, entry
);
369 if (cfs_status
.kvstore
) {
371 iov
[0].iov_base
= (char *)entry
;
372 iov
[0].iov_len
= clog_entry_size(entry
);
374 if (dfsm_is_initialized(cfs_status
.kvstore
))
375 dfsm_send_message(cfs_status
.kvstore
, KVSTORE_MESSAGE_LOG
, iov
, 1);
379 void cfs_status_init(void)
381 g_mutex_lock (&mutex
);
383 cfs_status
.start_time
= time(NULL
);
385 cfs_status
.vmlist
= vmlist_hash_new();
387 cfs_status
.kvhash
= kventry_hash_new();
389 cfs_status
.rrdhash
= rrdentry_hash_new();
391 cfs_status
.iphash
= g_hash_table_new_full(g_str_hash
, g_str_equal
, g_free
, g_free
);
393 cfs_status
.memdb_changes
= g_hash_table_new(g_str_hash
, g_str_equal
);
395 for (int i
= 0; i
< G_N_ELEMENTS(memdb_change_array
); i
++) {
396 g_hash_table_replace(cfs_status
.memdb_changes
,
397 memdb_change_array
[i
].path
,
398 &memdb_change_array
[i
]);
401 cfs_status
.clusterlog
= clusterlog_new();
404 clusterlog_add(cfs_status
.clusterlog
, "root", "cluster", getpid(),
405 LOG_INFO
, "starting cluster log");
407 g_mutex_unlock (&mutex
);
410 void cfs_status_cleanup(void)
412 g_mutex_lock (&mutex
);
414 cfs_status
.clinfo_version
++;
416 if (cfs_status
.clinfo
) {
417 cfs_clinfo_destroy(cfs_status
.clinfo
);
418 cfs_status
.clinfo
= NULL
;
421 if (cfs_status
.vmlist
) {
422 g_hash_table_destroy(cfs_status
.vmlist
);
423 cfs_status
.vmlist
= NULL
;
426 if (cfs_status
.kvhash
) {
427 g_hash_table_destroy(cfs_status
.kvhash
);
428 cfs_status
.kvhash
= NULL
;
431 if (cfs_status
.rrdhash
) {
432 g_hash_table_destroy(cfs_status
.rrdhash
);
433 cfs_status
.rrdhash
= NULL
;
436 if (cfs_status
.iphash
) {
437 g_hash_table_destroy(cfs_status
.iphash
);
438 cfs_status
.iphash
= NULL
;
441 if (cfs_status
.clusterlog
)
442 clusterlog_destroy(cfs_status
.clusterlog
);
444 g_mutex_unlock (&mutex
);
447 void cfs_status_set_clinfo(
448 cfs_clinfo_t
*clinfo
)
450 g_return_if_fail(clinfo
!= NULL
);
452 g_mutex_lock (&mutex
);
454 cfs_status
.clinfo_version
++;
456 cfs_clinfo_t
*old
= cfs_status
.clinfo
;
458 cfs_status
.clinfo
= clinfo
;
460 cfs_message("update cluster info (cluster name %s, version = %d)",
461 clinfo
->cluster_name
, clinfo
->cman_version
);
464 if (old
&& old
->nodes_byid
&& clinfo
->nodes_byid
) {
466 GHashTable
*ht
= clinfo
->nodes_byid
;
470 g_hash_table_iter_init (&iter
, ht
);
472 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
473 cfs_clnode_t
*node
= (cfs_clnode_t
*)value
;
474 cfs_clnode_t
*oldnode
;
475 if ((oldnode
= g_hash_table_lookup(old
->nodes_byid
, key
))) {
476 node
->online
= oldnode
->online
;
477 node
->kvhash
= oldnode
->kvhash
;
478 oldnode
->kvhash
= NULL
;
485 cfs_clinfo_destroy(old
);
488 g_mutex_unlock (&mutex
);
492 dump_kvstore_versions(
495 const char *nodename
)
497 g_return_if_fail(kvhash
!= NULL
);
498 g_return_if_fail(str
!= NULL
);
499 g_return_if_fail(nodename
!= NULL
);
501 GHashTable
*ht
= kvhash
;
505 g_string_append_printf(str
, "\"%s\": {\n", nodename
);
507 g_hash_table_iter_init (&iter
, ht
);
510 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
511 kventry_t
*entry
= (kventry_t
*)value
;
512 if (i
) g_string_append_printf(str
, ",\n");
514 g_string_append_printf(str
,"\"%s\": %u", entry
->key
, entry
->version
);
517 g_string_append_printf(str
, "}\n");
521 cfs_create_version_msg(GString
*str
)
523 g_return_val_if_fail(str
!= NULL
, -EINVAL
);
525 g_mutex_lock (&mutex
);
527 g_string_append_printf(str
,"{\n");
529 g_string_append_printf(str
, "\"starttime\": %lu,\n", (unsigned long)cfs_status
.start_time
);
531 g_string_append_printf(str
, "\"clinfo\": %u,\n", cfs_status
.clinfo_version
);
533 g_string_append_printf(str
, "\"vmlist\": %u,\n", cfs_status
.vmlist_version
);
535 for (int i
= 0; i
< G_N_ELEMENTS(memdb_change_array
); i
++) {
536 g_string_append_printf(str
, "\"%s\": %u,\n",
537 memdb_change_array
[i
].path
,
538 memdb_change_array
[i
].version
);
541 g_string_append_printf(str
, "\"kvstore\": {\n");
543 dump_kvstore_versions(str
, cfs_status
.kvhash
, cfs
.nodename
);
545 cfs_clinfo_t
*clinfo
= cfs_status
.clinfo
;
547 if (clinfo
&& clinfo
->nodes_byid
) {
548 GHashTable
*ht
= clinfo
->nodes_byid
;
552 g_hash_table_iter_init (&iter
, ht
);
554 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
555 cfs_clnode_t
*node
= (cfs_clnode_t
*)value
;
558 g_string_append_printf(str
, ",\n");
559 dump_kvstore_versions(str
, node
->kvhash
, node
->name
);
563 g_string_append_printf(str
,"}\n");
565 g_string_append_printf(str
,"}\n");
567 g_mutex_unlock (&mutex
);
573 vmlist_hash_new(void)
575 return g_hash_table_new_full(g_int_hash
, g_int_equal
, NULL
,
576 (GDestroyNotify
)vminfo_free
);
580 vmlist_hash_insert_vm(
584 const char *nodename
,
587 g_return_val_if_fail(vmlist
!= NULL
, FALSE
);
588 g_return_val_if_fail(nodename
!= NULL
, FALSE
);
589 g_return_val_if_fail(vmid
!= 0, FALSE
);
590 g_return_val_if_fail(vmtype
== VMTYPE_QEMU
|| vmtype
== VMTYPE_OPENVZ
||
591 vmtype
== VMTYPE_LXC
, FALSE
);
593 if (!replace
&& g_hash_table_lookup(vmlist
, &vmid
)) {
594 cfs_critical("detected duplicate VMID %d", vmid
);
598 vminfo_t
*vminfo
= g_new0(vminfo_t
, 1);
601 vminfo
->vmtype
= vmtype
;
602 vminfo
->nodename
= g_strdup(nodename
);
604 vminfo
->version
= ++vminfo_version_counter
;
606 g_hash_table_replace(vmlist
, &vminfo
->vmid
, vminfo
);
615 const char *nodename
)
617 g_return_if_fail(cfs_status
.vmlist
!= NULL
);
618 g_return_if_fail(nodename
!= NULL
);
619 g_return_if_fail(vmid
!= 0);
620 g_return_if_fail(vmtype
== VMTYPE_QEMU
|| vmtype
== VMTYPE_OPENVZ
||
621 vmtype
== VMTYPE_LXC
);
623 cfs_debug("vmlist_register_vm: %s/%u %d", nodename
, vmid
, vmtype
);
625 g_mutex_lock (&mutex
);
627 cfs_status
.vmlist_version
++;
629 vmlist_hash_insert_vm(cfs_status
.vmlist
, vmtype
, vmid
, nodename
, TRUE
);
631 g_mutex_unlock (&mutex
);
635 vmlist_different_vm_exists(
638 const char *nodename
)
640 g_return_val_if_fail(cfs_status
.vmlist
!= NULL
, FALSE
);
641 g_return_val_if_fail(vmid
!= 0, FALSE
);
643 gboolean res
= FALSE
;
645 g_mutex_lock (&mutex
);
648 if ((vminfo
= (vminfo_t
*)g_hash_table_lookup(cfs_status
.vmlist
, &vmid
))) {
649 if (!(vminfo
->vmtype
== vmtype
&& strcmp(vminfo
->nodename
, nodename
) == 0))
652 g_mutex_unlock (&mutex
);
661 g_return_val_if_fail(cfs_status
.vmlist
!= NULL
, FALSE
);
662 g_return_val_if_fail(vmid
!= 0, FALSE
);
664 g_mutex_lock (&mutex
);
666 gpointer res
= g_hash_table_lookup(cfs_status
.vmlist
, &vmid
);
668 g_mutex_unlock (&mutex
);
677 g_return_if_fail(cfs_status
.vmlist
!= NULL
);
678 g_return_if_fail(vmid
!= 0);
680 g_mutex_lock (&mutex
);
682 cfs_status
.vmlist_version
++;
684 g_hash_table_remove(cfs_status
.vmlist
, &vmid
);
686 g_mutex_unlock (&mutex
);
689 void cfs_status_set_vmlist(
692 g_return_if_fail(vmlist
!= NULL
);
694 g_mutex_lock (&mutex
);
696 cfs_status
.vmlist_version
++;
698 if (cfs_status
.vmlist
)
699 g_hash_table_destroy(cfs_status
.vmlist
);
701 cfs_status
.vmlist
= vmlist
;
703 g_mutex_unlock (&mutex
);
707 cfs_create_vmlist_msg(GString
*str
)
709 g_return_val_if_fail(cfs_status
.vmlist
!= NULL
, -EINVAL
);
710 g_return_val_if_fail(str
!= NULL
, -EINVAL
);
712 g_mutex_lock (&mutex
);
714 g_string_append_printf(str
,"{\n");
716 GHashTable
*ht
= cfs_status
.vmlist
;
718 guint count
= g_hash_table_size(ht
);
721 g_string_append_printf(str
,"\"version\": %u\n", cfs_status
.vmlist_version
);
723 g_string_append_printf(str
,"\"version\": %u,\n", cfs_status
.vmlist_version
);
725 g_string_append_printf(str
,"\"ids\": {\n");
730 g_hash_table_iter_init (&iter
, ht
);
733 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
734 vminfo_t
*vminfo
= (vminfo_t
*)value
;
736 if (vminfo
->vmtype
== VMTYPE_QEMU
) {
738 } else if (vminfo
->vmtype
== VMTYPE_OPENVZ
) {
740 } else if (vminfo
->vmtype
== VMTYPE_LXC
) {
747 g_string_append_printf(str
, ",\n");
750 g_string_append_printf(str
,"\"%u\": { \"node\": \"%s\", \"type\": \"%s\", \"version\": %u }",
751 vminfo
->vmid
, vminfo
->nodename
, type
, vminfo
->version
);
754 g_string_append_printf(str
,"}\n");
756 g_string_append_printf(str
,"\n}\n");
758 g_mutex_unlock (&mutex
);
764 record_memdb_change(const char *path
)
766 g_return_if_fail(cfs_status
.memdb_changes
!= 0);
770 if ((ce
= (memdb_change_t
*)g_hash_table_lookup(cfs_status
.memdb_changes
, path
))) {
776 record_memdb_reload(void)
778 for (int i
= 0; i
< G_N_ELEMENTS(memdb_change_array
); i
++) {
779 memdb_change_array
[i
].version
++;
790 g_return_val_if_fail(kvhash
!= NULL
, FALSE
);
791 g_return_val_if_fail(key
!= NULL
, FALSE
);
792 g_return_val_if_fail(data
!= NULL
, FALSE
);
795 if ((entry
= (kventry_t
*)g_hash_table_lookup(kvhash
, key
))) {
797 entry
->data
= g_memdup(data
, len
);
801 kventry_t
*entry
= g_new0(kventry_t
, 1);
803 entry
->key
= g_strdup(key
);
804 entry
->data
= g_memdup(data
, len
);
807 g_hash_table_replace(kvhash
, entry
->key
, entry
);
813 static const char *rrd_def_node
[] = {
814 "DS:loadavg:GAUGE:120:0:U",
815 "DS:maxcpu:GAUGE:120:0:U",
816 "DS:cpu:GAUGE:120:0:U",
817 "DS:iowait:GAUGE:120:0:U",
818 "DS:memtotal:GAUGE:120:0:U",
819 "DS:memused:GAUGE:120:0:U",
820 "DS:swaptotal:GAUGE:120:0:U",
821 "DS:swapused:GAUGE:120:0:U",
822 "DS:roottotal:GAUGE:120:0:U",
823 "DS:rootused:GAUGE:120:0:U",
824 "DS:netin:DERIVE:120:0:U",
825 "DS:netout:DERIVE:120:0:U",
827 "RRA:AVERAGE:0.5:1:70", // 1 min avg - one hour
828 "RRA:AVERAGE:0.5:30:70", // 30 min avg - one day
829 "RRA:AVERAGE:0.5:180:70", // 3 hour avg - one week
830 "RRA:AVERAGE:0.5:720:70", // 12 hour avg - one month
831 "RRA:AVERAGE:0.5:10080:70", // 7 day avg - ony year
833 "RRA:MAX:0.5:1:70", // 1 min max - one hour
834 "RRA:MAX:0.5:30:70", // 30 min max - one day
835 "RRA:MAX:0.5:180:70", // 3 hour max - one week
836 "RRA:MAX:0.5:720:70", // 12 hour max - one month
837 "RRA:MAX:0.5:10080:70", // 7 day max - ony year
841 static const char *rrd_def_vm
[] = {
842 "DS:maxcpu:GAUGE:120:0:U",
843 "DS:cpu:GAUGE:120:0:U",
844 "DS:maxmem:GAUGE:120:0:U",
845 "DS:mem:GAUGE:120:0:U",
846 "DS:maxdisk:GAUGE:120:0:U",
847 "DS:disk:GAUGE:120:0:U",
848 "DS:netin:DERIVE:120:0:U",
849 "DS:netout:DERIVE:120:0:U",
850 "DS:diskread:DERIVE:120:0:U",
851 "DS:diskwrite:DERIVE:120:0:U",
853 "RRA:AVERAGE:0.5:1:70", // 1 min avg - one hour
854 "RRA:AVERAGE:0.5:30:70", // 30 min avg - one day
855 "RRA:AVERAGE:0.5:180:70", // 3 hour avg - one week
856 "RRA:AVERAGE:0.5:720:70", // 12 hour avg - one month
857 "RRA:AVERAGE:0.5:10080:70", // 7 day avg - ony year
859 "RRA:MAX:0.5:1:70", // 1 min max - one hour
860 "RRA:MAX:0.5:30:70", // 30 min max - one day
861 "RRA:MAX:0.5:180:70", // 3 hour max - one week
862 "RRA:MAX:0.5:720:70", // 12 hour max - one month
863 "RRA:MAX:0.5:10080:70", // 7 day max - ony year
867 static const char *rrd_def_storage
[] = {
868 "DS:total:GAUGE:120:0:U",
869 "DS:used:GAUGE:120:0:U",
871 "RRA:AVERAGE:0.5:1:70", // 1 min avg - one hour
872 "RRA:AVERAGE:0.5:30:70", // 30 min avg - one day
873 "RRA:AVERAGE:0.5:180:70", // 3 hour avg - one week
874 "RRA:AVERAGE:0.5:720:70", // 12 hour avg - one month
875 "RRA:AVERAGE:0.5:10080:70", // 7 day avg - ony year
877 "RRA:MAX:0.5:1:70", // 1 min max - one hour
878 "RRA:MAX:0.5:30:70", // 30 min max - one day
879 "RRA:MAX:0.5:180:70", // 3 hour max - one week
880 "RRA:MAX:0.5:720:70", // 12 hour max - one month
881 "RRA:MAX:0.5:10080:70", // 7 day max - ony year
885 #define RRDDIR "/var/lib/rrdcached/db"
889 const char *filename
,
891 const char *rrddef
[])
893 /* start at day boundary */
896 struct tm
*ltm
= localtime(&ctime
);
902 if (rrd_create_r(filename
, 60, timelocal(ltm
), argcount
, rrddef
)) {
903 cfs_message("RRD create error %s: %s", filename
, rrd_get_error());
907 static inline const char *
913 while (*data
&& found
< count
) {
926 g_return_if_fail(key
!= NULL
);
927 g_return_if_fail(data
!= NULL
);
928 g_return_if_fail(len
> 0);
929 g_return_if_fail(len
< 4096);
931 static const char *rrdcsock
= "unix:/var/run/rrdcached.sock";
934 if (rrdc_connect(rrdcsock
) != 0)
937 char *filename
= NULL
;
941 if (strncmp(key
, "pve2-node/", 10) == 0) {
942 const char *node
= key
+ 10;
946 if (strchr(node
, '/') != NULL
)
949 if (strlen(node
) < 1)
952 filename
= g_strdup_printf(RRDDIR
"/%s", key
);
954 if (!g_file_test(filename
, G_FILE_TEST_EXISTS
)) {
956 mkdir(RRDDIR
"/pve2-node", 0755);
957 int argcount
= sizeof(rrd_def_node
)/sizeof(void*) - 1;
958 create_rrd_file(filename
, argcount
, rrd_def_node
);
961 } else if ((strncmp(key
, "pve2-vm/", 8) == 0) ||
962 (strncmp(key
, "pve2.3-vm/", 10) == 0)) {
965 if (strncmp(key
, "pve2-vm/", 8) == 0) {
973 if (strchr(vmid
, '/') != NULL
)
976 if (strlen(vmid
) < 1)
979 filename
= g_strdup_printf(RRDDIR
"/%s/%s", "pve2-vm", vmid
);
981 if (!g_file_test(filename
, G_FILE_TEST_EXISTS
)) {
983 mkdir(RRDDIR
"/pve2-vm", 0755);
984 int argcount
= sizeof(rrd_def_vm
)/sizeof(void*) - 1;
985 create_rrd_file(filename
, argcount
, rrd_def_vm
);
988 } else if (strncmp(key
, "pve2-storage/", 13) == 0) {
989 const char *node
= key
+ 13;
991 const char *storage
= node
;
992 while (*storage
&& *storage
!= '/')
995 if (*storage
!= '/' || ((storage
- node
) < 1))
1000 if (strchr(storage
, '/') != NULL
)
1003 if (strlen(storage
) < 1)
1006 filename
= g_strdup_printf(RRDDIR
"/%s", key
);
1008 if (!g_file_test(filename
, G_FILE_TEST_EXISTS
)) {
1010 mkdir(RRDDIR
"/pve2-storage", 0755);
1012 char *dir
= g_path_get_dirname(filename
);
1016 int argcount
= sizeof(rrd_def_storage
)/sizeof(void*) - 1;
1017 create_rrd_file(filename
, argcount
, rrd_def_storage
);
1024 const char *dp
= skip
? rrd_skip_data(data
, skip
) : data
;
1026 const char *update_args
[] = { dp
, NULL
};
1030 if ((status
= rrdc_update(filename
, 1, update_args
)) != 0) {
1031 cfs_message("RRDC update error %s: %d", filename
, status
);
1034 if (rrd_update_r(filename
, NULL
, 1, update_args
) != 0) {
1035 cfs_message("RRD update error %s: %s", filename
, rrd_get_error());
1041 if (rrd_update_r(filename
, NULL
, 1, update_args
) != 0) {
1042 cfs_message("RRD update error %s: %s", filename
, rrd_get_error());
1053 cfs_critical("RRD update error: unknown/wrong key %s", key
);
1063 rrdentry_t
*entry
= (rrdentry_t
*)value
;
1064 uint32_t ctime
= GPOINTER_TO_UINT(user_data
);
1066 int diff
= ctime
- entry
->time
;
1068 /* remove everything older than 5 minutes */
1071 return (diff
> expire
) ? TRUE
: FALSE
;
1074 static char *rrd_dump_buf
= NULL
;
1075 static time_t rrd_dump_last
= 0;
1078 cfs_rrd_dump(GString
*str
)
1082 g_mutex_lock (&mutex
);
1085 if (rrd_dump_buf
&& (ctime
- rrd_dump_last
) < 2) {
1086 g_string_assign(str
, rrd_dump_buf
);
1087 g_mutex_unlock (&mutex
);
1091 /* remove old data */
1092 g_hash_table_foreach_remove(cfs_status
.rrdhash
, rrd_entry_is_old
,
1093 GUINT_TO_POINTER(ctime
));
1095 g_string_set_size(str
, 0);
1097 GHashTableIter iter
;
1098 gpointer key
, value
;
1100 g_hash_table_iter_init (&iter
, cfs_status
.rrdhash
);
1102 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
1103 rrdentry_t
*entry
= (rrdentry_t
*)value
;
1104 g_string_append(str
, key
);
1105 g_string_append(str
, ":");
1106 g_string_append(str
, entry
->data
);
1107 g_string_append(str
, "\n");
1110 g_string_append_c(str
, 0); // never return undef
1112 rrd_dump_last
= ctime
;
1114 g_free(rrd_dump_buf
);
1115 rrd_dump_buf
= g_strdup(str
->str
);
1117 g_mutex_unlock (&mutex
);
1123 const char *nodename
,
1127 g_return_val_if_fail(iphash
!= NULL
, FALSE
);
1128 g_return_val_if_fail(nodename
!= NULL
, FALSE
);
1129 g_return_val_if_fail(ip
!= NULL
, FALSE
);
1130 g_return_val_if_fail(len
> 0, FALSE
);
1131 g_return_val_if_fail(len
< 256, FALSE
);
1132 g_return_val_if_fail(ip
[len
-1] == 0, FALSE
);
1134 char *oldip
= (char *)g_hash_table_lookup(iphash
, nodename
);
1136 if (!oldip
|| (strcmp(oldip
, ip
) != 0)) {
1137 cfs_status
.clinfo_version
++;
1138 g_hash_table_replace(iphash
, g_strdup(nodename
), g_strdup(ip
));
1146 GHashTable
*rrdhash
,
1151 g_return_val_if_fail(rrdhash
!= NULL
, FALSE
);
1152 g_return_val_if_fail(key
!= NULL
, FALSE
);
1153 g_return_val_if_fail(data
!= NULL
, FALSE
);
1154 g_return_val_if_fail(len
> 0, FALSE
);
1155 g_return_val_if_fail(len
< 4096, FALSE
);
1156 g_return_val_if_fail(data
[len
-1] == 0, FALSE
);
1159 if ((entry
= (rrdentry_t
*)g_hash_table_lookup(rrdhash
, key
))) {
1160 g_free(entry
->data
);
1161 entry
->data
= g_memdup(data
, len
);
1163 entry
->time
= time(NULL
);
1165 rrdentry_t
*entry
= g_new0(rrdentry_t
, 1);
1167 entry
->key
= g_strdup(key
);
1168 entry
->data
= g_memdup(data
, len
);
1170 entry
->time
= time(NULL
);
1172 g_hash_table_replace(rrdhash
, entry
->key
, entry
);
1175 update_rrd_data(key
, data
, len
);
1181 kvstore_send_update_message(
1187 if (!dfsm_is_initialized(dfsm
))
1190 struct iovec iov
[2];
1193 g_strlcpy(name
, key
, sizeof(name
));
1195 iov
[0].iov_base
= &name
;
1196 iov
[0].iov_len
= sizeof(name
);
1198 iov
[1].iov_base
= (char *)data
;
1199 iov
[1].iov_len
= len
;
1201 if (dfsm_send_message(dfsm
, KVSTORE_MESSAGE_UPDATE
, iov
, 2) == CS_OK
)
1207 static clog_entry_t
*
1208 kvstore_parse_log_message(
1212 g_return_val_if_fail(msg
!= NULL
, NULL
);
1214 if (msg_len
< sizeof(clog_entry_t
)) {
1215 cfs_critical("received short log message (%zu < %zu)", msg_len
, sizeof(clog_entry_t
));
1219 clog_entry_t
*entry
= (clog_entry_t
*)msg
;
1221 uint32_t size
= sizeof(clog_entry_t
) + entry
->node_len
+
1222 entry
->ident_len
+ entry
->tag_len
+ entry
->msg_len
;
1224 if (msg_len
!= size
) {
1225 cfs_critical("received log message with wrong size (%zu != %u)", msg_len
, size
);
1231 if (*((char *)msg
+ entry
->node_len
- 1)) {
1232 cfs_critical("unterminated string in log message");
1235 msg
+= entry
->node_len
;
1237 if (*((char *)msg
+ entry
->ident_len
- 1)) {
1238 cfs_critical("unterminated string in log message");
1241 msg
+= entry
->ident_len
;
1243 if (*((char *)msg
+ entry
->tag_len
- 1)) {
1244 cfs_critical("unterminated string in log message");
1247 msg
+= entry
->tag_len
;
1249 if (*((char *)msg
+ entry
->msg_len
- 1)) {
1250 cfs_critical("unterminated string in log message");
1258 kvstore_parse_update_message(
1262 gconstpointer
*data
,
1265 g_return_val_if_fail(msg
!= NULL
, FALSE
);
1266 g_return_val_if_fail(key
!= NULL
, FALSE
);
1267 g_return_val_if_fail(data
!= NULL
, FALSE
);
1268 g_return_val_if_fail(len
!= NULL
, FALSE
);
1270 if (msg_len
< 256) {
1271 cfs_critical("received short kvstore message (%zu < 256)", msg_len
);
1275 /* test if key is null terminated */
1277 for (i
= 0; i
< 256; i
++)
1278 if (((char *)msg
)[i
] == 0)
1285 *len
= msg_len
- 256;
1293 cfs_create_status_msg(
1295 const char *nodename
,
1298 g_return_val_if_fail(str
!= NULL
, -EINVAL
);
1299 g_return_val_if_fail(key
!= NULL
, -EINVAL
);
1303 GHashTable
*kvhash
= NULL
;
1305 g_mutex_lock (&mutex
);
1307 if (!nodename
|| !nodename
[0] || !strcmp(nodename
, cfs
.nodename
)) {
1308 kvhash
= cfs_status
.kvhash
;
1309 } else if (cfs_status
.clinfo
&& cfs_status
.clinfo
->nodes_byname
) {
1310 cfs_clnode_t
*clnode
;
1311 if ((clnode
= g_hash_table_lookup(cfs_status
.clinfo
->nodes_byname
, nodename
)))
1312 kvhash
= clnode
->kvhash
;
1316 if (kvhash
&& (entry
= (kventry_t
*)g_hash_table_lookup(kvhash
, key
))) {
1317 g_string_append_len(str
, entry
->data
, entry
->len
);
1321 g_mutex_unlock (&mutex
);
1332 g_return_val_if_fail(key
!= NULL
, FALSE
);
1333 g_return_val_if_fail(data
!= NULL
, FALSE
);
1334 g_return_val_if_fail(cfs_status
.kvhash
!= NULL
, FALSE
);
1336 if (len
> CFS_MAX_STATUS_SIZE
)
1339 g_mutex_lock (&mutex
);
1343 if (strncmp(key
, "rrd/", 4) == 0) {
1344 res
= rrdentry_hash_set(cfs_status
.rrdhash
, key
+ 4, data
, len
);
1345 } else if (!strcmp(key
, "nodeip")) {
1346 res
= nodeip_hash_set(cfs_status
.iphash
, cfs
.nodename
, data
, len
);
1348 res
= kventry_hash_set(cfs_status
.kvhash
, key
, data
, len
);
1350 g_mutex_unlock (&mutex
);
1352 if (cfs_status
.kvstore
)
1353 kvstore_send_update_message(cfs_status
.kvstore
, key
, data
, len
);
1355 return res
? 0 : -ENOMEM
;
1359 cfs_kvstore_node_set(
1365 g_return_val_if_fail(nodeid
!= 0, FALSE
);
1366 g_return_val_if_fail(key
!= NULL
, FALSE
);
1367 g_return_val_if_fail(data
!= NULL
, FALSE
);
1369 g_mutex_lock (&mutex
);
1371 if (!cfs_status
.clinfo
|| !cfs_status
.clinfo
->nodes_byid
)
1372 goto ret
; /* ignore */
1374 cfs_clnode_t
*clnode
= g_hash_table_lookup(cfs_status
.clinfo
->nodes_byid
, &nodeid
);
1376 goto ret
; /* ignore */
1378 cfs_debug("got node %d status update %s", nodeid
, key
);
1380 if (strncmp(key
, "rrd/", 4) == 0) {
1381 rrdentry_hash_set(cfs_status
.rrdhash
, key
+ 4, data
, len
);
1382 } else if (!strcmp(key
, "nodeip")) {
1383 nodeip_hash_set(cfs_status
.iphash
, clnode
->name
, data
, len
);
1385 if (!clnode
->kvhash
) {
1386 if (!(clnode
->kvhash
= kventry_hash_new())) {
1387 goto ret
; /*ignore */
1391 kventry_hash_set(clnode
->kvhash
, key
, data
, len
);
1395 g_mutex_unlock (&mutex
);
1401 cfs_kvstore_sync(void)
1403 g_return_val_if_fail(cfs_status
.kvhash
!= NULL
, FALSE
);
1404 g_return_val_if_fail(cfs_status
.kvstore
!= NULL
, FALSE
);
1406 gboolean res
= TRUE
;
1408 g_mutex_lock (&mutex
);
1410 GHashTable
*ht
= cfs_status
.kvhash
;
1411 GHashTableIter iter
;
1412 gpointer key
, value
;
1414 g_hash_table_iter_init (&iter
, ht
);
1416 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
1417 kventry_t
*entry
= (kventry_t
*)value
;
1418 kvstore_send_update_message(cfs_status
.kvstore
, entry
->key
, entry
->data
, entry
->len
);
1421 g_mutex_unlock (&mutex
);
1438 g_return_val_if_fail(dfsm
!= NULL
, -1);
1439 g_return_val_if_fail(msg
!= NULL
, -1);
1440 g_return_val_if_fail(res_ptr
!= NULL
, -1);
1442 /* ignore message for ourself */
1443 if (dfsm_nodeid_is_local(dfsm
, nodeid
, pid
))
1446 if (msg_type
== KVSTORE_MESSAGE_UPDATE
) {
1450 if (kvstore_parse_update_message(msg
, msg_len
, &key
, &data
, &len
)) {
1451 cfs_kvstore_node_set(nodeid
, key
, data
, len
);
1453 cfs_critical("cant parse update message");
1455 } else if (msg_type
== KVSTORE_MESSAGE_LOG
) {
1456 cfs_message("received log"); // fixme: remove
1457 const clog_entry_t
*entry
;
1458 if ((entry
= kvstore_parse_log_message(msg
, msg_len
))) {
1459 clusterlog_insert(cfs_status
.clusterlog
, entry
);
1461 cfs_critical("cant parse log message");
1464 cfs_critical("received unknown message type %d\n", msg_type
);
1481 const struct cpg_address
*member_list
,
1482 size_t member_list_entries
)
1484 g_return_if_fail(dfsm
!= NULL
);
1485 g_return_if_fail(member_list
!= NULL
);
1487 cfs_debug("enter %s", __func__
);
1489 g_mutex_lock (&mutex
);
1491 cfs_clinfo_t
*clinfo
= cfs_status
.clinfo
;
1493 if (clinfo
&& clinfo
->nodes_byid
) {
1495 GHashTable
*ht
= clinfo
->nodes_byid
;
1496 GHashTableIter iter
;
1497 gpointer key
, value
;
1499 g_hash_table_iter_init (&iter
, ht
);
1501 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
1502 cfs_clnode_t
*node
= (cfs_clnode_t
*)value
;
1503 node
->online
= FALSE
;
1506 for (int i
= 0; i
< member_list_entries
; i
++) {
1508 if ((node
= g_hash_table_lookup(clinfo
->nodes_byid
, &member_list
[i
].nodeid
))) {
1509 node
->online
= TRUE
;
1513 cfs_status
.clinfo_version
++;
1516 g_mutex_unlock (&mutex
);
1523 unsigned int *res_len
)
1525 g_return_val_if_fail(dfsm
!= NULL
, NULL
);
1527 gpointer msg
= clusterlog_get_state(cfs_status
.clusterlog
, res_len
);
1533 dfsm_process_update(
1536 dfsm_sync_info_t
*syncinfo
,
1542 cfs_critical("%s: received unexpected update message", __func__
);
1548 dfsm_process_state_update(
1551 dfsm_sync_info_t
*syncinfo
)
1553 g_return_val_if_fail(dfsm
!= NULL
, -1);
1554 g_return_val_if_fail(syncinfo
!= NULL
, -1);
1556 clog_base_t
*clog
[syncinfo
->node_count
];
1558 int local_index
= -1;
1559 for (int i
= 0; i
< syncinfo
->node_count
; i
++) {
1560 dfsm_node_info_t
*ni
= &syncinfo
->nodes
[i
];
1563 if (syncinfo
->local
== ni
)
1566 clog_base_t
*base
= (clog_base_t
*)ni
->state
;
1567 if (ni
->state_len
> 8 && ni
->state_len
== clog_size(base
)) {
1568 clog
[i
] = ni
->state
;
1570 cfs_critical("received log with wrong size %u", ni
->state_len
);
1575 if (!clusterlog_merge(cfs_status
.clusterlog
, clog
, syncinfo
->node_count
, local_index
)) {
1576 cfs_critical("unable to merge log files");
1588 dfsm_sync_info_t
*syncinfo
)
1590 g_return_val_if_fail(dfsm
!= NULL
, -1);
1591 g_return_val_if_fail(syncinfo
!= NULL
, -1);
1597 dfsm_synced(dfsm_t
*dfsm
)
1599 g_return_if_fail(dfsm
!= NULL
);
1601 char *ip
= (char *)g_hash_table_lookup(cfs_status
.iphash
, cfs
.nodename
);
1605 cfs_status_set("nodeip", ip
, strlen(ip
) + 1);
1612 dfsm_sync_info_t
*syncinfo
)
1617 static dfsm_callbacks_t kvstore_dfsm_callbacks
= {
1618 .dfsm_deliver_fn
= dfsm_deliver
,
1619 .dfsm_confchg_fn
= dfsm_confchg
,
1621 .dfsm_get_state_fn
= dfsm_get_state
,
1622 .dfsm_process_state_update_fn
= dfsm_process_state_update
,
1623 .dfsm_process_update_fn
= dfsm_process_update
,
1624 .dfsm_commit_fn
= dfsm_commit
,
1625 .dfsm_cleanup_fn
= dfsm_cleanup
,
1626 .dfsm_synced_fn
= dfsm_synced
,
1630 cfs_status_dfsm_new(void)
1632 g_mutex_lock (&mutex
);
1634 cfs_status
.kvstore
= dfsm_new(NULL
, KVSTORE_CPG_GROUP_NAME
, G_LOG_DOMAIN
,
1635 0, &kvstore_dfsm_callbacks
);
1636 g_mutex_unlock (&mutex
);
1638 return cfs_status
.kvstore
;
1642 cfs_is_quorate(void)
1644 g_mutex_lock (&mutex
);
1645 gboolean res
= cfs_status
.quorate
;
1646 g_mutex_unlock (&mutex
);
1656 g_mutex_lock (&mutex
);
1658 uint32_t prev_quorate
= cfs_status
.quorate
;
1659 cfs_status
.quorate
= quorate
;
1661 if (!prev_quorate
&& cfs_status
.quorate
) {
1663 cfs_message("node has quorum");
1666 if (prev_quorate
&& !cfs_status
.quorate
) {
1668 cfs_message("node lost quorum");
1671 g_mutex_unlock (&mutex
);