From 67cb54f57e6f17ef780967a77f35dc051c0ca451 Mon Sep 17 00:00:00 2001 From: Jean-Christophe VASSORT Date: Sat, 3 Dec 2016 10:15:26 +0100 Subject: [PATCH 1/5] Add SSH class This class allows you to use native SSH in replacement of python Paramiko module. Never forget you have to populate a ~/.ssh/config file with at least SSH multiplexing params: ControlMaster auto ControlPath ~/.ssh/sockets/%r@%h:%p ControlPersist 600 http://blog.scottlowe.org/2015/12/11/using-ssh-multiplexing/ --- schecks.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/schecks.py b/schecks.py index b86daac..47ace44 100644 --- a/schecks.py +++ b/schecks.py @@ -218,3 +218,38 @@ def exit(self): else: print self.output sys.exit(self.exit_code) + +class SSH(object): + def __init__(self, host, *args): + """ + Never forget you have to populate a ~/.ssh/config file with at least SSH multiplexing params: + ControlMaster auto + ControlPath ~/.ssh/sockets/%r@%h:%p + ControlPersist 600 + http://blog.scottlowe.org/2015/12/11/using-ssh-multiplexing/ + + subprocess.call is used in __init__ to create SSH socket + subprocess.call runs the command and waits for it to complete + http://stackoverflow.com/questions/8077868/python-subprocess-popen-and-subprocess-call-hang?answertab=votes#tab-top + """ + self.host = host + process = subprocess.call(["ssh", self.host, 'exit']) + + def exec_command(self, cmd): + try: + process = subprocess.Popen(["ssh", self.host, cmd], + shell=False, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE) + stdout, stderr = process.communicate() + except OSError, exp: + stderr = exp.__str__() + stdout = stdin = '' + return '',stdout.splitlines(),stderr.splitlines() + + def close(self): + """ + This close method do nothing, it has been created only for compatibility + with Paramiko in checks provided by this module. + """ + return True From a27d867d268465751b11e87e646b058f0462c121 Mon Sep 17 00:00:00 2001 From: Jean-Christophe VASSORT Date: Sun, 4 Dec 2016 07:14:18 +0100 Subject: [PATCH 2/5] Use get_parser from schecks module --- check_cpu_stats_by_ssh.py | 16 +--------------- check_kernel_stats_by_ssh.py | 20 +------------------- check_memory_by_ssh.py | 15 +-------------- check_net_stats_by_ssh.py | 16 +--------------- check_nfs_stats_by_ssh.py | 15 +-------------- check_ntp_sync_by_ssh.py | 15 +-------------- check_processes_by_ssh.py | 15 +-------------- check_ro_filesystem_by_ssh.py | 17 +---------------- check_ssh_connexion.py | 18 +----------------- check_ssh_proxy_check.py | 14 +------------- check_tcp_states_by_ssh.py | 16 +--------------- check_uptime_by_ssh.py | 15 +-------------- 12 files changed, 12 insertions(+), 180 deletions(-) diff --git a/check_cpu_stats_by_ssh.py b/check_cpu_stats_by_ssh.py index 0d9ccf7..68522b8 100755 --- a/check_cpu_stats_by_ssh.py +++ b/check_cpu_stats_by_ssh.py @@ -103,21 +103,7 @@ def get_mpstat(client): -parser = optparse.OptionParser( - "%prog [options]", version="%prog " + VERSION) -parser.add_option('-H', '--hostname', - dest="hostname", help='Hostname to connect to') -parser.add_option('-p', '--port', - dest="port", type="int", default=22, - help='SSH port to connect to. Default : 22') -parser.add_option('-i', '--ssh-key', - dest="ssh_key_file", - help='SSH key file to use. By default will take ~/.ssh/id_rsa.') -parser.add_option('-u', '--user', - dest="user", help='remote use to use. By default shinken.') -parser.add_option('-P', '--passphrase', - dest="passphrase", help='SSH key passphrase. By default will use void') - +parser = schecks.get_parser() if __name__ == '__main__': # Ok first job : parse args diff --git a/check_kernel_stats_by_ssh.py b/check_kernel_stats_by_ssh.py index 91fd39d..bc1c8e5 100755 --- a/check_kernel_stats_by_ssh.py +++ b/check_kernel_stats_by_ssh.py @@ -121,25 +121,7 @@ def get_kernel_stats(client): return diff, stats - - - - -parser = optparse.OptionParser( - "%prog [options]", version="%prog " + VERSION) -parser.add_option('-H', '--hostname', - dest="hostname", help='Hostname to connect to') -parser.add_option('-p', '--port', - dest="port", type="int", default=22, - help='SSH port to connect to. Default : 22') -parser.add_option('-i', '--ssh-key', - dest="ssh_key_file", - help='SSH key file to use. By default will take ~/.ssh/id_rsa.') -parser.add_option('-u', '--user', - dest="user", help='remote use to use. By default shinken.') -parser.add_option('-P', '--passphrase', - dest="passphrase", help='SSH key passphrase. By default will use void') - +parser = schecks.get_parser() if __name__ == '__main__': # Ok first job : parse args diff --git a/check_memory_by_ssh.py b/check_memory_by_ssh.py index 9b5d665..682f54e 100755 --- a/check_memory_by_ssh.py +++ b/check_memory_by_ssh.py @@ -80,20 +80,7 @@ def get_meminfo(client): -parser = optparse.OptionParser( - "%prog [options]", version="%prog " + VERSION) -parser.add_option('-H', '--hostname', - dest="hostname", help='Hostname to connect to') -parser.add_option('-p', '--port', - dest="port", type="int", default=22, - help='SSH port to connect to. Default : 22') -parser.add_option('-i', '--ssh-key', - dest="ssh_key_file", - help='SSH key file to use. By default will take ~/.ssh/id_rsa.') -parser.add_option('-u', '--user', - dest="user", help='remote use to use. By default shinken.') -parser.add_option('-P', '--passphrase', - dest="passphrase", help='SSH key passphrase. By default will use void') +parser = schecks.get_parser() parser.add_option('-m', '--measurement', dest="measurement",action="store_true",default=False, help='Measurement in absolute value of the memory behavior. Absolute value ' diff --git a/check_net_stats_by_ssh.py b/check_net_stats_by_ssh.py index b6e3ad3..fc5bb51 100755 --- a/check_net_stats_by_ssh.py +++ b/check_net_stats_by_ssh.py @@ -127,21 +127,7 @@ def get_net_stats(client): -parser = optparse.OptionParser( - "%prog [options]", version="%prog " + VERSION) -parser.add_option('-H', '--hostname', - dest="hostname", help='Hostname to connect to') -parser.add_option('-p', '--port', - dest="port", type="int", default=22, - help='SSH port to connect to. Default : 22') -parser.add_option('-i', '--ssh-key', - dest="ssh_key_file", - help='SSH key file to use. By default will take ~/.ssh/id_rsa.') -parser.add_option('-u', '--user', - dest="user", help='remote use to use. By default shinken.') -parser.add_option('-P', '--passphrase', - dest="passphrase", - help='SSH key passphrase. By default will use void') +parser = schecks.get_parser() parser.add_option('-w', '--warning', dest="warning", help='Warning value for physical used memory. In percent. Default : 75%') diff --git a/check_nfs_stats_by_ssh.py b/check_nfs_stats_by_ssh.py index 59945c7..7dc8547 100755 --- a/check_nfs_stats_by_ssh.py +++ b/check_nfs_stats_by_ssh.py @@ -142,20 +142,7 @@ def get_nfs_stats(client): -parser = optparse.OptionParser( - "%prog [options]", version="%prog " + VERSION) -parser.add_option('-H', '--hostname', - dest="hostname", help='Hostname to connect to') -parser.add_option('-p', '--port', - dest="port", type="int", default=22, - help='SSH port to connect to. Default : 22') -parser.add_option('-i', '--ssh-key', - dest="ssh_key_file", - help='SSH key file to use. By default will take ~/.ssh/id_rsa.') -parser.add_option('-u', '--user', - dest="user", help='remote use to use. By default shinken.') -parser.add_option('-P', '--passphrase', - dest="passphrase", help='SSH key passphrase. By default will use void') +parser = schecks.get_parser() parser.add_option('-w', '--warning', dest="warning", help='Warning value for physical used memory. In percent. Default : 75%') diff --git a/check_ntp_sync_by_ssh.py b/check_ntp_sync_by_ssh.py index e687be0..7c7393d 100755 --- a/check_ntp_sync_by_ssh.py +++ b/check_ntp_sync_by_ssh.py @@ -138,20 +138,7 @@ def get_chrony_sync(client): -parser = optparse.OptionParser( - "%prog [options]", version="%prog " + VERSION) -parser.add_option('-H', '--hostname', - dest="hostname", help='Hostname to connect to') -parser.add_option('-p', '--port', - dest="port", type="int", default=22, - help='SSH port to connect to. Default : 22') -parser.add_option('-i', '--ssh-key', - dest="ssh_key_file", - help='SSH key file to use. By default will take ~/.ssh/id_rsa.') -parser.add_option('-u', '--user', - dest="user", help='remote use to use. By default shinken.') -parser.add_option('-P', '--passphrase', - dest="passphrase", help='SSH key passphrase. By default will use void') +parser = schecks.get_parser() parser.add_option('-w', '--warning', dest="warning", help='Warning delay for ntp, like 10. couple delay,offset value for chrony ' diff --git a/check_processes_by_ssh.py b/check_processes_by_ssh.py index 3e84c78..10a14dc 100755 --- a/check_processes_by_ssh.py +++ b/check_processes_by_ssh.py @@ -91,20 +91,7 @@ def get_processes(client): -parser = optparse.OptionParser( - "%prog [options]", version="%prog " + VERSION) -parser.add_option('-H', '--hostname', - dest="hostname", help='Hostname to connect to') -parser.add_option('-p', '--port', - dest="port", type="int", default=22, - help='SSH port to connect to. Default : 22') -parser.add_option('-i', '--ssh-key', - dest="ssh_key_file", - help='SSH key file to use. By default will take ~/.ssh/id_rsa.') -parser.add_option('-u', '--user', - dest="user", help='remote use to use. By default shinken.') -parser.add_option('-P', '--passphrase', - dest="passphrase", help='SSH key passphrase. By default will use void') +parser = schecks.get_parser() parser.add_option('-w', '--warning', dest="warning", help='Warning value for RSS used memory. In MB. Default : 100') diff --git a/check_ro_filesystem_by_ssh.py b/check_ro_filesystem_by_ssh.py index 2e58f17..a72277e 100755 --- a/check_ro_filesystem_by_ssh.py +++ b/check_ro_filesystem_by_ssh.py @@ -79,22 +79,7 @@ def get_fs(client): -parser = optparse.OptionParser( - "%prog [options]", version="%prog " + VERSION) -parser.add_option('-H', '--hostname', - dest="hostname", help='Hostname to connect to') -parser.add_option('-p', '--port', - dest="port", type="int", default=22, - help='SSH port to connect to. Default : 22') -parser.add_option('-i', '--ssh-key', - dest="ssh_key_file", - help='SSH key file to use. By default will take ~/.ssh/id_rsa.') -parser.add_option('-u', '--user', - dest="user", - help='remote use to use. By default shinken.') -parser.add_option('-P', '--passphrase', - dest="passphrase", - help='SSH key passphrase. By default will use void') +parser = schecks.get_parser() parser.add_option('-e', '--exclude', action="append", dest="exclude", diff --git a/check_ssh_connexion.py b/check_ssh_connexion.py index a3a8df4..66fc9c6 100755 --- a/check_ssh_connexion.py +++ b/check_ssh_connexion.py @@ -66,23 +66,7 @@ def get_echo(client): -parser = optparse.OptionParser( - "%prog [options]", version="%prog " + VERSION) -parser.add_option('-H', '--hostname', - dest="hostname", - help='Hostname to connect to') -parser.add_option('-p', '--port', - dest="port", type="int", default=22, - help='SSH port to connect to. Default : 22') -parser.add_option('-i', '--ssh-key', - dest="ssh_key_file", - help='SSH key file to use. By default will take ~/.ssh/id_rsa.') -parser.add_option('-u', '--user', - dest="user", - help='remote use to use. By default shinken.') -parser.add_option('-P', '--passphrase', - dest="passphrase", - help='SSH key passphrase. By default will use void') +parser = schecks.get_parser() parser.add_option('-w', '--warning', dest="warning", help='Warning value for physical used memory. In percent. Default : 75%') diff --git a/check_ssh_proxy_check.py b/check_ssh_proxy_check.py index a1a38ca..91c283a 100755 --- a/check_ssh_proxy_check.py +++ b/check_ssh_proxy_check.py @@ -67,19 +67,7 @@ def execute_check(client, check_path): return lines[0].strip() -parser = optparse.OptionParser( - "%prog [options]", version="%prog " + VERSION) -parser.add_option('-H', '--hostname', - dest="hostname", help='Hostname to connect to') -parser.add_option('-p', '--port', - dest="port", type="int", default=22, - help='SSH port to connect to. Default : 22') -parser.add_option('-i', '--ssh-key', - dest="ssh_key_file", help='SSH key file to use. By default will take ~/.ssh/id_rsa.') -parser.add_option('-u', '--user', - dest="user", help='remote use to use. By default shinken.') -parser.add_option('-P', '--passphrase', - dest="passphrase", help='SSH key passphrase. By default will use void') +parser = schecks.get_parser() parser.add_option('-r', '--check_path', dest="check_path", help='Path of the remote perfdata check to execute') diff --git a/check_tcp_states_by_ssh.py b/check_tcp_states_by_ssh.py index a506675..4d34a88 100755 --- a/check_tcp_states_by_ssh.py +++ b/check_tcp_states_by_ssh.py @@ -71,21 +71,7 @@ def get_tcp_states(client): -parser = optparse.OptionParser( - "%prog [options]", version="%prog " + VERSION) -parser.add_option('-H', '--hostname', - dest="hostname", help='Hostname to connect to') -parser.add_option('-p', '--port', - dest="port", type="int", default=22, - help='SSH port to connect to. Default : 22') -parser.add_option('-i', '--ssh-key', - dest="ssh_key_file", - help='SSH key file to use. By default will take ~/.ssh/id_rsa.') -parser.add_option('-u', '--user', - dest="user", help='remote use to use. By default shinken.') -parser.add_option('-P', '--passphrase', - dest="passphrase", help='SSH key passphrase. By default will use void') - +parser = schecks.get_parser() if __name__ == '__main__': diff --git a/check_uptime_by_ssh.py b/check_uptime_by_ssh.py index 6d4c9c6..53b8dae 100755 --- a/check_uptime_by_ssh.py +++ b/check_uptime_by_ssh.py @@ -67,21 +67,8 @@ def get_uptime(client): -parser = optparse.OptionParser( - "%prog [options]", version="%prog " + VERSION) -parser.add_option('-H', '--hostname', - dest="hostname", help='Hostname to connect to') -parser.add_option('-p', '--port', - dest="port", type="int", default=22, - help='SSH port to connect to. Default : 22') -parser.add_option('-i', '--ssh-key', - dest="ssh_key_file", - help='SSH key file to use. By default will take ~/.ssh/id_rsa.') -parser.add_option('-u', '--user', - dest="user", help='remote use to use. By default shinken.') -parser.add_option('-P', '--passphrase', - dest="passphrase", help='SSH key passphrase. By default will use void') +parser = schecks.get_parser() parser.add_option('-c', '--critical', dest="critical", help='Critical value for uptime in seconds. Less means critical error. Default : 3600') From d393c63ae56a25caf1a6ef048ac6002e0538c3c0 Mon Sep 17 00:00:00 2001 From: Jean-Christophe VASSORT Date: Sun, 4 Dec 2016 19:40:36 +0100 Subject: [PATCH 3/5] Add ssh authentication param if checks are launched with '-a ssh' param, native ssh will be used instead of paramiko module --- schecks.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/schecks.py b/schecks.py index 47ace44..edd8146 100644 --- a/schecks.py +++ b/schecks.py @@ -73,11 +73,13 @@ def get_client(opts): passphrase = opts.passphrase # Ok now connect, and try to get values for memory - client = connect(hostname, port, ssh_key_file, passphrase, user) + client = connect(hostname, port, ssh_key_file, passphrase, user, ssh_auth=opts.ssh_auth) return client -def connect(hostname, port, ssh_key_file, passphrase, user): +def connect(hostname, port, ssh_key_file, passphrase, user, ssh_auth='paramiko'): + if ssh_auth == 'ssh': + return SSH(hostname) # If we are in a localhost case, don't play with ssh if is_local(hostname): return LocalExec() @@ -151,6 +153,8 @@ def get_warn_crit(s_warn, s_crit): def get_parser(): parser = optparse.OptionParser( "%prog [options]", version="%prog " + str(VERSION)) + parser.add_option('-a', '--auth', default='paramiko', + dest="ssh_auth", help='SSH Authentication. Can be paramiko or ssh') parser.add_option('-H', '--hostname', default='', dest="hostname", help='Hostname to connect to') parser.add_option('-p', '--port', From 5296907c8b5a7f5ea38cca3df4f2dde27c7b6f18 Mon Sep 17 00:00:00 2001 From: Jean-Christophe VASSORT Date: Sun, 4 Dec 2016 19:42:18 +0100 Subject: [PATCH 4/5] Add connect() ssh_auth param to all checks --- check_cpu_stats_by_ssh.py | 2 +- check_kernel_stats_by_ssh.py | 2 +- check_load_average_by_ssh.py | 2 +- check_memory_by_ssh.py | 2 +- check_net_stats_by_ssh.py | 2 +- check_nfs_stats_by_ssh.py | 2 +- check_ntp_sync_by_ssh.py | 2 +- check_processes_by_ssh.py | 2 +- check_ro_filesystem_by_ssh.py | 2 +- check_ssh_connexion.py | 2 +- check_ssh_proxy_check.py | 2 +- check_tcp_states_by_ssh.py | 2 +- check_uptime_by_ssh.py | 2 +- 13 files changed, 13 insertions(+), 13 deletions(-) diff --git a/check_cpu_stats_by_ssh.py b/check_cpu_stats_by_ssh.py index 68522b8..f3583d7 100755 --- a/check_cpu_stats_by_ssh.py +++ b/check_cpu_stats_by_ssh.py @@ -119,7 +119,7 @@ def get_mpstat(client): passphrase = opts.passphrase or '' # Ok now connect, and try to get values for memory - client = schecks.connect(hostname, port, ssh_key_file, passphrase, user) + client = schecks.connect(hostname, port, ssh_key_file, passphrase, user, ssh_auth=opts.ssh_auth) stats = get_mpstat(client) # Maybe we failed at getting data diff --git a/check_kernel_stats_by_ssh.py b/check_kernel_stats_by_ssh.py index bc1c8e5..cbc8c50 100755 --- a/check_kernel_stats_by_ssh.py +++ b/check_kernel_stats_by_ssh.py @@ -137,7 +137,7 @@ def get_kernel_stats(client): passphrase = opts.passphrase or '' # Ok now connect, and try to get values for memory - client = schecks.connect(hostname, port, ssh_key_file, passphrase, user) + client = schecks.connect(hostname, port, ssh_key_file, passphrase, user, ssh_auth=opts.ssh_auth) diff, stats = get_kernel_stats(client) # Maybe we failed at getting data diff --git a/check_load_average_by_ssh.py b/check_load_average_by_ssh.py index a7ae909..ac30433 100755 --- a/check_load_average_by_ssh.py +++ b/check_load_average_by_ssh.py @@ -123,7 +123,7 @@ def get_load(client): critical = [float(v) for v in s_critical.split(',')] # Ok now connect, and try to get values for memory - client = schecks.connect(hostname, port, ssh_key_file, passphrase, user) + client = schecks.connect(hostname, port, ssh_key_file, passphrase, user, ssh_auth=opts.ssh_auth) load1, load5, load15, nb_cpus = get_load(client) # Two cases : cpu_based_load or not. For CPU the real warning is based on warning*nb_cpu diff --git a/check_memory_by_ssh.py b/check_memory_by_ssh.py index 682f54e..37e6fc7 100755 --- a/check_memory_by_ssh.py +++ b/check_memory_by_ssh.py @@ -117,7 +117,7 @@ def get_meminfo(client): warning, critical = schecks.get_warn_crit(s_warning, s_critical) # Ok now connect, and try to get values for memory - client = schecks.connect(hostname, port, ssh_key_file, passphrase, user) + client = schecks.connect(hostname, port, ssh_key_file, passphrase, user, ssh_auth=opts.ssh_auth) total, used, free, shared, buffed, cached, swap_total, swap_used, swap_free = get_meminfo(client) # Maybe we failed at getting data diff --git a/check_net_stats_by_ssh.py b/check_net_stats_by_ssh.py index fc5bb51..34f11a5 100755 --- a/check_net_stats_by_ssh.py +++ b/check_net_stats_by_ssh.py @@ -163,7 +163,7 @@ def get_net_stats(client): excluded_interfaces = opts.exclude # Ok now connect, and try to get values for memory - client = schecks.connect(hostname, port, ssh_key_file, passphrase, user) + client = schecks.connect(hostname, port, ssh_key_file, passphrase, user, ssh_auth=opts.ssh_auth) diff, stats = get_net_stats(client) # Maybe we failed at getting data diff --git a/check_nfs_stats_by_ssh.py b/check_nfs_stats_by_ssh.py index 7dc8547..754598c 100755 --- a/check_nfs_stats_by_ssh.py +++ b/check_nfs_stats_by_ssh.py @@ -171,7 +171,7 @@ def get_nfs_stats(client): warning, critical = schecks.get_warn_crit(s_warning, s_critical) # Ok now connect, and try to get values for memory - client = schecks.connect(hostname, port, ssh_key_file, passphrase, user) + client = schecks.connect(hostname, port, ssh_key_file, passphrase, user, ssh_auth=opts.ssh_auth) diff, stats = get_nfs_stats(client) # Maybe we failed at getting data diff --git a/check_ntp_sync_by_ssh.py b/check_ntp_sync_by_ssh.py index 7c7393d..3ea0b45 100755 --- a/check_ntp_sync_by_ssh.py +++ b/check_ntp_sync_by_ssh.py @@ -194,7 +194,7 @@ def get_chrony_sync(client): # Ok now connect, and try to get values for memory - client = schecks.connect(hostname, port, ssh_key_file, passphrase, user) + client = schecks.connect(hostname, port, ssh_key_file, passphrase, user, ssh_auth=opts.ssh_auth) if not chrony: ref_delay = get_ntp_sync(client) diff --git a/check_processes_by_ssh.py b/check_processes_by_ssh.py index 10a14dc..e4c683a 100755 --- a/check_processes_by_ssh.py +++ b/check_processes_by_ssh.py @@ -134,7 +134,7 @@ def get_processes(client): # Ok now connect, and try to get values for memory - client = schecks.connect(hostname, port, ssh_key_file, passphrase, user) + client = schecks.connect(hostname, port, ssh_key_file, passphrase, user, ssh_auth=opts.ssh_auth) pss = get_processes(client) # Maybe we failed at getting data diff --git a/check_ro_filesystem_by_ssh.py b/check_ro_filesystem_by_ssh.py index a72277e..3affcd0 100755 --- a/check_ro_filesystem_by_ssh.py +++ b/check_ro_filesystem_by_ssh.py @@ -108,7 +108,7 @@ def get_fs(client): excluded_mountpoint = tuple() if opts.exclude is None else tuple(opts.exclude) # Ok now connect, and try to get values for memory - client = schecks.connect(hostname, port, ssh_key_file, passphrase, user) + client = schecks.connect(hostname, port, ssh_key_file, passphrase, user, ssh_auth=opts.ssh_auth) bad_fs = get_fs(client) if len(bad_fs) == 0: diff --git a/check_ssh_connexion.py b/check_ssh_connexion.py index 66fc9c6..ee25450 100755 --- a/check_ssh_connexion.py +++ b/check_ssh_connexion.py @@ -90,7 +90,7 @@ def get_echo(client): passphrase = opts.passphrase or '' # Ok now connect, and try to get values for memory - client = schecks.connect(hostname, port, ssh_key_file, passphrase, user) + client = schecks.connect(hostname, port, ssh_key_file, passphrase, user, ssh_auth=opts.ssh_auth) is_ok = get_echo(client) if not is_ok: diff --git a/check_ssh_proxy_check.py b/check_ssh_proxy_check.py index 91c283a..7ad7b46 100755 --- a/check_ssh_proxy_check.py +++ b/check_ssh_proxy_check.py @@ -93,7 +93,7 @@ def execute_check(client, check_path): passphrase = opts.passphrase or '' # Ok now connect, and try to get values for memory - client = schecks.connect(hostname, port, ssh_key_file, passphrase, user) + client = schecks.connect(hostname, port, ssh_key_file, passphrase, user, ssh_auth=opts.ssh_auth) result = execute_check(client, check_path) print result diff --git a/check_tcp_states_by_ssh.py b/check_tcp_states_by_ssh.py index 4d34a88..1330922 100755 --- a/check_tcp_states_by_ssh.py +++ b/check_tcp_states_by_ssh.py @@ -88,7 +88,7 @@ def get_tcp_states(client): passphrase = opts.passphrase or '' # Ok now connect, and try to get values for memory - client = schecks.connect(hostname, port, ssh_key_file, passphrase, user) + client = schecks.connect(hostname, port, ssh_key_file, passphrase, user, ssh_auth=opts.ssh_auth) states = get_tcp_states(client) # Thanks the "/proc and /sys" book :) diff --git a/check_uptime_by_ssh.py b/check_uptime_by_ssh.py index 53b8dae..086c2a1 100755 --- a/check_uptime_by_ssh.py +++ b/check_uptime_by_ssh.py @@ -95,7 +95,7 @@ def get_uptime(client): _, critical = schecks.get_warn_crit(s_warning, s_critical) # Ok now connect, and try to get values for memory - client = schecks.connect(hostname, port, ssh_key_file, passphrase, user) + client = schecks.connect(hostname, port, ssh_key_file, passphrase, user, ssh_auth=opts.ssh_auth) uptime = get_uptime(client) # Two cases : cpu_based_load or not. For CPU the real warning is based on warning*nb_cpu From cf37f69563a87575c0e76f0935b2cded30e13f25 Mon Sep 17 00:00:00 2001 From: Jean-Christophe VASSORT Date: Tue, 6 Dec 2016 09:08:05 +0100 Subject: [PATCH 5/5] Replace deprecated optparse with argparse https://docs.python.org/2/library/optparse.html "Deprecated since version 2.7: The optparse module is deprecated and will not be developed further; development will continue with the argparse module." --- check_cpu_stats_by_ssh.py | 6 +----- check_disks_by_ssh.py | 11 +++++------ check_disks_stats_by_ssh.py | 4 +--- check_kernel_stats_by_ssh.py | 6 +----- check_linux.py | 2 -- check_load_average_by_ssh.py | 27 +++++---------------------- check_mdadm_by_ssh.py | 4 +--- check_memory_by_ssh.py | 14 +++++--------- check_net_stats_by_ssh.py | 12 ++++-------- check_nfs_stats_by_ssh.py | 10 +++------- check_ntp_sync_by_ssh.py | 14 +++++--------- check_processes_by_ssh.py | 14 +++++--------- check_ro_filesystem_by_ssh.py | 12 ++++-------- check_ssh_connexion.py | 10 +++------- check_ssh_proxy_check.py | 8 ++------ check_tcp_states_by_ssh.py | 6 +----- check_uptime_by_ssh.py | 7 ++----- checks/disks.py | 8 ++++---- schecks.py | 22 +++++++++++----------- 19 files changed, 63 insertions(+), 134 deletions(-) diff --git a/check_cpu_stats_by_ssh.py b/check_cpu_stats_by_ssh.py index f3583d7..eb9302e 100755 --- a/check_cpu_stats_by_ssh.py +++ b/check_cpu_stats_by_ssh.py @@ -26,7 +26,6 @@ import os import sys -import optparse # Ok try to load our directory to load the plugin utils. my_dir = os.path.dirname(__file__) @@ -106,10 +105,7 @@ def get_mpstat(client): parser = schecks.get_parser() if __name__ == '__main__': - # Ok first job : parse args - opts, args = parser.parse_args() - if args: - parser.error("Does not accept any argument.") + opts = parser.parse_args() port = opts.port hostname = opts.hostname or '' diff --git a/check_disks_by_ssh.py b/check_disks_by_ssh.py index dc11071..18c1530 100755 --- a/check_disks_by_ssh.py +++ b/check_disks_by_ssh.py @@ -125,24 +125,23 @@ def get_df(client): parser = schecks.get_parser() ## Specific options -parser.add_option('-w', '--warning', +parser.add_argument('-w', '--warning', dest="warning", help='Warning value for physical used memory. In percent. Default : 75%') -parser.add_option('-c', '--critical', +parser.add_argument('-c', '--critical', dest="critical", help='Critical value for physical used memory. In percent. Must be ' 'superior to warning value. Default : 90%') -parser.add_option('-m', '--mount-points', +parser.add_argument('-m', '--mount-points', dest="mounts", help='comma separated list of mountpoints to check. Default all mount ' 'points except if mounted in /dev, /sys and /run') -parser.add_option('-U', '--unit', +parser.add_argument('-U', '--unit', dest="unit", help='Unit of Disk Space. B, KB, GB, TB. Default : B') if __name__ == '__main__': - # Ok first job : parse args - opts, args = parser.parse_args() + opts = parser.parse_args() if opts.mounts: mounts = opts.mounts.split(',') diff --git a/check_disks_stats_by_ssh.py b/check_disks_stats_by_ssh.py index 99a3016..100be2a 100755 --- a/check_disks_stats_by_ssh.py +++ b/check_disks_stats_by_ssh.py @@ -31,7 +31,6 @@ ''' import os import sys -import optparse # Ok try to load our directory to load the plugin utils. my_dir = os.path.dirname(__file__) @@ -125,8 +124,7 @@ def get_disks_stats(client): parser = schecks.get_parser() if __name__ == '__main__': - # Ok first job : parse args - opts, args = parser.parse_args() + opts = parser.parse_args() # Ok now got an object that link to our destination client = schecks.get_client(opts) diff --git a/check_kernel_stats_by_ssh.py b/check_kernel_stats_by_ssh.py index cbc8c50..fc05dfb 100755 --- a/check_kernel_stats_by_ssh.py +++ b/check_kernel_stats_by_ssh.py @@ -31,7 +31,6 @@ ''' import os import sys -import optparse # Ok try to load our directory to load the plugin utils. my_dir = os.path.dirname(__file__) @@ -124,10 +123,7 @@ def get_kernel_stats(client): parser = schecks.get_parser() if __name__ == '__main__': - # Ok first job : parse args - opts, args = parser.parse_args() - if args: - parser.error("Does not accept any argument.") + opts = parser.parse_args() port = opts.port hostname = opts.hostname or '' diff --git a/check_linux.py b/check_linux.py index 510cd6c..b5fa38e 100755 --- a/check_linux.py +++ b/check_linux.py @@ -42,8 +42,6 @@ VERSION = "0.1" -import optparse - if __name__ == '__main__': modname = '' diff --git a/check_load_average_by_ssh.py b/check_load_average_by_ssh.py index ac30433..5919b40 100755 --- a/check_load_average_by_ssh.py +++ b/check_load_average_by_ssh.py @@ -30,7 +30,6 @@ ''' import os import sys -import optparse # Ok try to load our directory to load the plugin utils. my_dir = os.path.dirname(__file__) @@ -70,27 +69,14 @@ def get_load(client): -parser = optparse.OptionParser( - "%prog [options]", version="%prog " + VERSION) -parser.add_option('-H', '--hostname', - dest="hostname", help='Hostname to connect to') -parser.add_option('-p', '--port', - dest="port", type="int", default=22, - help='SSH port to connect to. Default : 22') -parser.add_option('-i', '--ssh-key', - dest="ssh_key_file", - help='SSH key file to use. By default will take ~/.ssh/id_rsa.') -parser.add_option('-u', '--user', - dest="user", help='remote use to use. By default shinken.') -parser.add_option('-P', '--passphrase', - dest="passphrase", help='SSH key passphrase. By default will use void') -parser.add_option('-w', '--warning', +parser = schecks.get_parser() +parser.add_argument('-w', '--warning', dest="warning", help='Warning value for load average, as 3 values, for 1m,5m,15m. Default : 1,1,1') -parser.add_option('-c', '--critical', +parser.add_argument('-c', '--critical', dest="critical", help='Critical value for load average, as 3 values, for 1m,5m,15m. Default : 2,2,2') -parser.add_option('-C', '--cpu-based', action='store_true', +parser.add_argument('-C', '--cpu-based', action='store_true', dest="cpu_based", help='Set the warning/critical number of cpu based values. For example ' '1,1,1 will warn if the load if over the number of CPUs. ' @@ -98,10 +84,7 @@ def get_load(client): if __name__ == '__main__': - # Ok first job : parse args - opts, args = parser.parse_args() - if args: - parser.error("Does not accept any argument.") + opts = parser.parse_args() port = opts.port hostname = opts.hostname or '' diff --git a/check_mdadm_by_ssh.py b/check_mdadm_by_ssh.py index 19c509f..bf55179 100755 --- a/check_mdadm_by_ssh.py +++ b/check_mdadm_by_ssh.py @@ -30,7 +30,6 @@ ''' import os import sys -import optparse # Ok try to load our directory to load the plugin utils. my_dir = os.path.dirname(__file__) @@ -118,8 +117,7 @@ def get_raid_status(client): parser = schecks.get_parser() if __name__ == '__main__': - # Ok first job : parse args - opts, args = parser.parse_args() + opts = parser.parse_args() # Ok now got an object that link to our destination client = schecks.get_client(opts) diff --git a/check_memory_by_ssh.py b/check_memory_by_ssh.py index 37e6fc7..8222587 100755 --- a/check_memory_by_ssh.py +++ b/check_memory_by_ssh.py @@ -31,7 +31,6 @@ ''' import os import sys -import optparse import base64 import subprocess @@ -81,28 +80,25 @@ def get_meminfo(client): parser = schecks.get_parser() -parser.add_option('-m', '--measurement', +parser.add_argument('-m', '--measurement', dest="measurement",action="store_true",default=False, help='Measurement in absolute value of the memory behavior. Absolute value ' 'currently can not be used as a check') -parser.add_option('-s', '--swap', +parser.add_argument('-s', '--swap', dest="swap",action="store_true",default=False, help='Enable swap value measurement. Swap value currently can not be used ' 'as a check') -parser.add_option('-w', '--warning', +parser.add_argument('-w', '--warning', dest="warning", help='Warning value for physical used memory. In percent. Default : 75%') -parser.add_option('-c', '--critical', +parser.add_argument('-c', '--critical', dest="critical", help='Critical value for physical used memory. In percent. Must be ' 'superior to warning value. Default : 90%') if __name__ == '__main__': - # Ok first job : parse args - opts, args = parser.parse_args() - if args: - parser.error("Does not accept any argument.") + opts = parser.parse_args() port = opts.port hostname = opts.hostname or '' diff --git a/check_net_stats_by_ssh.py b/check_net_stats_by_ssh.py index 34f11a5..2ef6af8 100755 --- a/check_net_stats_by_ssh.py +++ b/check_net_stats_by_ssh.py @@ -30,7 +30,6 @@ ''' import os import sys -import optparse import base64 import subprocess @@ -128,24 +127,21 @@ def get_net_stats(client): parser = schecks.get_parser() -parser.add_option('-w', '--warning', +parser.add_argument('-w', '--warning', dest="warning", help='Warning value for physical used memory. In percent. Default : 75%') -parser.add_option('-c', '--critical', +parser.add_argument('-c', '--critical', dest="critical", help='Critical value for physical used memory. In percent. Must be ' 'superior to warning value. Default : 90%') -parser.add_option('-e', '--exclude', +parser.add_argument('-e', '--exclude', action="append", dest="exclude", help='Interfaces to exclude. Can appear several time.') if __name__ == '__main__': - # Ok first job : parse args - opts, args = parser.parse_args() - if args: - parser.error("Does not accept any argument.") + opts = parser.parse_args() port = opts.port hostname = opts.hostname or '' diff --git a/check_nfs_stats_by_ssh.py b/check_nfs_stats_by_ssh.py index 754598c..47953ba 100755 --- a/check_nfs_stats_by_ssh.py +++ b/check_nfs_stats_by_ssh.py @@ -31,7 +31,6 @@ ''' import os import sys -import optparse import base64 import subprocess @@ -143,20 +142,17 @@ def get_nfs_stats(client): parser = schecks.get_parser() -parser.add_option('-w', '--warning', +parser.add_argument('-w', '--warning', dest="warning", help='Warning value for physical used memory. In percent. Default : 75%') -parser.add_option('-c', '--critical', +parser.add_argument('-c', '--critical', dest="critical", help='Critical value for physical used memory. In percent. Must be ' 'superior to warning value. Default : 90%') if __name__ == '__main__': - # Ok first job : parse args - opts, args = parser.parse_args() - if args: - parser.error("Does not accept any argument.") + opts = parser.parse_args() port = opts.port hostname = opts.hostname or '' diff --git a/check_ntp_sync_by_ssh.py b/check_ntp_sync_by_ssh.py index 3ea0b45..5fbbe57 100755 --- a/check_ntp_sync_by_ssh.py +++ b/check_ntp_sync_by_ssh.py @@ -30,7 +30,6 @@ ''' import os import sys -import optparse # Ok try to load our directory to load the plugin utils. my_dir = os.path.dirname(__file__) @@ -139,25 +138,22 @@ def get_chrony_sync(client): parser = schecks.get_parser() -parser.add_option('-w', '--warning', +parser.add_argument('-w', '--warning', dest="warning", help='Warning delay for ntp, like 10. couple delay,offset value for chrony ' '0.100,0.0025') -parser.add_option('-c', '--critical', +parser.add_argument('-c', '--critical', dest="critical", help='Warning delay for ntp, like 10. couple delay,offset value for chrony ' '0.150,0.005') -parser.add_option('-C', '--chrony', action='store_true', +parser.add_argument('-C', '--chrony', action='store_true', dest="chrony", help='check Chrony instead of ntpd') -parser.add_option('-n', '--ntpq', +parser.add_argument('-n', '--ntpq', dest="ntpq", help="remote ntpq bianry path") if __name__ == '__main__': - # Ok first job : parse args - opts, args = parser.parse_args() - if args: - parser.error("Does not accept any argument.") + opts = parser.parse_args() port = opts.port hostname = opts.hostname or '' diff --git a/check_processes_by_ssh.py b/check_processes_by_ssh.py index e4c683a..ced66c9 100755 --- a/check_processes_by_ssh.py +++ b/check_processes_by_ssh.py @@ -30,7 +30,6 @@ ''' import os import sys -import optparse import base64 import subprocess @@ -92,26 +91,23 @@ def get_processes(client): parser = schecks.get_parser() -parser.add_option('-w', '--warning', +parser.add_argument('-w', '--warning', dest="warning", help='Warning value for RSS used memory. In MB. Default : 100') -parser.add_option('-c', '--critical', +parser.add_argument('-c', '--critical', dest="critical", help='Critical value for RSS used memory. In MB. Must be superior to ' 'warning value. Default : 200') # Specific parameters -parser.add_option('-C', '--command', +parser.add_argument('-C', '--command', dest="command", help='Command name to match for the check') -parser.add_option('-S', '--sum', action='store_true', +parser.add_argument('-S', '--sum', action='store_true', dest="sum_all", help='Sum all consomtion of matched processes for the check') if __name__ == '__main__': - # Ok first job : parse args - opts, args = parser.parse_args() - if args: - parser.error("Does not accept any argument.") + opts = parser.parse_args() port = opts.port hostname = opts.hostname or '' diff --git a/check_ro_filesystem_by_ssh.py b/check_ro_filesystem_by_ssh.py index 3affcd0..cc5606f 100755 --- a/check_ro_filesystem_by_ssh.py +++ b/check_ro_filesystem_by_ssh.py @@ -31,7 +31,6 @@ ''' import os import sys -import optparse @@ -80,24 +79,21 @@ def get_fs(client): parser = schecks.get_parser() -parser.add_option('-e', '--exclude', +parser.add_argument('-e', '--exclude', action="append", dest="exclude", help='Mount point to exclude. Can appear several time.') -parser.add_option('-w', '--warning', +parser.add_argument('-w', '--warning', dest="warning", help='Warning value for physical used memory. In percent. Default : 75%') -parser.add_option('-c', '--critical', +parser.add_argument('-c', '--critical', dest="critical", help='Critical value for physical used memory. In percent. Must be ' 'superior to warning value. Default : 90%') if __name__ == '__main__': - # Ok first job : parse args - opts, args = parser.parse_args() - if args: - parser.error("Does not accept any argument.") + opts = parser.parse_args() port = opts.port hostname = opts.hostname or '' diff --git a/check_ssh_connexion.py b/check_ssh_connexion.py index ee25450..d05158a 100755 --- a/check_ssh_connexion.py +++ b/check_ssh_connexion.py @@ -31,7 +31,6 @@ ''' import os import sys -import optparse import base64 # Ok try to load our directory to load the plugin utils. @@ -67,20 +66,17 @@ def get_echo(client): parser = schecks.get_parser() -parser.add_option('-w', '--warning', +parser.add_argument('-w', '--warning', dest="warning", help='Warning value for physical used memory. In percent. Default : 75%') -parser.add_option('-c', '--critical', +parser.add_argument('-c', '--critical', dest="critical", help='Critical value for physical used memory. In percent. Must be ' 'superior to warning value. Default : 90%') if __name__ == '__main__': - # Ok first job : parse args - opts, args = parser.parse_args() - if args: - parser.error("Does not accept any argument.") + opts = parser.parse_args() port = opts.port hostname = opts.hostname or '' diff --git a/check_ssh_proxy_check.py b/check_ssh_proxy_check.py index 7ad7b46..9040c86 100755 --- a/check_ssh_proxy_check.py +++ b/check_ssh_proxy_check.py @@ -30,7 +30,6 @@ ''' import os import sys -import optparse import base64 import subprocess try: @@ -68,15 +67,12 @@ def execute_check(client, check_path): parser = schecks.get_parser() -parser.add_option('-r', '--check_path', +parser.add_argument('-r', '--check_path', dest="check_path", help='Path of the remote perfdata check to execute') if __name__ == '__main__': - # Ok first job : parse args - opts, args = parser.parse_args() - if args: - parser.error("Does not accept any argument.") + opts = parser.parse_args() hostname = opts.hostname if not hostname: diff --git a/check_tcp_states_by_ssh.py b/check_tcp_states_by_ssh.py index 1330922..6594068 100755 --- a/check_tcp_states_by_ssh.py +++ b/check_tcp_states_by_ssh.py @@ -29,7 +29,6 @@ ''' import os import sys -import optparse # Ok try to load our directory to load the plugin utils. my_dir = os.path.dirname(__file__) @@ -75,10 +74,7 @@ def get_tcp_states(client): if __name__ == '__main__': - # Ok first job : parse args - opts, args = parser.parse_args() - if args: - parser.error("Does not accept any argument.") + opts = parser.parse_args() port = opts.port hostname = opts.hostname or '' diff --git a/check_uptime_by_ssh.py b/check_uptime_by_ssh.py index 086c2a1..55c46cb 100755 --- a/check_uptime_by_ssh.py +++ b/check_uptime_by_ssh.py @@ -30,7 +30,6 @@ ''' import os import sys -import optparse # Ok try to load our directory to load the plugin utils. my_dir = os.path.dirname(__file__) @@ -69,7 +68,7 @@ def get_uptime(client): parser = schecks.get_parser() -parser.add_option('-c', '--critical', +parser.add_argument('-c', '--critical', dest="critical", help='Critical value for uptime in seconds. Less means critical error. Default : 3600') @@ -77,9 +76,7 @@ def get_uptime(client): if __name__ == '__main__': # Ok first job : parse args - opts, args = parser.parse_args() - if args: - parser.error("Does not accept any argument.") + opts = parser.parse_args() hostname = opts.hostname or '' port = opts.port diff --git a/checks/disks.py b/checks/disks.py index 8146f1a..ea3c782 100644 --- a/checks/disks.py +++ b/checks/disks.py @@ -111,18 +111,18 @@ def get_df(client): class Check(schecks.GenCheck): def fill_parser(self): - self.parser.add_option('-w', '--warning', + self.parser.add_argument('-w', '--warning', dest="warning", help='Warning value for physical used memory. In percent. Default : 75%') - self.parser.add_option('-c', '--critical', + self.parser.add_argument('-c', '--critical', dest="critical", help='Critical value for physical used memory. In percent. Must be ' 'superior to warning value. Default : 90%') - self.parser.add_option('-m', '--mount-points', + self.parser.add_argument('-m', '--mount-points', dest="mounts", help='comma separated list of mountpoints to check. Default all mount ' 'points except of tmpfs types') - self.parser.add_option('-U', '--unit', + self.parser.add_argument('-U', '--unit', dest="unit", help='Unit of Disk Space. B, KB, GB, TB. Default : B') diff --git a/schecks.py b/schecks.py index edd8146..5d18045 100644 --- a/schecks.py +++ b/schecks.py @@ -30,7 +30,7 @@ import os import sys import subprocess -import optparse +import argparse VERSION = 0.1 @@ -151,24 +151,24 @@ def get_warn_crit(s_warn, s_crit): def get_parser(): - parser = optparse.OptionParser( + parser = argparse.ArgumentParser( "%prog [options]", version="%prog " + str(VERSION)) - parser.add_option('-a', '--auth', default='paramiko', + parser.add_argument('-a', '--auth', default='paramiko', dest="ssh_auth", help='SSH Authentication. Can be paramiko or ssh') - parser.add_option('-H', '--hostname', default='', + parser.add_argument('-H', '--hostname', default='', dest="hostname", help='Hostname to connect to') - parser.add_option('-p', '--port', - dest="port", type="int", default=22, + parser.add_argument('-p', '--port', + dest="port", type=int, default=22, help='SSH port to connect to. Default : 22') - parser.add_option('-i', '--ssh-key', default=os.path.expanduser('~/.ssh/id_rsa'), + parser.add_argument('-i', '--ssh-key', default=os.path.expanduser('~/.ssh/id_rsa'), dest="ssh_key_file", help='SSH key file to use. By default will take ~/.ssh/id_rsa.') - parser.add_option('-u', '--user', default='shinken', + parser.add_argument('-u', '--user', default='shinken', dest="user", help='remote use to use. By default shinken.') - parser.add_option('-P', '--passphrase', default='', + parser.add_argument('-P', '--passphrase', default='', dest="passphrase", help='SSH key passphrase. By default will use void') - parser.add_option('-t', + parser.add_argument('-t', dest="modname", help='Check to load') - parser.add_option('-l', action='store_true', + parser.add_argument('-l', action='store_true', dest="listmod", help='List all checks available') return parser