-
Notifications
You must be signed in to change notification settings - Fork 813
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Arthur/ssh check #1117
Arthur/ssh check #1117
Conversation
import time | ||
import paramiko as p | ||
from checks import AgentCheck | ||
import socket |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Concerning the imports we usually try to put them in 3 separate categories:
# stdlib
# 3p
# project
-refactored try cases in sftp_check to be more efficient -exception message modified to be none by default -file renamed to ssh check and sftp is now an option
@remh Please review |
|
||
class CheckSSH(AgentCheck): | ||
|
||
def _load_conf(self, instance): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can refactor and make them clearer by using namedtuples:
from collections import namedtuple
OPTIONS = [
('host', True, None, str),
('port', False, 22, int),
('username', True, None, str),
('password', False, None, str),
('private_key', False, None, str),
('sftp_check', False, True, bool),
]
Config = namedtuple('Config', [
'host',
'port',
'username',
'password',
'private_key',
'sftp_check',
]
)
def _load_conf(self, instance):
params = []
for option, required, default, expected_type in REQUIRED:
value = instance.get(option)
if required and not value or type(value) != expected_type :
raise Exception("Please specify a valid {0}".format(option))
if value is None or type(value) != expected_type:
self.log.debug("Bad or missing value for {0} parameter. Using default".format(option))
params.append(value)
return Config._make(params)
self.gauge('sftp.response_time', time_taken) | ||
|
||
except Exception as e: | ||
exception_message = "{0}".format(e) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
exception_message = str(e)
Looks good besides the few small comments! |
-load function that uses namedtuple -easier and cleaner to read -yaml file now commented with required and optional
return self.Config._make(params) | ||
|
||
def check(self, instance): | ||
host, port, username, password, private_key_file, sftp_check, add_missing_keys = self._load_conf(instance) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You shouldn't unpack the conf like that. It defeats the purpose of using a namedtuple.
You should do:
conf = self._load_conf(instance)
and then if you want to get the host you can just get it using:
conf.host
Last point, would it be possible to add a test for that check ? Maybe using http://sdf.org/ ? |
self.service_check('ssh.can_connect', status, message=exception_message) | ||
if conf.sftp_check: | ||
self.service_check('sftp.can_connect', status, message=exception_message) | ||
raise Exception (e) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can just do:
raise
-tests for SSH connection
|
||
class SshTestCase(unittest.TestCase): | ||
|
||
def test_ssh(self): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you configure your editors to have 4 spaces indentation instead of tabs ?
Check meant to:
2.Check The response time
@LeoCavaille