#!/usr/bin/python3
# -*- coding: utf-8 -*-

import subprocess
import argparse

class Main(Settings):
  """Contains the script."""

    def ping(self, host):
        """
        Ping one host and set status to a Zipato sensor.
        
        :param str host: Target host.
        :rtype: str
        :returns: Status message
        """
        if host in self.API_PING_HOSTS.keys():
            ep = self.API_PING_HOSTS[host]['ep']
            apikey = self.API_PING_HOSTS[host]['apikey']
        else:
            message = "Host '{}' has not been configured for 'ping'!"
            message = message.format(host)
            return self._json_response(message, 400)
        for i in range(self.PING_COUNT):
            command = "{}ping -c {} {}"
            command = command.format(self.PING_PATH, str(self.PING_COUNT), host)
            p = subprocess.Popen(
                command, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
                shell=True)
            stdout, stderr = p.communicate()
            result = re.match('.*0 received.*', str(stdout), re.DOTALL)
            ping_ok = result is None
            if ping_ok:
                break
            sleep(self.PING_INTERVAL)
        serial = self.ZIPATO_SERIAL
        # Set the status of a Zipato sensor to the ping status
        command = ("https://my.zipato.com/zipato-web/remoting/attribute/se"
                   "t?serial={}&ep={}&apiKey={}&state={}")
        if ping_ok:
            command = command.format(serial, ep, apikey, 'true')
        else:
            command = command.format(serial, ep, apikey, 'false')
        self.debug_print(10, command, 'zipatoserver', 'ZipatoServer', '_ping')
        r = requests.get(command)
        status_code = r.status_code
        if status_code == 200:
            message = 'Zipato ping status was updated'
        else:
            message = 'Zipato ping status could not be updated'
        return self._json_response(message, status_code)
      
      
      except:
            error_log = LogFile(self.ERROR_LOG)
            error_log.write(message)
            traceback_message = traceback.format_exc()
            error_log.write(traceback_message, date_time=False)
            error_log.close()
            if self.DEBUG > 0:
                return self._json_response(traceback_message, 500)
            return self._json_response('Internal system error!', 500)
        message_log = LogFile(self.MESSAGE_LOG)
        message_log.write(message)
        message_log.close()
        return result
      
    @staticmethod
    def _parse_command_line_options():
        """
        Parse options from the command line.
        
        :rtype: Namespace
        
        """
        debug_help = 'Debugging printout level.'
        host_help = 'Host or IP address to get ping status for.'
        description = 'Ping one host and set status to a Zipato sensor.'
        parser = argparse.ArgumentParser(description=description)
        parser.add_argument('-o, '--host', type=str, help=host_help, required=True)
        parser.add_argument('--debug', type=int, default=0,
                            help=debug_help, required=False)
        args = parser.parse_args()
        return args
      
    def run(self):
        """
        Run the script.
        
        """
        args = self._parse_command_line_options()
        Settings.load_settings_from_yaml(settings_path='/etc/')
        Settings.DEBUG = args.debug
        self._ping(host)

if __name__ == '__main__':
    main = Main()
    main.run()

@@ -525,17 +525,34 @@
  = %22
-Host '%7B%7D'
+ping host=%7B%7D, status='Host
  has
@@ -580,15 +580,13 @@
 for 
-'
 ping'
-!
 %22%0A  
@@ -1218,44 +1218,8 @@
 AL)%0A
-        serial = self.ZIPATO_SERIAL%0A
     
@@ -1424,528 +1424,520 @@
     
-if ping_ok:%0A            command = command.format(serial, ep, apikey, 'true')%0A        else:%0A            command = command.format(serial, ep, apikey, 'false')%0A        self.debug_print(10, command, 'zipatoserver', 'ZipatoServer', '_ping')%0A        r = requests.get(command)%0A        status_code = r.status_code%0A        if status_code == 200:%0A            message = 'Zipato ping status was updated'%0A        else:%0A            message = 'Zipato ping status could not be updated'%0A        return self._json_response(message
+zipato_connection = ZipatoConnection(self.ZIPATO_SERIAL)%0A        if ping_ok:%0A            zipato_connection.set_sensor_status(ep=ep, apikey=apikey, status='true')%0A        else:%0A            zipato_connection.set_sensor_status(ep=ep, apikey=apikey, status='false')%0A        self.debug_print(10, command, 'zipatoserver', 'ZipatoServer', '_ping')%0A        r = requests.get(command)%0A        zipato_connection.set_sensor_status(ep=ep, apikey=apikey, status)%0A        message = 'ping host=%7B%7D, status=%7B%7D'.format(host
 , status
 _cod
@@ -1936,13 +1936,8 @@
 atus
-_code
 )%0A  
@@ -2479,30 +2479,8 @@
 e()%0A
-        return result%0A
     

Update ping.py