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
||
585 vmtype
== VMTYPE_LXC
, FALSE
);
587 if (!replace
&& g_hash_table_lookup(vmlist
, &vmid
)) {
588 cfs_critical("detected duplicate VMID %d", vmid
);
592 vminfo_t
*vminfo
= g_new0(vminfo_t
, 1);
595 vminfo
->vmtype
= vmtype
;
596 vminfo
->nodename
= g_strdup(nodename
);
598 vminfo
->version
= ++vminfo_version_counter
;
600 g_hash_table_replace(vmlist
, &vminfo
->vmid
, vminfo
);
609 const char *nodename
)
611 g_return_if_fail(cfs_status
.vmlist
!= NULL
);
612 g_return_if_fail(nodename
!= NULL
);
613 g_return_if_fail(vmid
!= 0);
614 g_return_if_fail(vmtype
== VMTYPE_QEMU
|| vmtype
== VMTYPE_OPENVZ
||
615 vmtype
== VMTYPE_LXC
);
617 cfs_debug("vmlist_register_vm: %s/%u %d", nodename
, vmid
, vmtype
);
619 g_mutex_lock (&mutex
);
621 cfs_status
.vmlist_version
++;
623 vmlist_hash_insert_vm(cfs_status
.vmlist
, vmtype
, vmid
, nodename
, TRUE
);
625 g_mutex_unlock (&mutex
);
629 vmlist_different_vm_exists(
632 const char *nodename
)
634 g_return_val_if_fail(cfs_status
.vmlist
!= NULL
, FALSE
);
635 g_return_val_if_fail(vmid
!= 0, FALSE
);
637 gboolean res
= FALSE
;
639 g_mutex_lock (&mutex
);
642 if ((vminfo
= (vminfo_t
*)g_hash_table_lookup(cfs_status
.vmlist
, &vmid
))) {
643 if (!(vminfo
->vmtype
== vmtype
&& strcmp(vminfo
->nodename
, nodename
) == 0))
646 g_mutex_unlock (&mutex
);
655 g_return_val_if_fail(cfs_status
.vmlist
!= NULL
, FALSE
);
656 g_return_val_if_fail(vmid
!= 0, FALSE
);
658 g_mutex_lock (&mutex
);
660 gpointer res
= g_hash_table_lookup(cfs_status
.vmlist
, &vmid
);
662 g_mutex_unlock (&mutex
);
671 g_return_if_fail(cfs_status
.vmlist
!= NULL
);
672 g_return_if_fail(vmid
!= 0);
674 g_mutex_lock (&mutex
);
676 cfs_status
.vmlist_version
++;
678 g_hash_table_remove(cfs_status
.vmlist
, &vmid
);
680 g_mutex_unlock (&mutex
);
683 void cfs_status_set_vmlist(
686 g_return_if_fail(vmlist
!= NULL
);
688 g_mutex_lock (&mutex
);
690 cfs_status
.vmlist_version
++;
692 if (cfs_status
.vmlist
)
693 g_hash_table_destroy(cfs_status
.vmlist
);
695 cfs_status
.vmlist
= vmlist
;
697 g_mutex_unlock (&mutex
);
701 cfs_create_vmlist_msg(GString
*str
)
703 g_return_val_if_fail(cfs_status
.vmlist
!= NULL
, -EINVAL
);
704 g_return_val_if_fail(str
!= NULL
, -EINVAL
);
706 g_mutex_lock (&mutex
);
708 g_string_append_printf(str
,"{\n");
710 GHashTable
*ht
= cfs_status
.vmlist
;
712 guint count
= g_hash_table_size(ht
);
715 g_string_append_printf(str
,"\"version\": %u\n", cfs_status
.vmlist_version
);
717 g_string_append_printf(str
,"\"version\": %u,\n", cfs_status
.vmlist_version
);
719 g_string_append_printf(str
,"\"ids\": {\n");
724 g_hash_table_iter_init (&iter
, ht
);
727 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
728 vminfo_t
*vminfo
= (vminfo_t
*)value
;
730 if (vminfo
->vmtype
== VMTYPE_QEMU
) {
732 } else if (vminfo
->vmtype
== VMTYPE_OPENVZ
) {
734 } else if (vminfo
->vmtype
== VMTYPE_LXC
) {
741 g_string_append_printf(str
, ",\n");
744 g_string_append_printf(str
,"\"%u\": { \"node\": \"%s\", \"type\": \"%s\", \"version\": %u }",
745 vminfo
->vmid
, vminfo
->nodename
, type
, vminfo
->version
);
748 g_string_append_printf(str
,"}\n");
750 g_string_append_printf(str
,"\n}\n");
752 g_mutex_unlock (&mutex
);
758 record_memdb_change(const char *path
)
760 g_return_if_fail(cfs_status
.memdb_changes
!= 0);
764 unsigned int vmid
= 0;
767 if (cfs_status
.vmlist
&&
768 sscanf(path
, "nodes/%255[^/]/lxc/%u/%4095s", nodename
, &vmid
, rest
) == 3) {
769 vminfo_t
*vminfo
= (vminfo_t
*)g_hash_table_lookup(cfs_status
.vmlist
, &vmid
);
770 if (vminfo
&& (vminfo
->vmtype
== VMTYPE_LXC
&& strcmp(vminfo
->nodename
, nodename
) == 0)) {
771 cfs_status
.vmlist_version
++;
772 vminfo
->version
= ++vminfo_version_counter
;
776 if ((ce
= (memdb_change_t
*)g_hash_table_lookup(cfs_status
.memdb_changes
, path
))) {
782 record_memdb_reload(void)
784 for (int i
= 0; i
< G_N_ELEMENTS(memdb_change_array
); i
++) {
785 memdb_change_array
[i
].version
++;
796 g_return_val_if_fail(kvhash
!= NULL
, FALSE
);
797 g_return_val_if_fail(key
!= NULL
, FALSE
);
798 g_return_val_if_fail(data
!= NULL
, FALSE
);
801 if ((entry
= (kventry_t
*)g_hash_table_lookup(kvhash
, key
))) {
803 entry
->data
= g_memdup(data
, len
);
807 kventry_t
*entry
= g_new0(kventry_t
, 1);
809 entry
->key
= g_strdup(key
);
810 entry
->data
= g_memdup(data
, len
);
813 g_hash_table_replace(kvhash
, entry
->key
, entry
);
819 static const char *rrd_def_node
[] = {
820 "DS:loadavg:GAUGE:120:0:U",
821 "DS:maxcpu:GAUGE:120:0:U",
822 "DS:cpu:GAUGE:120:0:U",
823 "DS:iowait:GAUGE:120:0:U",
824 "DS:memtotal:GAUGE:120:0:U",
825 "DS:memused:GAUGE:120:0:U",
826 "DS:swaptotal:GAUGE:120:0:U",
827 "DS:swapused:GAUGE:120:0:U",
828 "DS:roottotal:GAUGE:120:0:U",
829 "DS:rootused:GAUGE:120:0:U",
830 "DS:netin:DERIVE:120:0:U",
831 "DS:netout:DERIVE:120:0:U",
833 "RRA:AVERAGE:0.5:1:70", // 1 min avg - one hour
834 "RRA:AVERAGE:0.5:30:70", // 30 min avg - one day
835 "RRA:AVERAGE:0.5:180:70", // 3 hour avg - one week
836 "RRA:AVERAGE:0.5:720:70", // 12 hour avg - one month
837 "RRA:AVERAGE:0.5:10080:70", // 7 day avg - ony year
839 "RRA:MAX:0.5:1:70", // 1 min max - one hour
840 "RRA:MAX:0.5:30:70", // 30 min max - one day
841 "RRA:MAX:0.5:180:70", // 3 hour max - one week
842 "RRA:MAX:0.5:720:70", // 12 hour max - one month
843 "RRA:MAX:0.5:10080:70", // 7 day max - ony year
847 static const char *rrd_def_vm
[] = {
848 "DS:maxcpu:GAUGE:120:0:U",
849 "DS:cpu:GAUGE:120:0:U",
850 "DS:maxmem:GAUGE:120:0:U",
851 "DS:mem:GAUGE:120:0:U",
852 "DS:maxdisk:GAUGE:120:0:U",
853 "DS:disk:GAUGE:120:0:U",
854 "DS:netin:DERIVE:120:0:U",
855 "DS:netout:DERIVE:120:0:U",
856 "DS:diskread:DERIVE:120:0:U",
857 "DS:diskwrite:DERIVE:120:0:U",
859 "RRA:AVERAGE:0.5:1:70", // 1 min avg - one hour
860 "RRA:AVERAGE:0.5:30:70", // 30 min avg - one day
861 "RRA:AVERAGE:0.5:180:70", // 3 hour avg - one week
862 "RRA:AVERAGE:0.5:720:70", // 12 hour avg - one month
863 "RRA:AVERAGE:0.5:10080:70", // 7 day avg - ony year
865 "RRA:MAX:0.5:1:70", // 1 min max - one hour
866 "RRA:MAX:0.5:30:70", // 30 min max - one day
867 "RRA:MAX:0.5:180:70", // 3 hour max - one week
868 "RRA:MAX:0.5:720:70", // 12 hour max - one month
869 "RRA:MAX:0.5:10080:70", // 7 day max - ony year
873 static const char *rrd_def_storage
[] = {
874 "DS:total:GAUGE:120:0:U",
875 "DS:used:GAUGE:120:0:U",
877 "RRA:AVERAGE:0.5:1:70", // 1 min avg - one hour
878 "RRA:AVERAGE:0.5:30:70", // 30 min avg - one day
879 "RRA:AVERAGE:0.5:180:70", // 3 hour avg - one week
880 "RRA:AVERAGE:0.5:720:70", // 12 hour avg - one month
881 "RRA:AVERAGE:0.5:10080:70", // 7 day avg - ony year
883 "RRA:MAX:0.5:1:70", // 1 min max - one hour
884 "RRA:MAX:0.5:30:70", // 30 min max - one day
885 "RRA:MAX:0.5:180:70", // 3 hour max - one week
886 "RRA:MAX:0.5:720:70", // 12 hour max - one month
887 "RRA:MAX:0.5:10080:70", // 7 day max - ony year
891 #define RRDDIR "/var/lib/rrdcached/db"
895 const char *filename
,
897 const char *rrddef
[])
899 /* start at day boundary */
902 struct tm
*ltm
= localtime(&ctime
);
908 if (rrd_create_r(filename
, 60, timelocal(ltm
), argcount
, rrddef
)) {
909 cfs_message("RRD create error %s: %s", filename
, rrd_get_error());
913 static inline const char *
919 while (*data
&& found
< count
) {
932 g_return_if_fail(key
!= NULL
);
933 g_return_if_fail(data
!= NULL
);
934 g_return_if_fail(len
> 0);
935 g_return_if_fail(len
< 4096);
937 static const char *rrdcsock
= "unix:/var/run/rrdcached.sock";
940 if (rrdc_connect(rrdcsock
) != 0)
943 char *filename
= NULL
;
947 if (strncmp(key
, "pve2-node/", 10) == 0) {
948 const char *node
= key
+ 10;
952 if (strchr(node
, '/') != NULL
)
955 if (strlen(node
) < 1)
958 filename
= g_strdup_printf(RRDDIR
"/%s", key
);
960 if (!g_file_test(filename
, G_FILE_TEST_EXISTS
)) {
962 mkdir(RRDDIR
"/pve2-node", 0755);
963 int argcount
= sizeof(rrd_def_node
)/sizeof(void*) - 1;
964 create_rrd_file(filename
, argcount
, rrd_def_node
);
967 } else if ((strncmp(key
, "pve2-vm/", 8) == 0) ||
968 (strncmp(key
, "pve2.3-vm/", 10) == 0)) {
971 if (strncmp(key
, "pve2-vm/", 8) == 0) {
979 if (strchr(vmid
, '/') != NULL
)
982 if (strlen(vmid
) < 1)
985 filename
= g_strdup_printf(RRDDIR
"/%s/%s", "pve2-vm", vmid
);
987 if (!g_file_test(filename
, G_FILE_TEST_EXISTS
)) {
989 mkdir(RRDDIR
"/pve2-vm", 0755);
990 int argcount
= sizeof(rrd_def_vm
)/sizeof(void*) - 1;
991 create_rrd_file(filename
, argcount
, rrd_def_vm
);
994 } else if (strncmp(key
, "pve2-storage/", 13) == 0) {
995 const char *node
= key
+ 13;
997 const char *storage
= node
;
998 while (*storage
&& *storage
!= '/')
1001 if (*storage
!= '/' || ((storage
- node
) < 1))
1006 if (strchr(storage
, '/') != NULL
)
1009 if (strlen(storage
) < 1)
1012 filename
= g_strdup_printf(RRDDIR
"/%s", key
);
1014 if (!g_file_test(filename
, G_FILE_TEST_EXISTS
)) {
1016 mkdir(RRDDIR
"/pve2-storage", 0755);
1018 char *dir
= g_path_get_dirname(filename
);
1022 int argcount
= sizeof(rrd_def_storage
)/sizeof(void*) - 1;
1023 create_rrd_file(filename
, argcount
, rrd_def_storage
);
1030 const char *dp
= skip
? rrd_skip_data(data
, skip
) : data
;
1032 const char *update_args
[] = { dp
, NULL
};
1036 if ((status
= rrdc_update(filename
, 1, update_args
)) != 0) {
1037 cfs_message("RRDC update error %s: %d", filename
, status
);
1040 if (rrd_update_r(filename
, NULL
, 1, update_args
) != 0) {
1041 cfs_message("RRD update error %s: %s", filename
, rrd_get_error());
1047 if (rrd_update_r(filename
, NULL
, 1, update_args
) != 0) {
1048 cfs_message("RRD update error %s: %s", filename
, rrd_get_error());
1059 cfs_critical("RRD update error: unknown/wrong key %s", key
);
1069 rrdentry_t
*entry
= (rrdentry_t
*)value
;
1070 uint32_t ctime
= GPOINTER_TO_UINT(user_data
);
1072 int diff
= ctime
- entry
->time
;
1074 /* remove everything older than 5 minutes */
1077 return (diff
> expire
) ? TRUE
: FALSE
;
1080 static char *rrd_dump_buf
= NULL
;
1081 static time_t rrd_dump_last
= 0;
1084 cfs_rrd_dump(GString
*str
)
1089 if (rrd_dump_buf
&& (ctime
- rrd_dump_last
) < 2) {
1090 g_string_assign(str
, rrd_dump_buf
);
1094 /* remove old data */
1095 g_hash_table_foreach_remove(cfs_status
.rrdhash
, rrd_entry_is_old
,
1096 GUINT_TO_POINTER(ctime
));
1098 g_string_set_size(str
, 0);
1100 GHashTableIter iter
;
1101 gpointer key
, value
;
1103 g_hash_table_iter_init (&iter
, cfs_status
.rrdhash
);
1105 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
1106 rrdentry_t
*entry
= (rrdentry_t
*)value
;
1107 g_string_append(str
, key
);
1108 g_string_append(str
, ":");
1109 g_string_append(str
, entry
->data
);
1110 g_string_append(str
, "\n");
1113 g_string_append_c(str
, 0); // never return undef
1115 rrd_dump_last
= ctime
;
1117 g_free(rrd_dump_buf
);
1118 rrd_dump_buf
= g_strdup(str
->str
);
1124 const char *nodename
,
1128 g_return_val_if_fail(iphash
!= NULL
, FALSE
);
1129 g_return_val_if_fail(nodename
!= NULL
, FALSE
);
1130 g_return_val_if_fail(ip
!= NULL
, FALSE
);
1131 g_return_val_if_fail(len
> 0, FALSE
);
1132 g_return_val_if_fail(len
< 256, FALSE
);
1133 g_return_val_if_fail(ip
[len
-1] == 0, FALSE
);
1135 char *oldip
= (char *)g_hash_table_lookup(iphash
, nodename
);
1137 if (!oldip
|| (strcmp(oldip
, ip
) != 0)) {
1138 cfs_status
.clinfo_version
++;
1139 g_hash_table_replace(iphash
, g_strdup(nodename
), g_strdup(ip
));
1147 GHashTable
*rrdhash
,
1152 g_return_val_if_fail(rrdhash
!= NULL
, FALSE
);
1153 g_return_val_if_fail(key
!= NULL
, FALSE
);
1154 g_return_val_if_fail(data
!= NULL
, FALSE
);
1155 g_return_val_if_fail(len
> 0, FALSE
);
1156 g_return_val_if_fail(len
< 4096, FALSE
);
1157 g_return_val_if_fail(data
[len
-1] == 0, FALSE
);
1160 if ((entry
= (rrdentry_t
*)g_hash_table_lookup(rrdhash
, key
))) {
1161 g_free(entry
->data
);
1162 entry
->data
= g_memdup(data
, len
);
1164 entry
->time
= time(NULL
);
1166 rrdentry_t
*entry
= g_new0(rrdentry_t
, 1);
1168 entry
->key
= g_strdup(key
);
1169 entry
->data
= g_memdup(data
, len
);
1171 entry
->time
= time(NULL
);
1173 g_hash_table_replace(rrdhash
, entry
->key
, entry
);
1176 update_rrd_data(key
, data
, len
);
1182 kvstore_send_update_message(
1189 struct iovec iov
[2];
1192 g_strlcpy(name
, key
, sizeof(name
));
1194 iov
[0].iov_base
= &name
;
1195 iov
[0].iov_len
= sizeof(name
);
1197 iov
[1].iov_base
= (char *)data
;
1198 iov
[1].iov_len
= len
;
1200 if (dfsm_send_message(dfsm
, KVSTORE_MESSAGE_UPDATE
, iov
, 2) == CS_OK
)
1206 static clog_entry_t
*
1207 kvstore_parse_log_message(
1211 g_return_val_if_fail(msg
!= NULL
, NULL
);
1213 if (msg_len
< sizeof(clog_entry_t
)) {
1214 cfs_critical("received short log message (%lu < %lu)", msg_len
, sizeof(clog_entry_t
));
1218 clog_entry_t
*entry
= (clog_entry_t
*)msg
;
1220 uint32_t size
= sizeof(clog_entry_t
) + entry
->node_len
+
1221 entry
->ident_len
+ entry
->tag_len
+ entry
->msg_len
;
1223 if (msg_len
!= size
) {
1224 cfs_critical("received log message with wrong size (%lu != %u)", msg_len
, size
);
1230 if (*((char *)msg
+ entry
->node_len
- 1)) {
1231 cfs_critical("unterminated string in log message");
1234 msg
+= entry
->node_len
;
1236 if (*((char *)msg
+ entry
->ident_len
- 1)) {
1237 cfs_critical("unterminated string in log message");
1240 msg
+= entry
->ident_len
;
1242 if (*((char *)msg
+ entry
->tag_len
- 1)) {
1243 cfs_critical("unterminated string in log message");
1246 msg
+= entry
->tag_len
;
1248 if (*((char *)msg
+ entry
->msg_len
- 1)) {
1249 cfs_critical("unterminated string in log message");
1257 kvstore_parse_update_message(
1261 gconstpointer
*data
,
1264 g_return_val_if_fail(msg
!= NULL
, FALSE
);
1265 g_return_val_if_fail(key
!= NULL
, FALSE
);
1266 g_return_val_if_fail(data
!= NULL
, FALSE
);
1267 g_return_val_if_fail(len
!= NULL
, FALSE
);
1269 if (msg_len
< 256) {
1270 cfs_critical("received short kvstore message (%lu < 256)", msg_len
);
1274 /* test if key is null terminated */
1276 for (i
= 0; i
< 256; i
++)
1277 if (((char *)msg
)[i
] == 0)
1284 *len
= msg_len
- 256;
1292 cfs_create_status_msg(
1294 const char *nodename
,
1297 g_return_val_if_fail(str
!= NULL
, -EINVAL
);
1298 g_return_val_if_fail(key
!= NULL
, -EINVAL
);
1302 GHashTable
*kvhash
= NULL
;
1304 g_mutex_lock (&mutex
);
1306 if (!nodename
|| !nodename
[0] || !strcmp(nodename
, cfs
.nodename
)) {
1307 kvhash
= cfs_status
.kvhash
;
1309 cfs_clnode_t
*clnode
;
1310 if ((clnode
= g_hash_table_lookup(cfs_status
.clinfo
->nodes_byname
, nodename
)))
1311 kvhash
= clnode
->kvhash
;
1315 if (kvhash
&& (entry
= (kventry_t
*)g_hash_table_lookup(kvhash
, key
))) {
1316 g_string_append_len(str
, entry
->data
, entry
->len
);
1320 g_mutex_unlock (&mutex
);
1331 g_return_val_if_fail(key
!= NULL
, FALSE
);
1332 g_return_val_if_fail(data
!= NULL
, FALSE
);
1333 g_return_val_if_fail(cfs_status
.kvhash
!= NULL
, FALSE
);
1335 if (len
> CFS_MAX_STATUS_SIZE
)
1338 g_mutex_lock (&mutex
);
1342 if (strncmp(key
, "rrd/", 4) == 0) {
1343 res
= rrdentry_hash_set(cfs_status
.rrdhash
, key
+ 4, data
, len
);
1344 } else if (!strcmp(key
, "nodeip")) {
1345 res
= nodeip_hash_set(cfs_status
.iphash
, cfs
.nodename
, data
, len
);
1347 res
= kventry_hash_set(cfs_status
.kvhash
, key
, data
, len
);
1349 g_mutex_unlock (&mutex
);
1351 if (cfs_status
.kvstore
)
1352 kvstore_send_update_message(cfs_status
.kvstore
, key
, data
, len
);
1354 return res
? 0 : -ENOMEM
;
1358 cfs_kvstore_node_set(
1364 g_return_val_if_fail(nodeid
!= 0, FALSE
);
1365 g_return_val_if_fail(key
!= NULL
, FALSE
);
1366 g_return_val_if_fail(data
!= NULL
, FALSE
);
1368 g_mutex_lock (&mutex
);
1370 if (!cfs_status
.clinfo
|| !cfs_status
.clinfo
->nodes_byid
)
1371 goto ret
; /* ignore */
1373 cfs_clnode_t
*clnode
= g_hash_table_lookup(cfs_status
.clinfo
->nodes_byid
, &nodeid
);
1375 goto ret
; /* ignore */
1377 cfs_debug("got node %d status update %s", nodeid
, key
);
1379 if (strncmp(key
, "rrd/", 4) == 0) {
1380 rrdentry_hash_set(cfs_status
.rrdhash
, key
+ 4, data
, len
);
1381 } else if (!strcmp(key
, "nodeip")) {
1382 nodeip_hash_set(cfs_status
.iphash
, clnode
->name
, data
, len
);
1384 if (!clnode
->kvhash
) {
1385 if (!(clnode
->kvhash
= kventry_hash_new())) {
1386 goto ret
; /*ignore */
1390 kventry_hash_set(clnode
->kvhash
, key
, data
, len
);
1394 g_mutex_unlock (&mutex
);
1400 cfs_kvstore_sync(void)
1402 g_return_val_if_fail(cfs_status
.kvhash
!= NULL
, FALSE
);
1403 g_return_val_if_fail(cfs_status
.kvstore
!= NULL
, FALSE
);
1405 gboolean res
= TRUE
;
1407 g_mutex_lock (&mutex
);
1409 GHashTable
*ht
= cfs_status
.kvhash
;
1410 GHashTableIter iter
;
1411 gpointer key
, value
;
1413 g_hash_table_iter_init (&iter
, ht
);
1415 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
1416 kventry_t
*entry
= (kventry_t
*)value
;
1417 kvstore_send_update_message(cfs_status
.kvstore
, entry
->key
, entry
->data
, entry
->len
);
1420 g_mutex_unlock (&mutex
);
1437 g_return_val_if_fail(dfsm
!= NULL
, -1);
1438 g_return_val_if_fail(msg
!= NULL
, -1);
1439 g_return_val_if_fail(res_ptr
!= NULL
, -1);
1441 /* ignore message for ourself */
1442 if (dfsm_nodeid_is_local(dfsm
, nodeid
, pid
))
1445 if (msg_type
== KVSTORE_MESSAGE_UPDATE
) {
1449 if (kvstore_parse_update_message(msg
, msg_len
, &key
, &data
, &len
)) {
1450 cfs_kvstore_node_set(nodeid
, key
, data
, len
);
1452 cfs_critical("cant parse update message");
1454 } else if (msg_type
== KVSTORE_MESSAGE_LOG
) {
1455 cfs_message("received log"); // fixme: remove
1456 const clog_entry_t
*entry
;
1457 if ((entry
= kvstore_parse_log_message(msg
, msg_len
))) {
1458 clusterlog_insert(cfs_status
.clusterlog
, entry
);
1460 cfs_critical("cant parse log message");
1463 cfs_critical("received unknown message type %d\n", msg_type
);
1480 const struct cpg_address
*member_list
,
1481 size_t member_list_entries
)
1483 g_return_if_fail(dfsm
!= NULL
);
1484 g_return_if_fail(member_list
!= NULL
);
1486 cfs_debug("enter %s", __func__
);
1488 g_mutex_lock (&mutex
);
1490 cfs_clinfo_t
*clinfo
= cfs_status
.clinfo
;
1492 if (clinfo
&& clinfo
->nodes_byid
) {
1494 GHashTable
*ht
= clinfo
->nodes_byid
;
1495 GHashTableIter iter
;
1496 gpointer key
, value
;
1498 g_hash_table_iter_init (&iter
, ht
);
1500 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
1501 cfs_clnode_t
*node
= (cfs_clnode_t
*)value
;
1502 node
->online
= FALSE
;
1505 for (int i
= 0; i
< member_list_entries
; i
++) {
1507 if ((node
= g_hash_table_lookup(clinfo
->nodes_byid
, &member_list
[i
].nodeid
))) {
1508 node
->online
= TRUE
;
1512 cfs_status
.clinfo_version
++;
1515 g_mutex_unlock (&mutex
);
1522 unsigned int *res_len
)
1524 g_return_val_if_fail(dfsm
!= NULL
, NULL
);
1526 gpointer msg
= clusterlog_get_state(cfs_status
.clusterlog
, res_len
);
1532 dfsm_process_update(
1535 dfsm_sync_info_t
*syncinfo
,
1541 cfs_critical("%s: received unexpected update message", __func__
);
1547 dfsm_process_state_update(
1550 dfsm_sync_info_t
*syncinfo
)
1552 g_return_val_if_fail(dfsm
!= NULL
, -1);
1553 g_return_val_if_fail(syncinfo
!= NULL
, -1);
1555 clog_base_t
*clog
[syncinfo
->node_count
];
1557 int local_index
= -1;
1558 for (int i
= 0; i
< syncinfo
->node_count
; i
++) {
1559 dfsm_node_info_t
*ni
= &syncinfo
->nodes
[i
];
1562 if (syncinfo
->local
== ni
)
1565 clog_base_t
*base
= (clog_base_t
*)ni
->state
;
1566 if (ni
->state_len
> 8 && ni
->state_len
== clog_size(base
)) {
1567 clog
[i
] = ni
->state
;
1569 cfs_critical("received log with wrong size %u", ni
->state_len
);
1574 if (!clusterlog_merge(cfs_status
.clusterlog
, clog
, syncinfo
->node_count
, local_index
)) {
1575 cfs_critical("unable to merge log files");
1587 dfsm_sync_info_t
*syncinfo
)
1589 g_return_val_if_fail(dfsm
!= NULL
, -1);
1590 g_return_val_if_fail(syncinfo
!= NULL
, -1);
1596 dfsm_synced(dfsm_t
*dfsm
)
1598 g_return_if_fail(dfsm
!= NULL
);
1600 char *ip
= (char *)g_hash_table_lookup(cfs_status
.iphash
, cfs
.nodename
);
1604 cfs_status_set("nodeip", ip
, strlen(ip
) + 1);
1611 dfsm_sync_info_t
*syncinfo
)
1616 static dfsm_callbacks_t kvstore_dfsm_callbacks
= {
1617 .dfsm_deliver_fn
= dfsm_deliver
,
1618 .dfsm_confchg_fn
= dfsm_confchg
,
1620 .dfsm_get_state_fn
= dfsm_get_state
,
1621 .dfsm_process_state_update_fn
= dfsm_process_state_update
,
1622 .dfsm_process_update_fn
= dfsm_process_update
,
1623 .dfsm_commit_fn
= dfsm_commit
,
1624 .dfsm_cleanup_fn
= dfsm_cleanup
,
1625 .dfsm_synced_fn
= dfsm_synced
,
1629 cfs_status_dfsm_new(void)
1631 g_mutex_lock (&mutex
);
1633 cfs_status
.kvstore
= dfsm_new(NULL
, KVSTORE_CPG_GROUP_NAME
, G_LOG_DOMAIN
,
1634 0, &kvstore_dfsm_callbacks
);
1635 g_mutex_unlock (&mutex
);
1637 return cfs_status
.kvstore
;
1641 cfs_is_quorate(void)
1643 g_mutex_lock (&mutex
);
1644 gboolean res
= cfs_status
.quorate
;
1645 g_mutex_unlock (&mutex
);
1655 g_mutex_lock (&mutex
);
1657 uint32_t prev_quorate
= cfs_status
.quorate
;
1658 cfs_status
.quorate
= quorate
;
1660 if (!prev_quorate
&& cfs_status
.quorate
) {
1662 cfs_message("node has quorum");
1665 if (prev_quorate
&& !cfs_status
.quorate
) {
1667 cfs_message("node lost quorum");
1670 g_mutex_unlock (&mutex
);