checks.py 文件源码

python
阅读 21 收藏 0 点赞 0 评论 0

项目:fluffy 作者: m4ce 项目源码 文件源码
def run(self, name):
        """Run a check

        Raises:
            CheckError

        """

        if not self.exists(name):
            raise CheckNotFound("Check not found")

        if self._checks[name]['type'] == 'exec':
            proc = subprocess.Popen(
                self._checks[name]['command'], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
            try:
                out, err = proc.communicate(timeout=self._checks[name]['timeout'])
            except subprocess.TimeoutExpired as e:
                raise CheckError("Timed out")
            except Exception as e:
                raise CheckError(e.message)

            if proc.returncode:
                raise CheckError("Command failed with exitstatus {} [{}]".format(
                    proc.returncode, err.strip()))
        elif self._checks[name]['type'] == 'tcp':
            sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            sock.settimeout(self._checks[name]['timeout'])
            try:
                result = sock.connect_ex(
                    (self._checks[name]['host'], self._checks[name]['port']))
                sock.close()
                if result != 0:
                    raise Exception("Connection failed (Errno: {})".format(result))
            except socket.timeout as e:
                raise CheckError("Timed out")
            except Exception as e:
                raise CheckError(e.message)
            finally:
                sock.close()
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号