2019-02-05 04:28:43 +08:00
|
|
|
package kube_inventory
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
|
2021-03-18 05:35:25 +08:00
|
|
|
corev1 "k8s.io/api/core/v1"
|
2019-02-05 04:28:43 +08:00
|
|
|
|
|
|
|
|
"github.com/influxdata/telegraf"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func collectNodes(ctx context.Context, acc telegraf.Accumulator, ki *KubernetesInventory) {
|
|
|
|
|
list, err := ki.client.getNodes(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
acc.AddError(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
for _, n := range list.Items {
|
2021-03-18 05:35:25 +08:00
|
|
|
ki.gatherNode(n, acc)
|
2019-02-05 04:28:43 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-18 05:35:25 +08:00
|
|
|
func (ki *KubernetesInventory) gatherNode(n corev1.Node, acc telegraf.Accumulator) {
|
2019-02-05 04:28:43 +08:00
|
|
|
fields := map[string]interface{}{}
|
|
|
|
|
tags := map[string]string{
|
2021-03-18 05:35:25 +08:00
|
|
|
"node_name": n.Name,
|
2019-02-05 04:28:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for resourceName, val := range n.Status.Capacity {
|
|
|
|
|
switch resourceName {
|
|
|
|
|
case "cpu":
|
2021-10-27 23:48:57 +08:00
|
|
|
fields["capacity_cpu_cores"] = ki.convertQuantity(val.String(), 1)
|
|
|
|
|
fields["capacity_millicpu_cores"] = ki.convertQuantity(val.String(), 1000)
|
2019-02-05 04:28:43 +08:00
|
|
|
case "memory":
|
2021-10-27 23:48:57 +08:00
|
|
|
fields["capacity_memory_bytes"] = ki.convertQuantity(val.String(), 1)
|
2019-02-05 04:28:43 +08:00
|
|
|
case "pods":
|
2021-08-05 06:52:52 +08:00
|
|
|
fields["capacity_pods"] = atoi(val.String())
|
2019-02-05 04:28:43 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for resourceName, val := range n.Status.Allocatable {
|
|
|
|
|
switch resourceName {
|
|
|
|
|
case "cpu":
|
2021-10-27 23:48:57 +08:00
|
|
|
fields["allocatable_cpu_cores"] = ki.convertQuantity(val.String(), 1)
|
|
|
|
|
fields["allocatable_millicpu_cores"] = ki.convertQuantity(val.String(), 1000)
|
2019-02-05 04:28:43 +08:00
|
|
|
case "memory":
|
2021-10-27 23:48:57 +08:00
|
|
|
fields["allocatable_memory_bytes"] = ki.convertQuantity(val.String(), 1)
|
2019-02-05 04:28:43 +08:00
|
|
|
case "pods":
|
2021-08-05 06:52:52 +08:00
|
|
|
fields["allocatable_pods"] = atoi(val.String())
|
2019-02-05 04:28:43 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
acc.AddFields(nodeMeasurement, fields, tags)
|
|
|
|
|
}
|