@cryptotaxi247 / netdata-1 / commits / c98fcf575

feat: add support for cloud providers info to /api/v1/info (#12613)

Ilya Mashchenko committed Apr 6, 2022 at 13:42 UTC c98fcf5754f5cce226f8401b6a1e007703af9b20
5 files changed +88
daemon/system-info.sh
+44
@@ -403,6 +403,47 @@ elif pgrep "kubelet"; then
403 HOST_IS_K8S_NODE="true"
404 fi
405
406 +# ------------------------------------------------------------------------------------------------
407 +# Detect instance metadata for VMs running on cloud providers
408 +
409 +CLOUD_TYPE="unknown"
410 +CLOUD_INSTANCE_TYPE="unknown"
411 +CLOUD_INSTANCE_REGION="unknown"
412 +
413 +if [ "${VIRTUALIZATION}" != "none" ] && command -v curl > /dev/null 2>&1; then
414 + # Try AWS IMDSv2
415 + if [ "${CLOUD_TYPE}" = "unknown" ]; then
416 + AWS_IMDS_TOKEN="$(curl --fail -s -m 5 --noproxy "*" -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600")"
417 + if [ -n "${AWS_IMDS_TOKEN}" ]; then
418 + CLOUD_TYPE="AWS"
419 + CLOUD_INSTANCE_TYPE="$(curl --fail -s -m 5 --noproxy "*" -H "X-aws-ec2-metadata-token: $AWS_IMDS_TOKEN" -v "http://169.254.169.254/latest/meta-data/instance-type" 2> /dev/null)"
420 + CLOUD_INSTANCE_REGION="$(curl --fail -s -m 5 --noproxy "*" -H "X-aws-ec2-metadata-token: $AWS_IMDS_TOKEN" -v "http://169.254.169.254/latest/meta-data/placement/region" 2> /dev/null)"
421 + fi
422 + fi
423 +
424 + # Try GCE computeMetadata v1
425 + if [ "${CLOUD_TYPE}" = "unknown" ]; then
426 + if [ -n "$(curl --fail -s -m 5 --noproxy "*" -H "Metadata-Flavor: Google" "http://metadata.google.internal/computeMetadata/v1")" ]; then
427 + CLOUD_TYPE="GCP"
428 + CLOUD_INSTANCE_TYPE="$(curl --fail -s -m 5 --noproxy "*" -H "Metadata-Flavor: Google" "http://metadata.google.internal/computeMetadata/v1/instance/machine-type")"
429 + [ -n "$CLOUD_INSTANCE_TYPE" ] && CLOUD_INSTANCE_TYPE=$(basename "$CLOUD_INSTANCE_TYPE")
430 + CLOUD_INSTANCE_REGION="$(curl --fail -s -m 5 --noproxy "*" -H "Metadata-Flavor: Google" "http://metadata.google.internal/computeMetadata/v1/instance/zone")"
431 + [ -n "$CLOUD_INSTANCE_REGION" ] && CLOUD_INSTANCE_REGION=$(basename "$CLOUD_INSTANCE_REGION") && CLOUD_INSTANCE_REGION=${CLOUD_INSTANCE_REGION%-*}
432 + fi
433 + fi
434 +
435 + # TODO: needs to be tested in Microsoft Azure
436 + # Try Azure IMDS
437 + # if [ "${CLOUD_TYPE}" = "unknown" ]; then
438 + # AZURE_IMDS_DATA="$(curl --fail -s -m 5 -H "Metadata: true" --noproxy "*" "http://169.254.169.254/metadata/instance?version=2021-10-01")"
439 + # if [ -n "${AZURE_IMDS_DATA}" ]; then
440 + # CLOUD_TYPE="Azure"
441 + # CLOUD_INSTANCE_TYPE="$(curl --fail -s -m 5 -H "Metadata: true" --noproxy "*" "http://169.254.169.254/metadata/instance/compute/vmSize?version=2021-10-01&format=text")"
442 + # CLOUD_INSTANCE_REGION="$(curl --fail -s -m 5 -H "Metadata: true" --noproxy "*" "http://169.254.169.254/metadata/instance/compute/location?version=2021-10-01&format=text")"
443 + # fi
444 + # fi
445 +fi
446 +
447 echo "NETDATA_CONTAINER_OS_NAME=${CONTAINER_NAME}"
448 echo "NETDATA_CONTAINER_OS_ID=${CONTAINER_ID}"
449 echo "NETDATA_CONTAINER_OS_ID_LIKE=${CONTAINER_ID_LIKE}"
@@ -433,3 +474,6 @@ echo "NETDATA_SYSTEM_TOTAL_RAM=${TOTAL_RAM}"
474 echo "NETDATA_SYSTEM_RAM_DETECTION=${RAM_DETECTION}"
475 echo "NETDATA_SYSTEM_TOTAL_DISK_SIZE=${DISK_SIZE}"
476 echo "NETDATA_SYSTEM_DISK_DETECTION=${DISK_DETECTION}"
477 +echo "NETDATA_INSTANCE_CLOUD_TYPE=${CLOUD_TYPE}"
478 +echo "NETDATA_INSTANCE_CLOUD_INSTANCE_TYPE=${CLOUD_INSTANCE_TYPE}"
479 +echo "NETDATA_INSTANCE_CLOUD_INSTANCE_REGION=${CLOUD_INSTANCE_REGION}"
database/rrd.h
+4
@@ -732,6 +732,10 @@ typedef struct alarm_log {
732 // RRD HOST
733
734 struct rrdhost_system_info {
735 + char *cloud_provider_type;
736 + char *cloud_instance_type;
737 + char *cloud_instance_region;
738 +
739 char *host_os_name;
740 char *host_os_id;
741 char *host_os_id_like;
database/rrdhost.c
+27
@@ -806,6 +806,9 @@ void rrdhost_system_info_free(struct rrdhost_system_info *system_info) {
806 info("SYSTEM_INFO: free %p", system_info);
807
808 if(likely(system_info)) {
809 + freez(system_info->cloud_provider_type);
810 + freez(system_info->cloud_instance_type);
811 + freez(system_info->cloud_instance_region);
812 freez(system_info->host_os_name);
813 freez(system_info->host_os_id);
814 freez(system_info->host_os_id_like);
@@ -1034,6 +1037,18 @@ static struct label *rrdhost_load_auto_labels(void)
1037 {
1038 struct label *label_list = NULL;
1039
1040 + if (localhost->system_info->cloud_provider_type)
1041 + label_list =
1042 + add_label_to_list(label_list, "_cloud_provider_type", localhost->system_info->cloud_provider_type, LABEL_SOURCE_AUTO);
1043 +
1044 + if (localhost->system_info->cloud_instance_type)
1045 + label_list =
1046 + add_label_to_list(label_list, "_cloud_instance_type", localhost->system_info->cloud_instance_type, LABEL_SOURCE_AUTO);
1047 +
1048 + if (localhost->system_info->cloud_instance_region)
1049 + label_list =
1050 + add_label_to_list(label_list, "_cloud_instance_region", localhost->system_info->cloud_instance_region, LABEL_SOURCE_AUTO);
1051 +
1052 if (localhost->system_info->host_os_name)
1053 label_list =
1054 add_label_to_list(label_list, "_os_name", localhost->system_info->host_os_name, LABEL_SOURCE_AUTO);
@@ -1543,6 +1558,18 @@ int rrdhost_set_system_info_variable(struct rrdhost_system_info *system_info, ch
1558
1559 if (!strcmp(name, "NETDATA_PROTOCOL_VERSION"))
1560 return res;
1561 + else if(!strcmp(name, "NETDATA_INSTANCE_CLOUD_TYPE")){
1562 + freez(system_info->cloud_provider_type);
1563 + system_info->cloud_provider_type = strdupz(value);
1564 + }
1565 + else if(!strcmp(name, "NETDATA_INSTANCE_CLOUD_INSTANCE_TYPE")){
1566 + freez(system_info->cloud_instance_type);
1567 + system_info->cloud_instance_type = strdupz(value);
1568 + }
1569 + else if(!strcmp(name, "NETDATA_INSTANCE_CLOUD_INSTANCE_REGION")){
1570 + freez(system_info->cloud_instance_region);
1571 + system_info->cloud_instance_region = strdupz(value);
1572 + }
1573 else if(!strcmp(name, "NETDATA_CONTAINER_OS_NAME")){
1574 freez(system_info->container_os_name);
1575 system_info->container_os_name = strdupz(value);
streaming/sender.c
+6
@@ -301,6 +301,9 @@ if(!s->rrdpush_compression)
301 "&ml_enabled=%d"
302 "&tags=%s"
303 "&ver=%d"
304 + "&NETDATA_INSTANCE_CLOUD_TYPE=%s"
305 + "&NETDATA_INSTANCE_CLOUD_INSTANCE_TYPE=%s"
306 + "&NETDATA_INSTANCE_CLOUD_INSTANCE_REGION=%s"
307 "&NETDATA_SYSTEM_OS_NAME=%s"
308 "&NETDATA_SYSTEM_OS_ID=%s"
309 "&NETDATA_SYSTEM_OS_ID_LIKE=%s"
@@ -343,6 +346,9 @@ if(!s->rrdpush_compression)
346 , host->system_info->ml_enabled
347 , (host->tags) ? host->tags : ""
348 , s->version
349 + , (host->system_info->cloud_provider_type) ? host->system_info->cloud_provider_type : ""
350 + , (host->system_info->cloud_instance_type) ? host->system_info->cloud_instance_type : ""
351 + , (host->system_info->cloud_instance_region) ? host->system_info->cloud_instance_region : ""
352 , se.os_name
353 , se.os_id
354 , (host->system_info->host_os_id_like) ? host->system_info->host_os_id_like : ""
web/api/web_api_v1.c
+7
@@ -982,6 +982,13 @@ inline int web_client_api_request_v1_info_fill_buffer(RRDHOST *host, BUFFER *wb)
982 buffer_sprintf(wb, "\t\"container\": \"%s\",\n", (host->system_info->container) ? host->system_info->container : "");
983 buffer_sprintf(wb, "\t\"container_detection\": \"%s\",\n", (host->system_info->container_detection) ? host->system_info->container_detection : "");
984
985 + if (host->system_info->cloud_provider_type)
986 + buffer_sprintf(wb, "\t\"cloud_provider_type\": \"%s\",\n", host->system_info->cloud_provider_type);
987 + if (host->system_info->cloud_instance_type)
988 + buffer_sprintf(wb, "\t\"cloud_instance_type\": \"%s\",\n", host->system_info->cloud_instance_type);
989 + if (host->system_info->cloud_instance_region)
990 + buffer_sprintf(wb, "\t\"cloud_instance_region\": \"%s\",\n", host->system_info->cloud_instance_region);
991 +
992 buffer_strcat(wb, "\t\"host_labels\": {\n");
993 host_labels2json(host, wb, 2);
994 buffer_strcat(wb, "\t},\n");