Skip to content
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

Merged
merged 15 commits into from
Sep 23, 2014
Merged

Arthur/ssh check #1117

merged 15 commits into from
Sep 23, 2014

Conversation

whatarthurcodes
Copy link
Contributor

Check meant to:

  1. Check the status of SFTP
    2.Check The response time

@LeoCavaille

import time
import paramiko as p
from checks import AgentCheck
import socket
Copy link
Member

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
@whatarthurcodes
Copy link
Contributor Author

@remh Please review


class CheckSSH(AgentCheck):

def _load_conf(self, instance):
Copy link

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)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exception_message = str(e)

@remh
Copy link

remh commented Sep 17, 2014

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)
Copy link

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

@remh
Copy link

remh commented Sep 18, 2014

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)
Copy link

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


class SshTestCase(unittest.TestCase):

def test_ssh(self):
Copy link

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 ?

whatarthurcodes added a commit that referenced this pull request Sep 23, 2014
@whatarthurcodes whatarthurcodes merged commit 97da242 into master Sep 23, 2014
@remh remh deleted the arthur/ssh-check branch September 29, 2014 21:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants