Repository: DataDog/dd-agent

Issue:
[ssh] Multiple instances not differentiated
Any metrics sent through the ssh_check.py are not differentiated by instance. If multiple instances are running, the sftp.response_time metric will only be tagged with the host sending the metric: https://github.com/DataDog/dd-agent/blob/master/checks.d/ssh_check.py#L84



Discussion:
Thanks @handigarde .

Going to work on a fix right now.
It will go out with our 5.2.0 release that is planned for release in the next couple of weeks.

Thanks a lot for the feedback!

@remh update on this one?


Resolving patch:
diff --git a/checks.d/ssh_check.py b/checks.d/ssh_check.py
--- a/checks.d/ssh_check.py
+++ b/checks.d/ssh_check.py
@@ -45,6 +45,7 @@ def _load_conf(self, instance):
 
     def check(self, instance):
         conf = self._load_conf(instance)
+        tags = ["instance:{0}-{1}".format(conf.host, conf.port)] 
 
         try:
             private_key = paramiko.RSAKey.from_private_key_file (conf.private_key_file)
@@ -60,15 +61,19 @@ def check(self, instance):
         exception_message = None
         #Service Availability to check status of SSH
         try:
-            client.connect(conf.host, port=conf.port, username=conf.username, password=conf.password, pkey=private_key)
-            self.service_check('ssh.can_connect', AgentCheck.OK, message=exception_message)
+            client.connect(conf.host, port=conf.port, username=conf.username,
+                password=conf.password, pkey=private_key)
+            self.service_check('ssh.can_connect', AgentCheck.OK,  tags=tags,
+                message=exception_message)
 
         except Exception as e:
             exception_message = str(e)
             status = AgentCheck.CRITICAL
-            self.service_check('ssh.can_connect', status, message=exception_message)
+            self.service_check('ssh.can_connect', status, tags=tags,
+                message=exception_message)
             if conf.sftp_check:
-                self.service_check('sftp.can_connect', status, message=exception_message)
+                self.service_check('sftp.can_connect', status, tags=tags,
+                    message=exception_message)
             raise Exception (e)
 
         #Service Availability to check status of SFTP
@@ -81,7 +86,7 @@ def check(self, instance):
                 status = AgentCheck.OK
                 end_time = time.time()
                 time_taken = end_time - start_time
-                self.gauge('sftp.response_time', time_taken)
+                self.gauge('sftp.response_time', time_taken, tags=tags)
 
             except Exception as e:
                 exception_message = str(e)
@@ -90,4 +95,5 @@ def check(self, instance):
             if exception_message is None:
                 exception_message = "No errors occured"
 
-            self.service_check('sftp.can_connect', status, message=exception_message)
+            self.service_check('sftp.can_connect', status, tags=tags,
+                message=exception_message)


Test patch:
diff --git a/tests/test_ssh_check.py b/tests/test_ssh_check.py
--- a/tests/test_ssh_check.py
+++ b/tests/test_ssh_check.py
@@ -47,6 +47,7 @@ def test_ssh(self):
         service = self.check.get_service_checks()
         self.assertEqual(service[0].get('status'), AgentCheck.OK)
         self.assertEqual(service[0].get('message'), None)
+        self.assertEqual(service[0].get('tags'), ["instance:io.smashthestack.org-22"])
 
         #Testing that bad authentication will raise exception
         self.assertRaises(Exception, self.check.check, config['instances'][1])