diff --git a/check_cpu_stats_by_ssh.py b/check_cpu_stats_by_ssh.py index 0d9ccf7..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__) @@ -103,27 +102,10 @@ 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 - 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 '' @@ -133,7 +115,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_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 91fd39d..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__) @@ -121,31 +120,10 @@ 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 - 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 '' @@ -155,7 +133,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_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 a7ae909..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 '' @@ -123,7 +106,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_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 9b5d665..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 @@ -80,42 +79,26 @@ 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.add_option('-m', '--measurement', +parser = schecks.get_parser() +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 '' @@ -130,7 +113,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 b6e3ad3..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 @@ -127,39 +126,22 @@ 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.add_option('-w', '--warning', +parser = schecks.get_parser() +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 '' @@ -177,7 +159,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 59945c7..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 @@ -142,34 +141,18 @@ 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.add_option('-w', '--warning', +parser = schecks.get_parser() +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 '' @@ -184,7 +167,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 e687be0..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__) @@ -138,39 +137,23 @@ 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.add_option('-w', '--warning', +parser = schecks.get_parser() +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 '' @@ -207,7 +190,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 3e84c78..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 @@ -91,40 +90,24 @@ 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.add_option('-w', '--warning', +parser = schecks.get_parser() +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 '' @@ -147,7 +130,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 2e58f17..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 @@ -79,40 +78,22 @@ 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.add_option('-e', '--exclude', +parser = schecks.get_parser() +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 '' @@ -123,7 +104,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 a3a8df4..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. @@ -66,37 +65,18 @@ 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.add_option('-w', '--warning', +parser = schecks.get_parser() +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 '' @@ -106,7 +86,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 a1a38ca..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: @@ -67,28 +66,13 @@ 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.add_option('-r', '--check_path', +parser = schecks.get_parser() +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: @@ -105,7 +89,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 a506675..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__) @@ -71,28 +70,11 @@ 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__': - # 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 '' @@ -102,7 +84,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 6d4c9c6..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__) @@ -67,22 +66,9 @@ 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.add_option('-c', '--critical', +parser = schecks.get_parser() +parser.add_argument('-c', '--critical', dest="critical", help='Critical value for uptime in seconds. Less means critical error. Default : 3600') @@ -90,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 @@ -108,7 +92,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 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 b86daac..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 @@ -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() @@ -149,22 +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('-H', '--hostname', default='', + parser.add_argument('-a', '--auth', default='paramiko', + dest="ssh_auth", help='SSH Authentication. Can be paramiko or ssh') + 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 @@ -218,3 +222,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