From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <m.frank@proxmox.com>
Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256)
 (No client certificate requested)
 by lists.proxmox.com (Postfix) with ESMTPS id B043563CD9
 for <pve-devel@lists.proxmox.com>; Thu, 27 Jan 2022 12:14:19 +0100 (CET)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
 by firstgate.proxmox.com (Proxmox) with ESMTP id 9E62E21E20
 for <pve-devel@lists.proxmox.com>; Thu, 27 Jan 2022 12:13:49 +0100 (CET)
Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com
 [94.136.29.106])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits))
 (No client certificate requested)
 by firstgate.proxmox.com (Proxmox) with ESMTPS id D784C21E17
 for <pve-devel@lists.proxmox.com>; Thu, 27 Jan 2022 12:13:48 +0100 (CET)
Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1])
 by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 6EFF746C6F
 for <pve-devel@lists.proxmox.com>; Thu, 27 Jan 2022 12:13:48 +0100 (CET)
From: markus frank <m.frank@proxmox.com>
To: pve-devel@lists.proxmox.com
Date: Thu, 27 Jan 2022 12:13:18 +0100
Message-Id: <20220127111318.36615-1-m.frank@proxmox.com>
X-Mailer: git-send-email 2.30.2
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
X-SPAM-LEVEL: Spam detection results:  0
 BAYES_00                 -1.9 Bayes spam probability is 0 to 1%
 KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment
 SPF_HELO_NONE           0.001 SPF: HELO does not publish an SPF Record
 SPF_PASS               -0.001 SPF: sender matches SPF record
Subject: [pve-devel] [PATCH manager] fix #3815: influxdb vmname should
 always be a string
X-BeenThere: pve-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Proxmox VE development discussion <pve-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pve-devel/>
List-Post: <mailto:pve-devel@lists.proxmox.com>
List-Help: <mailto:pve-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=subscribe>
X-List-Received-Date: Thu, 27 Jan 2022 11:14:19 -0000

InfluxDB interprets the vmname 66601 as a number and the vmname vm42 as a String. This leads to problematic metrics, that will be dropped by influxdb.
To change that I added a $quoted hashmap (simular to $excluded) to quote a value. In this case the value of name.

Signed-off-by: markus frank <m.frank@proxmox.com>
---
 PVE/Status/InfluxDB.pm | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)

diff --git a/PVE/Status/InfluxDB.pm b/PVE/Status/InfluxDB.pm
index def7e2fd..f49feac4 100644
--- a/PVE/Status/InfluxDB.pm
+++ b/PVE/Status/InfluxDB.pm
@@ -116,7 +116,7 @@ sub update_qemu_status {
     $object =~ s/\s/\\ /g;
 
     # VMID is already added in base $object above, so exclude it from being re-added
-    build_influxdb_payload($class, $txn, $data, $ctime, $object, { 'vmid' => 1 });
+    build_influxdb_payload($class, $txn, $data, $ctime, $object, { 'vmid' => 1 }, { 'name' => 1 });
 }
 
 sub update_lxc_status {
@@ -131,7 +131,7 @@ sub update_lxc_status {
     $object =~ s/\s/\\ /g;
 
     # VMID is already added in base $object above, so exclude it from being re-added
-    build_influxdb_payload($class, $txn, $data, $ctime, $object, { 'vmid' => 1 });
+    build_influxdb_payload($class, $txn, $data, $ctime, $object, { 'vmid' => 1 }, { 'name' => 1 });
 }
 
 sub update_storage_status {
@@ -274,7 +274,7 @@ sub test_connection {
 }
 
 sub build_influxdb_payload {
-    my ($class, $txn, $data, $ctime, $tags, $excluded, $measurement, $instance) = @_;
+    my ($class, $txn, $data, $ctime, $tags, $excluded, $quoted, $measurement, $instance) = @_;
 
     my @values = ();
 
@@ -283,6 +283,10 @@ sub build_influxdb_payload {
 	my $value = $data->{$key};
 	next if !defined($value);
 
+	if (defined($quoted) && $quoted->{$key}){
+	    $value =~ s/\"/\\\"/g;
+            $value = "\"$value\"";
+        }
 	if (!ref($value) && $value ne '') {
 	    # value is scalar
 
@@ -293,9 +297,9 @@ sub build_influxdb_payload {
 	    # value is a hash
 
 	    if (!defined($measurement)) {
-		build_influxdb_payload($class, $txn, $value, $ctime, $tags, $excluded, $key);
+		build_influxdb_payload($class, $txn, $value, $ctime, $tags, $excluded, $quoted, $key);
 	    } elsif(!defined($instance)) {
-		build_influxdb_payload($class, $txn, $value, $ctime, $tags, $excluded, $measurement, $key);
+		build_influxdb_payload($class, $txn, $value, $ctime, $tags, $excluded, $quoted, $measurement, $key);
 	    } else {
 		push @values, get_recursive_values($value);
 	    }
-- 
2.30.2