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
= "status.cfg" },
100 cfs_clinfo_t
*clinfo
;
101 uint32_t clinfo_version
;
104 uint32_t vmlist_version
;
111 GHashTable
*memdb_changes
;
113 clusterlog_t
*clusterlog
;
116 static cfs_status_t cfs_status
;
128 uint32_t cman_version
;
130 GHashTable
*nodes_byid
;
131 GHashTable
*nodes_byname
;
135 g_int32_hash (gconstpointer v
)
137 return *(const uint32_t *) v
;
141 g_int32_equal (gconstpointer v1
,
144 return *((const uint32_t*) v1
) == *((const uint32_t*) v2
);
147 static void vminfo_free(vminfo_t
*vminfo
)
149 g_return_if_fail(vminfo
!= NULL
);
151 if (vminfo
->nodename
)
152 g_free(vminfo
->nodename
);
158 void cfs_clnode_destroy(
159 cfs_clnode_t
*clnode
)
161 g_return_if_fail(clnode
!= NULL
);
164 g_hash_table_destroy(clnode
->kvhash
);
167 g_free(clnode
->name
);
172 cfs_clnode_t
*cfs_clnode_new(
177 g_return_val_if_fail(name
!= NULL
, NULL
);
179 cfs_clnode_t
*clnode
= g_new0(cfs_clnode_t
, 1);
183 clnode
->name
= g_strdup(name
);
184 clnode
->nodeid
= nodeid
;
185 clnode
->votes
= votes
;
190 gboolean
cfs_clinfo_destroy(
191 cfs_clinfo_t
*clinfo
)
193 g_return_val_if_fail(clinfo
!= NULL
, FALSE
);
195 if (clinfo
->cluster_name
)
196 g_free(clinfo
->cluster_name
);
198 if (clinfo
->nodes_byname
)
199 g_hash_table_destroy(clinfo
->nodes_byname
);
201 if (clinfo
->nodes_byid
)
202 g_hash_table_destroy(clinfo
->nodes_byid
);
209 cfs_clinfo_t
*cfs_clinfo_new(
210 const char *cluster_name
,
211 uint32_t cman_version
)
213 g_return_val_if_fail(cluster_name
!= NULL
, NULL
);
215 cfs_clinfo_t
*clinfo
= g_new0(cfs_clinfo_t
, 1);
219 clinfo
->cluster_name
= g_strdup(cluster_name
);
220 clinfo
->cman_version
= cman_version
;
222 if (!(clinfo
->nodes_byid
= g_hash_table_new_full(
223 g_int32_hash
, g_int32_equal
, NULL
,
224 (GDestroyNotify
)cfs_clnode_destroy
)))
227 if (!(clinfo
->nodes_byname
= g_hash_table_new(g_str_hash
, g_str_equal
)))
233 cfs_clinfo_destroy(clinfo
);
238 gboolean
cfs_clinfo_add_node(
239 cfs_clinfo_t
*clinfo
,
240 cfs_clnode_t
*clnode
)
242 g_return_val_if_fail(clinfo
!= NULL
, FALSE
);
243 g_return_val_if_fail(clnode
!= NULL
, FALSE
);
245 g_hash_table_replace(clinfo
->nodes_byid
, &clnode
->nodeid
, clnode
);
246 g_hash_table_replace(clinfo
->nodes_byname
, clnode
->name
, clnode
);
252 cfs_create_memberlist_msg(
255 g_return_val_if_fail(str
!= NULL
, -EINVAL
);
257 g_mutex_lock (&mutex
);
259 g_string_append_printf(str
,"{\n");
263 cfs_clinfo_t
*clinfo
= cfs_status
.clinfo
;
265 if (clinfo
&& clinfo
->nodes_byid
)
266 nodecount
= g_hash_table_size(clinfo
->nodes_byid
);
269 g_string_append_printf(str
, "\"nodename\": \"%s\",\n", cfs
.nodename
);
270 g_string_append_printf(str
, "\"version\": %u,\n", cfs_status
.clinfo_version
);
272 g_string_append_printf(str
, "\"cluster\": { ");
273 g_string_append_printf(str
, "\"name\": \"%s\", \"version\": %d, "
274 "\"nodes\": %d, \"quorate\": %d ",
275 clinfo
->cluster_name
, clinfo
->cman_version
,
276 nodecount
, cfs_status
.quorate
);
278 g_string_append_printf(str
,"},\n");
279 g_string_append_printf(str
,"\"nodelist\": {\n");
281 GHashTable
*ht
= clinfo
->nodes_byid
;
285 g_hash_table_iter_init (&iter
, ht
);
288 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
289 cfs_clnode_t
*node
= (cfs_clnode_t
*)value
;
290 if (i
) g_string_append_printf(str
, ",\n");
293 g_string_append_printf(str
, " \"%s\": { \"id\": %d, \"online\": %d",
294 node
->name
, node
->nodeid
, node
->online
);
297 char *ip
= (char *)g_hash_table_lookup(cfs_status
.iphash
, node
->name
);
299 g_string_append_printf(str
, ", \"ip\": \"%s\"", ip
);
302 g_string_append_printf(str
, "}");
305 g_string_append_printf(str
,"\n }\n");
307 g_string_append_printf(str
, "\"nodename\": \"%s\",\n", cfs
.nodename
);
308 g_string_append_printf(str
, "\"version\": %u\n", cfs_status
.clinfo_version
);
311 g_string_append_printf(str
,"}\n");
313 g_mutex_unlock (&mutex
);
319 kventry_free(kventry_t
*entry
)
321 g_return_if_fail(entry
!= NULL
);
329 kventry_hash_new(void)
331 return g_hash_table_new_full(g_str_hash
, g_str_equal
, NULL
,
332 (GDestroyNotify
)kventry_free
);
336 rrdentry_free(rrdentry_t
*entry
)
338 g_return_if_fail(entry
!= NULL
);
346 rrdentry_hash_new(void)
348 return g_hash_table_new_full(g_str_hash
, g_str_equal
, NULL
,
349 (GDestroyNotify
)rrdentry_free
);
353 cfs_cluster_log_dump(GString
*str
, const char *user
, guint max_entries
)
355 clusterlog_dump(cfs_status
.clusterlog
, str
, user
, max_entries
);
359 cfs_cluster_log(clog_entry_t
*entry
)
361 g_return_if_fail(entry
!= NULL
);
363 clusterlog_insert(cfs_status
.clusterlog
, entry
);
365 if (cfs_status
.kvstore
) {
367 iov
[0].iov_base
= (char *)entry
;
368 iov
[0].iov_len
= clog_entry_size(entry
);
370 dfsm_send_message(cfs_status
.kvstore
, KVSTORE_MESSAGE_LOG
, iov
, 1);
374 void cfs_status_init(void)
376 g_mutex_lock (&mutex
);
378 cfs_status
.start_time
= time(NULL
);
380 cfs_status
.vmlist
= vmlist_hash_new();
382 cfs_status
.kvhash
= kventry_hash_new();
384 cfs_status
.rrdhash
= rrdentry_hash_new();
386 cfs_status
.iphash
= g_hash_table_new_full(g_str_hash
, g_str_equal
, g_free
, g_free
);
388 cfs_status
.memdb_changes
= g_hash_table_new(g_str_hash
, g_str_equal
);
390 for (int i
= 0; i
< G_N_ELEMENTS(memdb_change_array
); i
++) {
391 g_hash_table_replace(cfs_status
.memdb_changes
,
392 memdb_change_array
[i
].path
,
393 &memdb_change_array
[i
]);
396 cfs_status
.clusterlog
= clusterlog_new();
399 clusterlog_add(cfs_status
.clusterlog
, "root", "cluster", getpid(),
400 LOG_INFO
, "starting cluster log");
402 g_mutex_unlock (&mutex
);
405 void cfs_status_cleanup(void)
407 g_mutex_lock (&mutex
);
409 cfs_status
.clinfo_version
++;
411 if (cfs_status
.clinfo
) {
412 cfs_clinfo_destroy(cfs_status
.clinfo
);
413 cfs_status
.clinfo
= NULL
;
416 if (cfs_status
.vmlist
) {
417 g_hash_table_destroy(cfs_status
.vmlist
);
418 cfs_status
.vmlist
= NULL
;
421 if (cfs_status
.kvhash
) {
422 g_hash_table_destroy(cfs_status
.kvhash
);
423 cfs_status
.kvhash
= NULL
;
426 if (cfs_status
.rrdhash
) {
427 g_hash_table_destroy(cfs_status
.rrdhash
);
428 cfs_status
.rrdhash
= NULL
;
431 if (cfs_status
.iphash
) {
432 g_hash_table_destroy(cfs_status
.iphash
);
433 cfs_status
.iphash
= NULL
;
436 if (cfs_status
.clusterlog
)
437 clusterlog_destroy(cfs_status
.clusterlog
);
439 g_mutex_unlock (&mutex
);
442 void cfs_status_set_clinfo(
443 cfs_clinfo_t
*clinfo
)
445 g_return_if_fail(clinfo
!= NULL
);
447 g_mutex_lock (&mutex
);
449 cfs_status
.clinfo_version
++;
451 cfs_clinfo_t
*old
= cfs_status
.clinfo
;
453 cfs_status
.clinfo
= clinfo
;
455 cfs_message("update cluster info (cluster name %s, version = %d)",
456 clinfo
->cluster_name
, clinfo
->cman_version
);
459 if (old
&& old
->nodes_byid
&& clinfo
->nodes_byid
) {
461 GHashTable
*ht
= clinfo
->nodes_byid
;
465 g_hash_table_iter_init (&iter
, ht
);
467 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
468 cfs_clnode_t
*node
= (cfs_clnode_t
*)value
;
469 cfs_clnode_t
*oldnode
;
470 if ((oldnode
= g_hash_table_lookup(old
->nodes_byid
, key
))) {
471 node
->online
= oldnode
->online
;
472 node
->kvhash
= oldnode
->kvhash
;
473 oldnode
->kvhash
= NULL
;
480 cfs_clinfo_destroy(old
);
483 g_mutex_unlock (&mutex
);
487 dump_kvstore_versions(
490 const char *nodename
)
492 g_return_if_fail(kvhash
!= NULL
);
493 g_return_if_fail(str
!= NULL
);
494 g_return_if_fail(nodename
!= NULL
);
496 GHashTable
*ht
= kvhash
;
500 g_string_append_printf(str
, "\"%s\": {\n", nodename
);
502 g_hash_table_iter_init (&iter
, ht
);
505 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
506 kventry_t
*entry
= (kventry_t
*)value
;
507 if (i
) g_string_append_printf(str
, ",\n");
509 g_string_append_printf(str
,"\"%s\": %u", entry
->key
, entry
->version
);
512 g_string_append_printf(str
, "}\n");
516 cfs_create_version_msg(GString
*str
)
518 g_return_val_if_fail(str
!= NULL
, -EINVAL
);
520 g_mutex_lock (&mutex
);
522 g_string_append_printf(str
,"{\n");
524 g_string_append_printf(str
, "\"starttime\": %lu,\n", (unsigned long)cfs_status
.start_time
);
526 g_string_append_printf(str
, "\"clinfo\": %u,\n", cfs_status
.clinfo_version
);
528 g_string_append_printf(str
, "\"vmlist\": %u,\n", cfs_status
.vmlist_version
);
530 for (int i
= 0; i
< G_N_ELEMENTS(memdb_change_array
); i
++) {
531 g_string_append_printf(str
, "\"%s\": %u,\n",
532 memdb_change_array
[i
].path
,
533 memdb_change_array
[i
].version
);
536 g_string_append_printf(str
, "\"kvstore\": {\n");
538 dump_kvstore_versions(str
, cfs_status
.kvhash
, cfs
.nodename
);
540 cfs_clinfo_t
*clinfo
= cfs_status
.clinfo
;
542 if (clinfo
&& clinfo
->nodes_byid
) {
543 GHashTable
*ht
= clinfo
->nodes_byid
;
547 g_hash_table_iter_init (&iter
, ht
);
549 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
550 cfs_clnode_t
*node
= (cfs_clnode_t
*)value
;
553 g_string_append_printf(str
, ",\n");
554 dump_kvstore_versions(str
, node
->kvhash
, node
->name
);
558 g_string_append_printf(str
,"}\n");
560 g_string_append_printf(str
,"}\n");
562 g_mutex_unlock (&mutex
);
568 vmlist_hash_new(void)
570 return g_hash_table_new_full(g_int_hash
, g_int_equal
, NULL
,
571 (GDestroyNotify
)vminfo_free
);
575 vmlist_hash_insert_vm(
579 const char *nodename
,
582 g_return_val_if_fail(vmlist
!= NULL
, FALSE
);
583 g_return_val_if_fail(nodename
!= NULL
, FALSE
);
584 g_return_val_if_fail(vmid
!= 0, FALSE
);
585 g_return_val_if_fail(vmtype
== VMTYPE_QEMU
|| vmtype
== VMTYPE_OPENVZ
||
586 vmtype
== VMTYPE_LXC
, FALSE
);
588 if (!replace
&& g_hash_table_lookup(vmlist
, &vmid
)) {
589 cfs_critical("detected duplicate VMID %d", vmid
);
593 vminfo_t
*vminfo
= g_new0(vminfo_t
, 1);
596 vminfo
->vmtype
= vmtype
;
597 vminfo
->nodename
= g_strdup(nodename
);
599 vminfo
->version
= ++vminfo_version_counter
;
601 g_hash_table_replace(vmlist
, &vminfo
->vmid
, vminfo
);
610 const char *nodename
)
612 g_return_if_fail(cfs_status
.vmlist
!= NULL
);
613 g_return_if_fail(nodename
!= NULL
);
614 g_return_if_fail(vmid
!= 0);
615 g_return_if_fail(vmtype
== VMTYPE_QEMU
|| vmtype
== VMTYPE_OPENVZ
||
616 vmtype
== VMTYPE_LXC
);
618 cfs_debug("vmlist_register_vm: %s/%u %d", nodename
, vmid
, vmtype
);
620 g_mutex_lock (&mutex
);
622 cfs_status
.vmlist_version
++;
624 vmlist_hash_insert_vm(cfs_status
.vmlist
, vmtype
, vmid
, nodename
, TRUE
);
626 g_mutex_unlock (&mutex
);
630 vmlist_different_vm_exists(
633 const char *nodename
)
635 g_return_val_if_fail(cfs_status
.vmlist
!= NULL
, FALSE
);
636 g_return_val_if_fail(vmid
!= 0, FALSE
);
638 gboolean res
= FALSE
;
640 g_mutex_lock (&mutex
);
643 if ((vminfo
= (vminfo_t
*)g_hash_table_lookup(cfs_status
.vmlist
, &vmid
))) {
644 if (!(vminfo
->vmtype
== vmtype
&& strcmp(vminfo
->nodename
, nodename
) == 0))
647 g_mutex_unlock (&mutex
);
656 g_return_val_if_fail(cfs_status
.vmlist
!= NULL
, FALSE
);
657 g_return_val_if_fail(vmid
!= 0, FALSE
);
659 g_mutex_lock (&mutex
);
661 gpointer res
= g_hash_table_lookup(cfs_status
.vmlist
, &vmid
);
663 g_mutex_unlock (&mutex
);
672 g_return_if_fail(cfs_status
.vmlist
!= NULL
);
673 g_return_if_fail(vmid
!= 0);
675 g_mutex_lock (&mutex
);
677 cfs_status
.vmlist_version
++;
679 g_hash_table_remove(cfs_status
.vmlist
, &vmid
);
681 g_mutex_unlock (&mutex
);
684 void cfs_status_set_vmlist(
687 g_return_if_fail(vmlist
!= NULL
);
689 g_mutex_lock (&mutex
);
691 cfs_status
.vmlist_version
++;
693 if (cfs_status
.vmlist
)
694 g_hash_table_destroy(cfs_status
.vmlist
);
696 cfs_status
.vmlist
= vmlist
;
698 g_mutex_unlock (&mutex
);
702 cfs_create_vmlist_msg(GString
*str
)
704 g_return_val_if_fail(cfs_status
.vmlist
!= NULL
, -EINVAL
);
705 g_return_val_if_fail(str
!= NULL
, -EINVAL
);
707 g_mutex_lock (&mutex
);
709 g_string_append_printf(str
,"{\n");
711 GHashTable
*ht
= cfs_status
.vmlist
;
713 guint count
= g_hash_table_size(ht
);
716 g_string_append_printf(str
,"\"version\": %u\n", cfs_status
.vmlist_version
);
718 g_string_append_printf(str
,"\"version\": %u,\n", cfs_status
.vmlist_version
);
720 g_string_append_printf(str
,"\"ids\": {\n");
725 g_hash_table_iter_init (&iter
, ht
);
728 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
729 vminfo_t
*vminfo
= (vminfo_t
*)value
;
731 if (vminfo
->vmtype
== VMTYPE_QEMU
) {
733 } else if (vminfo
->vmtype
== VMTYPE_OPENVZ
) {
735 } else if (vminfo
->vmtype
== VMTYPE_LXC
) {
742 g_string_append_printf(str
, ",\n");
745 g_string_append_printf(str
,"\"%u\": { \"node\": \"%s\", \"type\": \"%s\", \"version\": %u }",
746 vminfo
->vmid
, vminfo
->nodename
, type
, vminfo
->version
);
749 g_string_append_printf(str
,"}\n");
751 g_string_append_printf(str
,"\n}\n");
753 g_mutex_unlock (&mutex
);
759 record_memdb_change(const char *path
)
761 g_return_if_fail(cfs_status
.memdb_changes
!= 0);
765 if ((ce
= (memdb_change_t
*)g_hash_table_lookup(cfs_status
.memdb_changes
, path
))) {
771 record_memdb_reload(void)
773 for (int i
= 0; i
< G_N_ELEMENTS(memdb_change_array
); i
++) {
774 memdb_change_array
[i
].version
++;
785 g_return_val_if_fail(kvhash
!= NULL
, FALSE
);
786 g_return_val_if_fail(key
!= NULL
, FALSE
);
787 g_return_val_if_fail(data
!= NULL
, FALSE
);
790 if ((entry
= (kventry_t
*)g_hash_table_lookup(kvhash
, key
))) {
792 entry
->data
= g_memdup(data
, len
);
796 kventry_t
*entry
= g_new0(kventry_t
, 1);
798 entry
->key
= g_strdup(key
);
799 entry
->data
= g_memdup(data
, len
);
802 g_hash_table_replace(kvhash
, entry
->key
, entry
);
808 static const char *rrd_def_node
[] = {
809 "DS:loadavg:GAUGE:120:0:U",
810 "DS:maxcpu:GAUGE:120:0:U",
811 "DS:cpu:GAUGE:120:0:U",
812 "DS:iowait:GAUGE:120:0:U",
813 "DS:memtotal:GAUGE:120:0:U",
814 "DS:memused:GAUGE:120:0:U",
815 "DS:swaptotal:GAUGE:120:0:U",
816 "DS:swapused:GAUGE:120:0:U",
817 "DS:roottotal:GAUGE:120:0:U",
818 "DS:rootused:GAUGE:120:0:U",
819 "DS:netin:DERIVE:120:0:U",
820 "DS:netout:DERIVE:120:0:U",
822 "RRA:AVERAGE:0.5:1:70", // 1 min avg - one hour
823 "RRA:AVERAGE:0.5:30:70", // 30 min avg - one day
824 "RRA:AVERAGE:0.5:180:70", // 3 hour avg - one week
825 "RRA:AVERAGE:0.5:720:70", // 12 hour avg - one month
826 "RRA:AVERAGE:0.5:10080:70", // 7 day avg - ony year
828 "RRA:MAX:0.5:1:70", // 1 min max - one hour
829 "RRA:MAX:0.5:30:70", // 30 min max - one day
830 "RRA:MAX:0.5:180:70", // 3 hour max - one week
831 "RRA:MAX:0.5:720:70", // 12 hour max - one month
832 "RRA:MAX:0.5:10080:70", // 7 day max - ony year
836 static const char *rrd_def_vm
[] = {
837 "DS:maxcpu:GAUGE:120:0:U",
838 "DS:cpu:GAUGE:120:0:U",
839 "DS:maxmem:GAUGE:120:0:U",
840 "DS:mem:GAUGE:120:0:U",
841 "DS:maxdisk:GAUGE:120:0:U",
842 "DS:disk:GAUGE:120:0:U",
843 "DS:netin:DERIVE:120:0:U",
844 "DS:netout:DERIVE:120:0:U",
845 "DS:diskread:DERIVE:120:0:U",
846 "DS:diskwrite:DERIVE:120:0:U",
848 "RRA:AVERAGE:0.5:1:70", // 1 min avg - one hour
849 "RRA:AVERAGE:0.5:30:70", // 30 min avg - one day
850 "RRA:AVERAGE:0.5:180:70", // 3 hour avg - one week
851 "RRA:AVERAGE:0.5:720:70", // 12 hour avg - one month
852 "RRA:AVERAGE:0.5:10080:70", // 7 day avg - ony year
854 "RRA:MAX:0.5:1:70", // 1 min max - one hour
855 "RRA:MAX:0.5:30:70", // 30 min max - one day
856 "RRA:MAX:0.5:180:70", // 3 hour max - one week
857 "RRA:MAX:0.5:720:70", // 12 hour max - one month
858 "RRA:MAX:0.5:10080:70", // 7 day max - ony year
862 static const char *rrd_def_storage
[] = {
863 "DS:total:GAUGE:120:0:U",
864 "DS:used:GAUGE:120:0:U",
866 "RRA:AVERAGE:0.5:1:70", // 1 min avg - one hour
867 "RRA:AVERAGE:0.5:30:70", // 30 min avg - one day
868 "RRA:AVERAGE:0.5:180:70", // 3 hour avg - one week
869 "RRA:AVERAGE:0.5:720:70", // 12 hour avg - one month
870 "RRA:AVERAGE:0.5:10080:70", // 7 day avg - ony year
872 "RRA:MAX:0.5:1:70", // 1 min max - one hour
873 "RRA:MAX:0.5:30:70", // 30 min max - one day
874 "RRA:MAX:0.5:180:70", // 3 hour max - one week
875 "RRA:MAX:0.5:720:70", // 12 hour max - one month
876 "RRA:MAX:0.5:10080:70", // 7 day max - ony year
880 #define RRDDIR "/var/lib/rrdcached/db"
884 const char *filename
,
886 const char *rrddef
[])
888 /* start at day boundary */
891 struct tm
*ltm
= localtime(&ctime
);
897 if (rrd_create_r(filename
, 60, timelocal(ltm
), argcount
, rrddef
)) {
898 cfs_message("RRD create error %s: %s", filename
, rrd_get_error());
902 static inline const char *
908 while (*data
&& found
< count
) {
921 g_return_if_fail(key
!= NULL
);
922 g_return_if_fail(data
!= NULL
);
923 g_return_if_fail(len
> 0);
924 g_return_if_fail(len
< 4096);
926 static const char *rrdcsock
= "unix:/var/run/rrdcached.sock";
929 if (rrdc_connect(rrdcsock
) != 0)
932 char *filename
= NULL
;
936 if (strncmp(key
, "pve2-node/", 10) == 0) {
937 const char *node
= key
+ 10;
941 if (strchr(node
, '/') != NULL
)
944 if (strlen(node
) < 1)
947 filename
= g_strdup_printf(RRDDIR
"/%s", key
);
949 if (!g_file_test(filename
, G_FILE_TEST_EXISTS
)) {
951 mkdir(RRDDIR
"/pve2-node", 0755);
952 int argcount
= sizeof(rrd_def_node
)/sizeof(void*) - 1;
953 create_rrd_file(filename
, argcount
, rrd_def_node
);
956 } else if ((strncmp(key
, "pve2-vm/", 8) == 0) ||
957 (strncmp(key
, "pve2.3-vm/", 10) == 0)) {
960 if (strncmp(key
, "pve2-vm/", 8) == 0) {
968 if (strchr(vmid
, '/') != NULL
)
971 if (strlen(vmid
) < 1)
974 filename
= g_strdup_printf(RRDDIR
"/%s/%s", "pve2-vm", vmid
);
976 if (!g_file_test(filename
, G_FILE_TEST_EXISTS
)) {
978 mkdir(RRDDIR
"/pve2-vm", 0755);
979 int argcount
= sizeof(rrd_def_vm
)/sizeof(void*) - 1;
980 create_rrd_file(filename
, argcount
, rrd_def_vm
);
983 } else if (strncmp(key
, "pve2-storage/", 13) == 0) {
984 const char *node
= key
+ 13;
986 const char *storage
= node
;
987 while (*storage
&& *storage
!= '/')
990 if (*storage
!= '/' || ((storage
- node
) < 1))
995 if (strchr(storage
, '/') != NULL
)
998 if (strlen(storage
) < 1)
1001 filename
= g_strdup_printf(RRDDIR
"/%s", key
);
1003 if (!g_file_test(filename
, G_FILE_TEST_EXISTS
)) {
1005 mkdir(RRDDIR
"/pve2-storage", 0755);
1007 char *dir
= g_path_get_dirname(filename
);
1011 int argcount
= sizeof(rrd_def_storage
)/sizeof(void*) - 1;
1012 create_rrd_file(filename
, argcount
, rrd_def_storage
);
1019 const char *dp
= skip
? rrd_skip_data(data
, skip
) : data
;
1021 const char *update_args
[] = { dp
, NULL
};
1025 if ((status
= rrdc_update(filename
, 1, update_args
)) != 0) {
1026 cfs_message("RRDC update error %s: %d", filename
, status
);
1029 if (rrd_update_r(filename
, NULL
, 1, update_args
) != 0) {
1030 cfs_message("RRD update error %s: %s", filename
, rrd_get_error());
1036 if (rrd_update_r(filename
, NULL
, 1, update_args
) != 0) {
1037 cfs_message("RRD update error %s: %s", filename
, rrd_get_error());
1048 cfs_critical("RRD update error: unknown/wrong key %s", key
);
1058 rrdentry_t
*entry
= (rrdentry_t
*)value
;
1059 uint32_t ctime
= GPOINTER_TO_UINT(user_data
);
1061 int diff
= ctime
- entry
->time
;
1063 /* remove everything older than 5 minutes */
1066 return (diff
> expire
) ? TRUE
: FALSE
;
1069 static char *rrd_dump_buf
= NULL
;
1070 static time_t rrd_dump_last
= 0;
1073 cfs_rrd_dump(GString
*str
)
1078 if (rrd_dump_buf
&& (ctime
- rrd_dump_last
) < 2) {
1079 g_string_assign(str
, rrd_dump_buf
);
1083 /* remove old data */
1084 g_hash_table_foreach_remove(cfs_status
.rrdhash
, rrd_entry_is_old
,
1085 GUINT_TO_POINTER(ctime
));
1087 g_string_set_size(str
, 0);
1089 GHashTableIter iter
;
1090 gpointer key
, value
;
1092 g_hash_table_iter_init (&iter
, cfs_status
.rrdhash
);
1094 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
1095 rrdentry_t
*entry
= (rrdentry_t
*)value
;
1096 g_string_append(str
, key
);
1097 g_string_append(str
, ":");
1098 g_string_append(str
, entry
->data
);
1099 g_string_append(str
, "\n");
1102 g_string_append_c(str
, 0); // never return undef
1104 rrd_dump_last
= ctime
;
1106 g_free(rrd_dump_buf
);
1107 rrd_dump_buf
= g_strdup(str
->str
);
1113 const char *nodename
,
1117 g_return_val_if_fail(iphash
!= NULL
, FALSE
);
1118 g_return_val_if_fail(nodename
!= NULL
, FALSE
);
1119 g_return_val_if_fail(ip
!= NULL
, FALSE
);
1120 g_return_val_if_fail(len
> 0, FALSE
);
1121 g_return_val_if_fail(len
< 256, FALSE
);
1122 g_return_val_if_fail(ip
[len
-1] == 0, FALSE
);
1124 char *oldip
= (char *)g_hash_table_lookup(iphash
, nodename
);
1126 if (!oldip
|| (strcmp(oldip
, ip
) != 0)) {
1127 cfs_status
.clinfo_version
++;
1128 g_hash_table_replace(iphash
, g_strdup(nodename
), g_strdup(ip
));
1136 GHashTable
*rrdhash
,
1141 g_return_val_if_fail(rrdhash
!= NULL
, FALSE
);
1142 g_return_val_if_fail(key
!= NULL
, FALSE
);
1143 g_return_val_if_fail(data
!= NULL
, FALSE
);
1144 g_return_val_if_fail(len
> 0, FALSE
);
1145 g_return_val_if_fail(len
< 4096, FALSE
);
1146 g_return_val_if_fail(data
[len
-1] == 0, FALSE
);
1149 if ((entry
= (rrdentry_t
*)g_hash_table_lookup(rrdhash
, key
))) {
1150 g_free(entry
->data
);
1151 entry
->data
= g_memdup(data
, len
);
1153 entry
->time
= time(NULL
);
1155 rrdentry_t
*entry
= g_new0(rrdentry_t
, 1);
1157 entry
->key
= g_strdup(key
);
1158 entry
->data
= g_memdup(data
, len
);
1160 entry
->time
= time(NULL
);
1162 g_hash_table_replace(rrdhash
, entry
->key
, entry
);
1165 update_rrd_data(key
, data
, len
);
1171 kvstore_send_update_message(
1178 struct iovec iov
[2];
1181 g_strlcpy(name
, key
, sizeof(name
));
1183 iov
[0].iov_base
= &name
;
1184 iov
[0].iov_len
= sizeof(name
);
1186 iov
[1].iov_base
= (char *)data
;
1187 iov
[1].iov_len
= len
;
1189 if (dfsm_send_message(dfsm
, KVSTORE_MESSAGE_UPDATE
, iov
, 2) == CS_OK
)
1195 static clog_entry_t
*
1196 kvstore_parse_log_message(
1200 g_return_val_if_fail(msg
!= NULL
, NULL
);
1202 if (msg_len
< sizeof(clog_entry_t
)) {
1203 cfs_critical("received short log message (%lu < %lu)", msg_len
, sizeof(clog_entry_t
));
1207 clog_entry_t
*entry
= (clog_entry_t
*)msg
;
1209 uint32_t size
= sizeof(clog_entry_t
) + entry
->node_len
+
1210 entry
->ident_len
+ entry
->tag_len
+ entry
->msg_len
;
1212 if (msg_len
!= size
) {
1213 cfs_critical("received log message with wrong size (%lu != %u)", msg_len
, size
);
1219 if (*((char *)msg
+ entry
->node_len
- 1)) {
1220 cfs_critical("unterminated string in log message");
1223 msg
+= entry
->node_len
;
1225 if (*((char *)msg
+ entry
->ident_len
- 1)) {
1226 cfs_critical("unterminated string in log message");
1229 msg
+= entry
->ident_len
;
1231 if (*((char *)msg
+ entry
->tag_len
- 1)) {
1232 cfs_critical("unterminated string in log message");
1235 msg
+= entry
->tag_len
;
1237 if (*((char *)msg
+ entry
->msg_len
- 1)) {
1238 cfs_critical("unterminated string in log message");
1246 kvstore_parse_update_message(
1250 gconstpointer
*data
,
1253 g_return_val_if_fail(msg
!= NULL
, FALSE
);
1254 g_return_val_if_fail(key
!= NULL
, FALSE
);
1255 g_return_val_if_fail(data
!= NULL
, FALSE
);
1256 g_return_val_if_fail(len
!= NULL
, FALSE
);
1258 if (msg_len
< 256) {
1259 cfs_critical("received short kvstore message (%lu < 256)", msg_len
);
1263 /* test if key is null terminated */
1265 for (i
= 0; i
< 256; i
++)
1266 if (((char *)msg
)[i
] == 0)
1273 *len
= msg_len
- 256;
1281 cfs_create_status_msg(
1283 const char *nodename
,
1286 g_return_val_if_fail(str
!= NULL
, -EINVAL
);
1287 g_return_val_if_fail(key
!= NULL
, -EINVAL
);
1291 GHashTable
*kvhash
= NULL
;
1293 g_mutex_lock (&mutex
);
1295 if (!nodename
|| !nodename
[0] || !strcmp(nodename
, cfs
.nodename
)) {
1296 kvhash
= cfs_status
.kvhash
;
1298 cfs_clnode_t
*clnode
;
1299 if ((clnode
= g_hash_table_lookup(cfs_status
.clinfo
->nodes_byname
, nodename
)))
1300 kvhash
= clnode
->kvhash
;
1304 if (kvhash
&& (entry
= (kventry_t
*)g_hash_table_lookup(kvhash
, key
))) {
1305 g_string_append_len(str
, entry
->data
, entry
->len
);
1309 g_mutex_unlock (&mutex
);
1320 g_return_val_if_fail(key
!= NULL
, FALSE
);
1321 g_return_val_if_fail(data
!= NULL
, FALSE
);
1322 g_return_val_if_fail(cfs_status
.kvhash
!= NULL
, FALSE
);
1324 if (len
> CFS_MAX_STATUS_SIZE
)
1327 g_mutex_lock (&mutex
);
1331 if (strncmp(key
, "rrd/", 4) == 0) {
1332 res
= rrdentry_hash_set(cfs_status
.rrdhash
, key
+ 4, data
, len
);
1333 } else if (!strcmp(key
, "nodeip")) {
1334 res
= nodeip_hash_set(cfs_status
.iphash
, cfs
.nodename
, data
, len
);
1336 res
= kventry_hash_set(cfs_status
.kvhash
, key
, data
, len
);
1338 g_mutex_unlock (&mutex
);
1340 if (cfs_status
.kvstore
)
1341 kvstore_send_update_message(cfs_status
.kvstore
, key
, data
, len
);
1343 return res
? 0 : -ENOMEM
;
1347 cfs_kvstore_node_set(
1353 g_return_val_if_fail(nodeid
!= 0, FALSE
);
1354 g_return_val_if_fail(key
!= NULL
, FALSE
);
1355 g_return_val_if_fail(data
!= NULL
, FALSE
);
1357 g_mutex_lock (&mutex
);
1359 if (!cfs_status
.clinfo
|| !cfs_status
.clinfo
->nodes_byid
)
1360 goto ret
; /* ignore */
1362 cfs_clnode_t
*clnode
= g_hash_table_lookup(cfs_status
.clinfo
->nodes_byid
, &nodeid
);
1364 goto ret
; /* ignore */
1366 cfs_debug("got node %d status update %s", nodeid
, key
);
1368 if (strncmp(key
, "rrd/", 4) == 0) {
1369 rrdentry_hash_set(cfs_status
.rrdhash
, key
+ 4, data
, len
);
1370 } else if (!strcmp(key
, "nodeip")) {
1371 nodeip_hash_set(cfs_status
.iphash
, clnode
->name
, data
, len
);
1373 if (!clnode
->kvhash
) {
1374 if (!(clnode
->kvhash
= kventry_hash_new())) {
1375 goto ret
; /*ignore */
1379 kventry_hash_set(clnode
->kvhash
, key
, data
, len
);
1383 g_mutex_unlock (&mutex
);
1389 cfs_kvstore_sync(void)
1391 g_return_val_if_fail(cfs_status
.kvhash
!= NULL
, FALSE
);
1392 g_return_val_if_fail(cfs_status
.kvstore
!= NULL
, FALSE
);
1394 gboolean res
= TRUE
;
1396 g_mutex_lock (&mutex
);
1398 GHashTable
*ht
= cfs_status
.kvhash
;
1399 GHashTableIter iter
;
1400 gpointer key
, value
;
1402 g_hash_table_iter_init (&iter
, ht
);
1404 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
1405 kventry_t
*entry
= (kventry_t
*)value
;
1406 kvstore_send_update_message(cfs_status
.kvstore
, entry
->key
, entry
->data
, entry
->len
);
1409 g_mutex_unlock (&mutex
);
1426 g_return_val_if_fail(dfsm
!= NULL
, -1);
1427 g_return_val_if_fail(msg
!= NULL
, -1);
1428 g_return_val_if_fail(res_ptr
!= NULL
, -1);
1430 /* ignore message for ourself */
1431 if (dfsm_nodeid_is_local(dfsm
, nodeid
, pid
))
1434 if (msg_type
== KVSTORE_MESSAGE_UPDATE
) {
1438 if (kvstore_parse_update_message(msg
, msg_len
, &key
, &data
, &len
)) {
1439 cfs_kvstore_node_set(nodeid
, key
, data
, len
);
1441 cfs_critical("cant parse update message");
1443 } else if (msg_type
== KVSTORE_MESSAGE_LOG
) {
1444 cfs_message("received log"); // fixme: remove
1445 const clog_entry_t
*entry
;
1446 if ((entry
= kvstore_parse_log_message(msg
, msg_len
))) {
1447 clusterlog_insert(cfs_status
.clusterlog
, entry
);
1449 cfs_critical("cant parse log message");
1452 cfs_critical("received unknown message type %d\n", msg_type
);
1469 const struct cpg_address
*member_list
,
1470 size_t member_list_entries
)
1472 g_return_if_fail(dfsm
!= NULL
);
1473 g_return_if_fail(member_list
!= NULL
);
1475 cfs_debug("enter %s", __func__
);
1477 g_mutex_lock (&mutex
);
1479 cfs_clinfo_t
*clinfo
= cfs_status
.clinfo
;
1481 if (clinfo
&& clinfo
->nodes_byid
) {
1483 GHashTable
*ht
= clinfo
->nodes_byid
;
1484 GHashTableIter iter
;
1485 gpointer key
, value
;
1487 g_hash_table_iter_init (&iter
, ht
);
1489 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
1490 cfs_clnode_t
*node
= (cfs_clnode_t
*)value
;
1491 node
->online
= FALSE
;
1494 for (int i
= 0; i
< member_list_entries
; i
++) {
1496 if ((node
= g_hash_table_lookup(clinfo
->nodes_byid
, &member_list
[i
].nodeid
))) {
1497 node
->online
= TRUE
;
1501 cfs_status
.clinfo_version
++;
1504 g_mutex_unlock (&mutex
);
1511 unsigned int *res_len
)
1513 g_return_val_if_fail(dfsm
!= NULL
, NULL
);
1515 gpointer msg
= clusterlog_get_state(cfs_status
.clusterlog
, res_len
);
1521 dfsm_process_update(
1524 dfsm_sync_info_t
*syncinfo
,
1530 cfs_critical("%s: received unexpected update message", __func__
);
1536 dfsm_process_state_update(
1539 dfsm_sync_info_t
*syncinfo
)
1541 g_return_val_if_fail(dfsm
!= NULL
, -1);
1542 g_return_val_if_fail(syncinfo
!= NULL
, -1);
1544 clog_base_t
*clog
[syncinfo
->node_count
];
1546 int local_index
= -1;
1547 for (int i
= 0; i
< syncinfo
->node_count
; i
++) {
1548 dfsm_node_info_t
*ni
= &syncinfo
->nodes
[i
];
1551 if (syncinfo
->local
== ni
)
1554 clog_base_t
*base
= (clog_base_t
*)ni
->state
;
1555 if (ni
->state_len
> 8 && ni
->state_len
== clog_size(base
)) {
1556 clog
[i
] = ni
->state
;
1558 cfs_critical("received log with wrong size %u", ni
->state_len
);
1563 if (!clusterlog_merge(cfs_status
.clusterlog
, clog
, syncinfo
->node_count
, local_index
)) {
1564 cfs_critical("unable to merge log files");
1576 dfsm_sync_info_t
*syncinfo
)
1578 g_return_val_if_fail(dfsm
!= NULL
, -1);
1579 g_return_val_if_fail(syncinfo
!= NULL
, -1);
1585 dfsm_synced(dfsm_t
*dfsm
)
1587 g_return_if_fail(dfsm
!= NULL
);
1589 char *ip
= (char *)g_hash_table_lookup(cfs_status
.iphash
, cfs
.nodename
);
1593 cfs_status_set("nodeip", ip
, strlen(ip
) + 1);
1600 dfsm_sync_info_t
*syncinfo
)
1605 static dfsm_callbacks_t kvstore_dfsm_callbacks
= {
1606 .dfsm_deliver_fn
= dfsm_deliver
,
1607 .dfsm_confchg_fn
= dfsm_confchg
,
1609 .dfsm_get_state_fn
= dfsm_get_state
,
1610 .dfsm_process_state_update_fn
= dfsm_process_state_update
,
1611 .dfsm_process_update_fn
= dfsm_process_update
,
1612 .dfsm_commit_fn
= dfsm_commit
,
1613 .dfsm_cleanup_fn
= dfsm_cleanup
,
1614 .dfsm_synced_fn
= dfsm_synced
,
1618 cfs_status_dfsm_new(void)
1620 g_mutex_lock (&mutex
);
1622 cfs_status
.kvstore
= dfsm_new(NULL
, KVSTORE_CPG_GROUP_NAME
, G_LOG_DOMAIN
,
1623 0, &kvstore_dfsm_callbacks
);
1624 g_mutex_unlock (&mutex
);
1626 return cfs_status
.kvstore
;
1630 cfs_is_quorate(void)
1632 g_mutex_lock (&mutex
);
1633 gboolean res
= cfs_status
.quorate
;
1634 g_mutex_unlock (&mutex
);
1644 g_mutex_lock (&mutex
);
1646 uint32_t prev_quorate
= cfs_status
.quorate
;
1647 cfs_status
.quorate
= quorate
;
1649 if (!prev_quorate
&& cfs_status
.quorate
) {
1651 cfs_message("node has quorum");
1654 if (prev_quorate
&& !cfs_status
.quorate
) {
1656 cfs_message("node lost quorum");
1659 g_mutex_unlock (&mutex
);