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" },
100 uint32_t clinfo_version
;
103 uint32_t vmlist_version
;
110 GHashTable
*memdb_changes
;
112 clusterlog_t
*clusterlog
;
115 static cfs_status_t cfs_status
;
127 uint32_t cman_version
;
129 GHashTable
*nodes_byid
;
130 GHashTable
*nodes_byname
;
134 g_int32_hash (gconstpointer v
)
136 return *(const uint32_t *) v
;
140 g_int32_equal (gconstpointer v1
,
143 return *((const uint32_t*) v1
) == *((const uint32_t*) v2
);
146 static void vminfo_free(vminfo_t
*vminfo
)
148 g_return_if_fail(vminfo
!= NULL
);
150 if (vminfo
->nodename
)
151 g_free(vminfo
->nodename
);
157 void cfs_clnode_destroy(
158 cfs_clnode_t
*clnode
)
160 g_return_if_fail(clnode
!= NULL
);
163 g_hash_table_destroy(clnode
->kvhash
);
166 g_free(clnode
->name
);
171 cfs_clnode_t
*cfs_clnode_new(
176 g_return_val_if_fail(name
!= NULL
, NULL
);
178 cfs_clnode_t
*clnode
= g_new0(cfs_clnode_t
, 1);
182 clnode
->name
= g_strdup(name
);
183 clnode
->nodeid
= nodeid
;
184 clnode
->votes
= votes
;
189 gboolean
cfs_clinfo_destroy(
190 cfs_clinfo_t
*clinfo
)
192 g_return_val_if_fail(clinfo
!= NULL
, FALSE
);
194 if (clinfo
->cluster_name
)
195 g_free(clinfo
->cluster_name
);
197 if (clinfo
->nodes_byname
)
198 g_hash_table_destroy(clinfo
->nodes_byname
);
200 if (clinfo
->nodes_byid
)
201 g_hash_table_destroy(clinfo
->nodes_byid
);
208 cfs_clinfo_t
*cfs_clinfo_new(
209 const char *cluster_name
,
210 uint32_t cman_version
)
212 g_return_val_if_fail(cluster_name
!= NULL
, NULL
);
214 cfs_clinfo_t
*clinfo
= g_new0(cfs_clinfo_t
, 1);
218 clinfo
->cluster_name
= g_strdup(cluster_name
);
219 clinfo
->cman_version
= cman_version
;
221 if (!(clinfo
->nodes_byid
= g_hash_table_new_full(
222 g_int32_hash
, g_int32_equal
, NULL
,
223 (GDestroyNotify
)cfs_clnode_destroy
)))
226 if (!(clinfo
->nodes_byname
= g_hash_table_new(g_str_hash
, g_str_equal
)))
232 cfs_clinfo_destroy(clinfo
);
237 gboolean
cfs_clinfo_add_node(
238 cfs_clinfo_t
*clinfo
,
239 cfs_clnode_t
*clnode
)
241 g_return_val_if_fail(clinfo
!= NULL
, FALSE
);
242 g_return_val_if_fail(clnode
!= NULL
, FALSE
);
244 g_hash_table_replace(clinfo
->nodes_byid
, &clnode
->nodeid
, clnode
);
245 g_hash_table_replace(clinfo
->nodes_byname
, clnode
->name
, clnode
);
251 cfs_create_memberlist_msg(
254 g_return_val_if_fail(str
!= NULL
, -EINVAL
);
256 g_mutex_lock (&mutex
);
258 g_string_append_printf(str
,"{\n");
262 cfs_clinfo_t
*clinfo
= cfs_status
.clinfo
;
264 if (clinfo
&& clinfo
->nodes_byid
)
265 nodecount
= g_hash_table_size(clinfo
->nodes_byid
);
268 g_string_append_printf(str
, "\"nodename\": \"%s\",\n", cfs
.nodename
);
269 g_string_append_printf(str
, "\"version\": %u,\n", cfs_status
.clinfo_version
);
271 g_string_append_printf(str
, "\"cluster\": { ");
272 g_string_append_printf(str
, "\"name\": \"%s\", \"version\": %d, "
273 "\"nodes\": %d, \"quorate\": %d ",
274 clinfo
->cluster_name
, clinfo
->cman_version
,
275 nodecount
, cfs_status
.quorate
);
277 g_string_append_printf(str
,"},\n");
278 g_string_append_printf(str
,"\"nodelist\": {\n");
280 GHashTable
*ht
= clinfo
->nodes_byid
;
284 g_hash_table_iter_init (&iter
, ht
);
287 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
288 cfs_clnode_t
*node
= (cfs_clnode_t
*)value
;
289 if (i
) g_string_append_printf(str
, ",\n");
292 g_string_append_printf(str
, " \"%s\": { \"id\": %d, \"online\": %d",
293 node
->name
, node
->nodeid
, node
->online
);
296 char *ip
= (char *)g_hash_table_lookup(cfs_status
.iphash
, node
->name
);
298 g_string_append_printf(str
, ", \"ip\": \"%s\"", ip
);
301 g_string_append_printf(str
, "}");
304 g_string_append_printf(str
,"\n }\n");
306 g_string_append_printf(str
, "\"nodename\": \"%s\",\n", cfs
.nodename
);
307 g_string_append_printf(str
, "\"version\": %u\n", cfs_status
.clinfo_version
);
310 g_string_append_printf(str
,"}\n");
312 g_mutex_unlock (&mutex
);
318 kventry_free(kventry_t
*entry
)
320 g_return_if_fail(entry
!= NULL
);
328 kventry_hash_new(void)
330 return g_hash_table_new_full(g_str_hash
, g_str_equal
, NULL
,
331 (GDestroyNotify
)kventry_free
);
335 rrdentry_free(rrdentry_t
*entry
)
337 g_return_if_fail(entry
!= NULL
);
345 rrdentry_hash_new(void)
347 return g_hash_table_new_full(g_str_hash
, g_str_equal
, NULL
,
348 (GDestroyNotify
)rrdentry_free
);
352 cfs_cluster_log_dump(GString
*str
, const char *user
, guint max_entries
)
354 clusterlog_dump(cfs_status
.clusterlog
, str
, user
, max_entries
);
358 cfs_cluster_log(clog_entry_t
*entry
)
360 g_return_if_fail(entry
!= NULL
);
362 clusterlog_insert(cfs_status
.clusterlog
, entry
);
364 if (cfs_status
.kvstore
) {
366 iov
[0].iov_base
= (char *)entry
;
367 iov
[0].iov_len
= clog_entry_size(entry
);
369 dfsm_send_message(cfs_status
.kvstore
, KVSTORE_MESSAGE_LOG
, iov
, 1);
373 void cfs_status_init(void)
375 g_mutex_lock (&mutex
);
377 cfs_status
.start_time
= time(NULL
);
379 cfs_status
.vmlist
= vmlist_hash_new();
381 cfs_status
.kvhash
= kventry_hash_new();
383 cfs_status
.rrdhash
= rrdentry_hash_new();
385 cfs_status
.iphash
= g_hash_table_new_full(g_str_hash
, g_str_equal
, g_free
, g_free
);
387 cfs_status
.memdb_changes
= g_hash_table_new(g_str_hash
, g_str_equal
);
389 for (int i
= 0; i
< G_N_ELEMENTS(memdb_change_array
); i
++) {
390 g_hash_table_replace(cfs_status
.memdb_changes
,
391 memdb_change_array
[i
].path
,
392 &memdb_change_array
[i
]);
395 cfs_status
.clusterlog
= clusterlog_new();
398 clusterlog_add(cfs_status
.clusterlog
, "root", "cluster", getpid(),
399 LOG_INFO
, "starting cluster log");
401 g_mutex_unlock (&mutex
);
404 void cfs_status_cleanup(void)
406 g_mutex_lock (&mutex
);
408 cfs_status
.clinfo_version
++;
410 if (cfs_status
.clinfo
) {
411 cfs_clinfo_destroy(cfs_status
.clinfo
);
412 cfs_status
.clinfo
= NULL
;
415 if (cfs_status
.vmlist
) {
416 g_hash_table_destroy(cfs_status
.vmlist
);
417 cfs_status
.vmlist
= NULL
;
420 if (cfs_status
.kvhash
) {
421 g_hash_table_destroy(cfs_status
.kvhash
);
422 cfs_status
.kvhash
= NULL
;
425 if (cfs_status
.rrdhash
) {
426 g_hash_table_destroy(cfs_status
.rrdhash
);
427 cfs_status
.rrdhash
= NULL
;
430 if (cfs_status
.iphash
) {
431 g_hash_table_destroy(cfs_status
.iphash
);
432 cfs_status
.iphash
= NULL
;
435 if (cfs_status
.clusterlog
)
436 clusterlog_destroy(cfs_status
.clusterlog
);
438 g_mutex_unlock (&mutex
);
441 void cfs_status_set_clinfo(
442 cfs_clinfo_t
*clinfo
)
444 g_return_if_fail(clinfo
!= NULL
);
446 g_mutex_lock (&mutex
);
448 cfs_status
.clinfo_version
++;
450 cfs_clinfo_t
*old
= cfs_status
.clinfo
;
452 cfs_status
.clinfo
= clinfo
;
454 cfs_message("update cluster info (cluster name %s, version = %d)",
455 clinfo
->cluster_name
, clinfo
->cman_version
);
458 if (old
&& old
->nodes_byid
&& clinfo
->nodes_byid
) {
460 GHashTable
*ht
= clinfo
->nodes_byid
;
464 g_hash_table_iter_init (&iter
, ht
);
466 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
467 cfs_clnode_t
*node
= (cfs_clnode_t
*)value
;
468 cfs_clnode_t
*oldnode
;
469 if ((oldnode
= g_hash_table_lookup(old
->nodes_byid
, key
))) {
470 node
->online
= oldnode
->online
;
471 node
->kvhash
= oldnode
->kvhash
;
472 oldnode
->kvhash
= NULL
;
479 cfs_clinfo_destroy(old
);
482 g_mutex_unlock (&mutex
);
486 dump_kvstore_versions(
489 const char *nodename
)
491 g_return_if_fail(kvhash
!= NULL
);
492 g_return_if_fail(str
!= NULL
);
493 g_return_if_fail(nodename
!= NULL
);
495 GHashTable
*ht
= kvhash
;
499 g_string_append_printf(str
, "\"%s\": {\n", nodename
);
501 g_hash_table_iter_init (&iter
, ht
);
504 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
505 kventry_t
*entry
= (kventry_t
*)value
;
506 if (i
) g_string_append_printf(str
, ",\n");
508 g_string_append_printf(str
,"\"%s\": %u", entry
->key
, entry
->version
);
511 g_string_append_printf(str
, "}\n");
515 cfs_create_version_msg(GString
*str
)
517 g_return_val_if_fail(str
!= NULL
, -EINVAL
);
519 g_mutex_lock (&mutex
);
521 g_string_append_printf(str
,"{\n");
523 g_string_append_printf(str
, "\"starttime\": %lu,\n", (unsigned long)cfs_status
.start_time
);
525 g_string_append_printf(str
, "\"clinfo\": %u,\n", cfs_status
.clinfo_version
);
527 g_string_append_printf(str
, "\"vmlist\": %u,\n", cfs_status
.vmlist_version
);
529 for (int i
= 0; i
< G_N_ELEMENTS(memdb_change_array
); i
++) {
530 g_string_append_printf(str
, "\"%s\": %u,\n",
531 memdb_change_array
[i
].path
,
532 memdb_change_array
[i
].version
);
535 g_string_append_printf(str
, "\"kvstore\": {\n");
537 dump_kvstore_versions(str
, cfs_status
.kvhash
, cfs
.nodename
);
539 cfs_clinfo_t
*clinfo
= cfs_status
.clinfo
;
541 if (clinfo
&& clinfo
->nodes_byid
) {
542 GHashTable
*ht
= clinfo
->nodes_byid
;
546 g_hash_table_iter_init (&iter
, ht
);
548 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
549 cfs_clnode_t
*node
= (cfs_clnode_t
*)value
;
552 g_string_append_printf(str
, ",\n");
553 dump_kvstore_versions(str
, node
->kvhash
, node
->name
);
557 g_string_append_printf(str
,"}\n");
559 g_string_append_printf(str
,"}\n");
561 g_mutex_unlock (&mutex
);
567 vmlist_hash_new(void)
569 return g_hash_table_new_full(g_int_hash
, g_int_equal
, NULL
,
570 (GDestroyNotify
)vminfo_free
);
574 vmlist_hash_insert_vm(
578 const char *nodename
,
581 g_return_val_if_fail(vmlist
!= NULL
, FALSE
);
582 g_return_val_if_fail(nodename
!= NULL
, FALSE
);
583 g_return_val_if_fail(vmid
!= 0, FALSE
);
584 g_return_val_if_fail(vmtype
== VMTYPE_QEMU
|| vmtype
== VMTYPE_OPENVZ
, FALSE
);
586 if (!replace
&& g_hash_table_lookup(vmlist
, &vmid
)) {
587 cfs_critical("detected duplicate VMID %d", vmid
);
591 vminfo_t
*vminfo
= g_new0(vminfo_t
, 1);
594 vminfo
->vmtype
= vmtype
;
595 vminfo
->nodename
= g_strdup(nodename
);
597 vminfo
->version
= ++vminfo_version_counter
;
599 g_hash_table_replace(vmlist
, &vminfo
->vmid
, vminfo
);
608 const char *nodename
)
610 g_return_if_fail(cfs_status
.vmlist
!= NULL
);
611 g_return_if_fail(nodename
!= NULL
);
612 g_return_if_fail(vmid
!= 0);
613 g_return_if_fail(vmtype
== VMTYPE_QEMU
|| vmtype
== VMTYPE_OPENVZ
);
615 cfs_debug("vmlist_register_vm: %s/%u %d", nodename
, vmid
, vmtype
);
617 g_mutex_lock (&mutex
);
619 cfs_status
.vmlist_version
++;
621 vmlist_hash_insert_vm(cfs_status
.vmlist
, vmtype
, vmid
, nodename
, TRUE
);
623 g_mutex_unlock (&mutex
);
627 vmlist_different_vm_exists(
630 const char *nodename
)
632 g_return_val_if_fail(cfs_status
.vmlist
!= NULL
, FALSE
);
633 g_return_val_if_fail(vmid
!= 0, FALSE
);
635 gboolean res
= FALSE
;
637 g_mutex_lock (&mutex
);
640 if ((vminfo
= (vminfo_t
*)g_hash_table_lookup(cfs_status
.vmlist
, &vmid
))) {
641 if (!(vminfo
->vmtype
== vmtype
&& strcmp(vminfo
->nodename
, nodename
) == 0))
644 g_mutex_unlock (&mutex
);
653 g_return_val_if_fail(cfs_status
.vmlist
!= NULL
, FALSE
);
654 g_return_val_if_fail(vmid
!= 0, FALSE
);
656 g_mutex_lock (&mutex
);
658 gpointer res
= g_hash_table_lookup(cfs_status
.vmlist
, &vmid
);
660 g_mutex_unlock (&mutex
);
669 g_return_if_fail(cfs_status
.vmlist
!= NULL
);
670 g_return_if_fail(vmid
!= 0);
672 g_mutex_lock (&mutex
);
674 cfs_status
.vmlist_version
++;
676 g_hash_table_remove(cfs_status
.vmlist
, &vmid
);
678 g_mutex_unlock (&mutex
);
681 void cfs_status_set_vmlist(
684 g_return_if_fail(vmlist
!= NULL
);
686 g_mutex_lock (&mutex
);
688 cfs_status
.vmlist_version
++;
690 if (cfs_status
.vmlist
)
691 g_hash_table_destroy(cfs_status
.vmlist
);
693 cfs_status
.vmlist
= vmlist
;
695 g_mutex_unlock (&mutex
);
699 cfs_create_vmlist_msg(GString
*str
)
701 g_return_val_if_fail(cfs_status
.vmlist
!= NULL
, -EINVAL
);
702 g_return_val_if_fail(str
!= NULL
, -EINVAL
);
704 g_mutex_lock (&mutex
);
706 g_string_append_printf(str
,"{\n");
708 GHashTable
*ht
= cfs_status
.vmlist
;
710 guint count
= g_hash_table_size(ht
);
713 g_string_append_printf(str
,"\"version\": %u\n", cfs_status
.vmlist_version
);
715 g_string_append_printf(str
,"\"version\": %u,\n", cfs_status
.vmlist_version
);
717 g_string_append_printf(str
,"\"ids\": {\n");
722 g_hash_table_iter_init (&iter
, ht
);
725 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
726 vminfo_t
*vminfo
= (vminfo_t
*)value
;
728 if (vminfo
->vmtype
== VMTYPE_QEMU
) {
730 } else if (vminfo
->vmtype
== VMTYPE_OPENVZ
) {
737 g_string_append_printf(str
, ",\n");
740 g_string_append_printf(str
,"\"%u\": { \"node\": \"%s\", \"type\": \"%s\", \"version\": %u }",
741 vminfo
->vmid
, vminfo
->nodename
, type
, vminfo
->version
);
744 g_string_append_printf(str
,"}\n");
746 g_string_append_printf(str
,"\n}\n");
748 g_mutex_unlock (&mutex
);
754 record_memdb_change(const char *path
)
756 g_return_if_fail(cfs_status
.memdb_changes
!= 0);
760 if ((ce
= (memdb_change_t
*)g_hash_table_lookup(cfs_status
.memdb_changes
, path
))) {
766 record_memdb_reload(void)
768 for (int i
= 0; i
< G_N_ELEMENTS(memdb_change_array
); i
++) {
769 memdb_change_array
[i
].version
++;
780 g_return_val_if_fail(kvhash
!= NULL
, FALSE
);
781 g_return_val_if_fail(key
!= NULL
, FALSE
);
782 g_return_val_if_fail(data
!= NULL
, FALSE
);
785 if ((entry
= (kventry_t
*)g_hash_table_lookup(kvhash
, key
))) {
787 entry
->data
= g_memdup(data
, len
);
791 kventry_t
*entry
= g_new0(kventry_t
, 1);
793 entry
->key
= g_strdup(key
);
794 entry
->data
= g_memdup(data
, len
);
797 g_hash_table_replace(kvhash
, entry
->key
, entry
);
803 static const char *rrd_def_node
[] = {
804 "DS:loadavg:GAUGE:120:0:U",
805 "DS:maxcpu:GAUGE:120:0:U",
806 "DS:cpu:GAUGE:120:0:U",
807 "DS:iowait:GAUGE:120:0:U",
808 "DS:memtotal:GAUGE:120:0:U",
809 "DS:memused:GAUGE:120:0:U",
810 "DS:swaptotal:GAUGE:120:0:U",
811 "DS:swapused:GAUGE:120:0:U",
812 "DS:roottotal:GAUGE:120:0:U",
813 "DS:rootused:GAUGE:120:0:U",
814 "DS:netin:DERIVE:120:0:U",
815 "DS:netout:DERIVE:120:0:U",
817 "RRA:AVERAGE:0.5:1:70", // 1 min avg - one hour
818 "RRA:AVERAGE:0.5:30:70", // 30 min avg - one day
819 "RRA:AVERAGE:0.5:180:70", // 3 hour avg - one week
820 "RRA:AVERAGE:0.5:720:70", // 12 hour avg - one month
821 "RRA:AVERAGE:0.5:10080:70", // 7 day avg - ony year
823 "RRA:MAX:0.5:1:70", // 1 min max - one hour
824 "RRA:MAX:0.5:30:70", // 30 min max - one day
825 "RRA:MAX:0.5:180:70", // 3 hour max - one week
826 "RRA:MAX:0.5:720:70", // 12 hour max - one month
827 "RRA:MAX:0.5:10080:70", // 7 day max - ony year
831 static const char *rrd_def_vm
[] = {
832 "DS:maxcpu:GAUGE:120:0:U",
833 "DS:cpu:GAUGE:120:0:U",
834 "DS:maxmem:GAUGE:120:0:U",
835 "DS:mem:GAUGE:120:0:U",
836 "DS:maxdisk:GAUGE:120:0:U",
837 "DS:disk:GAUGE:120:0:U",
838 "DS:netin:DERIVE:120:0:U",
839 "DS:netout:DERIVE:120:0:U",
840 "DS:diskread:DERIVE:120:0:U",
841 "DS:diskwrite:DERIVE:120:0:U",
843 "RRA:AVERAGE:0.5:1:70", // 1 min avg - one hour
844 "RRA:AVERAGE:0.5:30:70", // 30 min avg - one day
845 "RRA:AVERAGE:0.5:180:70", // 3 hour avg - one week
846 "RRA:AVERAGE:0.5:720:70", // 12 hour avg - one month
847 "RRA:AVERAGE:0.5:10080:70", // 7 day avg - ony year
849 "RRA:MAX:0.5:1:70", // 1 min max - one hour
850 "RRA:MAX:0.5:30:70", // 30 min max - one day
851 "RRA:MAX:0.5:180:70", // 3 hour max - one week
852 "RRA:MAX:0.5:720:70", // 12 hour max - one month
853 "RRA:MAX:0.5:10080:70", // 7 day max - ony year
857 static const char *rrd_def_storage
[] = {
858 "DS:total:GAUGE:120:0:U",
859 "DS:used:GAUGE:120:0:U",
861 "RRA:AVERAGE:0.5:1:70", // 1 min avg - one hour
862 "RRA:AVERAGE:0.5:30:70", // 30 min avg - one day
863 "RRA:AVERAGE:0.5:180:70", // 3 hour avg - one week
864 "RRA:AVERAGE:0.5:720:70", // 12 hour avg - one month
865 "RRA:AVERAGE:0.5:10080:70", // 7 day avg - ony year
867 "RRA:MAX:0.5:1:70", // 1 min max - one hour
868 "RRA:MAX:0.5:30:70", // 30 min max - one day
869 "RRA:MAX:0.5:180:70", // 3 hour max - one week
870 "RRA:MAX:0.5:720:70", // 12 hour max - one month
871 "RRA:MAX:0.5:10080:70", // 7 day max - ony year
875 #define RRDDIR "/var/lib/rrdcached/db"
879 const char *filename
,
881 const char *rrddef
[])
883 /* start at day boundary */
886 struct tm
*ltm
= localtime(&ctime
);
892 if (rrd_create_r(filename
, 60, timelocal(ltm
), argcount
, rrddef
)) {
893 cfs_message("RRD create error %s: %s", filename
, rrd_get_error());
897 static inline const char *
903 while (*data
&& found
< count
) {
916 g_return_if_fail(key
!= NULL
);
917 g_return_if_fail(data
!= NULL
);
918 g_return_if_fail(len
> 0);
919 g_return_if_fail(len
< 4096);
921 static const char *rrdcsock
= "unix:/var/run/rrdcached.sock";
924 if (rrdc_connect(rrdcsock
) != 0)
927 char *filename
= NULL
;
931 if (strncmp(key
, "pve2-node/", 10) == 0) {
932 const char *node
= key
+ 10;
936 if (strchr(node
, '/') != NULL
)
939 if (strlen(node
) < 1)
942 filename
= g_strdup_printf(RRDDIR
"/%s", key
);
944 if (!g_file_test(filename
, G_FILE_TEST_EXISTS
)) {
946 mkdir(RRDDIR
"/pve2-node", 0755);
947 int argcount
= sizeof(rrd_def_node
)/sizeof(void*) - 1;
948 create_rrd_file(filename
, argcount
, rrd_def_node
);
951 } else if ((strncmp(key
, "pve2-vm/", 8) == 0) ||
952 (strncmp(key
, "pve2.3-vm/", 10) == 0)) {
955 if (strncmp(key
, "pve2-vm/", 8) == 0) {
963 if (strchr(vmid
, '/') != NULL
)
966 if (strlen(vmid
) < 1)
969 filename
= g_strdup_printf(RRDDIR
"/%s/%s", "pve2-vm", vmid
);
971 if (!g_file_test(filename
, G_FILE_TEST_EXISTS
)) {
973 mkdir(RRDDIR
"/pve2-vm", 0755);
974 int argcount
= sizeof(rrd_def_vm
)/sizeof(void*) - 1;
975 create_rrd_file(filename
, argcount
, rrd_def_vm
);
978 } else if (strncmp(key
, "pve2-storage/", 13) == 0) {
979 const char *node
= key
+ 13;
981 const char *storage
= node
;
982 while (*storage
&& *storage
!= '/')
985 if (*storage
!= '/' || ((storage
- node
) < 1))
990 if (strchr(storage
, '/') != NULL
)
993 if (strlen(storage
) < 1)
996 filename
= g_strdup_printf(RRDDIR
"/%s", key
);
998 if (!g_file_test(filename
, G_FILE_TEST_EXISTS
)) {
1000 mkdir(RRDDIR
"/pve2-storage", 0755);
1002 char *dir
= g_path_get_dirname(filename
);
1006 int argcount
= sizeof(rrd_def_storage
)/sizeof(void*) - 1;
1007 create_rrd_file(filename
, argcount
, rrd_def_storage
);
1014 const char *dp
= skip
? rrd_skip_data(data
, skip
) : data
;
1016 const char *update_args
[] = { dp
, NULL
};
1020 if ((status
= rrdc_update(filename
, 1, update_args
)) != 0) {
1021 cfs_message("RRDC update error %s: %d", filename
, status
);
1024 if (rrd_update_r(filename
, NULL
, 1, update_args
) != 0) {
1025 cfs_message("RRD update error %s: %s", filename
, rrd_get_error());
1031 if (rrd_update_r(filename
, NULL
, 1, update_args
) != 0) {
1032 cfs_message("RRD update error %s: %s", filename
, rrd_get_error());
1043 cfs_critical("RRD update error: unknown/wrong key %s", key
);
1053 rrdentry_t
*entry
= (rrdentry_t
*)value
;
1054 uint32_t ctime
= GPOINTER_TO_UINT(user_data
);
1056 int diff
= ctime
- entry
->time
;
1058 /* remove everything older than 5 minutes */
1061 return (diff
> expire
) ? TRUE
: FALSE
;
1064 static char *rrd_dump_buf
= NULL
;
1065 static time_t rrd_dump_last
= 0;
1068 cfs_rrd_dump(GString
*str
)
1073 if (rrd_dump_buf
&& (ctime
- rrd_dump_last
) < 2) {
1074 g_string_assign(str
, rrd_dump_buf
);
1078 /* remove old data */
1079 g_hash_table_foreach_remove(cfs_status
.rrdhash
, rrd_entry_is_old
,
1080 GUINT_TO_POINTER(ctime
));
1082 g_string_set_size(str
, 0);
1084 GHashTableIter iter
;
1085 gpointer key
, value
;
1087 g_hash_table_iter_init (&iter
, cfs_status
.rrdhash
);
1089 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
1090 rrdentry_t
*entry
= (rrdentry_t
*)value
;
1091 g_string_append(str
, key
);
1092 g_string_append(str
, ":");
1093 g_string_append(str
, entry
->data
);
1094 g_string_append(str
, "\n");
1097 g_string_append_c(str
, 0); // never return undef
1099 rrd_dump_last
= ctime
;
1101 g_free(rrd_dump_buf
);
1102 rrd_dump_buf
= g_strdup(str
->str
);
1108 const char *nodename
,
1112 g_return_val_if_fail(iphash
!= NULL
, FALSE
);
1113 g_return_val_if_fail(nodename
!= NULL
, FALSE
);
1114 g_return_val_if_fail(ip
!= NULL
, FALSE
);
1115 g_return_val_if_fail(len
> 0, FALSE
);
1116 g_return_val_if_fail(len
< 256, FALSE
);
1117 g_return_val_if_fail(ip
[len
-1] == 0, FALSE
);
1119 char *oldip
= (char *)g_hash_table_lookup(iphash
, nodename
);
1121 if (!oldip
|| (strcmp(oldip
, ip
) != 0)) {
1122 cfs_status
.clinfo_version
++;
1123 g_hash_table_replace(iphash
, g_strdup(nodename
), g_strdup(ip
));
1131 GHashTable
*rrdhash
,
1136 g_return_val_if_fail(rrdhash
!= NULL
, FALSE
);
1137 g_return_val_if_fail(key
!= NULL
, FALSE
);
1138 g_return_val_if_fail(data
!= NULL
, FALSE
);
1139 g_return_val_if_fail(len
> 0, FALSE
);
1140 g_return_val_if_fail(len
< 4096, FALSE
);
1141 g_return_val_if_fail(data
[len
-1] == 0, FALSE
);
1144 if ((entry
= (rrdentry_t
*)g_hash_table_lookup(rrdhash
, key
))) {
1145 g_free(entry
->data
);
1146 entry
->data
= g_memdup(data
, len
);
1148 entry
->time
= time(NULL
);
1150 rrdentry_t
*entry
= g_new0(rrdentry_t
, 1);
1152 entry
->key
= g_strdup(key
);
1153 entry
->data
= g_memdup(data
, len
);
1155 entry
->time
= time(NULL
);
1157 g_hash_table_replace(rrdhash
, entry
->key
, entry
);
1160 update_rrd_data(key
, data
, len
);
1166 kvstore_send_update_message(
1173 struct iovec iov
[2];
1176 g_strlcpy(name
, key
, sizeof(name
));
1178 iov
[0].iov_base
= &name
;
1179 iov
[0].iov_len
= sizeof(name
);
1181 iov
[1].iov_base
= (char *)data
;
1182 iov
[1].iov_len
= len
;
1184 if (dfsm_send_message(dfsm
, KVSTORE_MESSAGE_UPDATE
, iov
, 2) == CS_OK
)
1190 static clog_entry_t
*
1191 kvstore_parse_log_message(
1195 g_return_val_if_fail(msg
!= NULL
, NULL
);
1197 if (msg_len
< sizeof(clog_entry_t
)) {
1198 cfs_critical("received short log message (%lu < %lu)", msg_len
, sizeof(clog_entry_t
));
1202 clog_entry_t
*entry
= (clog_entry_t
*)msg
;
1204 uint32_t size
= sizeof(clog_entry_t
) + entry
->node_len
+
1205 entry
->ident_len
+ entry
->tag_len
+ entry
->msg_len
;
1207 if (msg_len
!= size
) {
1208 cfs_critical("received log message with wrong size (%lu != %u)", msg_len
, size
);
1214 if (*((char *)msg
+ entry
->node_len
- 1)) {
1215 cfs_critical("unterminated string in log message");
1218 msg
+= entry
->node_len
;
1220 if (*((char *)msg
+ entry
->ident_len
- 1)) {
1221 cfs_critical("unterminated string in log message");
1224 msg
+= entry
->ident_len
;
1226 if (*((char *)msg
+ entry
->tag_len
- 1)) {
1227 cfs_critical("unterminated string in log message");
1230 msg
+= entry
->tag_len
;
1232 if (*((char *)msg
+ entry
->msg_len
- 1)) {
1233 cfs_critical("unterminated string in log message");
1241 kvstore_parse_update_message(
1245 gconstpointer
*data
,
1248 g_return_val_if_fail(msg
!= NULL
, FALSE
);
1249 g_return_val_if_fail(key
!= NULL
, FALSE
);
1250 g_return_val_if_fail(data
!= NULL
, FALSE
);
1251 g_return_val_if_fail(len
!= NULL
, FALSE
);
1253 if (msg_len
< 256) {
1254 cfs_critical("received short kvstore message (%lu < 256)", msg_len
);
1258 /* test if key is null terminated */
1260 for (i
= 0; i
< 256; i
++)
1261 if (((char *)msg
)[i
] == 0)
1268 *len
= msg_len
- 256;
1276 cfs_create_status_msg(
1278 const char *nodename
,
1281 g_return_val_if_fail(str
!= NULL
, -EINVAL
);
1282 g_return_val_if_fail(key
!= NULL
, -EINVAL
);
1286 GHashTable
*kvhash
= NULL
;
1288 g_mutex_lock (&mutex
);
1290 if (!nodename
|| !nodename
[0] || !strcmp(nodename
, cfs
.nodename
)) {
1291 kvhash
= cfs_status
.kvhash
;
1293 cfs_clnode_t
*clnode
;
1294 if ((clnode
= g_hash_table_lookup(cfs_status
.clinfo
->nodes_byname
, nodename
)))
1295 kvhash
= clnode
->kvhash
;
1299 if (kvhash
&& (entry
= (kventry_t
*)g_hash_table_lookup(kvhash
, key
))) {
1300 g_string_append_len(str
, entry
->data
, entry
->len
);
1304 g_mutex_unlock (&mutex
);
1315 g_return_val_if_fail(key
!= NULL
, FALSE
);
1316 g_return_val_if_fail(data
!= NULL
, FALSE
);
1317 g_return_val_if_fail(cfs_status
.kvhash
!= NULL
, FALSE
);
1319 if (len
> CFS_MAX_STATUS_SIZE
)
1322 g_mutex_lock (&mutex
);
1326 if (strncmp(key
, "rrd/", 4) == 0) {
1327 res
= rrdentry_hash_set(cfs_status
.rrdhash
, key
+ 4, data
, len
);
1328 } else if (!strcmp(key
, "nodeip")) {
1329 res
= nodeip_hash_set(cfs_status
.iphash
, cfs
.nodename
, data
, len
);
1331 res
= kventry_hash_set(cfs_status
.kvhash
, key
, data
, len
);
1333 g_mutex_unlock (&mutex
);
1335 if (cfs_status
.kvstore
)
1336 kvstore_send_update_message(cfs_status
.kvstore
, key
, data
, len
);
1338 return res
? 0 : -ENOMEM
;
1342 cfs_kvstore_node_set(
1348 g_return_val_if_fail(nodeid
!= 0, FALSE
);
1349 g_return_val_if_fail(key
!= NULL
, FALSE
);
1350 g_return_val_if_fail(data
!= NULL
, FALSE
);
1352 g_mutex_lock (&mutex
);
1354 if (!cfs_status
.clinfo
|| !cfs_status
.clinfo
->nodes_byid
)
1355 goto ret
; /* ignore */
1357 cfs_clnode_t
*clnode
= g_hash_table_lookup(cfs_status
.clinfo
->nodes_byid
, &nodeid
);
1359 goto ret
; /* ignore */
1361 cfs_debug("got node %d status update %s", nodeid
, key
);
1363 if (strncmp(key
, "rrd/", 4) == 0) {
1364 rrdentry_hash_set(cfs_status
.rrdhash
, key
+ 4, data
, len
);
1365 } else if (!strcmp(key
, "nodeip")) {
1366 nodeip_hash_set(cfs_status
.iphash
, clnode
->name
, data
, len
);
1368 if (!clnode
->kvhash
) {
1369 if (!(clnode
->kvhash
= kventry_hash_new())) {
1370 goto ret
; /*ignore */
1374 kventry_hash_set(clnode
->kvhash
, key
, data
, len
);
1378 g_mutex_unlock (&mutex
);
1384 cfs_kvstore_sync(void)
1386 g_return_val_if_fail(cfs_status
.kvhash
!= NULL
, FALSE
);
1387 g_return_val_if_fail(cfs_status
.kvstore
!= NULL
, FALSE
);
1389 gboolean res
= TRUE
;
1391 g_mutex_lock (&mutex
);
1393 GHashTable
*ht
= cfs_status
.kvhash
;
1394 GHashTableIter iter
;
1395 gpointer key
, value
;
1397 g_hash_table_iter_init (&iter
, ht
);
1399 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
1400 kventry_t
*entry
= (kventry_t
*)value
;
1401 kvstore_send_update_message(cfs_status
.kvstore
, entry
->key
, entry
->data
, entry
->len
);
1404 g_mutex_unlock (&mutex
);
1421 g_return_val_if_fail(dfsm
!= NULL
, -1);
1422 g_return_val_if_fail(msg
!= NULL
, -1);
1423 g_return_val_if_fail(res_ptr
!= NULL
, -1);
1425 /* ignore message for ourself */
1426 if (dfsm_nodeid_is_local(dfsm
, nodeid
, pid
))
1429 if (msg_type
== KVSTORE_MESSAGE_UPDATE
) {
1433 if (kvstore_parse_update_message(msg
, msg_len
, &key
, &data
, &len
)) {
1434 cfs_kvstore_node_set(nodeid
, key
, data
, len
);
1436 cfs_critical("cant parse update message");
1438 } else if (msg_type
== KVSTORE_MESSAGE_LOG
) {
1439 cfs_message("received log"); // fixme: remove
1440 const clog_entry_t
*entry
;
1441 if ((entry
= kvstore_parse_log_message(msg
, msg_len
))) {
1442 clusterlog_insert(cfs_status
.clusterlog
, entry
);
1444 cfs_critical("cant parse log message");
1447 cfs_critical("received unknown message type %d\n", msg_type
);
1464 const struct cpg_address
*member_list
,
1465 size_t member_list_entries
)
1467 g_return_if_fail(dfsm
!= NULL
);
1468 g_return_if_fail(member_list
!= NULL
);
1470 cfs_debug("enter %s", __func__
);
1472 g_mutex_lock (&mutex
);
1474 cfs_clinfo_t
*clinfo
= cfs_status
.clinfo
;
1476 if (clinfo
&& clinfo
->nodes_byid
) {
1478 GHashTable
*ht
= clinfo
->nodes_byid
;
1479 GHashTableIter iter
;
1480 gpointer key
, value
;
1482 g_hash_table_iter_init (&iter
, ht
);
1484 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
1485 cfs_clnode_t
*node
= (cfs_clnode_t
*)value
;
1486 node
->online
= FALSE
;
1489 for (int i
= 0; i
< member_list_entries
; i
++) {
1491 if ((node
= g_hash_table_lookup(clinfo
->nodes_byid
, &member_list
[i
].nodeid
))) {
1492 node
->online
= TRUE
;
1496 cfs_status
.clinfo_version
++;
1499 g_mutex_unlock (&mutex
);
1506 unsigned int *res_len
)
1508 g_return_val_if_fail(dfsm
!= NULL
, NULL
);
1510 gpointer msg
= clusterlog_get_state(cfs_status
.clusterlog
, res_len
);
1516 dfsm_process_update(
1519 dfsm_sync_info_t
*syncinfo
,
1525 cfs_critical("%s: received unexpected update message", __func__
);
1531 dfsm_process_state_update(
1534 dfsm_sync_info_t
*syncinfo
)
1536 g_return_val_if_fail(dfsm
!= NULL
, -1);
1537 g_return_val_if_fail(syncinfo
!= NULL
, -1);
1539 clog_base_t
*clog
[syncinfo
->node_count
];
1541 int local_index
= -1;
1542 for (int i
= 0; i
< syncinfo
->node_count
; i
++) {
1543 dfsm_node_info_t
*ni
= &syncinfo
->nodes
[i
];
1546 if (syncinfo
->local
== ni
)
1549 clog_base_t
*base
= (clog_base_t
*)ni
->state
;
1550 if (ni
->state_len
> 8 && ni
->state_len
== clog_size(base
)) {
1551 clog
[i
] = ni
->state
;
1553 cfs_critical("received log with wrong size %u", ni
->state_len
);
1558 if (!clusterlog_merge(cfs_status
.clusterlog
, clog
, syncinfo
->node_count
, local_index
)) {
1559 cfs_critical("unable to merge log files");
1571 dfsm_sync_info_t
*syncinfo
)
1573 g_return_val_if_fail(dfsm
!= NULL
, -1);
1574 g_return_val_if_fail(syncinfo
!= NULL
, -1);
1580 dfsm_synced(dfsm_t
*dfsm
)
1582 g_return_if_fail(dfsm
!= NULL
);
1584 char *ip
= (char *)g_hash_table_lookup(cfs_status
.iphash
, cfs
.nodename
);
1588 cfs_status_set("nodeip", ip
, strlen(ip
) + 1);
1595 dfsm_sync_info_t
*syncinfo
)
1600 static dfsm_callbacks_t kvstore_dfsm_callbacks
= {
1601 .dfsm_deliver_fn
= dfsm_deliver
,
1602 .dfsm_confchg_fn
= dfsm_confchg
,
1604 .dfsm_get_state_fn
= dfsm_get_state
,
1605 .dfsm_process_state_update_fn
= dfsm_process_state_update
,
1606 .dfsm_process_update_fn
= dfsm_process_update
,
1607 .dfsm_commit_fn
= dfsm_commit
,
1608 .dfsm_cleanup_fn
= dfsm_cleanup
,
1609 .dfsm_synced_fn
= dfsm_synced
,
1613 cfs_status_dfsm_new(void)
1615 g_mutex_lock (&mutex
);
1617 cfs_status
.kvstore
= dfsm_new(NULL
, KVSTORE_CPG_GROUP_NAME
, G_LOG_DOMAIN
,
1618 0, &kvstore_dfsm_callbacks
);
1619 g_mutex_unlock (&mutex
);
1621 return cfs_status
.kvstore
;
1625 cfs_is_quorate(void)
1627 g_mutex_lock (&mutex
);
1628 gboolean res
= cfs_status
.quorate
;
1629 g_mutex_unlock (&mutex
);
1639 g_mutex_lock (&mutex
);
1641 uint32_t prev_quorate
= cfs_status
.quorate
;
1642 cfs_status
.quorate
= quorate
;
1644 if (!prev_quorate
&& cfs_status
.quorate
) {
1646 cfs_message("node has quorum");
1649 if (prev_quorate
&& !cfs_status
.quorate
) {
1651 cfs_message("node lost quorum");
1654 g_mutex_unlock (&mutex
);